test_config.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2013, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #include "orconfig.h"
  6. #include "or.h"
  7. #include "addressmap.h"
  8. #include "config.h"
  9. #include "confparse.h"
  10. #include "connection_edge.h"
  11. #include "test.h"
  12. #include "util.h"
  13. #include "address.h"
  14. static void
  15. test_config_addressmap(void *arg)
  16. {
  17. char buf[1024];
  18. char address[256];
  19. time_t expires = TIME_MAX;
  20. (void)arg;
  21. strlcpy(buf, "MapAddress .invalidwildcard.com *.torserver.exit\n" // invalid
  22. "MapAddress *invalidasterisk.com *.torserver.exit\n" // invalid
  23. "MapAddress *.google.com *.torserver.exit\n"
  24. "MapAddress *.yahoo.com *.google.com.torserver.exit\n"
  25. "MapAddress *.cn.com www.cnn.com\n"
  26. "MapAddress *.cnn.com www.cnn.com\n"
  27. "MapAddress ex.com www.cnn.com\n"
  28. "MapAddress ey.com *.cnn.com\n"
  29. "MapAddress www.torproject.org 1.1.1.1\n"
  30. "MapAddress other.torproject.org "
  31. "this.torproject.org.otherserver.exit\n"
  32. "MapAddress test.torproject.org 2.2.2.2\n"
  33. "MapAddress www.google.com 3.3.3.3\n"
  34. "MapAddress www.example.org 4.4.4.4\n"
  35. "MapAddress 4.4.4.4 7.7.7.7\n"
  36. "MapAddress 4.4.4.4 5.5.5.5\n"
  37. "MapAddress www.infiniteloop.org 6.6.6.6\n"
  38. "MapAddress 6.6.6.6 www.infiniteloop.org\n"
  39. , sizeof(buf));
  40. config_get_lines(buf, &(get_options_mutable()->AddressMap), 0);
  41. config_register_addressmaps(get_options());
  42. /* Use old interface for now, so we don't need to rewrite the unit tests */
  43. #define addressmap_rewrite(a,s,eo,ao) \
  44. addressmap_rewrite((a),(s),AMR_FLAG_USE_IPV4_DNS|AMR_FLAG_USE_IPV6_DNS, \
  45. (eo),(ao))
  46. /* MapAddress .invalidwildcard.com .torserver.exit - no match */
  47. strlcpy(address, "www.invalidwildcard.com", sizeof(address));
  48. test_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  49. /* MapAddress *invalidasterisk.com .torserver.exit - no match */
  50. strlcpy(address, "www.invalidasterisk.com", sizeof(address));
  51. test_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  52. /* Where no mapping for FQDN match on top-level domain */
  53. /* MapAddress .google.com .torserver.exit */
  54. strlcpy(address, "reader.google.com", sizeof(address));
  55. test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  56. test_streq(address, "reader.torserver.exit");
  57. /* MapAddress *.yahoo.com *.google.com.torserver.exit */
  58. strlcpy(address, "reader.yahoo.com", sizeof(address));
  59. test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  60. test_streq(address, "reader.google.com.torserver.exit");
  61. /*MapAddress *.cnn.com www.cnn.com */
  62. strlcpy(address, "cnn.com", sizeof(address));
  63. test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  64. test_streq(address, "www.cnn.com");
  65. /* MapAddress .cn.com www.cnn.com */
  66. strlcpy(address, "www.cn.com", sizeof(address));
  67. test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  68. test_streq(address, "www.cnn.com");
  69. /* MapAddress ex.com www.cnn.com - no match */
  70. strlcpy(address, "www.ex.com", sizeof(address));
  71. test_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  72. /* MapAddress ey.com *.cnn.com - invalid expression */
  73. strlcpy(address, "ey.com", sizeof(address));
  74. test_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  75. /* Where mapping for FQDN match on FQDN */
  76. strlcpy(address, "www.google.com", sizeof(address));
  77. test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  78. test_streq(address, "3.3.3.3");
  79. strlcpy(address, "www.torproject.org", sizeof(address));
  80. test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  81. test_streq(address, "1.1.1.1");
  82. strlcpy(address, "other.torproject.org", sizeof(address));
  83. test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  84. test_streq(address, "this.torproject.org.otherserver.exit");
  85. strlcpy(address, "test.torproject.org", sizeof(address));
  86. test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  87. test_streq(address, "2.2.2.2");
  88. /* Test a chain of address mappings and the order in which they were added:
  89. "MapAddress www.example.org 4.4.4.4"
  90. "MapAddress 4.4.4.4 7.7.7.7"
  91. "MapAddress 4.4.4.4 5.5.5.5"
  92. */
  93. strlcpy(address, "www.example.org", sizeof(address));
  94. test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  95. test_streq(address, "5.5.5.5");
  96. /* Test infinite address mapping results in no change */
  97. strlcpy(address, "www.infiniteloop.org", sizeof(address));
  98. test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  99. test_streq(address, "www.infiniteloop.org");
  100. /* Test we don't find false positives */
  101. strlcpy(address, "www.example.com", sizeof(address));
  102. test_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  103. /* Test top-level-domain matching a bit harder */
  104. addressmap_clear_configured();
  105. strlcpy(buf, "MapAddress *.com *.torserver.exit\n"
  106. "MapAddress *.torproject.org 1.1.1.1\n"
  107. "MapAddress *.net 2.2.2.2\n"
  108. , sizeof(buf));
  109. config_get_lines(buf, &(get_options_mutable()->AddressMap), 0);
  110. config_register_addressmaps(get_options());
  111. strlcpy(address, "www.abc.com", sizeof(address));
  112. test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  113. test_streq(address, "www.abc.torserver.exit");
  114. strlcpy(address, "www.def.com", sizeof(address));
  115. test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  116. test_streq(address, "www.def.torserver.exit");
  117. strlcpy(address, "www.torproject.org", sizeof(address));
  118. test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  119. test_streq(address, "1.1.1.1");
  120. strlcpy(address, "test.torproject.org", sizeof(address));
  121. test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  122. test_streq(address, "1.1.1.1");
  123. strlcpy(address, "torproject.net", sizeof(address));
  124. test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  125. test_streq(address, "2.2.2.2");
  126. /* We don't support '*' as a mapping directive */
  127. addressmap_clear_configured();
  128. strlcpy(buf, "MapAddress * *.torserver.exit\n", sizeof(buf));
  129. config_get_lines(buf, &(get_options_mutable()->AddressMap), 0);
  130. config_register_addressmaps(get_options());
  131. strlcpy(address, "www.abc.com", sizeof(address));
  132. test_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  133. strlcpy(address, "www.def.net", sizeof(address));
  134. test_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  135. strlcpy(address, "www.torproject.org", sizeof(address));
  136. test_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  137. #undef addressmap_rewrite
  138. done:
  139. ;
  140. }
  141. /* Test helper function: Make sure that a bridge line gets parsed
  142. * properly. Also make sure that the resulting bridge_line_t structure
  143. * has its fields set correctly. */
  144. static void
  145. good_bridge_line_test(const char *string, const char *test_addrport,
  146. const char *test_digest, const char *test_transport,
  147. const smartlist_t *test_socks_args)
  148. {
  149. char *tmp = NULL;
  150. bridge_line_t *bridge_line = parse_bridge_line(string);
  151. test_assert(bridge_line);
  152. /* test addrport */
  153. tmp = tor_strdup(fmt_addrport(&bridge_line->addr, bridge_line->port));
  154. test_streq(test_addrport, tmp);
  155. tor_free(tmp);
  156. /* If we were asked to validate a digest, but we did not get a
  157. digest after parsing, we failed. */
  158. if (test_digest && tor_digest_is_zero(bridge_line->digest))
  159. test_assert(0);
  160. /* If we were not asked to validate a digest, and we got a digest
  161. after parsing, we failed again. */
  162. if (!test_digest && !tor_digest_is_zero(bridge_line->digest))
  163. test_assert(0);
  164. /* If we were asked to validate a digest, and we got a digest after
  165. parsing, make sure it's correct. */
  166. if (test_digest) {
  167. tmp = tor_strdup(hex_str(bridge_line->digest, DIGEST_LEN));
  168. tor_strlower(tmp);
  169. test_streq(test_digest, tmp);
  170. tor_free(tmp);
  171. }
  172. /* If we were asked to validate a transport name, make sure tha it
  173. matches with the transport name that was parsed. */
  174. if (test_transport && !bridge_line->transport_name)
  175. test_assert(0);
  176. if (!test_transport && bridge_line->transport_name)
  177. test_assert(0);
  178. if (test_transport)
  179. test_streq(test_transport, bridge_line->transport_name);
  180. /* Validate the SOCKS argument smartlist. */
  181. if (test_socks_args && !bridge_line->socks_args)
  182. test_assert(0);
  183. if (!test_socks_args && bridge_line->socks_args)
  184. test_assert(0);
  185. if (test_socks_args)
  186. test_assert(smartlist_strings_eq(test_socks_args,
  187. bridge_line->socks_args));
  188. done:
  189. tor_free(tmp);
  190. bridge_line_free(bridge_line);
  191. }
  192. /* Test helper function: Make sure that a bridge line is
  193. * unparseable. */
  194. static void
  195. bad_bridge_line_test(const char *string)
  196. {
  197. bridge_line_t *bridge_line = parse_bridge_line(string);
  198. test_assert(!bridge_line);
  199. done:
  200. bridge_line_free(bridge_line);
  201. }
  202. static void
  203. test_config_parse_bridge_line(void *arg)
  204. {
  205. (void) arg;
  206. good_bridge_line_test("192.0.2.1:4123",
  207. "192.0.2.1:4123", NULL, NULL, NULL);
  208. good_bridge_line_test("192.0.2.1",
  209. "192.0.2.1:443", NULL, NULL, NULL);
  210. good_bridge_line_test("transport [::1]",
  211. "[::1]:443", NULL, "transport", NULL);
  212. good_bridge_line_test("transport 192.0.2.1:12 "
  213. "4352e58420e68f5e40bf7c74faddccd9d1349413",
  214. "192.0.2.1:12",
  215. "4352e58420e68f5e40bf7c74faddccd9d1349413",
  216. "transport", NULL);
  217. {
  218. smartlist_t *sl_tmp = smartlist_new();
  219. smartlist_add_asprintf(sl_tmp, "twoandtwo=five");
  220. good_bridge_line_test("transport 192.0.2.1:12 "
  221. "4352e58420e68f5e40bf7c74faddccd9d1349413 twoandtwo=five",
  222. "192.0.2.1:12", "4352e58420e68f5e40bf7c74faddccd9d1349413",
  223. "transport", sl_tmp);
  224. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  225. smartlist_free(sl_tmp);
  226. }
  227. {
  228. smartlist_t *sl_tmp = smartlist_new();
  229. smartlist_add_asprintf(sl_tmp, "twoandtwo=five");
  230. smartlist_add_asprintf(sl_tmp, "z=z");
  231. good_bridge_line_test("transport 192.0.2.1:12 twoandtwo=five z=z",
  232. "192.0.2.1:12", NULL, "transport", sl_tmp);
  233. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  234. smartlist_free(sl_tmp);
  235. }
  236. good_bridge_line_test("192.0.2.1:1231 "
  237. "4352e58420e68f5e40bf7c74faddccd9d1349413",
  238. "192.0.2.1:1231",
  239. "4352e58420e68f5e40bf7c74faddccd9d1349413",
  240. NULL, NULL);
  241. /* Empty line */
  242. bad_bridge_line_test("");
  243. /* bad transport name */
  244. bad_bridge_line_test("tr$n_sp0r7 190.20.2.2");
  245. /* weird ip address */
  246. bad_bridge_line_test("a.b.c.d");
  247. /* invalid fpr */
  248. bad_bridge_line_test("2.2.2.2:1231 4352e58420e68f5e40bf7c74faddccd9d1349");
  249. /* no k=v in the end */
  250. bad_bridge_line_test("obfs2 2.2.2.2:1231 "
  251. "4352e58420e68f5e40bf7c74faddccd9d1349413 what");
  252. /* no addrport */
  253. bad_bridge_line_test("asdw");
  254. /* huge k=v value that can't fit in SOCKS fields */
  255. bad_bridge_line_test(
  256. "obfs2 2.2.2.2:1231 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  257. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  258. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  259. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  260. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  261. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  262. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  263. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  264. "aa=b");
  265. }
  266. #define CONFIG_TEST(name, flags) \
  267. { #name, test_config_ ## name, flags, NULL, NULL }
  268. struct testcase_t config_tests[] = {
  269. CONFIG_TEST(addressmap, 0),
  270. CONFIG_TEST(parse_bridge_line, 0),
  271. END_OF_TESTCASES
  272. };