test_addr.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2019, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #define ADDRESSMAP_PRIVATE
  6. #include "orconfig.h"
  7. #include "core/or/or.h"
  8. #include "lib/crypt_ops/crypto_rand.h"
  9. #include "test/test.h"
  10. #include "feature/client/addressmap.h"
  11. #include "test/log_test_helpers.h"
  12. #include "lib/net/resolve.h"
  13. #ifdef HAVE_SYS_UN_H
  14. #include <sys/un.h>
  15. #endif
  16. static void
  17. test_addr_basic(void *arg)
  18. {
  19. (void) arg;
  20. tt_int_op(0,OP_EQ, addr_mask_get_bits(0x0u));
  21. tt_int_op(32,OP_EQ, addr_mask_get_bits(0xFFFFFFFFu));
  22. tt_int_op(16,OP_EQ, addr_mask_get_bits(0xFFFF0000u));
  23. tt_int_op(31,OP_EQ, addr_mask_get_bits(0xFFFFFFFEu));
  24. tt_int_op(1,OP_EQ, addr_mask_get_bits(0x80000000u));
  25. /* Test inet_ntop */
  26. {
  27. char tmpbuf[TOR_ADDR_BUF_LEN];
  28. const char *ip = "176.192.208.224";
  29. struct in_addr in;
  30. /* good round trip */
  31. tt_int_op(tor_inet_pton(AF_INET, ip, &in), OP_EQ, 1);
  32. tt_ptr_op(tor_inet_ntop(AF_INET, &in, tmpbuf, sizeof(tmpbuf)),
  33. OP_EQ, &tmpbuf);
  34. tt_str_op(tmpbuf,OP_EQ, ip);
  35. /* just enough buffer length */
  36. tt_str_op(tor_inet_ntop(AF_INET, &in, tmpbuf, strlen(ip) + 1), OP_EQ, ip);
  37. /* too short buffer */
  38. tt_ptr_op(tor_inet_ntop(AF_INET, &in, tmpbuf, strlen(ip)),OP_EQ, NULL);
  39. }
  40. done:
  41. ;
  42. }
  43. #define test_op_ip6_(a,op,b,e1,e2) \
  44. STMT_BEGIN \
  45. tt_assert_test_fmt_type(a,b,e1" "#op" "e2,struct in6_addr*, \
  46. (fast_memcmp(val1_->s6_addr, val2_->s6_addr, 16) op 0), \
  47. char *, "%s", \
  48. { char *cp; \
  49. cp = print_ = tor_malloc(64); \
  50. for (int ii_=0;ii_<16;++ii_) { \
  51. tor_snprintf(cp, 3,"%02x", (unsigned)value_->s6_addr[ii_]); \
  52. cp += 2; \
  53. if (ii_ != 15) *cp++ = ':'; \
  54. } \
  55. }, \
  56. { tor_free(print_); }, \
  57. TT_EXIT_TEST_FUNCTION \
  58. ); \
  59. STMT_END
  60. /** Helper: Assert that two strings both decode as IPv6 addresses with
  61. * tor_inet_pton(), and both decode to the same address. */
  62. #define test_pton6_same(a,b) STMT_BEGIN \
  63. tt_int_op(tor_inet_pton(AF_INET6, a, &a1), OP_EQ, 1); \
  64. tt_int_op(tor_inet_pton(AF_INET6, b, &a2), OP_EQ, 1); \
  65. test_op_ip6_(&a1,OP_EQ,&a2,#a,#b); \
  66. STMT_END
  67. /** Helper: Assert that <b>a</b> is recognized as a bad IPv6 address by
  68. * tor_inet_pton(). */
  69. #define test_pton6_bad(a) \
  70. tt_int_op(0, OP_EQ, tor_inet_pton(AF_INET6, a, &a1))
  71. /** Helper: assert that <b>a</b>, when parsed by tor_inet_pton() and displayed
  72. * with tor_inet_ntop(), yields <b>b</b>. Also assert that <b>b</b> parses to
  73. * the same value as <b>a</b>. */
  74. #define test_ntop6_reduces(a,b) STMT_BEGIN \
  75. tt_int_op(tor_inet_pton(AF_INET6, a, &a1), OP_EQ, 1); \
  76. tt_str_op(tor_inet_ntop(AF_INET6, &a1, buf, sizeof(buf)), OP_EQ, b); \
  77. tt_int_op(tor_inet_pton(AF_INET6, b, &a2), OP_EQ, 1); \
  78. test_op_ip6_(&a1, OP_EQ, &a2, a, b); \
  79. STMT_END
  80. /** Helper: assert that <b>a</b> parses by tor_inet_pton() into a address that
  81. * passes tor_addr_is_internal() with <b>for_listening</b>. */
  82. #define test_internal_ip(a,for_listening) STMT_BEGIN \
  83. tt_int_op(tor_inet_pton(AF_INET6, a, &t1.addr.in6_addr), OP_EQ, 1); \
  84. t1.family = AF_INET6; \
  85. if (!tor_addr_is_internal(&t1, for_listening)) \
  86. TT_DIE(("%s was not internal", a)); \
  87. STMT_END
  88. /** Helper: assert that <b>a</b> parses by tor_inet_pton() into a address that
  89. * does not pass tor_addr_is_internal() with <b>for_listening</b>. */
  90. #define test_external_ip(a,for_listening) STMT_BEGIN \
  91. tt_int_op(tor_inet_pton(AF_INET6, a, &t1.addr.in6_addr), OP_EQ, 1); \
  92. t1.family = AF_INET6; \
  93. if (tor_addr_is_internal(&t1, for_listening)) \
  94. TT_DIE(("%s was not internal", a)); \
  95. STMT_END
  96. /** Helper: Assert that <b>a</b> and <b>b</b>, when parsed by
  97. * tor_inet_pton(), give addresses that compare in the order defined by
  98. * <b>op</b> with tor_addr_compare(). */
  99. #define test_addr_compare(a, op, b) STMT_BEGIN \
  100. tt_int_op(tor_inet_pton(AF_INET6, a, &t1.addr.in6_addr), OP_EQ, 1); \
  101. tt_int_op(tor_inet_pton(AF_INET6, b, &t2.addr.in6_addr), OP_EQ, 1); \
  102. t1.family = t2.family = AF_INET6; \
  103. r = tor_addr_compare(&t1,&t2,CMP_SEMANTIC); \
  104. if (!(r op 0)) \
  105. TT_DIE(("Failed: tor_addr_compare(%s,%s) %s 0", a, b, #op));\
  106. STMT_END
  107. /** Helper: Assert that <b>a</b> and <b>b</b>, when parsed by
  108. * tor_inet_pton(), give addresses that compare in the order defined by
  109. * <b>op</b> with tor_addr_compare_masked() with <b>m</b> masked. */
  110. #define test_addr_compare_masked(a, op, b, m) STMT_BEGIN \
  111. tt_int_op(tor_inet_pton(AF_INET6, a, &t1.addr.in6_addr), OP_EQ, 1); \
  112. tt_int_op(tor_inet_pton(AF_INET6, b, &t2.addr.in6_addr), OP_EQ, 1); \
  113. t1.family = t2.family = AF_INET6; \
  114. r = tor_addr_compare_masked(&t1,&t2,m,CMP_SEMANTIC); \
  115. if (!(r op 0)) \
  116. TT_DIE(("Failed: tor_addr_compare_masked(%s,%s,%d) %s 0", \
  117. a, b, m, #op)); \
  118. STMT_END
  119. /** Helper: assert that <b>xx</b> is parseable as a masked IPv6 address with
  120. * ports by tor_parse_mask_addr_ports(), with family <b>f</b>, IP address
  121. * as 4 32-bit words <b>ip1...ip4</b>, mask bits as <b>mm</b>, and port range
  122. * as <b>pt1..pt2</b>. */
  123. #define test_addr_mask_ports_parse(xx, f, ip1, ip2, ip3, ip4, mm, pt1, pt2) \
  124. STMT_BEGIN \
  125. tt_int_op(tor_addr_parse_mask_ports(xx, 0, &t1, &mask, &port1, &port2), \
  126. OP_EQ, f); \
  127. p1=tor_inet_ntop(AF_INET6, &t1.addr.in6_addr, bug, sizeof(bug)); \
  128. tt_int_op(htonl(ip1), OP_EQ, tor_addr_to_in6_addr32(&t1)[0]); \
  129. tt_int_op(htonl(ip2), OP_EQ, tor_addr_to_in6_addr32(&t1)[1]); \
  130. tt_int_op(htonl(ip3), OP_EQ, tor_addr_to_in6_addr32(&t1)[2]); \
  131. tt_int_op(htonl(ip4), OP_EQ, tor_addr_to_in6_addr32(&t1)[3]); \
  132. tt_int_op(mask, OP_EQ, mm); \
  133. tt_uint_op(port1, OP_EQ, pt1); \
  134. tt_uint_op(port2, OP_EQ, pt2); \
  135. STMT_END
  136. /** Run unit tests for IPv6 encoding/decoding/manipulation functions. */
  137. static void
  138. test_addr_ip6_helpers(void *arg)
  139. {
  140. char buf[TOR_ADDR_BUF_LEN], bug[TOR_ADDR_BUF_LEN];
  141. char rbuf[REVERSE_LOOKUP_NAME_BUF_LEN];
  142. struct in6_addr a1, a2;
  143. tor_addr_t t1, t2;
  144. int r, i;
  145. uint16_t port1, port2;
  146. maskbits_t mask;
  147. const char *p1;
  148. struct sockaddr_storage sa_storage;
  149. struct sockaddr_in *sin;
  150. struct sockaddr_in6 *sin6;
  151. /* Test tor_inet_ntop and tor_inet_pton: IPv6 */
  152. (void)arg;
  153. {
  154. const char *ip = "2001::1234";
  155. const char *ip_ffff = "::ffff:192.168.1.2";
  156. /* good round trip */
  157. tt_int_op(tor_inet_pton(AF_INET6, ip, &a1),OP_EQ, 1);
  158. tt_ptr_op(tor_inet_ntop(AF_INET6, &a1, buf, sizeof(buf)),OP_EQ, &buf);
  159. tt_str_op(buf,OP_EQ, ip);
  160. /* good round trip - ::ffff:0:0 style */
  161. tt_int_op(tor_inet_pton(AF_INET6, ip_ffff, &a2),OP_EQ, 1);
  162. tt_ptr_op(tor_inet_ntop(AF_INET6, &a2, buf, sizeof(buf)),OP_EQ, &buf);
  163. tt_str_op(buf,OP_EQ, ip_ffff);
  164. /* just long enough buffer (remember \0) */
  165. tt_str_op(tor_inet_ntop(AF_INET6, &a1, buf, strlen(ip)+1),OP_EQ, ip);
  166. tt_str_op(tor_inet_ntop(AF_INET6, &a2, buf, strlen(ip_ffff)+1),OP_EQ,
  167. ip_ffff);
  168. /* too short buffer (remember \0) */
  169. tt_ptr_op(tor_inet_ntop(AF_INET6, &a1, buf, strlen(ip)),OP_EQ, NULL);
  170. tt_ptr_op(tor_inet_ntop(AF_INET6, &a2, buf, strlen(ip_ffff)),OP_EQ, NULL);
  171. }
  172. /* ==== Converting to and from sockaddr_t. */
  173. sin = (struct sockaddr_in *)&sa_storage;
  174. sin->sin_family = AF_INET;
  175. sin->sin_port = htons(9090);
  176. sin->sin_addr.s_addr = htonl(0x7f7f0102); /*127.127.1.2*/
  177. tor_addr_from_sockaddr(&t1, (struct sockaddr *)sin, &port1);
  178. tt_int_op(tor_addr_family(&t1),OP_EQ, AF_INET);
  179. tt_int_op(tor_addr_to_ipv4h(&t1),OP_EQ, 0x7f7f0102);
  180. tt_int_op(port1, OP_EQ, 9090);
  181. memset(&sa_storage, 0, sizeof(sa_storage));
  182. tt_int_op(sizeof(struct sockaddr_in),OP_EQ,
  183. tor_addr_to_sockaddr(&t1, 1234, (struct sockaddr *)&sa_storage,
  184. sizeof(sa_storage)));
  185. tt_int_op(1234,OP_EQ, ntohs(sin->sin_port));
  186. tt_int_op(0x7f7f0102,OP_EQ, ntohl(sin->sin_addr.s_addr));
  187. memset(&sa_storage, 0, sizeof(sa_storage));
  188. sin6 = (struct sockaddr_in6 *)&sa_storage;
  189. sin6->sin6_family = AF_INET6;
  190. sin6->sin6_port = htons(7070);
  191. sin6->sin6_addr.s6_addr[0] = 128;
  192. tor_addr_from_sockaddr(&t1, (struct sockaddr *)sin6, &port1);
  193. tt_int_op(tor_addr_family(&t1),OP_EQ, AF_INET6);
  194. tt_int_op(port1, OP_EQ, 7070);
  195. p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 0);
  196. tt_str_op(p1,OP_EQ, "8000::");
  197. memset(&sa_storage, 0, sizeof(sa_storage));
  198. tt_int_op(sizeof(struct sockaddr_in6),OP_EQ,
  199. tor_addr_to_sockaddr(&t1, 9999, (struct sockaddr *)&sa_storage,
  200. sizeof(sa_storage)));
  201. tt_int_op(AF_INET6,OP_EQ, sin6->sin6_family);
  202. tt_int_op(9999,OP_EQ, ntohs(sin6->sin6_port));
  203. tt_int_op(0x80000000,OP_EQ, ntohl(S6_ADDR32(sin6->sin6_addr)[0]));
  204. /* ==== tor_addr_lookup: static cases. (Can't test dns without knowing we
  205. * have a good resolver. */
  206. tt_int_op(0,OP_EQ, tor_addr_lookup("127.128.129.130", AF_UNSPEC, &t1));
  207. tt_int_op(AF_INET,OP_EQ, tor_addr_family(&t1));
  208. tt_int_op(tor_addr_to_ipv4h(&t1),OP_EQ, 0x7f808182);
  209. tt_int_op(0,OP_EQ, tor_addr_lookup("9000::5", AF_UNSPEC, &t1));
  210. tt_int_op(AF_INET6,OP_EQ, tor_addr_family(&t1));
  211. tt_int_op(0x90,OP_EQ, tor_addr_to_in6_addr8(&t1)[0]);
  212. tt_assert(tor_mem_is_zero((char*)tor_addr_to_in6_addr8(&t1)+1, 14));
  213. tt_int_op(0x05,OP_EQ, tor_addr_to_in6_addr8(&t1)[15]);
  214. /* === Test pton: valid af_inet6 */
  215. /* Simple, valid parsing. */
  216. r = tor_inet_pton(AF_INET6,
  217. "0102:0304:0506:0708:090A:0B0C:0D0E:0F10", &a1);
  218. tt_int_op(r, OP_EQ, 1);
  219. for (i=0;i<16;++i) { tt_int_op(i+1,OP_EQ, (int)a1.s6_addr[i]); }
  220. /* ipv4 ending. */
  221. test_pton6_same("0102:0304:0506:0708:090A:0B0C:0D0E:0F10",
  222. "0102:0304:0506:0708:090A:0B0C:13.14.15.16");
  223. /* shortened words. */
  224. test_pton6_same("0001:0099:BEEF:0000:0123:FFFF:0001:0001",
  225. "1:99:BEEF:0:0123:FFFF:1:1");
  226. /* zeros at the beginning */
  227. test_pton6_same("0000:0000:0000:0000:0009:C0A8:0001:0001",
  228. "::9:c0a8:1:1");
  229. test_pton6_same("0000:0000:0000:0000:0009:C0A8:0001:0001",
  230. "::9:c0a8:0.1.0.1");
  231. /* zeros in the middle. */
  232. test_pton6_same("fe80:0000:0000:0000:0202:1111:0001:0001",
  233. "fe80::202:1111:1:1");
  234. /* zeros at the end. */
  235. test_pton6_same("1000:0001:0000:0007:0000:0000:0000:0000",
  236. "1000:1:0:7::");
  237. /* === Test ntop: af_inet6 */
  238. test_ntop6_reduces("0:0:0:0:0:0:0:0", "::");
  239. test_ntop6_reduces("0001:0099:BEEF:0006:0123:FFFF:0001:0001",
  240. "1:99:beef:6:123:ffff:1:1");
  241. //test_ntop6_reduces("0:0:0:0:0:0:c0a8:0101", "::192.168.1.1");
  242. test_ntop6_reduces("0:0:0:0:0:ffff:c0a8:0101", "::ffff:192.168.1.1");
  243. test_ntop6_reduces("0:0:0:0:0:0:c0a8:0101", "::192.168.1.1");
  244. test_ntop6_reduces("002:0:0000:0:3::4", "2::3:0:0:4");
  245. test_ntop6_reduces("0:0::1:0:3", "::1:0:3");
  246. test_ntop6_reduces("008:0::0", "8::");
  247. test_ntop6_reduces("0:0:0:0:0:ffff::1", "::ffff:0.0.0.1");
  248. test_ntop6_reduces("abcd:0:0:0:0:0:7f00::", "abcd::7f00:0");
  249. test_ntop6_reduces("0000:0000:0000:0000:0009:C0A8:0001:0001",
  250. "::9:c0a8:1:1");
  251. test_ntop6_reduces("fe80:0000:0000:0000:0202:1111:0001:0001",
  252. "fe80::202:1111:1:1");
  253. test_ntop6_reduces("1000:0001:0000:0007:0000:0000:0000:0000",
  254. "1000:1:0:7::");
  255. /* Bad af param */
  256. tt_int_op(tor_inet_pton(AF_UNSPEC, 0, 0),OP_EQ, -1);
  257. /* === Test pton: invalid in6. */
  258. test_pton6_bad("foobar.");
  259. test_pton6_bad("-1::");
  260. test_pton6_bad("00001::");
  261. test_pton6_bad("10000::");
  262. test_pton6_bad("::10000");
  263. test_pton6_bad("55555::");
  264. test_pton6_bad("9:-60::");
  265. test_pton6_bad("9:+60::");
  266. test_pton6_bad("9|60::");
  267. test_pton6_bad("0x60::");
  268. test_pton6_bad("::0x60");
  269. test_pton6_bad("9:0x60::");
  270. test_pton6_bad("1:2:33333:4:0002:3::");
  271. test_pton6_bad("1:2:3333:4:fish:3::");
  272. test_pton6_bad("1:2:3:4:5:6:7:8:9");
  273. test_pton6_bad("1:2:3:4:5:6:7");
  274. test_pton6_bad("1:2:3:4:5:6:1.2.3.4.5");
  275. test_pton6_bad("1:2:3:4:5:6:1.2.3");
  276. test_pton6_bad("::1.2.3");
  277. test_pton6_bad("::1.2.3.4.5");
  278. test_pton6_bad("::ffff:0xff.0.0.0");
  279. test_pton6_bad("::ffff:ff.0.0.0");
  280. test_pton6_bad("::ffff:256.0.0.0");
  281. test_pton6_bad("::ffff:-1.0.0.0");
  282. test_pton6_bad("99");
  283. test_pton6_bad("");
  284. test_pton6_bad(".");
  285. test_pton6_bad(":");
  286. test_pton6_bad("1::2::3:4");
  287. test_pton6_bad("a:::b:c");
  288. test_pton6_bad(":::a:b:c");
  289. test_pton6_bad("a:b:c:::");
  290. test_pton6_bad("1.2.3.4");
  291. test_pton6_bad(":1.2.3.4");
  292. test_pton6_bad(".2.3.4");
  293. /* Regression tests for 22789. */
  294. test_pton6_bad("0xfoo");
  295. test_pton6_bad("0x88");
  296. test_pton6_bad("0xyxxy");
  297. test_pton6_bad("0XFOO");
  298. test_pton6_bad("0X88");
  299. test_pton6_bad("0XYXXY");
  300. test_pton6_bad("0x");
  301. test_pton6_bad("0X");
  302. /* test internal checking */
  303. test_external_ip("fbff:ffff::2:7", 0);
  304. test_internal_ip("fc01::2:7", 0);
  305. test_internal_ip("fc01::02:7", 0);
  306. test_internal_ip("fc01::002:7", 0);
  307. test_internal_ip("fc01::0002:7", 0);
  308. test_internal_ip("fdff:ffff::f:f", 0);
  309. test_external_ip("fe00::3:f", 0);
  310. test_external_ip("fe7f:ffff::2:7", 0);
  311. test_internal_ip("fe80::2:7", 0);
  312. test_internal_ip("febf:ffff::f:f", 0);
  313. test_internal_ip("fec0::2:7:7", 0);
  314. test_internal_ip("feff:ffff::e:7:7", 0);
  315. test_external_ip("ff00::e:7:7", 0);
  316. test_internal_ip("::", 0);
  317. test_internal_ip("::1", 0);
  318. test_internal_ip("::1", 1);
  319. test_internal_ip("::", 0);
  320. test_external_ip("::", 1);
  321. test_external_ip("::2", 0);
  322. test_external_ip("2001::", 0);
  323. test_external_ip("ffff::", 0);
  324. test_external_ip("::ffff:0.0.0.0", 1);
  325. test_internal_ip("::ffff:0.0.0.0", 0);
  326. test_internal_ip("::ffff:0.255.255.255", 0);
  327. test_external_ip("::ffff:1.0.0.0", 0);
  328. test_external_ip("::ffff:9.255.255.255", 0);
  329. test_internal_ip("::ffff:10.0.0.0", 0);
  330. test_internal_ip("::ffff:10.255.255.255", 0);
  331. test_external_ip("::ffff:11.0.0.0", 0);
  332. test_external_ip("::ffff:126.255.255.255", 0);
  333. test_internal_ip("::ffff:127.0.0.0", 0);
  334. test_internal_ip("::ffff:127.255.255.255", 0);
  335. test_external_ip("::ffff:128.0.0.0", 0);
  336. test_external_ip("::ffff:172.15.255.255", 0);
  337. test_internal_ip("::ffff:172.16.0.0", 0);
  338. test_internal_ip("::ffff:172.31.255.255", 0);
  339. test_external_ip("::ffff:172.32.0.0", 0);
  340. test_external_ip("::ffff:192.167.255.255", 0);
  341. test_internal_ip("::ffff:192.168.0.0", 0);
  342. test_internal_ip("::ffff:192.168.255.255", 0);
  343. test_external_ip("::ffff:192.169.0.0", 0);
  344. test_external_ip("::ffff:169.253.255.255", 0);
  345. test_internal_ip("::ffff:169.254.0.0", 0);
  346. test_internal_ip("::ffff:169.254.255.255", 0);
  347. test_external_ip("::ffff:169.255.0.0", 0);
  348. /* tor_addr_compare(tor_addr_t x2) */
  349. test_addr_compare("ffff::", OP_EQ, "ffff::0");
  350. test_addr_compare("0::3:2:1", OP_LT, "0::ffff:0.3.2.1");
  351. test_addr_compare("0::2:2:1", OP_LT, "0::ffff:0.3.2.1");
  352. test_addr_compare("0::ffff:0.3.2.1", OP_GT, "0::0:0:0");
  353. test_addr_compare("0::ffff:5.2.2.1", OP_LT,
  354. "::ffff:6.0.0.0"); /* XXXX wrong. */
  355. tor_addr_parse_mask_ports("[::ffff:2.3.4.5]", 0, &t1, NULL, NULL, NULL);
  356. tor_addr_parse_mask_ports("2.3.4.5", 0, &t2, NULL, NULL, NULL);
  357. tt_int_op(tor_addr_compare(&t1, &t2, CMP_SEMANTIC), OP_EQ, 0);
  358. tor_addr_parse_mask_ports("[::ffff:2.3.4.4]", 0, &t1, NULL, NULL, NULL);
  359. tor_addr_parse_mask_ports("2.3.4.5", 0, &t2, NULL, NULL, NULL);
  360. tt_int_op(tor_addr_compare(&t1, &t2, CMP_SEMANTIC), OP_LT, 0);
  361. /* test compare_masked */
  362. test_addr_compare_masked("ffff::", OP_EQ, "ffff::0", 128);
  363. test_addr_compare_masked("ffff::", OP_EQ, "ffff::0", 64);
  364. test_addr_compare_masked("0::2:2:1", OP_LT, "0::8000:2:1", 81);
  365. test_addr_compare_masked("0::2:2:1", OP_EQ, "0::8000:2:1", 80);
  366. /* Test undecorated tor_addr_to_str */
  367. tt_int_op(AF_INET6,OP_EQ, tor_addr_parse(&t1, "[123:45:6789::5005:11]"));
  368. p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 0);
  369. tt_str_op(p1,OP_EQ, "123:45:6789::5005:11");
  370. tt_int_op(AF_INET,OP_EQ, tor_addr_parse(&t1, "18.0.0.1"));
  371. p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 0);
  372. tt_str_op(p1,OP_EQ, "18.0.0.1");
  373. /* Test decorated tor_addr_to_str */
  374. tt_int_op(AF_INET6,OP_EQ, tor_addr_parse(&t1, "[123:45:6789::5005:11]"));
  375. p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 1);
  376. tt_str_op(p1,OP_EQ, "[123:45:6789::5005:11]");
  377. tt_int_op(AF_INET,OP_EQ, tor_addr_parse(&t1, "18.0.0.1"));
  378. p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 1);
  379. tt_str_op(p1,OP_EQ, "18.0.0.1");
  380. /* Test buffer bounds checking of tor_addr_to_str */
  381. tt_int_op(AF_INET6,OP_EQ, tor_addr_parse(&t1, "::")); /* 2 + \0 */
  382. tt_ptr_op(tor_addr_to_str(buf, &t1, 2, 0),OP_EQ, NULL); /* too short buf */
  383. tt_str_op(tor_addr_to_str(buf, &t1, 3, 0),OP_EQ, "::");
  384. tt_ptr_op(tor_addr_to_str(buf, &t1, 4, 1),OP_EQ, NULL); /* too short buf */
  385. tt_str_op(tor_addr_to_str(buf, &t1, 5, 1),OP_EQ, "[::]");
  386. tt_int_op(AF_INET6,OP_EQ, tor_addr_parse(&t1, "2000::1337")); /* 10 + \0 */
  387. tt_ptr_op(tor_addr_to_str(buf, &t1, 10, 0),OP_EQ, NULL); /* too short buf */
  388. tt_str_op(tor_addr_to_str(buf, &t1, 11, 0),OP_EQ, "2000::1337");
  389. tt_ptr_op(tor_addr_to_str(buf, &t1, 12, 1),OP_EQ, NULL); /* too short buf */
  390. tt_str_op(tor_addr_to_str(buf, &t1, 13, 1),OP_EQ, "[2000::1337]");
  391. tt_int_op(AF_INET,OP_EQ, tor_addr_parse(&t1, "1.2.3.4")); /* 7 + \0 */
  392. tt_ptr_op(tor_addr_to_str(buf, &t1, 7, 0),OP_EQ, NULL); /* too short buf */
  393. tt_str_op(tor_addr_to_str(buf, &t1, 8, 0),OP_EQ, "1.2.3.4");
  394. tt_int_op(AF_INET, OP_EQ,
  395. tor_addr_parse(&t1, "255.255.255.255")); /* 15 + \0 */
  396. tt_ptr_op(tor_addr_to_str(buf, &t1, 15, 0),OP_EQ, NULL); /* too short buf */
  397. tt_str_op(tor_addr_to_str(buf, &t1, 16, 0),OP_EQ, "255.255.255.255");
  398. tt_ptr_op(tor_addr_to_str(buf, &t1, 15, 1),OP_EQ, NULL); /* too short buf */
  399. tt_str_op(tor_addr_to_str(buf, &t1, 16, 1),OP_EQ, "255.255.255.255");
  400. t1.family = AF_UNSPEC;
  401. tt_ptr_op(tor_addr_to_str(buf, &t1, sizeof(buf), 0),OP_EQ, NULL);
  402. /* Test tor_addr_parse_PTR_name */
  403. i = tor_addr_parse_PTR_name(&t1, "Foobar.baz", AF_UNSPEC, 0);
  404. tt_int_op(0,OP_EQ, i);
  405. i = tor_addr_parse_PTR_name(&t1, "Foobar.baz", AF_UNSPEC, 1);
  406. tt_int_op(0,OP_EQ, i);
  407. i = tor_addr_parse_PTR_name(&t1, "9999999999999999999999999999.in-addr.arpa",
  408. AF_UNSPEC, 1);
  409. tt_int_op(-1,OP_EQ, i);
  410. i = tor_addr_parse_PTR_name(&t1, "1.0.168.192.in-addr.arpa",
  411. AF_UNSPEC, 1);
  412. tt_int_op(1,OP_EQ, i);
  413. tt_int_op(tor_addr_family(&t1),OP_EQ, AF_INET);
  414. p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 1);
  415. tt_str_op(p1,OP_EQ, "192.168.0.1");
  416. i = tor_addr_parse_PTR_name(&t1, "192.168.0.99", AF_UNSPEC, 0);
  417. tt_int_op(0,OP_EQ, i);
  418. i = tor_addr_parse_PTR_name(&t1, "192.168.0.99", AF_UNSPEC, 1);
  419. tt_int_op(1,OP_EQ, i);
  420. p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 1);
  421. tt_str_op(p1,OP_EQ, "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. tt_int_op(1,OP_EQ, i);
  429. p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 1);
  430. tt_str_op(p1,OP_EQ, "[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. tt_int_op(i,OP_EQ, -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. tt_int_op(i,OP_EQ, -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. tt_int_op(i,OP_EQ, -1);
  450. i = tor_addr_parse_PTR_name(&t1, "32.1.1.in-addr.arpa",
  451. AF_UNSPEC, 0);
  452. tt_int_op(i,OP_EQ, -1);
  453. i = tor_addr_parse_PTR_name(&t1, ".in-addr.arpa",
  454. AF_UNSPEC, 0);
  455. tt_int_op(i,OP_EQ, -1);
  456. i = tor_addr_parse_PTR_name(&t1, "1.2.3.4.5.in-addr.arpa",
  457. AF_UNSPEC, 0);
  458. tt_int_op(i,OP_EQ, -1);
  459. i = tor_addr_parse_PTR_name(&t1, "1.2.3.4.5.in-addr.arpa",
  460. AF_INET6, 0);
  461. tt_int_op(i,OP_EQ, -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. tt_int_op(i,OP_EQ, -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. tt_int_op(tor_addr_to_PTR_name(rbuf, 1, &t1),OP_EQ, -1);
  477. tt_int_op(tor_addr_to_PTR_name(rbuf,
  478. strlen("3.2.1.127.in-addr.arpa") - 1,
  479. &t1),OP_EQ, -1);
  480. /* Check IPv4 PTR - valid addr */
  481. tt_int_op(tor_addr_to_PTR_name(rbuf, sizeof(rbuf), &t1),OP_EQ,
  482. strlen("3.2.1.127.in-addr.arpa"));
  483. tt_str_op(rbuf,OP_EQ, "3.2.1.127.in-addr.arpa");
  484. /* Invalid addr family */
  485. t1.family = AF_UNSPEC;
  486. tt_int_op(tor_addr_to_PTR_name(rbuf, sizeof(rbuf), &t1),OP_EQ, -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. tt_int_op(tor_addr_to_PTR_name(rbuf, 0, &t1),OP_EQ, -1);
  500. tt_int_op(tor_addr_to_PTR_name(rbuf, strlen(addr_PTR) - 1, &t1),OP_EQ, -1);
  501. /* Check IPv6 PTR - valid addr */
  502. tt_int_op(tor_addr_to_PTR_name(rbuf, sizeof(rbuf), &t1),OP_EQ,
  503. strlen(addr_PTR));
  504. tt_str_op(rbuf,OP_EQ, 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. tt_str_op(p1,OP_EQ, "::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. tt_str_op(p1,OP_EQ, "::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. tt_str_op(p1,OP_EQ, "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. tt_int_op(r, OP_EQ, AF_INET6);
  523. r=tor_addr_parse_mask_ports("[ffff:1111:1111:1111:1111:1111:1111:11111]",
  524. 0, &t1, NULL, NULL, NULL);
  525. tt_int_op(r, OP_EQ, -1);
  526. r=tor_addr_parse_mask_ports("[ffff:1111:1111:1111:1111:1111:1111:1111:1]",
  527. 0, &t1, NULL, NULL, NULL);
  528. tt_int_op(r, OP_EQ, -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. tt_int_op(r, OP_EQ, -1);
  536. /* Try some failing cases. */
  537. r=tor_addr_parse_mask_ports("[fefef::]/112", 0, &t1, NULL, NULL, NULL);
  538. tt_int_op(r, OP_EQ, -1);
  539. r=tor_addr_parse_mask_ports("[fefe::/112", 0, &t1, NULL, NULL, NULL);
  540. tt_int_op(r, OP_EQ, -1);
  541. r=tor_addr_parse_mask_ports("[fefe::", 0, &t1, NULL, NULL, NULL);
  542. tt_int_op(r, OP_EQ, -1);
  543. r=tor_addr_parse_mask_ports("[fefe::X]", 0, &t1, NULL, NULL, NULL);
  544. tt_int_op(r, OP_EQ, -1);
  545. r=tor_addr_parse_mask_ports("efef::/112", 0, &t1, NULL, NULL, NULL);
  546. tt_int_op(r, OP_EQ, -1);
  547. r=tor_addr_parse_mask_ports("[f:f:f:f:f:f:f:f::]",0,&t1, NULL, NULL, NULL);
  548. tt_int_op(r, OP_EQ, -1);
  549. r=tor_addr_parse_mask_ports("[::f:f:f:f:f:f:f:f]",0,&t1, NULL, NULL, NULL);
  550. tt_int_op(r, OP_EQ, -1);
  551. r=tor_addr_parse_mask_ports("[f:f:f:f:f:f:f:f:f]",0,&t1, NULL, NULL, NULL);
  552. tt_int_op(r, OP_EQ, -1);
  553. r=tor_addr_parse_mask_ports("[f:f:f:f:f::]/fred",0,&t1,&mask, NULL, NULL);
  554. tt_int_op(r, OP_EQ, -1);
  555. r=tor_addr_parse_mask_ports("[f:f:f:f:f::]/255.255.0.0",
  556. 0,&t1, NULL, NULL, NULL);
  557. tt_int_op(r, OP_EQ, -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. tt_int_op(r, OP_EQ, -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. tt_int_op(r, OP_EQ, -1);
  564. r=tor_addr_parse_mask_ports("1.1.2.2/33",0,&t1, &mask, NULL, NULL);
  565. tt_int_op(r, OP_EQ, -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. tt_int_op(r, OP_EQ, -1);
  569. r=tor_addr_parse_mask_ports("*6",0,&t1, &mask, NULL, NULL);
  570. tt_int_op(r, OP_EQ, -1);
  571. tt_int_op(r, OP_EQ, -1);
  572. /* Try a mask with a wildcard. */
  573. r=tor_addr_parse_mask_ports("*/16",0,&t1, &mask, NULL, NULL);
  574. tt_int_op(r, OP_EQ, -1);
  575. r=tor_addr_parse_mask_ports("*4/16",TAPMP_EXTENDED_STAR,
  576. &t1, &mask, NULL, NULL);
  577. tt_int_op(r, OP_EQ, -1);
  578. r=tor_addr_parse_mask_ports("*6/30",TAPMP_EXTENDED_STAR,
  579. &t1, &mask, NULL, NULL);
  580. tt_int_op(r, OP_EQ, -1);
  581. /* Basic mask tests*/
  582. r=tor_addr_parse_mask_ports("1.1.2.2/31",0,&t1, &mask, NULL, NULL);
  583. tt_int_op(r, OP_EQ, AF_INET);
  584. tt_int_op(mask,OP_EQ,31);
  585. tt_int_op(tor_addr_family(&t1),OP_EQ,AF_INET);
  586. tt_int_op(tor_addr_to_ipv4h(&t1),OP_EQ,0x01010202);
  587. r=tor_addr_parse_mask_ports("3.4.16.032:1-2",0,&t1, &mask, &port1, &port2);
  588. tt_int_op(r, OP_EQ, AF_INET);
  589. tt_int_op(mask,OP_EQ,32);
  590. tt_int_op(tor_addr_family(&t1),OP_EQ,AF_INET);
  591. tt_int_op(tor_addr_to_ipv4h(&t1),OP_EQ,0x03041020);
  592. tt_uint_op(port1, OP_EQ, 1);
  593. tt_uint_op(port2, OP_EQ, 2);
  594. r=tor_addr_parse_mask_ports("1.1.2.3/255.255.128.0",0,&t1, &mask,NULL,NULL);
  595. tt_int_op(r, OP_EQ, AF_INET);
  596. tt_int_op(mask,OP_EQ,17);
  597. tt_int_op(tor_addr_family(&t1),OP_EQ,AF_INET);
  598. tt_int_op(tor_addr_to_ipv4h(&t1),OP_EQ,0x01010203);
  599. r=tor_addr_parse_mask_ports("[efef::]/112",0,&t1, &mask, &port1, &port2);
  600. tt_int_op(r, OP_EQ, AF_INET6);
  601. tt_uint_op(port1, OP_EQ, 1);
  602. tt_uint_op(port2, OP_EQ, 65535);
  603. /* Try regular wildcard behavior without TAPMP_EXTENDED_STAR */
  604. r=tor_addr_parse_mask_ports("*:80-443",0,&t1,&mask,&port1,&port2);
  605. tt_int_op(r,OP_EQ,AF_INET); /* Old users of this always get inet */
  606. tt_int_op(tor_addr_family(&t1),OP_EQ,AF_INET);
  607. tt_int_op(tor_addr_to_ipv4h(&t1),OP_EQ,0);
  608. tt_int_op(mask,OP_EQ,0);
  609. tt_int_op(port1,OP_EQ,80);
  610. tt_int_op(port2,OP_EQ,443);
  611. /* Now try wildcards *with* TAPMP_EXTENDED_STAR */
  612. r=tor_addr_parse_mask_ports("*:8000-9000",TAPMP_EXTENDED_STAR,
  613. &t1,&mask,&port1,&port2);
  614. tt_int_op(r,OP_EQ,AF_UNSPEC);
  615. tt_int_op(tor_addr_family(&t1),OP_EQ,AF_UNSPEC);
  616. tt_int_op(mask,OP_EQ,0);
  617. tt_int_op(port1,OP_EQ,8000);
  618. tt_int_op(port2,OP_EQ,9000);
  619. r=tor_addr_parse_mask_ports("*4:6667",TAPMP_EXTENDED_STAR,
  620. &t1,&mask,&port1,&port2);
  621. tt_int_op(r,OP_EQ,AF_INET);
  622. tt_int_op(tor_addr_family(&t1),OP_EQ,AF_INET);
  623. tt_int_op(tor_addr_to_ipv4h(&t1),OP_EQ,0);
  624. tt_int_op(mask,OP_EQ,0);
  625. tt_int_op(port1,OP_EQ,6667);
  626. tt_int_op(port2,OP_EQ,6667);
  627. r=tor_addr_parse_mask_ports("*6",TAPMP_EXTENDED_STAR,
  628. &t1,&mask,&port1,&port2);
  629. tt_int_op(r,OP_EQ,AF_INET6);
  630. tt_int_op(tor_addr_family(&t1),OP_EQ,AF_INET6);
  631. tt_assert(tor_mem_is_zero((const char*)tor_addr_to_in6_addr32(&t1), 16));
  632. tt_int_op(mask,OP_EQ,0);
  633. tt_int_op(port1,OP_EQ,1);
  634. tt_int_op(port2,OP_EQ,65535);
  635. /* make sure inet address lengths >= max */
  636. tt_int_op(INET_NTOA_BUF_LEN, OP_GE, sizeof("255.255.255.255"));
  637. tt_int_op(TOR_ADDR_BUF_LEN, OP_GE,
  638. sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"));
  639. tt_assert(sizeof(tor_addr_t) >= sizeof(struct in6_addr));
  640. /* get interface addresses */
  641. r = get_interface_address6(LOG_DEBUG, AF_INET, &t1);
  642. tt_int_op(r, OP_LE, 0); // "it worked or it didn't"
  643. i = get_interface_address6(LOG_DEBUG, AF_INET6, &t2);
  644. tt_int_op(i, OP_LE, 0); // "it worked or it didn't"
  645. TT_BLATHER(("v4 address: %s (family=%d)", fmt_addr(&t1),
  646. tor_addr_family(&t1)));
  647. TT_BLATHER(("v6 address: %s (family=%d)", fmt_addr(&t2),
  648. tor_addr_family(&t2)));
  649. done:
  650. ;
  651. }
  652. /** Test tor_addr_port_parse(). */
  653. static void
  654. test_addr_parse(void *arg)
  655. {
  656. int r;
  657. tor_addr_t addr;
  658. char buf[TOR_ADDR_BUF_LEN];
  659. uint16_t port = 0;
  660. /* Correct call. */
  661. (void)arg;
  662. r= tor_addr_port_parse(LOG_DEBUG,
  663. "192.0.2.1:1234",
  664. &addr, &port, -1);
  665. tt_int_op(r, OP_EQ, 0);
  666. tor_addr_to_str(buf, &addr, sizeof(buf), 0);
  667. tt_str_op(buf,OP_EQ, "192.0.2.1");
  668. tt_int_op(port,OP_EQ, 1234);
  669. r= tor_addr_port_parse(LOG_DEBUG,
  670. "[::1]:1234",
  671. &addr, &port, -1);
  672. tt_int_op(r, OP_EQ, 0);
  673. tor_addr_to_str(buf, &addr, sizeof(buf), 0);
  674. tt_str_op(buf,OP_EQ, "::1");
  675. tt_int_op(port,OP_EQ, 1234);
  676. /* Domain name. */
  677. r= tor_addr_port_parse(LOG_DEBUG,
  678. "torproject.org:1234",
  679. &addr, &port, -1);
  680. tt_int_op(r, OP_EQ, -1);
  681. /* Only IP. */
  682. r= tor_addr_port_parse(LOG_DEBUG,
  683. "192.0.2.2",
  684. &addr, &port, -1);
  685. tt_int_op(r, OP_EQ, -1);
  686. r= tor_addr_port_parse(LOG_DEBUG,
  687. "192.0.2.2",
  688. &addr, &port, 200);
  689. tt_int_op(r, OP_EQ, 0);
  690. tt_int_op(port,OP_EQ,200);
  691. r= tor_addr_port_parse(LOG_DEBUG,
  692. "[::1]",
  693. &addr, &port, -1);
  694. tt_int_op(r, OP_EQ, -1);
  695. r= tor_addr_port_parse(LOG_DEBUG,
  696. "[::1]",
  697. &addr, &port, 400);
  698. tt_int_op(r, OP_EQ, 0);
  699. tt_int_op(port,OP_EQ,400);
  700. /* Bad port. */
  701. r= tor_addr_port_parse(LOG_DEBUG,
  702. "192.0.2.2:66666",
  703. &addr, &port, -1);
  704. tt_int_op(r, OP_EQ, -1);
  705. r= tor_addr_port_parse(LOG_DEBUG,
  706. "192.0.2.2:66666",
  707. &addr, &port, 200);
  708. tt_int_op(r, OP_EQ, -1);
  709. /* Only domain name */
  710. r= tor_addr_port_parse(LOG_DEBUG,
  711. "torproject.org",
  712. &addr, &port, -1);
  713. tt_int_op(r, OP_EQ, -1);
  714. r= tor_addr_port_parse(LOG_DEBUG,
  715. "torproject.org",
  716. &addr, &port, 200);
  717. tt_int_op(r, OP_EQ, -1);
  718. /* Bad IP address */
  719. r= tor_addr_port_parse(LOG_DEBUG,
  720. "192.0.2:1234",
  721. &addr, &port, -1);
  722. tt_int_op(r, OP_EQ, -1);
  723. /* Make sure that the default port has lower priority than the real
  724. one */
  725. r= tor_addr_port_parse(LOG_DEBUG,
  726. "192.0.2.2:1337",
  727. &addr, &port, 200);
  728. tt_int_op(r, OP_EQ, 0);
  729. tt_int_op(port,OP_EQ,1337);
  730. r= tor_addr_port_parse(LOG_DEBUG,
  731. "[::1]:1369",
  732. &addr, &port, 200);
  733. tt_int_op(r, OP_EQ, 0);
  734. tt_int_op(port,OP_EQ,1369);
  735. done:
  736. ;
  737. }
  738. static void
  739. update_difference(int ipv6, uint8_t *d,
  740. const tor_addr_t *a, const tor_addr_t *b)
  741. {
  742. const int n_bytes = ipv6 ? 16 : 4;
  743. uint8_t a_tmp[4], b_tmp[4];
  744. const uint8_t *ba, *bb;
  745. int i;
  746. if (ipv6) {
  747. ba = tor_addr_to_in6_addr8(a);
  748. bb = tor_addr_to_in6_addr8(b);
  749. } else {
  750. set_uint32(a_tmp, tor_addr_to_ipv4n(a));
  751. set_uint32(b_tmp, tor_addr_to_ipv4n(b));
  752. ba = a_tmp; bb = b_tmp;
  753. }
  754. for (i = 0; i < n_bytes; ++i) {
  755. d[i] |= ba[i] ^ bb[i];
  756. }
  757. }
  758. static void
  759. test_virtaddrmap(void *data)
  760. {
  761. /* Let's start with a bunch of random addresses. */
  762. int ipv6, bits, iter, b;
  763. virtual_addr_conf_t cfg[2];
  764. uint8_t bytes[16];
  765. (void)data;
  766. tor_addr_parse(&cfg[0].addr, "64.65.0.0");
  767. tor_addr_parse(&cfg[1].addr, "3491:c0c0::");
  768. for (ipv6 = 0; ipv6 <= 1; ++ipv6) {
  769. for (bits = 0; bits < 18; ++bits) {
  770. tor_addr_t last_a;
  771. cfg[ipv6].bits = bits;
  772. memset(bytes, 0, sizeof(bytes));
  773. tor_addr_copy(&last_a, &cfg[ipv6].addr);
  774. /* Generate 128 addresses with each addr/bits combination. */
  775. for (iter = 0; iter < 128; ++iter) {
  776. tor_addr_t a;
  777. get_random_virtual_addr(&cfg[ipv6], &a);
  778. //printf("%s\n", fmt_addr(&a));
  779. /* Make sure that the first b bits match the configured network */
  780. tt_int_op(0, OP_EQ, tor_addr_compare_masked(&a, &cfg[ipv6].addr,
  781. bits, CMP_EXACT));
  782. /* And track which bits have been different between pairs of
  783. * addresses */
  784. update_difference(ipv6, bytes, &last_a, &a);
  785. }
  786. /* Now make sure all but the first 'bits' bits of bytes are true */
  787. for (b = bits+1; b < (ipv6?128:32); ++b) {
  788. tt_assert(1 & (bytes[b/8] >> (7-(b&7))));
  789. }
  790. }
  791. }
  792. done:
  793. ;
  794. }
  795. static const char *canned_data = NULL;
  796. static size_t canned_data_len = 0;
  797. /* Mock replacement for crypto_rand() that returns canned data from
  798. * canned_data above. */
  799. static void
  800. crypto_canned(char *ptr, size_t n)
  801. {
  802. if (canned_data_len) {
  803. size_t to_copy = MIN(n, canned_data_len);
  804. memcpy(ptr, canned_data, to_copy);
  805. canned_data += to_copy;
  806. canned_data_len -= to_copy;
  807. n -= to_copy;
  808. ptr += to_copy;
  809. }
  810. if (n) {
  811. crypto_rand_unmocked(ptr, n);
  812. }
  813. }
  814. static void
  815. test_virtaddrmap_persist(void *data)
  816. {
  817. (void)data;
  818. const char *a, *b, *c;
  819. tor_addr_t addr;
  820. char *ones = NULL;
  821. addressmap_init();
  822. // Try a hostname.
  823. a = addressmap_register_virtual_address(RESOLVED_TYPE_HOSTNAME,
  824. tor_strdup("foobar.baz"));
  825. tt_assert(a);
  826. tt_assert(!strcmpend(a, ".virtual"));
  827. // mock crypto_rand to repeat the same result twice; make sure we get
  828. // different outcomes. (Because even though the odds for receiving the
  829. // same 80-bit address twice is only 1/2^40, it could still happen for
  830. // some user -- but running our test through 2^40 iterations isn't
  831. // reasonable.)
  832. canned_data = "1234567890" // the first call returns this.
  833. "1234567890" // the second call returns this.
  834. "abcdefghij"; // the third call returns this.
  835. canned_data_len = 30;
  836. MOCK(crypto_rand, crypto_canned);
  837. a = addressmap_register_virtual_address(RESOLVED_TYPE_HOSTNAME,
  838. tor_strdup("quuxit.baz"));
  839. b = addressmap_register_virtual_address(RESOLVED_TYPE_HOSTNAME,
  840. tor_strdup("nescio.baz"));
  841. tt_assert(a);
  842. tt_assert(b);
  843. tt_str_op(a, OP_EQ, "gezdgnbvgy3tqojq.virtual");
  844. tt_str_op(b, OP_EQ, "mfrggzdfmztwq2lk.virtual");
  845. // Now try something to get us an ipv4 address
  846. UNMOCK(crypto_rand);
  847. tt_int_op(0,OP_EQ, parse_virtual_addr_network("192.168.0.0/16",
  848. AF_INET, 0, NULL));
  849. a = addressmap_register_virtual_address(RESOLVED_TYPE_IPV4,
  850. tor_strdup("foobar.baz"));
  851. tt_assert(a);
  852. tt_assert(!strcmpstart(a, "192.168."));
  853. tor_addr_parse(&addr, a);
  854. tt_int_op(AF_INET, OP_EQ, tor_addr_family(&addr));
  855. b = addressmap_register_virtual_address(RESOLVED_TYPE_IPV4,
  856. tor_strdup("quuxit.baz"));
  857. tt_str_op(b, OP_NE, a);
  858. tt_assert(!strcmpstart(b, "192.168."));
  859. // Try some canned entropy and verify all the we discard duplicates,
  860. // addresses that end with 0, and addresses that end with 255.
  861. MOCK(crypto_rand, crypto_canned);
  862. canned_data = "\x01\x02\x03\x04" // okay
  863. "\x01\x02\x03\x04" // duplicate
  864. "\x03\x04\x00\x00" // bad ending 1
  865. "\x05\x05\x00\xff" // bad ending 2
  866. "\x05\x06\x07\xf0"; // okay
  867. canned_data_len = 20;
  868. a = addressmap_register_virtual_address(RESOLVED_TYPE_IPV4,
  869. tor_strdup("wumble.onion"));
  870. b = addressmap_register_virtual_address(RESOLVED_TYPE_IPV4,
  871. tor_strdup("wumpus.onion"));
  872. tt_str_op(a, OP_EQ, "192.168.3.4");
  873. tt_str_op(b, OP_EQ, "192.168.7.240");
  874. // Now try IPv6!
  875. UNMOCK(crypto_rand);
  876. tt_int_op(0,OP_EQ, parse_virtual_addr_network("1010:F000::/20",
  877. AF_INET6, 0, NULL));
  878. a = addressmap_register_virtual_address(RESOLVED_TYPE_IPV6,
  879. tor_strdup("foobar.baz"));
  880. tt_assert(a);
  881. tt_assert(!strcmpstart(a, "[1010:f"));
  882. tor_addr_parse(&addr, a);
  883. tt_int_op(AF_INET6, OP_EQ, tor_addr_family(&addr));
  884. b = addressmap_register_virtual_address(RESOLVED_TYPE_IPV6,
  885. tor_strdup("quuxit.baz"));
  886. tt_str_op(b, OP_NE, a);
  887. tt_assert(!strcmpstart(b, "[1010:f"));
  888. // Try IPv6 with canned entropy, to make sure we detect duplicates.
  889. MOCK(crypto_rand, crypto_canned);
  890. canned_data = "acanthopterygian" // okay
  891. "cinematographist" // okay
  892. "acanthopterygian" // duplicate
  893. "acanthopterygian" // duplicate
  894. "acanthopterygian" // duplicate
  895. "cinematographist" // duplicate
  896. "coadministration"; // okay
  897. canned_data_len = 16 * 7;
  898. a = addressmap_register_virtual_address(RESOLVED_TYPE_IPV6,
  899. tor_strdup("wuffle.baz"));
  900. b = addressmap_register_virtual_address(RESOLVED_TYPE_IPV6,
  901. tor_strdup("gribble.baz"));
  902. c = addressmap_register_virtual_address(RESOLVED_TYPE_IPV6,
  903. tor_strdup("surprisingly-legible.baz"));
  904. tt_str_op(a, OP_EQ, "[1010:f16e:7468:6f70:7465:7279:6769:616e]");
  905. tt_str_op(b, OP_EQ, "[1010:fe65:6d61:746f:6772:6170:6869:7374]");
  906. tt_str_op(c, OP_EQ, "[1010:f164:6d69:6e69:7374:7261:7469:6f6e]");
  907. // Try address exhaustion: make sure we can actually fail if we
  908. // get too many already-existing addresses.
  909. canned_data_len = 128*1024;
  910. canned_data = ones = tor_malloc(canned_data_len);
  911. memset(ones, 1, canned_data_len);
  912. // There is some chance this one will fail if a previous random
  913. // allocation gave out the address already.
  914. a = addressmap_register_virtual_address(RESOLVED_TYPE_IPV4,
  915. tor_strdup("might-work.onion"));
  916. if (a) {
  917. tt_str_op(a, OP_EQ, "192.168.1.1");
  918. }
  919. setup_capture_of_logs(LOG_WARN);
  920. // This one will definitely fail, since we've set up the RNG to hand
  921. // out "1" forever.
  922. b = addressmap_register_virtual_address(RESOLVED_TYPE_IPV4,
  923. tor_strdup("wont-work.onion"));
  924. tt_assert(b == NULL);
  925. expect_single_log_msg_containing("Ran out of virtual addresses!");
  926. done:
  927. UNMOCK(crypto_rand);
  928. tor_free(ones);
  929. addressmap_free_all();
  930. teardown_capture_of_logs();
  931. }
  932. static void
  933. test_addr_localname(void *arg)
  934. {
  935. (void)arg;
  936. tt_assert(tor_addr_hostname_is_local("localhost"));
  937. tt_assert(tor_addr_hostname_is_local("LOCALHOST"));
  938. tt_assert(tor_addr_hostname_is_local("LocalHost"));
  939. tt_assert(tor_addr_hostname_is_local("local"));
  940. tt_assert(tor_addr_hostname_is_local("LOCAL"));
  941. tt_assert(tor_addr_hostname_is_local("here.now.local"));
  942. tt_assert(tor_addr_hostname_is_local("here.now.LOCAL"));
  943. tt_assert(!tor_addr_hostname_is_local(" localhost"));
  944. tt_assert(!tor_addr_hostname_is_local("www.torproject.org"));
  945. done:
  946. ;
  947. }
  948. static void
  949. test_addr_dup_ip(void *arg)
  950. {
  951. char *v = NULL;
  952. (void)arg;
  953. #define CHECK(ip, s) do { \
  954. v = tor_dup_ip(ip); \
  955. tt_str_op(v,OP_EQ,(s)); \
  956. tor_free(v); \
  957. } while (0)
  958. CHECK(0xffffffff, "255.255.255.255");
  959. CHECK(0x00000000, "0.0.0.0");
  960. CHECK(0x7f000001, "127.0.0.1");
  961. CHECK(0x01020304, "1.2.3.4");
  962. #undef CHECK
  963. done:
  964. tor_free(v);
  965. }
  966. static void
  967. test_addr_sockaddr_to_str(void *arg)
  968. {
  969. char *v = NULL;
  970. struct sockaddr_in sin;
  971. struct sockaddr_in6 sin6;
  972. struct sockaddr_storage ss;
  973. #ifdef HAVE_SYS_UN_H
  974. struct sockaddr_un s_un;
  975. #endif
  976. #define CHECK(sa, s) do { \
  977. v = tor_sockaddr_to_str((const struct sockaddr*) &(sa)); \
  978. tt_str_op(v,OP_EQ,(s)); \
  979. tor_free(v); \
  980. } while (0)
  981. (void)arg;
  982. memset(&ss,0,sizeof(ss));
  983. ss.ss_family = AF_UNSPEC;
  984. CHECK(ss, "unspec");
  985. memset(&sin,0,sizeof(sin));
  986. sin.sin_family = AF_INET;
  987. sin.sin_addr.s_addr = htonl(0x7f808001);
  988. sin.sin_port = htons(1234);
  989. CHECK(sin, "127.128.128.1:1234");
  990. #ifdef HAVE_SYS_UN_H
  991. memset(&s_un,0,sizeof(s_un));
  992. s_un.sun_family = AF_UNIX;
  993. strlcpy(s_un.sun_path, "/here/is/a/path", sizeof(s_un.sun_path));
  994. CHECK(s_un, "unix:/here/is/a/path");
  995. #endif /* defined(HAVE_SYS_UN_H) */
  996. memset(&sin6,0,sizeof(sin6));
  997. sin6.sin6_family = AF_INET6;
  998. memcpy(sin6.sin6_addr.s6_addr, "\x20\x00\x00\x00\x00\x00\x00\x00"
  999. "\x00\x1a\x2b\x3c\x4d\x5e\x00\x01", 16);
  1000. sin6.sin6_port = htons(1234);
  1001. CHECK(sin6, "[2000::1a:2b3c:4d5e:1]:1234");
  1002. done:
  1003. tor_free(v);
  1004. }
  1005. static void
  1006. test_addr_is_loopback(void *data)
  1007. {
  1008. static const struct loopback_item {
  1009. const char *name;
  1010. int is_loopback;
  1011. } loopback_items[] = {
  1012. { "::1", 1 },
  1013. { "127.0.0.1", 1 },
  1014. { "127.99.100.101", 1 },
  1015. { "128.99.100.101", 0 },
  1016. { "8.8.8.8", 0 },
  1017. { "0.0.0.0", 0 },
  1018. { "::2", 0 },
  1019. { "::", 0 },
  1020. { "::1.0.0.0", 0 },
  1021. { NULL, 0 }
  1022. };
  1023. int i;
  1024. tor_addr_t addr;
  1025. (void)data;
  1026. for (i=0; loopback_items[i].name; ++i) {
  1027. tt_int_op(tor_addr_parse(&addr, loopback_items[i].name), OP_GE, 0);
  1028. tt_int_op(tor_addr_is_loopback(&addr), OP_EQ,
  1029. loopback_items[i].is_loopback);
  1030. }
  1031. tor_addr_make_unspec(&addr);
  1032. tt_int_op(tor_addr_is_loopback(&addr), OP_EQ, 0);
  1033. done:
  1034. ;
  1035. }
  1036. static void
  1037. test_addr_make_null(void *data)
  1038. {
  1039. tor_addr_t *addr = tor_malloc(sizeof(*addr));
  1040. tor_addr_t *zeros = tor_malloc_zero(sizeof(*addr));
  1041. char buf[TOR_ADDR_BUF_LEN];
  1042. (void) data;
  1043. /* Ensure that before tor_addr_make_null, addr != 0's */
  1044. memset(addr, 1, sizeof(*addr));
  1045. tt_int_op(fast_memcmp(addr, zeros, sizeof(*addr)), OP_NE, 0);
  1046. /* Test with AF == AF_INET */
  1047. zeros->family = AF_INET;
  1048. tor_addr_make_null(addr, AF_INET);
  1049. tt_int_op(fast_memcmp(addr, zeros, sizeof(*addr)), OP_EQ, 0);
  1050. tt_str_op(tor_addr_to_str(buf, addr, sizeof(buf), 0), OP_EQ, "0.0.0.0");
  1051. /* Test with AF == AF_INET6 */
  1052. memset(addr, 1, sizeof(*addr));
  1053. zeros->family = AF_INET6;
  1054. tor_addr_make_null(addr, AF_INET6);
  1055. tt_int_op(fast_memcmp(addr, zeros, sizeof(*addr)), OP_EQ, 0);
  1056. tt_str_op(tor_addr_to_str(buf, addr, sizeof(buf), 0), OP_EQ, "::");
  1057. done:
  1058. tor_free(addr);
  1059. tor_free(zeros);
  1060. }
  1061. #define ADDR_LEGACY(name) \
  1062. { #name, test_addr_ ## name , 0, NULL, NULL }
  1063. struct testcase_t addr_tests[] = {
  1064. ADDR_LEGACY(basic),
  1065. ADDR_LEGACY(ip6_helpers),
  1066. ADDR_LEGACY(parse),
  1067. { "virtaddr", test_virtaddrmap, 0, NULL, NULL },
  1068. { "virtaddr_persist", test_virtaddrmap_persist, TT_FORK, NULL, NULL },
  1069. { "localname", test_addr_localname, 0, NULL, NULL },
  1070. { "dup_ip", test_addr_dup_ip, 0, NULL, NULL },
  1071. { "sockaddr_to_str", test_addr_sockaddr_to_str, 0, NULL, NULL },
  1072. { "is_loopback", test_addr_is_loopback, 0, NULL, NULL },
  1073. { "make_null", test_addr_make_null, 0, NULL, NULL },
  1074. END_OF_TESTCASES
  1075. };