test_config.c 29 KB

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