test_config.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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. #define CONFIG_PRIVATE
  7. #include "or.h"
  8. #include "addressmap.h"
  9. #include "config.h"
  10. #include "confparse.h"
  11. #include "connection_edge.h"
  12. #include "test.h"
  13. #include "util.h"
  14. #include "address.h"
  15. static void
  16. test_config_addressmap(void *arg)
  17. {
  18. char buf[1024];
  19. char address[256];
  20. time_t expires = TIME_MAX;
  21. (void)arg;
  22. strlcpy(buf, "MapAddress .invalidwildcard.com *.torserver.exit\n" // invalid
  23. "MapAddress *invalidasterisk.com *.torserver.exit\n" // invalid
  24. "MapAddress *.google.com *.torserver.exit\n"
  25. "MapAddress *.yahoo.com *.google.com.torserver.exit\n"
  26. "MapAddress *.cn.com www.cnn.com\n"
  27. "MapAddress *.cnn.com www.cnn.com\n"
  28. "MapAddress ex.com www.cnn.com\n"
  29. "MapAddress ey.com *.cnn.com\n"
  30. "MapAddress www.torproject.org 1.1.1.1\n"
  31. "MapAddress other.torproject.org "
  32. "this.torproject.org.otherserver.exit\n"
  33. "MapAddress test.torproject.org 2.2.2.2\n"
  34. "MapAddress www.google.com 3.3.3.3\n"
  35. "MapAddress www.example.org 4.4.4.4\n"
  36. "MapAddress 4.4.4.4 7.7.7.7\n"
  37. "MapAddress 4.4.4.4 5.5.5.5\n"
  38. "MapAddress www.infiniteloop.org 6.6.6.6\n"
  39. "MapAddress 6.6.6.6 www.infiniteloop.org\n"
  40. , sizeof(buf));
  41. config_get_lines(buf, &(get_options_mutable()->AddressMap), 0);
  42. config_register_addressmaps(get_options());
  43. /* Use old interface for now, so we don't need to rewrite the unit tests */
  44. #define addressmap_rewrite(a,s,eo,ao) \
  45. addressmap_rewrite((a),(s),AMR_FLAG_USE_IPV4_DNS|AMR_FLAG_USE_IPV6_DNS, \
  46. (eo),(ao))
  47. /* MapAddress .invalidwildcard.com .torserver.exit - no match */
  48. strlcpy(address, "www.invalidwildcard.com", sizeof(address));
  49. test_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  50. /* MapAddress *invalidasterisk.com .torserver.exit - no match */
  51. strlcpy(address, "www.invalidasterisk.com", sizeof(address));
  52. test_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  53. /* Where no mapping for FQDN match on top-level domain */
  54. /* MapAddress .google.com .torserver.exit */
  55. strlcpy(address, "reader.google.com", sizeof(address));
  56. test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  57. test_streq(address, "reader.torserver.exit");
  58. /* MapAddress *.yahoo.com *.google.com.torserver.exit */
  59. strlcpy(address, "reader.yahoo.com", sizeof(address));
  60. test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  61. test_streq(address, "reader.google.com.torserver.exit");
  62. /*MapAddress *.cnn.com www.cnn.com */
  63. strlcpy(address, "cnn.com", sizeof(address));
  64. test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  65. test_streq(address, "www.cnn.com");
  66. /* MapAddress .cn.com www.cnn.com */
  67. strlcpy(address, "www.cn.com", sizeof(address));
  68. test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  69. test_streq(address, "www.cnn.com");
  70. /* MapAddress ex.com www.cnn.com - no match */
  71. strlcpy(address, "www.ex.com", sizeof(address));
  72. test_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  73. /* MapAddress ey.com *.cnn.com - invalid expression */
  74. strlcpy(address, "ey.com", sizeof(address));
  75. test_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  76. /* Where mapping for FQDN match on FQDN */
  77. strlcpy(address, "www.google.com", sizeof(address));
  78. test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  79. test_streq(address, "3.3.3.3");
  80. strlcpy(address, "www.torproject.org", sizeof(address));
  81. test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  82. test_streq(address, "1.1.1.1");
  83. strlcpy(address, "other.torproject.org", sizeof(address));
  84. test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  85. test_streq(address, "this.torproject.org.otherserver.exit");
  86. strlcpy(address, "test.torproject.org", sizeof(address));
  87. test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  88. test_streq(address, "2.2.2.2");
  89. /* Test a chain of address mappings and the order in which they were added:
  90. "MapAddress www.example.org 4.4.4.4"
  91. "MapAddress 4.4.4.4 7.7.7.7"
  92. "MapAddress 4.4.4.4 5.5.5.5"
  93. */
  94. strlcpy(address, "www.example.org", sizeof(address));
  95. test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  96. test_streq(address, "5.5.5.5");
  97. /* Test infinite address mapping results in no change */
  98. strlcpy(address, "www.infiniteloop.org", sizeof(address));
  99. test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  100. test_streq(address, "www.infiniteloop.org");
  101. /* Test we don't find false positives */
  102. strlcpy(address, "www.example.com", sizeof(address));
  103. test_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  104. /* Test top-level-domain matching a bit harder */
  105. addressmap_clear_configured();
  106. strlcpy(buf, "MapAddress *.com *.torserver.exit\n"
  107. "MapAddress *.torproject.org 1.1.1.1\n"
  108. "MapAddress *.net 2.2.2.2\n"
  109. , sizeof(buf));
  110. config_get_lines(buf, &(get_options_mutable()->AddressMap), 0);
  111. config_register_addressmaps(get_options());
  112. strlcpy(address, "www.abc.com", sizeof(address));
  113. test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  114. test_streq(address, "www.abc.torserver.exit");
  115. strlcpy(address, "www.def.com", sizeof(address));
  116. test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  117. test_streq(address, "www.def.torserver.exit");
  118. strlcpy(address, "www.torproject.org", sizeof(address));
  119. test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  120. test_streq(address, "1.1.1.1");
  121. strlcpy(address, "test.torproject.org", sizeof(address));
  122. test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  123. test_streq(address, "1.1.1.1");
  124. strlcpy(address, "torproject.net", sizeof(address));
  125. test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  126. test_streq(address, "2.2.2.2");
  127. /* We don't support '*' as a mapping directive */
  128. addressmap_clear_configured();
  129. strlcpy(buf, "MapAddress * *.torserver.exit\n", sizeof(buf));
  130. config_get_lines(buf, &(get_options_mutable()->AddressMap), 0);
  131. config_register_addressmaps(get_options());
  132. strlcpy(address, "www.abc.com", sizeof(address));
  133. test_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  134. strlcpy(address, "www.def.net", sizeof(address));
  135. test_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  136. strlcpy(address, "www.torproject.org", sizeof(address));
  137. test_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  138. #undef addressmap_rewrite
  139. done:
  140. ;
  141. }
  142. static int
  143. is_private_dir(const char* path)
  144. {
  145. struct stat st;
  146. int r = stat(path, &st);
  147. if (r) {
  148. return 0;
  149. }
  150. #if !defined (_WIN32) || defined (WINCE)
  151. if ((st.st_mode & (S_IFDIR | 0777)) != (S_IFDIR | 0700)) {
  152. return 0;
  153. }
  154. #endif
  155. return 1;
  156. }
  157. static void
  158. test_config_check_or_create_data_subdir(void *arg)
  159. {
  160. or_options_t *options = get_options_mutable();
  161. char *datadir = options->DataDirectory = tor_strdup(get_fname("datadir-0"));
  162. const char *subdir = "test_stats";
  163. char *subpath = get_datadir_fname(subdir);
  164. struct stat st;
  165. int r;
  166. #if !defined (_WIN32) || defined (WINCE)
  167. unsigned group_permission;
  168. #endif
  169. (void)arg;
  170. #if defined (_WIN32) && !defined (WINCE)
  171. tt_int_op(mkdir(options->DataDirectory), ==, 0);
  172. #else
  173. tt_int_op(mkdir(options->DataDirectory, 0700), ==, 0);
  174. #endif
  175. r = stat(subpath, &st);
  176. // The subdirectory shouldn't exist yet,
  177. // but should be created by the call to check_or_create_data_subdir.
  178. test_assert(r && (errno == ENOENT));
  179. test_assert(!check_or_create_data_subdir(subdir));
  180. test_assert(is_private_dir(subpath));
  181. // The check should return 0, if the directory already exists
  182. // and is private to the user.
  183. test_assert(!check_or_create_data_subdir(subdir));
  184. r = stat(subpath, &st);
  185. if (r) {
  186. tt_abort_perror("stat");
  187. }
  188. #if !defined (_WIN32) || defined (WINCE)
  189. group_permission = st.st_mode | 0070;
  190. r = chmod(subpath, group_permission);
  191. if (r) {
  192. tt_abort_perror("chmod");
  193. }
  194. // If the directory exists, but its mode is too permissive
  195. // a call to check_or_create_data_subdir should reset the mode.
  196. test_assert(!is_private_dir(subpath));
  197. test_assert(!check_or_create_data_subdir(subdir));
  198. test_assert(is_private_dir(subpath));
  199. #endif
  200. done:
  201. rmdir(subpath);
  202. tor_free(datadir);
  203. tor_free(subpath);
  204. }
  205. static void
  206. test_config_write_to_data_subdir(void *arg)
  207. {
  208. or_options_t* options = get_options_mutable();
  209. char *datadir = options->DataDirectory = tor_strdup(get_fname("datadir-1"));
  210. char *cp = NULL;
  211. const char* subdir = "test_stats";
  212. const char* fname = "test_file";
  213. const char* str =
  214. "Lorem ipsum dolor sit amet, consetetur sadipscing\n"
  215. "elitr, sed diam nonumy eirmod\n"
  216. "tempor invidunt ut labore et dolore magna aliquyam\n"
  217. "erat, sed diam voluptua.\n"
  218. "At vero eos et accusam et justo duo dolores et ea\n"
  219. "rebum. Stet clita kasd gubergren,\n"
  220. "no sea takimata sanctus est Lorem ipsum dolor sit amet.\n"
  221. "Lorem ipsum dolor sit amet,\n"
  222. "consetetur sadipscing elitr, sed diam nonumy eirmod\n"
  223. "tempor invidunt ut labore et dolore\n"
  224. "magna aliquyam erat, sed diam voluptua. At vero eos et\n"
  225. "accusam et justo duo dolores et\n"
  226. "ea rebum. Stet clita kasd gubergren, no sea takimata\n"
  227. "sanctus est Lorem ipsum dolor sit amet.";
  228. char* filepath = get_datadir_fname2(subdir, fname);
  229. (void)arg;
  230. #if defined (_WIN32) && !defined (WINCE)
  231. tt_int_op(mkdir(options->DataDirectory), ==, 0);
  232. #else
  233. tt_int_op(mkdir(options->DataDirectory, 0700), ==, 0);
  234. #endif
  235. // Write attempt shoudl fail, if subdirectory doesn't exist.
  236. test_assert(write_to_data_subdir(subdir, fname, str, NULL));
  237. test_assert(! check_or_create_data_subdir(subdir));
  238. // Content of file after write attempt should be
  239. // equal to the original string.
  240. test_assert(!write_to_data_subdir(subdir, fname, str, NULL));
  241. cp = read_file_to_str(filepath, 0, NULL);
  242. test_streq(cp, str);
  243. tor_free(cp);
  244. // A second write operation should overwrite the old content.
  245. test_assert(!write_to_data_subdir(subdir, fname, str, NULL));
  246. cp = read_file_to_str(filepath, 0, NULL);
  247. test_streq(cp, str);
  248. tor_free(cp);
  249. done:
  250. (void) unlink(filepath);
  251. rmdir(options->DataDirectory);
  252. tor_free(datadir);
  253. tor_free(filepath);
  254. tor_free(cp);
  255. }
  256. /* Test helper function: Make sure that a bridge line gets parsed
  257. * properly. Also make sure that the resulting bridge_line_t structure
  258. * has its fields set correctly. */
  259. static void
  260. good_bridge_line_test(const char *string, const char *test_addrport,
  261. const char *test_digest, const char *test_transport,
  262. const smartlist_t *test_socks_args)
  263. {
  264. char *tmp = NULL;
  265. bridge_line_t *bridge_line = parse_bridge_line(string);
  266. test_assert(bridge_line);
  267. /* test addrport */
  268. tmp = tor_strdup(fmt_addrport(&bridge_line->addr, bridge_line->port));
  269. test_streq(test_addrport, tmp);
  270. tor_free(tmp);
  271. /* If we were asked to validate a digest, but we did not get a
  272. digest after parsing, we failed. */
  273. if (test_digest && tor_digest_is_zero(bridge_line->digest))
  274. test_assert(0);
  275. /* If we were not asked to validate a digest, and we got a digest
  276. after parsing, we failed again. */
  277. if (!test_digest && !tor_digest_is_zero(bridge_line->digest))
  278. test_assert(0);
  279. /* If we were asked to validate a digest, and we got a digest after
  280. parsing, make sure it's correct. */
  281. if (test_digest) {
  282. tmp = tor_strdup(hex_str(bridge_line->digest, DIGEST_LEN));
  283. tor_strlower(tmp);
  284. test_streq(test_digest, tmp);
  285. tor_free(tmp);
  286. }
  287. /* If we were asked to validate a transport name, make sure tha it
  288. matches with the transport name that was parsed. */
  289. if (test_transport && !bridge_line->transport_name)
  290. test_assert(0);
  291. if (!test_transport && bridge_line->transport_name)
  292. test_assert(0);
  293. if (test_transport)
  294. test_streq(test_transport, bridge_line->transport_name);
  295. /* Validate the SOCKS argument smartlist. */
  296. if (test_socks_args && !bridge_line->socks_args)
  297. test_assert(0);
  298. if (!test_socks_args && bridge_line->socks_args)
  299. test_assert(0);
  300. if (test_socks_args)
  301. test_assert(smartlist_strings_eq(test_socks_args,
  302. bridge_line->socks_args));
  303. done:
  304. tor_free(tmp);
  305. bridge_line_free(bridge_line);
  306. }
  307. /* Test helper function: Make sure that a bridge line is
  308. * unparseable. */
  309. static void
  310. bad_bridge_line_test(const char *string)
  311. {
  312. bridge_line_t *bridge_line = parse_bridge_line(string);
  313. if (bridge_line)
  314. TT_FAIL(("%s was supposed to fail, but it didn't.", string));
  315. test_assert(!bridge_line);
  316. done:
  317. bridge_line_free(bridge_line);
  318. }
  319. static void
  320. test_config_parse_bridge_line(void *arg)
  321. {
  322. (void) arg;
  323. good_bridge_line_test("192.0.2.1:4123",
  324. "192.0.2.1:4123", NULL, NULL, NULL);
  325. good_bridge_line_test("192.0.2.1",
  326. "192.0.2.1:443", NULL, NULL, NULL);
  327. good_bridge_line_test("transport [::1]",
  328. "[::1]:443", NULL, "transport", NULL);
  329. good_bridge_line_test("transport 192.0.2.1:12 "
  330. "4352e58420e68f5e40bf7c74faddccd9d1349413",
  331. "192.0.2.1:12",
  332. "4352e58420e68f5e40bf7c74faddccd9d1349413",
  333. "transport", NULL);
  334. {
  335. smartlist_t *sl_tmp = smartlist_new();
  336. smartlist_add_asprintf(sl_tmp, "twoandtwo=five");
  337. good_bridge_line_test("transport 192.0.2.1:12 "
  338. "4352e58420e68f5e40bf7c74faddccd9d1349413 twoandtwo=five",
  339. "192.0.2.1:12", "4352e58420e68f5e40bf7c74faddccd9d1349413",
  340. "transport", sl_tmp);
  341. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  342. smartlist_free(sl_tmp);
  343. }
  344. {
  345. smartlist_t *sl_tmp = smartlist_new();
  346. smartlist_add_asprintf(sl_tmp, "twoandtwo=five");
  347. smartlist_add_asprintf(sl_tmp, "z=z");
  348. good_bridge_line_test("transport 192.0.2.1:12 twoandtwo=five z=z",
  349. "192.0.2.1:12", NULL, "transport", sl_tmp);
  350. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  351. smartlist_free(sl_tmp);
  352. }
  353. {
  354. smartlist_t *sl_tmp = smartlist_new();
  355. smartlist_add_asprintf(sl_tmp, "dub=come");
  356. smartlist_add_asprintf(sl_tmp, "save=me");
  357. good_bridge_line_test("transport 192.0.2.1:12 "
  358. "4352e58420e68f5e40bf7c74faddccd9d1349666 "
  359. "dub=come save=me",
  360. "192.0.2.1:12",
  361. "4352e58420e68f5e40bf7c74faddccd9d1349666",
  362. "transport", sl_tmp);
  363. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  364. smartlist_free(sl_tmp);
  365. }
  366. good_bridge_line_test("192.0.2.1:1231 "
  367. "4352e58420e68f5e40bf7c74faddccd9d1349413",
  368. "192.0.2.1:1231",
  369. "4352e58420e68f5e40bf7c74faddccd9d1349413",
  370. NULL, NULL);
  371. /* Empty line */
  372. bad_bridge_line_test("");
  373. /* bad transport name */
  374. bad_bridge_line_test("tr$n_sp0r7 190.20.2.2");
  375. /* weird ip address */
  376. bad_bridge_line_test("a.b.c.d");
  377. /* invalid fpr */
  378. bad_bridge_line_test("2.2.2.2:1231 4352e58420e68f5e40bf7c74faddccd9d1349");
  379. /* no k=v in the end */
  380. bad_bridge_line_test("obfs2 2.2.2.2:1231 "
  381. "4352e58420e68f5e40bf7c74faddccd9d1349413 what");
  382. /* no addrport */
  383. bad_bridge_line_test("asdw");
  384. /* huge k=v value that can't fit in SOCKS fields */
  385. bad_bridge_line_test(
  386. "obfs2 2.2.2.2:1231 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  387. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  388. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  389. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  390. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  391. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  392. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  393. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  394. "aa=b");
  395. }
  396. static void
  397. test_config_parse_transport_options_line(void *arg)
  398. {
  399. smartlist_t *options_sl = NULL, *sl_tmp = NULL;
  400. (void) arg;
  401. { /* too small line */
  402. options_sl = get_options_from_transport_options_line("valley", NULL);
  403. test_assert(!options_sl);
  404. }
  405. { /* no k=v values */
  406. options_sl = get_options_from_transport_options_line("hit it!", NULL);
  407. test_assert(!options_sl);
  408. }
  409. { /* correct line, but wrong transport specified */
  410. options_sl =
  411. get_options_from_transport_options_line("trebuchet k=v", "rook");
  412. test_assert(!options_sl);
  413. }
  414. { /* correct -- no transport specified */
  415. sl_tmp = smartlist_new();
  416. smartlist_add_asprintf(sl_tmp, "ladi=dadi");
  417. smartlist_add_asprintf(sl_tmp, "weliketo=party");
  418. options_sl =
  419. get_options_from_transport_options_line("rook ladi=dadi weliketo=party",
  420. NULL);
  421. test_assert(options_sl);
  422. test_assert(smartlist_strings_eq(options_sl, sl_tmp));
  423. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  424. smartlist_free(sl_tmp);
  425. sl_tmp = NULL;
  426. SMARTLIST_FOREACH(options_sl, char *, s, tor_free(s));
  427. smartlist_free(options_sl);
  428. options_sl = NULL;
  429. }
  430. { /* correct -- correct transport specified */
  431. sl_tmp = smartlist_new();
  432. smartlist_add_asprintf(sl_tmp, "ladi=dadi");
  433. smartlist_add_asprintf(sl_tmp, "weliketo=party");
  434. options_sl =
  435. get_options_from_transport_options_line("rook ladi=dadi weliketo=party",
  436. "rook");
  437. test_assert(options_sl);
  438. test_assert(smartlist_strings_eq(options_sl, sl_tmp));
  439. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  440. smartlist_free(sl_tmp);
  441. sl_tmp = NULL;
  442. SMARTLIST_FOREACH(options_sl, char *, s, tor_free(s));
  443. smartlist_free(options_sl);
  444. options_sl = NULL;
  445. }
  446. done:
  447. if (options_sl) {
  448. SMARTLIST_FOREACH(options_sl, char *, s, tor_free(s));
  449. smartlist_free(options_sl);
  450. }
  451. if (sl_tmp) {
  452. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  453. smartlist_free(sl_tmp);
  454. }
  455. }
  456. // Tests if an options with MyFamily fingerprints missing '$' normalises
  457. // them correctly and also ensure it also works with multiple fingerprints
  458. static void
  459. test_config_fix_my_family(void *arg)
  460. {
  461. char *err = NULL;
  462. const char *family = "$1111111111111111111111111111111111111111, "
  463. "1111111111111111111111111111111111111112, "
  464. "$1111111111111111111111111111111111111113";
  465. or_options_t* options = options_new();
  466. or_options_t* defaults = options_new();
  467. (void) arg;
  468. options_init(options);
  469. options_init(defaults);
  470. options->MyFamily = tor_strdup(family);
  471. options_validate(NULL, options, defaults, 0, &err) ;
  472. if (err != NULL) {
  473. TT_FAIL(("options_validate failed: %s", err));
  474. }
  475. test_streq(options->MyFamily, "$1111111111111111111111111111111111111111, "
  476. "$1111111111111111111111111111111111111112, "
  477. "$1111111111111111111111111111111111111113");
  478. done:
  479. if (err != NULL) {
  480. tor_free(err);
  481. }
  482. or_options_free(options);
  483. or_options_free(defaults);
  484. }
  485. #define CONFIG_TEST(name, flags) \
  486. { #name, test_config_ ## name, flags, NULL, NULL }
  487. struct testcase_t config_tests[] = {
  488. CONFIG_TEST(addressmap, 0),
  489. CONFIG_TEST(parse_bridge_line, 0),
  490. CONFIG_TEST(parse_transport_options_line, 0),
  491. CONFIG_TEST(check_or_create_data_subdir, TT_FORK),
  492. CONFIG_TEST(write_to_data_subdir, TT_FORK),
  493. CONFIG_TEST(fix_my_family, 0),
  494. END_OF_TESTCASES
  495. };