PageRenderTime 31ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llmessage/tests/llhost_test.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 251 lines | 172 code | 34 blank | 45 comment | 30 complexity | e8b9e1321d904d45d15fa88eb2ab954f MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llhost_test.cpp
  3. * @author Adroit
  4. * @date 2007-02
  5. * @brief llhost test cases.
  6. *
  7. * $LicenseInfo:firstyear=2007&license=viewerlgpl$
  8. * Second Life Viewer Source Code
  9. * Copyright (C) 2010, Linden Research, Inc.
  10. *
  11. * This library is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation;
  14. * version 2.1 of the License only.
  15. *
  16. * This library is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with this library; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. *
  25. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  26. * $/LicenseInfo$
  27. */
  28. #include "linden_common.h"
  29. #include "../llhost.h"
  30. #include "../test/lltut.h"
  31. namespace tut
  32. {
  33. struct host_data
  34. {
  35. };
  36. typedef test_group<host_data> host_test;
  37. typedef host_test::object host_object;
  38. tut::host_test host_testcase("LLHost");
  39. template<> template<>
  40. void host_object::test<1>()
  41. {
  42. LLHost host;
  43. ensure("IP address is not NULL", (0 == host.getAddress()) && (0 == host.getPort()) && !host.isOk());
  44. }
  45. template<> template<>
  46. void host_object::test<2>()
  47. {
  48. U32 ip_addr = 0xc098017d;
  49. U32 port = 8080;
  50. LLHost host(ip_addr, port);
  51. ensure("IP address is invalid", ip_addr == host.getAddress());
  52. ensure("Port Number is invalid", port == host.getPort());
  53. ensure("IP address and port number both should be ok", host.isOk());
  54. }
  55. template<> template<>
  56. void host_object::test<3>()
  57. {
  58. const char* str = "192.168.1.1";
  59. U32 port = 8080;
  60. LLHost host(str, port);
  61. ensure("IP address could not be processed", (host.getAddress() == ip_string_to_u32(str)));
  62. ensure("Port Number is invalid", (port == host.getPort()));
  63. }
  64. template<> template<>
  65. void host_object::test<4>()
  66. {
  67. U32 ip = ip_string_to_u32("192.168.1.1");
  68. U32 port = 22;
  69. U64 ip_port = (((U64) ip) << 32) | port;
  70. LLHost host(ip_port);
  71. ensure("IP address is invalid", ip == host.getAddress());
  72. ensure("Port Number is invalid", port == host.getPort());
  73. }
  74. template<> template<>
  75. void host_object::test<5>()
  76. {
  77. std::string ip_port_string = "192.168.1.1:8080";
  78. U32 ip = ip_string_to_u32("192.168.1.1");
  79. U32 port = 8080;
  80. LLHost host(ip_port_string);
  81. ensure("IP address from IP:port is invalid", ip == host.getAddress());
  82. ensure("Port Number from from IP:port is invalid", port == host.getPort());
  83. }
  84. template<> template<>
  85. void host_object::test<6>()
  86. {
  87. U32 ip = 0xc098017d, port = 8080;
  88. LLHost host;
  89. host.set(ip,port);
  90. ensure("IP address is invalid", (ip == host.getAddress()));
  91. ensure("Port Number is invalid", (port == host.getPort()));
  92. }
  93. template<> template<>
  94. void host_object::test<7>()
  95. {
  96. const char* str = "192.168.1.1";
  97. U32 port = 8080, ip;
  98. LLHost host;
  99. host.set(str,port);
  100. ip = ip_string_to_u32(str);
  101. ensure("IP address is invalid", (ip == host.getAddress()));
  102. ensure("Port Number is invalid", (port == host.getPort()));
  103. str = "64.233.187.99";
  104. ip = ip_string_to_u32(str);
  105. host.setAddress(str);
  106. ensure("IP address is invalid", (ip == host.getAddress()));
  107. ip = 0xc098017b;
  108. host.setAddress(ip);
  109. ensure("IP address is invalid", (ip == host.getAddress()));
  110. // should still use the old port
  111. ensure("Port Number is invalid", (port == host.getPort()));
  112. port = 8084;
  113. host.setPort(port);
  114. ensure("Port Number is invalid", (port == host.getPort()));
  115. // should still use the old address
  116. ensure("IP address is invalid", (ip == host.getAddress()));
  117. }
  118. template<> template<>
  119. void host_object::test<8>()
  120. {
  121. const std::string str("192.168.1.1");
  122. U32 port = 8080;
  123. LLHost host;
  124. host.set(str,port);
  125. std::string ip_string = host.getIPString();
  126. ensure("Function Failed", (ip_string == str));
  127. std::string ip_string_port = host.getIPandPort();
  128. ensure("Function Failed", (ip_string_port == "192.168.1.1:8080"));
  129. }
  130. // getHostName() and setHostByName
  131. template<> template<>
  132. void host_object::test<9>()
  133. {
  134. skip("this test is flaky, but we should figure out why...");
  135. // skip("setHostByName(\"google.com\"); getHostName() -> (e.g.) \"yx-in-f100.1e100.net\"");
  136. std::string hostStr = "lindenlab.com";
  137. LLHost host;
  138. host.setHostByName(hostStr);
  139. // reverse DNS will likely result in appending of some
  140. // sub-domain to the main hostname. so look for
  141. // the main domain name and not do the exact compare
  142. std::string hostname = host.getHostName();
  143. try
  144. {
  145. ensure("getHostName failed", hostname.find(hostStr) != std::string::npos);
  146. }
  147. catch (const std::exception&)
  148. {
  149. std::cerr << "set '" << hostStr << "'; reported '" << hostname << "'" << std::endl;
  150. throw;
  151. }
  152. }
  153. // setHostByName for dotted IP
  154. template<> template<>
  155. void host_object::test<10>()
  156. {
  157. std::string hostStr = "64.233.167.99";
  158. LLHost host;
  159. host.setHostByName(hostStr);
  160. ensure("SetHostByName for dotted IP Address failed", host.getAddress() == ip_string_to_u32(hostStr.c_str()));
  161. }
  162. template<> template<>
  163. void host_object::test<11>()
  164. {
  165. LLHost host1(0xc098017d, 8080);
  166. LLHost host2 = host1;
  167. ensure("Both IP addresses are not same", (host1.getAddress() == host2.getAddress()));
  168. ensure("Both port numbers are not same", (host1.getPort() == host2.getPort()));
  169. }
  170. template<> template<>
  171. void host_object::test<12>()
  172. {
  173. LLHost host1("192.168.1.1", 8080);
  174. std::string str1 = "192.168.1.1:8080";
  175. std::ostringstream stream;
  176. stream << host1;
  177. ensure("Operator << failed", ( stream.str()== str1));
  178. // There is no istream >> llhost operator.
  179. //std::istringstream is(stream.str());
  180. //LLHost host2;
  181. //is >> host2;
  182. //ensure("Operator >> failed. Not compatible with <<", host1 == host2);
  183. }
  184. // operators ==, !=, <
  185. template<> template<>
  186. void host_object::test<13>()
  187. {
  188. U32 ip_addr = 0xc098017d;
  189. U32 port = 8080;
  190. LLHost host1(ip_addr, port);
  191. LLHost host2(ip_addr, port);
  192. ensure("operator== failed", host1 == host2);
  193. // change port
  194. host2.setPort(7070);
  195. ensure("operator!= failed", host1 != host2);
  196. // set port back to 8080 and change IP address now
  197. host2.setPort(8080);
  198. host2.setAddress(ip_addr+10);
  199. ensure("operator!= failed", host1 != host2);
  200. ensure("operator< failed", host1 < host2);
  201. // set IP address back to same value and change port
  202. host2.setAddress(ip_addr);
  203. host2.setPort(host1.getPort() + 10);
  204. ensure("operator< failed", host1 < host2);
  205. }
  206. // invalid ip address string
  207. template<> template<>
  208. void host_object::test<14>()
  209. {
  210. LLHost host1("10.0.1.2", 6143);
  211. ensure("10.0.1.2 should be a valid address", host1.isOk());
  212. LLHost host2("booger-brains", 6143);
  213. ensure("booger-brains should be an invalid ip addess", !host2.isOk());
  214. LLHost host3("255.255.255.255", 6143);
  215. ensure("255.255.255.255 should be valid broadcast address", host3.isOk());
  216. }
  217. }