test_addr.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665
  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 that addr_str successfully parses, and:
  654. * - the address has family expect_family,
  655. * - the fmt_decorated result of tor_addr_to_str() is expect_str.
  656. */
  657. #define TEST_ADDR_PARSE_FMT(addr_str, expect_family, fmt_decorated, \
  658. expect_str) \
  659. STMT_BEGIN \
  660. r = tor_addr_parse(&addr, addr_str); \
  661. tt_int_op(r, OP_EQ, expect_family); \
  662. sv = tor_addr_to_str(buf, &addr, sizeof(buf), fmt_decorated); \
  663. tt_str_op(sv, OP_EQ, buf); \
  664. tt_str_op(buf, OP_EQ, expect_str); \
  665. STMT_END
  666. /* Test that addr_str fails to parse, and:
  667. * - the returned address is null.
  668. */
  669. #define TEST_ADDR_PARSE_XFAIL(addr_str) \
  670. STMT_BEGIN \
  671. r = tor_addr_parse(&addr, addr_str); \
  672. tt_int_op(r, OP_EQ, -1); \
  673. tt_assert(tor_addr_is_null(&addr)); \
  674. STMT_END
  675. /* Test that addr_port_str and default_port successfully parse, and:
  676. * - the address has family expect_family,
  677. * - the fmt_decorated result of tor_addr_to_str() is expect_str,
  678. * - the port is expect_port.
  679. */
  680. #define TEST_ADDR_PORT_PARSE_FMT(addr_port_str, default_port, expect_family, \
  681. fmt_decorated, expect_str, expect_port) \
  682. STMT_BEGIN \
  683. r = tor_addr_port_parse(LOG_DEBUG, addr_port_str, &addr, &port, \
  684. default_port); \
  685. tt_int_op(r, OP_EQ, 0); \
  686. tt_int_op(tor_addr_family(&addr), OP_EQ, expect_family); \
  687. sv = tor_addr_to_str(buf, &addr, sizeof(buf), fmt_decorated); \
  688. tt_str_op(sv, OP_EQ, buf); \
  689. tt_str_op(buf, OP_EQ, expect_str); \
  690. tt_int_op(port, OP_EQ, expect_port); \
  691. STMT_END
  692. /* Test that addr_port_str and default_port fail to parse, and:
  693. * - the returned address is null,
  694. * - the returned port is 0.
  695. */
  696. #define TEST_ADDR_PORT_PARSE_XFAIL(addr_port_str, default_port) \
  697. STMT_BEGIN \
  698. r = tor_addr_port_parse(LOG_DEBUG, addr_port_str, &addr, &port, \
  699. default_port); \
  700. tt_int_op(r, OP_EQ, -1); \
  701. tt_assert(tor_addr_is_null(&addr)); \
  702. tt_int_op(port, OP_EQ, 0); \
  703. STMT_END
  704. /* Test that addr_str successfully parses as an IPv4 address using
  705. * tor_lookup_hostname(), and:
  706. * - the fmt_addr32() of the result is expect_str.
  707. */
  708. #define TEST_ADDR_V4_LOOKUP_HOSTNAME(addr_str, expect_str) \
  709. STMT_BEGIN \
  710. r = tor_lookup_hostname(addr_str, &addr32h); \
  711. tt_int_op(r, OP_EQ, 0); \
  712. tt_str_op(fmt_addr32(addr32h), OP_EQ, expect_str); \
  713. STMT_END
  714. /* Test that bad_str fails to parse using tor_lookup_hostname(), with a
  715. * permanent failure, and:
  716. * - the returned address is 0.
  717. */
  718. #define TEST_ADDR_V4_LOOKUP_XFAIL(bad_str) \
  719. STMT_BEGIN \
  720. r = tor_lookup_hostname(bad_str, &addr32h); \
  721. tt_int_op(r, OP_EQ, -1); \
  722. tt_int_op(addr32h, OP_EQ, 0); \
  723. STMT_END
  724. /* Test that looking up host_str as an IPv4 address using tor_lookup_hostname()
  725. * does something sensible:
  726. * - the result is -1, 0, or 1.
  727. * - if the result is a failure, the returned address is 0.
  728. * We can't rely on the result of this function, because it depends on the
  729. * network.
  730. */
  731. #define TEST_HOST_V4_LOOKUP(host_str) \
  732. STMT_BEGIN \
  733. r = tor_lookup_hostname(host_str, &addr32h); \
  734. tt_int_op(r, OP_GE, -1); \
  735. tt_int_op(r, OP_LE, 1); \
  736. if (r != 0) \
  737. tt_int_op(addr32h, OP_EQ, 0); \
  738. STMT_END
  739. /* Test that addr_str successfully parses as a require_family IP address using
  740. * tor_addr_lookup(), and:
  741. * - the address has family expect_family,
  742. * - the fmt_decorated result of tor_addr_to_str() is expect_str.
  743. */
  744. #define TEST_ADDR_LOOKUP_FMT(addr_str, require_family, expect_family, \
  745. fmt_decorated, expect_str) \
  746. STMT_BEGIN \
  747. r = tor_addr_lookup(addr_str, require_family, &addr); \
  748. tt_int_op(r, OP_EQ, 0); \
  749. tt_int_op(tor_addr_family(&addr), OP_EQ, expect_family); \
  750. sv = tor_addr_to_str(buf, &addr, sizeof(buf), fmt_decorated); \
  751. tt_str_op(sv, OP_EQ, buf); \
  752. tt_str_op(buf, OP_EQ, expect_str); \
  753. STMT_END
  754. /* Test that bad_str fails to parse as a require_family IP address using
  755. * tor_addr_lookup(), with a permanent failure, and:
  756. * - the returned address is null.
  757. */
  758. #define TEST_ADDR_LOOKUP_XFAIL(bad_str, require_family) \
  759. STMT_BEGIN \
  760. r = tor_addr_lookup(bad_str, require_family, &addr); \
  761. tt_int_op(r, OP_EQ, -1); \
  762. tt_assert(tor_addr_is_null(&addr)); \
  763. STMT_END
  764. /* Test that looking up host_string as a require_family IP address using
  765. * tor_addr_lookup(), does something sensible:
  766. * - the result is -1, 0, or 1.
  767. * - if the result is a failure, the returned address is null.
  768. * We can't rely on the result of this function, because it depends on the
  769. * network.
  770. */
  771. #define TEST_HOST_LOOKUP(host_str, require_family) \
  772. STMT_BEGIN \
  773. r = tor_addr_lookup(host_str, require_family, &addr); \
  774. tt_int_op(r, OP_GE, -1); \
  775. tt_int_op(r, OP_LE, 1); \
  776. if (r != 0) \
  777. tt_assert(tor_addr_is_null(&addr)); \
  778. STMT_END
  779. /* Test that addr_port_str successfully parses as an IP address and port
  780. * using tor_addr_port_lookup(), and:
  781. * - the address has family expect_family,
  782. * - the fmt_decorated result of tor_addr_to_str() is expect_str,
  783. * - the port is expect_port.
  784. */
  785. #define TEST_ADDR_PORT_LOOKUP_FMT(addr_port_str, expect_family, \
  786. fmt_decorated, expect_str, expect_port) \
  787. STMT_BEGIN \
  788. r = tor_addr_port_lookup(addr_port_str, &addr, &port); \
  789. tt_int_op(r, OP_EQ, 0); \
  790. tt_int_op(tor_addr_family(&addr), OP_EQ, expect_family); \
  791. sv = tor_addr_to_str(buf, &addr, sizeof(buf), fmt_decorated); \
  792. tt_str_op(sv, OP_EQ, buf); \
  793. tt_str_op(buf, OP_EQ, expect_str); \
  794. tt_int_op(port, OP_EQ, expect_port); \
  795. STMT_END
  796. /* Test that bad_str fails to parse as an IP address and port
  797. * using tor_addr_port_lookup(), and:
  798. * - the returned address is null,
  799. * - the returned port is 0.
  800. */
  801. #define TEST_ADDR_PORT_LOOKUP_XFAIL(bad_str) \
  802. STMT_BEGIN \
  803. r = tor_addr_port_lookup(bad_str, &addr, &port); \
  804. tt_int_op(r, OP_EQ, -1); \
  805. tt_assert(tor_addr_is_null(&addr)); \
  806. tt_int_op(port, OP_EQ, 0); \
  807. STMT_END
  808. /* Test that looking up host_port_str as an IP address using
  809. * tor_addr_port_lookup(), does something sensible:
  810. * - the result is -1 or 0.
  811. * - if the result is a failure, the returned address is null, and the
  812. * returned port is zero,
  813. * - if the result is a success, the returned port is expect_success_port,
  814. * and the returned family is AF_INET or AF_INET6.
  815. * We can't rely on the result of this function, because it depends on the
  816. * network.
  817. */
  818. #define TEST_HOST_PORT_LOOKUP(host_port_str, expect_success_port) \
  819. STMT_BEGIN \
  820. r = tor_addr_port_lookup(host_port_str, &addr, &port); \
  821. tt_int_op(r, OP_GE, -1); \
  822. tt_int_op(r, OP_LE, 0); \
  823. if (r == -1) { \
  824. tt_assert(tor_addr_is_null(&addr)); \
  825. tt_int_op(port, OP_EQ, 0); \
  826. } else { \
  827. tt_assert(tor_addr_family(&addr) == AF_INET || \
  828. tor_addr_family(&addr) == AF_INET6); \
  829. tt_int_op(port, OP_EQ, expect_success_port); \
  830. } \
  831. STMT_END
  832. /* Test that addr_str successfully parses as a canonical IPv4 address.
  833. * Check for successful parsing using:
  834. * - tor_addr_parse(),
  835. * - tor_addr_port_parse() with a default port,
  836. * - tor_lookup_hostname(),
  837. * - tor_addr_lookup() with AF_INET,
  838. * - tor_addr_lookup() with AF_UNSPEC,
  839. * - tor_addr_port_lookup(), with a zero port.
  840. * Check for failures using:
  841. * - tor_addr_port_parse() without a default port, because there is no port,
  842. * - tor_addr_lookup() with AF_INET6,
  843. * - tor_addr_port_lookup(), because there is no port.
  844. */
  845. #define TEST_ADDR_V4_PARSE_CANONICAL(addr_str) \
  846. STMT_BEGIN \
  847. TEST_ADDR_PARSE_FMT(addr_str, AF_INET, 0, addr_str); \
  848. TEST_ADDR_PORT_PARSE_FMT(addr_str, 111, AF_INET, 0, \
  849. addr_str, 111); \
  850. TEST_ADDR_V4_LOOKUP_HOSTNAME(addr_str, addr_str); \
  851. TEST_ADDR_PORT_LOOKUP_FMT(addr_str, AF_INET, 0, addr_str, 0); \
  852. TEST_ADDR_LOOKUP_FMT(addr_str, AF_INET, AF_INET, 0, addr_str); \
  853. TEST_ADDR_LOOKUP_FMT(addr_str, AF_UNSPEC, AF_INET, 0, addr_str); \
  854. TEST_ADDR_PORT_PARSE_XFAIL(addr_str, -1); \
  855. TEST_ADDR_LOOKUP_XFAIL(addr_str, AF_INET6); \
  856. STMT_END
  857. /* Test that addr_str successfully parses as a canonical fmt_decorated
  858. * IPv6 address.
  859. * Check for successful parsing using:
  860. * - tor_addr_parse(),
  861. * - tor_addr_port_parse() with a default port,
  862. * - tor_addr_lookup() with AF_INET6,
  863. * - tor_addr_lookup() with AF_UNSPEC,
  864. * - tor_addr_port_lookup(), with a zero port.
  865. * Check for failures using:
  866. * - tor_addr_port_parse() without a default port, because there is no port,
  867. * - tor_lookup_hostname(), because it only supports IPv4,
  868. * - tor_addr_lookup() with AF_INET.
  869. */
  870. #define TEST_ADDR_V6_PARSE_CANONICAL(addr_str, fmt_decorated) \
  871. STMT_BEGIN \
  872. TEST_ADDR_PARSE_FMT(addr_str, AF_INET6, fmt_decorated, addr_str); \
  873. TEST_ADDR_PORT_PARSE_FMT(addr_str, 222, AF_INET6, fmt_decorated, \
  874. addr_str, 222); \
  875. TEST_ADDR_LOOKUP_FMT(addr_str, AF_INET6, AF_INET6, fmt_decorated, \
  876. addr_str); \
  877. TEST_ADDR_LOOKUP_FMT(addr_str, AF_UNSPEC, AF_INET6, fmt_decorated, \
  878. addr_str); \
  879. TEST_ADDR_PORT_LOOKUP_FMT(addr_str, AF_INET6, fmt_decorated, addr_str, \
  880. 0); \
  881. TEST_ADDR_PORT_PARSE_XFAIL(addr_str, -1); \
  882. TEST_ADDR_V4_LOOKUP_XFAIL(addr_str); \
  883. TEST_ADDR_LOOKUP_XFAIL(addr_str, AF_INET); \
  884. STMT_END
  885. /* Test that addr_str successfully parses, and the fmt_decorated canonical
  886. * IPv6 string is expect_str.
  887. * Check for successful parsing using:
  888. * - tor_addr_parse(),
  889. * - tor_addr_port_parse() with a default port,
  890. * - tor_addr_lookup() with AF_INET6,
  891. * - tor_addr_lookup() with AF_UNSPEC,
  892. * - tor_addr_port_lookup(), with a zero port.
  893. * Check for failures using:
  894. * - tor_addr_port_parse() without a default port, because there is no port.
  895. * - tor_lookup_hostname(), because it only supports IPv4,
  896. * - tor_addr_lookup() with AF_INET.
  897. */
  898. #define TEST_ADDR_V6_PARSE(addr_str, fmt_decorated, expect_str) \
  899. STMT_BEGIN \
  900. TEST_ADDR_PARSE_FMT(addr_str, AF_INET6, fmt_decorated, expect_str); \
  901. TEST_ADDR_PORT_PARSE_FMT(addr_str, 333, AF_INET6, fmt_decorated, \
  902. expect_str, 333); \
  903. TEST_ADDR_LOOKUP_FMT(addr_str, AF_INET6, AF_INET6, fmt_decorated, \
  904. expect_str); \
  905. TEST_ADDR_LOOKUP_FMT(addr_str, AF_UNSPEC, AF_INET6, fmt_decorated, \
  906. expect_str); \
  907. TEST_ADDR_PORT_LOOKUP_FMT(addr_str, AF_INET6, fmt_decorated, expect_str, \
  908. 0); \
  909. TEST_ADDR_PORT_PARSE_XFAIL(addr_str, -1); \
  910. TEST_ADDR_V4_LOOKUP_XFAIL(addr_str); \
  911. TEST_ADDR_LOOKUP_XFAIL(addr_str, AF_INET); \
  912. STMT_END
  913. /* Test that addr_port_str successfully parses to the canonical IPv4 address
  914. * string expect_str, and port expect_port.
  915. * Check for successful parsing using:
  916. * - tor_addr_port_parse() without a default port,
  917. * - tor_addr_port_parse() with a default port,
  918. * - tor_addr_port_lookup().
  919. * Check for failures using:
  920. * - tor_addr_parse(), because there is a port,
  921. * - tor_lookup_hostname(), because there is a port.
  922. * - tor_addr_lookup(), regardless of the address family, because there is a
  923. * port.
  924. */
  925. #define TEST_ADDR_V4_PORT_PARSE(addr_port_str, expect_str, expect_port) \
  926. STMT_BEGIN \
  927. TEST_ADDR_PORT_PARSE_FMT(addr_port_str, -1, AF_INET, 0, expect_str, \
  928. expect_port); \
  929. TEST_ADDR_PORT_PARSE_FMT(addr_port_str, 444, AF_INET, 0, expect_str, \
  930. expect_port); \
  931. TEST_ADDR_PORT_LOOKUP_FMT(addr_port_str, AF_INET, 0, expect_str, \
  932. expect_port); \
  933. TEST_ADDR_PARSE_XFAIL(addr_port_str); \
  934. TEST_ADDR_V4_LOOKUP_XFAIL(addr_port_str); \
  935. TEST_ADDR_LOOKUP_XFAIL(addr_port_str, AF_INET); \
  936. TEST_ADDR_LOOKUP_XFAIL(addr_port_str, AF_UNSPEC); \
  937. TEST_ADDR_LOOKUP_XFAIL(addr_port_str, AF_INET6); \
  938. STMT_END
  939. /* Test that addr_port_str successfully parses to the canonical undecorated
  940. * IPv6 address string expect_str, and port expect_port.
  941. * Check for successful parsing using:
  942. * - tor_addr_port_parse() without a default port,
  943. * - tor_addr_port_parse() with a default port,
  944. * - tor_addr_port_lookup().
  945. * Check for failures using:
  946. * - tor_addr_parse(), because there is a port,
  947. * - tor_lookup_hostname(), because there is a port, and because it only
  948. * supports IPv4,
  949. * - tor_addr_lookup(), regardless of the address family, because there is a
  950. * port.
  951. */
  952. #define TEST_ADDR_V6_PORT_PARSE(addr_port_str, expect_str, expect_port) \
  953. STMT_BEGIN \
  954. TEST_ADDR_PORT_PARSE_FMT(addr_port_str, -1, AF_INET6, 0, expect_str, \
  955. expect_port); \
  956. TEST_ADDR_PORT_PARSE_FMT(addr_port_str, 555, AF_INET6, 0, expect_str, \
  957. expect_port); \
  958. TEST_ADDR_PORT_LOOKUP_FMT(addr_port_str, AF_INET6, 0, expect_str, \
  959. expect_port); \
  960. TEST_ADDR_PARSE_XFAIL(addr_port_str); \
  961. TEST_ADDR_V4_LOOKUP_XFAIL(addr_port_str); \
  962. TEST_ADDR_LOOKUP_XFAIL(addr_port_str, AF_INET6); \
  963. TEST_ADDR_LOOKUP_XFAIL(addr_port_str, AF_UNSPEC); \
  964. TEST_ADDR_LOOKUP_XFAIL(addr_port_str, AF_INET); \
  965. STMT_END
  966. /* Test that bad_str fails to parse due to a bad address or port.
  967. * Check for failures using:
  968. * - tor_addr_parse(),
  969. * - tor_addr_port_parse() without a default port,
  970. * - tor_addr_port_parse() with a default port,
  971. * - tor_lookup_hostname(),
  972. * - tor_addr_lookup(), regardless of the address family,
  973. * - tor_addr_port_lookup().
  974. */
  975. #define TEST_ADDR_PARSE_XFAIL_MALFORMED(bad_str) \
  976. STMT_BEGIN \
  977. TEST_ADDR_PARSE_XFAIL(bad_str); \
  978. TEST_ADDR_PORT_PARSE_XFAIL(bad_str, -1); \
  979. TEST_ADDR_PORT_PARSE_XFAIL(bad_str, 666); \
  980. TEST_ADDR_V4_LOOKUP_XFAIL(bad_str); \
  981. TEST_ADDR_LOOKUP_XFAIL(bad_str, AF_UNSPEC); \
  982. TEST_ADDR_LOOKUP_XFAIL(bad_str, AF_INET); \
  983. TEST_ADDR_LOOKUP_XFAIL(bad_str, AF_INET6); \
  984. TEST_ADDR_PORT_LOOKUP_XFAIL(bad_str); \
  985. STMT_END
  986. /* Test that host_str is treated as a hostname, and not an address.
  987. * Check for success or failure using the network-dependent functions:
  988. * - tor_lookup_hostname(),
  989. * - tor_addr_lookup(), regardless of the address family,
  990. * - tor_addr_port_lookup(), expecting a zero port.
  991. * Check for failures using:
  992. * - tor_addr_parse(),
  993. * - tor_addr_port_parse() without a default port,
  994. * - tor_addr_port_parse() with a default port.
  995. */
  996. #define TEST_HOSTNAME(host_str) \
  997. STMT_BEGIN \
  998. TEST_HOST_V4_LOOKUP(host_str); \
  999. TEST_HOST_LOOKUP(host_str, AF_UNSPEC); \
  1000. TEST_HOST_LOOKUP(host_str, AF_INET); \
  1001. TEST_HOST_LOOKUP(host_str, AF_INET6); \
  1002. TEST_HOST_PORT_LOOKUP(host_str, 0); \
  1003. TEST_ADDR_PARSE_XFAIL(host_str); \
  1004. TEST_ADDR_PORT_PARSE_XFAIL(host_str, -1); \
  1005. TEST_ADDR_PORT_PARSE_XFAIL(host_str, 777); \
  1006. STMT_END
  1007. /* Test that host_port_str is treated as a hostname and port, and not a
  1008. * hostname or an address.
  1009. * Check for success or failure using the network-dependent function:
  1010. * - tor_addr_port_lookup(), expecting expect_success_port if the lookup is
  1011. * successful.
  1012. * Check for failures using:
  1013. * - tor_addr_parse(),
  1014. * - tor_addr_port_parse() without a default port,
  1015. * - tor_addr_port_parse() with a default port,
  1016. * - tor_lookup_hostname(), because it doesn't support ports,
  1017. * - tor_addr_lookup(), regardless of the address family, because it doesn't
  1018. * support ports.
  1019. */
  1020. #define TEST_HOSTNAME_PORT(host_port_str, expect_success_port) \
  1021. STMT_BEGIN \
  1022. TEST_HOST_PORT_LOOKUP(host_port_str, expect_success_port); \
  1023. TEST_ADDR_PARSE_XFAIL(host_port_str); \
  1024. TEST_ADDR_PORT_PARSE_XFAIL(host_port_str, -1); \
  1025. TEST_ADDR_PORT_PARSE_XFAIL(host_port_str, 888); \
  1026. TEST_ADDR_V4_LOOKUP_XFAIL(host_port_str); \
  1027. TEST_ADDR_LOOKUP_XFAIL(host_port_str, AF_UNSPEC); \
  1028. TEST_ADDR_LOOKUP_XFAIL(host_port_str, AF_INET); \
  1029. TEST_ADDR_LOOKUP_XFAIL(host_port_str, AF_INET6); \
  1030. STMT_END
  1031. static void
  1032. test_addr_parse_canonical(void *arg)
  1033. {
  1034. int r;
  1035. tor_addr_t addr;
  1036. uint16_t port;
  1037. const char *sv;
  1038. uint32_t addr32h;
  1039. char buf[TOR_ADDR_BUF_LEN];
  1040. (void)arg;
  1041. /* Correct calls. */
  1042. TEST_ADDR_V4_PARSE_CANONICAL("192.0.2.1");
  1043. TEST_ADDR_V4_PARSE_CANONICAL("192.0.2.2");
  1044. TEST_ADDR_V6_PARSE_CANONICAL("[11:22::33:44]", 1);
  1045. TEST_ADDR_V6_PARSE_CANONICAL("[::1]", 1);
  1046. TEST_ADDR_V6_PARSE_CANONICAL("[::]", 1);
  1047. TEST_ADDR_V6_PARSE_CANONICAL("[2::]", 1);
  1048. TEST_ADDR_V6_PARSE_CANONICAL("[11:22:33:44:55:66:77:88]", 1);
  1049. /* Allow IPv6 without square brackets, when there is no port, but only if
  1050. * there is a default port */
  1051. TEST_ADDR_V6_PARSE_CANONICAL("11:22::33:44", 0);
  1052. TEST_ADDR_V6_PARSE_CANONICAL("::1", 0);
  1053. TEST_ADDR_V6_PARSE_CANONICAL("::", 0);
  1054. TEST_ADDR_V6_PARSE_CANONICAL("2::", 0);
  1055. TEST_ADDR_V6_PARSE_CANONICAL("11:22:33:44:55:66:77:88", 0);
  1056. done:
  1057. ;
  1058. }
  1059. /** Test tor_addr_parse() and tor_addr_port_parse(). */
  1060. static void
  1061. test_addr_parse(void *arg)
  1062. {
  1063. int r;
  1064. tor_addr_t addr;
  1065. uint16_t port;
  1066. const char *sv;
  1067. uint32_t addr32h;
  1068. char buf[TOR_ADDR_BUF_LEN];
  1069. (void)arg;
  1070. /* IPv6-mapped IPv4 addresses. Tor doesn't really use these. */
  1071. TEST_ADDR_V6_PARSE("11:22:33:44:55:66:1.2.3.4", 0,
  1072. "11:22:33:44:55:66:102:304");
  1073. TEST_ADDR_V6_PARSE("11:22::33:44:1.2.3.4", 0,
  1074. "11:22::33:44:102:304");
  1075. /* Ports. */
  1076. TEST_ADDR_V4_PORT_PARSE("192.0.2.1:1234", "192.0.2.1", 1234);
  1077. TEST_ADDR_V6_PORT_PARSE("[::1]:1234", "::1", 1234);
  1078. /* Host names. */
  1079. TEST_HOSTNAME("localhost");
  1080. TEST_HOSTNAME_PORT("localhost:1234", 1234);
  1081. TEST_HOSTNAME_PORT("localhost:0", 0);
  1082. TEST_HOSTNAME("torproject.org");
  1083. TEST_HOSTNAME_PORT("torproject.org:56", 56);
  1084. TEST_HOSTNAME("probably-not-a-valid-dns.name-tld");
  1085. TEST_HOSTNAME_PORT("probably-not-a-valid-dns.name-tld:789", 789);
  1086. /* Malformed addresses. */
  1087. /* Empty string. */
  1088. TEST_ADDR_PARSE_XFAIL_MALFORMED("");
  1089. /* Square brackets around IPv4 address. */
  1090. TEST_ADDR_PARSE_XFAIL_MALFORMED("[192.0.2.1]");
  1091. TEST_ADDR_PARSE_XFAIL_MALFORMED("[192.0.2.3]:12345");
  1092. /* Only left square bracket. */
  1093. TEST_ADDR_PARSE_XFAIL_MALFORMED("[11:22::33:44");
  1094. /* Only right square bracket. */
  1095. TEST_ADDR_PARSE_XFAIL_MALFORMED("11:22::33:44]");
  1096. /* Leading colon. */
  1097. TEST_ADDR_PARSE_XFAIL_MALFORMED(":11:22::33:44");
  1098. /* Trailing colon. */
  1099. TEST_ADDR_PARSE_XFAIL_MALFORMED("11:22::33:44:");
  1100. TEST_ADDR_PARSE_XFAIL_MALFORMED("[::1]:");
  1101. TEST_ADDR_PARSE_XFAIL_MALFORMED("localhost:");
  1102. /* Bad port. */
  1103. TEST_ADDR_PARSE_XFAIL_MALFORMED("192.0.2.2:66666");
  1104. TEST_ADDR_PARSE_XFAIL_MALFORMED("[::1]:77777");
  1105. TEST_ADDR_PARSE_XFAIL_MALFORMED("::1:88888");
  1106. TEST_ADDR_PARSE_XFAIL_MALFORMED("localhost:99999");
  1107. TEST_ADDR_PARSE_XFAIL_MALFORMED("192.0.2.2:-1");
  1108. TEST_ADDR_PARSE_XFAIL_MALFORMED("[::1]:-2");
  1109. TEST_ADDR_PARSE_XFAIL_MALFORMED("::1:-3");
  1110. TEST_ADDR_PARSE_XFAIL_MALFORMED("localhost:-4");
  1111. TEST_ADDR_PARSE_XFAIL_MALFORMED("192.0.2.2:1 bad");
  1112. TEST_ADDR_PARSE_XFAIL_MALFORMED("192.0.2.2:bad-port");
  1113. TEST_ADDR_PARSE_XFAIL_MALFORMED("[::1]:bad-port-1");
  1114. TEST_ADDR_PARSE_XFAIL_MALFORMED("::1:1-bad-port");
  1115. TEST_ADDR_PARSE_XFAIL_MALFORMED("localhost:1-bad-port");
  1116. TEST_ADDR_PARSE_XFAIL_MALFORMED("localhost:1-bad-port-1");
  1117. /* Bad hostname */
  1118. TEST_ADDR_PARSE_XFAIL_MALFORMED("definitely invalid");
  1119. TEST_ADDR_PARSE_XFAIL_MALFORMED("definitely invalid:22222");
  1120. /* Ambiguous cases */
  1121. /* Too many hex words in IPv4-mapped IPv6 address.
  1122. * But some OS host lookup routines accept it as a hostname, or
  1123. * as an IP address?? (I assume they discard unused characters). */
  1124. TEST_HOSTNAME("11:22:33:44:55:66:77:88:1.2.3.4");
  1125. /* IPv6 address with port and no brackets
  1126. * We reject it, but some OS host lookup routines accept it as an
  1127. * IPv6 address:port ? */
  1128. TEST_HOSTNAME_PORT("11:22::33:44:12345", 12345);
  1129. /* Is it a port, or are there too many hex words?
  1130. * We reject it either way, but some OS host lookup routines accept it as an
  1131. * IPv6 address:port */
  1132. TEST_HOSTNAME_PORT("11:22:33:44:55:66:77:88:99", 99);
  1133. /* But we accept it if it has square brackets. */
  1134. TEST_ADDR_V6_PORT_PARSE("[11:22:33:44:55:66:77:88]:99",
  1135. "11:22:33:44:55:66:77:88",99);
  1136. /* Bad IPv4 address
  1137. * We reject it, but some OS host lookup routines accept it as an
  1138. * IPv4 address[:port], with a zero last octet */
  1139. TEST_HOSTNAME("192.0.1");
  1140. TEST_HOSTNAME_PORT("192.0.2:1234", 1234);
  1141. /* More bad IPv6 addresses and ports: no brackets
  1142. * We reject it, but some OS host lookup routines accept it as an
  1143. * IPv6 address[:port] */
  1144. TEST_HOSTNAME_PORT("::1:12345", 12345);
  1145. TEST_HOSTNAME_PORT("11:22::33:44:12345", 12345);
  1146. /* And this is an ambiguous case, which is interpreted as an IPv6 address. */
  1147. TEST_ADDR_V6_PARSE_CANONICAL("11:22::88:99", 0);
  1148. /* Use square brackets to resolve the ambiguity */
  1149. TEST_ADDR_V6_PARSE_CANONICAL("[11:22::88:99]", 1);
  1150. TEST_ADDR_V6_PORT_PARSE("[11:22::88]:99",
  1151. "11:22::88",99);
  1152. done:
  1153. ;
  1154. }
  1155. static void
  1156. update_difference(int ipv6, uint8_t *d,
  1157. const tor_addr_t *a, const tor_addr_t *b)
  1158. {
  1159. const int n_bytes = ipv6 ? 16 : 4;
  1160. uint8_t a_tmp[4], b_tmp[4];
  1161. const uint8_t *ba, *bb;
  1162. int i;
  1163. if (ipv6) {
  1164. ba = tor_addr_to_in6_addr8(a);
  1165. bb = tor_addr_to_in6_addr8(b);
  1166. } else {
  1167. set_uint32(a_tmp, tor_addr_to_ipv4n(a));
  1168. set_uint32(b_tmp, tor_addr_to_ipv4n(b));
  1169. ba = a_tmp; bb = b_tmp;
  1170. }
  1171. for (i = 0; i < n_bytes; ++i) {
  1172. d[i] |= ba[i] ^ bb[i];
  1173. }
  1174. }
  1175. static void
  1176. test_virtaddrmap(void *data)
  1177. {
  1178. /* Let's start with a bunch of random addresses. */
  1179. int ipv6, bits, iter, b;
  1180. virtual_addr_conf_t cfg[2];
  1181. uint8_t bytes[16];
  1182. (void)data;
  1183. tor_addr_parse(&cfg[0].addr, "64.65.0.0");
  1184. tor_addr_parse(&cfg[1].addr, "3491:c0c0::");
  1185. for (ipv6 = 0; ipv6 <= 1; ++ipv6) {
  1186. for (bits = 0; bits < 18; ++bits) {
  1187. tor_addr_t last_a;
  1188. cfg[ipv6].bits = bits;
  1189. memset(bytes, 0, sizeof(bytes));
  1190. tor_addr_copy(&last_a, &cfg[ipv6].addr);
  1191. /* Generate 128 addresses with each addr/bits combination. */
  1192. for (iter = 0; iter < 128; ++iter) {
  1193. tor_addr_t a;
  1194. get_random_virtual_addr(&cfg[ipv6], &a);
  1195. //printf("%s\n", fmt_addr(&a));
  1196. /* Make sure that the first b bits match the configured network */
  1197. tt_int_op(0, OP_EQ, tor_addr_compare_masked(&a, &cfg[ipv6].addr,
  1198. bits, CMP_EXACT));
  1199. /* And track which bits have been different between pairs of
  1200. * addresses */
  1201. update_difference(ipv6, bytes, &last_a, &a);
  1202. }
  1203. /* Now make sure all but the first 'bits' bits of bytes are true */
  1204. for (b = bits+1; b < (ipv6?128:32); ++b) {
  1205. tt_assert(1 & (bytes[b/8] >> (7-(b&7))));
  1206. }
  1207. }
  1208. }
  1209. done:
  1210. ;
  1211. }
  1212. static void
  1213. test_virtaddrmap_persist(void *data)
  1214. {
  1215. (void)data;
  1216. const char *a, *b, *c;
  1217. tor_addr_t addr;
  1218. char *ones = NULL;
  1219. const char *canned_data;
  1220. size_t canned_data_len;
  1221. addressmap_init();
  1222. // Try a hostname.
  1223. a = addressmap_register_virtual_address(RESOLVED_TYPE_HOSTNAME,
  1224. tor_strdup("foobar.baz"));
  1225. tt_assert(a);
  1226. tt_assert(!strcmpend(a, ".virtual"));
  1227. // mock crypto_rand to repeat the same result twice; make sure we get
  1228. // different outcomes. (Because even though the odds for receiving the
  1229. // same 80-bit address twice is only 1/2^40, it could still happen for
  1230. // some user -- but running our test through 2^40 iterations isn't
  1231. // reasonable.)
  1232. canned_data = "1234567890" // the first call returns this.
  1233. "1234567890" // the second call returns this.
  1234. "abcdefghij"; // the third call returns this.
  1235. canned_data_len = 30;
  1236. testing_enable_prefilled_rng(canned_data, canned_data_len);
  1237. a = addressmap_register_virtual_address(RESOLVED_TYPE_HOSTNAME,
  1238. tor_strdup("quuxit.baz"));
  1239. b = addressmap_register_virtual_address(RESOLVED_TYPE_HOSTNAME,
  1240. tor_strdup("nescio.baz"));
  1241. tt_assert(a);
  1242. tt_assert(b);
  1243. tt_str_op(a, OP_EQ, "gezdgnbvgy3tqojq.virtual");
  1244. tt_str_op(b, OP_EQ, "mfrggzdfmztwq2lk.virtual");
  1245. testing_disable_prefilled_rng();
  1246. // Now try something to get us an ipv4 address
  1247. tt_int_op(0,OP_EQ, parse_virtual_addr_network("192.168.0.0/16",
  1248. AF_INET, 0, NULL));
  1249. a = addressmap_register_virtual_address(RESOLVED_TYPE_IPV4,
  1250. tor_strdup("foobar.baz"));
  1251. tt_assert(a);
  1252. tt_assert(!strcmpstart(a, "192.168."));
  1253. tor_addr_parse(&addr, a);
  1254. tt_int_op(AF_INET, OP_EQ, tor_addr_family(&addr));
  1255. b = addressmap_register_virtual_address(RESOLVED_TYPE_IPV4,
  1256. tor_strdup("quuxit.baz"));
  1257. tt_str_op(b, OP_NE, a);
  1258. tt_assert(!strcmpstart(b, "192.168."));
  1259. // Try some canned entropy and verify all the we discard duplicates,
  1260. // addresses that end with 0, and addresses that end with 255.
  1261. canned_data = "\x01\x02\x03\x04" // okay
  1262. "\x01\x02\x03\x04" // duplicate
  1263. "\x03\x04\x00\x00" // bad ending 1
  1264. "\x05\x05\x00\xff" // bad ending 2
  1265. "\x05\x06\x07\xf0"; // okay
  1266. canned_data_len = 20;
  1267. testing_enable_prefilled_rng(canned_data, canned_data_len);
  1268. a = addressmap_register_virtual_address(RESOLVED_TYPE_IPV4,
  1269. tor_strdup("wumble.onion"));
  1270. b = addressmap_register_virtual_address(RESOLVED_TYPE_IPV4,
  1271. tor_strdup("wumpus.onion"));
  1272. tt_str_op(a, OP_EQ, "192.168.3.4");
  1273. tt_str_op(b, OP_EQ, "192.168.7.240");
  1274. testing_disable_prefilled_rng();
  1275. // Now try IPv6!
  1276. tt_int_op(0,OP_EQ, parse_virtual_addr_network("1010:F000::/20",
  1277. AF_INET6, 0, NULL));
  1278. a = addressmap_register_virtual_address(RESOLVED_TYPE_IPV6,
  1279. tor_strdup("foobar.baz"));
  1280. tt_assert(a);
  1281. tt_assert(!strcmpstart(a, "[1010:f"));
  1282. tor_addr_parse(&addr, a);
  1283. tt_int_op(AF_INET6, OP_EQ, tor_addr_family(&addr));
  1284. b = addressmap_register_virtual_address(RESOLVED_TYPE_IPV6,
  1285. tor_strdup("quuxit.baz"));
  1286. tt_str_op(b, OP_NE, a);
  1287. tt_assert(!strcmpstart(b, "[1010:f"));
  1288. // Try IPv6 with canned entropy, to make sure we detect duplicates.
  1289. canned_data = "acanthopterygian" // okay
  1290. "cinematographist" // okay
  1291. "acanthopterygian" // duplicate
  1292. "acanthopterygian" // duplicate
  1293. "acanthopterygian" // duplicate
  1294. "cinematographist" // duplicate
  1295. "coadministration"; // okay
  1296. canned_data_len = 16 * 7;
  1297. testing_enable_prefilled_rng(canned_data, canned_data_len);
  1298. a = addressmap_register_virtual_address(RESOLVED_TYPE_IPV6,
  1299. tor_strdup("wuffle.baz"));
  1300. b = addressmap_register_virtual_address(RESOLVED_TYPE_IPV6,
  1301. tor_strdup("gribble.baz"));
  1302. c = addressmap_register_virtual_address(RESOLVED_TYPE_IPV6,
  1303. tor_strdup("surprisingly-legible.baz"));
  1304. tt_str_op(a, OP_EQ, "[1010:f16e:7468:6f70:7465:7279:6769:616e]");
  1305. tt_str_op(b, OP_EQ, "[1010:fe65:6d61:746f:6772:6170:6869:7374]");
  1306. tt_str_op(c, OP_EQ, "[1010:f164:6d69:6e69:7374:7261:7469:6f6e]");
  1307. // Try address exhaustion: make sure we can actually fail if we
  1308. // get too many already-existing addresses.
  1309. testing_disable_prefilled_rng();
  1310. canned_data_len = 128*1024;
  1311. canned_data = ones = tor_malloc(canned_data_len);
  1312. memset(ones, 1, canned_data_len);
  1313. testing_enable_prefilled_rng(canned_data, canned_data_len);
  1314. // There is some chance this one will fail if a previous random
  1315. // allocation gave out the address already.
  1316. a = addressmap_register_virtual_address(RESOLVED_TYPE_IPV4,
  1317. tor_strdup("might-work.onion"));
  1318. if (a) {
  1319. tt_str_op(a, OP_EQ, "192.168.1.1");
  1320. }
  1321. setup_capture_of_logs(LOG_WARN);
  1322. // This one will definitely fail, since we've set up the RNG to hand
  1323. // out "1" forever.
  1324. b = addressmap_register_virtual_address(RESOLVED_TYPE_IPV4,
  1325. tor_strdup("wont-work.onion"));
  1326. tt_assert(b == NULL);
  1327. expect_single_log_msg_containing("Ran out of virtual addresses!");
  1328. done:
  1329. testing_disable_prefilled_rng();
  1330. tor_free(ones);
  1331. addressmap_free_all();
  1332. teardown_capture_of_logs();
  1333. }
  1334. static void
  1335. test_addr_localname(void *arg)
  1336. {
  1337. (void)arg;
  1338. tt_assert(tor_addr_hostname_is_local("localhost"));
  1339. tt_assert(tor_addr_hostname_is_local("LOCALHOST"));
  1340. tt_assert(tor_addr_hostname_is_local("LocalHost"));
  1341. tt_assert(tor_addr_hostname_is_local("local"));
  1342. tt_assert(tor_addr_hostname_is_local("LOCAL"));
  1343. tt_assert(tor_addr_hostname_is_local("here.now.local"));
  1344. tt_assert(tor_addr_hostname_is_local("here.now.LOCAL"));
  1345. tt_assert(!tor_addr_hostname_is_local(" localhost"));
  1346. tt_assert(!tor_addr_hostname_is_local("www.torproject.org"));
  1347. done:
  1348. ;
  1349. }
  1350. static void
  1351. test_addr_dup_ip(void *arg)
  1352. {
  1353. char *v = NULL;
  1354. (void)arg;
  1355. #define CHECK(ip, s) do { \
  1356. v = tor_dup_ip(ip); \
  1357. tt_str_op(v,OP_EQ,(s)); \
  1358. tor_free(v); \
  1359. } while (0)
  1360. CHECK(0xffffffff, "255.255.255.255");
  1361. CHECK(0x00000000, "0.0.0.0");
  1362. CHECK(0x7f000001, "127.0.0.1");
  1363. CHECK(0x01020304, "1.2.3.4");
  1364. #undef CHECK
  1365. done:
  1366. tor_free(v);
  1367. }
  1368. static void
  1369. test_addr_sockaddr_to_str(void *arg)
  1370. {
  1371. char *v = NULL;
  1372. struct sockaddr_in sin;
  1373. struct sockaddr_in6 sin6;
  1374. struct sockaddr_storage ss;
  1375. #ifdef HAVE_SYS_UN_H
  1376. struct sockaddr_un s_un;
  1377. #endif
  1378. #define CHECK(sa, s) do { \
  1379. v = tor_sockaddr_to_str((const struct sockaddr*) &(sa)); \
  1380. tt_str_op(v,OP_EQ,(s)); \
  1381. tor_free(v); \
  1382. } while (0)
  1383. (void)arg;
  1384. memset(&ss,0,sizeof(ss));
  1385. ss.ss_family = AF_UNSPEC;
  1386. CHECK(ss, "unspec");
  1387. memset(&sin,0,sizeof(sin));
  1388. sin.sin_family = AF_INET;
  1389. sin.sin_addr.s_addr = htonl(0x7f808001);
  1390. sin.sin_port = htons(1234);
  1391. CHECK(sin, "127.128.128.1:1234");
  1392. #ifdef HAVE_SYS_UN_H
  1393. memset(&s_un,0,sizeof(s_un));
  1394. s_un.sun_family = AF_UNIX;
  1395. strlcpy(s_un.sun_path, "/here/is/a/path", sizeof(s_un.sun_path));
  1396. CHECK(s_un, "unix:/here/is/a/path");
  1397. #endif /* defined(HAVE_SYS_UN_H) */
  1398. memset(&sin6,0,sizeof(sin6));
  1399. sin6.sin6_family = AF_INET6;
  1400. memcpy(sin6.sin6_addr.s6_addr, "\x20\x00\x00\x00\x00\x00\x00\x00"
  1401. "\x00\x1a\x2b\x3c\x4d\x5e\x00\x01", 16);
  1402. sin6.sin6_port = htons(1234);
  1403. CHECK(sin6, "[2000::1a:2b3c:4d5e:1]:1234");
  1404. done:
  1405. tor_free(v);
  1406. }
  1407. static void
  1408. test_addr_is_loopback(void *data)
  1409. {
  1410. static const struct loopback_item {
  1411. const char *name;
  1412. int is_loopback;
  1413. } loopback_items[] = {
  1414. { "::1", 1 },
  1415. { "127.0.0.1", 1 },
  1416. { "127.99.100.101", 1 },
  1417. { "128.99.100.101", 0 },
  1418. { "8.8.8.8", 0 },
  1419. { "0.0.0.0", 0 },
  1420. { "::2", 0 },
  1421. { "::", 0 },
  1422. { "::1.0.0.0", 0 },
  1423. { NULL, 0 }
  1424. };
  1425. int i;
  1426. tor_addr_t addr;
  1427. (void)data;
  1428. for (i=0; loopback_items[i].name; ++i) {
  1429. tt_int_op(tor_addr_parse(&addr, loopback_items[i].name), OP_GE, 0);
  1430. tt_int_op(tor_addr_is_loopback(&addr), OP_EQ,
  1431. loopback_items[i].is_loopback);
  1432. }
  1433. tor_addr_make_unspec(&addr);
  1434. tt_int_op(tor_addr_is_loopback(&addr), OP_EQ, 0);
  1435. done:
  1436. ;
  1437. }
  1438. static void
  1439. test_addr_make_null(void *data)
  1440. {
  1441. tor_addr_t *addr = tor_malloc(sizeof(*addr));
  1442. tor_addr_t *zeros = tor_malloc_zero(sizeof(*addr));
  1443. char buf[TOR_ADDR_BUF_LEN];
  1444. (void) data;
  1445. /* Ensure that before tor_addr_make_null, addr != 0's */
  1446. memset(addr, 1, sizeof(*addr));
  1447. tt_int_op(fast_memcmp(addr, zeros, sizeof(*addr)), OP_NE, 0);
  1448. /* Test with AF == AF_INET */
  1449. zeros->family = AF_INET;
  1450. tor_addr_make_null(addr, AF_INET);
  1451. tt_int_op(fast_memcmp(addr, zeros, sizeof(*addr)), OP_EQ, 0);
  1452. tt_str_op(tor_addr_to_str(buf, addr, sizeof(buf), 0), OP_EQ, "0.0.0.0");
  1453. /* Test with AF == AF_INET6 */
  1454. memset(addr, 1, sizeof(*addr));
  1455. zeros->family = AF_INET6;
  1456. tor_addr_make_null(addr, AF_INET6);
  1457. tt_int_op(fast_memcmp(addr, zeros, sizeof(*addr)), OP_EQ, 0);
  1458. tt_str_op(tor_addr_to_str(buf, addr, sizeof(buf), 0), OP_EQ, "::");
  1459. done:
  1460. tor_free(addr);
  1461. tor_free(zeros);
  1462. }
  1463. #define TEST_ADDR_INTERNAL(a, for_listening, rv) STMT_BEGIN \
  1464. tor_addr_t t; \
  1465. tt_int_op(tor_inet_pton(AF_INET, a, &t.addr.in_addr), OP_EQ, 1); \
  1466. t.family = AF_INET; \
  1467. tt_int_op(tor_addr_is_internal(&t, for_listening), OP_EQ, rv); \
  1468. STMT_END;
  1469. static void
  1470. test_addr_rfc6598(void *arg)
  1471. {
  1472. (void)arg;
  1473. TEST_ADDR_INTERNAL("100.64.0.1", 0, 1);
  1474. TEST_ADDR_INTERNAL("100.64.0.1", 1, 0);
  1475. done:
  1476. ;
  1477. }
  1478. #define ADDR_LEGACY(name) \
  1479. { #name, test_addr_ ## name , 0, NULL, NULL }
  1480. struct testcase_t addr_tests[] = {
  1481. ADDR_LEGACY(basic),
  1482. ADDR_LEGACY(ip6_helpers),
  1483. ADDR_LEGACY(parse),
  1484. ADDR_LEGACY(parse_canonical),
  1485. { "virtaddr", test_virtaddrmap, 0, NULL, NULL },
  1486. { "virtaddr_persist", test_virtaddrmap_persist, TT_FORK, NULL, NULL },
  1487. { "localname", test_addr_localname, 0, NULL, NULL },
  1488. { "dup_ip", test_addr_dup_ip, 0, NULL, NULL },
  1489. { "sockaddr_to_str", test_addr_sockaddr_to_str, 0, NULL, NULL },
  1490. { "is_loopback", test_addr_is_loopback, 0, NULL, NULL },
  1491. { "make_null", test_addr_make_null, 0, NULL, NULL },
  1492. { "rfc6598", test_addr_rfc6598, 0, NULL, NULL },
  1493. END_OF_TESTCASES
  1494. };