test_addr.c 47 KB

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