test_addr.c 47 KB

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