test_config.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2013, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #include "orconfig.h"
  6. #define CONFIG_PRIVATE
  7. #include "or.h"
  8. #include "addressmap.h"
  9. #include "config.h"
  10. #include "confparse.h"
  11. #include "connection_edge.h"
  12. #include "test.h"
  13. #include "util.h"
  14. #include "address.h"
  15. static void
  16. test_config_addressmap(void *arg)
  17. {
  18. char buf[1024];
  19. char address[256];
  20. time_t expires = TIME_MAX;
  21. (void)arg;
  22. strlcpy(buf, "MapAddress .invalidwildcard.com *.torserver.exit\n" // invalid
  23. "MapAddress *invalidasterisk.com *.torserver.exit\n" // invalid
  24. "MapAddress *.google.com *.torserver.exit\n"
  25. "MapAddress *.yahoo.com *.google.com.torserver.exit\n"
  26. "MapAddress *.cn.com www.cnn.com\n"
  27. "MapAddress *.cnn.com www.cnn.com\n"
  28. "MapAddress ex.com www.cnn.com\n"
  29. "MapAddress ey.com *.cnn.com\n"
  30. "MapAddress www.torproject.org 1.1.1.1\n"
  31. "MapAddress other.torproject.org "
  32. "this.torproject.org.otherserver.exit\n"
  33. "MapAddress test.torproject.org 2.2.2.2\n"
  34. "MapAddress www.google.com 3.3.3.3\n"
  35. "MapAddress www.example.org 4.4.4.4\n"
  36. "MapAddress 4.4.4.4 7.7.7.7\n"
  37. "MapAddress 4.4.4.4 5.5.5.5\n"
  38. "MapAddress www.infiniteloop.org 6.6.6.6\n"
  39. "MapAddress 6.6.6.6 www.infiniteloop.org\n"
  40. , sizeof(buf));
  41. config_get_lines(buf, &(get_options_mutable()->AddressMap), 0);
  42. config_register_addressmaps(get_options());
  43. /* Use old interface for now, so we don't need to rewrite the unit tests */
  44. #define addressmap_rewrite(a,s,eo,ao) \
  45. addressmap_rewrite((a),(s),AMR_FLAG_USE_IPV4_DNS|AMR_FLAG_USE_IPV6_DNS, \
  46. (eo),(ao))
  47. /* MapAddress .invalidwildcard.com .torserver.exit - no match */
  48. strlcpy(address, "www.invalidwildcard.com", sizeof(address));
  49. test_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  50. /* MapAddress *invalidasterisk.com .torserver.exit - no match */
  51. strlcpy(address, "www.invalidasterisk.com", sizeof(address));
  52. test_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  53. /* Where no mapping for FQDN match on top-level domain */
  54. /* MapAddress .google.com .torserver.exit */
  55. strlcpy(address, "reader.google.com", sizeof(address));
  56. test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  57. test_streq(address, "reader.torserver.exit");
  58. /* MapAddress *.yahoo.com *.google.com.torserver.exit */
  59. strlcpy(address, "reader.yahoo.com", sizeof(address));
  60. test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  61. test_streq(address, "reader.google.com.torserver.exit");
  62. /*MapAddress *.cnn.com www.cnn.com */
  63. strlcpy(address, "cnn.com", sizeof(address));
  64. test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  65. test_streq(address, "www.cnn.com");
  66. /* MapAddress .cn.com www.cnn.com */
  67. strlcpy(address, "www.cn.com", sizeof(address));
  68. test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  69. test_streq(address, "www.cnn.com");
  70. /* MapAddress ex.com www.cnn.com - no match */
  71. strlcpy(address, "www.ex.com", sizeof(address));
  72. test_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  73. /* MapAddress ey.com *.cnn.com - invalid expression */
  74. strlcpy(address, "ey.com", sizeof(address));
  75. test_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  76. /* Where mapping for FQDN match on FQDN */
  77. strlcpy(address, "www.google.com", sizeof(address));
  78. test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  79. test_streq(address, "3.3.3.3");
  80. strlcpy(address, "www.torproject.org", sizeof(address));
  81. test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  82. test_streq(address, "1.1.1.1");
  83. strlcpy(address, "other.torproject.org", sizeof(address));
  84. test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  85. test_streq(address, "this.torproject.org.otherserver.exit");
  86. strlcpy(address, "test.torproject.org", sizeof(address));
  87. test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  88. test_streq(address, "2.2.2.2");
  89. /* Test a chain of address mappings and the order in which they were added:
  90. "MapAddress www.example.org 4.4.4.4"
  91. "MapAddress 4.4.4.4 7.7.7.7"
  92. "MapAddress 4.4.4.4 5.5.5.5"
  93. */
  94. strlcpy(address, "www.example.org", sizeof(address));
  95. test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  96. test_streq(address, "5.5.5.5");
  97. /* Test infinite address mapping results in no change */
  98. strlcpy(address, "www.infiniteloop.org", sizeof(address));
  99. test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  100. test_streq(address, "www.infiniteloop.org");
  101. /* Test we don't find false positives */
  102. strlcpy(address, "www.example.com", sizeof(address));
  103. test_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  104. /* Test top-level-domain matching a bit harder */
  105. addressmap_clear_configured();
  106. strlcpy(buf, "MapAddress *.com *.torserver.exit\n"
  107. "MapAddress *.torproject.org 1.1.1.1\n"
  108. "MapAddress *.net 2.2.2.2\n"
  109. , sizeof(buf));
  110. config_get_lines(buf, &(get_options_mutable()->AddressMap), 0);
  111. config_register_addressmaps(get_options());
  112. strlcpy(address, "www.abc.com", sizeof(address));
  113. test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  114. test_streq(address, "www.abc.torserver.exit");
  115. strlcpy(address, "www.def.com", sizeof(address));
  116. test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  117. test_streq(address, "www.def.torserver.exit");
  118. strlcpy(address, "www.torproject.org", sizeof(address));
  119. test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  120. test_streq(address, "1.1.1.1");
  121. strlcpy(address, "test.torproject.org", sizeof(address));
  122. test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  123. test_streq(address, "1.1.1.1");
  124. strlcpy(address, "torproject.net", sizeof(address));
  125. test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  126. test_streq(address, "2.2.2.2");
  127. /* We don't support '*' as a mapping directive */
  128. addressmap_clear_configured();
  129. strlcpy(buf, "MapAddress * *.torserver.exit\n", sizeof(buf));
  130. config_get_lines(buf, &(get_options_mutable()->AddressMap), 0);
  131. config_register_addressmaps(get_options());
  132. strlcpy(address, "www.abc.com", sizeof(address));
  133. test_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  134. strlcpy(address, "www.def.net", sizeof(address));
  135. test_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  136. strlcpy(address, "www.torproject.org", sizeof(address));
  137. test_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  138. #undef addressmap_rewrite
  139. done:
  140. ;
  141. }
  142. static int
  143. is_private_dir(const char* path)
  144. {
  145. struct stat st;
  146. int r = stat(path, &st);
  147. if (r) {
  148. return 0;
  149. }
  150. #if !defined (_WIN32) || defined (WINCE)
  151. if ((st.st_mode & (S_IFDIR | 0777)) != (S_IFDIR | 0700)) {
  152. return 0;
  153. }
  154. #endif
  155. return 1;
  156. }
  157. static void
  158. test_config_check_or_create_data_subdir(void *arg)
  159. {
  160. or_options_t *options = get_options_mutable();
  161. char *datadir = options->DataDirectory = tor_strdup(get_fname("datadir-0"));
  162. const char *subdir = "test_stats";
  163. char *subpath = get_datadir_fname(subdir);
  164. struct stat st;
  165. int r;
  166. #if !defined (_WIN32) || defined (WINCE)
  167. unsigned group_permission;
  168. #endif
  169. (void)arg;
  170. #if defined (_WIN32) && !defined (WINCE)
  171. tt_int_op(mkdir(options->DataDirectory), ==, 0);
  172. #else
  173. tt_int_op(mkdir(options->DataDirectory, 0700), ==, 0);
  174. #endif
  175. r = stat(subpath, &st);
  176. // The subdirectory shouldn't exist yet,
  177. // but should be created by the call to check_or_create_data_subdir.
  178. test_assert(r && (errno == ENOENT));
  179. test_assert(!check_or_create_data_subdir(subdir));
  180. test_assert(is_private_dir(subpath));
  181. // The check should return 0, if the directory already exists
  182. // and is private to the user.
  183. test_assert(!check_or_create_data_subdir(subdir));
  184. r = stat(subpath, &st);
  185. if (r) {
  186. tt_abort_perror("stat");
  187. }
  188. #if !defined (_WIN32) || defined (WINCE)
  189. group_permission = st.st_mode | 0070;
  190. r = chmod(subpath, group_permission);
  191. if (r) {
  192. tt_abort_perror("chmod");
  193. }
  194. // If the directory exists, but its mode is too permissive
  195. // a call to check_or_create_data_subdir should reset the mode.
  196. test_assert(!is_private_dir(subpath));
  197. test_assert(!check_or_create_data_subdir(subdir));
  198. test_assert(is_private_dir(subpath));
  199. #endif
  200. done:
  201. rmdir(subpath);
  202. tor_free(datadir);
  203. tor_free(subpath);
  204. }
  205. static void
  206. test_config_write_to_data_subdir(void *arg)
  207. {
  208. or_options_t* options = get_options_mutable();
  209. char *datadir = options->DataDirectory = tor_strdup(get_fname("datadir-1"));
  210. char *cp = NULL;
  211. const char* subdir = "test_stats";
  212. const char* fname = "test_file";
  213. const char* str =
  214. "Lorem ipsum dolor sit amet, consetetur sadipscing\n"
  215. "elitr, sed diam nonumy eirmod\n"
  216. "tempor invidunt ut labore et dolore magna aliquyam\n"
  217. "erat, sed diam voluptua.\n"
  218. "At vero eos et accusam et justo duo dolores et ea\n"
  219. "rebum. Stet clita kasd gubergren,\n"
  220. "no sea takimata sanctus est Lorem ipsum dolor sit amet.\n"
  221. "Lorem ipsum dolor sit amet,\n"
  222. "consetetur sadipscing elitr, sed diam nonumy eirmod\n"
  223. "tempor invidunt ut labore et dolore\n"
  224. "magna aliquyam erat, sed diam voluptua. At vero eos et\n"
  225. "accusam et justo duo dolores et\n"
  226. "ea rebum. Stet clita kasd gubergren, no sea takimata\n"
  227. "sanctus est Lorem ipsum dolor sit amet.";
  228. char* filepath = get_datadir_fname2(subdir, fname);
  229. (void)arg;
  230. #if defined (_WIN32) && !defined (WINCE)
  231. tt_int_op(mkdir(options->DataDirectory), ==, 0);
  232. #else
  233. tt_int_op(mkdir(options->DataDirectory, 0700), ==, 0);
  234. #endif
  235. // Write attempt shoudl fail, if subdirectory doesn't exist.
  236. test_assert(write_to_data_subdir(subdir, fname, str, NULL));
  237. test_assert(! check_or_create_data_subdir(subdir));
  238. // Content of file after write attempt should be
  239. // equal to the original string.
  240. test_assert(!write_to_data_subdir(subdir, fname, str, NULL));
  241. cp = read_file_to_str(filepath, 0, NULL);
  242. test_streq(cp, str);
  243. tor_free(cp);
  244. // A second write operation should overwrite the old content.
  245. test_assert(!write_to_data_subdir(subdir, fname, str, NULL));
  246. cp = read_file_to_str(filepath, 0, NULL);
  247. test_streq(cp, str);
  248. tor_free(cp);
  249. done:
  250. (void) unlink(filepath);
  251. rmdir(options->DataDirectory);
  252. tor_free(datadir);
  253. tor_free(filepath);
  254. tor_free(cp);
  255. }
  256. /* Test helper function: Make sure that a bridge line gets parsed
  257. * properly. Also make sure that the resulting bridge_line_t structure
  258. * has its fields set correctly. */
  259. static void
  260. good_bridge_line_test(const char *string, const char *test_addrport,
  261. const char *test_digest, const char *test_transport,
  262. const smartlist_t *test_socks_args)
  263. {
  264. char *tmp = NULL;
  265. bridge_line_t *bridge_line = parse_bridge_line(string);
  266. test_assert(bridge_line);
  267. /* test addrport */
  268. tmp = tor_strdup(fmt_addrport(&bridge_line->addr, bridge_line->port));
  269. test_streq(test_addrport, tmp);
  270. tor_free(tmp);
  271. /* If we were asked to validate a digest, but we did not get a
  272. digest after parsing, we failed. */
  273. if (test_digest && tor_digest_is_zero(bridge_line->digest))
  274. test_assert(0);
  275. /* If we were not asked to validate a digest, and we got a digest
  276. after parsing, we failed again. */
  277. if (!test_digest && !tor_digest_is_zero(bridge_line->digest))
  278. test_assert(0);
  279. /* If we were asked to validate a digest, and we got a digest after
  280. parsing, make sure it's correct. */
  281. if (test_digest) {
  282. tmp = tor_strdup(hex_str(bridge_line->digest, DIGEST_LEN));
  283. tor_strlower(tmp);
  284. test_streq(test_digest, tmp);
  285. tor_free(tmp);
  286. }
  287. /* If we were asked to validate a transport name, make sure tha it
  288. matches with the transport name that was parsed. */
  289. if (test_transport && !bridge_line->transport_name)
  290. test_assert(0);
  291. if (!test_transport && bridge_line->transport_name)
  292. test_assert(0);
  293. if (test_transport)
  294. test_streq(test_transport, bridge_line->transport_name);
  295. /* Validate the SOCKS argument smartlist. */
  296. if (test_socks_args && !bridge_line->socks_args)
  297. test_assert(0);
  298. if (!test_socks_args && bridge_line->socks_args)
  299. test_assert(0);
  300. if (test_socks_args)
  301. test_assert(smartlist_strings_eq(test_socks_args,
  302. bridge_line->socks_args));
  303. done:
  304. tor_free(tmp);
  305. bridge_line_free(bridge_line);
  306. }
  307. /* Test helper function: Make sure that a bridge line is
  308. * unparseable. */
  309. static void
  310. bad_bridge_line_test(const char *string)
  311. {
  312. bridge_line_t *bridge_line = parse_bridge_line(string);
  313. if (bridge_line)
  314. TT_FAIL(("%s was supposed to fail, but it didn't.", string));
  315. test_assert(!bridge_line);
  316. done:
  317. bridge_line_free(bridge_line);
  318. }
  319. static void
  320. test_config_parse_bridge_line(void *arg)
  321. {
  322. (void) arg;
  323. good_bridge_line_test("192.0.2.1:4123",
  324. "192.0.2.1:4123", NULL, NULL, NULL);
  325. good_bridge_line_test("192.0.2.1",
  326. "192.0.2.1:443", NULL, NULL, NULL);
  327. good_bridge_line_test("transport [::1]",
  328. "[::1]:443", NULL, "transport", NULL);
  329. good_bridge_line_test("transport 192.0.2.1:12 "
  330. "4352e58420e68f5e40bf7c74faddccd9d1349413",
  331. "192.0.2.1:12",
  332. "4352e58420e68f5e40bf7c74faddccd9d1349413",
  333. "transport", NULL);
  334. {
  335. smartlist_t *sl_tmp = smartlist_new();
  336. smartlist_add_asprintf(sl_tmp, "twoandtwo=five");
  337. good_bridge_line_test("transport 192.0.2.1:12 "
  338. "4352e58420e68f5e40bf7c74faddccd9d1349413 twoandtwo=five",
  339. "192.0.2.1:12", "4352e58420e68f5e40bf7c74faddccd9d1349413",
  340. "transport", sl_tmp);
  341. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  342. smartlist_free(sl_tmp);
  343. }
  344. {
  345. smartlist_t *sl_tmp = smartlist_new();
  346. smartlist_add_asprintf(sl_tmp, "twoandtwo=five");
  347. smartlist_add_asprintf(sl_tmp, "z=z");
  348. good_bridge_line_test("transport 192.0.2.1:12 twoandtwo=five z=z",
  349. "192.0.2.1:12", NULL, "transport", sl_tmp);
  350. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  351. smartlist_free(sl_tmp);
  352. }
  353. {
  354. smartlist_t *sl_tmp = smartlist_new();
  355. smartlist_add_asprintf(sl_tmp, "dub=come");
  356. smartlist_add_asprintf(sl_tmp, "save=me");
  357. good_bridge_line_test("transport 192.0.2.1:12 "
  358. "4352e58420e68f5e40bf7c74faddccd9d1349666 "
  359. "dub=come save=me",
  360. "192.0.2.1:12",
  361. "4352e58420e68f5e40bf7c74faddccd9d1349666",
  362. "transport", sl_tmp);
  363. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  364. smartlist_free(sl_tmp);
  365. }
  366. good_bridge_line_test("192.0.2.1:1231 "
  367. "4352e58420e68f5e40bf7c74faddccd9d1349413",
  368. "192.0.2.1:1231",
  369. "4352e58420e68f5e40bf7c74faddccd9d1349413",
  370. NULL, NULL);
  371. /* Empty line */
  372. bad_bridge_line_test("");
  373. /* bad transport name */
  374. bad_bridge_line_test("tr$n_sp0r7 190.20.2.2");
  375. /* weird ip address */
  376. bad_bridge_line_test("a.b.c.d");
  377. /* invalid fpr */
  378. bad_bridge_line_test("2.2.2.2:1231 4352e58420e68f5e40bf7c74faddccd9d1349");
  379. /* no k=v in the end */
  380. bad_bridge_line_test("obfs2 2.2.2.2:1231 "
  381. "4352e58420e68f5e40bf7c74faddccd9d1349413 what");
  382. /* no addrport */
  383. bad_bridge_line_test("asdw");
  384. /* huge k=v value that can't fit in SOCKS fields */
  385. bad_bridge_line_test(
  386. "obfs2 2.2.2.2:1231 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  387. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  388. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  389. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  390. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  391. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  392. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  393. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  394. "aa=b");
  395. }
  396. static void
  397. test_config_parse_transport_options_line(void *arg)
  398. {
  399. smartlist_t *options_sl = NULL, *sl_tmp = NULL;
  400. (void) arg;
  401. { /* too small line */
  402. options_sl = get_options_from_transport_options_line("valley", NULL);
  403. test_assert(!options_sl);
  404. }
  405. { /* no k=v values */
  406. options_sl = get_options_from_transport_options_line("hit it!", NULL);
  407. test_assert(!options_sl);
  408. }
  409. { /* correct line, but wrong transport specified */
  410. options_sl =
  411. get_options_from_transport_options_line("trebuchet k=v", "rook");
  412. test_assert(!options_sl);
  413. }
  414. { /* correct -- no transport specified */
  415. sl_tmp = smartlist_new();
  416. smartlist_add_asprintf(sl_tmp, "ladi=dadi");
  417. smartlist_add_asprintf(sl_tmp, "weliketo=party");
  418. options_sl =
  419. get_options_from_transport_options_line("rook ladi=dadi weliketo=party",
  420. NULL);
  421. test_assert(options_sl);
  422. test_assert(smartlist_strings_eq(options_sl, sl_tmp));
  423. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  424. smartlist_free(sl_tmp);
  425. sl_tmp = NULL;
  426. SMARTLIST_FOREACH(options_sl, char *, s, tor_free(s));
  427. smartlist_free(options_sl);
  428. options_sl = NULL;
  429. }
  430. { /* correct -- correct transport specified */
  431. sl_tmp = smartlist_new();
  432. smartlist_add_asprintf(sl_tmp, "ladi=dadi");
  433. smartlist_add_asprintf(sl_tmp, "weliketo=party");
  434. options_sl =
  435. get_options_from_transport_options_line("rook ladi=dadi weliketo=party",
  436. "rook");
  437. test_assert(options_sl);
  438. test_assert(smartlist_strings_eq(options_sl, sl_tmp));
  439. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  440. smartlist_free(sl_tmp);
  441. sl_tmp = NULL;
  442. SMARTLIST_FOREACH(options_sl, char *, s, tor_free(s));
  443. smartlist_free(options_sl);
  444. options_sl = NULL;
  445. }
  446. done:
  447. if (options_sl) {
  448. SMARTLIST_FOREACH(options_sl, char *, s, tor_free(s));
  449. smartlist_free(options_sl);
  450. }
  451. if (sl_tmp) {
  452. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  453. smartlist_free(sl_tmp);
  454. }
  455. }
  456. // Tests if an options with MyFamily fingerprints missing '$' normalises
  457. // them correctly and also ensure it also works with multiple fingerprints
  458. static void
  459. test_config_fix_my_family(void *arg)
  460. {
  461. char *err = NULL;
  462. const char *family = "$1111111111111111111111111111111111111111, "
  463. "1111111111111111111111111111111111111112, "
  464. "$1111111111111111111111111111111111111113";
  465. or_options_t* options = options_new();
  466. or_options_t* defaults = options_new();
  467. (void) arg;
  468. options_init(options);
  469. options_init(defaults);
  470. options->MyFamily = tor_strdup(family);
  471. options_validate(NULL, options, defaults, 0, &err) ;
  472. if (err != NULL) {
  473. TT_FAIL(("options_validate failed: %s", err));
  474. }
  475. test_streq(options->MyFamily, "$1111111111111111111111111111111111111111, "
  476. "$1111111111111111111111111111111111111112, "
  477. "$1111111111111111111111111111111111111113");
  478. done:
  479. if (err != NULL) {
  480. tor_free(err);
  481. }
  482. or_options_free(options);
  483. or_options_free(defaults);
  484. }
  485. static int n_hostname_01010101 = 0;
  486. /** This mock function is meant to replace tor_lookup_hostname().
  487. * It answers with 1.1.1.1 as IP adddress that resulted from lookup.
  488. * This function increments <b>n_hostname_01010101</b> counter by one
  489. * every time it is called.
  490. */
  491. static int
  492. tor_lookup_hostname_01010101(const char *name, uint32_t *addr)
  493. {
  494. n_hostname_01010101++;
  495. if (name && addr) {
  496. *addr = ntohl(0x01010101);
  497. }
  498. return 0;
  499. }
  500. static int n_hostname_localhost = 0;
  501. /** This mock function is meant to replace tor_lookup_hostname().
  502. * It answers with 127.0.0.1 as IP adddress that resulted from lookup.
  503. * This function increments <b>n_hostname_localhost</b> counter by one
  504. * every time it is called.
  505. */
  506. static int
  507. tor_lookup_hostname_localhost(const char *name, uint32_t *addr)
  508. {
  509. n_hostname_localhost++;
  510. if (name && addr) {
  511. *addr = 0x7f000001;
  512. }
  513. return 0;
  514. }
  515. static int n_hostname_failure = 0;
  516. /** This mock function is meant to replace tor_lookup_hostname().
  517. * It pretends to fail by returning -1 to caller. Also, this function
  518. * increments <b>n_hostname_failure</b> every time it is called.
  519. */
  520. static int
  521. tor_lookup_hostname_failure(const char *name, uint32_t *addr)
  522. {
  523. (void)name;
  524. (void)addr;
  525. n_hostname_failure++;
  526. return -1;
  527. }
  528. static int n_gethostname_replacement = 0;
  529. /** This mock function is meant to replace tor_gethostname(). It
  530. * responds with string "onionrouter" as hostname. This function
  531. * increments <b>n_gethostname_replacement</b> by one every time
  532. * it is called.
  533. */
  534. static int
  535. tor_gethostname_replacement(char *name, size_t namelen)
  536. {
  537. n_gethostname_replacement++;
  538. if (name && namelen) {
  539. strlcpy(name,"onionrouter",namelen);
  540. }
  541. return 0;
  542. }
  543. static int n_gethostname_localhost = 0;
  544. /** This mock function is meant to replace tor_gethostname(). It
  545. * responds with string "127.0.0.1" as hostname. This function
  546. * increments <b>n_gethostname_localhost</b> by one every time
  547. * it is called.
  548. */
  549. static int
  550. tor_gethostname_localhost(char *name, size_t namelen)
  551. {
  552. n_gethostname_localhost++;
  553. if (name && namelen) {
  554. strlcpy(name,"127.0.0.1",namelen);
  555. }
  556. return 0;
  557. }
  558. static int n_gethostname_failure = 0;
  559. /** This mock function is meant to replace tor_gethostname.
  560. * It pretends to fail by returning -1. This function increments
  561. * <b>n_gethostname_failure</b> by one every time it is called.
  562. */
  563. static int
  564. tor_gethostname_failure(char *name, size_t namelen)
  565. {
  566. (void)name;
  567. (void)namelen;
  568. n_gethostname_failure++;
  569. return -1;
  570. }
  571. static int n_get_interface_address = 0;
  572. /** This mock function is meant to replace get_interface_address().
  573. * It answers with address 8.8.8.8. This function increments
  574. * <b>n_get_interface_address</b> by one every time it is called.
  575. */
  576. static int
  577. get_interface_address_08080808(int severity, uint32_t *addr)
  578. {
  579. (void)severity;
  580. n_get_interface_address++;
  581. if (addr) {
  582. *addr = ntohl(0x08080808);
  583. }
  584. return 0;
  585. }
  586. static int n_get_interface_address6 = 0;
  587. static sa_family_t last_address6_family;
  588. /** This mock function is meant to replace get_interface_address6().
  589. * It answers with IP address 9.9.9.9 iff both of the following are true:
  590. * - <b>family</b> is AF_INET
  591. * - <b>addr</b> pointer is not NULL.
  592. * This function increments <b>n_get_interface_address6</b> by one every
  593. * time it is called.
  594. */
  595. static int
  596. get_interface_address6_replacement(int severity, sa_family_t family,
  597. tor_addr_t *addr)
  598. {
  599. (void)severity;
  600. last_address6_family = family;
  601. n_get_interface_address6++;
  602. if ((family != AF_INET) || !addr) {
  603. return -1;
  604. }
  605. tor_addr_from_ipv4h(addr,0x09090909);
  606. return 0;
  607. }
  608. static int n_get_interface_address_failure = 0;
  609. /**
  610. * This mock function is meant to replace get_interface_address().
  611. * It pretends to fail getting interface address by returning -1.
  612. * <b>n_get_interface_address_failure</b> is incremented by one
  613. * every time this function is called.
  614. */
  615. static int
  616. get_interface_address_failure(int severity, uint32_t *addr)
  617. {
  618. (void)severity;
  619. (void)addr;
  620. n_get_interface_address_failure++;
  621. return -1;
  622. }
  623. static int n_get_interface_address6_failure = 0;
  624. /**
  625. * This mock function is meant to replace get_interface_addres6().
  626. * It will pretent to fail by return -1.
  627. * <b>n_get_interface_address6_failure</b> is incremented by one
  628. * every time this function is called and <b>last_address6_family</b>
  629. * is assigned the value of <b>family</b> argument.
  630. */
  631. static int
  632. get_interface_address6_failure(int severity, sa_family_t family,
  633. tor_addr_t *addr)
  634. {
  635. (void)severity;
  636. (void)addr;
  637. n_get_interface_address6_failure++;
  638. last_address6_family = family;
  639. return -1;
  640. }
  641. static void
  642. test_config_resolve_my_address(void *arg)
  643. {
  644. or_options_t *options;
  645. uint32_t resolved_addr;
  646. const char *method_used;
  647. char *hostname_out;
  648. int retval;
  649. int prev_n_hostname_01010101;
  650. int prev_n_hostname_localhost;
  651. int prev_n_hostname_failure;
  652. int prev_n_gethostname_replacement;
  653. int prev_n_gethostname_failure;
  654. int prev_n_gethostname_localhost;
  655. int prev_n_get_interface_address;
  656. int prev_n_get_interface_address_failure;
  657. int prev_n_get_interface_address6;
  658. int prev_n_get_interface_address6_failure;
  659. (void)arg;
  660. options = options_new();
  661. options_init(options);
  662. /*
  663. * CASE 1:
  664. * If options->Address is a valid IPv4 address string, we want
  665. * the corresponding address to be parsed and returned.
  666. */
  667. options->Address = tor_strdup("128.52.128.105");
  668. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  669. &method_used,&hostname_out);
  670. tt_want(retval == 0);
  671. tt_want_str_op(method_used,==,"CONFIGURED");
  672. tt_want(hostname_out == NULL);
  673. tt_assert(htonl(resolved_addr) == 0x69803480);
  674. tor_free(options->Address);
  675. /*
  676. * CASE 2:
  677. * If options->Address is a valid DNS address, we want resolve_my_address()
  678. * function to ask tor_lookup_hostname() for help with resolving it
  679. * and return the address that was resolved (in host order).
  680. */
  681. MOCK(tor_lookup_hostname,tor_lookup_hostname_01010101);
  682. tor_free(options->Address);
  683. options->Address = tor_strdup("www.torproject.org");
  684. prev_n_hostname_01010101 = n_hostname_01010101;
  685. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  686. &method_used,&hostname_out);
  687. tt_want(retval == 0);
  688. tt_want(n_hostname_01010101 == prev_n_hostname_01010101 + 1);
  689. tt_want_str_op(method_used,==,"RESOLVED");
  690. tt_want_str_op(hostname_out,==,"www.torproject.org");
  691. tt_assert(htonl(resolved_addr) == 0x01010101);
  692. UNMOCK(tor_lookup_hostname);
  693. tor_free(options->Address);
  694. /*
  695. * CASE 3:
  696. * Given that options->Address is NULL, we want resolve_my_address()
  697. * to try and use tor_gethostname() to get hostname AND use
  698. * tor_lookup_hostname() to get IP address.
  699. */
  700. resolved_addr = 0;
  701. tor_free(options->Address);
  702. options->Address = NULL;
  703. MOCK(tor_gethostname,tor_gethostname_replacement);
  704. MOCK(tor_lookup_hostname,tor_lookup_hostname_01010101);
  705. prev_n_gethostname_replacement = n_gethostname_replacement;
  706. prev_n_hostname_01010101 = n_hostname_01010101;
  707. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  708. &method_used,&hostname_out);
  709. tt_want(retval == 0);
  710. tt_want(n_gethostname_replacement == prev_n_gethostname_replacement + 1);
  711. tt_want(n_hostname_01010101 == prev_n_hostname_01010101 + 1);
  712. tt_want_str_op(method_used,==,"GETHOSTNAME");
  713. tt_want_str_op(hostname_out,==,"onionrouter");
  714. tt_assert(htonl(resolved_addr) == 0x01010101);
  715. UNMOCK(tor_gethostname);
  716. UNMOCK(tor_lookup_hostname);
  717. /*
  718. * CASE 4:
  719. * Given that options->Address is a local host address, we want
  720. * resolve_my_address() function to fail.
  721. */
  722. resolved_addr = 0;
  723. tor_free(options->Address);
  724. options->Address = tor_strdup("127.0.0.1");
  725. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  726. &method_used,&hostname_out);
  727. tt_want(resolved_addr == 0);
  728. tt_assert(retval == -1);
  729. tor_free(options->Address);
  730. /*
  731. * CASE 5:
  732. * We want resolve_my_address() to fail if DNS address in options->Address
  733. * cannot be resolved.
  734. */
  735. MOCK(tor_lookup_hostname,tor_lookup_hostname_failure);
  736. prev_n_hostname_failure = n_hostname_failure;
  737. tor_free(options->Address);
  738. options->Address = tor_strdup("www.tor-project.org");
  739. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  740. &method_used,&hostname_out);
  741. tt_want(n_hostname_failure == prev_n_hostname_failure + 1);
  742. tt_assert(retval == -1);
  743. UNMOCK(tor_lookup_hostname);
  744. tor_free(options->Address);
  745. options->Address = NULL;
  746. /*
  747. * CASE 6:
  748. * If options->Address is NULL AND gettting local hostname fails, we want
  749. * resolve_my_address() to fail as well.
  750. */
  751. MOCK(tor_gethostname,tor_gethostname_failure);
  752. prev_n_gethostname_failure = n_gethostname_failure;
  753. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  754. &method_used,&hostname_out);
  755. tt_want(n_gethostname_failure == prev_n_gethostname_failure + 1);
  756. tt_assert(retval == -1);
  757. UNMOCK(tor_gethostname);
  758. /*
  759. * CASE 7:
  760. * We want resolve_my_address() to try and get network interface address via
  761. * get_interface_address() if hostname returned by tor_gethostname() cannot be
  762. * resolved into IP address.
  763. */
  764. MOCK(tor_gethostname,tor_gethostname_replacement);
  765. MOCK(get_interface_address,get_interface_address_08080808);
  766. prev_n_gethostname_replacement = n_gethostname_replacement;
  767. prev_n_get_interface_address = n_get_interface_address;
  768. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  769. &method_used,&hostname_out);
  770. tt_want(retval == 0);
  771. tt_want(n_gethostname_replacement == prev_n_gethostname_replacement + 1);
  772. tt_want(n_get_interface_address == prev_n_get_interface_address + 1);
  773. tt_want_str_op(method_used,==,"INTERFACE");
  774. tt_want(hostname_out == NULL);
  775. tt_assert(resolved_addr == ntohl(0x08080808));
  776. UNMOCK(get_interface_address);
  777. /*
  778. * CASE 8:
  779. * Suppose options->Address is NULL AND hostname returned by tor_gethostname()
  780. * is unresolvable. We want resolve_my_address to fail if
  781. * get_interface_address() fails.
  782. */
  783. MOCK(get_interface_address,get_interface_address_failure);
  784. prev_n_get_interface_address_failure = n_get_interface_address_failure;
  785. prev_n_gethostname_replacement = n_gethostname_replacement;
  786. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  787. &method_used,&hostname_out);
  788. tt_want(n_get_interface_address_failure ==
  789. prev_n_get_interface_address_failure + 1);
  790. tt_want(n_gethostname_replacement ==
  791. prev_n_gethostname_replacement + 1);
  792. tt_assert(retval == -1);
  793. UNMOCK(get_interface_address);
  794. /*
  795. * CASE 9:
  796. * Given that options->Address is NULL AND tor_lookup_hostname()
  797. * fails AND hostname returned by gethostname() resolves
  798. * to local IP address, we want resolve_my_address() function to
  799. * call get_interface_address6(.,AF_INET,.) and return IP address
  800. * the latter function has found.
  801. */
  802. MOCK(tor_lookup_hostname,tor_lookup_hostname_failure);
  803. MOCK(tor_gethostname,tor_gethostname_replacement);
  804. MOCK(get_interface_address6,get_interface_address6_replacement);
  805. prev_n_gethostname_replacement = n_gethostname_replacement;
  806. prev_n_hostname_failure = n_hostname_failure;
  807. prev_n_get_interface_address6 = n_get_interface_address6;
  808. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  809. &method_used,&hostname_out);
  810. tt_want(last_address6_family == AF_INET);
  811. tt_want(n_get_interface_address6 == prev_n_get_interface_address6 + 1);
  812. tt_want(n_hostname_failure == prev_n_hostname_failure + 1);
  813. tt_want(n_gethostname_replacement == prev_n_gethostname_replacement + 1);
  814. tt_want(retval == 0);
  815. tt_want_str_op(method_used,==,"INTERFACE");
  816. tt_assert(htonl(resolved_addr) == 0x09090909);
  817. UNMOCK(tor_lookup_hostname);
  818. UNMOCK(tor_gethostname);
  819. UNMOCK(get_interface_address6);
  820. /*
  821. * CASE 10: We want resolve_my_address() to fail if all of the following
  822. * are true:
  823. * 1. options->Address is not NULL
  824. * 2. ... but it cannot be converted to struct in_addr by
  825. * tor_inet_aton()
  826. * 3. ... and tor_lookup_hostname() fails to resolve the
  827. * options->Address
  828. */
  829. MOCK(tor_lookup_hostname,tor_lookup_hostname_failure);
  830. prev_n_hostname_failure = n_hostname_failure;
  831. tor_free(options->Address);
  832. options->Address = tor_strdup("some_hostname");
  833. retval = resolve_my_address(LOG_NOTICE, options, &resolved_addr,
  834. &method_used,&hostname_out);
  835. tt_want(n_hostname_failure == prev_n_hostname_failure + 1);
  836. tt_assert(retval == -1);
  837. UNMOCK(tor_gethostname);
  838. UNMOCK(tor_lookup_hostname);
  839. /*
  840. * CASE 11:
  841. * Suppose the following sequence of events:
  842. * 1. options->Address is NULL
  843. * 2. tor_gethostname() succeeds to get hostname of machine Tor
  844. * if running on.
  845. * 3. Hostname from previous step cannot be converted to
  846. * address by using tor_inet_aton() function.
  847. * 4. However, tor_lookup_hostname() succeds in resolving the
  848. * hostname from step 2.
  849. * 5. Unfortunately, tor_addr_is_internal() deems this address
  850. * to be internal.
  851. * 6. get_interface_address6(.,AF_INET,.) returns non-internal
  852. * IPv4
  853. *
  854. * We want resolve_my_addr() to succeed with method "INTERFACE"
  855. * and address from step 6.
  856. */
  857. tor_free(options->Address);
  858. options->Address = NULL;
  859. MOCK(tor_gethostname,tor_gethostname_replacement);
  860. MOCK(tor_lookup_hostname,tor_lookup_hostname_localhost);
  861. MOCK(get_interface_address6,get_interface_address6_replacement);
  862. prev_n_gethostname_replacement = n_gethostname_replacement;
  863. prev_n_hostname_localhost = n_hostname_localhost;
  864. prev_n_get_interface_address6 = n_get_interface_address6;
  865. retval = resolve_my_address(LOG_DEBUG, options, &resolved_addr,
  866. &method_used,&hostname_out);
  867. tt_want(n_gethostname_replacement == prev_n_gethostname_replacement + 1);
  868. tt_want(n_hostname_localhost == prev_n_hostname_localhost + 1);
  869. tt_want(n_get_interface_address6 == prev_n_get_interface_address6 + 1);
  870. tt_str_op(method_used,==,"INTERFACE");
  871. tt_assert(!hostname_out);
  872. tt_assert(retval == 0);
  873. /*
  874. * CASE 11b:
  875. * 1-5 as above.
  876. * 6. get_interface_address6() fails.
  877. *
  878. * In this subcase, we want resolve_my_address() to fail.
  879. */
  880. UNMOCK(get_interface_address6);
  881. MOCK(get_interface_address6,get_interface_address6_failure);
  882. prev_n_gethostname_replacement = n_gethostname_replacement;
  883. prev_n_hostname_localhost = n_hostname_localhost;
  884. prev_n_get_interface_address6_failure = n_get_interface_address6_failure;
  885. retval = resolve_my_address(LOG_DEBUG, options, &resolved_addr,
  886. &method_used,&hostname_out);
  887. tt_want(n_gethostname_replacement == prev_n_gethostname_replacement + 1);
  888. tt_want(n_hostname_localhost == prev_n_hostname_localhost + 1);
  889. tt_want(n_get_interface_address6_failure ==
  890. prev_n_get_interface_address6_failure + 1);
  891. tt_assert(retval == -1);
  892. UNMOCK(tor_gethostname);
  893. UNMOCK(tor_lookup_hostname);
  894. UNMOCK(get_interface_address6);
  895. /* CASE 12:
  896. * Suppose the following happens:
  897. * 1. options->Address is NULL AND options->DirAuthorities is 1.
  898. * 2. tor_gethostname() succeeds in getting hostname of a machine ...
  899. * 3. ... which is successfully parsed by tor_inet_aton() ...
  900. * 4. into IPv4 address that tor_addr_is_inernal() considers to be
  901. * internal.
  902. *
  903. * In this case, we want resolve_my_address() to fail.
  904. */
  905. tor_free(options->Address);
  906. options->Address = NULL;
  907. options->DirAuthorities = tor_malloc_zero(sizeof(config_line_t));
  908. MOCK(tor_gethostname,tor_gethostname_localhost);
  909. prev_n_gethostname_localhost = n_gethostname_localhost;
  910. retval = resolve_my_address(LOG_DEBUG, options, &resolved_addr,
  911. &method_used,&hostname_out);
  912. tt_want(n_gethostname_localhost == prev_n_gethostname_localhost + 1);
  913. tt_assert(retval == -1);
  914. UNMOCK(tor_gethostname);
  915. done:
  916. tor_free(options->Address);
  917. tor_free(options->DirAuthorities);
  918. or_options_free(options);
  919. UNMOCK(tor_gethostname);
  920. UNMOCK(tor_lookup_hostname);
  921. UNMOCK(get_interface_address);
  922. UNMOCK(get_interface_address6);
  923. UNMOCK(tor_gethostname);
  924. }
  925. #define CONFIG_TEST(name, flags) \
  926. { #name, test_config_ ## name, flags, NULL, NULL }
  927. struct testcase_t config_tests[] = {
  928. CONFIG_TEST(resolve_my_address, TT_FORK),
  929. CONFIG_TEST(addressmap, 0),
  930. CONFIG_TEST(parse_bridge_line, 0),
  931. CONFIG_TEST(parse_transport_options_line, 0),
  932. CONFIG_TEST(check_or_create_data_subdir, TT_FORK),
  933. CONFIG_TEST(write_to_data_subdir, TT_FORK),
  934. CONFIG_TEST(fix_my_family, 0),
  935. END_OF_TESTCASES
  936. };