test_addr.c 45 KB

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