test_addr.c 22 KB

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