test_config.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2015, 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), ~0, (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,OP_EQ, "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,OP_EQ, "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,OP_EQ, "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,OP_EQ, "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,OP_EQ, "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,OP_EQ, "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,OP_EQ, "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,OP_EQ, "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,OP_EQ, "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,OP_EQ, "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,OP_EQ, "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,OP_EQ, "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,OP_EQ, "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,OP_EQ, "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,OP_EQ, "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), OP_EQ, 0);
  180. #else
  181. tt_int_op(mkdir(options->DataDirectory, 0700), OP_EQ, 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), OP_EQ, 0);
  243. #else
  244. tt_int_op(mkdir(options->DataDirectory, 0700), OP_EQ, 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,OP_EQ, 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,OP_EQ, 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,OP_EQ, 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,OP_EQ, 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,OP_EQ, 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. (void) transport_list;
  483. (void) proxy_argv;
  484. (void) is_server;
  485. /* XXXX check that args are as expected. */
  486. ++pt_kickstart_proxy_mock_call_count;
  487. free_execve_args(proxy_argv);
  488. }
  489. static int
  490. transport_add_from_config_mock(const tor_addr_t *addr,
  491. uint16_t port, const char *name,
  492. int socks_ver)
  493. {
  494. (void) addr;
  495. (void) port;
  496. (void) name;
  497. (void) socks_ver;
  498. /* XXXX check that args are as expected. */
  499. ++transport_add_from_config_mock_call_count;
  500. return 0;
  501. }
  502. static int
  503. transport_is_needed_mock(const char *transport_name)
  504. {
  505. (void) transport_name;
  506. /* XXXX check that arg is as expected. */
  507. ++transport_is_needed_mock_call_count;
  508. return transport_is_needed_mock_return;
  509. }
  510. /**
  511. * Test parsing for the ClientTransportPlugin and ServerTransportPlugin config
  512. * options.
  513. */
  514. static void
  515. test_config_parse_transport_plugin_line(void *arg)
  516. {
  517. (void)arg;
  518. or_options_t *options = get_options_mutable();
  519. int r, tmp;
  520. int old_pt_kickstart_proxy_mock_call_count;
  521. int old_transport_add_from_config_mock_call_count;
  522. int old_transport_is_needed_mock_call_count;
  523. /* Bad transport lines - too short */
  524. r = parse_transport_line(options, "bad", 1, 0);
  525. tt_assert(r < 0);
  526. r = parse_transport_line(options, "bad", 1, 1);
  527. tt_assert(r < 0);
  528. r = parse_transport_line(options, "bad bad", 1, 0);
  529. tt_assert(r < 0);
  530. r = parse_transport_line(options, "bad bad", 1, 1);
  531. tt_assert(r < 0);
  532. /* Test transport list parsing */
  533. r = parse_transport_line(options,
  534. "transport_1 exec /usr/bin/fake-transport", 1, 0);
  535. tt_assert(r == 0);
  536. r = parse_transport_line(options,
  537. "transport_1 exec /usr/bin/fake-transport", 1, 1);
  538. tt_assert(r == 0);
  539. r = parse_transport_line(options,
  540. "transport_1,transport_2 exec /usr/bin/fake-transport", 1, 0);
  541. tt_assert(r == 0);
  542. r = parse_transport_line(options,
  543. "transport_1,transport_2 exec /usr/bin/fake-transport", 1, 1);
  544. tt_assert(r == 0);
  545. /* Bad transport identifiers */
  546. r = parse_transport_line(options,
  547. "transport_* exec /usr/bin/fake-transport", 1, 0);
  548. tt_assert(r < 0);
  549. r = parse_transport_line(options,
  550. "transport_* exec /usr/bin/fake-transport", 1, 1);
  551. tt_assert(r < 0);
  552. /* Check SOCKS cases for client transport */
  553. r = parse_transport_line(options,
  554. "transport_1 socks4 1.2.3.4:567", 1, 0);
  555. tt_assert(r == 0);
  556. r = parse_transport_line(options,
  557. "transport_1 socks5 1.2.3.4:567", 1, 0);
  558. tt_assert(r == 0);
  559. /* Proxy case for server transport */
  560. r = parse_transport_line(options,
  561. "transport_1 proxy 1.2.3.4:567", 1, 1);
  562. tt_assert(r == 0);
  563. /* Multiple-transport error exit */
  564. r = parse_transport_line(options,
  565. "transport_1,transport_2 socks5 1.2.3.4:567", 1, 0);
  566. tt_assert(r < 0);
  567. r = parse_transport_line(options,
  568. "transport_1,transport_2 proxy 1.2.3.4:567", 1, 1);
  569. /* No port error exit */
  570. r = parse_transport_line(options,
  571. "transport_1 socks5 1.2.3.4", 1, 0);
  572. tt_assert(r < 0);
  573. r = parse_transport_line(options,
  574. "transport_1 proxy 1.2.3.4", 1, 1);
  575. tt_assert(r < 0);
  576. /* Unparsable address error exit */
  577. r = parse_transport_line(options,
  578. "transport_1 socks5 1.2.3:6x7", 1, 0);
  579. tt_assert(r < 0);
  580. r = parse_transport_line(options,
  581. "transport_1 proxy 1.2.3:6x7", 1, 1);
  582. tt_assert(r < 0);
  583. /* "Strange {Client|Server}TransportPlugin field" error exit */
  584. r = parse_transport_line(options,
  585. "transport_1 foo bar", 1, 0);
  586. tt_assert(r < 0);
  587. r = parse_transport_line(options,
  588. "transport_1 foo bar", 1, 1);
  589. tt_assert(r < 0);
  590. /* No sandbox mode error exit */
  591. tmp = options->Sandbox;
  592. options->Sandbox = 1;
  593. r = parse_transport_line(options,
  594. "transport_1 exec /usr/bin/fake-transport", 1, 0);
  595. tt_assert(r < 0);
  596. r = parse_transport_line(options,
  597. "transport_1 exec /usr/bin/fake-transport", 1, 1);
  598. tt_assert(r < 0);
  599. options->Sandbox = tmp;
  600. /*
  601. * These final test cases cover code paths that only activate without
  602. * validate_only, so they need mocks in place.
  603. */
  604. MOCK(pt_kickstart_proxy, pt_kickstart_proxy_mock);
  605. old_pt_kickstart_proxy_mock_call_count =
  606. pt_kickstart_proxy_mock_call_count;
  607. r = parse_transport_line(options,
  608. "transport_1 exec /usr/bin/fake-transport", 0, 1);
  609. tt_assert(r == 0);
  610. tt_assert(pt_kickstart_proxy_mock_call_count ==
  611. old_pt_kickstart_proxy_mock_call_count + 1);
  612. UNMOCK(pt_kickstart_proxy);
  613. /* This one hits a log line in the !validate_only case only */
  614. r = parse_transport_line(options,
  615. "transport_1 proxy 1.2.3.4:567", 0, 1);
  616. tt_assert(r == 0);
  617. /* Check mocked client transport cases */
  618. MOCK(pt_kickstart_proxy, pt_kickstart_proxy_mock);
  619. MOCK(transport_add_from_config, transport_add_from_config_mock);
  620. MOCK(transport_is_needed, transport_is_needed_mock);
  621. /* Unnecessary transport case */
  622. transport_is_needed_mock_return = 0;
  623. old_pt_kickstart_proxy_mock_call_count =
  624. pt_kickstart_proxy_mock_call_count;
  625. old_transport_add_from_config_mock_call_count =
  626. transport_add_from_config_mock_call_count;
  627. old_transport_is_needed_mock_call_count =
  628. transport_is_needed_mock_call_count;
  629. r = parse_transport_line(options,
  630. "transport_1 exec /usr/bin/fake-transport", 0, 0);
  631. /* Should have succeeded */
  632. tt_assert(r == 0);
  633. /* transport_is_needed() should have been called */
  634. tt_assert(transport_is_needed_mock_call_count ==
  635. old_transport_is_needed_mock_call_count + 1);
  636. /*
  637. * pt_kickstart_proxy() and transport_add_from_config() should
  638. * not have been called.
  639. */
  640. tt_assert(pt_kickstart_proxy_mock_call_count ==
  641. old_pt_kickstart_proxy_mock_call_count);
  642. tt_assert(transport_add_from_config_mock_call_count ==
  643. old_transport_add_from_config_mock_call_count);
  644. /* Necessary transport case */
  645. transport_is_needed_mock_return = 1;
  646. old_pt_kickstart_proxy_mock_call_count =
  647. pt_kickstart_proxy_mock_call_count;
  648. old_transport_add_from_config_mock_call_count =
  649. transport_add_from_config_mock_call_count;
  650. old_transport_is_needed_mock_call_count =
  651. transport_is_needed_mock_call_count;
  652. r = parse_transport_line(options,
  653. "transport_1 exec /usr/bin/fake-transport", 0, 0);
  654. /* Should have succeeded */
  655. tt_assert(r == 0);
  656. /*
  657. * transport_is_needed() and pt_kickstart_proxy() should have been
  658. * called.
  659. */
  660. tt_assert(pt_kickstart_proxy_mock_call_count ==
  661. old_pt_kickstart_proxy_mock_call_count + 1);
  662. tt_assert(transport_is_needed_mock_call_count ==
  663. old_transport_is_needed_mock_call_count + 1);
  664. /* transport_add_from_config() should not have been called. */
  665. tt_assert(transport_add_from_config_mock_call_count ==
  666. old_transport_add_from_config_mock_call_count);
  667. /* proxy case */
  668. transport_is_needed_mock_return = 1;
  669. old_pt_kickstart_proxy_mock_call_count =
  670. pt_kickstart_proxy_mock_call_count;
  671. old_transport_add_from_config_mock_call_count =
  672. transport_add_from_config_mock_call_count;
  673. old_transport_is_needed_mock_call_count =
  674. transport_is_needed_mock_call_count;
  675. r = parse_transport_line(options,
  676. "transport_1 socks5 1.2.3.4:567", 0, 0);
  677. /* Should have succeeded */
  678. tt_assert(r == 0);
  679. /*
  680. * transport_is_needed() and transport_add_from_config() should have
  681. * been called.
  682. */
  683. tt_assert(transport_add_from_config_mock_call_count ==
  684. old_transport_add_from_config_mock_call_count + 1);
  685. tt_assert(transport_is_needed_mock_call_count ==
  686. old_transport_is_needed_mock_call_count + 1);
  687. /* pt_kickstart_proxy() should not have been called. */
  688. tt_assert(pt_kickstart_proxy_mock_call_count ==
  689. old_pt_kickstart_proxy_mock_call_count);
  690. /* Done with mocked client transport cases */
  691. UNMOCK(transport_is_needed);
  692. UNMOCK(transport_add_from_config);
  693. UNMOCK(pt_kickstart_proxy);
  694. done:
  695. /* Make sure we undo all mocks */
  696. UNMOCK(pt_kickstart_proxy);
  697. UNMOCK(transport_add_from_config);
  698. UNMOCK(transport_is_needed);
  699. return;
  700. }
  701. // Tests if an options with MyFamily fingerprints missing '$' normalises
  702. // them correctly and also ensure it also works with multiple fingerprints
  703. static void
  704. test_config_fix_my_family(void *arg)
  705. {
  706. char *err = NULL;
  707. const char *family = "$1111111111111111111111111111111111111111, "
  708. "1111111111111111111111111111111111111112, "
  709. "$1111111111111111111111111111111111111113";
  710. or_options_t* options = options_new();
  711. or_options_t* defaults = options_new();
  712. (void) arg;
  713. options_init(options);
  714. options_init(defaults);
  715. options->MyFamily = tor_strdup(family);
  716. options_validate(NULL, options, defaults, 0, &err) ;
  717. if (err != NULL) {
  718. TT_FAIL(("options_validate failed: %s", err));
  719. }
  720. tt_str_op(options->MyFamily,OP_EQ,
  721. "$1111111111111111111111111111111111111111, "
  722. "$1111111111111111111111111111111111111112, "
  723. "$1111111111111111111111111111111111111113");
  724. done:
  725. if (err != NULL) {
  726. tor_free(err);
  727. }
  728. or_options_free(options);
  729. or_options_free(defaults);
  730. }
  731. static int n_hostname_01010101 = 0;
  732. /** This mock function is meant to replace tor_lookup_hostname().
  733. * It answers with 1.1.1.1 as IP adddress that resulted from lookup.
  734. * This function increments <b>n_hostname_01010101</b> counter by one
  735. * every time it is called.
  736. */
  737. static int
  738. tor_lookup_hostname_01010101(const char *name, uint32_t *addr)
  739. {
  740. n_hostname_01010101++;
  741. if (name && addr) {
  742. *addr = ntohl(0x01010101);
  743. }
  744. return 0;
  745. }
  746. static int n_hostname_localhost = 0;
  747. /** This mock function is meant to replace tor_lookup_hostname().
  748. * It answers with 127.0.0.1 as IP adddress that resulted from lookup.
  749. * This function increments <b>n_hostname_localhost</b> counter by one
  750. * every time it is called.
  751. */
  752. static int
  753. tor_lookup_hostname_localhost(const char *name, uint32_t *addr)
  754. {
  755. n_hostname_localhost++;
  756. if (name && addr) {
  757. *addr = 0x7f000001;
  758. }
  759. return 0;
  760. }
  761. static int n_hostname_failure = 0;
  762. /** This mock function is meant to replace tor_lookup_hostname().
  763. * It pretends to fail by returning -1 to caller. Also, this function
  764. * increments <b>n_hostname_failure</b> every time it is called.
  765. */
  766. static int
  767. tor_lookup_hostname_failure(const char *name, uint32_t *addr)
  768. {
  769. (void)name;
  770. (void)addr;
  771. n_hostname_failure++;
  772. return -1;
  773. }
  774. static int n_gethostname_replacement = 0;
  775. /** This mock function is meant to replace tor_gethostname(). It
  776. * responds with string "onionrouter!" as hostname. This function
  777. * increments <b>n_gethostname_replacement</b> by one every time
  778. * it is called.
  779. */
  780. static int
  781. tor_gethostname_replacement(char *name, size_t namelen)
  782. {
  783. n_gethostname_replacement++;
  784. if (name && namelen) {
  785. strlcpy(name,"onionrouter!",namelen);
  786. }
  787. return 0;
  788. }
  789. static int n_gethostname_localhost = 0;
  790. /** This mock function is meant to replace tor_gethostname(). It
  791. * responds with string "127.0.0.1" as hostname. This function
  792. * increments <b>n_gethostname_localhost</b> by one every time
  793. * it is called.
  794. */
  795. static int
  796. tor_gethostname_localhost(char *name, size_t namelen)
  797. {
  798. n_gethostname_localhost++;
  799. if (name && namelen) {
  800. strlcpy(name,"127.0.0.1",namelen);
  801. }
  802. return 0;
  803. }
  804. static int n_gethostname_failure = 0;
  805. /** This mock function is meant to replace tor_gethostname.
  806. * It pretends to fail by returning -1. This function increments
  807. * <b>n_gethostname_failure</b> by one every time it is called.
  808. */
  809. static int
  810. tor_gethostname_failure(char *name, size_t namelen)
  811. {
  812. (void)name;
  813. (void)namelen;
  814. n_gethostname_failure++;
  815. return -1;
  816. }
  817. static int n_get_interface_address = 0;
  818. /** This mock function is meant to replace get_interface_address().
  819. * It answers with address 8.8.8.8. This function increments
  820. * <b>n_get_interface_address</b> by one every time it is called.
  821. */
  822. static int
  823. get_interface_address_08080808(int severity, uint32_t *addr)
  824. {
  825. (void)severity;
  826. n_get_interface_address++;
  827. if (addr) {
  828. *addr = ntohl(0x08080808);
  829. }
  830. return 0;
  831. }
  832. static int n_get_interface_address6 = 0;
  833. static sa_family_t last_address6_family;
  834. /** This mock function is meant to replace get_interface_address6().
  835. * It answers with IP address 9.9.9.9 iff both of the following are true:
  836. * - <b>family</b> is AF_INET
  837. * - <b>addr</b> pointer is not NULL.
  838. * This function increments <b>n_get_interface_address6</b> by one every
  839. * time it is called.
  840. */
  841. static int
  842. get_interface_address6_replacement(int severity, sa_family_t family,
  843. tor_addr_t *addr)
  844. {
  845. (void)severity;
  846. last_address6_family = family;
  847. n_get_interface_address6++;
  848. if ((family != AF_INET) || !addr) {
  849. return -1;
  850. }
  851. tor_addr_from_ipv4h(addr,0x09090909);
  852. return 0;
  853. }
  854. static int n_get_interface_address_failure = 0;
  855. /**
  856. * This mock function is meant to replace get_interface_address().
  857. * It pretends to fail getting interface address by returning -1.
  858. * <b>n_get_interface_address_failure</b> is incremented by one
  859. * every time this function is called.
  860. */
  861. static int
  862. get_interface_address_failure(int severity, uint32_t *addr)
  863. {
  864. (void)severity;
  865. (void)addr;
  866. n_get_interface_address_failure++;
  867. return -1;
  868. }
  869. static int n_get_interface_address6_failure = 0;
  870. /**
  871. * This mock function is meant to replace get_interface_addres6().
  872. * It will pretent to fail by return -1.
  873. * <b>n_get_interface_address6_failure</b> is incremented by one
  874. * every time this function is called and <b>last_address6_family</b>
  875. * is assigned the value of <b>family</b> argument.
  876. */
  877. static int
  878. get_interface_address6_failure(int severity, sa_family_t family,
  879. tor_addr_t *addr)
  880. {
  881. (void)severity;
  882. (void)addr;
  883. n_get_interface_address6_failure++;
  884. last_address6_family = family;
  885. return -1;
  886. }
  887. static void
  888. test_config_resolve_my_address(void *arg)
  889. {
  890. or_options_t *options;
  891. uint32_t resolved_addr;
  892. const char *method_used;
  893. char *hostname_out = NULL;
  894. int retval;
  895. int prev_n_hostname_01010101;
  896. int prev_n_hostname_localhost;
  897. int prev_n_hostname_failure;
  898. int prev_n_gethostname_replacement;
  899. int prev_n_gethostname_failure;
  900. int prev_n_gethostname_localhost;
  901. int prev_n_get_interface_address;
  902. int prev_n_get_interface_address_failure;
  903. int prev_n_get_interface_address6;
  904. int prev_n_get_interface_address6_failure;
  905. (void)arg;
  906. options = options_new();
  907. options_init(options);
  908. /*
  909. * CASE 1:
  910. * If options->Address is a valid IPv4 address string, we want
  911. * the corresponding address to be parsed and returned.
  912. */
  913. options->Address = tor_strdup("128.52.128.105");
  914. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  915. &method_used,&hostname_out);
  916. tt_want(retval == 0);
  917. tt_want_str_op(method_used,==,"CONFIGURED");
  918. tt_want(hostname_out == NULL);
  919. tt_assert(htonl(resolved_addr) == 0x69803480);
  920. tor_free(options->Address);
  921. /*
  922. * CASE 2:
  923. * If options->Address is a valid DNS address, we want resolve_my_address()
  924. * function to ask tor_lookup_hostname() for help with resolving it
  925. * and return the address that was resolved (in host order).
  926. */
  927. MOCK(tor_lookup_hostname,tor_lookup_hostname_01010101);
  928. tor_free(options->Address);
  929. options->Address = tor_strdup("www.torproject.org");
  930. prev_n_hostname_01010101 = n_hostname_01010101;
  931. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  932. &method_used,&hostname_out);
  933. tt_want(retval == 0);
  934. tt_want(n_hostname_01010101 == prev_n_hostname_01010101 + 1);
  935. tt_want_str_op(method_used,==,"RESOLVED");
  936. tt_want_str_op(hostname_out,==,"www.torproject.org");
  937. tt_assert(htonl(resolved_addr) == 0x01010101);
  938. UNMOCK(tor_lookup_hostname);
  939. tor_free(options->Address);
  940. tor_free(hostname_out);
  941. /*
  942. * CASE 3:
  943. * Given that options->Address is NULL, we want resolve_my_address()
  944. * to try and use tor_gethostname() to get hostname AND use
  945. * tor_lookup_hostname() to get IP address.
  946. */
  947. resolved_addr = 0;
  948. tor_free(options->Address);
  949. options->Address = NULL;
  950. MOCK(tor_gethostname,tor_gethostname_replacement);
  951. MOCK(tor_lookup_hostname,tor_lookup_hostname_01010101);
  952. prev_n_gethostname_replacement = n_gethostname_replacement;
  953. prev_n_hostname_01010101 = n_hostname_01010101;
  954. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  955. &method_used,&hostname_out);
  956. tt_want(retval == 0);
  957. tt_want(n_gethostname_replacement == prev_n_gethostname_replacement + 1);
  958. tt_want(n_hostname_01010101 == prev_n_hostname_01010101 + 1);
  959. tt_want_str_op(method_used,==,"GETHOSTNAME");
  960. tt_want_str_op(hostname_out,==,"onionrouter!");
  961. tt_assert(htonl(resolved_addr) == 0x01010101);
  962. UNMOCK(tor_gethostname);
  963. UNMOCK(tor_lookup_hostname);
  964. tor_free(hostname_out);
  965. /*
  966. * CASE 4:
  967. * Given that options->Address is a local host address, we want
  968. * resolve_my_address() function to fail.
  969. */
  970. resolved_addr = 0;
  971. tor_free(options->Address);
  972. options->Address = tor_strdup("127.0.0.1");
  973. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  974. &method_used,&hostname_out);
  975. tt_want(resolved_addr == 0);
  976. tt_assert(retval == -1);
  977. tor_free(options->Address);
  978. tor_free(hostname_out);
  979. /*
  980. * CASE 5:
  981. * We want resolve_my_address() to fail if DNS address in options->Address
  982. * cannot be resolved.
  983. */
  984. MOCK(tor_lookup_hostname,tor_lookup_hostname_failure);
  985. prev_n_hostname_failure = n_hostname_failure;
  986. tor_free(options->Address);
  987. options->Address = tor_strdup("www.tor-project.org");
  988. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  989. &method_used,&hostname_out);
  990. tt_want(n_hostname_failure == prev_n_hostname_failure + 1);
  991. tt_assert(retval == -1);
  992. UNMOCK(tor_lookup_hostname);
  993. tor_free(options->Address);
  994. tor_free(hostname_out);
  995. /*
  996. * CASE 6:
  997. * If options->Address is NULL AND gettting local hostname fails, we want
  998. * resolve_my_address() to fail as well.
  999. */
  1000. MOCK(tor_gethostname,tor_gethostname_failure);
  1001. prev_n_gethostname_failure = n_gethostname_failure;
  1002. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  1003. &method_used,&hostname_out);
  1004. tt_want(n_gethostname_failure == prev_n_gethostname_failure + 1);
  1005. tt_assert(retval == -1);
  1006. UNMOCK(tor_gethostname);
  1007. tor_free(hostname_out);
  1008. /*
  1009. * CASE 7:
  1010. * We want resolve_my_address() to try and get network interface address via
  1011. * get_interface_address() if hostname returned by tor_gethostname() cannot be
  1012. * resolved into IP address.
  1013. */
  1014. MOCK(tor_gethostname,tor_gethostname_replacement);
  1015. MOCK(tor_lookup_hostname,tor_lookup_hostname_failure);
  1016. MOCK(get_interface_address,get_interface_address_08080808);
  1017. prev_n_gethostname_replacement = n_gethostname_replacement;
  1018. prev_n_get_interface_address = n_get_interface_address;
  1019. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  1020. &method_used,&hostname_out);
  1021. tt_want(retval == 0);
  1022. tt_want_int_op(n_gethostname_replacement, ==,
  1023. prev_n_gethostname_replacement + 1);
  1024. tt_want_int_op(n_get_interface_address, ==,
  1025. prev_n_get_interface_address + 1);
  1026. tt_want_str_op(method_used,==,"INTERFACE");
  1027. tt_want(hostname_out == NULL);
  1028. tt_assert(resolved_addr == ntohl(0x08080808));
  1029. UNMOCK(get_interface_address);
  1030. tor_free(hostname_out);
  1031. /*
  1032. * CASE 8:
  1033. * Suppose options->Address is NULL AND hostname returned by tor_gethostname()
  1034. * is unresolvable. We want resolve_my_address to fail if
  1035. * get_interface_address() fails.
  1036. */
  1037. MOCK(get_interface_address,get_interface_address_failure);
  1038. prev_n_get_interface_address_failure = n_get_interface_address_failure;
  1039. prev_n_gethostname_replacement = n_gethostname_replacement;
  1040. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  1041. &method_used,&hostname_out);
  1042. tt_want(n_get_interface_address_failure ==
  1043. prev_n_get_interface_address_failure + 1);
  1044. tt_want(n_gethostname_replacement ==
  1045. prev_n_gethostname_replacement + 1);
  1046. tt_assert(retval == -1);
  1047. UNMOCK(get_interface_address);
  1048. tor_free(hostname_out);
  1049. /*
  1050. * CASE 9:
  1051. * Given that options->Address is NULL AND tor_lookup_hostname()
  1052. * fails AND hostname returned by gethostname() resolves
  1053. * to local IP address, we want resolve_my_address() function to
  1054. * call get_interface_address6(.,AF_INET,.) and return IP address
  1055. * the latter function has found.
  1056. */
  1057. MOCK(tor_lookup_hostname,tor_lookup_hostname_failure);
  1058. MOCK(tor_gethostname,tor_gethostname_replacement);
  1059. MOCK(get_interface_address6,get_interface_address6_replacement);
  1060. prev_n_gethostname_replacement = n_gethostname_replacement;
  1061. prev_n_hostname_failure = n_hostname_failure;
  1062. prev_n_get_interface_address6 = n_get_interface_address6;
  1063. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  1064. &method_used,&hostname_out);
  1065. tt_want(last_address6_family == AF_INET);
  1066. tt_want(n_get_interface_address6 == prev_n_get_interface_address6 + 1);
  1067. tt_want(n_hostname_failure == prev_n_hostname_failure + 1);
  1068. tt_want(n_gethostname_replacement == prev_n_gethostname_replacement + 1);
  1069. tt_want(retval == 0);
  1070. tt_want_str_op(method_used,==,"INTERFACE");
  1071. tt_assert(htonl(resolved_addr) == 0x09090909);
  1072. UNMOCK(tor_lookup_hostname);
  1073. UNMOCK(tor_gethostname);
  1074. UNMOCK(get_interface_address6);
  1075. tor_free(hostname_out);
  1076. /*
  1077. * CASE 10: We want resolve_my_address() to fail if all of the following
  1078. * are true:
  1079. * 1. options->Address is not NULL
  1080. * 2. ... but it cannot be converted to struct in_addr by
  1081. * tor_inet_aton()
  1082. * 3. ... and tor_lookup_hostname() fails to resolve the
  1083. * options->Address
  1084. */
  1085. MOCK(tor_lookup_hostname,tor_lookup_hostname_failure);
  1086. prev_n_hostname_failure = n_hostname_failure;
  1087. tor_free(options->Address);
  1088. options->Address = tor_strdup("some_hostname");
  1089. retval = resolve_my_address(LOG_NOTICE, options, &resolved_addr,
  1090. &method_used,&hostname_out);
  1091. tt_want(n_hostname_failure == prev_n_hostname_failure + 1);
  1092. tt_assert(retval == -1);
  1093. UNMOCK(tor_gethostname);
  1094. UNMOCK(tor_lookup_hostname);
  1095. tor_free(hostname_out);
  1096. /*
  1097. * CASE 11:
  1098. * Suppose the following sequence of events:
  1099. * 1. options->Address is NULL
  1100. * 2. tor_gethostname() succeeds to get hostname of machine Tor
  1101. * if running on.
  1102. * 3. Hostname from previous step cannot be converted to
  1103. * address by using tor_inet_aton() function.
  1104. * 4. However, tor_lookup_hostname() succeds in resolving the
  1105. * hostname from step 2.
  1106. * 5. Unfortunately, tor_addr_is_internal() deems this address
  1107. * to be internal.
  1108. * 6. get_interface_address6(.,AF_INET,.) returns non-internal
  1109. * IPv4
  1110. *
  1111. * We want resolve_my_addr() to succeed with method "INTERFACE"
  1112. * and address from step 6.
  1113. */
  1114. tor_free(options->Address);
  1115. options->Address = NULL;
  1116. MOCK(tor_gethostname,tor_gethostname_replacement);
  1117. MOCK(tor_lookup_hostname,tor_lookup_hostname_localhost);
  1118. MOCK(get_interface_address6,get_interface_address6_replacement);
  1119. prev_n_gethostname_replacement = n_gethostname_replacement;
  1120. prev_n_hostname_localhost = n_hostname_localhost;
  1121. prev_n_get_interface_address6 = n_get_interface_address6;
  1122. retval = resolve_my_address(LOG_DEBUG, options, &resolved_addr,
  1123. &method_used,&hostname_out);
  1124. tt_want(n_gethostname_replacement == prev_n_gethostname_replacement + 1);
  1125. tt_want(n_hostname_localhost == prev_n_hostname_localhost + 1);
  1126. tt_want(n_get_interface_address6 == prev_n_get_interface_address6 + 1);
  1127. tt_str_op(method_used,==,"INTERFACE");
  1128. tt_assert(!hostname_out);
  1129. tt_assert(retval == 0);
  1130. /*
  1131. * CASE 11b:
  1132. * 1-5 as above.
  1133. * 6. get_interface_address6() fails.
  1134. *
  1135. * In this subcase, we want resolve_my_address() to fail.
  1136. */
  1137. UNMOCK(get_interface_address6);
  1138. MOCK(get_interface_address6,get_interface_address6_failure);
  1139. prev_n_gethostname_replacement = n_gethostname_replacement;
  1140. prev_n_hostname_localhost = n_hostname_localhost;
  1141. prev_n_get_interface_address6_failure = n_get_interface_address6_failure;
  1142. retval = resolve_my_address(LOG_DEBUG, options, &resolved_addr,
  1143. &method_used,&hostname_out);
  1144. tt_want(n_gethostname_replacement == prev_n_gethostname_replacement + 1);
  1145. tt_want(n_hostname_localhost == prev_n_hostname_localhost + 1);
  1146. tt_want(n_get_interface_address6_failure ==
  1147. prev_n_get_interface_address6_failure + 1);
  1148. tt_assert(retval == -1);
  1149. UNMOCK(tor_gethostname);
  1150. UNMOCK(tor_lookup_hostname);
  1151. UNMOCK(get_interface_address6);
  1152. /* CASE 12:
  1153. * Suppose the following happens:
  1154. * 1. options->Address is NULL AND options->DirAuthorities is 1.
  1155. * 2. tor_gethostname() succeeds in getting hostname of a machine ...
  1156. * 3. ... which is successfully parsed by tor_inet_aton() ...
  1157. * 4. into IPv4 address that tor_addr_is_inernal() considers to be
  1158. * internal.
  1159. *
  1160. * In this case, we want resolve_my_address() to fail.
  1161. */
  1162. tor_free(options->Address);
  1163. options->Address = NULL;
  1164. options->DirAuthorities = tor_malloc_zero(sizeof(config_line_t));
  1165. MOCK(tor_gethostname,tor_gethostname_localhost);
  1166. prev_n_gethostname_localhost = n_gethostname_localhost;
  1167. retval = resolve_my_address(LOG_DEBUG, options, &resolved_addr,
  1168. &method_used,&hostname_out);
  1169. tt_want(n_gethostname_localhost == prev_n_gethostname_localhost + 1);
  1170. tt_assert(retval == -1);
  1171. UNMOCK(tor_gethostname);
  1172. done:
  1173. tor_free(options->Address);
  1174. tor_free(options->DirAuthorities);
  1175. or_options_free(options);
  1176. tor_free(hostname_out);
  1177. UNMOCK(tor_gethostname);
  1178. UNMOCK(tor_lookup_hostname);
  1179. UNMOCK(get_interface_address);
  1180. UNMOCK(get_interface_address6);
  1181. UNMOCK(tor_gethostname);
  1182. }
  1183. #define CONFIG_TEST(name, flags) \
  1184. { #name, test_config_ ## name, flags, NULL, NULL }
  1185. struct testcase_t config_tests[] = {
  1186. CONFIG_TEST(resolve_my_address, TT_FORK),
  1187. CONFIG_TEST(addressmap, 0),
  1188. CONFIG_TEST(parse_bridge_line, 0),
  1189. CONFIG_TEST(parse_transport_options_line, 0),
  1190. CONFIG_TEST(parse_transport_plugin_line, TT_FORK),
  1191. CONFIG_TEST(check_or_create_data_subdir, TT_FORK),
  1192. CONFIG_TEST(write_to_data_subdir, TT_FORK),
  1193. CONFIG_TEST(fix_my_family, 0),
  1194. END_OF_TESTCASES
  1195. };