test_addr.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2012, 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_ptr(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. /* good round trip */
  53. test_eq(tor_inet_pton(AF_INET, ip, &in), 1);
  54. test_eq_ptr(tor_inet_ntop(AF_INET, &in, tmpbuf, sizeof(tmpbuf)), &tmpbuf);
  55. test_streq(tmpbuf, ip);
  56. /* just enough buffer length */
  57. test_streq(tor_inet_ntop(AF_INET, &in, tmpbuf, strlen(ip) + 1), ip);
  58. /* too short buffer */
  59. test_eq_ptr(tor_inet_ntop(AF_INET, &in, tmpbuf, strlen(ip)), NULL);
  60. }
  61. done:
  62. ;
  63. }
  64. #define test_op_ip6_(a,op,b,e1,e2) \
  65. STMT_BEGIN \
  66. tt_assert_test_fmt_type(a,b,e1" "#op" "e2,struct in6_addr*, \
  67. (memcmp(val1_->s6_addr, val2_->s6_addr, 16) op 0), \
  68. char *, "%s", \
  69. { int i; char *cp; \
  70. cp = print_ = tor_malloc(64); \
  71. for (i=0;i<16;++i) { \
  72. tor_snprintf(cp, 3,"%02x", (unsigned)value_->s6_addr[i]);\
  73. cp += 2; \
  74. if (i != 15) *cp++ = ':'; \
  75. } \
  76. }, \
  77. { tor_free(print_); }, \
  78. TT_EXIT_TEST_FUNCTION \
  79. ); \
  80. STMT_END
  81. /** Helper: Assert that two strings both decode as IPv6 addresses with
  82. * tor_inet_pton(), and both decode to the same address. */
  83. #define test_pton6_same(a,b) STMT_BEGIN \
  84. test_eq(tor_inet_pton(AF_INET6, a, &a1), 1); \
  85. test_eq(tor_inet_pton(AF_INET6, b, &a2), 1); \
  86. test_op_ip6_(&a1,==,&a2,#a,#b); \
  87. STMT_END
  88. /** Helper: Assert that <b>a</b> is recognized as a bad IPv6 address by
  89. * tor_inet_pton(). */
  90. #define test_pton6_bad(a) \
  91. test_eq(0, tor_inet_pton(AF_INET6, a, &a1))
  92. /** Helper: assert that <b>a</b>, when parsed by tor_inet_pton() and displayed
  93. * with tor_inet_ntop(), yields <b>b</b>. Also assert that <b>b</b> parses to
  94. * the same value as <b>a</b>. */
  95. #define test_ntop6_reduces(a,b) STMT_BEGIN \
  96. test_eq(tor_inet_pton(AF_INET6, a, &a1), 1); \
  97. test_streq(tor_inet_ntop(AF_INET6, &a1, buf, sizeof(buf)), b); \
  98. test_eq(tor_inet_pton(AF_INET6, b, &a2), 1); \
  99. test_op_ip6_(&a1, ==, &a2, a, b); \
  100. STMT_END
  101. /** Helper: assert that <b>a</b> parses by tor_inet_pton() into a address that
  102. * passes tor_addr_is_internal() with <b>for_listening</b>. */
  103. #define test_internal_ip(a,for_listening) STMT_BEGIN \
  104. test_eq(tor_inet_pton(AF_INET6, a, &t1.addr.in6_addr), 1); \
  105. t1.family = AF_INET6; \
  106. if (!tor_addr_is_internal(&t1, for_listening)) \
  107. test_fail_msg( a "was not internal."); \
  108. STMT_END
  109. /** Helper: assert that <b>a</b> parses by tor_inet_pton() into a address that
  110. * does not pass tor_addr_is_internal() with <b>for_listening</b>. */
  111. #define test_external_ip(a,for_listening) STMT_BEGIN \
  112. test_eq(tor_inet_pton(AF_INET6, a, &t1.addr.in6_addr), 1); \
  113. t1.family = AF_INET6; \
  114. if (tor_addr_is_internal(&t1, for_listening)) \
  115. test_fail_msg(a "was not external."); \
  116. STMT_END
  117. /** Helper: Assert that <b>a</b> and <b>b</b>, when parsed by
  118. * tor_inet_pton(), give addresses that compare in the order defined by
  119. * <b>op</b> with tor_addr_compare(). */
  120. #define test_addr_compare(a, op, b) STMT_BEGIN \
  121. test_eq(tor_inet_pton(AF_INET6, a, &t1.addr.in6_addr), 1); \
  122. test_eq(tor_inet_pton(AF_INET6, b, &t2.addr.in6_addr), 1); \
  123. t1.family = t2.family = AF_INET6; \
  124. r = tor_addr_compare(&t1,&t2,CMP_SEMANTIC); \
  125. if (!(r op 0)) \
  126. test_fail_msg("failed: tor_addr_compare("a","b") "#op" 0"); \
  127. STMT_END
  128. /** Helper: Assert that <b>a</b> and <b>b</b>, when parsed by
  129. * tor_inet_pton(), give addresses that compare in the order defined by
  130. * <b>op</b> with tor_addr_compare_masked() with <b>m</b> masked. */
  131. #define test_addr_compare_masked(a, op, b, m) STMT_BEGIN \
  132. test_eq(tor_inet_pton(AF_INET6, a, &t1.addr.in6_addr), 1); \
  133. test_eq(tor_inet_pton(AF_INET6, b, &t2.addr.in6_addr), 1); \
  134. t1.family = t2.family = AF_INET6; \
  135. r = tor_addr_compare_masked(&t1,&t2,m,CMP_SEMANTIC); \
  136. if (!(r op 0)) \
  137. test_fail_msg("failed: tor_addr_compare_masked("a","b","#m") "#op" 0"); \
  138. STMT_END
  139. /** Helper: assert that <b>xx</b> is parseable as a masked IPv6 address with
  140. * ports by tor_parse_mask_addr_ports(), with family <b>f</b>, IP address
  141. * as 4 32-bit words <b>ip1...ip4</b>, mask bits as <b>mm</b>, and port range
  142. * as <b>pt1..pt2</b>. */
  143. #define test_addr_mask_ports_parse(xx, f, ip1, ip2, ip3, ip4, mm, pt1, pt2) \
  144. STMT_BEGIN \
  145. test_eq(tor_addr_parse_mask_ports(xx, 0, &t1, &mask, &port1, &port2), \
  146. f); \
  147. p1=tor_inet_ntop(AF_INET6, &t1.addr.in6_addr, bug, sizeof(bug)); \
  148. test_eq(htonl(ip1), tor_addr_to_in6_addr32(&t1)[0]); \
  149. test_eq(htonl(ip2), tor_addr_to_in6_addr32(&t1)[1]); \
  150. test_eq(htonl(ip3), tor_addr_to_in6_addr32(&t1)[2]); \
  151. test_eq(htonl(ip4), tor_addr_to_in6_addr32(&t1)[3]); \
  152. test_eq(mask, mm); \
  153. test_eq(port1, pt1); \
  154. test_eq(port2, pt2); \
  155. STMT_END
  156. /** Run unit tests for IPv6 encoding/decoding/manipulation functions. */
  157. static void
  158. test_addr_ip6_helpers(void)
  159. {
  160. char buf[TOR_ADDR_BUF_LEN], bug[TOR_ADDR_BUF_LEN];
  161. char rbuf[REVERSE_LOOKUP_NAME_BUF_LEN];
  162. struct in6_addr a1, a2;
  163. tor_addr_t t1, t2;
  164. int r, i;
  165. uint16_t port1, port2;
  166. maskbits_t mask;
  167. const char *p1;
  168. struct sockaddr_storage sa_storage;
  169. struct sockaddr_in *sin;
  170. struct sockaddr_in6 *sin6;
  171. /* Test tor_inet_ntop and tor_inet_pton: IPv6 */
  172. {
  173. const char *ip = "2001::1234";
  174. const char *ip_ffff = "::ffff:192.168.1.2";
  175. /* good round trip */
  176. test_eq(tor_inet_pton(AF_INET6, ip, &a1), 1);
  177. test_eq_ptr(tor_inet_ntop(AF_INET6, &a1, buf, sizeof(buf)), &buf);
  178. test_streq(buf, ip);
  179. /* good round trip - ::ffff:0:0 style */
  180. test_eq(tor_inet_pton(AF_INET6, ip_ffff, &a2), 1);
  181. test_eq_ptr(tor_inet_ntop(AF_INET6, &a2, buf, sizeof(buf)), &buf);
  182. test_streq(buf, ip_ffff);
  183. /* just long enough buffer (remember \0) */
  184. test_streq(tor_inet_ntop(AF_INET6, &a1, buf, strlen(ip)+1), ip);
  185. test_streq(tor_inet_ntop(AF_INET6, &a2, buf, strlen(ip_ffff)+1),
  186. ip_ffff);
  187. /* too short buffer (remember \0) */
  188. test_eq_ptr(tor_inet_ntop(AF_INET6, &a1, buf, strlen(ip)), NULL);
  189. test_eq_ptr(tor_inet_ntop(AF_INET6, &a2, buf, strlen(ip_ffff)), NULL);
  190. }
  191. /* ==== Converting to and from sockaddr_t. */
  192. sin = (struct sockaddr_in *)&sa_storage;
  193. sin->sin_family = AF_INET;
  194. sin->sin_port = 9090;
  195. sin->sin_addr.s_addr = htonl(0x7f7f0102); /*127.127.1.2*/
  196. tor_addr_from_sockaddr(&t1, (struct sockaddr *)sin, NULL);
  197. test_eq(tor_addr_family(&t1), AF_INET);
  198. test_eq(tor_addr_to_ipv4h(&t1), 0x7f7f0102);
  199. memset(&sa_storage, 0, sizeof(sa_storage));
  200. test_eq(sizeof(struct sockaddr_in),
  201. tor_addr_to_sockaddr(&t1, 1234, (struct sockaddr *)&sa_storage,
  202. sizeof(sa_storage)));
  203. test_eq(1234, ntohs(sin->sin_port));
  204. test_eq(0x7f7f0102, ntohl(sin->sin_addr.s_addr));
  205. memset(&sa_storage, 0, sizeof(sa_storage));
  206. sin6 = (struct sockaddr_in6 *)&sa_storage;
  207. sin6->sin6_family = AF_INET6;
  208. sin6->sin6_port = htons(7070);
  209. sin6->sin6_addr.s6_addr[0] = 128;
  210. tor_addr_from_sockaddr(&t1, (struct sockaddr *)sin6, NULL);
  211. test_eq(tor_addr_family(&t1), AF_INET6);
  212. p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 0);
  213. test_streq(p1, "8000::");
  214. memset(&sa_storage, 0, sizeof(sa_storage));
  215. test_eq(sizeof(struct sockaddr_in6),
  216. tor_addr_to_sockaddr(&t1, 9999, (struct sockaddr *)&sa_storage,
  217. sizeof(sa_storage)));
  218. test_eq(AF_INET6, sin6->sin6_family);
  219. test_eq(9999, ntohs(sin6->sin6_port));
  220. test_eq(0x80000000, ntohl(S6_ADDR32(sin6->sin6_addr)[0]));
  221. /* ==== tor_addr_lookup: static cases. (Can't test dns without knowing we
  222. * have a good resolver. */
  223. test_eq(0, tor_addr_lookup("127.128.129.130", AF_UNSPEC, &t1));
  224. test_eq(AF_INET, tor_addr_family(&t1));
  225. test_eq(tor_addr_to_ipv4h(&t1), 0x7f808182);
  226. test_eq(0, tor_addr_lookup("9000::5", AF_UNSPEC, &t1));
  227. test_eq(AF_INET6, tor_addr_family(&t1));
  228. test_eq(0x90, tor_addr_to_in6_addr8(&t1)[0]);
  229. test_assert(tor_mem_is_zero((char*)tor_addr_to_in6_addr8(&t1)+1, 14));
  230. test_eq(0x05, tor_addr_to_in6_addr8(&t1)[15]);
  231. /* === Test pton: valid af_inet6 */
  232. /* Simple, valid parsing. */
  233. r = tor_inet_pton(AF_INET6,
  234. "0102:0304:0506:0708:090A:0B0C:0D0E:0F10", &a1);
  235. test_assert(r==1);
  236. for (i=0;i<16;++i) { test_eq(i+1, (int)a1.s6_addr[i]); }
  237. /* ipv4 ending. */
  238. test_pton6_same("0102:0304:0506:0708:090A:0B0C:0D0E:0F10",
  239. "0102:0304:0506:0708:090A:0B0C:13.14.15.16");
  240. /* shortened words. */
  241. test_pton6_same("0001:0099:BEEF:0000:0123:FFFF:0001:0001",
  242. "1:99:BEEF:0:0123:FFFF:1:1");
  243. /* zeros at the beginning */
  244. test_pton6_same("0000:0000:0000:0000:0009:C0A8:0001:0001",
  245. "::9:c0a8:1:1");
  246. test_pton6_same("0000:0000:0000:0000:0009:C0A8:0001:0001",
  247. "::9:c0a8:0.1.0.1");
  248. /* zeros in the middle. */
  249. test_pton6_same("fe80:0000:0000:0000:0202:1111:0001:0001",
  250. "fe80::202:1111:1:1");
  251. /* zeros at the end. */
  252. test_pton6_same("1000:0001:0000:0007:0000:0000:0000:0000",
  253. "1000:1:0:7::");
  254. /* === Test ntop: af_inet6 */
  255. test_ntop6_reduces("0:0:0:0:0:0:0:0", "::");
  256. test_ntop6_reduces("0001:0099:BEEF:0006:0123:FFFF:0001:0001",
  257. "1:99:beef:6:123:ffff:1:1");
  258. //test_ntop6_reduces("0:0:0:0:0:0:c0a8:0101", "::192.168.1.1");
  259. test_ntop6_reduces("0:0:0:0:0:ffff:c0a8:0101", "::ffff:192.168.1.1");
  260. test_ntop6_reduces("002:0:0000:0:3::4", "2::3:0:0:4");
  261. test_ntop6_reduces("0:0::1:0:3", "::1:0:3");
  262. test_ntop6_reduces("008:0::0", "8::");
  263. test_ntop6_reduces("0:0:0:0:0:ffff::1", "::ffff:0.0.0.1");
  264. test_ntop6_reduces("abcd:0:0:0:0:0:7f00::", "abcd::7f00:0");
  265. test_ntop6_reduces("0000:0000:0000:0000:0009:C0A8:0001:0001",
  266. "::9:c0a8:1:1");
  267. test_ntop6_reduces("fe80:0000:0000:0000:0202:1111:0001:0001",
  268. "fe80::202:1111:1:1");
  269. test_ntop6_reduces("1000:0001:0000:0007:0000:0000:0000:0000",
  270. "1000:1:0:7::");
  271. /* Bad af param */
  272. test_eq(tor_inet_pton(AF_UNSPEC, 0, 0), -1);
  273. /* === Test pton: invalid in6. */
  274. test_pton6_bad("foobar.");
  275. test_pton6_bad("-1::");
  276. test_pton6_bad("00001::");
  277. test_pton6_bad("10000::");
  278. test_pton6_bad("::10000");
  279. test_pton6_bad("55555::");
  280. test_pton6_bad("9:-60::");
  281. test_pton6_bad("9:+60::");
  282. test_pton6_bad("9|60::");
  283. test_pton6_bad("0x60::");
  284. test_pton6_bad("::0x60");
  285. test_pton6_bad("9:0x60::");
  286. test_pton6_bad("1:2:33333:4:0002:3::");
  287. test_pton6_bad("1:2:3333:4:fish:3::");
  288. test_pton6_bad("1:2:3:4:5:6:7:8:9");
  289. test_pton6_bad("1:2:3:4:5:6:7");
  290. test_pton6_bad("1:2:3:4:5:6:1.2.3.4.5");
  291. test_pton6_bad("1:2:3:4:5:6:1.2.3");
  292. test_pton6_bad("::1.2.3");
  293. test_pton6_bad("::1.2.3.4.5");
  294. test_pton6_bad("::ffff:0xff.0.0.0");
  295. test_pton6_bad("::ffff:ff.0.0.0");
  296. test_pton6_bad("::ffff:256.0.0.0");
  297. test_pton6_bad("::ffff:-1.0.0.0");
  298. test_pton6_bad("99");
  299. test_pton6_bad("");
  300. test_pton6_bad(".");
  301. test_pton6_bad(":");
  302. test_pton6_bad("1::2::3:4");
  303. test_pton6_bad("a:::b:c");
  304. test_pton6_bad(":::a:b:c");
  305. test_pton6_bad("a:b:c:::");
  306. /* test internal checking */
  307. test_external_ip("fbff:ffff::2:7", 0);
  308. test_internal_ip("fc01::2:7", 0);
  309. test_internal_ip("fc01::02:7", 0);
  310. test_internal_ip("fc01::002:7", 0);
  311. test_internal_ip("fc01::0002:7", 0);
  312. test_internal_ip("fdff:ffff::f:f", 0);
  313. test_external_ip("fe00::3:f", 0);
  314. test_external_ip("fe7f:ffff::2:7", 0);
  315. test_internal_ip("fe80::2:7", 0);
  316. test_internal_ip("febf:ffff::f:f", 0);
  317. test_internal_ip("fec0::2:7:7", 0);
  318. test_internal_ip("feff:ffff::e:7:7", 0);
  319. test_external_ip("ff00::e:7:7", 0);
  320. test_internal_ip("::", 0);
  321. test_internal_ip("::1", 0);
  322. test_internal_ip("::1", 1);
  323. test_internal_ip("::", 0);
  324. test_external_ip("::", 1);
  325. test_external_ip("::2", 0);
  326. test_external_ip("2001::", 0);
  327. test_external_ip("ffff::", 0);
  328. test_external_ip("::ffff:0.0.0.0", 1);
  329. test_internal_ip("::ffff:0.0.0.0", 0);
  330. test_internal_ip("::ffff:0.255.255.255", 0);
  331. test_external_ip("::ffff:1.0.0.0", 0);
  332. test_external_ip("::ffff:9.255.255.255", 0);
  333. test_internal_ip("::ffff:10.0.0.0", 0);
  334. test_internal_ip("::ffff:10.255.255.255", 0);
  335. test_external_ip("::ffff:11.0.0.0", 0);
  336. test_external_ip("::ffff:126.255.255.255", 0);
  337. test_internal_ip("::ffff:127.0.0.0", 0);
  338. test_internal_ip("::ffff:127.255.255.255", 0);
  339. test_external_ip("::ffff:128.0.0.0", 0);
  340. test_external_ip("::ffff:172.15.255.255", 0);
  341. test_internal_ip("::ffff:172.16.0.0", 0);
  342. test_internal_ip("::ffff:172.31.255.255", 0);
  343. test_external_ip("::ffff:172.32.0.0", 0);
  344. test_external_ip("::ffff:192.167.255.255", 0);
  345. test_internal_ip("::ffff:192.168.0.0", 0);
  346. test_internal_ip("::ffff:192.168.255.255", 0);
  347. test_external_ip("::ffff:192.169.0.0", 0);
  348. test_external_ip("::ffff:169.253.255.255", 0);
  349. test_internal_ip("::ffff:169.254.0.0", 0);
  350. test_internal_ip("::ffff:169.254.255.255", 0);
  351. test_external_ip("::ffff:169.255.0.0", 0);
  352. test_assert(is_internal_IP(0x7f000001, 0));
  353. /* tor_addr_compare(tor_addr_t x2) */
  354. test_addr_compare("ffff::", ==, "ffff::0");
  355. test_addr_compare("0::3:2:1", <, "0::ffff:0.3.2.1");
  356. test_addr_compare("0::2:2:1", <, "0::ffff:0.3.2.1");
  357. test_addr_compare("0::ffff:0.3.2.1", >, "0::0:0:0");
  358. test_addr_compare("0::ffff:5.2.2.1", <, "::ffff:6.0.0.0"); /* XXXX wrong. */
  359. tor_addr_parse_mask_ports("[::ffff:2.3.4.5]", 0, &t1, NULL, NULL, NULL);
  360. tor_addr_parse_mask_ports("2.3.4.5", 0, &t2, NULL, NULL, NULL);
  361. test_assert(tor_addr_compare(&t1, &t2, CMP_SEMANTIC) == 0);
  362. tor_addr_parse_mask_ports("[::ffff:2.3.4.4]", 0, &t1, NULL, NULL, NULL);
  363. tor_addr_parse_mask_ports("2.3.4.5", 0, &t2, NULL, NULL, NULL);
  364. test_assert(tor_addr_compare(&t1, &t2, CMP_SEMANTIC) < 0);
  365. /* test compare_masked */
  366. test_addr_compare_masked("ffff::", ==, "ffff::0", 128);
  367. test_addr_compare_masked("ffff::", ==, "ffff::0", 64);
  368. test_addr_compare_masked("0::2:2:1", <, "0::8000:2:1", 81);
  369. test_addr_compare_masked("0::2:2:1", ==, "0::8000:2:1", 80);
  370. /* Test undecorated tor_addr_to_str */
  371. test_eq(AF_INET6, tor_addr_parse(&t1, "[123:45:6789::5005:11]"));
  372. p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 0);
  373. test_streq(p1, "123:45:6789::5005:11");
  374. test_eq(AF_INET, tor_addr_parse(&t1, "18.0.0.1"));
  375. p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 0);
  376. test_streq(p1, "18.0.0.1");
  377. /* Test decorated tor_addr_to_str */
  378. test_eq(AF_INET6, tor_addr_parse(&t1, "[123:45:6789::5005:11]"));
  379. p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 1);
  380. test_streq(p1, "[123:45:6789::5005:11]");
  381. test_eq(AF_INET, tor_addr_parse(&t1, "18.0.0.1"));
  382. p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 1);
  383. test_streq(p1, "18.0.0.1");
  384. /* Test buffer bounds checking of tor_addr_to_str */
  385. test_eq(AF_INET6, tor_addr_parse(&t1, "::")); /* 2 + \0 */
  386. test_eq_ptr(tor_addr_to_str(buf, &t1, 2, 0), NULL); /* too short buf */
  387. test_streq(tor_addr_to_str(buf, &t1, 3, 0), "::");
  388. test_eq_ptr(tor_addr_to_str(buf, &t1, 4, 1), NULL); /* too short buf */
  389. test_streq(tor_addr_to_str(buf, &t1, 5, 1), "[::]");
  390. test_eq(AF_INET6, tor_addr_parse(&t1, "2000::1337")); /* 10 + \0 */
  391. test_eq_ptr(tor_addr_to_str(buf, &t1, 10, 0), NULL); /* too short buf */
  392. test_streq(tor_addr_to_str(buf, &t1, 11, 0), "2000::1337");
  393. test_eq_ptr(tor_addr_to_str(buf, &t1, 12, 1), NULL); /* too short buf */
  394. test_streq(tor_addr_to_str(buf, &t1, 13, 1), "[2000::1337]");
  395. test_eq(AF_INET, tor_addr_parse(&t1, "1.2.3.4")); /* 7 + \0 */
  396. test_eq_ptr(tor_addr_to_str(buf, &t1, 7, 0), NULL); /* too short buf */
  397. test_streq(tor_addr_to_str(buf, &t1, 8, 0), "1.2.3.4");
  398. test_eq(AF_INET, tor_addr_parse(&t1, "255.255.255.255")); /* 15 + \0 */
  399. test_eq_ptr(tor_addr_to_str(buf, &t1, 15, 0), NULL); /* too short buf */
  400. test_streq(tor_addr_to_str(buf, &t1, 16, 0), "255.255.255.255");
  401. test_eq_ptr(tor_addr_to_str(buf, &t1, 15, 1), NULL); /* too short buf */
  402. test_streq(tor_addr_to_str(buf, &t1, 16, 1), "255.255.255.255");
  403. t1.family = AF_UNSPEC;
  404. test_eq_ptr(tor_addr_to_str(buf, &t1, sizeof(buf), 0), NULL);
  405. /* Test tor_addr_parse_PTR_name */
  406. i = tor_addr_parse_PTR_name(&t1, "Foobar.baz", AF_UNSPEC, 0);
  407. test_eq(0, i);
  408. i = tor_addr_parse_PTR_name(&t1, "Foobar.baz", AF_UNSPEC, 1);
  409. test_eq(0, i);
  410. i = tor_addr_parse_PTR_name(&t1, "1.0.168.192.in-addr.arpa",
  411. AF_UNSPEC, 1);
  412. test_eq(1, i);
  413. test_eq(tor_addr_family(&t1), AF_INET);
  414. p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 1);
  415. test_streq(p1, "192.168.0.1");
  416. i = tor_addr_parse_PTR_name(&t1, "192.168.0.99", AF_UNSPEC, 0);
  417. test_eq(0, i);
  418. i = tor_addr_parse_PTR_name(&t1, "192.168.0.99", AF_UNSPEC, 1);
  419. test_eq(1, i);
  420. p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 1);
  421. test_streq(p1, "192.168.0.99");
  422. memset(&t1, 0, sizeof(t1));
  423. i = tor_addr_parse_PTR_name(&t1,
  424. "0.1.2.3.4.5.6.7.8.9.a.b.c.d.e.f."
  425. "f.e.e.b.1.e.b.e.e.f.f.e.e.e.d.9."
  426. "ip6.ARPA",
  427. AF_UNSPEC, 0);
  428. test_eq(1, i);
  429. p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 1);
  430. test_streq(p1, "[9dee:effe:ebe1:beef:fedc:ba98:7654:3210]");
  431. /* Failing cases. */
  432. i = tor_addr_parse_PTR_name(&t1,
  433. "6.7.8.9.a.b.c.d.e.f."
  434. "f.e.e.b.1.e.b.e.e.f.f.e.e.e.d.9."
  435. "ip6.ARPA",
  436. AF_UNSPEC, 0);
  437. test_eq(i, -1);
  438. i = tor_addr_parse_PTR_name(&t1,
  439. "6.7.8.9.a.b.c.d.e.f.a.b.c.d.e.f.0."
  440. "f.e.e.b.1.e.b.e.e.f.f.e.e.e.d.9."
  441. "ip6.ARPA",
  442. AF_UNSPEC, 0);
  443. test_eq(i, -1);
  444. i = tor_addr_parse_PTR_name(&t1,
  445. "6.7.8.9.a.b.c.d.e.f.X.0.0.0.0.9."
  446. "f.e.e.b.1.e.b.e.e.f.f.e.e.e.d.9."
  447. "ip6.ARPA",
  448. AF_UNSPEC, 0);
  449. test_eq(i, -1);
  450. i = tor_addr_parse_PTR_name(&t1, "32.1.1.in-addr.arpa",
  451. AF_UNSPEC, 0);
  452. test_eq(i, -1);
  453. i = tor_addr_parse_PTR_name(&t1, ".in-addr.arpa",
  454. AF_UNSPEC, 0);
  455. test_eq(i, -1);
  456. i = tor_addr_parse_PTR_name(&t1, "1.2.3.4.5.in-addr.arpa",
  457. AF_UNSPEC, 0);
  458. test_eq(i, -1);
  459. i = tor_addr_parse_PTR_name(&t1, "1.2.3.4.5.in-addr.arpa",
  460. AF_INET6, 0);
  461. test_eq(i, -1);
  462. i = tor_addr_parse_PTR_name(&t1,
  463. "6.7.8.9.a.b.c.d.e.f.a.b.c.d.e.0."
  464. "f.e.e.b.1.e.b.e.e.f.f.e.e.e.d.9."
  465. "ip6.ARPA",
  466. AF_INET, 0);
  467. test_eq(i, -1);
  468. /* === Test tor_addr_to_PTR_name */
  469. /* Stage IPv4 addr */
  470. memset(&sa_storage, 0, sizeof(sa_storage));
  471. sin = (struct sockaddr_in *)&sa_storage;
  472. sin->sin_family = AF_INET;
  473. sin->sin_addr.s_addr = htonl(0x7f010203); /* 127.1.2.3 */
  474. tor_addr_from_sockaddr(&t1, (struct sockaddr *)sin, NULL);
  475. /* Check IPv4 PTR - too short buffer */
  476. test_eq(tor_addr_to_PTR_name(rbuf, 1, &t1), -1);
  477. test_eq(tor_addr_to_PTR_name(rbuf,
  478. strlen("3.2.1.127.in-addr.arpa") - 1,
  479. &t1), -1);
  480. /* Check IPv4 PTR - valid addr */
  481. test_eq(tor_addr_to_PTR_name(rbuf, sizeof(rbuf), &t1),
  482. strlen("3.2.1.127.in-addr.arpa"));
  483. test_streq(rbuf, "3.2.1.127.in-addr.arpa");
  484. /* Invalid addr family */
  485. t1.family = AF_UNSPEC;
  486. test_eq(tor_addr_to_PTR_name(rbuf, sizeof(rbuf), &t1), -1);
  487. /* Stage IPv6 addr */
  488. memset(&sa_storage, 0, sizeof(sa_storage));
  489. sin6 = (struct sockaddr_in6 *)&sa_storage;
  490. sin6->sin6_family = AF_INET6;
  491. sin6->sin6_addr.s6_addr[0] = 0x80; /* 8000::abcd */
  492. sin6->sin6_addr.s6_addr[14] = 0xab;
  493. sin6->sin6_addr.s6_addr[15] = 0xcd;
  494. tor_addr_from_sockaddr(&t1, (struct sockaddr *)sin6, NULL);
  495. {
  496. const char* addr_PTR = "d.c.b.a.0.0.0.0.0.0.0.0.0.0.0.0."
  497. "0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.ip6.arpa";
  498. /* Check IPv6 PTR - too short buffer */
  499. test_eq(tor_addr_to_PTR_name(rbuf, 0, &t1), -1);
  500. test_eq(tor_addr_to_PTR_name(rbuf, strlen(addr_PTR) - 1, &t1), -1);
  501. /* Check IPv6 PTR - valid addr */
  502. test_eq(tor_addr_to_PTR_name(rbuf, sizeof(rbuf), &t1),
  503. strlen(addr_PTR));
  504. test_streq(rbuf, addr_PTR);
  505. }
  506. /* XXXX turn this into a separate function; it's not all IPv6. */
  507. /* test tor_addr_parse_mask_ports */
  508. test_addr_mask_ports_parse("[::f]/17:47-95", AF_INET6,
  509. 0, 0, 0, 0x0000000f, 17, 47, 95);
  510. test_streq(p1, "::f");
  511. //test_addr_parse("[::fefe:4.1.1.7/120]:999-1000");
  512. //test_addr_parse_check("::fefe:401:107", 120, 999, 1000);
  513. test_addr_mask_ports_parse("[::ffff:4.1.1.7]/120:443", AF_INET6,
  514. 0, 0, 0x0000ffff, 0x04010107, 120, 443, 443);
  515. test_streq(p1, "::ffff:4.1.1.7");
  516. test_addr_mask_ports_parse("[abcd:2::44a:0]:2-65000", AF_INET6,
  517. 0xabcd0002, 0, 0, 0x044a0000, 128, 2, 65000);
  518. test_streq(p1, "abcd:2::44a:0");
  519. /* Try some long addresses. */
  520. r=tor_addr_parse_mask_ports("[ffff:1111:1111:1111:1111:1111:1111:1111]",
  521. 0, &t1, NULL, NULL, NULL);
  522. test_assert(r == AF_INET6);
  523. r=tor_addr_parse_mask_ports("[ffff:1111:1111:1111:1111:1111:1111:11111]",
  524. 0, &t1, NULL, NULL, NULL);
  525. test_assert(r == -1);
  526. r=tor_addr_parse_mask_ports("[ffff:1111:1111:1111:1111:1111:1111:1111:1]",
  527. 0, &t1, NULL, NULL, NULL);
  528. test_assert(r == -1);
  529. r=tor_addr_parse_mask_ports(
  530. "[ffff:1111:1111:1111:1111:1111:1111:ffff:"
  531. "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:"
  532. "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:"
  533. "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]",
  534. 0, &t1, NULL, NULL, NULL);
  535. test_assert(r == -1);
  536. /* Try some failing cases. */
  537. r=tor_addr_parse_mask_ports("[fefef::]/112", 0, &t1, NULL, NULL, NULL);
  538. test_assert(r == -1);
  539. r=tor_addr_parse_mask_ports("[fefe::/112", 0, &t1, NULL, NULL, NULL);
  540. test_assert(r == -1);
  541. r=tor_addr_parse_mask_ports("[fefe::", 0, &t1, NULL, NULL, NULL);
  542. test_assert(r == -1);
  543. r=tor_addr_parse_mask_ports("[fefe::X]", 0, &t1, NULL, NULL, NULL);
  544. test_assert(r == -1);
  545. r=tor_addr_parse_mask_ports("efef::/112", 0, &t1, NULL, NULL, NULL);
  546. test_assert(r == -1);
  547. r=tor_addr_parse_mask_ports("[f:f:f:f:f:f:f:f::]",0,&t1, NULL, NULL, NULL);
  548. test_assert(r == -1);
  549. r=tor_addr_parse_mask_ports("[::f:f:f:f:f:f:f:f]",0,&t1, NULL, NULL, NULL);
  550. test_assert(r == -1);
  551. r=tor_addr_parse_mask_ports("[f:f:f:f:f:f:f:f:f]",0,&t1, NULL, NULL, NULL);
  552. test_assert(r == -1);
  553. r=tor_addr_parse_mask_ports("[f:f:f:f:f::]/fred",0,&t1,&mask, NULL, NULL);
  554. test_assert(r == -1);
  555. r=tor_addr_parse_mask_ports("[f:f:f:f:f::]/255.255.0.0",
  556. 0,&t1, NULL, NULL, NULL);
  557. test_assert(r == -1);
  558. /* This one will get rejected because it isn't a pure prefix. */
  559. r=tor_addr_parse_mask_ports("1.1.2.3/255.255.64.0",0,&t1, &mask,NULL,NULL);
  560. test_assert(r == -1);
  561. /* Test for V4-mapped address with mask < 96. (arguably not valid) */
  562. r=tor_addr_parse_mask_ports("[::ffff:1.1.2.2/33]",0,&t1, &mask, NULL, NULL);
  563. test_assert(r == -1);
  564. r=tor_addr_parse_mask_ports("1.1.2.2/33",0,&t1, &mask, NULL, NULL);
  565. test_assert(r == -1);
  566. /* Try extended wildcard addresses with out TAPMP_EXTENDED_STAR*/
  567. r=tor_addr_parse_mask_ports("*4",0,&t1, &mask, NULL, NULL);
  568. test_assert(r == -1);
  569. r=tor_addr_parse_mask_ports("*6",0,&t1, &mask, NULL, NULL);
  570. test_assert(r == -1);
  571. #if 0
  572. /* Try a mask with a wildcard. */
  573. r=tor_addr_parse_mask_ports("*/16",0,&t1, &mask, NULL, NULL);
  574. test_assert(r == -1);
  575. r=tor_addr_parse_mask_ports("*4/16",TAPMP_EXTENDED_STAR,
  576. &t1, &mask, NULL, NULL);
  577. test_assert(r == -1);
  578. r=tor_addr_parse_mask_ports("*6/30",TAPMP_EXTENDED_STAR,
  579. &t1, &mask, NULL, NULL);
  580. test_assert(r == -1);
  581. #endif
  582. /* Basic mask tests*/
  583. r=tor_addr_parse_mask_ports("1.1.2.2/31",0,&t1, &mask, NULL, NULL);
  584. test_assert(r == AF_INET);
  585. tt_int_op(mask,==,31);
  586. tt_int_op(tor_addr_family(&t1),==,AF_INET);
  587. tt_int_op(tor_addr_to_ipv4h(&t1),==,0x01010202);
  588. r=tor_addr_parse_mask_ports("3.4.16.032:1-2",0,&t1, &mask, &port1, &port2);
  589. test_assert(r == AF_INET);
  590. tt_int_op(mask,==,32);
  591. tt_int_op(tor_addr_family(&t1),==,AF_INET);
  592. tt_int_op(tor_addr_to_ipv4h(&t1),==,0x03041020);
  593. test_assert(port1 == 1);
  594. test_assert(port2 == 2);
  595. r=tor_addr_parse_mask_ports("1.1.2.3/255.255.128.0",0,&t1, &mask,NULL,NULL);
  596. test_assert(r == AF_INET);
  597. tt_int_op(mask,==,17);
  598. tt_int_op(tor_addr_family(&t1),==,AF_INET);
  599. tt_int_op(tor_addr_to_ipv4h(&t1),==,0x01010203);
  600. r=tor_addr_parse_mask_ports("[efef::]/112",0,&t1, &mask, &port1, &port2);
  601. test_assert(r == AF_INET6);
  602. test_assert(port1 == 1);
  603. test_assert(port2 == 65535);
  604. /* Try regular wildcard behavior without TAPMP_EXTENDED_STAR */
  605. r=tor_addr_parse_mask_ports("*:80-443",0,&t1,&mask,&port1,&port2);
  606. tt_int_op(r,==,AF_INET); /* Old users of this always get inet */
  607. tt_int_op(tor_addr_family(&t1),==,AF_INET);
  608. tt_int_op(tor_addr_to_ipv4h(&t1),==,0);
  609. tt_int_op(mask,==,0);
  610. tt_int_op(port1,==,80);
  611. tt_int_op(port2,==,443);
  612. /* Now try wildcards *with* TAPMP_EXTENDED_STAR */
  613. r=tor_addr_parse_mask_ports("*:8000-9000",TAPMP_EXTENDED_STAR,
  614. &t1,&mask,&port1,&port2);
  615. tt_int_op(r,==,AF_UNSPEC);
  616. tt_int_op(tor_addr_family(&t1),==,AF_UNSPEC);
  617. tt_int_op(mask,==,0);
  618. tt_int_op(port1,==,8000);
  619. tt_int_op(port2,==,9000);
  620. r=tor_addr_parse_mask_ports("*4:6667",TAPMP_EXTENDED_STAR,
  621. &t1,&mask,&port1,&port2);
  622. tt_int_op(r,==,AF_INET);
  623. tt_int_op(tor_addr_family(&t1),==,AF_INET);
  624. tt_int_op(tor_addr_to_ipv4h(&t1),==,0);
  625. tt_int_op(mask,==,0);
  626. tt_int_op(port1,==,6667);
  627. tt_int_op(port2,==,6667);
  628. r=tor_addr_parse_mask_ports("*6",TAPMP_EXTENDED_STAR,
  629. &t1,&mask,&port1,&port2);
  630. tt_int_op(r,==,AF_INET6);
  631. tt_int_op(tor_addr_family(&t1),==,AF_INET6);
  632. tt_assert(tor_mem_is_zero((const char*)tor_addr_to_in6_addr32(&t1), 16));
  633. tt_int_op(mask,==,0);
  634. tt_int_op(port1,==,1);
  635. tt_int_op(port2,==,65535);
  636. /* make sure inet address lengths >= max */
  637. test_assert(INET_NTOA_BUF_LEN >= sizeof("255.255.255.255"));
  638. test_assert(TOR_ADDR_BUF_LEN >=
  639. sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"));
  640. test_assert(sizeof(tor_addr_t) >= sizeof(struct in6_addr));
  641. /* get interface addresses */
  642. r = get_interface_address6(LOG_DEBUG, AF_INET, &t1);
  643. i = get_interface_address6(LOG_DEBUG, AF_INET6, &t2);
  644. TT_BLATHER(("v4 address: %s (family=%d)", fmt_addr(&t1),
  645. tor_addr_family(&t1)));
  646. TT_BLATHER(("v6 address: %s (family=%d)", fmt_addr(&t2),
  647. tor_addr_family(&t2)));
  648. done:
  649. ;
  650. }
  651. /** Test tor_addr_port_parse(). */
  652. static void
  653. test_addr_parse(void)
  654. {
  655. int r;
  656. tor_addr_t addr;
  657. char buf[TOR_ADDR_BUF_LEN];
  658. uint16_t port = 0;
  659. /* Correct call. */
  660. r= tor_addr_port_parse(LOG_DEBUG,
  661. "192.0.2.1:1234",
  662. &addr, &port);
  663. test_assert(r == 0);
  664. tor_addr_to_str(buf, &addr, sizeof(buf), 0);
  665. test_streq(buf, "192.0.2.1");
  666. test_eq(port, 1234);
  667. /* Domain name. */
  668. r= tor_addr_port_parse(LOG_DEBUG,
  669. "torproject.org:1234",
  670. &addr, &port);
  671. test_assert(r == -1);
  672. /* Only IP. */
  673. r= tor_addr_port_parse(LOG_DEBUG,
  674. "192.0.2.2",
  675. &addr, &port);
  676. test_assert(r == -1);
  677. /* Bad port. */
  678. r= tor_addr_port_parse(LOG_DEBUG,
  679. "192.0.2.2:66666",
  680. &addr, &port);
  681. test_assert(r == -1);
  682. /* Only domain name */
  683. r= tor_addr_port_parse(LOG_DEBUG,
  684. "torproject.org",
  685. &addr, &port);
  686. test_assert(r == -1);
  687. /* Bad IP address */
  688. r= tor_addr_port_parse(LOG_DEBUG,
  689. "192.0.2:1234",
  690. &addr, &port);
  691. test_assert(r == -1);
  692. done:
  693. ;
  694. }
  695. #define ADDR_LEGACY(name) \
  696. { #name, legacy_test_helper, 0, &legacy_setup, test_addr_ ## name }
  697. struct testcase_t addr_tests[] = {
  698. ADDR_LEGACY(basic),
  699. ADDR_LEGACY(ip6_helpers),
  700. ADDR_LEGACY(parse),
  701. END_OF_TESTCASES
  702. };