test_config.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460
  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. addressmap_free_all();
  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. static int n_hostname_01010101 = 0;
  733. /** This mock function is meant to replace tor_lookup_hostname().
  734. * It answers with 1.1.1.1 as IP adddress that resulted from lookup.
  735. * This function increments <b>n_hostname_01010101</b> counter by one
  736. * every time it is called.
  737. */
  738. static int
  739. tor_lookup_hostname_01010101(const char *name, uint32_t *addr)
  740. {
  741. n_hostname_01010101++;
  742. if (name && addr) {
  743. *addr = ntohl(0x01010101);
  744. }
  745. return 0;
  746. }
  747. static int n_hostname_localhost = 0;
  748. /** This mock function is meant to replace tor_lookup_hostname().
  749. * It answers with 127.0.0.1 as IP adddress that resulted from lookup.
  750. * This function increments <b>n_hostname_localhost</b> counter by one
  751. * every time it is called.
  752. */
  753. static int
  754. tor_lookup_hostname_localhost(const char *name, uint32_t *addr)
  755. {
  756. n_hostname_localhost++;
  757. if (name && addr) {
  758. *addr = 0x7f000001;
  759. }
  760. return 0;
  761. }
  762. static int n_hostname_failure = 0;
  763. /** This mock function is meant to replace tor_lookup_hostname().
  764. * It pretends to fail by returning -1 to caller. Also, this function
  765. * increments <b>n_hostname_failure</b> every time it is called.
  766. */
  767. static int
  768. tor_lookup_hostname_failure(const char *name, uint32_t *addr)
  769. {
  770. (void)name;
  771. (void)addr;
  772. n_hostname_failure++;
  773. return -1;
  774. }
  775. static int n_gethostname_replacement = 0;
  776. /** This mock function is meant to replace tor_gethostname(). It
  777. * responds with string "onionrouter!" as hostname. This function
  778. * increments <b>n_gethostname_replacement</b> by one every time
  779. * it is called.
  780. */
  781. static int
  782. tor_gethostname_replacement(char *name, size_t namelen)
  783. {
  784. n_gethostname_replacement++;
  785. if (name && namelen) {
  786. strlcpy(name,"onionrouter!",namelen);
  787. }
  788. return 0;
  789. }
  790. static int n_gethostname_localhost = 0;
  791. /** This mock function is meant to replace tor_gethostname(). It
  792. * responds with string "127.0.0.1" as hostname. This function
  793. * increments <b>n_gethostname_localhost</b> by one every time
  794. * it is called.
  795. */
  796. static int
  797. tor_gethostname_localhost(char *name, size_t namelen)
  798. {
  799. n_gethostname_localhost++;
  800. if (name && namelen) {
  801. strlcpy(name,"127.0.0.1",namelen);
  802. }
  803. return 0;
  804. }
  805. static int n_gethostname_failure = 0;
  806. /** This mock function is meant to replace tor_gethostname.
  807. * It pretends to fail by returning -1. This function increments
  808. * <b>n_gethostname_failure</b> by one every time it is called.
  809. */
  810. static int
  811. tor_gethostname_failure(char *name, size_t namelen)
  812. {
  813. (void)name;
  814. (void)namelen;
  815. n_gethostname_failure++;
  816. return -1;
  817. }
  818. static int n_get_interface_address = 0;
  819. /** This mock function is meant to replace get_interface_address().
  820. * It answers with address 8.8.8.8. This function increments
  821. * <b>n_get_interface_address</b> by one every time it is called.
  822. */
  823. static int
  824. get_interface_address_08080808(int severity, uint32_t *addr)
  825. {
  826. (void)severity;
  827. n_get_interface_address++;
  828. if (addr) {
  829. *addr = ntohl(0x08080808);
  830. }
  831. return 0;
  832. }
  833. static int n_get_interface_address6 = 0;
  834. static sa_family_t last_address6_family;
  835. /** This mock function is meant to replace get_interface_address6().
  836. * It answers with IP address 9.9.9.9 iff both of the following are true:
  837. * - <b>family</b> is AF_INET
  838. * - <b>addr</b> pointer is not NULL.
  839. * This function increments <b>n_get_interface_address6</b> by one every
  840. * time it is called.
  841. */
  842. static int
  843. get_interface_address6_replacement(int severity, sa_family_t family,
  844. tor_addr_t *addr)
  845. {
  846. (void)severity;
  847. last_address6_family = family;
  848. n_get_interface_address6++;
  849. if ((family != AF_INET) || !addr) {
  850. return -1;
  851. }
  852. tor_addr_from_ipv4h(addr,0x09090909);
  853. return 0;
  854. }
  855. static int n_get_interface_address_failure = 0;
  856. /**
  857. * This mock function is meant to replace get_interface_address().
  858. * It pretends to fail getting interface address by returning -1.
  859. * <b>n_get_interface_address_failure</b> is incremented by one
  860. * every time this function is called.
  861. */
  862. static int
  863. get_interface_address_failure(int severity, uint32_t *addr)
  864. {
  865. (void)severity;
  866. (void)addr;
  867. n_get_interface_address_failure++;
  868. return -1;
  869. }
  870. static int n_get_interface_address6_failure = 0;
  871. /**
  872. * This mock function is meant to replace get_interface_addres6().
  873. * It will pretent to fail by return -1.
  874. * <b>n_get_interface_address6_failure</b> is incremented by one
  875. * every time this function is called and <b>last_address6_family</b>
  876. * is assigned the value of <b>family</b> argument.
  877. */
  878. static int
  879. get_interface_address6_failure(int severity, sa_family_t family,
  880. tor_addr_t *addr)
  881. {
  882. (void)severity;
  883. (void)addr;
  884. n_get_interface_address6_failure++;
  885. last_address6_family = family;
  886. return -1;
  887. }
  888. static void
  889. test_config_resolve_my_address(void *arg)
  890. {
  891. or_options_t *options;
  892. uint32_t resolved_addr;
  893. const char *method_used;
  894. char *hostname_out = NULL;
  895. int retval;
  896. int prev_n_hostname_01010101;
  897. int prev_n_hostname_localhost;
  898. int prev_n_hostname_failure;
  899. int prev_n_gethostname_replacement;
  900. int prev_n_gethostname_failure;
  901. int prev_n_gethostname_localhost;
  902. int prev_n_get_interface_address;
  903. int prev_n_get_interface_address_failure;
  904. int prev_n_get_interface_address6;
  905. int prev_n_get_interface_address6_failure;
  906. (void)arg;
  907. options = options_new();
  908. options_init(options);
  909. /*
  910. * CASE 1:
  911. * If options->Address is a valid IPv4 address string, we want
  912. * the corresponding address to be parsed and returned.
  913. */
  914. options->Address = tor_strdup("128.52.128.105");
  915. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  916. &method_used,&hostname_out);
  917. tt_want(retval == 0);
  918. tt_want_str_op(method_used,==,"CONFIGURED");
  919. tt_want(hostname_out == NULL);
  920. tt_assert(resolved_addr == 0x80348069);
  921. tor_free(options->Address);
  922. /*
  923. * CASE 2:
  924. * If options->Address is a valid DNS address, we want resolve_my_address()
  925. * function to ask tor_lookup_hostname() for help with resolving it
  926. * and return the address that was resolved (in host order).
  927. */
  928. MOCK(tor_lookup_hostname,tor_lookup_hostname_01010101);
  929. tor_free(options->Address);
  930. options->Address = tor_strdup("www.torproject.org");
  931. prev_n_hostname_01010101 = n_hostname_01010101;
  932. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  933. &method_used,&hostname_out);
  934. tt_want(retval == 0);
  935. tt_want(n_hostname_01010101 == prev_n_hostname_01010101 + 1);
  936. tt_want_str_op(method_used,==,"RESOLVED");
  937. tt_want_str_op(hostname_out,==,"www.torproject.org");
  938. tt_assert(resolved_addr == 0x01010101);
  939. UNMOCK(tor_lookup_hostname);
  940. tor_free(options->Address);
  941. tor_free(hostname_out);
  942. /*
  943. * CASE 3:
  944. * Given that options->Address is NULL, we want resolve_my_address()
  945. * to try and use tor_gethostname() to get hostname AND use
  946. * tor_lookup_hostname() to get IP address.
  947. */
  948. resolved_addr = 0;
  949. tor_free(options->Address);
  950. options->Address = NULL;
  951. MOCK(tor_gethostname,tor_gethostname_replacement);
  952. MOCK(tor_lookup_hostname,tor_lookup_hostname_01010101);
  953. prev_n_gethostname_replacement = n_gethostname_replacement;
  954. prev_n_hostname_01010101 = n_hostname_01010101;
  955. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  956. &method_used,&hostname_out);
  957. tt_want(retval == 0);
  958. tt_want(n_gethostname_replacement == prev_n_gethostname_replacement + 1);
  959. tt_want(n_hostname_01010101 == prev_n_hostname_01010101 + 1);
  960. tt_want_str_op(method_used,==,"GETHOSTNAME");
  961. tt_want_str_op(hostname_out,==,"onionrouter!");
  962. tt_assert(resolved_addr == 0x01010101);
  963. UNMOCK(tor_gethostname);
  964. UNMOCK(tor_lookup_hostname);
  965. tor_free(hostname_out);
  966. /*
  967. * CASE 4:
  968. * Given that options->Address is a local host address, we want
  969. * resolve_my_address() function to fail.
  970. */
  971. resolved_addr = 0;
  972. tor_free(options->Address);
  973. options->Address = tor_strdup("127.0.0.1");
  974. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  975. &method_used,&hostname_out);
  976. tt_want(resolved_addr == 0);
  977. tt_assert(retval == -1);
  978. tor_free(options->Address);
  979. tor_free(hostname_out);
  980. /*
  981. * CASE 5:
  982. * We want resolve_my_address() to fail if DNS address in options->Address
  983. * cannot be resolved.
  984. */
  985. MOCK(tor_lookup_hostname,tor_lookup_hostname_failure);
  986. prev_n_hostname_failure = n_hostname_failure;
  987. tor_free(options->Address);
  988. options->Address = tor_strdup("www.tor-project.org");
  989. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  990. &method_used,&hostname_out);
  991. tt_want(n_hostname_failure == prev_n_hostname_failure + 1);
  992. tt_assert(retval == -1);
  993. UNMOCK(tor_lookup_hostname);
  994. tor_free(options->Address);
  995. tor_free(hostname_out);
  996. /*
  997. * CASE 6:
  998. * If options->Address is NULL AND gettting local hostname fails, we want
  999. * resolve_my_address() to fail as well.
  1000. */
  1001. MOCK(tor_gethostname,tor_gethostname_failure);
  1002. prev_n_gethostname_failure = n_gethostname_failure;
  1003. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  1004. &method_used,&hostname_out);
  1005. tt_want(n_gethostname_failure == prev_n_gethostname_failure + 1);
  1006. tt_assert(retval == -1);
  1007. UNMOCK(tor_gethostname);
  1008. tor_free(hostname_out);
  1009. /*
  1010. * CASE 7:
  1011. * We want resolve_my_address() to try and get network interface address via
  1012. * get_interface_address() if hostname returned by tor_gethostname() cannot be
  1013. * resolved into IP address.
  1014. */
  1015. MOCK(tor_gethostname,tor_gethostname_replacement);
  1016. MOCK(tor_lookup_hostname,tor_lookup_hostname_failure);
  1017. MOCK(get_interface_address,get_interface_address_08080808);
  1018. prev_n_gethostname_replacement = n_gethostname_replacement;
  1019. prev_n_get_interface_address = n_get_interface_address;
  1020. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  1021. &method_used,&hostname_out);
  1022. tt_want(retval == 0);
  1023. tt_want_int_op(n_gethostname_replacement, ==,
  1024. prev_n_gethostname_replacement + 1);
  1025. tt_want_int_op(n_get_interface_address, ==,
  1026. prev_n_get_interface_address + 1);
  1027. tt_want_str_op(method_used,==,"INTERFACE");
  1028. tt_want(hostname_out == NULL);
  1029. tt_assert(resolved_addr == 0x08080808);
  1030. UNMOCK(get_interface_address);
  1031. tor_free(hostname_out);
  1032. /*
  1033. * CASE 8:
  1034. * Suppose options->Address is NULL AND hostname returned by tor_gethostname()
  1035. * is unresolvable. We want resolve_my_address to fail if
  1036. * get_interface_address() fails.
  1037. */
  1038. MOCK(get_interface_address,get_interface_address_failure);
  1039. prev_n_get_interface_address_failure = n_get_interface_address_failure;
  1040. prev_n_gethostname_replacement = n_gethostname_replacement;
  1041. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  1042. &method_used,&hostname_out);
  1043. tt_want(n_get_interface_address_failure ==
  1044. prev_n_get_interface_address_failure + 1);
  1045. tt_want(n_gethostname_replacement ==
  1046. prev_n_gethostname_replacement + 1);
  1047. tt_assert(retval == -1);
  1048. UNMOCK(get_interface_address);
  1049. tor_free(hostname_out);
  1050. /*
  1051. * CASE 9:
  1052. * Given that options->Address is NULL AND tor_lookup_hostname()
  1053. * fails AND hostname returned by gethostname() resolves
  1054. * to local IP address, we want resolve_my_address() function to
  1055. * call get_interface_address6(.,AF_INET,.) and return IP address
  1056. * the latter function has found.
  1057. */
  1058. MOCK(tor_lookup_hostname,tor_lookup_hostname_failure);
  1059. MOCK(tor_gethostname,tor_gethostname_replacement);
  1060. MOCK(get_interface_address6,get_interface_address6_replacement);
  1061. prev_n_gethostname_replacement = n_gethostname_replacement;
  1062. prev_n_hostname_failure = n_hostname_failure;
  1063. prev_n_get_interface_address6 = n_get_interface_address6;
  1064. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  1065. &method_used,&hostname_out);
  1066. tt_want(last_address6_family == AF_INET);
  1067. tt_want(n_get_interface_address6 == prev_n_get_interface_address6 + 1);
  1068. tt_want(n_hostname_failure == prev_n_hostname_failure + 1);
  1069. tt_want(n_gethostname_replacement == prev_n_gethostname_replacement + 1);
  1070. tt_want(retval == 0);
  1071. tt_want_str_op(method_used,==,"INTERFACE");
  1072. tt_assert(resolved_addr == 0x09090909);
  1073. UNMOCK(tor_lookup_hostname);
  1074. UNMOCK(tor_gethostname);
  1075. UNMOCK(get_interface_address6);
  1076. tor_free(hostname_out);
  1077. /*
  1078. * CASE 10: We want resolve_my_address() to fail if all of the following
  1079. * are true:
  1080. * 1. options->Address is not NULL
  1081. * 2. ... but it cannot be converted to struct in_addr by
  1082. * tor_inet_aton()
  1083. * 3. ... and tor_lookup_hostname() fails to resolve the
  1084. * options->Address
  1085. */
  1086. MOCK(tor_lookup_hostname,tor_lookup_hostname_failure);
  1087. prev_n_hostname_failure = n_hostname_failure;
  1088. tor_free(options->Address);
  1089. options->Address = tor_strdup("some_hostname");
  1090. retval = resolve_my_address(LOG_NOTICE, options, &resolved_addr,
  1091. &method_used,&hostname_out);
  1092. tt_want(n_hostname_failure == prev_n_hostname_failure + 1);
  1093. tt_assert(retval == -1);
  1094. UNMOCK(tor_gethostname);
  1095. UNMOCK(tor_lookup_hostname);
  1096. tor_free(hostname_out);
  1097. /*
  1098. * CASE 11:
  1099. * Suppose the following sequence of events:
  1100. * 1. options->Address is NULL
  1101. * 2. tor_gethostname() succeeds to get hostname of machine Tor
  1102. * if running on.
  1103. * 3. Hostname from previous step cannot be converted to
  1104. * address by using tor_inet_aton() function.
  1105. * 4. However, tor_lookup_hostname() succeds in resolving the
  1106. * hostname from step 2.
  1107. * 5. Unfortunately, tor_addr_is_internal() deems this address
  1108. * to be internal.
  1109. * 6. get_interface_address6(.,AF_INET,.) returns non-internal
  1110. * IPv4
  1111. *
  1112. * We want resolve_my_addr() to succeed with method "INTERFACE"
  1113. * and address from step 6.
  1114. */
  1115. tor_free(options->Address);
  1116. options->Address = NULL;
  1117. MOCK(tor_gethostname,tor_gethostname_replacement);
  1118. MOCK(tor_lookup_hostname,tor_lookup_hostname_localhost);
  1119. MOCK(get_interface_address6,get_interface_address6_replacement);
  1120. prev_n_gethostname_replacement = n_gethostname_replacement;
  1121. prev_n_hostname_localhost = n_hostname_localhost;
  1122. prev_n_get_interface_address6 = n_get_interface_address6;
  1123. retval = resolve_my_address(LOG_DEBUG, options, &resolved_addr,
  1124. &method_used,&hostname_out);
  1125. tt_want(n_gethostname_replacement == prev_n_gethostname_replacement + 1);
  1126. tt_want(n_hostname_localhost == prev_n_hostname_localhost + 1);
  1127. tt_want(n_get_interface_address6 == prev_n_get_interface_address6 + 1);
  1128. tt_str_op(method_used,==,"INTERFACE");
  1129. tt_assert(!hostname_out);
  1130. tt_assert(retval == 0);
  1131. /*
  1132. * CASE 11b:
  1133. * 1-5 as above.
  1134. * 6. get_interface_address6() fails.
  1135. *
  1136. * In this subcase, we want resolve_my_address() to fail.
  1137. */
  1138. UNMOCK(get_interface_address6);
  1139. MOCK(get_interface_address6,get_interface_address6_failure);
  1140. prev_n_gethostname_replacement = n_gethostname_replacement;
  1141. prev_n_hostname_localhost = n_hostname_localhost;
  1142. prev_n_get_interface_address6_failure = n_get_interface_address6_failure;
  1143. retval = resolve_my_address(LOG_DEBUG, options, &resolved_addr,
  1144. &method_used,&hostname_out);
  1145. tt_want(n_gethostname_replacement == prev_n_gethostname_replacement + 1);
  1146. tt_want(n_hostname_localhost == prev_n_hostname_localhost + 1);
  1147. tt_want(n_get_interface_address6_failure ==
  1148. prev_n_get_interface_address6_failure + 1);
  1149. tt_assert(retval == -1);
  1150. UNMOCK(tor_gethostname);
  1151. UNMOCK(tor_lookup_hostname);
  1152. UNMOCK(get_interface_address6);
  1153. /* CASE 12:
  1154. * Suppose the following happens:
  1155. * 1. options->Address is NULL AND options->DirAuthorities is 1.
  1156. * 2. tor_gethostname() succeeds in getting hostname of a machine ...
  1157. * 3. ... which is successfully parsed by tor_inet_aton() ...
  1158. * 4. into IPv4 address that tor_addr_is_inernal() considers to be
  1159. * internal.
  1160. *
  1161. * In this case, we want resolve_my_address() to fail.
  1162. */
  1163. tor_free(options->Address);
  1164. options->Address = NULL;
  1165. options->DirAuthorities = tor_malloc_zero(sizeof(config_line_t));
  1166. MOCK(tor_gethostname,tor_gethostname_localhost);
  1167. prev_n_gethostname_localhost = n_gethostname_localhost;
  1168. retval = resolve_my_address(LOG_DEBUG, options, &resolved_addr,
  1169. &method_used,&hostname_out);
  1170. tt_want(n_gethostname_localhost == prev_n_gethostname_localhost + 1);
  1171. tt_assert(retval == -1);
  1172. UNMOCK(tor_gethostname);
  1173. done:
  1174. tor_free(options->Address);
  1175. tor_free(options->DirAuthorities);
  1176. or_options_free(options);
  1177. tor_free(hostname_out);
  1178. UNMOCK(tor_gethostname);
  1179. UNMOCK(tor_lookup_hostname);
  1180. UNMOCK(get_interface_address);
  1181. UNMOCK(get_interface_address6);
  1182. UNMOCK(tor_gethostname);
  1183. }
  1184. #define CONFIG_TEST(name, flags) \
  1185. { #name, test_config_ ## name, flags, NULL, NULL }
  1186. struct testcase_t config_tests[] = {
  1187. CONFIG_TEST(resolve_my_address, TT_FORK),
  1188. CONFIG_TEST(addressmap, 0),
  1189. CONFIG_TEST(parse_bridge_line, 0),
  1190. CONFIG_TEST(parse_transport_options_line, 0),
  1191. CONFIG_TEST(parse_transport_plugin_line, TT_FORK),
  1192. CONFIG_TEST(check_or_create_data_subdir, TT_FORK),
  1193. CONFIG_TEST(write_to_data_subdir, TT_FORK),
  1194. CONFIG_TEST(fix_my_family, 0),
  1195. END_OF_TESTCASES
  1196. };