unittest_udp_address.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. Copyright (c) 2018 Contributors as noted in the AUTHORS file
  3. This file is part of 0MQ.
  4. 0MQ is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. 0MQ is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include <unity.h>
  16. #include "../tests/testutil.hpp"
  17. #include "../unittests/unittest_resolver_common.hpp"
  18. #include <ip.hpp>
  19. #include <udp_address.hpp>
  20. void setUp ()
  21. {
  22. }
  23. void tearDown ()
  24. {
  25. }
  26. // Test an UDP address resolution. If 'dest_addr_' is NULL assume the
  27. // resolution is supposed to fail.
  28. static void test_resolve (bool bind_,
  29. int family_,
  30. const char *name_,
  31. const char *target_addr_,
  32. uint16_t expected_port_,
  33. const char *bind_addr_,
  34. bool multicast_)
  35. {
  36. if (family_ == AF_INET6 && !is_ipv6_available ()) {
  37. TEST_IGNORE_MESSAGE ("ipv6 is not available");
  38. }
  39. zmq::udp_address_t addr;
  40. int rc = addr.resolve (name_, bind_, family_ == AF_INET6);
  41. if (target_addr_ == NULL) {
  42. TEST_ASSERT_EQUAL (-1, rc);
  43. TEST_ASSERT_EQUAL (EINVAL, errno);
  44. return;
  45. }
  46. TEST_ASSERT_EQUAL (0, rc);
  47. TEST_ASSERT_EQUAL (multicast_, addr.is_mcast ());
  48. if (bind_addr_ == NULL) {
  49. // Bind ANY
  50. if (family_ == AF_INET) {
  51. bind_addr_ = "0.0.0.0";
  52. } else {
  53. bind_addr_ = "::";
  54. }
  55. }
  56. validate_address (family_, addr.target_addr (), target_addr_,
  57. expected_port_);
  58. validate_address (family_, addr.bind_addr (), bind_addr_, expected_port_);
  59. }
  60. static void test_resolve_bind (int family_,
  61. const char *name_,
  62. const char *dest_addr_,
  63. uint16_t expected_port_ = 0,
  64. const char *bind_addr_ = NULL,
  65. bool multicast_ = false)
  66. {
  67. test_resolve (true, family_, name_, dest_addr_, expected_port_, bind_addr_,
  68. multicast_);
  69. }
  70. static void test_resolve_connect (int family_,
  71. const char *name_,
  72. const char *dest_addr_,
  73. uint16_t expected_port_ = 0,
  74. const char *bind_addr_ = NULL,
  75. bool multicast_ = false)
  76. {
  77. test_resolve (false, family_, name_, dest_addr_, expected_port_, bind_addr_,
  78. multicast_);
  79. }
  80. static void test_resolve_ipv4_simple ()
  81. {
  82. test_resolve_connect (AF_INET, "127.0.0.1:5555", "127.0.0.1", 5555);
  83. }
  84. static void test_resolve_ipv6_simple ()
  85. {
  86. test_resolve_connect (AF_INET6, "[::1]:123", "::1", 123);
  87. }
  88. static void test_resolve_ipv4_bind ()
  89. {
  90. test_resolve_bind (AF_INET, "127.0.0.1:5555", "127.0.0.1", 5555,
  91. "127.0.0.1");
  92. }
  93. static void test_resolve_ipv6_bind ()
  94. {
  95. test_resolve_bind (AF_INET6, "[abcd::1234:1]:5555", "abcd::1234:1", 5555,
  96. "abcd::1234:1");
  97. }
  98. static void test_resolve_ipv4_bind_any ()
  99. {
  100. test_resolve_bind (AF_INET, "*:*", "0.0.0.0", 0, "0.0.0.0");
  101. }
  102. static void test_resolve_ipv6_bind_any ()
  103. {
  104. test_resolve_bind (AF_INET6, "*:*", "::", 0, "::");
  105. }
  106. static void test_resolve_ipv4_bind_anyport ()
  107. {
  108. test_resolve_bind (AF_INET, "127.0.0.1:*", "127.0.0.1", 0, "127.0.0.1");
  109. }
  110. static void test_resolve_ipv6_bind_anyport ()
  111. {
  112. test_resolve_bind (AF_INET6, "[1:2:3:4::5]:*", "1:2:3:4::5", 0,
  113. "1:2:3:4::5");
  114. }
  115. static void test_resolve_ipv4_bind_any_port ()
  116. {
  117. test_resolve_bind (AF_INET, "*:5555", "0.0.0.0", 5555, "0.0.0.0");
  118. }
  119. static void test_resolve_ipv6_bind_any_port ()
  120. {
  121. test_resolve_bind (AF_INET6, "*:5555", "::", 5555, "::");
  122. }
  123. static void test_resolve_ipv4_connect_any ()
  124. {
  125. // Cannot use wildcard for connection
  126. test_resolve_connect (AF_INET, "*:5555", NULL);
  127. }
  128. static void test_resolve_ipv6_connect_any ()
  129. {
  130. // Cannot use wildcard for connection
  131. test_resolve_connect (AF_INET6, "*:5555", NULL);
  132. }
  133. static void test_resolve_ipv4_connect_anyport ()
  134. {
  135. test_resolve_connect (AF_INET, "127.0.0.1:*", NULL);
  136. }
  137. static void test_resolve_ipv6_connect_anyport ()
  138. {
  139. test_resolve_connect (AF_INET6, "[::1]:*", NULL);
  140. }
  141. static void test_resolve_ipv4_connect_port0 ()
  142. {
  143. test_resolve_connect (AF_INET, "127.0.0.1:0", "127.0.0.1", 0);
  144. }
  145. static void test_resolve_ipv6_connect_port0 ()
  146. {
  147. test_resolve_connect (AF_INET6, "[2000:abcd::1]:0", "2000:abcd::1", 0);
  148. }
  149. static void test_resolve_ipv4_bind_mcast ()
  150. {
  151. test_resolve_bind (AF_INET, "239.0.0.1:1234", "239.0.0.1", 1234, "0.0.0.0",
  152. true);
  153. }
  154. static void test_resolve_ipv6_bind_mcast ()
  155. {
  156. test_resolve_bind (AF_INET6, "[ff00::1]:1234", "ff00::1", 1234, "::", true);
  157. }
  158. static void test_resolve_ipv4_connect_mcast ()
  159. {
  160. test_resolve_connect (AF_INET, "239.0.0.1:2222", "239.0.0.1", 2222, NULL,
  161. true);
  162. }
  163. static void test_resolve_ipv6_connect_mcast ()
  164. {
  165. test_resolve_connect (AF_INET6, "[ff00::1]:2222", "ff00::1", 2222, NULL,
  166. true);
  167. }
  168. static void test_resolve_ipv4_mcast_src_bind ()
  169. {
  170. test_resolve_bind (AF_INET, "127.0.0.1;230.2.8.12:5555", "230.2.8.12", 5555,
  171. "127.0.0.1", true);
  172. }
  173. static void test_resolve_ipv6_mcast_src_bind ()
  174. {
  175. if (!is_ipv6_available ()) {
  176. TEST_IGNORE_MESSAGE ("ipv6 is not available");
  177. }
  178. zmq::udp_address_t addr;
  179. int rc = addr.resolve ("[::1];[ffab::4]:5555", true, true);
  180. // For the time being this fails because we only support binding multicast
  181. // by interface name, not interface IP
  182. TEST_ASSERT_EQUAL (-1, rc);
  183. TEST_ASSERT_EQUAL (ENODEV, errno);
  184. }
  185. static void test_resolve_ipv4_mcast_src_bind_any ()
  186. {
  187. test_resolve_bind (AF_INET, "*;230.2.8.12:5555", "230.2.8.12", 5555,
  188. "0.0.0.0", true);
  189. }
  190. static void test_resolve_ipv6_mcast_src_bind_any ()
  191. {
  192. test_resolve_bind (AF_INET6, "*;[ffff::]:5555", "ffff::", 5555, "::", true);
  193. }
  194. static void test_resolve_ipv4_mcast_src_connect ()
  195. {
  196. test_resolve_connect (AF_INET, "8.9.10.11;230.2.8.12:5555", "230.2.8.12",
  197. 5555, "8.9.10.11", true);
  198. }
  199. static void test_resolve_ipv6_mcast_src_connect ()
  200. {
  201. if (!is_ipv6_available ()) {
  202. TEST_IGNORE_MESSAGE ("ipv6 is not available");
  203. }
  204. zmq::udp_address_t addr;
  205. int rc = addr.resolve ("[1:2:3::4];[ff01::1]:5555", false, true);
  206. // For the time being this fails because we only support binding multicast
  207. // by interface name, not interface IP
  208. TEST_ASSERT_EQUAL (-1, rc);
  209. TEST_ASSERT_EQUAL (ENODEV, errno);
  210. }
  211. static void test_resolve_ipv4_mcast_src_connect_any ()
  212. {
  213. test_resolve_connect (AF_INET, "*;230.2.8.12:5555", "230.2.8.12", 5555,
  214. "0.0.0.0", true);
  215. }
  216. static void test_resolve_ipv6_mcast_src_connect_any ()
  217. {
  218. test_resolve_connect (AF_INET6, "*;[ff10::1]:5555", "ff10::1", 5555,
  219. "::", true);
  220. }
  221. static void test_resolve_ipv4_mcast_src_bind_bad ()
  222. {
  223. test_resolve_bind (AF_INET, "127.0.0.1;1.2.3.4:5555", NULL);
  224. }
  225. static void test_resolve_ipv6_mcast_src_bind_bad ()
  226. {
  227. test_resolve_bind (AF_INET6, "[::1];[fe00::1]:5555", NULL);
  228. }
  229. static void test_resolve_ipv4_mcast_src_connect_bad ()
  230. {
  231. test_resolve_connect (AF_INET, "127.0.0.1;1.2.3.4:5555", NULL);
  232. }
  233. static void test_resolve_ipv6_mcast_src_connect_bad ()
  234. {
  235. test_resolve_connect (AF_INET6, "[::1];[fe00:1]:5555", NULL);
  236. }
  237. int main (void)
  238. {
  239. zmq::initialize_network ();
  240. setup_test_environment ();
  241. UNITY_BEGIN ();
  242. RUN_TEST (test_resolve_ipv4_simple);
  243. RUN_TEST (test_resolve_ipv6_simple);
  244. RUN_TEST (test_resolve_ipv4_bind);
  245. RUN_TEST (test_resolve_ipv6_bind);
  246. RUN_TEST (test_resolve_ipv4_bind_any);
  247. RUN_TEST (test_resolve_ipv6_bind_any);
  248. RUN_TEST (test_resolve_ipv4_bind_anyport);
  249. RUN_TEST (test_resolve_ipv6_bind_anyport);
  250. RUN_TEST (test_resolve_ipv4_bind_any_port);
  251. RUN_TEST (test_resolve_ipv6_bind_any_port);
  252. RUN_TEST (test_resolve_ipv4_connect_any);
  253. RUN_TEST (test_resolve_ipv6_connect_any);
  254. RUN_TEST (test_resolve_ipv4_connect_anyport);
  255. RUN_TEST (test_resolve_ipv6_connect_anyport);
  256. RUN_TEST (test_resolve_ipv4_connect_port0);
  257. RUN_TEST (test_resolve_ipv6_connect_port0);
  258. RUN_TEST (test_resolve_ipv4_bind_mcast);
  259. RUN_TEST (test_resolve_ipv6_bind_mcast);
  260. RUN_TEST (test_resolve_ipv4_connect_mcast);
  261. RUN_TEST (test_resolve_ipv6_connect_mcast);
  262. RUN_TEST (test_resolve_ipv4_mcast_src_bind);
  263. RUN_TEST (test_resolve_ipv6_mcast_src_bind);
  264. RUN_TEST (test_resolve_ipv4_mcast_src_bind_any);
  265. RUN_TEST (test_resolve_ipv6_mcast_src_bind_any);
  266. RUN_TEST (test_resolve_ipv4_mcast_src_connect);
  267. RUN_TEST (test_resolve_ipv6_mcast_src_connect);
  268. RUN_TEST (test_resolve_ipv4_mcast_src_connect_any);
  269. RUN_TEST (test_resolve_ipv6_mcast_src_connect_any);
  270. RUN_TEST (test_resolve_ipv4_mcast_src_bind_bad);
  271. RUN_TEST (test_resolve_ipv6_mcast_src_bind_bad);
  272. RUN_TEST (test_resolve_ipv4_mcast_src_connect_bad);
  273. RUN_TEST (test_resolve_ipv6_mcast_src_connect_bad);
  274. zmq::shutdown_network ();
  275. return UNITY_END ();
  276. }