test_addr.c 41 KB

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