test_addr.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2009, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #include "orconfig.h"
  6. #include "or.h"
  7. #include "test.h"
  8. #define _test_op_ip6(a,op,b,e1,e2) \
  9. STMT_BEGIN \
  10. tt_assert_test_fmt_type(a,b,e1" "#op" "e2,struct in6_addr*, \
  11. (memcmp(_val1->s6_addr, _val2->s6_addr, 16) op 0), \
  12. char *, "%s", \
  13. { int i; char *cp; \
  14. cp = _print = tor_malloc(64); \
  15. for (i=0;i<16;++i) { \
  16. tor_snprintf(cp, 3,"%02x", (unsigned)_value->s6_addr[i]);\
  17. cp += 2; \
  18. if (i != 15) *cp++ = ':'; \
  19. } \
  20. }, { tor_free(_print); } \
  21. ); \
  22. STMT_END
  23. /** Helper: Assert that two strings both decode as IPv6 addresses with
  24. * tor_inet_pton(), and both decode to the same address. */
  25. #define test_pton6_same(a,b) STMT_BEGIN \
  26. test_eq(tor_inet_pton(AF_INET6, a, &a1), 1); \
  27. test_eq(tor_inet_pton(AF_INET6, b, &a2), 1); \
  28. _test_op_ip6(&a1,==,&a2,#a,#b); \
  29. STMT_END
  30. /** Helper: Assert that <b>a</b> is recognized as a bad IPv6 address by
  31. * tor_inet_pton(). */
  32. #define test_pton6_bad(a) \
  33. test_eq(0, tor_inet_pton(AF_INET6, a, &a1))
  34. /** Helper: assert that <b>a</b>, when parsed by tor_inet_pton() and displayed
  35. * with tor_inet_ntop(), yields <b>b</b>. Also assert that <b>b</b> parses to
  36. * the same value as <b>a</b>. */
  37. #define test_ntop6_reduces(a,b) STMT_BEGIN \
  38. test_eq(tor_inet_pton(AF_INET6, a, &a1), 1); \
  39. test_streq(tor_inet_ntop(AF_INET6, &a1, buf, sizeof(buf)), b); \
  40. test_eq(tor_inet_pton(AF_INET6, b, &a2), 1); \
  41. _test_op_ip6(&a1, ==, &a2, a, b); \
  42. STMT_END
  43. /** Helper: assert that <b>a</b> parses by tor_inet_pton() into a address that
  44. * passes tor_addr_is_internal() with <b>for_listening</b>. */
  45. #define test_internal_ip(a,for_listening) STMT_BEGIN \
  46. test_eq(tor_inet_pton(AF_INET6, a, &t1.addr.in6_addr), 1); \
  47. t1.family = AF_INET6; \
  48. if (!tor_addr_is_internal(&t1, for_listening)) \
  49. test_fail_msg( a "was not internal."); \
  50. STMT_END
  51. /** Helper: assert that <b>a</b> parses by tor_inet_pton() into a address that
  52. * does not pass tor_addr_is_internal() with <b>for_listening</b>. */
  53. #define test_external_ip(a,for_listening) STMT_BEGIN \
  54. test_eq(tor_inet_pton(AF_INET6, a, &t1.addr.in6_addr), 1); \
  55. t1.family = AF_INET6; \
  56. if (tor_addr_is_internal(&t1, for_listening)) \
  57. test_fail_msg(a "was not external."); \
  58. STMT_END
  59. /** Helper: Assert that <b>a</b> and <b>b</b>, when parsed by
  60. * tor_inet_pton(), give addresses that compare in the order defined by
  61. * <b>op</b> with tor_addr_compare(). */
  62. #define test_addr_compare(a, op, b) STMT_BEGIN \
  63. test_eq(tor_inet_pton(AF_INET6, a, &t1.addr.in6_addr), 1); \
  64. test_eq(tor_inet_pton(AF_INET6, b, &t2.addr.in6_addr), 1); \
  65. t1.family = t2.family = AF_INET6; \
  66. r = tor_addr_compare(&t1,&t2,CMP_SEMANTIC); \
  67. if (!(r op 0)) \
  68. test_fail_msg("failed: tor_addr_compare("a","b") "#op" 0"); \
  69. STMT_END
  70. /** Helper: Assert that <b>a</b> and <b>b</b>, when parsed by
  71. * tor_inet_pton(), give addresses that compare in the order defined by
  72. * <b>op</b> with tor_addr_compare_masked() with <b>m</b> masked. */
  73. #define test_addr_compare_masked(a, op, b, m) STMT_BEGIN \
  74. test_eq(tor_inet_pton(AF_INET6, a, &t1.addr.in6_addr), 1); \
  75. test_eq(tor_inet_pton(AF_INET6, b, &t2.addr.in6_addr), 1); \
  76. t1.family = t2.family = AF_INET6; \
  77. r = tor_addr_compare_masked(&t1,&t2,m,CMP_SEMANTIC); \
  78. if (!(r op 0)) \
  79. test_fail_msg("failed: tor_addr_compare_masked("a","b","#m") "#op" 0"); \
  80. STMT_END
  81. /** Helper: assert that <b>xx</b> is parseable as a masked IPv6 address with
  82. * ports by tor_parse_mask_addr_ports(), with family <b>f</b>, IP address
  83. * as 4 32-bit words <b>ip1...ip4</b>, mask bits as <b>mm</b>, and port range
  84. * as <b>pt1..pt2</b>. */
  85. #define test_addr_mask_ports_parse(xx, f, ip1, ip2, ip3, ip4, mm, pt1, pt2) \
  86. STMT_BEGIN \
  87. test_eq(tor_addr_parse_mask_ports(xx, &t1, &mask, &port1, &port2), f); \
  88. p1=tor_inet_ntop(AF_INET6, &t1.addr.in6_addr, bug, sizeof(bug)); \
  89. test_eq(htonl(ip1), tor_addr_to_in6_addr32(&t1)[0]); \
  90. test_eq(htonl(ip2), tor_addr_to_in6_addr32(&t1)[1]); \
  91. test_eq(htonl(ip3), tor_addr_to_in6_addr32(&t1)[2]); \
  92. test_eq(htonl(ip4), tor_addr_to_in6_addr32(&t1)[3]); \
  93. test_eq(mask, mm); \
  94. test_eq(port1, pt1); \
  95. test_eq(port2, pt2); \
  96. STMT_END
  97. /** Run unit tests for IPv6 encoding/decoding/manipulation functions. */
  98. static void
  99. test_addr_ip6_helpers(void)
  100. {
  101. char buf[TOR_ADDR_BUF_LEN], bug[TOR_ADDR_BUF_LEN];
  102. struct in6_addr a1, a2;
  103. tor_addr_t t1, t2;
  104. int r, i;
  105. uint16_t port1, port2;
  106. maskbits_t mask;
  107. const char *p1;
  108. struct sockaddr_storage sa_storage;
  109. struct sockaddr_in *sin;
  110. struct sockaddr_in6 *sin6;
  111. // struct in_addr b1, b2;
  112. /* Test tor_inet_ntop and tor_inet_pton: IPv6 */
  113. /* ==== Converting to and from sockaddr_t. */
  114. sin = (struct sockaddr_in *)&sa_storage;
  115. sin->sin_family = AF_INET;
  116. sin->sin_port = 9090;
  117. sin->sin_addr.s_addr = htonl(0x7f7f0102); /*127.127.1.2*/
  118. tor_addr_from_sockaddr(&t1, (struct sockaddr *)sin, NULL);
  119. test_eq(tor_addr_family(&t1), AF_INET);
  120. test_eq(tor_addr_to_ipv4h(&t1), 0x7f7f0102);
  121. memset(&sa_storage, 0, sizeof(sa_storage));
  122. test_eq(sizeof(struct sockaddr_in),
  123. tor_addr_to_sockaddr(&t1, 1234, (struct sockaddr *)&sa_storage,
  124. sizeof(sa_storage)));
  125. test_eq(1234, ntohs(sin->sin_port));
  126. test_eq(0x7f7f0102, ntohl(sin->sin_addr.s_addr));
  127. memset(&sa_storage, 0, sizeof(sa_storage));
  128. sin6 = (struct sockaddr_in6 *)&sa_storage;
  129. sin6->sin6_family = AF_INET6;
  130. sin6->sin6_port = htons(7070);
  131. sin6->sin6_addr.s6_addr[0] = 128;
  132. tor_addr_from_sockaddr(&t1, (struct sockaddr *)sin6, NULL);
  133. test_eq(tor_addr_family(&t1), AF_INET6);
  134. p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 0);
  135. test_streq(p1, "8000::");
  136. memset(&sa_storage, 0, sizeof(sa_storage));
  137. test_eq(sizeof(struct sockaddr_in6),
  138. tor_addr_to_sockaddr(&t1, 9999, (struct sockaddr *)&sa_storage,
  139. sizeof(sa_storage)));
  140. test_eq(AF_INET6, sin6->sin6_family);
  141. test_eq(9999, ntohs(sin6->sin6_port));
  142. test_eq(0x80000000, ntohl(S6_ADDR32(sin6->sin6_addr)[0]));
  143. /* ==== tor_addr_lookup: static cases. (Can't test dns without knowing we
  144. * have a good resolver. */
  145. test_eq(0, tor_addr_lookup("127.128.129.130", AF_UNSPEC, &t1));
  146. test_eq(AF_INET, tor_addr_family(&t1));
  147. test_eq(tor_addr_to_ipv4h(&t1), 0x7f808182);
  148. test_eq(0, tor_addr_lookup("9000::5", AF_UNSPEC, &t1));
  149. test_eq(AF_INET6, tor_addr_family(&t1));
  150. test_eq(0x90, tor_addr_to_in6_addr8(&t1)[0]);
  151. test_assert(tor_mem_is_zero((char*)tor_addr_to_in6_addr8(&t1)+1, 14));
  152. test_eq(0x05, tor_addr_to_in6_addr8(&t1)[15]);
  153. /* === Test pton: valid af_inet6 */
  154. /* Simple, valid parsing. */
  155. r = tor_inet_pton(AF_INET6,
  156. "0102:0304:0506:0708:090A:0B0C:0D0E:0F10", &a1);
  157. test_assert(r==1);
  158. for (i=0;i<16;++i) { test_eq(i+1, (int)a1.s6_addr[i]); }
  159. /* ipv4 ending. */
  160. test_pton6_same("0102:0304:0506:0708:090A:0B0C:0D0E:0F10",
  161. "0102:0304:0506:0708:090A:0B0C:13.14.15.16");
  162. /* shortened words. */
  163. test_pton6_same("0001:0099:BEEF:0000:0123:FFFF:0001:0001",
  164. "1:99:BEEF:0:0123:FFFF:1:1");
  165. /* zeros at the beginning */
  166. test_pton6_same("0000:0000:0000:0000:0009:C0A8:0001:0001",
  167. "::9:c0a8:1:1");
  168. test_pton6_same("0000:0000:0000:0000:0009:C0A8:0001:0001",
  169. "::9:c0a8:0.1.0.1");
  170. /* zeros in the middle. */
  171. test_pton6_same("fe80:0000:0000:0000:0202:1111:0001:0001",
  172. "fe80::202:1111:1:1");
  173. /* zeros at the end. */
  174. test_pton6_same("1000:0001:0000:0007:0000:0000:0000:0000",
  175. "1000:1:0:7::");
  176. /* === Test ntop: af_inet6 */
  177. test_ntop6_reduces("0:0:0:0:0:0:0:0", "::");
  178. test_ntop6_reduces("0001:0099:BEEF:0006:0123:FFFF:0001:0001",
  179. "1:99:beef:6:123:ffff:1:1");
  180. //test_ntop6_reduces("0:0:0:0:0:0:c0a8:0101", "::192.168.1.1");
  181. test_ntop6_reduces("0:0:0:0:0:ffff:c0a8:0101", "::ffff:192.168.1.1");
  182. test_ntop6_reduces("002:0:0000:0:3::4", "2::3:0:0:4");
  183. test_ntop6_reduces("0:0::1:0:3", "::1:0:3");
  184. test_ntop6_reduces("008:0::0", "8::");
  185. test_ntop6_reduces("0:0:0:0:0:ffff::1", "::ffff:0.0.0.1");
  186. test_ntop6_reduces("abcd:0:0:0:0:0:7f00::", "abcd::7f00:0");
  187. test_ntop6_reduces("0000:0000:0000:0000:0009:C0A8:0001:0001",
  188. "::9:c0a8:1:1");
  189. test_ntop6_reduces("fe80:0000:0000:0000:0202:1111:0001:0001",
  190. "fe80::202:1111:1:1");
  191. test_ntop6_reduces("1000:0001:0000:0007:0000:0000:0000:0000",
  192. "1000:1:0:7::");
  193. /* === Test pton: invalid in6. */
  194. test_pton6_bad("foobar.");
  195. test_pton6_bad("55555::");
  196. test_pton6_bad("9:-60::");
  197. test_pton6_bad("1:2:33333:4:0002:3::");
  198. //test_pton6_bad("1:2:3333:4:00002:3::");// BAD, but glibc doesn't say so.
  199. test_pton6_bad("1:2:3333:4:fish:3::");
  200. test_pton6_bad("1:2:3:4:5:6:7:8:9");
  201. test_pton6_bad("1:2:3:4:5:6:7");
  202. test_pton6_bad("1:2:3:4:5:6:1.2.3.4.5");
  203. test_pton6_bad("1:2:3:4:5:6:1.2.3");
  204. test_pton6_bad("::1.2.3");
  205. test_pton6_bad("::1.2.3.4.5");
  206. test_pton6_bad("99");
  207. test_pton6_bad("");
  208. test_pton6_bad("1::2::3:4");
  209. test_pton6_bad("a:::b:c");
  210. test_pton6_bad(":::a:b:c");
  211. test_pton6_bad("a:b:c:::");
  212. /* test internal checking */
  213. test_external_ip("fbff:ffff::2:7", 0);
  214. test_internal_ip("fc01::2:7", 0);
  215. test_internal_ip("fdff:ffff::f:f", 0);
  216. test_external_ip("fe00::3:f", 0);
  217. test_external_ip("fe7f:ffff::2:7", 0);
  218. test_internal_ip("fe80::2:7", 0);
  219. test_internal_ip("febf:ffff::f:f", 0);
  220. test_internal_ip("fec0::2:7:7", 0);
  221. test_internal_ip("feff:ffff::e:7:7", 0);
  222. test_external_ip("ff00::e:7:7", 0);
  223. test_internal_ip("::", 0);
  224. test_internal_ip("::1", 0);
  225. test_internal_ip("::1", 1);
  226. test_internal_ip("::", 0);
  227. test_external_ip("::", 1);
  228. test_external_ip("::2", 0);
  229. test_external_ip("2001::", 0);
  230. test_external_ip("ffff::", 0);
  231. test_external_ip("::ffff:0.0.0.0", 1);
  232. test_internal_ip("::ffff:0.0.0.0", 0);
  233. test_internal_ip("::ffff:0.255.255.255", 0);
  234. test_external_ip("::ffff:1.0.0.0", 0);
  235. test_external_ip("::ffff:9.255.255.255", 0);
  236. test_internal_ip("::ffff:10.0.0.0", 0);
  237. test_internal_ip("::ffff:10.255.255.255", 0);
  238. test_external_ip("::ffff:11.0.0.0", 0);
  239. test_external_ip("::ffff:126.255.255.255", 0);
  240. test_internal_ip("::ffff:127.0.0.0", 0);
  241. test_internal_ip("::ffff:127.255.255.255", 0);
  242. test_external_ip("::ffff:128.0.0.0", 0);
  243. test_external_ip("::ffff:172.15.255.255", 0);
  244. test_internal_ip("::ffff:172.16.0.0", 0);
  245. test_internal_ip("::ffff:172.31.255.255", 0);
  246. test_external_ip("::ffff:172.32.0.0", 0);
  247. test_external_ip("::ffff:192.167.255.255", 0);
  248. test_internal_ip("::ffff:192.168.0.0", 0);
  249. test_internal_ip("::ffff:192.168.255.255", 0);
  250. test_external_ip("::ffff:192.169.0.0", 0);
  251. test_external_ip("::ffff:169.253.255.255", 0);
  252. test_internal_ip("::ffff:169.254.0.0", 0);
  253. test_internal_ip("::ffff:169.254.255.255", 0);
  254. test_external_ip("::ffff:169.255.0.0", 0);
  255. test_assert(is_internal_IP(0x7f000001, 0));
  256. /* tor_addr_compare(tor_addr_t x2) */
  257. test_addr_compare("ffff::", ==, "ffff::0");
  258. test_addr_compare("0::3:2:1", <, "0::ffff:0.3.2.1");
  259. test_addr_compare("0::2:2:1", <, "0::ffff:0.3.2.1");
  260. test_addr_compare("0::ffff:0.3.2.1", >, "0::0:0:0");
  261. test_addr_compare("0::ffff:5.2.2.1", <, "::ffff:6.0.0.0"); /* XXXX wrong. */
  262. tor_addr_parse_mask_ports("[::ffff:2.3.4.5]", &t1, NULL, NULL, NULL);
  263. tor_addr_parse_mask_ports("2.3.4.5", &t2, NULL, NULL, NULL);
  264. test_assert(tor_addr_compare(&t1, &t2, CMP_SEMANTIC) == 0);
  265. tor_addr_parse_mask_ports("[::ffff:2.3.4.4]", &t1, NULL, NULL, NULL);
  266. tor_addr_parse_mask_ports("2.3.4.5", &t2, NULL, NULL, NULL);
  267. test_assert(tor_addr_compare(&t1, &t2, CMP_SEMANTIC) < 0);
  268. /* test compare_masked */
  269. test_addr_compare_masked("ffff::", ==, "ffff::0", 128);
  270. test_addr_compare_masked("ffff::", ==, "ffff::0", 64);
  271. test_addr_compare_masked("0::2:2:1", <, "0::8000:2:1", 81);
  272. test_addr_compare_masked("0::2:2:1", ==, "0::8000:2:1", 80);
  273. /* Test decorated addr_to_string. */
  274. test_eq(AF_INET6, tor_addr_from_str(&t1, "[123:45:6789::5005:11]"));
  275. p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 1);
  276. test_streq(p1, "[123:45:6789::5005:11]");
  277. test_eq(AF_INET, tor_addr_from_str(&t1, "18.0.0.1"));
  278. p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 1);
  279. test_streq(p1, "18.0.0.1");
  280. /* Test tor_addr_parse_reverse_lookup_name */
  281. i = tor_addr_parse_reverse_lookup_name(&t1, "Foobar.baz", AF_UNSPEC, 0);
  282. test_eq(0, i);
  283. i = tor_addr_parse_reverse_lookup_name(&t1, "Foobar.baz", AF_UNSPEC, 1);
  284. test_eq(0, i);
  285. i = tor_addr_parse_reverse_lookup_name(&t1, "1.0.168.192.in-addr.arpa",
  286. AF_UNSPEC, 1);
  287. test_eq(1, i);
  288. test_eq(tor_addr_family(&t1), AF_INET);
  289. p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 1);
  290. test_streq(p1, "192.168.0.1");
  291. i = tor_addr_parse_reverse_lookup_name(&t1, "192.168.0.99", AF_UNSPEC, 0);
  292. test_eq(0, i);
  293. i = tor_addr_parse_reverse_lookup_name(&t1, "192.168.0.99", AF_UNSPEC, 1);
  294. test_eq(1, i);
  295. p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 1);
  296. test_streq(p1, "192.168.0.99");
  297. memset(&t1, 0, sizeof(t1));
  298. i = tor_addr_parse_reverse_lookup_name(&t1,
  299. "0.1.2.3.4.5.6.7.8.9.a.b.c.d.e.f."
  300. "f.e.e.b.1.e.b.e.e.f.f.e.e.e.d.9."
  301. "ip6.ARPA",
  302. AF_UNSPEC, 0);
  303. test_eq(1, i);
  304. p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 1);
  305. test_streq(p1, "[9dee:effe:ebe1:beef:fedc:ba98:7654:3210]");
  306. /* Failing cases. */
  307. i = tor_addr_parse_reverse_lookup_name(&t1,
  308. "6.7.8.9.a.b.c.d.e.f."
  309. "f.e.e.b.1.e.b.e.e.f.f.e.e.e.d.9."
  310. "ip6.ARPA",
  311. AF_UNSPEC, 0);
  312. test_eq(i, -1);
  313. i = tor_addr_parse_reverse_lookup_name(&t1,
  314. "6.7.8.9.a.b.c.d.e.f.a.b.c.d.e.f.0."
  315. "f.e.e.b.1.e.b.e.e.f.f.e.e.e.d.9."
  316. "ip6.ARPA",
  317. AF_UNSPEC, 0);
  318. test_eq(i, -1);
  319. i = tor_addr_parse_reverse_lookup_name(&t1,
  320. "6.7.8.9.a.b.c.d.e.f.X.0.0.0.0.9."
  321. "f.e.e.b.1.e.b.e.e.f.f.e.e.e.d.9."
  322. "ip6.ARPA",
  323. AF_UNSPEC, 0);
  324. test_eq(i, -1);
  325. i = tor_addr_parse_reverse_lookup_name(&t1, "32.1.1.in-addr.arpa",
  326. AF_UNSPEC, 0);
  327. test_eq(i, -1);
  328. i = tor_addr_parse_reverse_lookup_name(&t1, ".in-addr.arpa",
  329. AF_UNSPEC, 0);
  330. test_eq(i, -1);
  331. i = tor_addr_parse_reverse_lookup_name(&t1, "1.2.3.4.5.in-addr.arpa",
  332. AF_UNSPEC, 0);
  333. test_eq(i, -1);
  334. i = tor_addr_parse_reverse_lookup_name(&t1, "1.2.3.4.5.in-addr.arpa",
  335. AF_INET6, 0);
  336. test_eq(i, -1);
  337. i = tor_addr_parse_reverse_lookup_name(&t1,
  338. "6.7.8.9.a.b.c.d.e.f.a.b.c.d.e.0."
  339. "f.e.e.b.1.e.b.e.e.f.f.e.e.e.d.9."
  340. "ip6.ARPA",
  341. AF_INET, 0);
  342. test_eq(i, -1);
  343. /* test tor_addr_parse_mask_ports */
  344. test_addr_mask_ports_parse("[::f]/17:47-95", AF_INET6,
  345. 0, 0, 0, 0x0000000f, 17, 47, 95);
  346. //test_addr_parse("[::fefe:4.1.1.7/120]:999-1000");
  347. //test_addr_parse_check("::fefe:401:107", 120, 999, 1000);
  348. test_addr_mask_ports_parse("[::ffff:4.1.1.7]/120:443", AF_INET6,
  349. 0, 0, 0x0000ffff, 0x04010107, 120, 443, 443);
  350. test_addr_mask_ports_parse("[abcd:2::44a:0]:2-65000", AF_INET6,
  351. 0xabcd0002, 0, 0, 0x044a0000, 128, 2, 65000);
  352. r=tor_addr_parse_mask_ports("[fefef::]/112", &t1, NULL, NULL, NULL);
  353. test_assert(r == -1);
  354. r=tor_addr_parse_mask_ports("efef::/112", &t1, NULL, NULL, NULL);
  355. test_assert(r == -1);
  356. r=tor_addr_parse_mask_ports("[f:f:f:f:f:f:f:f::]", &t1, NULL, NULL, NULL);
  357. test_assert(r == -1);
  358. r=tor_addr_parse_mask_ports("[::f:f:f:f:f:f:f:f]", &t1, NULL, NULL, NULL);
  359. test_assert(r == -1);
  360. r=tor_addr_parse_mask_ports("[f:f:f:f:f:f:f:f:f]", &t1, NULL, NULL, NULL);
  361. test_assert(r == -1);
  362. /* Test for V4-mapped address with mask < 96. (arguably not valid) */
  363. r=tor_addr_parse_mask_ports("[::ffff:1.1.2.2/33]", &t1, &mask, NULL, NULL);
  364. test_assert(r == -1);
  365. r=tor_addr_parse_mask_ports("1.1.2.2/33", &t1, &mask, NULL, NULL);
  366. test_assert(r == -1);
  367. r=tor_addr_parse_mask_ports("1.1.2.2/31", &t1, &mask, NULL, NULL);
  368. test_assert(r == AF_INET);
  369. r=tor_addr_parse_mask_ports("[efef::]/112", &t1, &mask, &port1, &port2);
  370. test_assert(r == AF_INET6);
  371. test_assert(port1 == 1);
  372. test_assert(port2 == 65535);
  373. /* make sure inet address lengths >= max */
  374. test_assert(INET_NTOA_BUF_LEN >= sizeof("255.255.255.255"));
  375. test_assert(TOR_ADDR_BUF_LEN >=
  376. sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"));
  377. test_assert(sizeof(tor_addr_t) >= sizeof(struct in6_addr));
  378. /* get interface addresses */
  379. r = get_interface_address6(LOG_DEBUG, AF_INET, &t1);
  380. i = get_interface_address6(LOG_DEBUG, AF_INET6, &t2);
  381. #if 0
  382. tor_inet_ntop(AF_INET, &t1.sa.sin_addr, buf, sizeof(buf));
  383. printf("\nv4 address: %s (family=%i)", buf, IN_FAMILY(&t1));
  384. tor_inet_ntop(AF_INET6, &t2.sa6.sin6_addr, buf, sizeof(buf));
  385. printf("\nv6 address: %s (family=%i)", buf, IN_FAMILY(&t2));
  386. #endif
  387. done:
  388. ;
  389. }
  390. #define ADDR_LEGACY(name) \
  391. { #name, legacy_test_helper, 0, &legacy_setup, test_addr_ ## name }
  392. struct testcase_t addr_tests[] = {
  393. ADDR_LEGACY(ip6_helpers),
  394. END_OF_TESTCASES
  395. };