Back to index

tor  0.2.3.18-rc
test_addr.c
Go to the documentation of this file.
00001 /* Copyright (c) 2001-2004, Roger Dingledine.
00002  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
00003  * Copyright (c) 2007-2012, The Tor Project, Inc. */
00004 /* See LICENSE for licensing information */
00005 
00006 #include "orconfig.h"
00007 #include "or.h"
00008 #include "test.h"
00009 
00010 static void
00011 test_addr_basic(void)
00012 {
00013   uint32_t u32;
00014   uint16_t u16;
00015   char *cp;
00016 
00017   /* Test addr_port_lookup */
00018   cp = NULL; u32 = 3; u16 = 3;
00019   test_assert(!addr_port_lookup(LOG_WARN, "1.2.3.4", &cp, &u32, &u16));
00020   test_streq(cp, "1.2.3.4");
00021   test_eq(u32, 0x01020304u);
00022   test_eq(u16, 0);
00023   tor_free(cp);
00024   test_assert(!addr_port_lookup(LOG_WARN, "4.3.2.1:99", &cp, &u32, &u16));
00025   test_streq(cp, "4.3.2.1");
00026   test_eq(u32, 0x04030201u);
00027   test_eq(u16, 99);
00028   tor_free(cp);
00029   test_assert(!addr_port_lookup(LOG_WARN, "nonexistent.address:4040",
00030                                &cp, NULL, &u16));
00031   test_streq(cp, "nonexistent.address");
00032   test_eq(u16, 4040);
00033   tor_free(cp);
00034   test_assert(!addr_port_lookup(LOG_WARN, "localhost:9999", &cp, &u32, &u16));
00035   test_streq(cp, "localhost");
00036   test_eq(u32, 0x7f000001u);
00037   test_eq(u16, 9999);
00038   tor_free(cp);
00039   u32 = 3;
00040   test_assert(!addr_port_lookup(LOG_WARN, "localhost", NULL, &u32, &u16));
00041   test_eq(cp, NULL);
00042   test_eq(u32, 0x7f000001u);
00043   test_eq(u16, 0);
00044   tor_free(cp);
00045   test_eq(0, addr_mask_get_bits(0x0u));
00046   test_eq(32, addr_mask_get_bits(0xFFFFFFFFu));
00047   test_eq(16, addr_mask_get_bits(0xFFFF0000u));
00048   test_eq(31, addr_mask_get_bits(0xFFFFFFFEu));
00049   test_eq(1, addr_mask_get_bits(0x80000000u));
00050 
00051   /* Test inet_ntop */
00052   {
00053     char tmpbuf[TOR_ADDR_BUF_LEN];
00054     const char *ip = "176.192.208.224";
00055     struct in_addr in;
00056 
00057     /* good round trip */
00058     test_eq(tor_inet_pton(AF_INET, ip, &in), 1);
00059     test_eq_ptr(tor_inet_ntop(AF_INET, &in, tmpbuf, sizeof(tmpbuf)), &tmpbuf);
00060     test_streq(tmpbuf, ip);
00061 
00062     /* just enough buffer length */
00063     test_streq(tor_inet_ntop(AF_INET, &in, tmpbuf, strlen(ip) + 1), ip);
00064 
00065     /* too short buffer */
00066     test_eq_ptr(tor_inet_ntop(AF_INET, &in, tmpbuf, strlen(ip)), NULL);
00067   }
00068 
00069  done:
00070   ;
00071 }
00072 
00073 #define _test_op_ip6(a,op,b,e1,e2)                               \
00074   STMT_BEGIN                                                     \
00075   tt_assert_test_fmt_type(a,b,e1" "#op" "e2,struct in6_addr*,    \
00076     (memcmp(val1_->s6_addr, val2_->s6_addr, 16) op 0),           \
00077     char *, "%s",                                                \
00078     { int i; char *cp;                                           \
00079       cp = print_ = tor_malloc(64);                              \
00080       for (i=0;i<16;++i) {                                       \
00081         tor_snprintf(cp, 3,"%02x", (unsigned)value_->s6_addr[i]);\
00082         cp += 2;                                                 \
00083         if (i != 15) *cp++ = ':';                                \
00084       }                                                          \
00085     },                                                           \
00086     { tor_free(print_); },                                       \
00087     TT_EXIT_TEST_FUNCTION                                        \
00088   );                                                             \
00089   STMT_END
00090 
00093 #define test_pton6_same(a,b) STMT_BEGIN                \
00094      test_eq(tor_inet_pton(AF_INET6, a, &a1), 1);      \
00095      test_eq(tor_inet_pton(AF_INET6, b, &a2), 1);      \
00096      _test_op_ip6(&a1,==,&a2,#a,#b);                   \
00097   STMT_END
00098 
00101 #define test_pton6_bad(a)                       \
00102   test_eq(0, tor_inet_pton(AF_INET6, a, &a1))
00103 
00107 #define test_ntop6_reduces(a,b) STMT_BEGIN                              \
00108     test_eq(tor_inet_pton(AF_INET6, a, &a1), 1);                        \
00109     test_streq(tor_inet_ntop(AF_INET6, &a1, buf, sizeof(buf)), b);      \
00110     test_eq(tor_inet_pton(AF_INET6, b, &a2), 1);                        \
00111     _test_op_ip6(&a1, ==, &a2, a, b);                                   \
00112   STMT_END
00113 
00116 #define test_internal_ip(a,for_listening) STMT_BEGIN           \
00117     test_eq(tor_inet_pton(AF_INET6, a, &t1.addr.in6_addr), 1); \
00118     t1.family = AF_INET6;                                      \
00119     if (!tor_addr_is_internal(&t1, for_listening))             \
00120       test_fail_msg( a "was not internal.");                   \
00121   STMT_END
00122 
00125 #define test_external_ip(a,for_listening) STMT_BEGIN           \
00126     test_eq(tor_inet_pton(AF_INET6, a, &t1.addr.in6_addr), 1); \
00127     t1.family = AF_INET6;                                      \
00128     if (tor_addr_is_internal(&t1, for_listening))              \
00129       test_fail_msg(a  "was not external.");                   \
00130   STMT_END
00131 
00135 #define test_addr_compare(a, op, b) STMT_BEGIN                    \
00136     test_eq(tor_inet_pton(AF_INET6, a, &t1.addr.in6_addr), 1);    \
00137     test_eq(tor_inet_pton(AF_INET6, b, &t2.addr.in6_addr), 1);    \
00138     t1.family = t2.family = AF_INET6;                             \
00139     r = tor_addr_compare(&t1,&t2,CMP_SEMANTIC);                   \
00140     if (!(r op 0))                                                \
00141       test_fail_msg("failed: tor_addr_compare("a","b") "#op" 0"); \
00142   STMT_END
00143 
00147 #define test_addr_compare_masked(a, op, b, m) STMT_BEGIN          \
00148     test_eq(tor_inet_pton(AF_INET6, a, &t1.addr.in6_addr), 1);    \
00149     test_eq(tor_inet_pton(AF_INET6, b, &t2.addr.in6_addr), 1);    \
00150     t1.family = t2.family = AF_INET6;                             \
00151     r = tor_addr_compare_masked(&t1,&t2,m,CMP_SEMANTIC);          \
00152     if (!(r op 0))                                                \
00153       test_fail_msg("failed: tor_addr_compare_masked("a","b","#m") "#op" 0"); \
00154   STMT_END
00155 
00160 #define test_addr_mask_ports_parse(xx, f, ip1, ip2, ip3, ip4, mm, pt1, pt2) \
00161   STMT_BEGIN                                                                \
00162     test_eq(tor_addr_parse_mask_ports(xx, &t1, &mask, &port1, &port2), f);  \
00163     p1=tor_inet_ntop(AF_INET6, &t1.addr.in6_addr, bug, sizeof(bug));        \
00164     test_eq(htonl(ip1), tor_addr_to_in6_addr32(&t1)[0]);            \
00165     test_eq(htonl(ip2), tor_addr_to_in6_addr32(&t1)[1]);            \
00166     test_eq(htonl(ip3), tor_addr_to_in6_addr32(&t1)[2]);            \
00167     test_eq(htonl(ip4), tor_addr_to_in6_addr32(&t1)[3]);            \
00168     test_eq(mask, mm);                                     \
00169     test_eq(port1, pt1);                                   \
00170     test_eq(port2, pt2);                                   \
00171   STMT_END
00172 
00174 static void
00175 test_addr_ip6_helpers(void)
00176 {
00177   char buf[TOR_ADDR_BUF_LEN], bug[TOR_ADDR_BUF_LEN];
00178   char rbuf[REVERSE_LOOKUP_NAME_BUF_LEN];
00179   struct in6_addr a1, a2;
00180   tor_addr_t t1, t2;
00181   int r, i;
00182   uint16_t port1, port2;
00183   maskbits_t mask;
00184   const char *p1;
00185   struct sockaddr_storage sa_storage;
00186   struct sockaddr_in *sin;
00187   struct sockaddr_in6 *sin6;
00188 
00189   /* Test tor_inet_ntop and tor_inet_pton: IPv6 */
00190   {
00191     const char *ip = "2001::1234";
00192     const char *ip_ffff = "::ffff:192.168.1.2";
00193 
00194     /* good round trip */
00195     test_eq(tor_inet_pton(AF_INET6, ip, &a1), 1);
00196     test_eq_ptr(tor_inet_ntop(AF_INET6, &a1, buf, sizeof(buf)), &buf);
00197     test_streq(buf, ip);
00198 
00199     /* good round trip - ::ffff:0:0 style */
00200     test_eq(tor_inet_pton(AF_INET6, ip_ffff, &a2), 1);
00201     test_eq_ptr(tor_inet_ntop(AF_INET6, &a2, buf, sizeof(buf)), &buf);
00202     test_streq(buf, ip_ffff);
00203 
00204     /* just long enough buffer (remember \0) */
00205     test_streq(tor_inet_ntop(AF_INET6, &a1, buf, strlen(ip)+1), ip);
00206     test_streq(tor_inet_ntop(AF_INET6, &a2, buf, strlen(ip_ffff)+1),
00207                ip_ffff);
00208 
00209     /* too short buffer (remember \0) */
00210     test_eq_ptr(tor_inet_ntop(AF_INET6, &a1, buf, strlen(ip)), NULL);
00211     test_eq_ptr(tor_inet_ntop(AF_INET6, &a2, buf, strlen(ip_ffff)), NULL);
00212   }
00213 
00214   /* ==== Converting to and from sockaddr_t. */
00215   sin = (struct sockaddr_in *)&sa_storage;
00216   sin->sin_family = AF_INET;
00217   sin->sin_port = 9090;
00218   sin->sin_addr.s_addr = htonl(0x7f7f0102); /*127.127.1.2*/
00219   tor_addr_from_sockaddr(&t1, (struct sockaddr *)sin, NULL);
00220   test_eq(tor_addr_family(&t1), AF_INET);
00221   test_eq(tor_addr_to_ipv4h(&t1), 0x7f7f0102);
00222 
00223   memset(&sa_storage, 0, sizeof(sa_storage));
00224   test_eq(sizeof(struct sockaddr_in),
00225           tor_addr_to_sockaddr(&t1, 1234, (struct sockaddr *)&sa_storage,
00226                                sizeof(sa_storage)));
00227   test_eq(1234, ntohs(sin->sin_port));
00228   test_eq(0x7f7f0102, ntohl(sin->sin_addr.s_addr));
00229 
00230   memset(&sa_storage, 0, sizeof(sa_storage));
00231   sin6 = (struct sockaddr_in6 *)&sa_storage;
00232   sin6->sin6_family = AF_INET6;
00233   sin6->sin6_port = htons(7070);
00234   sin6->sin6_addr.s6_addr[0] = 128;
00235   tor_addr_from_sockaddr(&t1, (struct sockaddr *)sin6, NULL);
00236   test_eq(tor_addr_family(&t1), AF_INET6);
00237   p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 0);
00238   test_streq(p1, "8000::");
00239 
00240   memset(&sa_storage, 0, sizeof(sa_storage));
00241   test_eq(sizeof(struct sockaddr_in6),
00242           tor_addr_to_sockaddr(&t1, 9999, (struct sockaddr *)&sa_storage,
00243                                sizeof(sa_storage)));
00244   test_eq(AF_INET6, sin6->sin6_family);
00245   test_eq(9999, ntohs(sin6->sin6_port));
00246   test_eq(0x80000000, ntohl(S6_ADDR32(sin6->sin6_addr)[0]));
00247 
00248   /* ==== tor_addr_lookup: static cases.  (Can't test dns without knowing we
00249    * have a good resolver. */
00250   test_eq(0, tor_addr_lookup("127.128.129.130", AF_UNSPEC, &t1));
00251   test_eq(AF_INET, tor_addr_family(&t1));
00252   test_eq(tor_addr_to_ipv4h(&t1), 0x7f808182);
00253 
00254   test_eq(0, tor_addr_lookup("9000::5", AF_UNSPEC, &t1));
00255   test_eq(AF_INET6, tor_addr_family(&t1));
00256   test_eq(0x90, tor_addr_to_in6_addr8(&t1)[0]);
00257   test_assert(tor_mem_is_zero((char*)tor_addr_to_in6_addr8(&t1)+1, 14));
00258   test_eq(0x05, tor_addr_to_in6_addr8(&t1)[15]);
00259 
00260   /* === Test pton: valid af_inet6 */
00261   /* Simple, valid parsing. */
00262   r = tor_inet_pton(AF_INET6,
00263                     "0102:0304:0506:0708:090A:0B0C:0D0E:0F10", &a1);
00264   test_assert(r==1);
00265   for (i=0;i<16;++i) { test_eq(i+1, (int)a1.s6_addr[i]); }
00266   /* ipv4 ending. */
00267   test_pton6_same("0102:0304:0506:0708:090A:0B0C:0D0E:0F10",
00268                   "0102:0304:0506:0708:090A:0B0C:13.14.15.16");
00269   /* shortened words. */
00270   test_pton6_same("0001:0099:BEEF:0000:0123:FFFF:0001:0001",
00271                   "1:99:BEEF:0:0123:FFFF:1:1");
00272   /* zeros at the beginning */
00273   test_pton6_same("0000:0000:0000:0000:0009:C0A8:0001:0001",
00274                   "::9:c0a8:1:1");
00275   test_pton6_same("0000:0000:0000:0000:0009:C0A8:0001:0001",
00276                   "::9:c0a8:0.1.0.1");
00277   /* zeros in the middle. */
00278   test_pton6_same("fe80:0000:0000:0000:0202:1111:0001:0001",
00279                   "fe80::202:1111:1:1");
00280   /* zeros at the end. */
00281   test_pton6_same("1000:0001:0000:0007:0000:0000:0000:0000",
00282                   "1000:1:0:7::");
00283 
00284   /* === Test ntop: af_inet6 */
00285   test_ntop6_reduces("0:0:0:0:0:0:0:0", "::");
00286 
00287   test_ntop6_reduces("0001:0099:BEEF:0006:0123:FFFF:0001:0001",
00288                      "1:99:beef:6:123:ffff:1:1");
00289 
00290   //test_ntop6_reduces("0:0:0:0:0:0:c0a8:0101", "::192.168.1.1");
00291   test_ntop6_reduces("0:0:0:0:0:ffff:c0a8:0101", "::ffff:192.168.1.1");
00292   test_ntop6_reduces("002:0:0000:0:3::4", "2::3:0:0:4");
00293   test_ntop6_reduces("0:0::1:0:3", "::1:0:3");
00294   test_ntop6_reduces("008:0::0", "8::");
00295   test_ntop6_reduces("0:0:0:0:0:ffff::1", "::ffff:0.0.0.1");
00296   test_ntop6_reduces("abcd:0:0:0:0:0:7f00::", "abcd::7f00:0");
00297   test_ntop6_reduces("0000:0000:0000:0000:0009:C0A8:0001:0001",
00298                      "::9:c0a8:1:1");
00299   test_ntop6_reduces("fe80:0000:0000:0000:0202:1111:0001:0001",
00300                      "fe80::202:1111:1:1");
00301   test_ntop6_reduces("1000:0001:0000:0007:0000:0000:0000:0000",
00302                      "1000:1:0:7::");
00303 
00304   /* Bad af param */
00305   test_eq(tor_inet_pton(AF_UNSPEC, 0, 0), -1);
00306 
00307   /* === Test pton: invalid in6. */
00308   test_pton6_bad("foobar.");
00309   test_pton6_bad("-1::");
00310   test_pton6_bad("00001::");
00311   test_pton6_bad("10000::");
00312   test_pton6_bad("::10000");
00313   test_pton6_bad("55555::");
00314   test_pton6_bad("9:-60::");
00315   test_pton6_bad("9:+60::");
00316   test_pton6_bad("9|60::");
00317   test_pton6_bad("0x60::");
00318   test_pton6_bad("::0x60");
00319   test_pton6_bad("9:0x60::");
00320   test_pton6_bad("1:2:33333:4:0002:3::");
00321   test_pton6_bad("1:2:3333:4:fish:3::");
00322   test_pton6_bad("1:2:3:4:5:6:7:8:9");
00323   test_pton6_bad("1:2:3:4:5:6:7");
00324   test_pton6_bad("1:2:3:4:5:6:1.2.3.4.5");
00325   test_pton6_bad("1:2:3:4:5:6:1.2.3");
00326   test_pton6_bad("::1.2.3");
00327   test_pton6_bad("::1.2.3.4.5");
00328   test_pton6_bad("::ffff:0xff.0.0.0");
00329   test_pton6_bad("::ffff:ff.0.0.0");
00330   test_pton6_bad("::ffff:256.0.0.0");
00331   test_pton6_bad("::ffff:-1.0.0.0");
00332   test_pton6_bad("99");
00333   test_pton6_bad("");
00334   test_pton6_bad(".");
00335   test_pton6_bad(":");
00336   test_pton6_bad("1::2::3:4");
00337   test_pton6_bad("a:::b:c");
00338   test_pton6_bad(":::a:b:c");
00339   test_pton6_bad("a:b:c:::");
00340 
00341   /* test internal checking */
00342   test_external_ip("fbff:ffff::2:7", 0);
00343   test_internal_ip("fc01::2:7", 0);
00344   test_internal_ip("fc01::02:7", 0);
00345   test_internal_ip("fc01::002:7", 0);
00346   test_internal_ip("fc01::0002:7", 0);
00347   test_internal_ip("fdff:ffff::f:f", 0);
00348   test_external_ip("fe00::3:f", 0);
00349 
00350   test_external_ip("fe7f:ffff::2:7", 0);
00351   test_internal_ip("fe80::2:7", 0);
00352   test_internal_ip("febf:ffff::f:f", 0);
00353 
00354   test_internal_ip("fec0::2:7:7", 0);
00355   test_internal_ip("feff:ffff::e:7:7", 0);
00356   test_external_ip("ff00::e:7:7", 0);
00357 
00358   test_internal_ip("::", 0);
00359   test_internal_ip("::1", 0);
00360   test_internal_ip("::1", 1);
00361   test_internal_ip("::", 0);
00362   test_external_ip("::", 1);
00363   test_external_ip("::2", 0);
00364   test_external_ip("2001::", 0);
00365   test_external_ip("ffff::", 0);
00366 
00367   test_external_ip("::ffff:0.0.0.0", 1);
00368   test_internal_ip("::ffff:0.0.0.0", 0);
00369   test_internal_ip("::ffff:0.255.255.255", 0);
00370   test_external_ip("::ffff:1.0.0.0", 0);
00371 
00372   test_external_ip("::ffff:9.255.255.255", 0);
00373   test_internal_ip("::ffff:10.0.0.0", 0);
00374   test_internal_ip("::ffff:10.255.255.255", 0);
00375   test_external_ip("::ffff:11.0.0.0", 0);
00376 
00377   test_external_ip("::ffff:126.255.255.255", 0);
00378   test_internal_ip("::ffff:127.0.0.0", 0);
00379   test_internal_ip("::ffff:127.255.255.255", 0);
00380   test_external_ip("::ffff:128.0.0.0", 0);
00381 
00382   test_external_ip("::ffff:172.15.255.255", 0);
00383   test_internal_ip("::ffff:172.16.0.0", 0);
00384   test_internal_ip("::ffff:172.31.255.255", 0);
00385   test_external_ip("::ffff:172.32.0.0", 0);
00386 
00387   test_external_ip("::ffff:192.167.255.255", 0);
00388   test_internal_ip("::ffff:192.168.0.0", 0);
00389   test_internal_ip("::ffff:192.168.255.255", 0);
00390   test_external_ip("::ffff:192.169.0.0", 0);
00391 
00392   test_external_ip("::ffff:169.253.255.255", 0);
00393   test_internal_ip("::ffff:169.254.0.0", 0);
00394   test_internal_ip("::ffff:169.254.255.255", 0);
00395   test_external_ip("::ffff:169.255.0.0", 0);
00396   test_assert(is_internal_IP(0x7f000001, 0));
00397 
00398   /* tor_addr_compare(tor_addr_t x2) */
00399   test_addr_compare("ffff::", ==, "ffff::0");
00400   test_addr_compare("0::3:2:1", <, "0::ffff:0.3.2.1");
00401   test_addr_compare("0::2:2:1", <, "0::ffff:0.3.2.1");
00402   test_addr_compare("0::ffff:0.3.2.1", >, "0::0:0:0");
00403   test_addr_compare("0::ffff:5.2.2.1", <, "::ffff:6.0.0.0"); /* XXXX wrong. */
00404   tor_addr_parse_mask_ports("[::ffff:2.3.4.5]", &t1, NULL, NULL, NULL);
00405   tor_addr_parse_mask_ports("2.3.4.5", &t2, NULL, NULL, NULL);
00406   test_assert(tor_addr_compare(&t1, &t2, CMP_SEMANTIC) == 0);
00407   tor_addr_parse_mask_ports("[::ffff:2.3.4.4]", &t1, NULL, NULL, NULL);
00408   tor_addr_parse_mask_ports("2.3.4.5", &t2, NULL, NULL, NULL);
00409   test_assert(tor_addr_compare(&t1, &t2, CMP_SEMANTIC) < 0);
00410 
00411   /* test compare_masked */
00412   test_addr_compare_masked("ffff::", ==, "ffff::0", 128);
00413   test_addr_compare_masked("ffff::", ==, "ffff::0", 64);
00414   test_addr_compare_masked("0::2:2:1", <, "0::8000:2:1", 81);
00415   test_addr_compare_masked("0::2:2:1", ==, "0::8000:2:1", 80);
00416 
00417   /* Test undecorated tor_addr_to_str */
00418   test_eq(AF_INET6, tor_addr_parse(&t1, "[123:45:6789::5005:11]"));
00419   p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 0);
00420   test_streq(p1, "123:45:6789::5005:11");
00421   test_eq(AF_INET, tor_addr_parse(&t1, "18.0.0.1"));
00422   p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 0);
00423   test_streq(p1, "18.0.0.1");
00424 
00425   /* Test decorated tor_addr_to_str */
00426   test_eq(AF_INET6, tor_addr_parse(&t1, "[123:45:6789::5005:11]"));
00427   p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 1);
00428   test_streq(p1, "[123:45:6789::5005:11]");
00429   test_eq(AF_INET, tor_addr_parse(&t1, "18.0.0.1"));
00430   p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 1);
00431   test_streq(p1, "18.0.0.1");
00432 
00433   /* Test buffer bounds checking of tor_addr_to_str */
00434   test_eq(AF_INET6, tor_addr_parse(&t1, "::")); /* 2 + \0 */
00435   test_eq_ptr(tor_addr_to_str(buf, &t1, 2, 0), NULL); /* too short buf */
00436   test_streq(tor_addr_to_str(buf, &t1, 3, 0), "::");
00437   test_eq_ptr(tor_addr_to_str(buf, &t1, 4, 1), NULL); /* too short buf */
00438   test_streq(tor_addr_to_str(buf, &t1, 5, 1), "[::]");
00439 
00440   test_eq(AF_INET6, tor_addr_parse(&t1, "2000::1337")); /* 10 + \0 */
00441   test_eq_ptr(tor_addr_to_str(buf, &t1, 10, 0), NULL); /* too short buf */
00442   test_streq(tor_addr_to_str(buf, &t1, 11, 0), "2000::1337");
00443   test_eq_ptr(tor_addr_to_str(buf, &t1, 12, 1), NULL); /* too short buf */
00444   test_streq(tor_addr_to_str(buf, &t1, 13, 1), "[2000::1337]");
00445 
00446   test_eq(AF_INET, tor_addr_parse(&t1, "1.2.3.4")); /* 7 + \0 */
00447   test_eq_ptr(tor_addr_to_str(buf, &t1, 7, 0), NULL); /* too short buf */
00448   test_streq(tor_addr_to_str(buf, &t1, 8, 0), "1.2.3.4");
00449 
00450   test_eq(AF_INET, tor_addr_parse(&t1, "255.255.255.255")); /* 15 + \0 */
00451   test_eq_ptr(tor_addr_to_str(buf, &t1, 15, 0), NULL); /* too short buf */
00452   test_streq(tor_addr_to_str(buf, &t1, 16, 0), "255.255.255.255");
00453   test_eq_ptr(tor_addr_to_str(buf, &t1, 15, 1), NULL); /* too short buf */
00454   test_streq(tor_addr_to_str(buf, &t1, 16, 1), "255.255.255.255");
00455 
00456   t1.family = AF_UNSPEC;
00457   test_eq_ptr(tor_addr_to_str(buf, &t1, sizeof(buf), 0), NULL);
00458 
00459   /* Test tor_addr_parse_PTR_name */
00460   i = tor_addr_parse_PTR_name(&t1, "Foobar.baz", AF_UNSPEC, 0);
00461   test_eq(0, i);
00462   i = tor_addr_parse_PTR_name(&t1, "Foobar.baz", AF_UNSPEC, 1);
00463   test_eq(0, i);
00464   i = tor_addr_parse_PTR_name(&t1, "1.0.168.192.in-addr.arpa",
00465                                          AF_UNSPEC, 1);
00466   test_eq(1, i);
00467   test_eq(tor_addr_family(&t1), AF_INET);
00468   p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 1);
00469   test_streq(p1, "192.168.0.1");
00470   i = tor_addr_parse_PTR_name(&t1, "192.168.0.99", AF_UNSPEC, 0);
00471   test_eq(0, i);
00472   i = tor_addr_parse_PTR_name(&t1, "192.168.0.99", AF_UNSPEC, 1);
00473   test_eq(1, i);
00474   p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 1);
00475   test_streq(p1, "192.168.0.99");
00476   memset(&t1, 0, sizeof(t1));
00477   i = tor_addr_parse_PTR_name(&t1,
00478                                          "0.1.2.3.4.5.6.7.8.9.a.b.c.d.e.f."
00479                                          "f.e.e.b.1.e.b.e.e.f.f.e.e.e.d.9."
00480                                          "ip6.ARPA",
00481                                          AF_UNSPEC, 0);
00482   test_eq(1, i);
00483   p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 1);
00484   test_streq(p1, "[9dee:effe:ebe1:beef:fedc:ba98:7654:3210]");
00485   /* Failing cases. */
00486   i = tor_addr_parse_PTR_name(&t1,
00487                                          "6.7.8.9.a.b.c.d.e.f."
00488                                          "f.e.e.b.1.e.b.e.e.f.f.e.e.e.d.9."
00489                                          "ip6.ARPA",
00490                                          AF_UNSPEC, 0);
00491   test_eq(i, -1);
00492   i = tor_addr_parse_PTR_name(&t1,
00493                                          "6.7.8.9.a.b.c.d.e.f.a.b.c.d.e.f.0."
00494                                          "f.e.e.b.1.e.b.e.e.f.f.e.e.e.d.9."
00495                                          "ip6.ARPA",
00496                                          AF_UNSPEC, 0);
00497   test_eq(i, -1);
00498   i = tor_addr_parse_PTR_name(&t1,
00499                                          "6.7.8.9.a.b.c.d.e.f.X.0.0.0.0.9."
00500                                          "f.e.e.b.1.e.b.e.e.f.f.e.e.e.d.9."
00501                                          "ip6.ARPA",
00502                                          AF_UNSPEC, 0);
00503   test_eq(i, -1);
00504   i = tor_addr_parse_PTR_name(&t1, "32.1.1.in-addr.arpa",
00505                                          AF_UNSPEC, 0);
00506   test_eq(i, -1);
00507   i = tor_addr_parse_PTR_name(&t1, ".in-addr.arpa",
00508                                          AF_UNSPEC, 0);
00509   test_eq(i, -1);
00510   i = tor_addr_parse_PTR_name(&t1, "1.2.3.4.5.in-addr.arpa",
00511                                          AF_UNSPEC, 0);
00512   test_eq(i, -1);
00513   i = tor_addr_parse_PTR_name(&t1, "1.2.3.4.5.in-addr.arpa",
00514                                          AF_INET6, 0);
00515   test_eq(i, -1);
00516   i = tor_addr_parse_PTR_name(&t1,
00517                                          "6.7.8.9.a.b.c.d.e.f.a.b.c.d.e.0."
00518                                          "f.e.e.b.1.e.b.e.e.f.f.e.e.e.d.9."
00519                                          "ip6.ARPA",
00520                                          AF_INET, 0);
00521   test_eq(i, -1);
00522 
00523   /* === Test tor_addr_to_PTR_name */
00524 
00525   /* Stage IPv4 addr */
00526   memset(&sa_storage, 0, sizeof(sa_storage));
00527   sin = (struct sockaddr_in *)&sa_storage;
00528   sin->sin_family = AF_INET;
00529   sin->sin_addr.s_addr = htonl(0x7f010203); /* 127.1.2.3 */
00530   tor_addr_from_sockaddr(&t1, (struct sockaddr *)sin, NULL);
00531 
00532   /* Check IPv4 PTR - too short buffer */
00533   test_eq(tor_addr_to_PTR_name(rbuf, 1, &t1), -1);
00534   test_eq(tor_addr_to_PTR_name(rbuf,
00535                                strlen("3.2.1.127.in-addr.arpa") - 1,
00536                                &t1), -1);
00537 
00538   /* Check IPv4 PTR - valid addr */
00539   test_eq(tor_addr_to_PTR_name(rbuf, sizeof(rbuf), &t1),
00540           strlen("3.2.1.127.in-addr.arpa"));
00541   test_streq(rbuf, "3.2.1.127.in-addr.arpa");
00542 
00543   /* Invalid addr family */
00544   t1.family = AF_UNSPEC;
00545   test_eq(tor_addr_to_PTR_name(rbuf, sizeof(rbuf), &t1), -1);
00546 
00547   /* Stage IPv6 addr */
00548   memset(&sa_storage, 0, sizeof(sa_storage));
00549   sin6 = (struct sockaddr_in6 *)&sa_storage;
00550   sin6->sin6_family = AF_INET6;
00551   sin6->sin6_addr.s6_addr[0] = 0x80; /* 8000::abcd */
00552   sin6->sin6_addr.s6_addr[14] = 0xab;
00553   sin6->sin6_addr.s6_addr[15] = 0xcd;
00554 
00555   tor_addr_from_sockaddr(&t1, (struct sockaddr *)sin6, NULL);
00556 
00557   {
00558     const char* addr_PTR = "d.c.b.a.0.0.0.0.0.0.0.0.0.0.0.0."
00559       "0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.ip6.arpa";
00560 
00561     /* Check IPv6 PTR - too short buffer */
00562     test_eq(tor_addr_to_PTR_name(rbuf, 0, &t1), -1);
00563     test_eq(tor_addr_to_PTR_name(rbuf, strlen(addr_PTR) - 1, &t1), -1);
00564 
00565     /* Check IPv6 PTR - valid addr */
00566     test_eq(tor_addr_to_PTR_name(rbuf, sizeof(rbuf), &t1),
00567             strlen(addr_PTR));
00568     test_streq(rbuf, addr_PTR);
00569   }
00570 
00571   /* test tor_addr_parse_mask_ports */
00572   test_addr_mask_ports_parse("[::f]/17:47-95", AF_INET6,
00573                              0, 0, 0, 0x0000000f, 17, 47, 95);
00574   test_streq(p1, "::f");
00575   //test_addr_parse("[::fefe:4.1.1.7/120]:999-1000");
00576   //test_addr_parse_check("::fefe:401:107", 120, 999, 1000);
00577   test_addr_mask_ports_parse("[::ffff:4.1.1.7]/120:443", AF_INET6,
00578                              0, 0, 0x0000ffff, 0x04010107, 120, 443, 443);
00579   test_streq(p1, "::ffff:4.1.1.7");
00580   test_addr_mask_ports_parse("[abcd:2::44a:0]:2-65000", AF_INET6,
00581                              0xabcd0002, 0, 0, 0x044a0000, 128, 2, 65000);
00582 
00583   test_streq(p1, "abcd:2::44a:0");
00584   r=tor_addr_parse_mask_ports("[fefef::]/112", &t1, NULL, NULL, NULL);
00585   test_assert(r == -1);
00586   r=tor_addr_parse_mask_ports("efef::/112", &t1, NULL, NULL, NULL);
00587   test_assert(r == -1);
00588   r=tor_addr_parse_mask_ports("[f:f:f:f:f:f:f:f::]", &t1, NULL, NULL, NULL);
00589   test_assert(r == -1);
00590   r=tor_addr_parse_mask_ports("[::f:f:f:f:f:f:f:f]", &t1, NULL, NULL, NULL);
00591   test_assert(r == -1);
00592   r=tor_addr_parse_mask_ports("[f:f:f:f:f:f:f:f:f]", &t1, NULL, NULL, NULL);
00593   test_assert(r == -1);
00594   /* Test for V4-mapped address with mask < 96.  (arguably not valid) */
00595   r=tor_addr_parse_mask_ports("[::ffff:1.1.2.2/33]", &t1, &mask, NULL, NULL);
00596   test_assert(r == -1);
00597   r=tor_addr_parse_mask_ports("1.1.2.2/33", &t1, &mask, NULL, NULL);
00598   test_assert(r == -1);
00599   r=tor_addr_parse_mask_ports("1.1.2.2/31", &t1, &mask, NULL, NULL);
00600   test_assert(r == AF_INET);
00601   r=tor_addr_parse_mask_ports("[efef::]/112", &t1, &mask, &port1, &port2);
00602   test_assert(r == AF_INET6);
00603   test_assert(port1 == 1);
00604   test_assert(port2 == 65535);
00605 
00606   /* make sure inet address lengths >= max */
00607   test_assert(INET_NTOA_BUF_LEN >= sizeof("255.255.255.255"));
00608   test_assert(TOR_ADDR_BUF_LEN >=
00609               sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"));
00610 
00611   test_assert(sizeof(tor_addr_t) >= sizeof(struct in6_addr));
00612 
00613   /* get interface addresses */
00614   r = get_interface_address6(LOG_DEBUG, AF_INET, &t1);
00615   i = get_interface_address6(LOG_DEBUG, AF_INET6, &t2);
00616 
00617   TT_BLATHER(("v4 address: %s (family=%d)", fmt_addr(&t1),
00618               tor_addr_family(&t1)));
00619   TT_BLATHER(("v6 address: %s (family=%d)", fmt_addr(&t2),
00620               tor_addr_family(&t2)));
00621 
00622  done:
00623   ;
00624 }
00625 
00626 #define ADDR_LEGACY(name)                                               \
00627   { #name, legacy_test_helper, 0, &legacy_setup, test_addr_ ## name }
00628 
00629 struct testcase_t addr_tests[] = {
00630   ADDR_LEGACY(basic),
00631   ADDR_LEGACY(ip6_helpers),
00632   END_OF_TESTCASES
00633 };
00634