io_endpoint_unittest.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. // Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <config.h>
  15. #include <asiolink/io_endpoint.h>
  16. #include <asiolink/io_error.h>
  17. #include <gtest/gtest.h>
  18. #include <boost/shared_ptr.hpp>
  19. #include <sstream>
  20. #include <string>
  21. #include <sys/types.h>
  22. #include <sys/socket.h>
  23. #include <netdb.h>
  24. #include <string.h>
  25. using namespace isc::asiolink;
  26. namespace {
  27. typedef boost::shared_ptr<const IOEndpoint> ConstIOEndpointPtr;
  28. TEST(IOEndpointTest, createUDPv4) {
  29. ConstIOEndpointPtr ep(IOEndpoint::create(IPPROTO_UDP,
  30. IOAddress("192.0.2.1"), 53210));
  31. EXPECT_EQ("192.0.2.1", ep->getAddress().toText());
  32. EXPECT_EQ(53210, ep->getPort());
  33. EXPECT_EQ(AF_INET, ep->getFamily());
  34. EXPECT_EQ(AF_INET, ep->getAddress().getFamily());
  35. EXPECT_EQ(IPPROTO_UDP, ep->getProtocol());
  36. }
  37. TEST(IOEndpointTest, createTCPv4) {
  38. ConstIOEndpointPtr ep(IOEndpoint::create(IPPROTO_TCP,
  39. IOAddress("192.0.2.1"), 5301));
  40. EXPECT_EQ("192.0.2.1", ep->getAddress().toText());
  41. EXPECT_EQ(5301, ep->getPort());
  42. EXPECT_EQ(AF_INET, ep->getFamily());
  43. EXPECT_EQ(AF_INET, ep->getAddress().getFamily());
  44. EXPECT_EQ(IPPROTO_TCP, ep->getProtocol());
  45. }
  46. TEST(IOEndpointTest, createUDPv6) {
  47. ConstIOEndpointPtr ep(IOEndpoint::create(IPPROTO_UDP,
  48. IOAddress("2001:db8::1234"),
  49. 5302));
  50. EXPECT_EQ("2001:db8::1234", ep->getAddress().toText());
  51. EXPECT_EQ(5302, ep->getPort());
  52. EXPECT_EQ(AF_INET6, ep->getFamily());
  53. EXPECT_EQ(AF_INET6, ep->getAddress().getFamily());
  54. EXPECT_EQ(IPPROTO_UDP, ep->getProtocol());
  55. }
  56. TEST(IOEndpointTest, createTCPv6) {
  57. ConstIOEndpointPtr ep(IOEndpoint::create(IPPROTO_TCP,
  58. IOAddress("2001:db8::1234"),
  59. 5303));
  60. EXPECT_EQ("2001:db8::1234", ep->getAddress().toText());
  61. EXPECT_EQ(5303, ep->getPort());
  62. EXPECT_EQ(AF_INET6, ep->getFamily());
  63. EXPECT_EQ(AF_INET6, ep->getAddress().getFamily());
  64. EXPECT_EQ(IPPROTO_TCP, ep->getProtocol());
  65. }
  66. TEST(IOEndpointTest, equality) {
  67. std::vector<ConstIOEndpointPtr> epv;
  68. epv.push_back(ConstIOEndpointPtr(
  69. IOEndpoint::create(IPPROTO_TCP,
  70. IOAddress("2001:db8::1234"), 5303)));
  71. epv.push_back(ConstIOEndpointPtr(
  72. IOEndpoint::create(IPPROTO_UDP,
  73. IOAddress("2001:db8::1234"), 5303)));
  74. epv.push_back(ConstIOEndpointPtr(
  75. IOEndpoint::create(IPPROTO_TCP,
  76. IOAddress("2001:db8::1234"), 5304)));
  77. epv.push_back(ConstIOEndpointPtr(
  78. IOEndpoint::create(IPPROTO_UDP,
  79. IOAddress("2001:db8::1234"), 5304)));
  80. epv.push_back(ConstIOEndpointPtr(
  81. IOEndpoint::create(IPPROTO_TCP,
  82. IOAddress("2001:db8::1235"), 5303)));
  83. epv.push_back(ConstIOEndpointPtr(
  84. IOEndpoint::create(IPPROTO_UDP,
  85. IOAddress("2001:db8::1235"), 5303)));
  86. epv.push_back(ConstIOEndpointPtr(
  87. IOEndpoint::create(IPPROTO_TCP,
  88. IOAddress("2001:db8::1235"), 5304)));
  89. epv.push_back(ConstIOEndpointPtr(
  90. IOEndpoint::create(IPPROTO_UDP,
  91. IOAddress("2001:db8::1235"), 5304)));
  92. epv.push_back(ConstIOEndpointPtr(
  93. IOEndpoint::create(IPPROTO_TCP,
  94. IOAddress("192.0.2.1"), 5303)));
  95. epv.push_back(ConstIOEndpointPtr(
  96. IOEndpoint::create(IPPROTO_UDP,
  97. IOAddress("192.0.2.1"), 5303)));
  98. epv.push_back(ConstIOEndpointPtr(
  99. IOEndpoint::create(IPPROTO_TCP,
  100. IOAddress("192.0.2.1"), 5304)));
  101. epv.push_back(ConstIOEndpointPtr(
  102. IOEndpoint::create(IPPROTO_UDP,
  103. IOAddress("192.0.2.1"), 5304)));
  104. epv.push_back(ConstIOEndpointPtr(
  105. IOEndpoint::create(IPPROTO_TCP,
  106. IOAddress("192.0.2.2"), 5303)));
  107. epv.push_back(ConstIOEndpointPtr(
  108. IOEndpoint::create(IPPROTO_UDP,
  109. IOAddress("192.0.2.2"), 5303)));
  110. epv.push_back(ConstIOEndpointPtr(
  111. IOEndpoint::create(IPPROTO_TCP,
  112. IOAddress("192.0.2.2"), 5304)));
  113. epv.push_back(ConstIOEndpointPtr(
  114. IOEndpoint::create(IPPROTO_UDP,
  115. IOAddress("192.0.2.2"), 5304)));
  116. for (size_t i = 0; i < epv.size(); ++i) {
  117. for (size_t j = 0; j < epv.size(); ++j) {
  118. if (i != j) {
  119. // We use EXPECT_TRUE/FALSE instead of _EQ here, since
  120. // _EQ requires there is an operator<< as well
  121. EXPECT_FALSE(*epv[i] == *epv[j]);
  122. EXPECT_TRUE(*epv[i] != *epv[j]);
  123. }
  124. }
  125. }
  126. // Create a second array with exactly the same values. We use create()
  127. // again to make sure we get different endpoints
  128. std::vector<ConstIOEndpointPtr> epv2;
  129. epv2.push_back(ConstIOEndpointPtr(
  130. IOEndpoint::create(IPPROTO_TCP,
  131. IOAddress("2001:db8::1234"), 5303)));
  132. epv2.push_back(ConstIOEndpointPtr(
  133. IOEndpoint::create(IPPROTO_UDP,
  134. IOAddress("2001:db8::1234"), 5303)));
  135. epv2.push_back(ConstIOEndpointPtr(
  136. IOEndpoint::create(IPPROTO_TCP,
  137. IOAddress("2001:db8::1234"), 5304)));
  138. epv2.push_back(ConstIOEndpointPtr(
  139. IOEndpoint::create(IPPROTO_UDP,
  140. IOAddress("2001:db8::1234"), 5304)));
  141. epv2.push_back(ConstIOEndpointPtr(
  142. IOEndpoint::create(IPPROTO_TCP,
  143. IOAddress("2001:db8::1235"), 5303)));
  144. epv2.push_back(ConstIOEndpointPtr(
  145. IOEndpoint::create(IPPROTO_UDP,
  146. IOAddress("2001:db8::1235"), 5303)));
  147. epv2.push_back(ConstIOEndpointPtr(
  148. IOEndpoint::create(IPPROTO_TCP,
  149. IOAddress("2001:db8::1235"), 5304)));
  150. epv2.push_back(ConstIOEndpointPtr(
  151. IOEndpoint::create(IPPROTO_UDP,
  152. IOAddress("2001:db8::1235"), 5304)));
  153. epv2.push_back(ConstIOEndpointPtr(
  154. IOEndpoint::create(IPPROTO_TCP,
  155. IOAddress("192.0.2.1"), 5303)));
  156. epv2.push_back(ConstIOEndpointPtr(
  157. IOEndpoint::create(IPPROTO_UDP,
  158. IOAddress("192.0.2.1"), 5303)));
  159. epv2.push_back(ConstIOEndpointPtr(
  160. IOEndpoint::create(IPPROTO_TCP, IOAddress("192.0.2.1"),
  161. 5304)));
  162. epv2.push_back(ConstIOEndpointPtr(
  163. IOEndpoint::create(IPPROTO_UDP, IOAddress("192.0.2.1"),
  164. 5304)));
  165. epv2.push_back(ConstIOEndpointPtr(
  166. IOEndpoint::create(IPPROTO_TCP, IOAddress("192.0.2.2"),
  167. 5303)));
  168. epv2.push_back(ConstIOEndpointPtr(
  169. IOEndpoint::create(IPPROTO_UDP, IOAddress("192.0.2.2"),
  170. 5303)));
  171. epv2.push_back(ConstIOEndpointPtr(
  172. IOEndpoint::create(IPPROTO_TCP, IOAddress("192.0.2.2"),
  173. 5304)));
  174. epv2.push_back(ConstIOEndpointPtr(
  175. IOEndpoint::create(IPPROTO_UDP, IOAddress("192.0.2.2"),
  176. 5304)));
  177. for (size_t i = 0; i < epv.size(); ++i) {
  178. EXPECT_TRUE(*epv[i] == *epv2[i]);
  179. EXPECT_FALSE(*epv[i] != *epv2[i]);
  180. }
  181. }
  182. TEST(IOEndpointTest, createIPProto) {
  183. EXPECT_THROW(IOEndpoint::create(IPPROTO_IP, IOAddress("192.0.2.1"),
  184. 53210)->getAddress().toText(),
  185. IOError);
  186. }
  187. void
  188. sockAddrMatch(const struct sockaddr& actual_sa,
  189. const char* const expected_addr_text,
  190. const char* const expected_port_text)
  191. {
  192. struct addrinfo hints;
  193. memset(&hints, 0, sizeof(hints));
  194. hints.ai_family = AF_UNSPEC;
  195. hints.ai_socktype = SOCK_DGRAM; // this shouldn't matter
  196. hints.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV;
  197. struct addrinfo* res;
  198. ASSERT_EQ(0, getaddrinfo(expected_addr_text, expected_port_text, &hints,
  199. &res));
  200. EXPECT_EQ(res->ai_family, actual_sa.sa_family);
  201. #ifdef HAVE_SA_LEN
  202. // ASIO doesn't seem to set sa_len, so we set it to the expected value
  203. res->ai_addr->sa_len = actual_sa.sa_len;
  204. #endif
  205. EXPECT_EQ(0, memcmp(res->ai_addr, &actual_sa, res->ai_addrlen));
  206. freeaddrinfo(res);
  207. }
  208. TEST(IOEndpointTest, getSockAddr) {
  209. // UDP/IPv4
  210. ConstIOEndpointPtr ep(IOEndpoint::create(IPPROTO_UDP,
  211. IOAddress("192.0.2.1"), 53210));
  212. sockAddrMatch(ep->getSockAddr(), "192.0.2.1", "53210");
  213. // UDP/IPv6
  214. ep.reset(IOEndpoint::create(IPPROTO_UDP, IOAddress("2001:db8::53"), 53));
  215. sockAddrMatch(ep->getSockAddr(), "2001:db8::53", "53");
  216. // TCP/IPv4
  217. ep.reset(IOEndpoint::create(IPPROTO_TCP, IOAddress("192.0.2.2"), 53211));
  218. sockAddrMatch(ep->getSockAddr(), "192.0.2.2", "53211");
  219. // TCP/IPv6
  220. ep.reset(IOEndpoint::create(IPPROTO_UDP, IOAddress("2001:db8::5300"), 35));
  221. sockAddrMatch(ep->getSockAddr(), "2001:db8::5300", "35");
  222. }
  223. // A faked IOEndpoint for an uncommon address family. It wouldn't be possible
  224. // to create via the normal factory, so we define a special derived class
  225. // for it.
  226. class TestIOEndpoint : public IOEndpoint {
  227. virtual IOAddress getAddress() const {
  228. return IOAddress("2001:db8::bad:add");
  229. }
  230. virtual uint16_t getPort() const { return (42); }
  231. virtual short getProtocol() const { return (IPPROTO_UDP); }
  232. virtual short getFamily() const { return (AF_UNSPEC); }
  233. virtual const struct sockaddr& getSockAddr() const {
  234. static struct sockaddr sa_placeholder;
  235. return (sa_placeholder);
  236. }
  237. };
  238. void
  239. checkEndpointText(const std::string& expected, const IOEndpoint& ep) {
  240. std::ostringstream oss;
  241. oss << ep;
  242. EXPECT_EQ(expected, oss.str());
  243. }
  244. // test operator<<. We simply confirm it appends the result of toText().
  245. TEST(IOEndpointTest, LeftShiftOperator) {
  246. // UDP/IPv4
  247. ConstIOEndpointPtr ep(IOEndpoint::create(IPPROTO_UDP,
  248. IOAddress("192.0.2.1"), 53210));
  249. checkEndpointText("192.0.2.1:53210", *ep);
  250. // UDP/IPv6
  251. ep.reset(IOEndpoint::create(IPPROTO_UDP, IOAddress("2001:db8::53"), 53));
  252. checkEndpointText("[2001:db8::53]:53", *ep);
  253. // Same for TCP: shouldn't be different
  254. ep.reset(IOEndpoint::create(IPPROTO_TCP, IOAddress("192.0.2.1"), 53210));
  255. checkEndpointText("192.0.2.1:53210", *ep);
  256. ep.reset(IOEndpoint::create(IPPROTO_TCP, IOAddress("2001:db8::53"), 53));
  257. checkEndpointText("[2001:db8::53]:53", *ep);
  258. // Uncommon address family. The actual behavior doesn't matter much
  259. // in practice, but we check such input doesn't make it crash.
  260. // We explicitly instantiate the test EP because otherwise some compilers
  261. // would be confused and complain.
  262. TestIOEndpoint test_ep;
  263. checkEndpointText("2001:db8::bad:add:42", test_ep);
  264. }
  265. }