test_config.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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 = NULL;
  235. (void)arg;
  236. tor_free(options->DataDirectory);
  237. datadir = options->DataDirectory = tor_strdup(get_fname("datadir-1"));
  238. filepath = get_datadir_fname2(subdir, fname);
  239. #if defined (_WIN32) && !defined (WINCE)
  240. tt_int_op(mkdir(options->DataDirectory), ==, 0);
  241. #else
  242. tt_int_op(mkdir(options->DataDirectory, 0700), ==, 0);
  243. #endif
  244. // Write attempt shoudl fail, if subdirectory doesn't exist.
  245. test_assert(write_to_data_subdir(subdir, fname, str, NULL));
  246. test_assert(! check_or_create_data_subdir(subdir));
  247. // Content of file after write attempt should be
  248. // equal to the original string.
  249. test_assert(!write_to_data_subdir(subdir, fname, str, NULL));
  250. cp = read_file_to_str(filepath, 0, NULL);
  251. test_streq(cp, str);
  252. tor_free(cp);
  253. // A second write operation should overwrite the old content.
  254. test_assert(!write_to_data_subdir(subdir, fname, str, NULL));
  255. cp = read_file_to_str(filepath, 0, NULL);
  256. test_streq(cp, str);
  257. tor_free(cp);
  258. done:
  259. (void) unlink(filepath);
  260. rmdir(options->DataDirectory);
  261. tor_free(datadir);
  262. tor_free(filepath);
  263. tor_free(cp);
  264. }
  265. /* Test helper function: Make sure that a bridge line gets parsed
  266. * properly. Also make sure that the resulting bridge_line_t structure
  267. * has its fields set correctly. */
  268. static void
  269. good_bridge_line_test(const char *string, const char *test_addrport,
  270. const char *test_digest, const char *test_transport,
  271. const smartlist_t *test_socks_args)
  272. {
  273. char *tmp = NULL;
  274. bridge_line_t *bridge_line = parse_bridge_line(string);
  275. test_assert(bridge_line);
  276. /* test addrport */
  277. tmp = tor_strdup(fmt_addrport(&bridge_line->addr, bridge_line->port));
  278. test_streq(test_addrport, tmp);
  279. tor_free(tmp);
  280. /* If we were asked to validate a digest, but we did not get a
  281. digest after parsing, we failed. */
  282. if (test_digest && tor_digest_is_zero(bridge_line->digest))
  283. test_assert(0);
  284. /* If we were not asked to validate a digest, and we got a digest
  285. after parsing, we failed again. */
  286. if (!test_digest && !tor_digest_is_zero(bridge_line->digest))
  287. test_assert(0);
  288. /* If we were asked to validate a digest, and we got a digest after
  289. parsing, make sure it's correct. */
  290. if (test_digest) {
  291. tmp = tor_strdup(hex_str(bridge_line->digest, DIGEST_LEN));
  292. tor_strlower(tmp);
  293. test_streq(test_digest, tmp);
  294. tor_free(tmp);
  295. }
  296. /* If we were asked to validate a transport name, make sure tha it
  297. matches with the transport name that was parsed. */
  298. if (test_transport && !bridge_line->transport_name)
  299. test_assert(0);
  300. if (!test_transport && bridge_line->transport_name)
  301. test_assert(0);
  302. if (test_transport)
  303. test_streq(test_transport, bridge_line->transport_name);
  304. /* Validate the SOCKS argument smartlist. */
  305. if (test_socks_args && !bridge_line->socks_args)
  306. test_assert(0);
  307. if (!test_socks_args && bridge_line->socks_args)
  308. test_assert(0);
  309. if (test_socks_args)
  310. test_assert(smartlist_strings_eq(test_socks_args,
  311. bridge_line->socks_args));
  312. done:
  313. tor_free(tmp);
  314. bridge_line_free(bridge_line);
  315. }
  316. /* Test helper function: Make sure that a bridge line is
  317. * unparseable. */
  318. static void
  319. bad_bridge_line_test(const char *string)
  320. {
  321. bridge_line_t *bridge_line = parse_bridge_line(string);
  322. if (bridge_line)
  323. TT_FAIL(("%s was supposed to fail, but it didn't.", string));
  324. test_assert(!bridge_line);
  325. done:
  326. bridge_line_free(bridge_line);
  327. }
  328. static void
  329. test_config_parse_bridge_line(void *arg)
  330. {
  331. (void) arg;
  332. good_bridge_line_test("192.0.2.1:4123",
  333. "192.0.2.1:4123", NULL, NULL, NULL);
  334. good_bridge_line_test("192.0.2.1",
  335. "192.0.2.1:443", NULL, NULL, NULL);
  336. good_bridge_line_test("transport [::1]",
  337. "[::1]:443", NULL, "transport", NULL);
  338. good_bridge_line_test("transport 192.0.2.1:12 "
  339. "4352e58420e68f5e40bf7c74faddccd9d1349413",
  340. "192.0.2.1:12",
  341. "4352e58420e68f5e40bf7c74faddccd9d1349413",
  342. "transport", NULL);
  343. {
  344. smartlist_t *sl_tmp = smartlist_new();
  345. smartlist_add_asprintf(sl_tmp, "twoandtwo=five");
  346. good_bridge_line_test("transport 192.0.2.1:12 "
  347. "4352e58420e68f5e40bf7c74faddccd9d1349413 twoandtwo=five",
  348. "192.0.2.1:12", "4352e58420e68f5e40bf7c74faddccd9d1349413",
  349. "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, "twoandtwo=five");
  356. smartlist_add_asprintf(sl_tmp, "z=z");
  357. good_bridge_line_test("transport 192.0.2.1:12 twoandtwo=five z=z",
  358. "192.0.2.1:12", NULL, "transport", sl_tmp);
  359. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  360. smartlist_free(sl_tmp);
  361. }
  362. {
  363. smartlist_t *sl_tmp = smartlist_new();
  364. smartlist_add_asprintf(sl_tmp, "dub=come");
  365. smartlist_add_asprintf(sl_tmp, "save=me");
  366. good_bridge_line_test("transport 192.0.2.1:12 "
  367. "4352e58420e68f5e40bf7c74faddccd9d1349666 "
  368. "dub=come save=me",
  369. "192.0.2.1:12",
  370. "4352e58420e68f5e40bf7c74faddccd9d1349666",
  371. "transport", sl_tmp);
  372. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  373. smartlist_free(sl_tmp);
  374. }
  375. good_bridge_line_test("192.0.2.1:1231 "
  376. "4352e58420e68f5e40bf7c74faddccd9d1349413",
  377. "192.0.2.1:1231",
  378. "4352e58420e68f5e40bf7c74faddccd9d1349413",
  379. NULL, NULL);
  380. /* Empty line */
  381. bad_bridge_line_test("");
  382. /* bad transport name */
  383. bad_bridge_line_test("tr$n_sp0r7 190.20.2.2");
  384. /* weird ip address */
  385. bad_bridge_line_test("a.b.c.d");
  386. /* invalid fpr */
  387. bad_bridge_line_test("2.2.2.2:1231 4352e58420e68f5e40bf7c74faddccd9d1349");
  388. /* no k=v in the end */
  389. bad_bridge_line_test("obfs2 2.2.2.2:1231 "
  390. "4352e58420e68f5e40bf7c74faddccd9d1349413 what");
  391. /* no addrport */
  392. bad_bridge_line_test("asdw");
  393. /* huge k=v value that can't fit in SOCKS fields */
  394. bad_bridge_line_test(
  395. "obfs2 2.2.2.2:1231 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  396. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  397. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  398. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  399. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  400. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  401. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  402. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  403. "aa=b");
  404. }
  405. static void
  406. test_config_parse_transport_options_line(void *arg)
  407. {
  408. smartlist_t *options_sl = NULL, *sl_tmp = NULL;
  409. (void) arg;
  410. { /* too small line */
  411. options_sl = get_options_from_transport_options_line("valley", NULL);
  412. test_assert(!options_sl);
  413. }
  414. { /* no k=v values */
  415. options_sl = get_options_from_transport_options_line("hit it!", NULL);
  416. test_assert(!options_sl);
  417. }
  418. { /* correct line, but wrong transport specified */
  419. options_sl =
  420. get_options_from_transport_options_line("trebuchet k=v", "rook");
  421. test_assert(!options_sl);
  422. }
  423. { /* correct -- no transport specified */
  424. sl_tmp = smartlist_new();
  425. smartlist_add_asprintf(sl_tmp, "ladi=dadi");
  426. smartlist_add_asprintf(sl_tmp, "weliketo=party");
  427. options_sl =
  428. get_options_from_transport_options_line("rook ladi=dadi weliketo=party",
  429. NULL);
  430. test_assert(options_sl);
  431. test_assert(smartlist_strings_eq(options_sl, sl_tmp));
  432. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  433. smartlist_free(sl_tmp);
  434. sl_tmp = NULL;
  435. SMARTLIST_FOREACH(options_sl, char *, s, tor_free(s));
  436. smartlist_free(options_sl);
  437. options_sl = NULL;
  438. }
  439. { /* correct -- correct transport specified */
  440. sl_tmp = smartlist_new();
  441. smartlist_add_asprintf(sl_tmp, "ladi=dadi");
  442. smartlist_add_asprintf(sl_tmp, "weliketo=party");
  443. options_sl =
  444. get_options_from_transport_options_line("rook ladi=dadi weliketo=party",
  445. "rook");
  446. test_assert(options_sl);
  447. test_assert(smartlist_strings_eq(options_sl, sl_tmp));
  448. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  449. smartlist_free(sl_tmp);
  450. sl_tmp = NULL;
  451. SMARTLIST_FOREACH(options_sl, char *, s, tor_free(s));
  452. smartlist_free(options_sl);
  453. options_sl = NULL;
  454. }
  455. done:
  456. if (options_sl) {
  457. SMARTLIST_FOREACH(options_sl, char *, s, tor_free(s));
  458. smartlist_free(options_sl);
  459. }
  460. if (sl_tmp) {
  461. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  462. smartlist_free(sl_tmp);
  463. }
  464. }
  465. // Tests if an options with MyFamily fingerprints missing '$' normalises
  466. // them correctly and also ensure it also works with multiple fingerprints
  467. static void
  468. test_config_fix_my_family(void *arg)
  469. {
  470. char *err = NULL;
  471. const char *family = "$1111111111111111111111111111111111111111, "
  472. "1111111111111111111111111111111111111112, "
  473. "$1111111111111111111111111111111111111113";
  474. or_options_t* options = options_new();
  475. or_options_t* defaults = options_new();
  476. (void) arg;
  477. options_init(options);
  478. options_init(defaults);
  479. options->MyFamily = tor_strdup(family);
  480. options_validate(NULL, options, defaults, 0, &err) ;
  481. if (err != NULL) {
  482. TT_FAIL(("options_validate failed: %s", err));
  483. }
  484. test_streq(options->MyFamily, "$1111111111111111111111111111111111111111, "
  485. "$1111111111111111111111111111111111111112, "
  486. "$1111111111111111111111111111111111111113");
  487. done:
  488. if (err != NULL) {
  489. tor_free(err);
  490. }
  491. or_options_free(options);
  492. or_options_free(defaults);
  493. }
  494. #define CONFIG_TEST(name, flags) \
  495. { #name, test_config_ ## name, flags, NULL, NULL }
  496. struct testcase_t config_tests[] = {
  497. CONFIG_TEST(addressmap, 0),
  498. CONFIG_TEST(parse_bridge_line, 0),
  499. CONFIG_TEST(parse_transport_options_line, 0),
  500. CONFIG_TEST(check_or_create_data_subdir, TT_FORK),
  501. CONFIG_TEST(write_to_data_subdir, TT_FORK),
  502. CONFIG_TEST(fix_my_family, 0),
  503. END_OF_TESTCASES
  504. };