test_config.c 20 KB

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