test_config.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2014, 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. #include "entrynodes.h"
  16. #include "transports.h"
  17. static void
  18. test_config_addressmap(void *arg)
  19. {
  20. char buf[1024];
  21. char address[256];
  22. time_t expires = TIME_MAX;
  23. (void)arg;
  24. strlcpy(buf, "MapAddress .invalidwildcard.com *.torserver.exit\n" // invalid
  25. "MapAddress *invalidasterisk.com *.torserver.exit\n" // invalid
  26. "MapAddress *.google.com *.torserver.exit\n"
  27. "MapAddress *.yahoo.com *.google.com.torserver.exit\n"
  28. "MapAddress *.cn.com www.cnn.com\n"
  29. "MapAddress *.cnn.com www.cnn.com\n"
  30. "MapAddress ex.com www.cnn.com\n"
  31. "MapAddress ey.com *.cnn.com\n"
  32. "MapAddress www.torproject.org 1.1.1.1\n"
  33. "MapAddress other.torproject.org "
  34. "this.torproject.org.otherserver.exit\n"
  35. "MapAddress test.torproject.org 2.2.2.2\n"
  36. "MapAddress www.google.com 3.3.3.3\n"
  37. "MapAddress www.example.org 4.4.4.4\n"
  38. "MapAddress 4.4.4.4 7.7.7.7\n"
  39. "MapAddress 4.4.4.4 5.5.5.5\n"
  40. "MapAddress www.infiniteloop.org 6.6.6.6\n"
  41. "MapAddress 6.6.6.6 www.infiniteloop.org\n"
  42. , sizeof(buf));
  43. config_get_lines(buf, &(get_options_mutable()->AddressMap), 0);
  44. config_register_addressmaps(get_options());
  45. /* Use old interface for now, so we don't need to rewrite the unit tests */
  46. #define addressmap_rewrite(a,s,eo,ao) \
  47. addressmap_rewrite((a),(s),AMR_FLAG_USE_IPV4_DNS|AMR_FLAG_USE_IPV6_DNS, \
  48. (eo),(ao))
  49. /* MapAddress .invalidwildcard.com .torserver.exit - no match */
  50. strlcpy(address, "www.invalidwildcard.com", sizeof(address));
  51. tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  52. /* MapAddress *invalidasterisk.com .torserver.exit - no match */
  53. strlcpy(address, "www.invalidasterisk.com", sizeof(address));
  54. tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  55. /* Where no mapping for FQDN match on top-level domain */
  56. /* MapAddress .google.com .torserver.exit */
  57. strlcpy(address, "reader.google.com", sizeof(address));
  58. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  59. tt_str_op(address,==, "reader.torserver.exit");
  60. /* MapAddress *.yahoo.com *.google.com.torserver.exit */
  61. strlcpy(address, "reader.yahoo.com", sizeof(address));
  62. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  63. tt_str_op(address,==, "reader.google.com.torserver.exit");
  64. /*MapAddress *.cnn.com www.cnn.com */
  65. strlcpy(address, "cnn.com", sizeof(address));
  66. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  67. tt_str_op(address,==, "www.cnn.com");
  68. /* MapAddress .cn.com www.cnn.com */
  69. strlcpy(address, "www.cn.com", sizeof(address));
  70. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  71. tt_str_op(address,==, "www.cnn.com");
  72. /* MapAddress ex.com www.cnn.com - no match */
  73. strlcpy(address, "www.ex.com", sizeof(address));
  74. tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  75. /* MapAddress ey.com *.cnn.com - invalid expression */
  76. strlcpy(address, "ey.com", sizeof(address));
  77. tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  78. /* Where mapping for FQDN match on FQDN */
  79. strlcpy(address, "www.google.com", sizeof(address));
  80. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  81. tt_str_op(address,==, "3.3.3.3");
  82. strlcpy(address, "www.torproject.org", sizeof(address));
  83. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  84. tt_str_op(address,==, "1.1.1.1");
  85. strlcpy(address, "other.torproject.org", sizeof(address));
  86. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  87. tt_str_op(address,==, "this.torproject.org.otherserver.exit");
  88. strlcpy(address, "test.torproject.org", sizeof(address));
  89. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  90. tt_str_op(address,==, "2.2.2.2");
  91. /* Test a chain of address mappings and the order in which they were added:
  92. "MapAddress www.example.org 4.4.4.4"
  93. "MapAddress 4.4.4.4 7.7.7.7"
  94. "MapAddress 4.4.4.4 5.5.5.5"
  95. */
  96. strlcpy(address, "www.example.org", sizeof(address));
  97. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  98. tt_str_op(address,==, "5.5.5.5");
  99. /* Test infinite address mapping results in no change */
  100. strlcpy(address, "www.infiniteloop.org", sizeof(address));
  101. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  102. tt_str_op(address,==, "www.infiniteloop.org");
  103. /* Test we don't find false positives */
  104. strlcpy(address, "www.example.com", sizeof(address));
  105. tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  106. /* Test top-level-domain matching a bit harder */
  107. config_free_lines(get_options_mutable()->AddressMap);
  108. addressmap_clear_configured();
  109. strlcpy(buf, "MapAddress *.com *.torserver.exit\n"
  110. "MapAddress *.torproject.org 1.1.1.1\n"
  111. "MapAddress *.net 2.2.2.2\n"
  112. , sizeof(buf));
  113. config_get_lines(buf, &(get_options_mutable()->AddressMap), 0);
  114. config_register_addressmaps(get_options());
  115. strlcpy(address, "www.abc.com", sizeof(address));
  116. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  117. tt_str_op(address,==, "www.abc.torserver.exit");
  118. strlcpy(address, "www.def.com", sizeof(address));
  119. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  120. tt_str_op(address,==, "www.def.torserver.exit");
  121. strlcpy(address, "www.torproject.org", sizeof(address));
  122. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  123. tt_str_op(address,==, "1.1.1.1");
  124. strlcpy(address, "test.torproject.org", sizeof(address));
  125. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  126. tt_str_op(address,==, "1.1.1.1");
  127. strlcpy(address, "torproject.net", sizeof(address));
  128. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  129. tt_str_op(address,==, "2.2.2.2");
  130. /* We don't support '*' as a mapping directive */
  131. config_free_lines(get_options_mutable()->AddressMap);
  132. addressmap_clear_configured();
  133. strlcpy(buf, "MapAddress * *.torserver.exit\n", sizeof(buf));
  134. config_get_lines(buf, &(get_options_mutable()->AddressMap), 0);
  135. config_register_addressmaps(get_options());
  136. strlcpy(address, "www.abc.com", sizeof(address));
  137. tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  138. strlcpy(address, "www.def.net", sizeof(address));
  139. tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  140. strlcpy(address, "www.torproject.org", sizeof(address));
  141. tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  142. #undef addressmap_rewrite
  143. done:
  144. config_free_lines(get_options_mutable()->AddressMap);
  145. get_options_mutable()->AddressMap = NULL;
  146. }
  147. static int
  148. is_private_dir(const char* path)
  149. {
  150. struct stat st;
  151. int r = stat(path, &st);
  152. if (r) {
  153. return 0;
  154. }
  155. #if !defined (_WIN32)
  156. if ((st.st_mode & (S_IFDIR | 0777)) != (S_IFDIR | 0700)) {
  157. return 0;
  158. }
  159. #endif
  160. return 1;
  161. }
  162. static void
  163. test_config_check_or_create_data_subdir(void *arg)
  164. {
  165. or_options_t *options = get_options_mutable();
  166. char *datadir;
  167. const char *subdir = "test_stats";
  168. char *subpath;
  169. struct stat st;
  170. int r;
  171. #if !defined (_WIN32)
  172. unsigned group_permission;
  173. #endif
  174. (void)arg;
  175. tor_free(options->DataDirectory);
  176. datadir = options->DataDirectory = tor_strdup(get_fname("datadir-0"));
  177. subpath = get_datadir_fname(subdir);
  178. #if defined (_WIN32)
  179. tt_int_op(mkdir(options->DataDirectory), ==, 0);
  180. #else
  181. tt_int_op(mkdir(options->DataDirectory, 0700), ==, 0);
  182. #endif
  183. r = stat(subpath, &st);
  184. // The subdirectory shouldn't exist yet,
  185. // but should be created by the call to check_or_create_data_subdir.
  186. tt_assert(r && (errno == ENOENT));
  187. tt_assert(!check_or_create_data_subdir(subdir));
  188. tt_assert(is_private_dir(subpath));
  189. // The check should return 0, if the directory already exists
  190. // and is private to the user.
  191. tt_assert(!check_or_create_data_subdir(subdir));
  192. r = stat(subpath, &st);
  193. if (r) {
  194. tt_abort_perror("stat");
  195. }
  196. #if !defined (_WIN32)
  197. group_permission = st.st_mode | 0070;
  198. r = chmod(subpath, group_permission);
  199. if (r) {
  200. tt_abort_perror("chmod");
  201. }
  202. // If the directory exists, but its mode is too permissive
  203. // a call to check_or_create_data_subdir should reset the mode.
  204. tt_assert(!is_private_dir(subpath));
  205. tt_assert(!check_or_create_data_subdir(subdir));
  206. tt_assert(is_private_dir(subpath));
  207. #endif
  208. done:
  209. rmdir(subpath);
  210. tor_free(datadir);
  211. tor_free(subpath);
  212. }
  213. static void
  214. test_config_write_to_data_subdir(void *arg)
  215. {
  216. or_options_t* options = get_options_mutable();
  217. char *datadir;
  218. char *cp = NULL;
  219. const char* subdir = "test_stats";
  220. const char* fname = "test_file";
  221. const char* str =
  222. "Lorem ipsum dolor sit amet, consetetur sadipscing\n"
  223. "elitr, sed diam nonumy eirmod\n"
  224. "tempor invidunt ut labore et dolore magna aliquyam\n"
  225. "erat, sed diam voluptua.\n"
  226. "At vero eos et accusam et justo duo dolores et ea\n"
  227. "rebum. Stet clita kasd gubergren,\n"
  228. "no sea takimata sanctus est Lorem ipsum dolor sit amet.\n"
  229. "Lorem ipsum dolor sit amet,\n"
  230. "consetetur sadipscing elitr, sed diam nonumy eirmod\n"
  231. "tempor invidunt ut labore et dolore\n"
  232. "magna aliquyam erat, sed diam voluptua. At vero eos et\n"
  233. "accusam et justo duo dolores et\n"
  234. "ea rebum. Stet clita kasd gubergren, no sea takimata\n"
  235. "sanctus est Lorem ipsum dolor sit amet.";
  236. char* filepath = NULL;
  237. (void)arg;
  238. tor_free(options->DataDirectory);
  239. datadir = options->DataDirectory = tor_strdup(get_fname("datadir-1"));
  240. filepath = get_datadir_fname2(subdir, fname);
  241. #if defined (_WIN32)
  242. tt_int_op(mkdir(options->DataDirectory), ==, 0);
  243. #else
  244. tt_int_op(mkdir(options->DataDirectory, 0700), ==, 0);
  245. #endif
  246. // Write attempt shoudl fail, if subdirectory doesn't exist.
  247. tt_assert(write_to_data_subdir(subdir, fname, str, NULL));
  248. tt_assert(! check_or_create_data_subdir(subdir));
  249. // Content of file after write attempt should be
  250. // equal to the original string.
  251. tt_assert(!write_to_data_subdir(subdir, fname, str, NULL));
  252. cp = read_file_to_str(filepath, 0, NULL);
  253. tt_str_op(cp,==, str);
  254. tor_free(cp);
  255. // A second write operation should overwrite the old content.
  256. tt_assert(!write_to_data_subdir(subdir, fname, str, NULL));
  257. cp = read_file_to_str(filepath, 0, NULL);
  258. tt_str_op(cp,==, str);
  259. tor_free(cp);
  260. done:
  261. (void) unlink(filepath);
  262. rmdir(options->DataDirectory);
  263. tor_free(datadir);
  264. tor_free(filepath);
  265. tor_free(cp);
  266. }
  267. /* Test helper function: Make sure that a bridge line gets parsed
  268. * properly. Also make sure that the resulting bridge_line_t structure
  269. * has its fields set correctly. */
  270. static void
  271. good_bridge_line_test(const char *string, const char *test_addrport,
  272. const char *test_digest, const char *test_transport,
  273. const smartlist_t *test_socks_args)
  274. {
  275. char *tmp = NULL;
  276. bridge_line_t *bridge_line = parse_bridge_line(string);
  277. tt_assert(bridge_line);
  278. /* test addrport */
  279. tmp = tor_strdup(fmt_addrport(&bridge_line->addr, bridge_line->port));
  280. tt_str_op(test_addrport,==, tmp);
  281. tor_free(tmp);
  282. /* If we were asked to validate a digest, but we did not get a
  283. digest after parsing, we failed. */
  284. if (test_digest && tor_digest_is_zero(bridge_line->digest))
  285. tt_assert(0);
  286. /* If we were not asked to validate a digest, and we got a digest
  287. after parsing, we failed again. */
  288. if (!test_digest && !tor_digest_is_zero(bridge_line->digest))
  289. tt_assert(0);
  290. /* If we were asked to validate a digest, and we got a digest after
  291. parsing, make sure it's correct. */
  292. if (test_digest) {
  293. tmp = tor_strdup(hex_str(bridge_line->digest, DIGEST_LEN));
  294. tor_strlower(tmp);
  295. tt_str_op(test_digest,==, tmp);
  296. tor_free(tmp);
  297. }
  298. /* If we were asked to validate a transport name, make sure tha it
  299. matches with the transport name that was parsed. */
  300. if (test_transport && !bridge_line->transport_name)
  301. tt_assert(0);
  302. if (!test_transport && bridge_line->transport_name)
  303. tt_assert(0);
  304. if (test_transport)
  305. tt_str_op(test_transport,==, bridge_line->transport_name);
  306. /* Validate the SOCKS argument smartlist. */
  307. if (test_socks_args && !bridge_line->socks_args)
  308. tt_assert(0);
  309. if (!test_socks_args && bridge_line->socks_args)
  310. tt_assert(0);
  311. if (test_socks_args)
  312. tt_assert(smartlist_strings_eq(test_socks_args,
  313. bridge_line->socks_args));
  314. done:
  315. tor_free(tmp);
  316. bridge_line_free(bridge_line);
  317. }
  318. /* Test helper function: Make sure that a bridge line is
  319. * unparseable. */
  320. static void
  321. bad_bridge_line_test(const char *string)
  322. {
  323. bridge_line_t *bridge_line = parse_bridge_line(string);
  324. if (bridge_line)
  325. TT_FAIL(("%s was supposed to fail, but it didn't.", string));
  326. tt_assert(!bridge_line);
  327. done:
  328. bridge_line_free(bridge_line);
  329. }
  330. static void
  331. test_config_parse_bridge_line(void *arg)
  332. {
  333. (void) arg;
  334. good_bridge_line_test("192.0.2.1:4123",
  335. "192.0.2.1:4123", NULL, NULL, NULL);
  336. good_bridge_line_test("192.0.2.1",
  337. "192.0.2.1:443", NULL, NULL, NULL);
  338. good_bridge_line_test("transport [::1]",
  339. "[::1]:443", NULL, "transport", NULL);
  340. good_bridge_line_test("transport 192.0.2.1:12 "
  341. "4352e58420e68f5e40bf7c74faddccd9d1349413",
  342. "192.0.2.1:12",
  343. "4352e58420e68f5e40bf7c74faddccd9d1349413",
  344. "transport", NULL);
  345. {
  346. smartlist_t *sl_tmp = smartlist_new();
  347. smartlist_add_asprintf(sl_tmp, "twoandtwo=five");
  348. good_bridge_line_test("transport 192.0.2.1:12 "
  349. "4352e58420e68f5e40bf7c74faddccd9d1349413 twoandtwo=five",
  350. "192.0.2.1:12", "4352e58420e68f5e40bf7c74faddccd9d1349413",
  351. "transport", sl_tmp);
  352. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  353. smartlist_free(sl_tmp);
  354. }
  355. {
  356. smartlist_t *sl_tmp = smartlist_new();
  357. smartlist_add_asprintf(sl_tmp, "twoandtwo=five");
  358. smartlist_add_asprintf(sl_tmp, "z=z");
  359. good_bridge_line_test("transport 192.0.2.1:12 twoandtwo=five z=z",
  360. "192.0.2.1:12", NULL, "transport", sl_tmp);
  361. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  362. smartlist_free(sl_tmp);
  363. }
  364. {
  365. smartlist_t *sl_tmp = smartlist_new();
  366. smartlist_add_asprintf(sl_tmp, "dub=come");
  367. smartlist_add_asprintf(sl_tmp, "save=me");
  368. good_bridge_line_test("transport 192.0.2.1:12 "
  369. "4352e58420e68f5e40bf7c74faddccd9d1349666 "
  370. "dub=come save=me",
  371. "192.0.2.1:12",
  372. "4352e58420e68f5e40bf7c74faddccd9d1349666",
  373. "transport", sl_tmp);
  374. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  375. smartlist_free(sl_tmp);
  376. }
  377. good_bridge_line_test("192.0.2.1:1231 "
  378. "4352e58420e68f5e40bf7c74faddccd9d1349413",
  379. "192.0.2.1:1231",
  380. "4352e58420e68f5e40bf7c74faddccd9d1349413",
  381. NULL, NULL);
  382. /* Empty line */
  383. bad_bridge_line_test("");
  384. /* bad transport name */
  385. bad_bridge_line_test("tr$n_sp0r7 190.20.2.2");
  386. /* weird ip address */
  387. bad_bridge_line_test("a.b.c.d");
  388. /* invalid fpr */
  389. bad_bridge_line_test("2.2.2.2:1231 4352e58420e68f5e40bf7c74faddccd9d1349");
  390. /* no k=v in the end */
  391. bad_bridge_line_test("obfs2 2.2.2.2:1231 "
  392. "4352e58420e68f5e40bf7c74faddccd9d1349413 what");
  393. /* no addrport */
  394. bad_bridge_line_test("asdw");
  395. /* huge k=v value that can't fit in SOCKS fields */
  396. bad_bridge_line_test(
  397. "obfs2 2.2.2.2:1231 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  398. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  399. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  400. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  401. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  402. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  403. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  404. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  405. "aa=b");
  406. }
  407. static void
  408. test_config_parse_transport_options_line(void *arg)
  409. {
  410. smartlist_t *options_sl = NULL, *sl_tmp = NULL;
  411. (void) arg;
  412. { /* too small line */
  413. options_sl = get_options_from_transport_options_line("valley", NULL);
  414. tt_assert(!options_sl);
  415. }
  416. { /* no k=v values */
  417. options_sl = get_options_from_transport_options_line("hit it!", NULL);
  418. tt_assert(!options_sl);
  419. }
  420. { /* correct line, but wrong transport specified */
  421. options_sl =
  422. get_options_from_transport_options_line("trebuchet k=v", "rook");
  423. tt_assert(!options_sl);
  424. }
  425. { /* correct -- no transport specified */
  426. sl_tmp = smartlist_new();
  427. smartlist_add_asprintf(sl_tmp, "ladi=dadi");
  428. smartlist_add_asprintf(sl_tmp, "weliketo=party");
  429. options_sl =
  430. get_options_from_transport_options_line("rook ladi=dadi weliketo=party",
  431. NULL);
  432. tt_assert(options_sl);
  433. tt_assert(smartlist_strings_eq(options_sl, sl_tmp));
  434. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  435. smartlist_free(sl_tmp);
  436. sl_tmp = NULL;
  437. SMARTLIST_FOREACH(options_sl, char *, s, tor_free(s));
  438. smartlist_free(options_sl);
  439. options_sl = NULL;
  440. }
  441. { /* correct -- correct transport specified */
  442. sl_tmp = smartlist_new();
  443. smartlist_add_asprintf(sl_tmp, "ladi=dadi");
  444. smartlist_add_asprintf(sl_tmp, "weliketo=party");
  445. options_sl =
  446. get_options_from_transport_options_line("rook ladi=dadi weliketo=party",
  447. "rook");
  448. tt_assert(options_sl);
  449. tt_assert(smartlist_strings_eq(options_sl, sl_tmp));
  450. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  451. smartlist_free(sl_tmp);
  452. sl_tmp = NULL;
  453. SMARTLIST_FOREACH(options_sl, char *, s, tor_free(s));
  454. smartlist_free(options_sl);
  455. options_sl = NULL;
  456. }
  457. done:
  458. if (options_sl) {
  459. SMARTLIST_FOREACH(options_sl, char *, s, tor_free(s));
  460. smartlist_free(options_sl);
  461. }
  462. if (sl_tmp) {
  463. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  464. smartlist_free(sl_tmp);
  465. }
  466. }
  467. /* Mocks needed for the transport plugin line test */
  468. static void pt_kickstart_proxy_mock(const smartlist_t *transport_list,
  469. char **proxy_argv, int is_server);
  470. static int transport_add_from_config_mock(const tor_addr_t *addr,
  471. uint16_t port, const char *name,
  472. int socks_ver);
  473. static int transport_is_needed_mock(const char *transport_name);
  474. static int pt_kickstart_proxy_mock_call_count = 0;
  475. static int transport_add_from_config_mock_call_count = 0;
  476. static int transport_is_needed_mock_call_count = 0;
  477. static int transport_is_needed_mock_return = 0;
  478. static void
  479. pt_kickstart_proxy_mock(const smartlist_t *transport_list,
  480. char **proxy_argv, int is_server)
  481. {
  482. ++pt_kickstart_proxy_mock_call_count;
  483. }
  484. static int
  485. transport_add_from_config_mock(const tor_addr_t *addr,
  486. uint16_t port, const char *name,
  487. int socks_ver)
  488. {
  489. ++transport_add_from_config_mock_call_count;
  490. return 0;
  491. }
  492. static int
  493. transport_is_needed_mock(const char *transport_name)
  494. {
  495. ++transport_is_needed_mock_call_count;
  496. return transport_is_needed_mock_return;
  497. }
  498. /**
  499. * Test parsing for the ClientTransportPlugin and ServerTransportPlugin config
  500. * options.
  501. */
  502. static void
  503. test_config_parse_transport_plugin_line(void *arg)
  504. {
  505. or_options_t *options = get_options_mutable();
  506. int r, tmp;
  507. int old_pt_kickstart_proxy_mock_call_count;
  508. int old_transport_add_from_config_mock_call_count;
  509. int old_transport_is_needed_mock_call_count;
  510. /* Bad transport lines - too short */
  511. r = parse_transport_line(options, "bad", 1, 0);
  512. tt_assert(r < 0);
  513. r = parse_transport_line(options, "bad", 1, 1);
  514. tt_assert(r < 0);
  515. r = parse_transport_line(options, "bad bad", 1, 0);
  516. tt_assert(r < 0);
  517. r = parse_transport_line(options, "bad bad", 1, 1);
  518. tt_assert(r < 0);
  519. /* Test transport list parsing */
  520. r = parse_transport_line(options,
  521. "transport_1 exec /usr/bin/fake-transport", 1, 0);
  522. tt_assert(r == 0);
  523. r = parse_transport_line(options,
  524. "transport_1 exec /usr/bin/fake-transport", 1, 1);
  525. tt_assert(r == 0);
  526. r = parse_transport_line(options,
  527. "transport_1,transport_2 exec /usr/bin/fake-transport", 1, 0);
  528. tt_assert(r == 0);
  529. r = parse_transport_line(options,
  530. "transport_1,transport_2 exec /usr/bin/fake-transport", 1, 1);
  531. tt_assert(r == 0);
  532. /* Bad transport identifiers */
  533. r = parse_transport_line(options,
  534. "transport_* exec /usr/bin/fake-transport", 1, 0);
  535. tt_assert(r < 0);
  536. r = parse_transport_line(options,
  537. "transport_* exec /usr/bin/fake-transport", 1, 1);
  538. tt_assert(r < 0);
  539. /* Check SOCKS cases for client transport */
  540. r = parse_transport_line(options,
  541. "transport_1 socks4 1.2.3.4:567", 1, 0);
  542. tt_assert(r == 0);
  543. r = parse_transport_line(options,
  544. "transport_1 socks5 1.2.3.4:567", 1, 0);
  545. tt_assert(r == 0);
  546. /* Proxy case for server transport */
  547. r = parse_transport_line(options,
  548. "transport_1 proxy 1.2.3.4:567", 1, 1);
  549. tt_assert(r == 0);
  550. /* Multiple-transport error exit */
  551. r = parse_transport_line(options,
  552. "transport_1,transport_2 socks5 1.2.3.4:567", 1, 0);
  553. tt_assert(r < 0);
  554. r = parse_transport_line(options,
  555. "transport_1,transport_2 proxy 1.2.3.4:567", 1, 1);
  556. /* No port error exit */
  557. r = parse_transport_line(options,
  558. "transport_1 socks5 1.2.3.4", 1, 0);
  559. tt_assert(r < 0);
  560. r = parse_transport_line(options,
  561. "transport_1 proxy 1.2.3.4", 1, 1);
  562. tt_assert(r < 0);
  563. /* Unparsable address error exit */
  564. r = parse_transport_line(options,
  565. "transport_1 socks5 1.2.3:6x7", 1, 0);
  566. tt_assert(r < 0);
  567. r = parse_transport_line(options,
  568. "transport_1 proxy 1.2.3:6x7", 1, 1);
  569. tt_assert(r < 0);
  570. /* "Strange {Client|Server}TransportPlugin field" error exit */
  571. r = parse_transport_line(options,
  572. "transport_1 foo bar", 1, 0);
  573. tt_assert(r < 0);
  574. r = parse_transport_line(options,
  575. "transport_1 foo bar", 1, 1);
  576. tt_assert(r < 0);
  577. /* No sandbox mode error exit */
  578. tmp = options->Sandbox;
  579. options->Sandbox = 1;
  580. r = parse_transport_line(options,
  581. "transport_1 exec /usr/bin/fake-transport", 1, 0);
  582. tt_assert(r < 0);
  583. r = parse_transport_line(options,
  584. "transport_1 exec /usr/bin/fake-transport", 1, 1);
  585. tt_assert(r < 0);
  586. options->Sandbox = tmp;
  587. /*
  588. * These final test cases cover code paths that only activate without
  589. * validate_only, so they need mocks in place.
  590. */
  591. MOCK(pt_kickstart_proxy, pt_kickstart_proxy_mock);
  592. old_pt_kickstart_proxy_mock_call_count =
  593. pt_kickstart_proxy_mock_call_count;
  594. r = parse_transport_line(options,
  595. "transport_1 exec /usr/bin/fake-transport", 0, 1);
  596. tt_assert(r == 0);
  597. tt_assert(pt_kickstart_proxy_mock_call_count ==
  598. old_pt_kickstart_proxy_mock_call_count + 1);
  599. UNMOCK(pt_kickstart_proxy);
  600. /* This one hits a log line in the !validate_only case only */
  601. r = parse_transport_line(options,
  602. "transport_1 proxy 1.2.3.4:567", 0, 1);
  603. tt_assert(r == 0);
  604. /* Check mocked client transport cases */
  605. MOCK(pt_kickstart_proxy, pt_kickstart_proxy_mock);
  606. MOCK(transport_add_from_config, transport_add_from_config_mock);
  607. MOCK(transport_is_needed, transport_is_needed_mock);
  608. /* Unnecessary transport case */
  609. transport_is_needed_mock_return = 0;
  610. old_pt_kickstart_proxy_mock_call_count =
  611. pt_kickstart_proxy_mock_call_count;
  612. old_transport_add_from_config_mock_call_count =
  613. transport_add_from_config_mock_call_count;
  614. old_transport_is_needed_mock_call_count =
  615. transport_is_needed_mock_call_count;
  616. r = parse_transport_line(options,
  617. "transport_1 exec /usr/bin/fake-transport", 0, 0);
  618. /* Should have succeeded */
  619. tt_assert(r == 0);
  620. /* transport_is_needed() should have been called */
  621. tt_assert(transport_is_needed_mock_call_count ==
  622. old_transport_is_needed_mock_call_count + 1);
  623. /*
  624. * pt_kickstart_proxy() and transport_add_from_config() should
  625. * not have been called.
  626. */
  627. tt_assert(pt_kickstart_proxy_mock_call_count ==
  628. old_pt_kickstart_proxy_mock_call_count);
  629. tt_assert(transport_add_from_config_mock_call_count ==
  630. old_transport_add_from_config_mock_call_count);
  631. /* Necessary transport case */
  632. transport_is_needed_mock_return = 1;
  633. old_pt_kickstart_proxy_mock_call_count =
  634. pt_kickstart_proxy_mock_call_count;
  635. old_transport_add_from_config_mock_call_count =
  636. transport_add_from_config_mock_call_count;
  637. old_transport_is_needed_mock_call_count =
  638. transport_is_needed_mock_call_count;
  639. r = parse_transport_line(options,
  640. "transport_1 exec /usr/bin/fake-transport", 0, 0);
  641. /* Should have succeeded */
  642. tt_assert(r == 0);
  643. /*
  644. * transport_is_needed() and pt_kickstart_proxy() should have been
  645. * called.
  646. */
  647. tt_assert(pt_kickstart_proxy_mock_call_count ==
  648. old_pt_kickstart_proxy_mock_call_count + 1);
  649. tt_assert(transport_is_needed_mock_call_count ==
  650. old_transport_is_needed_mock_call_count + 1);
  651. /* transport_add_from_config() should not have been called. */
  652. tt_assert(transport_add_from_config_mock_call_count ==
  653. old_transport_add_from_config_mock_call_count);
  654. /* proxy case */
  655. transport_is_needed_mock_return = 1;
  656. old_pt_kickstart_proxy_mock_call_count =
  657. pt_kickstart_proxy_mock_call_count;
  658. old_transport_add_from_config_mock_call_count =
  659. transport_add_from_config_mock_call_count;
  660. old_transport_is_needed_mock_call_count =
  661. transport_is_needed_mock_call_count;
  662. r = parse_transport_line(options,
  663. "transport_1 socks5 1.2.3.4:567", 0, 0);
  664. /* Should have succeeded */
  665. tt_assert(r == 0);
  666. /*
  667. * transport_is_needed() and transport_add_from_config() should have
  668. * been called.
  669. */
  670. tt_assert(transport_add_from_config_mock_call_count ==
  671. old_transport_add_from_config_mock_call_count + 1);
  672. tt_assert(transport_is_needed_mock_call_count ==
  673. old_transport_is_needed_mock_call_count + 1);
  674. /* pt_kickstart_proxy() should not have been called. */
  675. tt_assert(pt_kickstart_proxy_mock_call_count ==
  676. old_pt_kickstart_proxy_mock_call_count);
  677. /* Done with mocked client transport cases */
  678. UNMOCK(transport_is_needed);
  679. UNMOCK(transport_add_from_config);
  680. UNMOCK(pt_kickstart_proxy);
  681. done:
  682. /* Make sure we undo all mocks */
  683. UNMOCK(pt_kickstart_proxy);
  684. UNMOCK(transport_add_from_config);
  685. UNMOCK(transport_is_needed);
  686. return;
  687. }
  688. // Tests if an options with MyFamily fingerprints missing '$' normalises
  689. // them correctly and also ensure it also works with multiple fingerprints
  690. static void
  691. test_config_fix_my_family(void *arg)
  692. {
  693. char *err = NULL;
  694. const char *family = "$1111111111111111111111111111111111111111, "
  695. "1111111111111111111111111111111111111112, "
  696. "$1111111111111111111111111111111111111113";
  697. or_options_t* options = options_new();
  698. or_options_t* defaults = options_new();
  699. (void) arg;
  700. options_init(options);
  701. options_init(defaults);
  702. options->MyFamily = tor_strdup(family);
  703. options_validate(NULL, options, defaults, 0, &err) ;
  704. if (err != NULL) {
  705. TT_FAIL(("options_validate failed: %s", err));
  706. }
  707. tt_str_op(options->MyFamily,==, "$1111111111111111111111111111111111111111, "
  708. "$1111111111111111111111111111111111111112, "
  709. "$1111111111111111111111111111111111111113");
  710. done:
  711. if (err != NULL) {
  712. tor_free(err);
  713. }
  714. or_options_free(options);
  715. or_options_free(defaults);
  716. }
  717. #define CONFIG_TEST(name, flags) \
  718. { #name, test_config_ ## name, flags, NULL, NULL }
  719. struct testcase_t config_tests[] = {
  720. CONFIG_TEST(addressmap, 0),
  721. CONFIG_TEST(parse_bridge_line, 0),
  722. CONFIG_TEST(parse_transport_options_line, 0),
  723. CONFIG_TEST(parse_transport_plugin_line, TT_FORK),
  724. CONFIG_TEST(check_or_create_data_subdir, TT_FORK),
  725. CONFIG_TEST(write_to_data_subdir, TT_FORK),
  726. CONFIG_TEST(fix_my_family, 0),
  727. END_OF_TESTCASES
  728. };