test_config.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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. static int
  142. is_private_dir(const char* path)
  143. {
  144. struct stat st;
  145. int r = stat(path, &st);
  146. if (r) {
  147. return 0;
  148. }
  149. #if !defined (_WIN32) || defined (WINCE)
  150. if ((st.st_mode & (S_IFDIR | 0777)) != (S_IFDIR | 0700)) {
  151. return 0;
  152. }
  153. #endif
  154. return 1;
  155. }
  156. static void
  157. test_config_check_or_create_data_subdir(void *arg)
  158. {
  159. or_options_t *options = get_options_mutable();
  160. char *datadir = options->DataDirectory = tor_strdup(get_fname("datadir-0"));
  161. const char *subdir = "test_stats";
  162. char *subpath = get_datadir_fname(subdir);
  163. struct stat st;
  164. int r;
  165. #if !defined (_WIN32) || defined (WINCE)
  166. unsigned group_permission;
  167. #endif
  168. (void)arg;
  169. #if defined (_WIN32) && !defined (WINCE)
  170. tt_int_op(mkdir(options->DataDirectory), ==, 0);
  171. #else
  172. tt_int_op(mkdir(options->DataDirectory, 0700), ==, 0);
  173. #endif
  174. r = stat(subpath, &st);
  175. // The subdirectory shouldn't exist yet,
  176. // but should be created by the call to check_or_create_data_subdir.
  177. test_assert(r && (errno == ENOENT));
  178. test_assert(!check_or_create_data_subdir(subdir));
  179. test_assert(is_private_dir(subpath));
  180. // The check should return 0, if the directory already exists
  181. // and is private to the user.
  182. test_assert(!check_or_create_data_subdir(subdir));
  183. #if !defined (_WIN32) || defined (WINCE)
  184. group_permission = st.st_mode | 0070;
  185. r = chmod(subpath, group_permission);
  186. if (r) {
  187. test_fail_msg("Changing permissions for the subdirectory failed.");
  188. }
  189. // If the directory exists, but its mode is too permissive
  190. // a call to check_or_create_data_subdir should reset the mode.
  191. test_assert(!is_private_dir(subpath));
  192. test_assert(!check_or_create_data_subdir(subdir));
  193. test_assert(is_private_dir(subpath));
  194. #endif
  195. done:
  196. rmdir(subpath);
  197. tor_free(datadir);
  198. tor_free(subpath);
  199. }
  200. static void
  201. test_config_write_to_data_subdir(void *arg)
  202. {
  203. or_options_t* options = get_options_mutable();
  204. char *datadir = options->DataDirectory = tor_strdup(get_fname("datadir-1"));
  205. const char* subdir = "test_stats";
  206. const char* fname = "test_file";
  207. const char* str =
  208. "Lorem ipsum dolor sit amet, consetetur sadipscing\n"
  209. "elitr, sed diam nonumy eirmod\n"
  210. "tempor invidunt ut labore et dolore magna aliquyam\n"
  211. "erat, sed diam voluptua.\n"
  212. "At vero eos et accusam et justo duo dolores et ea\n"
  213. "rebum. Stet clita kasd gubergren,\n"
  214. "no sea takimata sanctus est Lorem ipsum dolor sit amet.\n"
  215. "Lorem ipsum dolor sit amet,\n"
  216. "consetetur sadipscing elitr, sed diam nonumy eirmod\n"
  217. "tempor invidunt ut labore et dolore\n"
  218. "magna aliquyam erat, sed diam voluptua. At vero eos et\n"
  219. "accusam et justo duo dolores et\n"
  220. "ea rebum. Stet clita kasd gubergren, no sea takimata\n"
  221. "sanctus est Lorem ipsum dolor sit amet.";
  222. char* filepath = get_datadir_fname2(subdir, fname);
  223. (void)arg;
  224. #if defined (_WIN32) && !defined (WINCE)
  225. tt_int_op(mkdir(options->DataDirectory), ==, 0);
  226. #else
  227. tt_int_op(mkdir(options->DataDirectory, 0700), ==, 0);
  228. #endif
  229. // Write attempt shoudl fail, if subdirectory doesn't exist.
  230. test_assert(write_to_data_subdir(subdir, fname, str, NULL));
  231. test_assert(! check_or_create_data_subdir(subdir));
  232. // Content of file after write attempt should be
  233. // equal to the original string.
  234. test_assert(!write_to_data_subdir(subdir, fname, str, NULL));
  235. test_streq(read_file_to_str(filepath, 0, NULL), str);
  236. // A second write operation should overwrite the old content.
  237. test_assert(!write_to_data_subdir(subdir, fname, str, NULL));
  238. test_streq(read_file_to_str(filepath, 0, NULL), str);
  239. done:
  240. (void) unlink(filepath);
  241. rmdir(options->DataDirectory);
  242. tor_free(datadir);
  243. tor_free(filepath);
  244. }
  245. /* Test helper function: Make sure that a bridge line gets parsed
  246. * properly. Also make sure that the resulting bridge_line_t structure
  247. * has its fields set correctly. */
  248. static void
  249. good_bridge_line_test(const char *string, const char *test_addrport,
  250. const char *test_digest, const char *test_transport,
  251. const smartlist_t *test_socks_args)
  252. {
  253. char *tmp = NULL;
  254. bridge_line_t *bridge_line = parse_bridge_line(string);
  255. test_assert(bridge_line);
  256. /* test addrport */
  257. tmp = tor_strdup(fmt_addrport(&bridge_line->addr, bridge_line->port));
  258. test_streq(test_addrport, tmp);
  259. tor_free(tmp);
  260. /* If we were asked to validate a digest, but we did not get a
  261. digest after parsing, we failed. */
  262. if (test_digest && tor_digest_is_zero(bridge_line->digest))
  263. test_assert(0);
  264. /* If we were not asked to validate a digest, and we got a digest
  265. after parsing, we failed again. */
  266. if (!test_digest && !tor_digest_is_zero(bridge_line->digest))
  267. test_assert(0);
  268. /* If we were asked to validate a digest, and we got a digest after
  269. parsing, make sure it's correct. */
  270. if (test_digest) {
  271. tmp = tor_strdup(hex_str(bridge_line->digest, DIGEST_LEN));
  272. tor_strlower(tmp);
  273. test_streq(test_digest, tmp);
  274. tor_free(tmp);
  275. }
  276. /* If we were asked to validate a transport name, make sure tha it
  277. matches with the transport name that was parsed. */
  278. if (test_transport && !bridge_line->transport_name)
  279. test_assert(0);
  280. if (!test_transport && bridge_line->transport_name)
  281. test_assert(0);
  282. if (test_transport)
  283. test_streq(test_transport, bridge_line->transport_name);
  284. /* Validate the SOCKS argument smartlist. */
  285. if (test_socks_args && !bridge_line->socks_args)
  286. test_assert(0);
  287. if (!test_socks_args && bridge_line->socks_args)
  288. test_assert(0);
  289. if (test_socks_args)
  290. test_assert(smartlist_strings_eq(test_socks_args,
  291. bridge_line->socks_args));
  292. done:
  293. tor_free(tmp);
  294. bridge_line_free(bridge_line);
  295. }
  296. /* Test helper function: Make sure that a bridge line is
  297. * unparseable. */
  298. static void
  299. bad_bridge_line_test(const char *string)
  300. {
  301. bridge_line_t *bridge_line = parse_bridge_line(string);
  302. test_assert(!bridge_line);
  303. done:
  304. bridge_line_free(bridge_line);
  305. }
  306. static void
  307. test_config_parse_bridge_line(void *arg)
  308. {
  309. (void) arg;
  310. good_bridge_line_test("192.0.2.1:4123",
  311. "192.0.2.1:4123", NULL, NULL, NULL);
  312. good_bridge_line_test("192.0.2.1",
  313. "192.0.2.1:443", NULL, NULL, NULL);
  314. good_bridge_line_test("transport [::1]",
  315. "[::1]:443", NULL, "transport", NULL);
  316. good_bridge_line_test("transport 192.0.2.1:12 "
  317. "4352e58420e68f5e40bf7c74faddccd9d1349413",
  318. "192.0.2.1:12",
  319. "4352e58420e68f5e40bf7c74faddccd9d1349413",
  320. "transport", NULL);
  321. {
  322. smartlist_t *sl_tmp = smartlist_new();
  323. smartlist_add_asprintf(sl_tmp, "twoandtwo=five");
  324. good_bridge_line_test("transport 192.0.2.1:12 "
  325. "4352e58420e68f5e40bf7c74faddccd9d1349413 twoandtwo=five",
  326. "192.0.2.1:12", "4352e58420e68f5e40bf7c74faddccd9d1349413",
  327. "transport", sl_tmp);
  328. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  329. smartlist_free(sl_tmp);
  330. }
  331. {
  332. smartlist_t *sl_tmp = smartlist_new();
  333. smartlist_add_asprintf(sl_tmp, "twoandtwo=five");
  334. smartlist_add_asprintf(sl_tmp, "z=z");
  335. good_bridge_line_test("transport 192.0.2.1:12 twoandtwo=five z=z",
  336. "192.0.2.1:12", NULL, "transport", sl_tmp);
  337. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  338. smartlist_free(sl_tmp);
  339. }
  340. good_bridge_line_test("192.0.2.1:1231 "
  341. "4352e58420e68f5e40bf7c74faddccd9d1349413",
  342. "192.0.2.1:1231",
  343. "4352e58420e68f5e40bf7c74faddccd9d1349413",
  344. NULL, NULL);
  345. /* Empty line */
  346. bad_bridge_line_test("");
  347. /* bad transport name */
  348. bad_bridge_line_test("tr$n_sp0r7 190.20.2.2");
  349. /* weird ip address */
  350. bad_bridge_line_test("a.b.c.d");
  351. /* invalid fpr */
  352. bad_bridge_line_test("2.2.2.2:1231 4352e58420e68f5e40bf7c74faddccd9d1349");
  353. /* no k=v in the end */
  354. bad_bridge_line_test("obfs2 2.2.2.2:1231 "
  355. "4352e58420e68f5e40bf7c74faddccd9d1349413 what");
  356. /* no addrport */
  357. bad_bridge_line_test("asdw");
  358. /* huge k=v value that can't fit in SOCKS fields */
  359. bad_bridge_line_test(
  360. "obfs2 2.2.2.2:1231 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  361. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  362. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  363. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  364. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  365. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  366. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  367. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  368. "aa=b");
  369. }
  370. static void
  371. test_config_parse_transport_options_line(void *arg)
  372. {
  373. smartlist_t *options_sl = NULL, *sl_tmp = NULL;
  374. (void) arg;
  375. { /* too small line */
  376. options_sl = get_options_from_transport_options_line("valley", NULL);
  377. test_assert(!options_sl);
  378. }
  379. { /* no k=v values */
  380. options_sl = get_options_from_transport_options_line("hit it!", NULL);
  381. test_assert(!options_sl);
  382. }
  383. { /* correct line, but wrong transport specified */
  384. options_sl =
  385. get_options_from_transport_options_line("trebuchet k=v", "rook");
  386. test_assert(!options_sl);
  387. }
  388. { /* correct -- no transport specified */
  389. sl_tmp = smartlist_new();
  390. smartlist_add_asprintf(sl_tmp, "ladi=dadi");
  391. smartlist_add_asprintf(sl_tmp, "weliketo=party");
  392. options_sl =
  393. get_options_from_transport_options_line("rook ladi=dadi weliketo=party",
  394. NULL);
  395. test_assert(options_sl);
  396. test_assert(smartlist_strings_eq(options_sl, sl_tmp));
  397. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  398. smartlist_free(sl_tmp);
  399. sl_tmp = NULL;
  400. SMARTLIST_FOREACH(options_sl, char *, s, tor_free(s));
  401. smartlist_free(options_sl);
  402. options_sl = NULL;
  403. }
  404. { /* correct -- correct transport specified */
  405. sl_tmp = smartlist_new();
  406. smartlist_add_asprintf(sl_tmp, "ladi=dadi");
  407. smartlist_add_asprintf(sl_tmp, "weliketo=party");
  408. options_sl =
  409. get_options_from_transport_options_line("rook ladi=dadi weliketo=party",
  410. "rook");
  411. test_assert(options_sl);
  412. test_assert(smartlist_strings_eq(options_sl, sl_tmp));
  413. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  414. smartlist_free(sl_tmp);
  415. sl_tmp = NULL;
  416. SMARTLIST_FOREACH(options_sl, char *, s, tor_free(s));
  417. smartlist_free(options_sl);
  418. options_sl = NULL;
  419. }
  420. done:
  421. if (options_sl) {
  422. SMARTLIST_FOREACH(options_sl, char *, s, tor_free(s));
  423. smartlist_free(options_sl);
  424. }
  425. if (sl_tmp) {
  426. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  427. smartlist_free(sl_tmp);
  428. }
  429. }
  430. #define CONFIG_TEST(name, flags) \
  431. { #name, test_config_ ## name, flags, NULL, NULL }
  432. struct testcase_t config_tests[] = {
  433. CONFIG_TEST(addressmap, 0),
  434. CONFIG_TEST(parse_bridge_line, 0),
  435. CONFIG_TEST(parse_transport_options_line, 0),
  436. CONFIG_TEST(check_or_create_data_subdir, TT_FORK),
  437. CONFIG_TEST(write_to_data_subdir, TT_FORK),
  438. END_OF_TESTCASES
  439. };