test_addr.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2016, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #define ADDRESSMAP_PRIVATE
  6. #include "orconfig.h"
  7. #include "or.h"
  8. #include "test.h"
  9. #include "addressmap.h"
  10. static void
  11. test_addr_basic(void *arg)
  12. {
  13. uint32_t u32;
  14. uint16_t u16;
  15. char *cp;
  16. /* Test addr_port_lookup */
  17. (void)arg;
  18. cp = NULL; u32 = 3; u16 = 3;
  19. tt_assert(!addr_port_lookup(LOG_WARN, "1.2.3.4", &cp, &u32, &u16));
  20. tt_str_op(cp,OP_EQ, "1.2.3.4");
  21. tt_int_op(u32,OP_EQ, 0x01020304u);
  22. tt_int_op(u16,OP_EQ, 0);
  23. tor_free(cp);
  24. tt_assert(!addr_port_lookup(LOG_WARN, "4.3.2.1:99", &cp, &u32, &u16));
  25. tt_str_op(cp,OP_EQ, "4.3.2.1");
  26. tt_int_op(u32,OP_EQ, 0x04030201u);
  27. tt_int_op(u16,OP_EQ, 99);
  28. tor_free(cp);
  29. tt_assert(!addr_port_lookup(LOG_WARN, "nonexistent.address:4040",
  30. &cp, NULL, &u16));
  31. tt_str_op(cp,OP_EQ, "nonexistent.address");
  32. tt_int_op(u16,OP_EQ, 4040);
  33. tor_free(cp);
  34. tt_assert(!addr_port_lookup(LOG_WARN, "localhost:9999", &cp, &u32, &u16));
  35. tt_str_op(cp,OP_EQ, "localhost");
  36. tt_int_op(u32,OP_EQ, 0x7f000001u);
  37. tt_int_op(u16,OP_EQ, 9999);
  38. tor_free(cp);
  39. u32 = 3;
  40. tt_assert(!addr_port_lookup(LOG_WARN, "localhost", NULL, &u32, &u16));
  41. tt_ptr_op(cp,OP_EQ, NULL);
  42. tt_int_op(u32,OP_EQ, 0x7f000001u);
  43. tt_int_op(u16,OP_EQ, 0);
  44. tor_free(cp);
  45. tt_assert(addr_port_lookup(LOG_WARN, "localhost:3", &cp, &u32, NULL));
  46. tor_free(cp);
  47. tt_int_op(0,OP_EQ, addr_mask_get_bits(0x0u));
  48. tt_int_op(32,OP_EQ, addr_mask_get_bits(0xFFFFFFFFu));
  49. tt_int_op(16,OP_EQ, addr_mask_get_bits(0xFFFF0000u));
  50. tt_int_op(31,OP_EQ, addr_mask_get_bits(0xFFFFFFFEu));
  51. tt_int_op(1,OP_EQ, addr_mask_get_bits(0x80000000u));
  52. /* Test inet_ntop */
  53. {
  54. char tmpbuf[TOR_ADDR_BUF_LEN];
  55. const char *ip = "176.192.208.224";
  56. struct in_addr in;
  57. /* good round trip */
  58. tt_int_op(tor_inet_pton(AF_INET, ip, &in), OP_EQ, 1);
  59. tt_ptr_op(tor_inet_ntop(AF_INET, &in, tmpbuf, sizeof(tmpbuf)),
  60. OP_EQ, &tmpbuf);
  61. tt_str_op(tmpbuf,OP_EQ, ip);
  62. /* just enough buffer length */
  63. tt_str_op(tor_inet_ntop(AF_INET, &in, tmpbuf, strlen(ip) + 1), OP_EQ, ip);
  64. /* too short buffer */
  65. tt_ptr_op(tor_inet_ntop(AF_INET, &in, tmpbuf, strlen(ip)),OP_EQ, NULL);
  66. }
  67. done:
  68. tor_free(cp);
  69. }
  70. #define test_op_ip6_(a,op,b,e1,e2) \
  71. STMT_BEGIN \
  72. tt_assert_test_fmt_type(a,b,e1" "#op" "e2,struct in6_addr*, \
  73. (memcmp(val1_->s6_addr, val2_->s6_addr, 16) op 0), \
  74. char *, "%s", \
  75. { int i; char *cp; \
  76. cp = print_ = tor_malloc(64); \
  77. for (i=0;i<16;++i) { \
  78. tor_snprintf(cp, 3,"%02x", (unsigned)value_->s6_addr[i]);\
  79. cp += 2; \
  80. if (i != 15) *cp++ = ':'; \
  81. } \
  82. }, \
  83. { tor_free(print_); }, \
  84. TT_EXIT_TEST_FUNCTION \
  85. ); \
  86. STMT_END
  87. /** Helper: Assert that two strings both decode as IPv6 addresses with
  88. * tor_inet_pton(), and both decode to the same address. */
  89. #define test_pton6_same(a,b) STMT_BEGIN \
  90. tt_int_op(tor_inet_pton(AF_INET6, a, &a1), OP_EQ, 1); \
  91. tt_int_op(tor_inet_pton(AF_INET6, b, &a2), OP_EQ, 1); \
  92. test_op_ip6_(&a1,OP_EQ,&a2,#a,#b); \
  93. STMT_END
  94. /** Helper: Assert that <b>a</b> is recognized as a bad IPv6 address by
  95. * tor_inet_pton(). */
  96. #define test_pton6_bad(a) \
  97. tt_int_op(0, OP_EQ, tor_inet_pton(AF_INET6, a, &a1))
  98. /** Helper: assert that <b>a</b>, when parsed by tor_inet_pton() and displayed
  99. * with tor_inet_ntop(), yields <b>b</b>. Also assert that <b>b</b> parses to
  100. * the same value as <b>a</b>. */
  101. #define test_ntop6_reduces(a,b) STMT_BEGIN \
  102. tt_int_op(tor_inet_pton(AF_INET6, a, &a1), OP_EQ, 1); \
  103. tt_str_op(tor_inet_ntop(AF_INET6, &a1, buf, sizeof(buf)), OP_EQ, b); \
  104. tt_int_op(tor_inet_pton(AF_INET6, b, &a2), OP_EQ, 1); \
  105. test_op_ip6_(&a1, OP_EQ, &a2, a, b); \
  106. STMT_END
  107. /** Helper: assert that <b>a</b> parses by tor_inet_pton() into a address that
  108. * passes tor_addr_is_internal() with <b>for_listening</b>. */
  109. #define test_internal_ip(a,for_listening) STMT_BEGIN \
  110. tt_int_op(tor_inet_pton(AF_INET6, a, &t1.addr.in6_addr), OP_EQ, 1); \
  111. t1.family = AF_INET6; \
  112. if (!tor_addr_is_internal(&t1, for_listening)) \
  113. TT_DIE(("%s was not internal", a)); \
  114. STMT_END
  115. /** Helper: assert that <b>a</b> parses by tor_inet_pton() into a address that
  116. * does not pass tor_addr_is_internal() with <b>for_listening</b>. */
  117. #define test_external_ip(a,for_listening) STMT_BEGIN \
  118. tt_int_op(tor_inet_pton(AF_INET6, a, &t1.addr.in6_addr), OP_EQ, 1); \
  119. t1.family = AF_INET6; \
  120. if (tor_addr_is_internal(&t1, for_listening)) \
  121. TT_DIE(("%s was not internal", a)); \
  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(). */
  126. #define test_addr_compare(a, op, b) STMT_BEGIN \
  127. tt_int_op(tor_inet_pton(AF_INET6, a, &t1.addr.in6_addr), OP_EQ, 1); \
  128. tt_int_op(tor_inet_pton(AF_INET6, b, &t2.addr.in6_addr), OP_EQ, 1); \
  129. t1.family = t2.family = AF_INET6; \
  130. r = tor_addr_compare(&t1,&t2,CMP_SEMANTIC); \
  131. if (!(r op 0)) \
  132. TT_DIE(("Failed: tor_addr_compare(%s,%s) %s 0", a, b, #op));\
  133. STMT_END
  134. /** Helper: Assert that <b>a</b> and <b>b</b>, when parsed by
  135. * tor_inet_pton(), give addresses that compare in the order defined by
  136. * <b>op</b> with tor_addr_compare_masked() with <b>m</b> masked. */
  137. #define test_addr_compare_masked(a, op, b, m) STMT_BEGIN \
  138. tt_int_op(tor_inet_pton(AF_INET6, a, &t1.addr.in6_addr), OP_EQ, 1); \
  139. tt_int_op(tor_inet_pton(AF_INET6, b, &t2.addr.in6_addr), OP_EQ, 1); \
  140. t1.family = t2.family = AF_INET6; \
  141. r = tor_addr_compare_masked(&t1,&t2,m,CMP_SEMANTIC); \
  142. if (!(r op 0)) \
  143. TT_DIE(("Failed: tor_addr_compare_masked(%s,%s,%d) %s 0", \
  144. a, b, m, #op)); \
  145. STMT_END
  146. /** Helper: assert that <b>xx</b> is parseable as a masked IPv6 address with
  147. * ports by tor_parse_mask_addr_ports(), with family <b>f</b>, IP address
  148. * as 4 32-bit words <b>ip1...ip4</b>, mask bits as <b>mm</b>, and port range
  149. * as <b>pt1..pt2</b>. */
  150. #define test_addr_mask_ports_parse(xx, f, ip1, ip2, ip3, ip4, mm, pt1, pt2) \
  151. STMT_BEGIN \
  152. tt_int_op(tor_addr_parse_mask_ports(xx, 0, &t1, &mask, &port1, &port2), \
  153. OP_EQ, f); \
  154. p1=tor_inet_ntop(AF_INET6, &t1.addr.in6_addr, bug, sizeof(bug)); \
  155. tt_int_op(htonl(ip1), OP_EQ, tor_addr_to_in6_addr32(&t1)[0]); \
  156. tt_int_op(htonl(ip2), OP_EQ, tor_addr_to_in6_addr32(&t1)[1]); \
  157. tt_int_op(htonl(ip3), OP_EQ, tor_addr_to_in6_addr32(&t1)[2]); \
  158. tt_int_op(htonl(ip4), OP_EQ, tor_addr_to_in6_addr32(&t1)[3]); \
  159. tt_int_op(mask, OP_EQ, mm); \
  160. tt_uint_op(port1, OP_EQ, pt1); \
  161. tt_uint_op(port2, OP_EQ, pt2); \
  162. STMT_END
  163. /** Run unit tests for IPv6 encoding/decoding/manipulation functions. */
  164. static void
  165. test_addr_ip6_helpers(void *arg)
  166. {
  167. char buf[TOR_ADDR_BUF_LEN], bug[TOR_ADDR_BUF_LEN];
  168. char rbuf[REVERSE_LOOKUP_NAME_BUF_LEN];
  169. struct in6_addr a1, a2;
  170. tor_addr_t t1, t2;
  171. int r, i;
  172. uint16_t port1, port2;
  173. maskbits_t mask;
  174. const char *p1;
  175. struct sockaddr_storage sa_storage;
  176. struct sockaddr_in *sin;
  177. struct sockaddr_in6 *sin6;
  178. /* Test tor_inet_ntop and tor_inet_pton: IPv6 */
  179. (void)arg;
  180. {
  181. const char *ip = "2001::1234";
  182. const char *ip_ffff = "::ffff:192.168.1.2";
  183. /* good round trip */
  184. tt_int_op(tor_inet_pton(AF_INET6, ip, &a1),OP_EQ, 1);
  185. tt_ptr_op(tor_inet_ntop(AF_INET6, &a1, buf, sizeof(buf)),OP_EQ, &buf);
  186. tt_str_op(buf,OP_EQ, ip);
  187. /* good round trip - ::ffff:0:0 style */
  188. tt_int_op(tor_inet_pton(AF_INET6, ip_ffff, &a2),OP_EQ, 1);
  189. tt_ptr_op(tor_inet_ntop(AF_INET6, &a2, buf, sizeof(buf)),OP_EQ, &buf);
  190. tt_str_op(buf,OP_EQ, ip_ffff);
  191. /* just long enough buffer (remember \0) */
  192. tt_str_op(tor_inet_ntop(AF_INET6, &a1, buf, strlen(ip)+1),OP_EQ, ip);
  193. tt_str_op(tor_inet_ntop(AF_INET6, &a2, buf, strlen(ip_ffff)+1),OP_EQ,
  194. ip_ffff);
  195. /* too short buffer (remember \0) */
  196. tt_ptr_op(tor_inet_ntop(AF_INET6, &a1, buf, strlen(ip)),OP_EQ, NULL);
  197. tt_ptr_op(tor_inet_ntop(AF_INET6, &a2, buf, strlen(ip_ffff)),OP_EQ, NULL);
  198. }
  199. /* ==== Converting to and from sockaddr_t. */
  200. sin = (struct sockaddr_in *)&sa_storage;
  201. sin->sin_family = AF_INET;
  202. sin->sin_port = htons(9090);
  203. sin->sin_addr.s_addr = htonl(0x7f7f0102); /*127.127.1.2*/
  204. tor_addr_from_sockaddr(&t1, (struct sockaddr *)sin, &port1);
  205. tt_int_op(tor_addr_family(&t1),OP_EQ, AF_INET);
  206. tt_int_op(tor_addr_to_ipv4h(&t1),OP_EQ, 0x7f7f0102);
  207. tt_int_op(port1, OP_EQ, 9090);
  208. memset(&sa_storage, 0, sizeof(sa_storage));
  209. tt_int_op(sizeof(struct sockaddr_in),OP_EQ,
  210. tor_addr_to_sockaddr(&t1, 1234, (struct sockaddr *)&sa_storage,
  211. sizeof(sa_storage)));
  212. tt_int_op(1234,OP_EQ, ntohs(sin->sin_port));
  213. tt_int_op(0x7f7f0102,OP_EQ, ntohl(sin->sin_addr.s_addr));
  214. memset(&sa_storage, 0, sizeof(sa_storage));
  215. sin6 = (struct sockaddr_in6 *)&sa_storage;
  216. sin6->sin6_family = AF_INET6;
  217. sin6->sin6_port = htons(7070);
  218. sin6->sin6_addr.s6_addr[0] = 128;
  219. tor_addr_from_sockaddr(&t1, (struct sockaddr *)sin6, &port1);
  220. tt_int_op(tor_addr_family(&t1),OP_EQ, AF_INET6);
  221. tt_int_op(port1, OP_EQ, 7070);
  222. p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 0);
  223. tt_str_op(p1,OP_EQ, "8000::");
  224. memset(&sa_storage, 0, sizeof(sa_storage));
  225. tt_int_op(sizeof(struct sockaddr_in6),OP_EQ,
  226. tor_addr_to_sockaddr(&t1, 9999, (struct sockaddr *)&sa_storage,
  227. sizeof(sa_storage)));
  228. tt_int_op(AF_INET6,OP_EQ, sin6->sin6_family);
  229. tt_int_op(9999,OP_EQ, ntohs(sin6->sin6_port));
  230. tt_int_op(0x80000000,OP_EQ, ntohl(S6_ADDR32(sin6->sin6_addr)[0]));
  231. /* ==== tor_addr_lookup: static cases. (Can't test dns without knowing we
  232. * have a good resolver. */
  233. tt_int_op(0,OP_EQ, tor_addr_lookup("127.128.129.130", AF_UNSPEC, &t1));
  234. tt_int_op(AF_INET,OP_EQ, tor_addr_family(&t1));
  235. tt_int_op(tor_addr_to_ipv4h(&t1),OP_EQ, 0x7f808182);
  236. tt_int_op(0,OP_EQ, tor_addr_lookup("9000::5", AF_UNSPEC, &t1));
  237. tt_int_op(AF_INET6,OP_EQ, tor_addr_family(&t1));
  238. tt_int_op(0x90,OP_EQ, tor_addr_to_in6_addr8(&t1)[0]);
  239. tt_assert(tor_mem_is_zero((char*)tor_addr_to_in6_addr8(&t1)+1, 14));
  240. tt_int_op(0x05,OP_EQ, tor_addr_to_in6_addr8(&t1)[15]);
  241. /* === Test pton: valid af_inet6 */
  242. /* Simple, valid parsing. */
  243. r = tor_inet_pton(AF_INET6,
  244. "0102:0304:0506:0708:090A:0B0C:0D0E:0F10", &a1);
  245. tt_int_op(r, OP_EQ, 1);
  246. for (i=0;i<16;++i) { tt_int_op(i+1,OP_EQ, (int)a1.s6_addr[i]); }
  247. /* ipv4 ending. */
  248. test_pton6_same("0102:0304:0506:0708:090A:0B0C:0D0E:0F10",
  249. "0102:0304:0506:0708:090A:0B0C:13.14.15.16");
  250. /* shortened words. */
  251. test_pton6_same("0001:0099:BEEF:0000:0123:FFFF:0001:0001",
  252. "1:99:BEEF:0:0123:FFFF:1:1");
  253. /* zeros at the beginning */
  254. test_pton6_same("0000:0000:0000:0000:0009:C0A8:0001:0001",
  255. "::9:c0a8:1:1");
  256. test_pton6_same("0000:0000:0000:0000:0009:C0A8:0001:0001",
  257. "::9:c0a8:0.1.0.1");
  258. /* zeros in the middle. */
  259. test_pton6_same("fe80:0000:0000:0000:0202:1111:0001:0001",
  260. "fe80::202:1111:1:1");
  261. /* zeros at the end. */
  262. test_pton6_same("1000:0001:0000:0007:0000:0000:0000:0000",
  263. "1000:1:0:7::");
  264. /* === Test ntop: af_inet6 */
  265. test_ntop6_reduces("0:0:0:0:0:0:0:0", "::");
  266. test_ntop6_reduces("0001:0099:BEEF:0006:0123:FFFF:0001:0001",
  267. "1:99:beef:6:123:ffff:1:1");
  268. //test_ntop6_reduces("0:0:0:0:0:0:c0a8:0101", "::192.168.1.1");
  269. test_ntop6_reduces("0:0:0:0:0:ffff:c0a8:0101", "::ffff:192.168.1.1");
  270. test_ntop6_reduces("0:0:0:0:0:0:c0a8:0101", "::192.168.1.1");
  271. test_ntop6_reduces("002:0:0000:0:3::4", "2::3:0:0:4");
  272. test_ntop6_reduces("0:0::1:0:3", "::1:0:3");
  273. test_ntop6_reduces("008:0::0", "8::");
  274. test_ntop6_reduces("0:0:0:0:0:ffff::1", "::ffff:0.0.0.1");
  275. test_ntop6_reduces("abcd:0:0:0:0:0:7f00::", "abcd::7f00:0");
  276. test_ntop6_reduces("0000:0000:0000:0000:0009:C0A8:0001:0001",
  277. "::9:c0a8:1:1");
  278. test_ntop6_reduces("fe80:0000:0000:0000:0202:1111:0001:0001",
  279. "fe80::202:1111:1:1");
  280. test_ntop6_reduces("1000:0001:0000:0007:0000:0000:0000:0000",
  281. "1000:1:0:7::");
  282. /* Bad af param */
  283. tt_int_op(tor_inet_pton(AF_UNSPEC, 0, 0),OP_EQ, -1);
  284. /* === Test pton: invalid in6. */
  285. test_pton6_bad("foobar.");
  286. test_pton6_bad("-1::");
  287. test_pton6_bad("00001::");
  288. test_pton6_bad("10000::");
  289. test_pton6_bad("::10000");
  290. test_pton6_bad("55555::");
  291. test_pton6_bad("9:-60::");
  292. test_pton6_bad("9:+60::");
  293. test_pton6_bad("9|60::");
  294. test_pton6_bad("0x60::");
  295. test_pton6_bad("::0x60");
  296. test_pton6_bad("9:0x60::");
  297. test_pton6_bad("1:2:33333:4:0002:3::");
  298. test_pton6_bad("1:2:3333:4:fish:3::");
  299. test_pton6_bad("1:2:3:4:5:6:7:8:9");
  300. test_pton6_bad("1:2:3:4:5:6:7");
  301. test_pton6_bad("1:2:3:4:5:6:1.2.3.4.5");
  302. test_pton6_bad("1:2:3:4:5:6:1.2.3");
  303. test_pton6_bad("::1.2.3");
  304. test_pton6_bad("::1.2.3.4.5");
  305. test_pton6_bad("::ffff:0xff.0.0.0");
  306. test_pton6_bad("::ffff:ff.0.0.0");
  307. test_pton6_bad("::ffff:256.0.0.0");
  308. test_pton6_bad("::ffff:-1.0.0.0");
  309. test_pton6_bad("99");
  310. test_pton6_bad("");
  311. test_pton6_bad(".");
  312. test_pton6_bad(":");
  313. test_pton6_bad("1::2::3:4");
  314. test_pton6_bad("a:::b:c");
  315. test_pton6_bad(":::a:b:c");
  316. test_pton6_bad("a:b:c:::");
  317. test_pton6_bad("1.2.3.4");
  318. test_pton6_bad(":1.2.3.4");
  319. test_pton6_bad(".2.3.4");
  320. /* Regression tests for 22789. */
  321. test_pton6_bad("0xfoo");
  322. test_pton6_bad("0x88");
  323. test_pton6_bad("0xyxxy");
  324. test_pton6_bad("0XFOO");
  325. test_pton6_bad("0X88");
  326. test_pton6_bad("0XYXXY");
  327. test_pton6_bad("0x");
  328. test_pton6_bad("0X");
  329. /* test internal checking */
  330. test_external_ip("fbff:ffff::2:7", 0);
  331. test_internal_ip("fc01::2:7", 0);
  332. test_internal_ip("fc01::02:7", 0);
  333. test_internal_ip("fc01::002:7", 0);
  334. test_internal_ip("fc01::0002:7", 0);
  335. test_internal_ip("fdff:ffff::f:f", 0);
  336. test_external_ip("fe00::3:f", 0);
  337. test_external_ip("fe7f:ffff::2:7", 0);
  338. test_internal_ip("fe80::2:7", 0);
  339. test_internal_ip("febf:ffff::f:f", 0);
  340. test_internal_ip("fec0::2:7:7", 0);
  341. test_internal_ip("feff:ffff::e:7:7", 0);
  342. test_external_ip("ff00::e:7:7", 0);
  343. test_internal_ip("::", 0);
  344. test_internal_ip("::1", 0);
  345. test_internal_ip("::1", 1);
  346. test_internal_ip("::", 0);
  347. test_external_ip("::", 1);
  348. test_external_ip("::2", 0);
  349. test_external_ip("2001::", 0);
  350. test_external_ip("ffff::", 0);
  351. test_external_ip("::ffff:0.0.0.0", 1);
  352. test_internal_ip("::ffff:0.0.0.0", 0);
  353. test_internal_ip("::ffff:0.255.255.255", 0);
  354. test_external_ip("::ffff:1.0.0.0", 0);
  355. test_external_ip("::ffff:9.255.255.255", 0);
  356. test_internal_ip("::ffff:10.0.0.0", 0);
  357. test_internal_ip("::ffff:10.255.255.255", 0);
  358. test_external_ip("::ffff:11.0.0.0", 0);
  359. test_external_ip("::ffff:126.255.255.255", 0);
  360. test_internal_ip("::ffff:127.0.0.0", 0);
  361. test_internal_ip("::ffff:127.255.255.255", 0);
  362. test_external_ip("::ffff:128.0.0.0", 0);
  363. test_external_ip("::ffff:172.15.255.255", 0);
  364. test_internal_ip("::ffff:172.16.0.0", 0);
  365. test_internal_ip("::ffff:172.31.255.255", 0);
  366. test_external_ip("::ffff:172.32.0.0", 0);
  367. test_external_ip("::ffff:192.167.255.255", 0);
  368. test_internal_ip("::ffff:192.168.0.0", 0);
  369. test_internal_ip("::ffff:192.168.255.255", 0);
  370. test_external_ip("::ffff:192.169.0.0", 0);
  371. test_external_ip("::ffff:169.253.255.255", 0);
  372. test_internal_ip("::ffff:169.254.0.0", 0);
  373. test_internal_ip("::ffff:169.254.255.255", 0);
  374. test_external_ip("::ffff:169.255.0.0", 0);
  375. /* tor_addr_compare(tor_addr_t x2) */
  376. test_addr_compare("ffff::", OP_EQ, "ffff::0");
  377. test_addr_compare("0::3:2:1", OP_LT, "0::ffff:0.3.2.1");
  378. test_addr_compare("0::2:2:1", OP_LT, "0::ffff:0.3.2.1");
  379. test_addr_compare("0::ffff:0.3.2.1", OP_GT, "0::0:0:0");
  380. test_addr_compare("0::ffff:5.2.2.1", OP_LT,
  381. "::ffff:6.0.0.0"); /* XXXX wrong. */
  382. tor_addr_parse_mask_ports("[::ffff:2.3.4.5]", 0, &t1, NULL, NULL, NULL);
  383. tor_addr_parse_mask_ports("2.3.4.5", 0, &t2, NULL, NULL, NULL);
  384. tt_assert(tor_addr_compare(&t1, &t2, CMP_SEMANTIC) == 0);
  385. tor_addr_parse_mask_ports("[::ffff:2.3.4.4]", 0, &t1, NULL, NULL, NULL);
  386. tor_addr_parse_mask_ports("2.3.4.5", 0, &t2, NULL, NULL, NULL);
  387. tt_assert(tor_addr_compare(&t1, &t2, CMP_SEMANTIC) < 0);
  388. /* test compare_masked */
  389. test_addr_compare_masked("ffff::", OP_EQ, "ffff::0", 128);
  390. test_addr_compare_masked("ffff::", OP_EQ, "ffff::0", 64);
  391. test_addr_compare_masked("0::2:2:1", OP_LT, "0::8000:2:1", 81);
  392. test_addr_compare_masked("0::2:2:1", OP_EQ, "0::8000:2:1", 80);
  393. /* Test undecorated tor_addr_to_str */
  394. tt_int_op(AF_INET6,OP_EQ, tor_addr_parse(&t1, "[123:45:6789::5005:11]"));
  395. p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 0);
  396. tt_str_op(p1,OP_EQ, "123:45:6789::5005:11");
  397. tt_int_op(AF_INET,OP_EQ, tor_addr_parse(&t1, "18.0.0.1"));
  398. p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 0);
  399. tt_str_op(p1,OP_EQ, "18.0.0.1");
  400. /* Test decorated tor_addr_to_str */
  401. tt_int_op(AF_INET6,OP_EQ, tor_addr_parse(&t1, "[123:45:6789::5005:11]"));
  402. p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 1);
  403. tt_str_op(p1,OP_EQ, "[123:45:6789::5005:11]");
  404. tt_int_op(AF_INET,OP_EQ, tor_addr_parse(&t1, "18.0.0.1"));
  405. p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 1);
  406. tt_str_op(p1,OP_EQ, "18.0.0.1");
  407. /* Test buffer bounds checking of tor_addr_to_str */
  408. tt_int_op(AF_INET6,OP_EQ, tor_addr_parse(&t1, "::")); /* 2 + \0 */
  409. tt_ptr_op(tor_addr_to_str(buf, &t1, 2, 0),OP_EQ, NULL); /* too short buf */
  410. tt_str_op(tor_addr_to_str(buf, &t1, 3, 0),OP_EQ, "::");
  411. tt_ptr_op(tor_addr_to_str(buf, &t1, 4, 1),OP_EQ, NULL); /* too short buf */
  412. tt_str_op(tor_addr_to_str(buf, &t1, 5, 1),OP_EQ, "[::]");
  413. tt_int_op(AF_INET6,OP_EQ, tor_addr_parse(&t1, "2000::1337")); /* 10 + \0 */
  414. tt_ptr_op(tor_addr_to_str(buf, &t1, 10, 0),OP_EQ, NULL); /* too short buf */
  415. tt_str_op(tor_addr_to_str(buf, &t1, 11, 0),OP_EQ, "2000::1337");
  416. tt_ptr_op(tor_addr_to_str(buf, &t1, 12, 1),OP_EQ, NULL); /* too short buf */
  417. tt_str_op(tor_addr_to_str(buf, &t1, 13, 1),OP_EQ, "[2000::1337]");
  418. tt_int_op(AF_INET,OP_EQ, tor_addr_parse(&t1, "1.2.3.4")); /* 7 + \0 */
  419. tt_ptr_op(tor_addr_to_str(buf, &t1, 7, 0),OP_EQ, NULL); /* too short buf */
  420. tt_str_op(tor_addr_to_str(buf, &t1, 8, 0),OP_EQ, "1.2.3.4");
  421. tt_int_op(AF_INET, OP_EQ,
  422. tor_addr_parse(&t1, "255.255.255.255")); /* 15 + \0 */
  423. tt_ptr_op(tor_addr_to_str(buf, &t1, 15, 0),OP_EQ, NULL); /* too short buf */
  424. tt_str_op(tor_addr_to_str(buf, &t1, 16, 0),OP_EQ, "255.255.255.255");
  425. tt_ptr_op(tor_addr_to_str(buf, &t1, 15, 1),OP_EQ, NULL); /* too short buf */
  426. tt_str_op(tor_addr_to_str(buf, &t1, 16, 1),OP_EQ, "255.255.255.255");
  427. t1.family = AF_UNSPEC;
  428. tt_ptr_op(tor_addr_to_str(buf, &t1, sizeof(buf), 0),OP_EQ, NULL);
  429. /* Test tor_addr_parse_PTR_name */
  430. i = tor_addr_parse_PTR_name(&t1, "Foobar.baz", AF_UNSPEC, 0);
  431. tt_int_op(0,OP_EQ, i);
  432. i = tor_addr_parse_PTR_name(&t1, "Foobar.baz", AF_UNSPEC, 1);
  433. tt_int_op(0,OP_EQ, i);
  434. i = tor_addr_parse_PTR_name(&t1, "9999999999999999999999999999.in-addr.arpa",
  435. AF_UNSPEC, 1);
  436. tt_int_op(-1,OP_EQ, i);
  437. i = tor_addr_parse_PTR_name(&t1, "1.0.168.192.in-addr.arpa",
  438. AF_UNSPEC, 1);
  439. tt_int_op(1,OP_EQ, i);
  440. tt_int_op(tor_addr_family(&t1),OP_EQ, AF_INET);
  441. p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 1);
  442. tt_str_op(p1,OP_EQ, "192.168.0.1");
  443. i = tor_addr_parse_PTR_name(&t1, "192.168.0.99", AF_UNSPEC, 0);
  444. tt_int_op(0,OP_EQ, i);
  445. i = tor_addr_parse_PTR_name(&t1, "192.168.0.99", AF_UNSPEC, 1);
  446. tt_int_op(1,OP_EQ, i);
  447. p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 1);
  448. tt_str_op(p1,OP_EQ, "192.168.0.99");
  449. memset(&t1, 0, sizeof(t1));
  450. i = tor_addr_parse_PTR_name(&t1,
  451. "0.1.2.3.4.5.6.7.8.9.a.b.c.d.e.f."
  452. "f.e.e.b.1.e.b.e.e.f.f.e.e.e.d.9."
  453. "ip6.ARPA",
  454. AF_UNSPEC, 0);
  455. tt_int_op(1,OP_EQ, i);
  456. p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 1);
  457. tt_str_op(p1,OP_EQ, "[9dee:effe:ebe1:beef:fedc:ba98:7654:3210]");
  458. /* Failing cases. */
  459. i = tor_addr_parse_PTR_name(&t1,
  460. "6.7.8.9.a.b.c.d.e.f."
  461. "f.e.e.b.1.e.b.e.e.f.f.e.e.e.d.9."
  462. "ip6.ARPA",
  463. AF_UNSPEC, 0);
  464. tt_int_op(i,OP_EQ, -1);
  465. i = tor_addr_parse_PTR_name(&t1,
  466. "6.7.8.9.a.b.c.d.e.f.a.b.c.d.e.f.0."
  467. "f.e.e.b.1.e.b.e.e.f.f.e.e.e.d.9."
  468. "ip6.ARPA",
  469. AF_UNSPEC, 0);
  470. tt_int_op(i,OP_EQ, -1);
  471. i = tor_addr_parse_PTR_name(&t1,
  472. "6.7.8.9.a.b.c.d.e.f.X.0.0.0.0.9."
  473. "f.e.e.b.1.e.b.e.e.f.f.e.e.e.d.9."
  474. "ip6.ARPA",
  475. AF_UNSPEC, 0);
  476. tt_int_op(i,OP_EQ, -1);
  477. i = tor_addr_parse_PTR_name(&t1, "32.1.1.in-addr.arpa",
  478. AF_UNSPEC, 0);
  479. tt_int_op(i,OP_EQ, -1);
  480. i = tor_addr_parse_PTR_name(&t1, ".in-addr.arpa",
  481. AF_UNSPEC, 0);
  482. tt_int_op(i,OP_EQ, -1);
  483. i = tor_addr_parse_PTR_name(&t1, "1.2.3.4.5.in-addr.arpa",
  484. AF_UNSPEC, 0);
  485. tt_int_op(i,OP_EQ, -1);
  486. i = tor_addr_parse_PTR_name(&t1, "1.2.3.4.5.in-addr.arpa",
  487. AF_INET6, 0);
  488. tt_int_op(i,OP_EQ, -1);
  489. i = tor_addr_parse_PTR_name(&t1,
  490. "6.7.8.9.a.b.c.d.e.f.a.b.c.d.e.0."
  491. "f.e.e.b.1.e.b.e.e.f.f.e.e.e.d.9."
  492. "ip6.ARPA",
  493. AF_INET, 0);
  494. tt_int_op(i,OP_EQ, -1);
  495. /* === Test tor_addr_to_PTR_name */
  496. /* Stage IPv4 addr */
  497. memset(&sa_storage, 0, sizeof(sa_storage));
  498. sin = (struct sockaddr_in *)&sa_storage;
  499. sin->sin_family = AF_INET;
  500. sin->sin_addr.s_addr = htonl(0x7f010203); /* 127.1.2.3 */
  501. tor_addr_from_sockaddr(&t1, (struct sockaddr *)sin, NULL);
  502. /* Check IPv4 PTR - too short buffer */
  503. tt_int_op(tor_addr_to_PTR_name(rbuf, 1, &t1),OP_EQ, -1);
  504. tt_int_op(tor_addr_to_PTR_name(rbuf,
  505. strlen("3.2.1.127.in-addr.arpa") - 1,
  506. &t1),OP_EQ, -1);
  507. /* Check IPv4 PTR - valid addr */
  508. tt_int_op(tor_addr_to_PTR_name(rbuf, sizeof(rbuf), &t1),OP_EQ,
  509. strlen("3.2.1.127.in-addr.arpa"));
  510. tt_str_op(rbuf,OP_EQ, "3.2.1.127.in-addr.arpa");
  511. /* Invalid addr family */
  512. t1.family = AF_UNSPEC;
  513. tt_int_op(tor_addr_to_PTR_name(rbuf, sizeof(rbuf), &t1),OP_EQ, -1);
  514. /* Stage IPv6 addr */
  515. memset(&sa_storage, 0, sizeof(sa_storage));
  516. sin6 = (struct sockaddr_in6 *)&sa_storage;
  517. sin6->sin6_family = AF_INET6;
  518. sin6->sin6_addr.s6_addr[0] = 0x80; /* 8000::abcd */
  519. sin6->sin6_addr.s6_addr[14] = 0xab;
  520. sin6->sin6_addr.s6_addr[15] = 0xcd;
  521. tor_addr_from_sockaddr(&t1, (struct sockaddr *)sin6, NULL);
  522. {
  523. const char* addr_PTR = "d.c.b.a.0.0.0.0.0.0.0.0.0.0.0.0."
  524. "0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.ip6.arpa";
  525. /* Check IPv6 PTR - too short buffer */
  526. tt_int_op(tor_addr_to_PTR_name(rbuf, 0, &t1),OP_EQ, -1);
  527. tt_int_op(tor_addr_to_PTR_name(rbuf, strlen(addr_PTR) - 1, &t1),OP_EQ, -1);
  528. /* Check IPv6 PTR - valid addr */
  529. tt_int_op(tor_addr_to_PTR_name(rbuf, sizeof(rbuf), &t1),OP_EQ,
  530. strlen(addr_PTR));
  531. tt_str_op(rbuf,OP_EQ, addr_PTR);
  532. }
  533. /* XXXX turn this into a separate function; it's not all IPv6. */
  534. /* test tor_addr_parse_mask_ports */
  535. test_addr_mask_ports_parse("[::f]/17:47-95", AF_INET6,
  536. 0, 0, 0, 0x0000000f, 17, 47, 95);
  537. tt_str_op(p1,OP_EQ, "::f");
  538. //test_addr_parse("[::fefe:4.1.1.7/120]:999-1000");
  539. //test_addr_parse_check("::fefe:401:107", 120, 999, 1000);
  540. test_addr_mask_ports_parse("[::ffff:4.1.1.7]/120:443", AF_INET6,
  541. 0, 0, 0x0000ffff, 0x04010107, 120, 443, 443);
  542. tt_str_op(p1,OP_EQ, "::ffff:4.1.1.7");
  543. test_addr_mask_ports_parse("[abcd:2::44a:0]:2-65000", AF_INET6,
  544. 0xabcd0002, 0, 0, 0x044a0000, 128, 2, 65000);
  545. tt_str_op(p1,OP_EQ, "abcd:2::44a:0");
  546. /* Try some long addresses. */
  547. r=tor_addr_parse_mask_ports("[ffff:1111:1111:1111:1111:1111:1111:1111]",
  548. 0, &t1, NULL, NULL, NULL);
  549. tt_assert(r == AF_INET6);
  550. r=tor_addr_parse_mask_ports("[ffff:1111:1111:1111:1111:1111:1111:11111]",
  551. 0, &t1, NULL, NULL, NULL);
  552. tt_int_op(r, OP_EQ, -1);
  553. r=tor_addr_parse_mask_ports("[ffff:1111:1111:1111:1111:1111:1111:1111:1]",
  554. 0, &t1, NULL, NULL, NULL);
  555. tt_int_op(r, OP_EQ, -1);
  556. r=tor_addr_parse_mask_ports(
  557. "[ffff:1111:1111:1111:1111:1111:1111:ffff:"
  558. "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:"
  559. "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:"
  560. "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]",
  561. 0, &t1, NULL, NULL, NULL);
  562. tt_int_op(r, OP_EQ, -1);
  563. /* Try some failing cases. */
  564. r=tor_addr_parse_mask_ports("[fefef::]/112", 0, &t1, NULL, NULL, NULL);
  565. tt_int_op(r, OP_EQ, -1);
  566. r=tor_addr_parse_mask_ports("[fefe::/112", 0, &t1, NULL, NULL, NULL);
  567. tt_int_op(r, OP_EQ, -1);
  568. r=tor_addr_parse_mask_ports("[fefe::", 0, &t1, NULL, NULL, NULL);
  569. tt_int_op(r, OP_EQ, -1);
  570. r=tor_addr_parse_mask_ports("[fefe::X]", 0, &t1, NULL, NULL, NULL);
  571. tt_int_op(r, OP_EQ, -1);
  572. r=tor_addr_parse_mask_ports("efef::/112", 0, &t1, NULL, NULL, NULL);
  573. tt_int_op(r, OP_EQ, -1);
  574. r=tor_addr_parse_mask_ports("[f:f:f:f:f:f:f:f::]",0,&t1, NULL, NULL, NULL);
  575. tt_int_op(r, OP_EQ, -1);
  576. r=tor_addr_parse_mask_ports("[::f:f:f:f:f:f:f:f]",0,&t1, NULL, NULL, NULL);
  577. tt_int_op(r, OP_EQ, -1);
  578. r=tor_addr_parse_mask_ports("[f:f:f:f:f:f:f:f:f]",0,&t1, NULL, NULL, NULL);
  579. tt_int_op(r, OP_EQ, -1);
  580. r=tor_addr_parse_mask_ports("[f:f:f:f:f::]/fred",0,&t1,&mask, NULL, NULL);
  581. tt_int_op(r, OP_EQ, -1);
  582. r=tor_addr_parse_mask_ports("[f:f:f:f:f::]/255.255.0.0",
  583. 0,&t1, NULL, NULL, NULL);
  584. tt_int_op(r, OP_EQ, -1);
  585. /* This one will get rejected because it isn't a pure prefix. */
  586. r=tor_addr_parse_mask_ports("1.1.2.3/255.255.64.0",0,&t1, &mask,NULL,NULL);
  587. tt_int_op(r, OP_EQ, -1);
  588. /* Test for V4-mapped address with mask < 96. (arguably not valid) */
  589. r=tor_addr_parse_mask_ports("[::ffff:1.1.2.2/33]",0,&t1, &mask, NULL, NULL);
  590. tt_int_op(r, OP_EQ, -1);
  591. r=tor_addr_parse_mask_ports("1.1.2.2/33",0,&t1, &mask, NULL, NULL);
  592. tt_int_op(r, OP_EQ, -1);
  593. /* Try extended wildcard addresses with out TAPMP_EXTENDED_STAR*/
  594. r=tor_addr_parse_mask_ports("*4",0,&t1, &mask, NULL, NULL);
  595. tt_int_op(r, OP_EQ, -1);
  596. r=tor_addr_parse_mask_ports("*6",0,&t1, &mask, NULL, NULL);
  597. tt_int_op(r, OP_EQ, -1);
  598. tt_assert(r == -1);
  599. /* Try a mask with a wildcard. */
  600. r=tor_addr_parse_mask_ports("*/16",0,&t1, &mask, NULL, NULL);
  601. tt_assert(r == -1);
  602. r=tor_addr_parse_mask_ports("*4/16",TAPMP_EXTENDED_STAR,
  603. &t1, &mask, NULL, NULL);
  604. tt_assert(r == -1);
  605. r=tor_addr_parse_mask_ports("*6/30",TAPMP_EXTENDED_STAR,
  606. &t1, &mask, NULL, NULL);
  607. tt_assert(r == -1);
  608. /* Basic mask tests*/
  609. r=tor_addr_parse_mask_ports("1.1.2.2/31",0,&t1, &mask, NULL, NULL);
  610. tt_assert(r == AF_INET);
  611. tt_int_op(mask,OP_EQ,31);
  612. tt_int_op(tor_addr_family(&t1),OP_EQ,AF_INET);
  613. tt_int_op(tor_addr_to_ipv4h(&t1),OP_EQ,0x01010202);
  614. r=tor_addr_parse_mask_ports("3.4.16.032:1-2",0,&t1, &mask, &port1, &port2);
  615. tt_assert(r == AF_INET);
  616. tt_int_op(mask,OP_EQ,32);
  617. tt_int_op(tor_addr_family(&t1),OP_EQ,AF_INET);
  618. tt_int_op(tor_addr_to_ipv4h(&t1),OP_EQ,0x03041020);
  619. tt_assert(port1 == 1);
  620. tt_assert(port2 == 2);
  621. r=tor_addr_parse_mask_ports("1.1.2.3/255.255.128.0",0,&t1, &mask,NULL,NULL);
  622. tt_assert(r == AF_INET);
  623. tt_int_op(mask,OP_EQ,17);
  624. tt_int_op(tor_addr_family(&t1),OP_EQ,AF_INET);
  625. tt_int_op(tor_addr_to_ipv4h(&t1),OP_EQ,0x01010203);
  626. r=tor_addr_parse_mask_ports("[efef::]/112",0,&t1, &mask, &port1, &port2);
  627. tt_assert(r == AF_INET6);
  628. tt_assert(port1 == 1);
  629. tt_assert(port2 == 65535);
  630. /* Try regular wildcard behavior without TAPMP_EXTENDED_STAR */
  631. r=tor_addr_parse_mask_ports("*:80-443",0,&t1,&mask,&port1,&port2);
  632. tt_int_op(r,OP_EQ,AF_INET); /* Old users of this always get inet */
  633. tt_int_op(tor_addr_family(&t1),OP_EQ,AF_INET);
  634. tt_int_op(tor_addr_to_ipv4h(&t1),OP_EQ,0);
  635. tt_int_op(mask,OP_EQ,0);
  636. tt_int_op(port1,OP_EQ,80);
  637. tt_int_op(port2,OP_EQ,443);
  638. /* Now try wildcards *with* TAPMP_EXTENDED_STAR */
  639. r=tor_addr_parse_mask_ports("*:8000-9000",TAPMP_EXTENDED_STAR,
  640. &t1,&mask,&port1,&port2);
  641. tt_int_op(r,OP_EQ,AF_UNSPEC);
  642. tt_int_op(tor_addr_family(&t1),OP_EQ,AF_UNSPEC);
  643. tt_int_op(mask,OP_EQ,0);
  644. tt_int_op(port1,OP_EQ,8000);
  645. tt_int_op(port2,OP_EQ,9000);
  646. r=tor_addr_parse_mask_ports("*4:6667",TAPMP_EXTENDED_STAR,
  647. &t1,&mask,&port1,&port2);
  648. tt_int_op(r,OP_EQ,AF_INET);
  649. tt_int_op(tor_addr_family(&t1),OP_EQ,AF_INET);
  650. tt_int_op(tor_addr_to_ipv4h(&t1),OP_EQ,0);
  651. tt_int_op(mask,OP_EQ,0);
  652. tt_int_op(port1,OP_EQ,6667);
  653. tt_int_op(port2,OP_EQ,6667);
  654. r=tor_addr_parse_mask_ports("*6",TAPMP_EXTENDED_STAR,
  655. &t1,&mask,&port1,&port2);
  656. tt_int_op(r,OP_EQ,AF_INET6);
  657. tt_int_op(tor_addr_family(&t1),OP_EQ,AF_INET6);
  658. tt_assert(tor_mem_is_zero((const char*)tor_addr_to_in6_addr32(&t1), 16));
  659. tt_int_op(mask,OP_EQ,0);
  660. tt_int_op(port1,OP_EQ,1);
  661. tt_int_op(port2,OP_EQ,65535);
  662. /* make sure inet address lengths >= max */
  663. tt_assert(INET_NTOA_BUF_LEN >= sizeof("255.255.255.255"));
  664. tt_assert(TOR_ADDR_BUF_LEN >=
  665. sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"));
  666. tt_assert(sizeof(tor_addr_t) >= sizeof(struct in6_addr));
  667. /* get interface addresses */
  668. r = get_interface_address6(LOG_DEBUG, AF_INET, &t1);
  669. i = get_interface_address6(LOG_DEBUG, AF_INET6, &t2);
  670. TT_BLATHER(("v4 address: %s (family=%d)", fmt_addr(&t1),
  671. tor_addr_family(&t1)));
  672. TT_BLATHER(("v6 address: %s (family=%d)", fmt_addr(&t2),
  673. tor_addr_family(&t2)));
  674. done:
  675. ;
  676. }
  677. /** Test tor_addr_port_parse(). */
  678. static void
  679. test_addr_parse(void *arg)
  680. {
  681. int r;
  682. tor_addr_t addr;
  683. char buf[TOR_ADDR_BUF_LEN];
  684. uint16_t port = 0;
  685. /* Correct call. */
  686. (void)arg;
  687. r= tor_addr_port_parse(LOG_DEBUG,
  688. "192.0.2.1:1234",
  689. &addr, &port, -1);
  690. tt_int_op(r, OP_EQ, 0);
  691. tor_addr_to_str(buf, &addr, sizeof(buf), 0);
  692. tt_str_op(buf,OP_EQ, "192.0.2.1");
  693. tt_int_op(port,OP_EQ, 1234);
  694. r= tor_addr_port_parse(LOG_DEBUG,
  695. "[::1]:1234",
  696. &addr, &port, -1);
  697. tt_int_op(r, OP_EQ, 0);
  698. tor_addr_to_str(buf, &addr, sizeof(buf), 0);
  699. tt_str_op(buf,OP_EQ, "::1");
  700. tt_int_op(port,OP_EQ, 1234);
  701. /* Domain name. */
  702. r= tor_addr_port_parse(LOG_DEBUG,
  703. "torproject.org:1234",
  704. &addr, &port, -1);
  705. tt_int_op(r, OP_EQ, -1);
  706. /* Only IP. */
  707. r= tor_addr_port_parse(LOG_DEBUG,
  708. "192.0.2.2",
  709. &addr, &port, -1);
  710. tt_int_op(r, OP_EQ, -1);
  711. r= tor_addr_port_parse(LOG_DEBUG,
  712. "192.0.2.2",
  713. &addr, &port, 200);
  714. tt_int_op(r, OP_EQ, 0);
  715. tt_int_op(port,OP_EQ,200);
  716. r= tor_addr_port_parse(LOG_DEBUG,
  717. "[::1]",
  718. &addr, &port, -1);
  719. tt_int_op(r, OP_EQ, -1);
  720. r= tor_addr_port_parse(LOG_DEBUG,
  721. "[::1]",
  722. &addr, &port, 400);
  723. tt_int_op(r, OP_EQ, 0);
  724. tt_int_op(port,OP_EQ,400);
  725. /* Bad port. */
  726. r= tor_addr_port_parse(LOG_DEBUG,
  727. "192.0.2.2:66666",
  728. &addr, &port, -1);
  729. tt_int_op(r, OP_EQ, -1);
  730. r= tor_addr_port_parse(LOG_DEBUG,
  731. "192.0.2.2:66666",
  732. &addr, &port, 200);
  733. tt_int_op(r, OP_EQ, -1);
  734. /* Only domain name */
  735. r= tor_addr_port_parse(LOG_DEBUG,
  736. "torproject.org",
  737. &addr, &port, -1);
  738. tt_int_op(r, OP_EQ, -1);
  739. r= tor_addr_port_parse(LOG_DEBUG,
  740. "torproject.org",
  741. &addr, &port, 200);
  742. tt_int_op(r, OP_EQ, -1);
  743. /* Bad IP address */
  744. r= tor_addr_port_parse(LOG_DEBUG,
  745. "192.0.2:1234",
  746. &addr, &port, -1);
  747. tt_int_op(r, OP_EQ, -1);
  748. /* Make sure that the default port has lower priority than the real
  749. one */
  750. r= tor_addr_port_parse(LOG_DEBUG,
  751. "192.0.2.2:1337",
  752. &addr, &port, 200);
  753. tt_int_op(r, OP_EQ, 0);
  754. tt_int_op(port,OP_EQ,1337);
  755. r= tor_addr_port_parse(LOG_DEBUG,
  756. "[::1]:1369",
  757. &addr, &port, 200);
  758. tt_int_op(r, OP_EQ, 0);
  759. tt_int_op(port,OP_EQ,1369);
  760. done:
  761. ;
  762. }
  763. static void
  764. update_difference(int ipv6, uint8_t *d,
  765. const tor_addr_t *a, const tor_addr_t *b)
  766. {
  767. const int n_bytes = ipv6 ? 16 : 4;
  768. uint8_t a_tmp[4], b_tmp[4];
  769. const uint8_t *ba, *bb;
  770. int i;
  771. if (ipv6) {
  772. ba = tor_addr_to_in6_addr8(a);
  773. bb = tor_addr_to_in6_addr8(b);
  774. } else {
  775. set_uint32(a_tmp, tor_addr_to_ipv4n(a));
  776. set_uint32(b_tmp, tor_addr_to_ipv4n(b));
  777. ba = a_tmp; bb = b_tmp;
  778. }
  779. for (i = 0; i < n_bytes; ++i) {
  780. d[i] |= ba[i] ^ bb[i];
  781. }
  782. }
  783. static void
  784. test_virtaddrmap(void *data)
  785. {
  786. /* Let's start with a bunch of random addresses. */
  787. int ipv6, bits, iter, b;
  788. virtual_addr_conf_t cfg[2];
  789. uint8_t bytes[16];
  790. (void)data;
  791. tor_addr_parse(&cfg[0].addr, "64.65.0.0");
  792. tor_addr_parse(&cfg[1].addr, "3491:c0c0::");
  793. for (ipv6 = 0; ipv6 <= 1; ++ipv6) {
  794. for (bits = 0; bits < 18; ++bits) {
  795. tor_addr_t last_a;
  796. cfg[ipv6].bits = bits;
  797. memset(bytes, 0, sizeof(bytes));
  798. tor_addr_copy(&last_a, &cfg[ipv6].addr);
  799. /* Generate 128 addresses with each addr/bits combination. */
  800. for (iter = 0; iter < 128; ++iter) {
  801. tor_addr_t a;
  802. get_random_virtual_addr(&cfg[ipv6], &a);
  803. //printf("%s\n", fmt_addr(&a));
  804. /* Make sure that the first b bits match the configured network */
  805. tt_int_op(0, OP_EQ, tor_addr_compare_masked(&a, &cfg[ipv6].addr,
  806. bits, CMP_EXACT));
  807. /* And track which bits have been different between pairs of
  808. * addresses */
  809. update_difference(ipv6, bytes, &last_a, &a);
  810. }
  811. /* Now make sure all but the first 'bits' bits of bytes are true */
  812. for (b = bits+1; b < (ipv6?128:32); ++b) {
  813. tt_assert(1 & (bytes[b/8] >> (7-(b&7))));
  814. }
  815. }
  816. }
  817. done:
  818. ;
  819. }
  820. static void
  821. test_addr_localname(void *arg)
  822. {
  823. (void)arg;
  824. tt_assert(tor_addr_hostname_is_local("localhost"));
  825. tt_assert(tor_addr_hostname_is_local("LOCALHOST"));
  826. tt_assert(tor_addr_hostname_is_local("LocalHost"));
  827. tt_assert(tor_addr_hostname_is_local("local"));
  828. tt_assert(tor_addr_hostname_is_local("LOCAL"));
  829. tt_assert(tor_addr_hostname_is_local("here.now.local"));
  830. tt_assert(tor_addr_hostname_is_local("here.now.LOCAL"));
  831. tt_assert(!tor_addr_hostname_is_local(" localhost"));
  832. tt_assert(!tor_addr_hostname_is_local("www.torproject.org"));
  833. done:
  834. ;
  835. }
  836. static void
  837. test_addr_dup_ip(void *arg)
  838. {
  839. char *v = NULL;
  840. (void)arg;
  841. #define CHECK(ip, s) do { \
  842. v = tor_dup_ip(ip); \
  843. tt_str_op(v,OP_EQ,(s)); \
  844. tor_free(v); \
  845. } while (0)
  846. CHECK(0xffffffff, "255.255.255.255");
  847. CHECK(0x00000000, "0.0.0.0");
  848. CHECK(0x7f000001, "127.0.0.1");
  849. CHECK(0x01020304, "1.2.3.4");
  850. #undef CHECK
  851. done:
  852. tor_free(v);
  853. }
  854. static void
  855. test_addr_sockaddr_to_str(void *arg)
  856. {
  857. char *v = NULL;
  858. struct sockaddr_in sin;
  859. struct sockaddr_in6 sin6;
  860. struct sockaddr_storage ss;
  861. #ifdef HAVE_SYS_UN_H
  862. struct sockaddr_un s_un;
  863. #endif
  864. #define CHECK(sa, s) do { \
  865. v = tor_sockaddr_to_str((const struct sockaddr*) &(sa)); \
  866. tt_str_op(v,OP_EQ,(s)); \
  867. tor_free(v); \
  868. } while (0)
  869. (void)arg;
  870. memset(&ss,0,sizeof(ss));
  871. ss.ss_family = AF_UNSPEC;
  872. CHECK(ss, "unspec");
  873. memset(&sin,0,sizeof(sin));
  874. sin.sin_family = AF_INET;
  875. sin.sin_addr.s_addr = htonl(0x7f808001);
  876. sin.sin_port = htons(1234);
  877. CHECK(sin, "127.128.128.1:1234");
  878. #ifdef HAVE_SYS_UN_H
  879. memset(&s_un,0,sizeof(s_un));
  880. s_un.sun_family = AF_UNIX;
  881. strlcpy(s_un.sun_path, "/here/is/a/path", sizeof(s_un.sun_path));
  882. CHECK(s_un, "unix:/here/is/a/path");
  883. #endif
  884. memset(&sin6,0,sizeof(sin6));
  885. sin6.sin6_family = AF_INET6;
  886. memcpy(sin6.sin6_addr.s6_addr, "\x20\x00\x00\x00\x00\x00\x00\x00"
  887. "\x00\x1a\x2b\x3c\x4d\x5e\x00\x01", 16);
  888. sin6.sin6_port = htons(1234);
  889. CHECK(sin6, "[2000::1a:2b3c:4d5e:1]:1234");
  890. done:
  891. tor_free(v);
  892. }
  893. static void
  894. test_addr_is_loopback(void *data)
  895. {
  896. static const struct loopback_item {
  897. const char *name;
  898. int is_loopback;
  899. } loopback_items[] = {
  900. { "::1", 1 },
  901. { "127.0.0.1", 1 },
  902. { "127.99.100.101", 1 },
  903. { "128.99.100.101", 0 },
  904. { "8.8.8.8", 0 },
  905. { "0.0.0.0", 0 },
  906. { "::2", 0 },
  907. { "::", 0 },
  908. { "::1.0.0.0", 0 },
  909. { NULL, 0 }
  910. };
  911. int i;
  912. tor_addr_t addr;
  913. (void)data;
  914. for (i=0; loopback_items[i].name; ++i) {
  915. tt_int_op(tor_addr_parse(&addr, loopback_items[i].name), OP_GE, 0);
  916. tt_int_op(tor_addr_is_loopback(&addr), OP_EQ,
  917. loopback_items[i].is_loopback);
  918. }
  919. tor_addr_make_unspec(&addr);
  920. tt_int_op(tor_addr_is_loopback(&addr), OP_EQ, 0);
  921. done:
  922. ;
  923. }
  924. static void
  925. test_addr_make_null(void *data)
  926. {
  927. tor_addr_t *addr = tor_malloc(sizeof(*addr));
  928. tor_addr_t *zeros = tor_malloc_zero(sizeof(*addr));
  929. char buf[TOR_ADDR_BUF_LEN];
  930. (void) data;
  931. /* Ensure that before tor_addr_make_null, addr != 0's */
  932. memset(addr, 1, sizeof(*addr));
  933. tt_int_op(memcmp(addr, zeros, sizeof(*addr)), OP_NE, 0);
  934. /* Test with AF == AF_INET */
  935. zeros->family = AF_INET;
  936. tor_addr_make_null(addr, AF_INET);
  937. tt_int_op(memcmp(addr, zeros, sizeof(*addr)), OP_EQ, 0);
  938. tt_str_op(tor_addr_to_str(buf, addr, sizeof(buf), 0), OP_EQ, "0.0.0.0");
  939. /* Test with AF == AF_INET6 */
  940. memset(addr, 1, sizeof(*addr));
  941. zeros->family = AF_INET6;
  942. tor_addr_make_null(addr, AF_INET6);
  943. tt_int_op(memcmp(addr, zeros, sizeof(*addr)), OP_EQ, 0);
  944. tt_str_op(tor_addr_to_str(buf, addr, sizeof(buf), 0), OP_EQ, "::");
  945. done:
  946. tor_free(addr);
  947. tor_free(zeros);
  948. }
  949. #define ADDR_LEGACY(name) \
  950. { #name, test_addr_ ## name , 0, NULL, NULL }
  951. struct testcase_t addr_tests[] = {
  952. ADDR_LEGACY(basic),
  953. ADDR_LEGACY(ip6_helpers),
  954. ADDR_LEGACY(parse),
  955. { "virtaddr", test_virtaddrmap, 0, NULL, NULL },
  956. { "localname", test_addr_localname, 0, NULL, NULL },
  957. { "dup_ip", test_addr_dup_ip, 0, NULL, NULL },
  958. { "sockaddr_to_str", test_addr_sockaddr_to_str, 0, NULL, NULL },
  959. { "is_loopback", test_addr_is_loopback, 0, NULL, NULL },
  960. { "make_null", test_addr_make_null, 0, NULL, NULL },
  961. END_OF_TESTCASES
  962. };