test_addr.c 41 KB

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