unittest_ip_resolver.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  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 "../src/macros.hpp"
  17. #include "../tests/testutil.hpp"
  18. #include "../tests/testutil_unity.hpp"
  19. #include "../unittests/unittest_resolver_common.hpp"
  20. #include <ip_resolver.hpp>
  21. #include <ip.hpp>
  22. #ifndef _WIN32
  23. #include <sys/types.h>
  24. #include <sys/socket.h>
  25. #include <netdb.h>
  26. #endif
  27. void setUp ()
  28. {
  29. }
  30. void tearDown ()
  31. {
  32. }
  33. class test_ip_resolver_t ZMQ_FINAL : public zmq::ip_resolver_t
  34. {
  35. public:
  36. test_ip_resolver_t (zmq::ip_resolver_options_t opts_) :
  37. ip_resolver_t (opts_)
  38. {
  39. }
  40. protected:
  41. struct dns_lut_t
  42. {
  43. const char *hostname;
  44. const char *ipv4;
  45. const char *ipv6;
  46. };
  47. int do_getaddrinfo (const char *node_,
  48. const char *service_,
  49. const struct addrinfo *hints_,
  50. struct addrinfo **res_) ZMQ_FINAL
  51. {
  52. static const struct dns_lut_t dns_lut[] = {
  53. {"ip.zeromq.org", "10.100.0.1", "fdf5:d058:d656::1"},
  54. {"ipv4only.zeromq.org", "10.100.0.2", "::ffff:10.100.0.2"},
  55. {"ipv6only.zeromq.org", NULL, "fdf5:d058:d656::2"},
  56. };
  57. unsigned lut_len = sizeof (dns_lut) / sizeof (dns_lut[0]);
  58. struct addrinfo ai;
  59. TEST_ASSERT_NULL (service_);
  60. bool ipv6 = (hints_->ai_family == AF_INET6);
  61. bool no_dns = (hints_->ai_flags & AI_NUMERICHOST) != 0;
  62. const char *ip = NULL;
  63. if (!no_dns) {
  64. for (unsigned i = 0; i < lut_len; i++) {
  65. if (strcmp (dns_lut[i].hostname, node_) == 0) {
  66. if (ipv6) {
  67. ip = dns_lut[i].ipv6;
  68. } else {
  69. ip = dns_lut[i].ipv4;
  70. if (ip == NULL) {
  71. // No address associated with NAME
  72. return EAI_NODATA;
  73. }
  74. }
  75. }
  76. }
  77. }
  78. if (ip == NULL) {
  79. // No entry for 'node_' found in the LUT (or DNS is
  80. // forbidden), assume that it's a numeric IP address
  81. ip = node_;
  82. }
  83. // Call the real getaddrinfo implementation, making sure that it won't
  84. // attempt to resolve using DNS
  85. ai = *hints_;
  86. ai.ai_flags |= AI_NUMERICHOST;
  87. return zmq::ip_resolver_t::do_getaddrinfo (ip, NULL, &ai, res_);
  88. }
  89. unsigned int do_if_nametoindex (const char *ifname_) ZMQ_FINAL
  90. {
  91. static const char *dummy_interfaces[] = {
  92. "lo0",
  93. "eth0",
  94. "eth1",
  95. };
  96. unsigned lut_len =
  97. sizeof (dummy_interfaces) / sizeof (dummy_interfaces[0]);
  98. for (unsigned i = 0; i < lut_len; i++) {
  99. if (strcmp (dummy_interfaces[i], ifname_) == 0) {
  100. // The dummy index will be the position in the array + 1 (0 is
  101. // invalid)
  102. return i + 1;
  103. }
  104. }
  105. // Not found
  106. return 0;
  107. }
  108. };
  109. // Attempt a resolution and test the results. If 'expected_addr_' is NULL
  110. // assume that the resolution is meant to fail.
  111. //
  112. // On windows we can receive an IPv4 address even when an IPv6 is requested, if
  113. // we're in this situation then we compare to 'expected_addr_v4_failover_'
  114. // instead.
  115. static void test_resolve (zmq::ip_resolver_options_t opts_,
  116. const char *name_,
  117. const char *expected_addr_,
  118. uint16_t expected_port_ = 0,
  119. uint16_t expected_zone_ = 0,
  120. const char *expected_addr_v4_failover_ = NULL)
  121. {
  122. zmq::ip_addr_t addr;
  123. int family = opts_.ipv6 () ? AF_INET6 : AF_INET;
  124. if (family == AF_INET6 && !is_ipv6_available ()) {
  125. TEST_IGNORE_MESSAGE ("ipv6 is not available");
  126. }
  127. // Generate an invalid but well-defined 'ip_addr_t'. Avoids testing
  128. // uninitialized values if the code is buggy.
  129. memset (&addr, 0xba, sizeof (addr));
  130. test_ip_resolver_t resolver (opts_);
  131. int rc = resolver.resolve (&addr, name_);
  132. if (expected_addr_ == NULL) {
  133. // TODO also check the expected errno
  134. TEST_ASSERT_EQUAL (-1, rc);
  135. return;
  136. }
  137. TEST_ASSERT_SUCCESS_ERRNO (rc);
  138. validate_address (family, &addr, expected_addr_, expected_port_,
  139. expected_zone_, expected_addr_v4_failover_);
  140. }
  141. // Helper macro to define the v4/v6 function pairs
  142. #define MAKE_TEST_V4V6(_test) \
  143. static void _test##_ipv4 () { _test (false); } \
  144. \
  145. static void _test##_ipv6 () { _test (true); }
  146. static void test_bind_any (bool ipv6_)
  147. {
  148. zmq::ip_resolver_options_t resolver_opts;
  149. resolver_opts.bindable (true).expect_port (true).ipv6 (ipv6_);
  150. const char *expected = ipv6_ ? "::" : "0.0.0.0";
  151. test_resolve (resolver_opts, "*:*", expected, 0);
  152. }
  153. MAKE_TEST_V4V6 (test_bind_any)
  154. static void test_bind_any_port0 (bool ipv6_)
  155. {
  156. zmq::ip_resolver_options_t resolver_opts;
  157. resolver_opts.bindable (true).expect_port (true).ipv6 (ipv6_);
  158. // Should be equivalent to "*:*"
  159. const char *expected = ipv6_ ? "::" : "0.0.0.0";
  160. test_resolve (resolver_opts, "*:0", expected, 0);
  161. }
  162. MAKE_TEST_V4V6 (test_bind_any_port0)
  163. static void test_nobind_any (bool ipv6_)
  164. {
  165. zmq::ip_resolver_options_t resolver_opts;
  166. resolver_opts.expect_port (true).ipv6 (ipv6_);
  167. // Wildcard should be rejected if we're not looking for a
  168. // bindable address
  169. test_resolve (resolver_opts, "*:*", NULL);
  170. }
  171. MAKE_TEST_V4V6 (test_nobind_any)
  172. static void test_nobind_any_port (bool ipv6_)
  173. {
  174. zmq::ip_resolver_options_t resolver_opts;
  175. resolver_opts.expect_port (true).ipv6 (ipv6_);
  176. // Wildcard should be rejected if we're not looking for a
  177. // bindable address
  178. test_resolve (resolver_opts, "*:1234", NULL);
  179. }
  180. MAKE_TEST_V4V6 (test_nobind_any_port)
  181. static void test_nobind_addr_anyport (bool ipv6_)
  182. {
  183. zmq::ip_resolver_options_t resolver_opts;
  184. resolver_opts.expect_port (true).ipv6 (ipv6_);
  185. // Wildcard port should be rejected for non-bindable addresses
  186. test_resolve (resolver_opts, "127.0.0.1:*", NULL);
  187. }
  188. MAKE_TEST_V4V6 (test_nobind_addr_anyport)
  189. static void test_nobind_addr_port0 (bool ipv6_)
  190. {
  191. zmq::ip_resolver_options_t resolver_opts;
  192. resolver_opts.expect_port (true).ipv6 (ipv6_);
  193. // Connecting to port 0 is allowed, although it might not be massively
  194. // useful
  195. const char *expected = ipv6_ ? "::ffff:127.0.0.1" : "127.0.0.1";
  196. const char *fallback = ipv6_ ? "127.0.0.1" : NULL;
  197. test_resolve (resolver_opts, "127.0.0.1:0", expected, 0, 0, fallback);
  198. }
  199. MAKE_TEST_V4V6 (test_nobind_addr_port0)
  200. static void test_parse_ipv4_simple ()
  201. {
  202. zmq::ip_resolver_options_t resolver_opts;
  203. test_resolve (resolver_opts, "1.2.128.129", "1.2.128.129");
  204. }
  205. static void test_parse_ipv4_zero ()
  206. {
  207. zmq::ip_resolver_options_t resolver_opts;
  208. test_resolve (resolver_opts, "0.0.0.0", "0.0.0.0");
  209. }
  210. static void test_parse_ipv4_max ()
  211. {
  212. zmq::ip_resolver_options_t resolver_opts;
  213. test_resolve (resolver_opts, "255.255.255.255", "255.255.255.255");
  214. }
  215. static void test_parse_ipv4_brackets ()
  216. {
  217. zmq::ip_resolver_options_t resolver_opts;
  218. // Not particularly useful, but valid
  219. test_resolve (resolver_opts, "[1.2.128.129]", "1.2.128.129");
  220. }
  221. static void test_parse_ipv4_brackets_missingl ()
  222. {
  223. zmq::ip_resolver_options_t resolver_opts;
  224. test_resolve (resolver_opts, "1.2.128.129]", NULL);
  225. }
  226. static void test_parse_ipv4_brackets_missingr ()
  227. {
  228. zmq::ip_resolver_options_t resolver_opts;
  229. test_resolve (resolver_opts, "[1.2.128.129", NULL);
  230. }
  231. static void test_parse_ipv4_brackets_bad ()
  232. {
  233. zmq::ip_resolver_options_t resolver_opts;
  234. test_resolve (resolver_opts, "[1.2.128].129", NULL);
  235. }
  236. static void test_parse_ipv4_reject_port ()
  237. {
  238. zmq::ip_resolver_options_t resolver_opts;
  239. // No port expected, should be rejected
  240. test_resolve (resolver_opts, "1.2.128.129:123", NULL);
  241. }
  242. static void test_parse_ipv4_reject_any ()
  243. {
  244. zmq::ip_resolver_options_t resolver_opts;
  245. // No port expected, should be rejected
  246. test_resolve (resolver_opts, "1.2.128.129:*", NULL);
  247. }
  248. static void test_parse_ipv4_reject_ipv6 ()
  249. {
  250. zmq::ip_resolver_options_t resolver_opts;
  251. // No port expected, should be rejected
  252. test_resolve (resolver_opts, "::1", NULL);
  253. }
  254. static void test_parse_ipv4_port ()
  255. {
  256. zmq::ip_resolver_options_t resolver_opts;
  257. resolver_opts.expect_port (true);
  258. test_resolve (resolver_opts, "1.2.128.129:123", "1.2.128.129", 123);
  259. }
  260. static void test_parse_ipv4_port0 ()
  261. {
  262. zmq::ip_resolver_options_t resolver_opts;
  263. resolver_opts.expect_port (true);
  264. // Port 0 is accepted and is equivalent to *
  265. test_resolve (resolver_opts, "1.2.128.129:0", "1.2.128.129", 0);
  266. }
  267. static void test_parse_ipv4_port_garbage ()
  268. {
  269. zmq::ip_resolver_options_t resolver_opts;
  270. resolver_opts.expect_port (true);
  271. // The code doesn't validate that the port doesn't contain garbage
  272. test_resolve (resolver_opts, "1.2.3.4:567bad", "1.2.3.4", 567);
  273. }
  274. static void test_parse_ipv4_port_missing ()
  275. {
  276. zmq::ip_resolver_options_t resolver_opts;
  277. resolver_opts.expect_port (true);
  278. test_resolve (resolver_opts, "1.2.3.4", NULL);
  279. }
  280. static void test_parse_ipv4_port_bad ()
  281. {
  282. zmq::ip_resolver_options_t resolver_opts;
  283. resolver_opts.expect_port (true);
  284. test_resolve (resolver_opts, "1.2.3.4:bad", NULL);
  285. }
  286. static void test_parse_ipv4_port_brackets ()
  287. {
  288. zmq::ip_resolver_options_t resolver_opts;
  289. resolver_opts.expect_port (true);
  290. test_resolve (resolver_opts, "[192.168.1.1]:5555", "192.168.1.1", 5555);
  291. }
  292. static void test_parse_ipv4_port_brackets_bad ()
  293. {
  294. zmq::ip_resolver_options_t resolver_opts;
  295. resolver_opts.expect_port (true);
  296. test_resolve (resolver_opts, "[192.168.1.1:]5555", NULL);
  297. }
  298. static void test_parse_ipv4_port_brackets_bad2 ()
  299. {
  300. zmq::ip_resolver_options_t resolver_opts;
  301. resolver_opts.expect_port (true);
  302. test_resolve (resolver_opts, "[192.168.1.1:5555]", NULL);
  303. }
  304. static void test_parse_ipv4_wild_brackets_bad ()
  305. {
  306. zmq::ip_resolver_options_t resolver_opts;
  307. resolver_opts.expect_port (true);
  308. test_resolve (resolver_opts, "[192.168.1.1:*]", NULL);
  309. }
  310. static void test_parse_ipv4_port_ipv6_reject ()
  311. {
  312. zmq::ip_resolver_options_t resolver_opts;
  313. resolver_opts.expect_port (true);
  314. test_resolve (resolver_opts, "[::1]:1234", NULL);
  315. }
  316. static void test_parse_ipv6_simple ()
  317. {
  318. zmq::ip_resolver_options_t resolver_opts;
  319. resolver_opts.ipv6 (true);
  320. test_resolve (resolver_opts, "::1", "::1");
  321. }
  322. static void test_parse_ipv6_simple2 ()
  323. {
  324. zmq::ip_resolver_options_t resolver_opts;
  325. resolver_opts.ipv6 (true);
  326. test_resolve (resolver_opts, "abcd:1234::1:0:234", "abcd:1234::1:0:234");
  327. }
  328. static void test_parse_ipv6_zero ()
  329. {
  330. zmq::ip_resolver_options_t resolver_opts;
  331. resolver_opts.ipv6 (true);
  332. test_resolve (resolver_opts, "::", "::");
  333. }
  334. static void test_parse_ipv6_max ()
  335. {
  336. zmq::ip_resolver_options_t resolver_opts;
  337. resolver_opts.ipv6 (true);
  338. test_resolve (resolver_opts, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
  339. "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
  340. }
  341. static void test_parse_ipv6_brackets ()
  342. {
  343. zmq::ip_resolver_options_t resolver_opts;
  344. resolver_opts.ipv6 (true);
  345. test_resolve (resolver_opts, "[::1]", "::1");
  346. }
  347. static void test_parse_ipv6_brackets_missingl ()
  348. {
  349. zmq::ip_resolver_options_t resolver_opts;
  350. resolver_opts.ipv6 (true);
  351. test_resolve (resolver_opts, "::1]", NULL);
  352. }
  353. static void test_parse_ipv6_brackets_missingr ()
  354. {
  355. zmq::ip_resolver_options_t resolver_opts;
  356. resolver_opts.ipv6 (true);
  357. test_resolve (resolver_opts, "[::1", NULL);
  358. }
  359. static void test_parse_ipv6_brackets_bad ()
  360. {
  361. zmq::ip_resolver_options_t resolver_opts;
  362. resolver_opts.ipv6 (true);
  363. test_resolve (resolver_opts, "[abcd:1234::1:]0:234", NULL);
  364. }
  365. static void test_parse_ipv6_port ()
  366. {
  367. zmq::ip_resolver_options_t resolver_opts;
  368. resolver_opts.ipv6 (true).expect_port (true);
  369. test_resolve (resolver_opts, "[1234::1]:80", "1234::1", 80);
  370. }
  371. static void test_parse_ipv6_port_any ()
  372. {
  373. zmq::ip_resolver_options_t resolver_opts;
  374. resolver_opts.ipv6 (true).expect_port (true).bindable (true);
  375. test_resolve (resolver_opts, "[1234::1]:*", "1234::1", 0);
  376. }
  377. static void test_parse_ipv6_port_nobrackets ()
  378. {
  379. zmq::ip_resolver_options_t resolver_opts;
  380. resolver_opts.ipv6 (true).expect_port (true);
  381. // Should this be allowed? Seems error-prone but so far ZMQ accepts it.
  382. test_resolve (resolver_opts, "abcd:1234::1:0:234:123", "abcd:1234::1:0:234",
  383. 123);
  384. }
  385. static void test_parse_ipv4_in_ipv6 ()
  386. {
  387. zmq::ip_resolver_options_t resolver_opts;
  388. resolver_opts.ipv6 (true);
  389. // Parsing IPv4 should also work if an IPv6 is requested, it returns an
  390. // IPv6 with the IPv4 address embedded (except sometimes on Windows where
  391. // we end up with an IPv4 anyway)
  392. test_resolve (resolver_opts, "11.22.33.44", "::ffff:11.22.33.44", 0, 0,
  393. "11.22.33.44");
  394. }
  395. static void test_parse_ipv4_in_ipv6_port ()
  396. {
  397. zmq::ip_resolver_options_t resolver_opts;
  398. resolver_opts.ipv6 (true).expect_port (true);
  399. test_resolve (resolver_opts, "11.22.33.44:55", "::ffff:11.22.33.44", 55, 0,
  400. "11.22.33.44");
  401. }
  402. static void test_parse_ipv6_scope_int ()
  403. {
  404. zmq::ip_resolver_options_t resolver_opts;
  405. resolver_opts.ipv6 (true);
  406. test_resolve (resolver_opts, "3000:4:5::1:234%2", "3000:4:5::1:234", 0, 2);
  407. }
  408. static void test_parse_ipv6_scope_zero ()
  409. {
  410. zmq::ip_resolver_options_t resolver_opts;
  411. resolver_opts.ipv6 (true);
  412. test_resolve (resolver_opts, "3000:4:5::1:234%0", NULL);
  413. }
  414. static void test_parse_ipv6_scope_int_port ()
  415. {
  416. zmq::ip_resolver_options_t resolver_opts;
  417. resolver_opts.expect_port (true).ipv6 (true);
  418. test_resolve (resolver_opts, "3000:4:5::1:234%2:1111", "3000:4:5::1:234",
  419. 1111, 2);
  420. }
  421. static void test_parse_ipv6_scope_if ()
  422. {
  423. zmq::ip_resolver_options_t resolver_opts;
  424. resolver_opts.ipv6 (true);
  425. test_resolve (resolver_opts, "3000:4:5::1:234%eth1", "3000:4:5::1:234", 0,
  426. 3);
  427. }
  428. static void test_parse_ipv6_scope_if_port ()
  429. {
  430. zmq::ip_resolver_options_t resolver_opts;
  431. resolver_opts.expect_port (true).ipv6 (true);
  432. test_resolve (resolver_opts, "3000:4:5::1:234%eth0:8080", "3000:4:5::1:234",
  433. 8080, 2);
  434. }
  435. static void test_parse_ipv6_scope_if_port_brackets ()
  436. {
  437. zmq::ip_resolver_options_t resolver_opts;
  438. resolver_opts.expect_port (true).ipv6 (true);
  439. test_resolve (resolver_opts, "[3000:4:5::1:234%eth0]:8080",
  440. "3000:4:5::1:234", 8080, 2);
  441. }
  442. static void test_parse_ipv6_scope_badif ()
  443. {
  444. zmq::ip_resolver_options_t resolver_opts;
  445. resolver_opts.ipv6 (true);
  446. test_resolve (resolver_opts, "3000:4:5::1:234%bad0", NULL);
  447. }
  448. static void test_dns_ipv4_simple ()
  449. {
  450. zmq::ip_resolver_options_t resolver_opts;
  451. resolver_opts.allow_dns (true);
  452. test_resolve (resolver_opts, "ip.zeromq.org", "10.100.0.1");
  453. }
  454. static void test_dns_ipv4_only ()
  455. {
  456. zmq::ip_resolver_options_t resolver_opts;
  457. resolver_opts.allow_dns (true);
  458. test_resolve (resolver_opts, "ipv4only.zeromq.org", "10.100.0.2");
  459. }
  460. static void test_dns_ipv4_invalid ()
  461. {
  462. zmq::ip_resolver_options_t resolver_opts;
  463. resolver_opts.allow_dns (true);
  464. test_resolve (resolver_opts, "invalid.zeromq.org", NULL);
  465. }
  466. static void test_dns_ipv4_ipv6 ()
  467. {
  468. zmq::ip_resolver_options_t resolver_opts;
  469. resolver_opts.allow_dns (true);
  470. test_resolve (resolver_opts, "ipv6only.zeromq.org", NULL);
  471. }
  472. static void test_dns_ipv4_numeric ()
  473. {
  474. zmq::ip_resolver_options_t resolver_opts;
  475. resolver_opts.allow_dns (true);
  476. // Numeric IPs should still work
  477. test_resolve (resolver_opts, "5.4.3.2", "5.4.3.2");
  478. }
  479. static void test_dns_ipv4_port ()
  480. {
  481. zmq::ip_resolver_options_t resolver_opts;
  482. resolver_opts.expect_port (true).allow_dns (true);
  483. test_resolve (resolver_opts, "ip.zeromq.org:1234", "10.100.0.1", 1234);
  484. }
  485. static void test_dns_ipv6_simple ()
  486. {
  487. zmq::ip_resolver_options_t resolver_opts;
  488. resolver_opts.ipv6 (true).allow_dns (true);
  489. test_resolve (resolver_opts, "ip.zeromq.org", "fdf5:d058:d656::1");
  490. }
  491. static void test_dns_ipv6_only ()
  492. {
  493. zmq::ip_resolver_options_t resolver_opts;
  494. resolver_opts.ipv6 (true).allow_dns (true);
  495. test_resolve (resolver_opts, "ipv6only.zeromq.org", "fdf5:d058:d656::2");
  496. }
  497. static void test_dns_ipv6_invalid ()
  498. {
  499. zmq::ip_resolver_options_t resolver_opts;
  500. resolver_opts.ipv6 (true).allow_dns (true);
  501. test_resolve (resolver_opts, "invalid.zeromq.org", NULL);
  502. }
  503. static void test_dns_ipv6_ipv4 ()
  504. {
  505. zmq::ip_resolver_options_t resolver_opts;
  506. resolver_opts.ipv6 (true).allow_dns (true);
  507. // If a host doesn't have an IPv6 then it should resolve as an embedded v4
  508. // address in an IPv6
  509. test_resolve (resolver_opts, "ipv4only.zeromq.org", "::ffff:10.100.0.2");
  510. }
  511. static void test_dns_ipv6_numeric ()
  512. {
  513. zmq::ip_resolver_options_t resolver_opts;
  514. resolver_opts.ipv6 (true).allow_dns (true);
  515. // Numeric IPs should still work
  516. test_resolve (resolver_opts, "fdf5:d058:d656::1", "fdf5:d058:d656::1");
  517. }
  518. static void test_dns_ipv6_port ()
  519. {
  520. zmq::ip_resolver_options_t resolver_opts;
  521. resolver_opts.ipv6 (true).expect_port (true).allow_dns (true);
  522. test_resolve (resolver_opts, "ip.zeromq.org:1234", "fdf5:d058:d656::1",
  523. 1234);
  524. }
  525. void test_dns_brackets ()
  526. {
  527. zmq::ip_resolver_options_t resolver_opts;
  528. resolver_opts.allow_dns (true);
  529. test_resolve (resolver_opts, "[ip.zeromq.org]", "10.100.0.1");
  530. }
  531. void test_dns_brackets_bad ()
  532. {
  533. zmq::ip_resolver_options_t resolver_opts;
  534. resolver_opts.allow_dns (true);
  535. test_resolve (resolver_opts, "[ip.zeromq].org", NULL);
  536. }
  537. void test_dns_brackets_port ()
  538. {
  539. zmq::ip_resolver_options_t resolver_opts;
  540. resolver_opts.allow_dns (true);
  541. test_resolve (resolver_opts, "[ip.zeromq.org]:22", "10.100.0.1", 22);
  542. }
  543. void test_dns_brackets_port_bad ()
  544. {
  545. zmq::ip_resolver_options_t resolver_opts;
  546. resolver_opts.allow_dns (true);
  547. test_resolve (resolver_opts, "[ip.zeromq.org:22]", NULL);
  548. }
  549. void test_dns_deny (bool ipv6_)
  550. {
  551. zmq::ip_resolver_options_t resolver_opts;
  552. resolver_opts.allow_dns (false).ipv6 (ipv6_);
  553. // DNS resolution shouldn't work when disallowed
  554. test_resolve (resolver_opts, "ip.zeromq.org", NULL);
  555. }
  556. MAKE_TEST_V4V6 (test_dns_deny)
  557. void test_dns_ipv6_scope ()
  558. {
  559. zmq::ip_resolver_options_t resolver_opts;
  560. resolver_opts.allow_dns (true).ipv6 (true);
  561. // Not sure if that's very useful but you could technically add a scope
  562. // identifier to a hostname
  563. test_resolve (resolver_opts, "ip.zeromq.org%lo0", "fdf5:d058:d656::1", 0,
  564. 1);
  565. }
  566. void test_dns_ipv6_scope_port ()
  567. {
  568. zmq::ip_resolver_options_t resolver_opts;
  569. resolver_opts.allow_dns (true).expect_port (true).ipv6 (true);
  570. // Not sure if that's very useful but you could technically add a scope
  571. // identifier to a hostname
  572. test_resolve (resolver_opts, "ip.zeromq.org%lo0:4444", "fdf5:d058:d656::1",
  573. 4444, 1);
  574. }
  575. void test_dns_ipv6_scope_port_brackets ()
  576. {
  577. zmq::ip_resolver_options_t resolver_opts;
  578. resolver_opts.allow_dns (true).expect_port (true).ipv6 (true);
  579. test_resolve (resolver_opts, "[ip.zeromq.org%lo0]:4444",
  580. "fdf5:d058:d656::1", 4444, 1);
  581. }
  582. static void test_addr (int family_, const char *addr_, bool multicast_)
  583. {
  584. if (family_ == AF_INET6 && !is_ipv6_available ()) {
  585. TEST_IGNORE_MESSAGE ("ipv6 is not available");
  586. }
  587. zmq::ip_resolver_options_t resolver_opts;
  588. resolver_opts.ipv6 (family_ == AF_INET6);
  589. test_ip_resolver_t resolver (resolver_opts);
  590. zmq::ip_addr_t addr;
  591. TEST_ASSERT_SUCCESS_ERRNO (resolver.resolve (&addr, addr_));
  592. TEST_ASSERT_EQUAL (family_, addr.family ());
  593. TEST_ASSERT_EQUAL (multicast_, addr.is_multicast ());
  594. }
  595. static void test_addr_unicast_ipv4 ()
  596. {
  597. test_addr (AF_INET, "1.2.3.4", false);
  598. }
  599. static void test_addr_unicast_ipv6 ()
  600. {
  601. test_addr (AF_INET6, "abcd::1", false);
  602. }
  603. static void test_addr_multicast_ipv4 ()
  604. {
  605. test_addr (AF_INET, "230.1.2.3", true);
  606. }
  607. static void test_addr_multicast_ipv6 ()
  608. {
  609. test_addr (AF_INET6, "ffab::1234", true);
  610. }
  611. static void test_addr_multicast_ipv4_min ()
  612. {
  613. test_addr (AF_INET, "224.0.0.0", true);
  614. }
  615. static void test_addr_multicast_ipv6_min ()
  616. {
  617. test_addr (AF_INET6, "ff00::", true);
  618. }
  619. static void test_addr_multicast_ipv4_max ()
  620. {
  621. test_addr (AF_INET, "239.255.255.255", true);
  622. }
  623. static void test_addr_multicast_ipv6_max ()
  624. {
  625. test_addr (AF_INET6, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", true);
  626. }
  627. static void test_addr_multicast_ipv4_sub ()
  628. {
  629. test_addr (AF_INET, "223.255.255.255", false);
  630. }
  631. static void test_addr_multicast_ipv6_sub ()
  632. {
  633. test_addr (AF_INET6, "feff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", false);
  634. }
  635. static void test_addr_multicast_ipv4_over ()
  636. {
  637. test_addr (AF_INET, "240.0.0.0", false);
  638. }
  639. int main (void)
  640. {
  641. zmq::initialize_network ();
  642. setup_test_environment ();
  643. UNITY_BEGIN ();
  644. RUN_TEST (test_bind_any_ipv4);
  645. RUN_TEST (test_bind_any_ipv6);
  646. RUN_TEST (test_bind_any_port0_ipv4);
  647. RUN_TEST (test_bind_any_port0_ipv6);
  648. RUN_TEST (test_nobind_any_ipv4);
  649. RUN_TEST (test_nobind_any_ipv6);
  650. RUN_TEST (test_nobind_any_port_ipv4);
  651. RUN_TEST (test_nobind_any_port_ipv6);
  652. RUN_TEST (test_nobind_addr_anyport_ipv4);
  653. RUN_TEST (test_nobind_addr_anyport_ipv6);
  654. RUN_TEST (test_nobind_addr_port0_ipv4);
  655. RUN_TEST (test_nobind_addr_port0_ipv6);
  656. RUN_TEST (test_parse_ipv4_simple);
  657. RUN_TEST (test_parse_ipv4_zero);
  658. RUN_TEST (test_parse_ipv4_max);
  659. RUN_TEST (test_parse_ipv4_brackets);
  660. RUN_TEST (test_parse_ipv4_brackets_missingl);
  661. RUN_TEST (test_parse_ipv4_brackets_missingr);
  662. RUN_TEST (test_parse_ipv4_brackets_bad);
  663. RUN_TEST (test_parse_ipv4_reject_port);
  664. RUN_TEST (test_parse_ipv4_reject_any);
  665. RUN_TEST (test_parse_ipv4_reject_ipv6);
  666. RUN_TEST (test_parse_ipv4_port);
  667. RUN_TEST (test_parse_ipv4_port0);
  668. RUN_TEST (test_parse_ipv4_port_garbage);
  669. RUN_TEST (test_parse_ipv4_port_missing);
  670. RUN_TEST (test_parse_ipv4_port_bad);
  671. RUN_TEST (test_parse_ipv4_port_brackets);
  672. RUN_TEST (test_parse_ipv4_port_brackets_bad);
  673. RUN_TEST (test_parse_ipv4_port_brackets_bad2);
  674. RUN_TEST (test_parse_ipv4_wild_brackets_bad);
  675. RUN_TEST (test_parse_ipv4_port_ipv6_reject);
  676. RUN_TEST (test_parse_ipv6_simple);
  677. RUN_TEST (test_parse_ipv6_simple2);
  678. RUN_TEST (test_parse_ipv6_zero);
  679. RUN_TEST (test_parse_ipv6_max);
  680. RUN_TEST (test_parse_ipv6_brackets);
  681. RUN_TEST (test_parse_ipv6_brackets_missingl);
  682. RUN_TEST (test_parse_ipv6_brackets_missingr);
  683. RUN_TEST (test_parse_ipv6_brackets_bad);
  684. RUN_TEST (test_parse_ipv6_port);
  685. RUN_TEST (test_parse_ipv6_port_any);
  686. RUN_TEST (test_parse_ipv6_port_nobrackets);
  687. RUN_TEST (test_parse_ipv4_in_ipv6);
  688. RUN_TEST (test_parse_ipv4_in_ipv6_port);
  689. RUN_TEST (test_parse_ipv6_scope_int);
  690. RUN_TEST (test_parse_ipv6_scope_zero);
  691. RUN_TEST (test_parse_ipv6_scope_int_port);
  692. RUN_TEST (test_parse_ipv6_scope_if);
  693. RUN_TEST (test_parse_ipv6_scope_if_port);
  694. RUN_TEST (test_parse_ipv6_scope_if_port_brackets);
  695. RUN_TEST (test_parse_ipv6_scope_badif);
  696. RUN_TEST (test_dns_ipv4_simple);
  697. RUN_TEST (test_dns_ipv4_only);
  698. RUN_TEST (test_dns_ipv4_invalid);
  699. RUN_TEST (test_dns_ipv4_ipv6);
  700. RUN_TEST (test_dns_ipv4_numeric);
  701. RUN_TEST (test_dns_ipv4_port);
  702. RUN_TEST (test_dns_ipv6_simple);
  703. RUN_TEST (test_dns_ipv6_only);
  704. RUN_TEST (test_dns_ipv6_invalid);
  705. RUN_TEST (test_dns_ipv6_ipv4);
  706. RUN_TEST (test_dns_ipv6_numeric);
  707. RUN_TEST (test_dns_ipv6_port);
  708. RUN_TEST (test_dns_brackets);
  709. RUN_TEST (test_dns_brackets_bad);
  710. RUN_TEST (test_dns_deny_ipv4);
  711. RUN_TEST (test_dns_deny_ipv6);
  712. RUN_TEST (test_dns_ipv6_scope);
  713. RUN_TEST (test_dns_ipv6_scope_port);
  714. RUN_TEST (test_dns_ipv6_scope_port_brackets);
  715. RUN_TEST (test_addr_unicast_ipv4);
  716. RUN_TEST (test_addr_unicast_ipv6);
  717. RUN_TEST (test_addr_multicast_ipv4);
  718. RUN_TEST (test_addr_multicast_ipv6);
  719. RUN_TEST (test_addr_multicast_ipv4_min);
  720. RUN_TEST (test_addr_multicast_ipv6_min);
  721. RUN_TEST (test_addr_multicast_ipv4_max);
  722. RUN_TEST (test_addr_multicast_ipv6_max);
  723. RUN_TEST (test_addr_multicast_ipv4_sub);
  724. RUN_TEST (test_addr_multicast_ipv6_sub);
  725. RUN_TEST (test_addr_multicast_ipv4_over);
  726. zmq::shutdown_network ();
  727. return UNITY_END ();
  728. }