test_policy.c 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850
  1. /* Copyright (c) 2013-2016, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include "or.h"
  4. #define CONFIG_PRIVATE
  5. #include "config.h"
  6. #include "router.h"
  7. #include "routerparse.h"
  8. #define POLICIES_PRIVATE
  9. #include "policies.h"
  10. #include "test.h"
  11. /* Helper: assert that short_policy parses and writes back out as itself,
  12. or as <b>expected</b> if that's provided. */
  13. static void
  14. test_short_policy_parse(const char *input,
  15. const char *expected)
  16. {
  17. short_policy_t *short_policy = NULL;
  18. char *out = NULL;
  19. if (expected == NULL)
  20. expected = input;
  21. short_policy = parse_short_policy(input);
  22. tt_assert(short_policy);
  23. out = write_short_policy(short_policy);
  24. tt_str_op(out, OP_EQ, expected);
  25. done:
  26. tor_free(out);
  27. short_policy_free(short_policy);
  28. }
  29. /** Helper: Parse the exit policy string in <b>policy_str</b>, and make sure
  30. * that policies_summarize() produces the string <b>expected_summary</b> from
  31. * it. */
  32. static void
  33. test_policy_summary_helper(const char *policy_str,
  34. const char *expected_summary)
  35. {
  36. config_line_t line;
  37. smartlist_t *policy = smartlist_new();
  38. char *summary = NULL;
  39. char *summary_after = NULL;
  40. int r;
  41. short_policy_t *short_policy = NULL;
  42. line.key = (char*)"foo";
  43. line.value = (char *)policy_str;
  44. line.next = NULL;
  45. r = policies_parse_exit_policy(&line, &policy,
  46. EXIT_POLICY_IPV6_ENABLED |
  47. EXIT_POLICY_ADD_DEFAULT, NULL);
  48. tt_int_op(r,OP_EQ, 0);
  49. summary = policy_summarize(policy, AF_INET);
  50. tt_assert(summary != NULL);
  51. tt_str_op(summary,OP_EQ, expected_summary);
  52. short_policy = parse_short_policy(summary);
  53. tt_assert(short_policy);
  54. summary_after = write_short_policy(short_policy);
  55. tt_str_op(summary,OP_EQ, summary_after);
  56. done:
  57. tor_free(summary_after);
  58. tor_free(summary);
  59. if (policy)
  60. addr_policy_list_free(policy);
  61. short_policy_free(short_policy);
  62. }
  63. /** Run unit tests for generating summary lines of exit policies */
  64. static void
  65. test_policies_general(void *arg)
  66. {
  67. int i;
  68. smartlist_t *policy = NULL, *policy2 = NULL, *policy3 = NULL,
  69. *policy4 = NULL, *policy5 = NULL, *policy6 = NULL,
  70. *policy7 = NULL, *policy8 = NULL, *policy9 = NULL,
  71. *policy10 = NULL, *policy11 = NULL, *policy12 = NULL;
  72. addr_policy_t *p;
  73. tor_addr_t tar, tar2;
  74. smartlist_t *addr_list = NULL;
  75. config_line_t line;
  76. smartlist_t *sm = NULL;
  77. char *policy_str = NULL;
  78. short_policy_t *short_parsed = NULL;
  79. int malformed_list = -1;
  80. (void)arg;
  81. policy = smartlist_new();
  82. p = router_parse_addr_policy_item_from_string("reject 192.168.0.0/16:*", -1,
  83. &malformed_list);
  84. tt_assert(p != NULL);
  85. tt_int_op(ADDR_POLICY_REJECT,OP_EQ, p->policy_type);
  86. tor_addr_from_ipv4h(&tar, 0xc0a80000u);
  87. tt_int_op(0,OP_EQ, tor_addr_compare(&p->addr, &tar, CMP_EXACT));
  88. tt_int_op(16,OP_EQ, p->maskbits);
  89. tt_int_op(1,OP_EQ, p->prt_min);
  90. tt_int_op(65535,OP_EQ, p->prt_max);
  91. smartlist_add(policy, p);
  92. tor_addr_from_ipv4h(&tar, 0x01020304u);
  93. tt_assert(ADDR_POLICY_ACCEPTED ==
  94. compare_tor_addr_to_addr_policy(&tar, 2, policy));
  95. tor_addr_make_unspec(&tar);
  96. tt_assert(ADDR_POLICY_PROBABLY_ACCEPTED ==
  97. compare_tor_addr_to_addr_policy(&tar, 2, policy));
  98. tor_addr_from_ipv4h(&tar, 0xc0a80102);
  99. tt_assert(ADDR_POLICY_REJECTED ==
  100. compare_tor_addr_to_addr_policy(&tar, 2, policy));
  101. tt_int_op(0, OP_EQ, policies_parse_exit_policy(NULL, &policy2,
  102. EXIT_POLICY_IPV6_ENABLED |
  103. EXIT_POLICY_REJECT_PRIVATE |
  104. EXIT_POLICY_ADD_DEFAULT, NULL));
  105. tt_assert(policy2);
  106. tor_addr_from_ipv4h(&tar, 0x0306090cu);
  107. tor_addr_parse(&tar2, "[2000::1234]");
  108. addr_list = smartlist_new();
  109. smartlist_add(addr_list, &tar);
  110. smartlist_add(addr_list, &tar2);
  111. tt_int_op(0, OP_EQ, policies_parse_exit_policy(NULL, &policy12,
  112. EXIT_POLICY_IPV6_ENABLED |
  113. EXIT_POLICY_REJECT_PRIVATE |
  114. EXIT_POLICY_ADD_DEFAULT,
  115. addr_list));
  116. smartlist_free(addr_list);
  117. addr_list = NULL;
  118. tt_assert(policy12);
  119. policy3 = smartlist_new();
  120. p = router_parse_addr_policy_item_from_string("reject *:*", -1,
  121. &malformed_list);
  122. tt_assert(p != NULL);
  123. smartlist_add(policy3, p);
  124. p = router_parse_addr_policy_item_from_string("accept *:*", -1,
  125. &malformed_list);
  126. tt_assert(p != NULL);
  127. smartlist_add(policy3, p);
  128. policy4 = smartlist_new();
  129. p = router_parse_addr_policy_item_from_string("accept *:443", -1,
  130. &malformed_list);
  131. tt_assert(p != NULL);
  132. smartlist_add(policy4, p);
  133. p = router_parse_addr_policy_item_from_string("accept *:443", -1,
  134. &malformed_list);
  135. tt_assert(p != NULL);
  136. smartlist_add(policy4, p);
  137. policy5 = smartlist_new();
  138. p = router_parse_addr_policy_item_from_string("reject 0.0.0.0/8:*", -1,
  139. &malformed_list);
  140. tt_assert(p != NULL);
  141. smartlist_add(policy5, p);
  142. p = router_parse_addr_policy_item_from_string("reject 169.254.0.0/16:*", -1,
  143. &malformed_list);
  144. tt_assert(p != NULL);
  145. smartlist_add(policy5, p);
  146. p = router_parse_addr_policy_item_from_string("reject 127.0.0.0/8:*", -1,
  147. &malformed_list);
  148. tt_assert(p != NULL);
  149. smartlist_add(policy5, p);
  150. p = router_parse_addr_policy_item_from_string("reject 192.168.0.0/16:*",
  151. -1, &malformed_list);
  152. tt_assert(p != NULL);
  153. smartlist_add(policy5, p);
  154. p = router_parse_addr_policy_item_from_string("reject 10.0.0.0/8:*", -1,
  155. &malformed_list);
  156. tt_assert(p != NULL);
  157. smartlist_add(policy5, p);
  158. p = router_parse_addr_policy_item_from_string("reject 172.16.0.0/12:*", -1,
  159. &malformed_list);
  160. tt_assert(p != NULL);
  161. smartlist_add(policy5, p);
  162. p = router_parse_addr_policy_item_from_string("reject 80.190.250.90:*", -1,
  163. &malformed_list);
  164. tt_assert(p != NULL);
  165. smartlist_add(policy5, p);
  166. p = router_parse_addr_policy_item_from_string("reject *:1-65534", -1,
  167. &malformed_list);
  168. tt_assert(p != NULL);
  169. smartlist_add(policy5, p);
  170. p = router_parse_addr_policy_item_from_string("reject *:65535", -1,
  171. &malformed_list);
  172. tt_assert(p != NULL);
  173. smartlist_add(policy5, p);
  174. p = router_parse_addr_policy_item_from_string("accept *:1-65535", -1,
  175. &malformed_list);
  176. tt_assert(p != NULL);
  177. smartlist_add(policy5, p);
  178. policy6 = smartlist_new();
  179. p = router_parse_addr_policy_item_from_string("accept 43.3.0.0/9:*", -1,
  180. &malformed_list);
  181. tt_assert(p != NULL);
  182. smartlist_add(policy6, p);
  183. policy7 = smartlist_new();
  184. p = router_parse_addr_policy_item_from_string("accept 0.0.0.0/8:*", -1,
  185. &malformed_list);
  186. tt_assert(p != NULL);
  187. smartlist_add(policy7, p);
  188. tt_int_op(0, OP_EQ, policies_parse_exit_policy(NULL, &policy8,
  189. EXIT_POLICY_IPV6_ENABLED |
  190. EXIT_POLICY_REJECT_PRIVATE |
  191. EXIT_POLICY_ADD_DEFAULT,
  192. NULL));
  193. tt_assert(policy8);
  194. tt_int_op(0, OP_EQ, policies_parse_exit_policy(NULL, &policy9,
  195. EXIT_POLICY_REJECT_PRIVATE |
  196. EXIT_POLICY_ADD_DEFAULT,
  197. NULL));
  198. tt_assert(policy9);
  199. /* accept6 * and reject6 * produce IPv6 wildcards only */
  200. policy10 = smartlist_new();
  201. p = router_parse_addr_policy_item_from_string("accept6 *:*", -1,
  202. &malformed_list);
  203. tt_assert(p != NULL);
  204. smartlist_add(policy10, p);
  205. policy11 = smartlist_new();
  206. p = router_parse_addr_policy_item_from_string("reject6 *:*", -1,
  207. &malformed_list);
  208. tt_assert(p != NULL);
  209. smartlist_add(policy11, p);
  210. tt_assert(!exit_policy_is_general_exit(policy));
  211. tt_assert(exit_policy_is_general_exit(policy2));
  212. tt_assert(!exit_policy_is_general_exit(NULL));
  213. tt_assert(!exit_policy_is_general_exit(policy3));
  214. tt_assert(!exit_policy_is_general_exit(policy4));
  215. tt_assert(!exit_policy_is_general_exit(policy5));
  216. tt_assert(!exit_policy_is_general_exit(policy6));
  217. tt_assert(!exit_policy_is_general_exit(policy7));
  218. tt_assert(exit_policy_is_general_exit(policy8));
  219. tt_assert(exit_policy_is_general_exit(policy9));
  220. tt_assert(!exit_policy_is_general_exit(policy10));
  221. tt_assert(!exit_policy_is_general_exit(policy11));
  222. tt_assert(cmp_addr_policies(policy, policy2));
  223. tt_assert(cmp_addr_policies(policy, NULL));
  224. tt_assert(!cmp_addr_policies(policy2, policy2));
  225. tt_assert(!cmp_addr_policies(NULL, NULL));
  226. tt_assert(!policy_is_reject_star(policy2, AF_INET));
  227. tt_assert(policy_is_reject_star(policy, AF_INET));
  228. tt_assert(policy_is_reject_star(policy10, AF_INET));
  229. tt_assert(!policy_is_reject_star(policy10, AF_INET6));
  230. tt_assert(policy_is_reject_star(policy11, AF_INET));
  231. tt_assert(policy_is_reject_star(policy11, AF_INET6));
  232. tt_assert(policy_is_reject_star(NULL, AF_INET));
  233. tt_assert(policy_is_reject_star(NULL, AF_INET6));
  234. addr_policy_list_free(policy);
  235. policy = NULL;
  236. /* make sure assume_action works */
  237. malformed_list = 0;
  238. p = router_parse_addr_policy_item_from_string("127.0.0.1",
  239. ADDR_POLICY_ACCEPT,
  240. &malformed_list);
  241. tt_assert(p);
  242. addr_policy_free(p);
  243. tt_assert(!malformed_list);
  244. p = router_parse_addr_policy_item_from_string("127.0.0.1:*",
  245. ADDR_POLICY_ACCEPT,
  246. &malformed_list);
  247. tt_assert(p);
  248. addr_policy_free(p);
  249. tt_assert(!malformed_list);
  250. p = router_parse_addr_policy_item_from_string("[::]",
  251. ADDR_POLICY_ACCEPT,
  252. &malformed_list);
  253. tt_assert(p);
  254. addr_policy_free(p);
  255. tt_assert(!malformed_list);
  256. p = router_parse_addr_policy_item_from_string("[::]:*",
  257. ADDR_POLICY_ACCEPT,
  258. &malformed_list);
  259. tt_assert(p);
  260. addr_policy_free(p);
  261. tt_assert(!malformed_list);
  262. p = router_parse_addr_policy_item_from_string("[face::b]",
  263. ADDR_POLICY_ACCEPT,
  264. &malformed_list);
  265. tt_assert(p);
  266. addr_policy_free(p);
  267. tt_assert(!malformed_list);
  268. p = router_parse_addr_policy_item_from_string("[b::aaaa]",
  269. ADDR_POLICY_ACCEPT,
  270. &malformed_list);
  271. tt_assert(p);
  272. addr_policy_free(p);
  273. tt_assert(!malformed_list);
  274. p = router_parse_addr_policy_item_from_string("*",
  275. ADDR_POLICY_ACCEPT,
  276. &malformed_list);
  277. tt_assert(p);
  278. addr_policy_free(p);
  279. tt_assert(!malformed_list);
  280. p = router_parse_addr_policy_item_from_string("*4",
  281. ADDR_POLICY_ACCEPT,
  282. &malformed_list);
  283. tt_assert(p);
  284. addr_policy_free(p);
  285. tt_assert(!malformed_list);
  286. p = router_parse_addr_policy_item_from_string("*6",
  287. ADDR_POLICY_ACCEPT,
  288. &malformed_list);
  289. tt_assert(p);
  290. addr_policy_free(p);
  291. tt_assert(!malformed_list);
  292. /* These are all ambiguous IPv6 addresses, it's good that we reject them */
  293. p = router_parse_addr_policy_item_from_string("acce::abcd",
  294. ADDR_POLICY_ACCEPT,
  295. &malformed_list);
  296. tt_assert(!p);
  297. tt_assert(malformed_list);
  298. malformed_list = 0;
  299. p = router_parse_addr_policy_item_from_string("7:1234",
  300. ADDR_POLICY_ACCEPT,
  301. &malformed_list);
  302. tt_assert(!p);
  303. tt_assert(malformed_list);
  304. malformed_list = 0;
  305. p = router_parse_addr_policy_item_from_string("::",
  306. ADDR_POLICY_ACCEPT,
  307. &malformed_list);
  308. tt_assert(!p);
  309. tt_assert(malformed_list);
  310. malformed_list = 0;
  311. /* make sure compacting logic works. */
  312. policy = NULL;
  313. line.key = (char*)"foo";
  314. line.value = (char*)"accept *:80,reject private:*,reject *:*";
  315. line.next = NULL;
  316. tt_int_op(0, OP_EQ, policies_parse_exit_policy(&line,&policy,
  317. EXIT_POLICY_IPV6_ENABLED |
  318. EXIT_POLICY_ADD_DEFAULT, NULL));
  319. tt_assert(policy);
  320. //test_streq(policy->string, "accept *:80");
  321. //test_streq(policy->next->string, "reject *:*");
  322. tt_int_op(smartlist_len(policy),OP_EQ, 4);
  323. /* test policy summaries */
  324. /* check if we properly ignore private IP addresses */
  325. test_policy_summary_helper("reject 192.168.0.0/16:*,"
  326. "reject 0.0.0.0/8:*,"
  327. "reject 10.0.0.0/8:*,"
  328. "accept *:10-30,"
  329. "accept *:90,"
  330. "reject *:*",
  331. "accept 10-30,90");
  332. /* check all accept policies, and proper counting of rejects */
  333. test_policy_summary_helper("reject 11.0.0.0/9:80,"
  334. "reject 12.0.0.0/9:80,"
  335. "reject 13.0.0.0/9:80,"
  336. "reject 14.0.0.0/9:80,"
  337. "accept *:*", "accept 1-65535");
  338. test_policy_summary_helper("reject 11.0.0.0/9:80,"
  339. "reject 12.0.0.0/9:80,"
  340. "reject 13.0.0.0/9:80,"
  341. "reject 14.0.0.0/9:80,"
  342. "reject 15.0.0.0:81,"
  343. "accept *:*", "accept 1-65535");
  344. test_policy_summary_helper("reject 11.0.0.0/9:80,"
  345. "reject 12.0.0.0/9:80,"
  346. "reject 13.0.0.0/9:80,"
  347. "reject 14.0.0.0/9:80,"
  348. "reject 15.0.0.0:80,"
  349. "accept *:*",
  350. "reject 80");
  351. /* no exits */
  352. test_policy_summary_helper("accept 11.0.0.0/9:80,"
  353. "reject *:*",
  354. "reject 1-65535");
  355. /* port merging */
  356. test_policy_summary_helper("accept *:80,"
  357. "accept *:81,"
  358. "accept *:100-110,"
  359. "accept *:111,"
  360. "reject *:*",
  361. "accept 80-81,100-111");
  362. /* border ports */
  363. test_policy_summary_helper("accept *:1,"
  364. "accept *:3,"
  365. "accept *:65535,"
  366. "reject *:*",
  367. "accept 1,3,65535");
  368. /* holes */
  369. test_policy_summary_helper("accept *:1,"
  370. "accept *:3,"
  371. "accept *:5,"
  372. "accept *:7,"
  373. "reject *:*",
  374. "accept 1,3,5,7");
  375. test_policy_summary_helper("reject *:1,"
  376. "reject *:3,"
  377. "reject *:5,"
  378. "reject *:7,"
  379. "accept *:*",
  380. "reject 1,3,5,7");
  381. /* Short policies with unrecognized formats should get accepted. */
  382. test_short_policy_parse("accept fred,2,3-5", "accept 2,3-5");
  383. test_short_policy_parse("accept 2,fred,3", "accept 2,3");
  384. test_short_policy_parse("accept 2,fred,3,bob", "accept 2,3");
  385. test_short_policy_parse("accept 2,-3,500-600", "accept 2,500-600");
  386. /* Short policies with nil entries are accepted too. */
  387. test_short_policy_parse("accept 1,,3", "accept 1,3");
  388. test_short_policy_parse("accept 100-200,,", "accept 100-200");
  389. test_short_policy_parse("reject ,1-10,,,,30-40", "reject 1-10,30-40");
  390. /* Try parsing various broken short policies */
  391. #define TT_BAD_SHORT_POLICY(s) \
  392. do { \
  393. tt_ptr_op(NULL, OP_EQ, (short_parsed = parse_short_policy((s)))); \
  394. } while (0)
  395. TT_BAD_SHORT_POLICY("accept 200-199");
  396. TT_BAD_SHORT_POLICY("");
  397. TT_BAD_SHORT_POLICY("rejekt 1,2,3");
  398. TT_BAD_SHORT_POLICY("reject ");
  399. TT_BAD_SHORT_POLICY("reject");
  400. TT_BAD_SHORT_POLICY("rej");
  401. TT_BAD_SHORT_POLICY("accept 2,3,100000");
  402. TT_BAD_SHORT_POLICY("accept 2,3x,4");
  403. TT_BAD_SHORT_POLICY("accept 2,3x,4");
  404. TT_BAD_SHORT_POLICY("accept 2-");
  405. TT_BAD_SHORT_POLICY("accept 2-x");
  406. TT_BAD_SHORT_POLICY("accept 1-,3");
  407. TT_BAD_SHORT_POLICY("accept 1-,3");
  408. /* Make sure that IPv4 addresses are ignored in accept6/reject6 lines. */
  409. p = router_parse_addr_policy_item_from_string("accept6 1.2.3.4:*", -1,
  410. &malformed_list);
  411. tt_assert(p == NULL);
  412. tt_assert(!malformed_list);
  413. p = router_parse_addr_policy_item_from_string("reject6 2.4.6.0/24:*", -1,
  414. &malformed_list);
  415. tt_assert(p == NULL);
  416. tt_assert(!malformed_list);
  417. p = router_parse_addr_policy_item_from_string("accept6 *4:*", -1,
  418. &malformed_list);
  419. tt_assert(p == NULL);
  420. tt_assert(!malformed_list);
  421. /* Make sure malformed policies are detected as such. */
  422. p = router_parse_addr_policy_item_from_string("bad_token *4:*", -1,
  423. &malformed_list);
  424. tt_assert(p == NULL);
  425. tt_assert(malformed_list);
  426. p = router_parse_addr_policy_item_from_string("accept6 **:*", -1,
  427. &malformed_list);
  428. tt_assert(p == NULL);
  429. tt_assert(malformed_list);
  430. p = router_parse_addr_policy_item_from_string("accept */15:*", -1,
  431. &malformed_list);
  432. tt_assert(p == NULL);
  433. tt_assert(malformed_list);
  434. p = router_parse_addr_policy_item_from_string("reject6 */:*", -1,
  435. &malformed_list);
  436. tt_assert(p == NULL);
  437. tt_assert(malformed_list);
  438. p = router_parse_addr_policy_item_from_string("accept 127.0.0.1/33:*", -1,
  439. &malformed_list);
  440. tt_assert(p == NULL);
  441. tt_assert(malformed_list);
  442. p = router_parse_addr_policy_item_from_string("accept6 [::1]/129:*", -1,
  443. &malformed_list);
  444. tt_assert(p == NULL);
  445. tt_assert(malformed_list);
  446. p = router_parse_addr_policy_item_from_string("reject 8.8.8.8/-1:*", -1,
  447. &malformed_list);
  448. tt_assert(p == NULL);
  449. tt_assert(malformed_list);
  450. p = router_parse_addr_policy_item_from_string("reject 8.8.4.4:10-5", -1,
  451. &malformed_list);
  452. tt_assert(p == NULL);
  453. tt_assert(malformed_list);
  454. p = router_parse_addr_policy_item_from_string("reject 1.2.3.4:-1", -1,
  455. &malformed_list);
  456. tt_assert(p == NULL);
  457. tt_assert(malformed_list);
  458. /* Test a too-long policy. */
  459. {
  460. int i;
  461. char *policy = NULL;
  462. smartlist_t *chunks = smartlist_new();
  463. smartlist_add(chunks, tor_strdup("accept "));
  464. for (i=1; i<10000; ++i)
  465. smartlist_add_asprintf(chunks, "%d,", i);
  466. smartlist_add(chunks, tor_strdup("20000"));
  467. policy = smartlist_join_strings(chunks, "", 0, NULL);
  468. SMARTLIST_FOREACH(chunks, char *, ch, tor_free(ch));
  469. smartlist_free(chunks);
  470. short_parsed = parse_short_policy(policy);/* shouldn't be accepted */
  471. tor_free(policy);
  472. tt_ptr_op(NULL, OP_EQ, short_parsed);
  473. }
  474. /* truncation ports */
  475. sm = smartlist_new();
  476. for (i=1; i<2000; i+=2) {
  477. char buf[POLICY_BUF_LEN];
  478. tor_snprintf(buf, sizeof(buf), "reject *:%d", i);
  479. smartlist_add(sm, tor_strdup(buf));
  480. }
  481. smartlist_add(sm, tor_strdup("accept *:*"));
  482. policy_str = smartlist_join_strings(sm, ",", 0, NULL);
  483. test_policy_summary_helper( policy_str,
  484. "accept 2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,"
  485. "46,48,50,52,54,56,58,60,62,64,66,68,70,72,74,76,78,80,82,84,86,88,90,"
  486. "92,94,96,98,100,102,104,106,108,110,112,114,116,118,120,122,124,126,128,"
  487. "130,132,134,136,138,140,142,144,146,148,150,152,154,156,158,160,162,164,"
  488. "166,168,170,172,174,176,178,180,182,184,186,188,190,192,194,196,198,200,"
  489. "202,204,206,208,210,212,214,216,218,220,222,224,226,228,230,232,234,236,"
  490. "238,240,242,244,246,248,250,252,254,256,258,260,262,264,266,268,270,272,"
  491. "274,276,278,280,282,284,286,288,290,292,294,296,298,300,302,304,306,308,"
  492. "310,312,314,316,318,320,322,324,326,328,330,332,334,336,338,340,342,344,"
  493. "346,348,350,352,354,356,358,360,362,364,366,368,370,372,374,376,378,380,"
  494. "382,384,386,388,390,392,394,396,398,400,402,404,406,408,410,412,414,416,"
  495. "418,420,422,424,426,428,430,432,434,436,438,440,442,444,446,448,450,452,"
  496. "454,456,458,460,462,464,466,468,470,472,474,476,478,480,482,484,486,488,"
  497. "490,492,494,496,498,500,502,504,506,508,510,512,514,516,518,520,522");
  498. done:
  499. addr_policy_list_free(policy);
  500. addr_policy_list_free(policy2);
  501. addr_policy_list_free(policy3);
  502. addr_policy_list_free(policy4);
  503. addr_policy_list_free(policy5);
  504. addr_policy_list_free(policy6);
  505. addr_policy_list_free(policy7);
  506. addr_policy_list_free(policy8);
  507. addr_policy_list_free(policy9);
  508. addr_policy_list_free(policy10);
  509. addr_policy_list_free(policy11);
  510. addr_policy_list_free(policy12);
  511. tor_free(policy_str);
  512. if (sm) {
  513. SMARTLIST_FOREACH(sm, char *, s, tor_free(s));
  514. smartlist_free(sm);
  515. }
  516. short_policy_free(short_parsed);
  517. }
  518. /** Helper: Check that policy_list contains address */
  519. static int
  520. test_policy_has_address_helper(const smartlist_t *policy_list,
  521. const tor_addr_t *addr)
  522. {
  523. int found = 0;
  524. tt_assert(policy_list);
  525. tt_assert(addr);
  526. SMARTLIST_FOREACH_BEGIN(policy_list, addr_policy_t*, p) {
  527. if (tor_addr_eq(&p->addr, addr)) {
  528. found = 1;
  529. }
  530. } SMARTLIST_FOREACH_END(p);
  531. return found;
  532. done:
  533. return 0;
  534. }
  535. #define TEST_IPV4_ADDR (0x01020304)
  536. #define TEST_IPV6_ADDR ("2002::abcd")
  537. /** Run unit tests for rejecting the configured addresses on this exit relay
  538. * using policies_parse_exit_policy_reject_private */
  539. static void
  540. test_policies_reject_exit_address(void *arg)
  541. {
  542. smartlist_t *policy = NULL;
  543. tor_addr_t ipv4_addr, ipv6_addr;
  544. smartlist_t *ipv4_list, *ipv6_list, *both_list, *dupl_list;
  545. (void)arg;
  546. tor_addr_from_ipv4h(&ipv4_addr, TEST_IPV4_ADDR);
  547. tor_addr_parse(&ipv6_addr, TEST_IPV6_ADDR);
  548. ipv4_list = smartlist_new();
  549. ipv6_list = smartlist_new();
  550. both_list = smartlist_new();
  551. dupl_list = smartlist_new();
  552. smartlist_add(ipv4_list, &ipv4_addr);
  553. smartlist_add(both_list, &ipv4_addr);
  554. smartlist_add(dupl_list, &ipv4_addr);
  555. smartlist_add(dupl_list, &ipv4_addr);
  556. smartlist_add(dupl_list, &ipv4_addr);
  557. smartlist_add(ipv6_list, &ipv6_addr);
  558. smartlist_add(both_list, &ipv6_addr);
  559. smartlist_add(dupl_list, &ipv6_addr);
  560. smartlist_add(dupl_list, &ipv6_addr);
  561. /* IPv4-Only Exits */
  562. /* test that IPv4 addresses are rejected on an IPv4-only exit */
  563. policies_parse_exit_policy_reject_private(&policy, 0, ipv4_list, 0, 0);
  564. tt_assert(policy);
  565. tt_assert(smartlist_len(policy) == 1);
  566. tt_assert(test_policy_has_address_helper(policy, &ipv4_addr));
  567. addr_policy_list_free(policy);
  568. policy = NULL;
  569. /* test that IPv6 addresses are NOT rejected on an IPv4-only exit
  570. * (all IPv6 addresses are rejected by policies_parse_exit_policy_internal
  571. * on IPv4-only exits, so policies_parse_exit_policy_reject_private doesn't
  572. * need to do anything) */
  573. policies_parse_exit_policy_reject_private(&policy, 0, ipv6_list, 0, 0);
  574. tt_assert(policy == NULL);
  575. /* test that only IPv4 addresses are rejected on an IPv4-only exit */
  576. policies_parse_exit_policy_reject_private(&policy, 0, both_list, 0, 0);
  577. tt_assert(policy);
  578. tt_assert(smartlist_len(policy) == 1);
  579. tt_assert(test_policy_has_address_helper(policy, &ipv4_addr));
  580. addr_policy_list_free(policy);
  581. policy = NULL;
  582. /* Test that lists with duplicate entries produce the same results */
  583. policies_parse_exit_policy_reject_private(&policy, 0, dupl_list, 0, 0);
  584. tt_assert(policy);
  585. tt_assert(smartlist_len(policy) == 1);
  586. tt_assert(test_policy_has_address_helper(policy, &ipv4_addr));
  587. addr_policy_list_free(policy);
  588. policy = NULL;
  589. /* IPv4/IPv6 Exits */
  590. /* test that IPv4 addresses are rejected on an IPv4/IPv6 exit */
  591. policies_parse_exit_policy_reject_private(&policy, 1, ipv4_list, 0, 0);
  592. tt_assert(policy);
  593. tt_assert(smartlist_len(policy) == 1);
  594. tt_assert(test_policy_has_address_helper(policy, &ipv4_addr));
  595. addr_policy_list_free(policy);
  596. policy = NULL;
  597. /* test that IPv6 addresses are rejected on an IPv4/IPv6 exit */
  598. policies_parse_exit_policy_reject_private(&policy, 1, ipv6_list, 0, 0);
  599. tt_assert(policy);
  600. tt_assert(smartlist_len(policy) == 1);
  601. tt_assert(test_policy_has_address_helper(policy, &ipv6_addr));
  602. addr_policy_list_free(policy);
  603. policy = NULL;
  604. /* test that IPv4 and IPv6 addresses are rejected on an IPv4/IPv6 exit */
  605. policies_parse_exit_policy_reject_private(&policy, 1, both_list, 0, 0);
  606. tt_assert(policy);
  607. tt_assert(smartlist_len(policy) == 2);
  608. tt_assert(test_policy_has_address_helper(policy, &ipv4_addr));
  609. tt_assert(test_policy_has_address_helper(policy, &ipv6_addr));
  610. addr_policy_list_free(policy);
  611. policy = NULL;
  612. /* Test that lists with duplicate entries produce the same results */
  613. policies_parse_exit_policy_reject_private(&policy, 1, dupl_list, 0, 0);
  614. tt_assert(policy);
  615. tt_assert(smartlist_len(policy) == 2);
  616. tt_assert(test_policy_has_address_helper(policy, &ipv4_addr));
  617. tt_assert(test_policy_has_address_helper(policy, &ipv6_addr));
  618. addr_policy_list_free(policy);
  619. policy = NULL;
  620. done:
  621. addr_policy_list_free(policy);
  622. smartlist_free(ipv4_list);
  623. smartlist_free(ipv6_list);
  624. smartlist_free(both_list);
  625. smartlist_free(dupl_list);
  626. }
  627. static smartlist_t *test_configured_ports = NULL;
  628. /** Returns test_configured_ports */
  629. static const smartlist_t *
  630. mock_get_configured_ports(void)
  631. {
  632. return test_configured_ports;
  633. }
  634. /** Run unit tests for rejecting publicly routable configured port addresses
  635. * on this exit relay using policies_parse_exit_policy_reject_private */
  636. static void
  637. test_policies_reject_port_address(void *arg)
  638. {
  639. smartlist_t *policy = NULL;
  640. port_cfg_t *ipv4_port = NULL;
  641. port_cfg_t *ipv6_port = NULL;
  642. (void)arg;
  643. test_configured_ports = smartlist_new();
  644. ipv4_port = port_cfg_new(0);
  645. tor_addr_from_ipv4h(&ipv4_port->addr, TEST_IPV4_ADDR);
  646. smartlist_add(test_configured_ports, ipv4_port);
  647. ipv6_port = port_cfg_new(0);
  648. tor_addr_parse(&ipv6_port->addr, TEST_IPV6_ADDR);
  649. smartlist_add(test_configured_ports, ipv6_port);
  650. MOCK(get_configured_ports, mock_get_configured_ports);
  651. /* test that an IPv4 port is rejected on an IPv4-only exit, but an IPv6 port
  652. * is NOT rejected (all IPv6 addresses are rejected by
  653. * policies_parse_exit_policy_internal on IPv4-only exits, so
  654. * policies_parse_exit_policy_reject_private doesn't need to do anything
  655. * with IPv6 addresses on IPv4-only exits) */
  656. policies_parse_exit_policy_reject_private(&policy, 0, NULL, 0, 1);
  657. tt_assert(policy);
  658. tt_assert(smartlist_len(policy) == 1);
  659. tt_assert(test_policy_has_address_helper(policy, &ipv4_port->addr));
  660. addr_policy_list_free(policy);
  661. policy = NULL;
  662. /* test that IPv4 and IPv6 ports are rejected on an IPv4/IPv6 exit */
  663. policies_parse_exit_policy_reject_private(&policy, 1, NULL, 0, 1);
  664. tt_assert(policy);
  665. tt_assert(smartlist_len(policy) == 2);
  666. tt_assert(test_policy_has_address_helper(policy, &ipv4_port->addr));
  667. tt_assert(test_policy_has_address_helper(policy, &ipv6_port->addr));
  668. addr_policy_list_free(policy);
  669. policy = NULL;
  670. done:
  671. addr_policy_list_free(policy);
  672. if (test_configured_ports) {
  673. SMARTLIST_FOREACH(test_configured_ports,
  674. port_cfg_t *, p, port_cfg_free(p));
  675. smartlist_free(test_configured_ports);
  676. test_configured_ports = NULL;
  677. }
  678. UNMOCK(get_configured_ports);
  679. }
  680. static smartlist_t *mock_ipv4_addrs = NULL;
  681. static smartlist_t *mock_ipv6_addrs = NULL;
  682. /* mock get_interface_address6_list, returning a deep copy of the template
  683. * address list ipv4_interface_address_list or ipv6_interface_address_list */
  684. static smartlist_t *
  685. mock_get_interface_address6_list(int severity,
  686. sa_family_t family,
  687. int include_internal)
  688. {
  689. (void)severity;
  690. (void)include_internal;
  691. smartlist_t *clone_list = smartlist_new();
  692. smartlist_t *template_list = NULL;
  693. if (family == AF_INET) {
  694. template_list = mock_ipv4_addrs;
  695. } else if (family == AF_INET6) {
  696. template_list = mock_ipv6_addrs;
  697. } else {
  698. return NULL;
  699. }
  700. tt_assert(template_list);
  701. SMARTLIST_FOREACH_BEGIN(template_list, tor_addr_t *, src_addr) {
  702. tor_addr_t *dest_addr = tor_malloc(sizeof(tor_addr_t));
  703. memset(dest_addr, 0, sizeof(*dest_addr));
  704. tor_addr_copy_tight(dest_addr, src_addr);
  705. smartlist_add(clone_list, dest_addr);
  706. } SMARTLIST_FOREACH_END(src_addr);
  707. return clone_list;
  708. done:
  709. free_interface_address6_list(clone_list);
  710. return NULL;
  711. }
  712. /** Run unit tests for rejecting publicly routable interface addresses on this
  713. * exit relay using policies_parse_exit_policy_reject_private */
  714. static void
  715. test_policies_reject_interface_address(void *arg)
  716. {
  717. smartlist_t *policy = NULL;
  718. smartlist_t *public_ipv4_addrs =
  719. get_interface_address6_list(LOG_INFO, AF_INET, 0);
  720. smartlist_t *public_ipv6_addrs =
  721. get_interface_address6_list(LOG_INFO, AF_INET6, 0);
  722. tor_addr_t ipv4_addr, ipv6_addr;
  723. (void)arg;
  724. /* test that no addresses are rejected when none are supplied/requested */
  725. policies_parse_exit_policy_reject_private(&policy, 0, NULL, 0, 0);
  726. tt_assert(policy == NULL);
  727. /* test that only IPv4 interface addresses are rejected on an IPv4-only exit
  728. * (and allow for duplicates)
  729. */
  730. policies_parse_exit_policy_reject_private(&policy, 0, NULL, 1, 0);
  731. if (policy) {
  732. tt_assert(smartlist_len(policy) <= smartlist_len(public_ipv4_addrs));
  733. addr_policy_list_free(policy);
  734. policy = NULL;
  735. }
  736. /* test that IPv4 and IPv6 interface addresses are rejected on an IPv4/IPv6
  737. * exit (and allow for duplicates) */
  738. policies_parse_exit_policy_reject_private(&policy, 1, NULL, 1, 0);
  739. if (policy) {
  740. tt_assert(smartlist_len(policy) <= (smartlist_len(public_ipv4_addrs)
  741. + smartlist_len(public_ipv6_addrs)));
  742. addr_policy_list_free(policy);
  743. policy = NULL;
  744. }
  745. /* Now do it all again, but mocked */
  746. tor_addr_from_ipv4h(&ipv4_addr, TEST_IPV4_ADDR);
  747. mock_ipv4_addrs = smartlist_new();
  748. smartlist_add(mock_ipv4_addrs, (void *)&ipv4_addr);
  749. tor_addr_parse(&ipv6_addr, TEST_IPV6_ADDR);
  750. mock_ipv6_addrs = smartlist_new();
  751. smartlist_add(mock_ipv6_addrs, (void *)&ipv6_addr);
  752. MOCK(get_interface_address6_list, mock_get_interface_address6_list);
  753. /* test that no addresses are rejected when none are supplied/requested */
  754. policies_parse_exit_policy_reject_private(&policy, 0, NULL, 0, 0);
  755. tt_assert(policy == NULL);
  756. /* test that only IPv4 interface addresses are rejected on an IPv4-only exit
  757. */
  758. policies_parse_exit_policy_reject_private(&policy, 0, NULL, 1, 0);
  759. tt_assert(policy);
  760. tt_assert(smartlist_len(policy) == smartlist_len(mock_ipv4_addrs));
  761. addr_policy_list_free(policy);
  762. policy = NULL;
  763. /* test that IPv4 and IPv6 interface addresses are rejected on an IPv4/IPv6
  764. * exit */
  765. policies_parse_exit_policy_reject_private(&policy, 1, NULL, 1, 0);
  766. tt_assert(policy);
  767. tt_assert(smartlist_len(policy) == (smartlist_len(mock_ipv4_addrs)
  768. + smartlist_len(mock_ipv6_addrs)));
  769. addr_policy_list_free(policy);
  770. policy = NULL;
  771. done:
  772. addr_policy_list_free(policy);
  773. free_interface_address6_list(public_ipv4_addrs);
  774. free_interface_address6_list(public_ipv6_addrs);
  775. UNMOCK(get_interface_address6_list);
  776. /* we don't use free_interface_address6_list on these lists because their
  777. * address pointers are stack-based */
  778. smartlist_free(mock_ipv4_addrs);
  779. smartlist_free(mock_ipv6_addrs);
  780. }
  781. #undef TEST_IPV4_ADDR
  782. #undef TEST_IPV6_ADDR
  783. static void
  784. test_dump_exit_policy_to_string(void *arg)
  785. {
  786. char *ep;
  787. addr_policy_t *policy_entry;
  788. int malformed_list = -1;
  789. routerinfo_t *ri = tor_malloc_zero(sizeof(routerinfo_t));
  790. (void)arg;
  791. ri->policy_is_reject_star = 1;
  792. ri->exit_policy = NULL; // expecting "reject *:*"
  793. ep = router_dump_exit_policy_to_string(ri,1,1);
  794. tt_str_op("reject *:*",OP_EQ, ep);
  795. tor_free(ep);
  796. ri->exit_policy = smartlist_new();
  797. ri->policy_is_reject_star = 0;
  798. policy_entry = router_parse_addr_policy_item_from_string("accept *:*", -1,
  799. &malformed_list);
  800. smartlist_add(ri->exit_policy,policy_entry);
  801. ep = router_dump_exit_policy_to_string(ri,1,1);
  802. tt_str_op("accept *:*",OP_EQ, ep);
  803. tor_free(ep);
  804. policy_entry = router_parse_addr_policy_item_from_string("reject *:25", -1,
  805. &malformed_list);
  806. smartlist_add(ri->exit_policy,policy_entry);
  807. ep = router_dump_exit_policy_to_string(ri,1,1);
  808. tt_str_op("accept *:*\nreject *:25",OP_EQ, ep);
  809. tor_free(ep);
  810. policy_entry =
  811. router_parse_addr_policy_item_from_string("reject 8.8.8.8:*", -1,
  812. &malformed_list);
  813. smartlist_add(ri->exit_policy,policy_entry);
  814. ep = router_dump_exit_policy_to_string(ri,1,1);
  815. tt_str_op("accept *:*\nreject *:25\nreject 8.8.8.8:*",OP_EQ, ep);
  816. tor_free(ep);
  817. policy_entry =
  818. router_parse_addr_policy_item_from_string("reject6 [FC00::]/7:*", -1,
  819. &malformed_list);
  820. smartlist_add(ri->exit_policy,policy_entry);
  821. ep = router_dump_exit_policy_to_string(ri,1,1);
  822. tt_str_op("accept *:*\nreject *:25\nreject 8.8.8.8:*\n"
  823. "reject6 [fc00::]/7:*",OP_EQ, ep);
  824. tor_free(ep);
  825. policy_entry =
  826. router_parse_addr_policy_item_from_string("accept6 [c000::]/3:*", -1,
  827. &malformed_list);
  828. smartlist_add(ri->exit_policy,policy_entry);
  829. ep = router_dump_exit_policy_to_string(ri,1,1);
  830. tt_str_op("accept *:*\nreject *:25\nreject 8.8.8.8:*\n"
  831. "reject6 [fc00::]/7:*\naccept6 [c000::]/3:*",OP_EQ, ep);
  832. done:
  833. if (ri->exit_policy) {
  834. SMARTLIST_FOREACH(ri->exit_policy, addr_policy_t *,
  835. entry, addr_policy_free(entry));
  836. smartlist_free(ri->exit_policy);
  837. }
  838. tor_free(ri);
  839. tor_free(ep);
  840. }
  841. static routerinfo_t *mock_desc_routerinfo = NULL;
  842. static const routerinfo_t *
  843. mock_router_get_my_routerinfo(void)
  844. {
  845. return mock_desc_routerinfo;
  846. }
  847. #define DEFAULT_POLICY_STRING "reject *:*"
  848. #define TEST_IPV4_ADDR (0x02040608)
  849. #define TEST_IPV6_ADDR ("2003::ef01")
  850. static or_options_t mock_options;
  851. static const or_options_t *
  852. mock_get_options(void)
  853. {
  854. return &mock_options;
  855. }
  856. /** Run unit tests for generating summary lines of exit policies */
  857. static void
  858. test_policies_getinfo_helper_policies(void *arg)
  859. {
  860. (void)arg;
  861. int rv = 0;
  862. size_t ipv4_len = 0, ipv6_len = 0;
  863. char *answer = NULL;
  864. const char *errmsg = NULL;
  865. routerinfo_t mock_my_routerinfo;
  866. memset(&mock_my_routerinfo, 0, sizeof(mock_my_routerinfo));
  867. rv = getinfo_helper_policies(NULL, "exit-policy/default", &answer, &errmsg);
  868. tt_assert(rv == 0);
  869. tt_assert(answer != NULL);
  870. tt_assert(strlen(answer) > 0);
  871. tor_free(answer);
  872. rv = getinfo_helper_policies(NULL, "exit-policy/reject-private/default",
  873. &answer, &errmsg);
  874. tt_assert(rv == 0);
  875. tt_assert(answer != NULL);
  876. tt_assert(strlen(answer) > 0);
  877. tor_free(answer);
  878. memset(&mock_my_routerinfo, 0, sizeof(routerinfo_t));
  879. MOCK(router_get_my_routerinfo, mock_router_get_my_routerinfo);
  880. mock_my_routerinfo.exit_policy = smartlist_new();
  881. mock_desc_routerinfo = &mock_my_routerinfo;
  882. memset(&mock_options, 0, sizeof(or_options_t));
  883. MOCK(get_options, mock_get_options);
  884. rv = getinfo_helper_policies(NULL, "exit-policy/reject-private/relay",
  885. &answer, &errmsg);
  886. tt_assert(rv == 0);
  887. tt_assert(answer != NULL);
  888. tt_assert(strlen(answer) == 0);
  889. tor_free(answer);
  890. rv = getinfo_helper_policies(NULL, "exit-policy/ipv4", &answer,
  891. &errmsg);
  892. tt_assert(rv == 0);
  893. tt_assert(answer != NULL);
  894. ipv4_len = strlen(answer);
  895. tt_assert(ipv4_len == 0 || ipv4_len == strlen(DEFAULT_POLICY_STRING));
  896. tt_assert(ipv4_len == 0 || !strcasecmp(answer, DEFAULT_POLICY_STRING));
  897. tor_free(answer);
  898. rv = getinfo_helper_policies(NULL, "exit-policy/ipv6", &answer,
  899. &errmsg);
  900. tt_assert(rv == 0);
  901. tt_assert(answer != NULL);
  902. ipv6_len = strlen(answer);
  903. tt_assert(ipv6_len == 0 || ipv6_len == strlen(DEFAULT_POLICY_STRING));
  904. tt_assert(ipv6_len == 0 || !strcasecmp(answer, DEFAULT_POLICY_STRING));
  905. tor_free(answer);
  906. rv = getinfo_helper_policies(NULL, "exit-policy/full", &answer,
  907. &errmsg);
  908. tt_assert(rv == 0);
  909. tt_assert(answer != NULL);
  910. /* It's either empty or it's the default */
  911. tt_assert(strlen(answer) == 0 || !strcasecmp(answer, DEFAULT_POLICY_STRING));
  912. tor_free(answer);
  913. mock_my_routerinfo.addr = TEST_IPV4_ADDR;
  914. tor_addr_parse(&mock_my_routerinfo.ipv6_addr, TEST_IPV6_ADDR);
  915. append_exit_policy_string(&mock_my_routerinfo.exit_policy, "accept *4:*");
  916. append_exit_policy_string(&mock_my_routerinfo.exit_policy, "reject *6:*");
  917. mock_options.IPv6Exit = 1;
  918. mock_options.ExitPolicyRejectPrivate = 1;
  919. tor_addr_from_ipv4h(&mock_options.OutboundBindAddressIPv4_, TEST_IPV4_ADDR);
  920. tor_addr_parse(&mock_options.OutboundBindAddressIPv6_, TEST_IPV6_ADDR);
  921. rv = getinfo_helper_policies(NULL, "exit-policy/reject-private/relay",
  922. &answer, &errmsg);
  923. tt_assert(rv == 0);
  924. tt_assert(answer != NULL);
  925. tt_assert(strlen(answer) > 0);
  926. tor_free(answer);
  927. rv = getinfo_helper_policies(NULL, "exit-policy/ipv4", &answer,
  928. &errmsg);
  929. tt_assert(rv == 0);
  930. tt_assert(answer != NULL);
  931. ipv4_len = strlen(answer);
  932. tt_assert(ipv4_len > 0);
  933. tor_free(answer);
  934. rv = getinfo_helper_policies(NULL, "exit-policy/ipv6", &answer,
  935. &errmsg);
  936. tt_assert(rv == 0);
  937. tt_assert(answer != NULL);
  938. ipv6_len = strlen(answer);
  939. tt_assert(ipv6_len > 0);
  940. tor_free(answer);
  941. rv = getinfo_helper_policies(NULL, "exit-policy/full", &answer,
  942. &errmsg);
  943. tt_assert(rv == 0);
  944. tt_assert(answer != NULL);
  945. tt_assert(strlen(answer) > 0);
  946. tt_assert(strlen(answer) == ipv4_len + ipv6_len + 1);
  947. tor_free(answer);
  948. done:
  949. tor_free(answer);
  950. UNMOCK(get_options);
  951. UNMOCK(router_get_my_routerinfo);
  952. addr_policy_list_free(mock_my_routerinfo.exit_policy);
  953. }
  954. #undef DEFAULT_POLICY_STRING
  955. #undef TEST_IPV4_ADDR
  956. #undef TEST_IPV6_ADDR
  957. #define TEST_IPV4_ADDR_STR "1.2.3.4"
  958. #define TEST_IPV6_ADDR_STR "[1002::4567]"
  959. #define REJECT_IPv4_FINAL_STR "reject 0.0.0.0/0:*"
  960. #define REJECT_IPv6_FINAL_STR "reject [::]/0:*"
  961. #define OTHER_IPV4_ADDR_STR "6.7.8.9"
  962. #define OTHER_IPV6_ADDR_STR "[afff::]"
  963. /** Run unit tests for fascist_firewall_allows_address */
  964. static void
  965. test_policies_fascist_firewall_allows_address(void *arg)
  966. {
  967. (void)arg;
  968. tor_addr_t ipv4_addr, ipv6_addr, r_ipv4_addr, r_ipv6_addr;
  969. tor_addr_t n_ipv4_addr, n_ipv6_addr;
  970. const uint16_t port = 1234;
  971. smartlist_t *policy = NULL;
  972. smartlist_t *e_policy = NULL;
  973. addr_policy_t *item = NULL;
  974. int malformed_list = 0;
  975. /* Setup the options and the items in the policies */
  976. memset(&mock_options, 0, sizeof(or_options_t));
  977. MOCK(get_options, mock_get_options);
  978. policy = smartlist_new();
  979. item = router_parse_addr_policy_item_from_string("accept "
  980. TEST_IPV4_ADDR_STR ":*",
  981. ADDR_POLICY_ACCEPT,
  982. &malformed_list);
  983. tt_assert(item);
  984. tt_assert(!malformed_list);
  985. smartlist_add(policy, item);
  986. item = router_parse_addr_policy_item_from_string("accept "
  987. TEST_IPV6_ADDR_STR,
  988. ADDR_POLICY_ACCEPT,
  989. &malformed_list);
  990. tt_assert(item);
  991. tt_assert(!malformed_list);
  992. smartlist_add(policy, item);
  993. /* Normally, policy_expand_unspec would do this for us */
  994. item = router_parse_addr_policy_item_from_string(REJECT_IPv4_FINAL_STR,
  995. ADDR_POLICY_ACCEPT,
  996. &malformed_list);
  997. tt_assert(item);
  998. tt_assert(!malformed_list);
  999. smartlist_add(policy, item);
  1000. item = router_parse_addr_policy_item_from_string(REJECT_IPv6_FINAL_STR,
  1001. ADDR_POLICY_ACCEPT,
  1002. &malformed_list);
  1003. tt_assert(item);
  1004. tt_assert(!malformed_list);
  1005. smartlist_add(policy, item);
  1006. item = NULL;
  1007. e_policy = smartlist_new();
  1008. /*
  1009. char *polstr = policy_dump_to_string(policy, 1, 1);
  1010. printf("%s\n", polstr);
  1011. tor_free(polstr);
  1012. */
  1013. /* Parse the addresses */
  1014. tor_addr_parse(&ipv4_addr, TEST_IPV4_ADDR_STR);
  1015. tor_addr_parse(&ipv6_addr, TEST_IPV6_ADDR_STR);
  1016. tor_addr_parse(&r_ipv4_addr, OTHER_IPV4_ADDR_STR);
  1017. tor_addr_parse(&r_ipv6_addr, OTHER_IPV6_ADDR_STR);
  1018. tor_addr_make_null(&n_ipv4_addr, AF_INET);
  1019. tor_addr_make_null(&n_ipv6_addr, AF_INET6);
  1020. /* Test the function's address matching with IPv4 and IPv6 on */
  1021. memset(&mock_options, 0, sizeof(or_options_t));
  1022. mock_options.ClientUseIPv4 = 1;
  1023. mock_options.ClientUseIPv6 = 1;
  1024. mock_options.UseBridges = 0;
  1025. tt_assert(fascist_firewall_allows_address(&ipv4_addr, port, policy, 0, 0)
  1026. == 1);
  1027. tt_assert(fascist_firewall_allows_address(&ipv6_addr, port, policy, 0, 0)
  1028. == 1);
  1029. tt_assert(fascist_firewall_allows_address(&r_ipv4_addr, port, policy, 0, 0)
  1030. == 0);
  1031. tt_assert(fascist_firewall_allows_address(&r_ipv6_addr, port, policy, 0, 0)
  1032. == 0);
  1033. /* Preferring IPv4 */
  1034. tt_assert(fascist_firewall_allows_address(&ipv4_addr, port, policy, 1, 0)
  1035. == 1);
  1036. tt_assert(fascist_firewall_allows_address(&ipv6_addr, port, policy, 1, 0)
  1037. == 0);
  1038. tt_assert(fascist_firewall_allows_address(&r_ipv4_addr, port, policy, 1, 0)
  1039. == 0);
  1040. tt_assert(fascist_firewall_allows_address(&r_ipv6_addr, port, policy, 1, 0)
  1041. == 0);
  1042. /* Preferring IPv6 */
  1043. tt_assert(fascist_firewall_allows_address(&ipv4_addr, port, policy, 1, 1)
  1044. == 0);
  1045. tt_assert(fascist_firewall_allows_address(&ipv6_addr, port, policy, 1, 1)
  1046. == 1);
  1047. tt_assert(fascist_firewall_allows_address(&r_ipv4_addr, port, policy, 1, 1)
  1048. == 0);
  1049. tt_assert(fascist_firewall_allows_address(&r_ipv6_addr, port, policy, 1, 1)
  1050. == 0);
  1051. /* Test the function's address matching with UseBridges on */
  1052. memset(&mock_options, 0, sizeof(or_options_t));
  1053. mock_options.ClientUseIPv4 = 1;
  1054. mock_options.ClientUseIPv6 = 1;
  1055. mock_options.UseBridges = 1;
  1056. tt_assert(fascist_firewall_allows_address(&ipv4_addr, port, policy, 0, 0)
  1057. == 1);
  1058. tt_assert(fascist_firewall_allows_address(&ipv6_addr, port, policy, 0, 0)
  1059. == 1);
  1060. tt_assert(fascist_firewall_allows_address(&r_ipv4_addr, port, policy, 0, 0)
  1061. == 0);
  1062. tt_assert(fascist_firewall_allows_address(&r_ipv6_addr, port, policy, 0, 0)
  1063. == 0);
  1064. /* Preferring IPv4 */
  1065. tt_assert(fascist_firewall_allows_address(&ipv4_addr, port, policy, 1, 0)
  1066. == 1);
  1067. tt_assert(fascist_firewall_allows_address(&ipv6_addr, port, policy, 1, 0)
  1068. == 0);
  1069. tt_assert(fascist_firewall_allows_address(&r_ipv4_addr, port, policy, 1, 0)
  1070. == 0);
  1071. tt_assert(fascist_firewall_allows_address(&r_ipv6_addr, port, policy, 1, 0)
  1072. == 0);
  1073. /* Preferring IPv6 */
  1074. tt_assert(fascist_firewall_allows_address(&ipv4_addr, port, policy, 1, 1)
  1075. == 0);
  1076. tt_assert(fascist_firewall_allows_address(&ipv6_addr, port, policy, 1, 1)
  1077. == 1);
  1078. tt_assert(fascist_firewall_allows_address(&r_ipv4_addr, port, policy, 1, 1)
  1079. == 0);
  1080. tt_assert(fascist_firewall_allows_address(&r_ipv6_addr, port, policy, 1, 1)
  1081. == 0);
  1082. /* bridge clients always use IPv6, regardless of ClientUseIPv6 */
  1083. mock_options.ClientUseIPv4 = 1;
  1084. mock_options.ClientUseIPv6 = 0;
  1085. tt_assert(fascist_firewall_allows_address(&ipv4_addr, port, policy, 0, 0)
  1086. == 1);
  1087. tt_assert(fascist_firewall_allows_address(&ipv6_addr, port, policy, 0, 0)
  1088. == 1);
  1089. tt_assert(fascist_firewall_allows_address(&r_ipv4_addr, port, policy, 0, 0)
  1090. == 0);
  1091. tt_assert(fascist_firewall_allows_address(&r_ipv6_addr, port, policy, 0, 0)
  1092. == 0);
  1093. /* Test the function's address matching with IPv4 on */
  1094. memset(&mock_options, 0, sizeof(or_options_t));
  1095. mock_options.ClientUseIPv4 = 1;
  1096. mock_options.ClientUseIPv6 = 0;
  1097. mock_options.UseBridges = 0;
  1098. tt_assert(fascist_firewall_allows_address(&ipv4_addr, port, policy, 0, 0)
  1099. == 1);
  1100. tt_assert(fascist_firewall_allows_address(&ipv6_addr, port, policy, 0, 0)
  1101. == 0);
  1102. tt_assert(fascist_firewall_allows_address(&r_ipv4_addr, port, policy, 0, 0)
  1103. == 0);
  1104. tt_assert(fascist_firewall_allows_address(&r_ipv6_addr, port, policy, 0, 0)
  1105. == 0);
  1106. /* Test the function's address matching with IPv6 on */
  1107. memset(&mock_options, 0, sizeof(or_options_t));
  1108. mock_options.ClientUseIPv4 = 0;
  1109. mock_options.ClientUseIPv6 = 1;
  1110. mock_options.UseBridges = 0;
  1111. tt_assert(fascist_firewall_allows_address(&ipv4_addr, port, policy, 0, 0)
  1112. == 0);
  1113. tt_assert(fascist_firewall_allows_address(&ipv6_addr, port, policy, 0, 0)
  1114. == 1);
  1115. tt_assert(fascist_firewall_allows_address(&r_ipv4_addr, port, policy, 0, 0)
  1116. == 0);
  1117. tt_assert(fascist_firewall_allows_address(&r_ipv6_addr, port, policy, 0, 0)
  1118. == 0);
  1119. /* Test the function's address matching with ClientUseIPv4 0.
  1120. * This means "use IPv6" regardless of the other settings. */
  1121. memset(&mock_options, 0, sizeof(or_options_t));
  1122. mock_options.ClientUseIPv4 = 0;
  1123. mock_options.ClientUseIPv6 = 0;
  1124. mock_options.UseBridges = 0;
  1125. tt_assert(fascist_firewall_allows_address(&ipv4_addr, port, policy, 0, 0)
  1126. == 0);
  1127. tt_assert(fascist_firewall_allows_address(&ipv6_addr, port, policy, 0, 0)
  1128. == 1);
  1129. tt_assert(fascist_firewall_allows_address(&r_ipv4_addr, port, policy, 0, 0)
  1130. == 0);
  1131. tt_assert(fascist_firewall_allows_address(&r_ipv6_addr, port, policy, 0, 0)
  1132. == 0);
  1133. /* Test the function's address matching for unusual inputs */
  1134. memset(&mock_options, 0, sizeof(or_options_t));
  1135. mock_options.ClientUseIPv4 = 1;
  1136. mock_options.ClientUseIPv6 = 1;
  1137. mock_options.UseBridges = 1;
  1138. /* NULL and tor_addr_is_null addresses are rejected */
  1139. tt_assert(fascist_firewall_allows_address(NULL, port, policy, 0, 0) == 0);
  1140. tt_assert(fascist_firewall_allows_address(&n_ipv4_addr, port, policy, 0, 0)
  1141. == 0);
  1142. tt_assert(fascist_firewall_allows_address(&n_ipv6_addr, port, policy, 0, 0)
  1143. == 0);
  1144. /* zero ports are rejected */
  1145. tt_assert(fascist_firewall_allows_address(&ipv4_addr, 0, policy, 0, 0)
  1146. == 0);
  1147. tt_assert(fascist_firewall_allows_address(&ipv6_addr, 0, policy, 0, 0)
  1148. == 0);
  1149. /* NULL and empty policies accept everything */
  1150. tt_assert(fascist_firewall_allows_address(&ipv4_addr, port, NULL, 0, 0)
  1151. == 1);
  1152. tt_assert(fascist_firewall_allows_address(&ipv6_addr, port, NULL, 0, 0)
  1153. == 1);
  1154. tt_assert(fascist_firewall_allows_address(&ipv4_addr, port, e_policy, 0, 0)
  1155. == 1);
  1156. tt_assert(fascist_firewall_allows_address(&ipv6_addr, port, e_policy, 0, 0)
  1157. == 1);
  1158. done:
  1159. addr_policy_free(item);
  1160. addr_policy_list_free(policy);
  1161. addr_policy_list_free(e_policy);
  1162. UNMOCK(get_options);
  1163. }
  1164. #undef REJECT_IPv4_FINAL_STR
  1165. #undef REJECT_IPv6_FINAL_STR
  1166. #undef OTHER_IPV4_ADDR_STR
  1167. #undef OTHER_IPV6_ADDR_STR
  1168. #define TEST_IPV4_OR_PORT 1234
  1169. #define TEST_IPV4_DIR_PORT 2345
  1170. #define TEST_IPV6_OR_PORT 61234
  1171. #define TEST_IPV6_DIR_PORT 62345
  1172. /* Check that fascist_firewall_choose_address_rs() returns the expected
  1173. * results. */
  1174. #define CHECK_CHOSEN_ADDR_RS(fake_rs, fw_connection, pref_only, expect_rv, \
  1175. expect_ap) \
  1176. STMT_BEGIN \
  1177. tor_addr_port_t chosen_rs_ap; \
  1178. tor_addr_make_null(&chosen_rs_ap.addr, AF_INET); \
  1179. chosen_rs_ap.port = 0; \
  1180. tt_int_op(fascist_firewall_choose_address_rs(&(fake_rs), \
  1181. (fw_connection), \
  1182. (pref_only), \
  1183. &chosen_rs_ap), \
  1184. OP_EQ, (expect_rv)); \
  1185. tt_assert(tor_addr_eq(&(expect_ap).addr, &chosen_rs_ap.addr)); \
  1186. tt_int_op((expect_ap).port, OP_EQ, chosen_rs_ap.port); \
  1187. STMT_END
  1188. /* Check that fascist_firewall_choose_address_node() returns the expected
  1189. * results. */
  1190. #define CHECK_CHOSEN_ADDR_NODE(fake_node, fw_connection, pref_only, \
  1191. expect_rv, expect_ap) \
  1192. STMT_BEGIN \
  1193. tor_addr_port_t chosen_node_ap; \
  1194. tor_addr_make_null(&chosen_node_ap.addr, AF_INET); \
  1195. chosen_node_ap.port = 0; \
  1196. tt_int_op(fascist_firewall_choose_address_node(&(fake_node), \
  1197. (fw_connection), \
  1198. (pref_only), \
  1199. &chosen_node_ap), \
  1200. OP_EQ, (expect_rv)); \
  1201. tt_assert(tor_addr_eq(&(expect_ap).addr, &chosen_node_ap.addr)); \
  1202. tt_int_op((expect_ap).port, OP_EQ, chosen_node_ap.port); \
  1203. STMT_END
  1204. /* Check that fascist_firewall_choose_address_rs and
  1205. * fascist_firewall_choose_address_node() both return the expected results. */
  1206. #define CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, fw_connection, pref_only, \
  1207. expect_rv, expect_ap) \
  1208. STMT_BEGIN \
  1209. CHECK_CHOSEN_ADDR_RS(fake_rs, fw_connection, pref_only, expect_rv, \
  1210. expect_ap); \
  1211. CHECK_CHOSEN_ADDR_NODE(fake_node, fw_connection, pref_only, expect_rv, \
  1212. expect_ap); \
  1213. STMT_END
  1214. /** Run unit tests for fascist_firewall_choose_address */
  1215. static void
  1216. test_policies_fascist_firewall_choose_address(void *arg)
  1217. {
  1218. (void)arg;
  1219. tor_addr_port_t ipv4_or_ap, ipv4_dir_ap, ipv6_or_ap, ipv6_dir_ap;
  1220. tor_addr_port_t n_ipv4_ap, n_ipv6_ap;
  1221. /* Setup the options */
  1222. memset(&mock_options, 0, sizeof(or_options_t));
  1223. MOCK(get_options, mock_get_options);
  1224. /* Parse the addresses */
  1225. tor_addr_parse(&ipv4_or_ap.addr, TEST_IPV4_ADDR_STR);
  1226. ipv4_or_ap.port = TEST_IPV4_OR_PORT;
  1227. tor_addr_parse(&ipv4_dir_ap.addr, TEST_IPV4_ADDR_STR);
  1228. ipv4_dir_ap.port = TEST_IPV4_DIR_PORT;
  1229. tor_addr_parse(&ipv6_or_ap.addr, TEST_IPV6_ADDR_STR);
  1230. ipv6_or_ap.port = TEST_IPV6_OR_PORT;
  1231. tor_addr_parse(&ipv6_dir_ap.addr, TEST_IPV6_ADDR_STR);
  1232. ipv6_dir_ap.port = TEST_IPV6_DIR_PORT;
  1233. tor_addr_make_null(&n_ipv4_ap.addr, AF_INET);
  1234. n_ipv4_ap.port = 0;
  1235. tor_addr_make_null(&n_ipv6_ap.addr, AF_INET6);
  1236. n_ipv6_ap.port = 0;
  1237. /* Sanity check fascist_firewall_choose_address with IPv4 and IPv6 on */
  1238. memset(&mock_options, 0, sizeof(or_options_t));
  1239. mock_options.ClientUseIPv4 = 1;
  1240. mock_options.ClientUseIPv6 = 1;
  1241. mock_options.UseBridges = 0;
  1242. /* Prefer IPv4 */
  1243. tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &ipv6_or_ap, 1,
  1244. FIREWALL_OR_CONNECTION, 0, 0)
  1245. == &ipv4_or_ap);
  1246. tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &ipv6_or_ap, 1,
  1247. FIREWALL_OR_CONNECTION, 1, 0)
  1248. == &ipv4_or_ap);
  1249. tt_assert(fascist_firewall_choose_address(&ipv4_dir_ap, &ipv6_dir_ap, 1,
  1250. FIREWALL_DIR_CONNECTION, 0, 0)
  1251. == &ipv4_dir_ap);
  1252. tt_assert(fascist_firewall_choose_address(&ipv4_dir_ap, &ipv6_dir_ap, 1,
  1253. FIREWALL_DIR_CONNECTION, 1, 0)
  1254. == &ipv4_dir_ap);
  1255. /* Prefer IPv6 */
  1256. tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &ipv6_or_ap, 0,
  1257. FIREWALL_OR_CONNECTION, 0, 1)
  1258. == &ipv6_or_ap);
  1259. tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &ipv6_or_ap, 0,
  1260. FIREWALL_OR_CONNECTION, 1, 1)
  1261. == &ipv6_or_ap);
  1262. tt_assert(fascist_firewall_choose_address(&ipv4_dir_ap, &ipv6_dir_ap, 0,
  1263. FIREWALL_DIR_CONNECTION, 0, 1)
  1264. == &ipv6_dir_ap);
  1265. tt_assert(fascist_firewall_choose_address(&ipv4_dir_ap, &ipv6_dir_ap, 0,
  1266. FIREWALL_DIR_CONNECTION, 1, 1)
  1267. == &ipv6_dir_ap);
  1268. /* Unusual inputs */
  1269. /* null preferred OR addresses */
  1270. tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &n_ipv6_ap, 0,
  1271. FIREWALL_OR_CONNECTION, 0, 1)
  1272. == &ipv4_or_ap);
  1273. tt_assert(fascist_firewall_choose_address(&n_ipv4_ap, &ipv6_or_ap, 1,
  1274. FIREWALL_OR_CONNECTION, 0, 0)
  1275. == &ipv6_or_ap);
  1276. /* null both OR addresses */
  1277. tt_assert(fascist_firewall_choose_address(&n_ipv4_ap, &n_ipv6_ap, 0,
  1278. FIREWALL_OR_CONNECTION, 0, 1)
  1279. == NULL);
  1280. tt_assert(fascist_firewall_choose_address(&n_ipv4_ap, &n_ipv6_ap, 1,
  1281. FIREWALL_OR_CONNECTION, 0, 0)
  1282. == NULL);
  1283. /* null preferred Dir addresses */
  1284. tt_assert(fascist_firewall_choose_address(&ipv4_dir_ap, &n_ipv6_ap, 0,
  1285. FIREWALL_DIR_CONNECTION, 0, 1)
  1286. == &ipv4_dir_ap);
  1287. tt_assert(fascist_firewall_choose_address(&n_ipv4_ap, &ipv6_dir_ap, 1,
  1288. FIREWALL_DIR_CONNECTION, 0, 0)
  1289. == &ipv6_dir_ap);
  1290. /* null both Dir addresses */
  1291. tt_assert(fascist_firewall_choose_address(&n_ipv4_ap, &n_ipv6_ap, 0,
  1292. FIREWALL_DIR_CONNECTION, 0, 1)
  1293. == NULL);
  1294. tt_assert(fascist_firewall_choose_address(&n_ipv4_ap, &n_ipv6_ap, 1,
  1295. FIREWALL_DIR_CONNECTION, 0, 0)
  1296. == NULL);
  1297. /* Prefer IPv4 but want IPv6 (contradictory) */
  1298. tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &ipv6_or_ap, 0,
  1299. FIREWALL_OR_CONNECTION, 0, 0)
  1300. == &ipv4_or_ap);
  1301. tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &ipv6_or_ap, 0,
  1302. FIREWALL_OR_CONNECTION, 1, 0)
  1303. == &ipv4_or_ap);
  1304. /* Prefer IPv6 but want IPv4 (contradictory) */
  1305. tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &ipv6_or_ap, 1,
  1306. FIREWALL_OR_CONNECTION, 0, 1)
  1307. == &ipv6_or_ap);
  1308. tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &ipv6_or_ap, 1,
  1309. FIREWALL_OR_CONNECTION, 1, 1)
  1310. == &ipv6_or_ap);
  1311. /* Make a fake rs. There will be no corresponding node.
  1312. * This is what happens when there's no consensus and we're bootstrapping
  1313. * from authorities / fallbacks. */
  1314. routerstatus_t fake_rs;
  1315. memset(&fake_rs, 0, sizeof(routerstatus_t));
  1316. /* In a routerstatus, the OR and Dir addresses are the same */
  1317. fake_rs.addr = tor_addr_to_ipv4h(&ipv4_or_ap.addr);
  1318. fake_rs.or_port = ipv4_or_ap.port;
  1319. fake_rs.dir_port = ipv4_dir_ap.port;
  1320. tor_addr_copy(&fake_rs.ipv6_addr, &ipv6_or_ap.addr);
  1321. fake_rs.ipv6_orport = ipv6_or_ap.port;
  1322. /* In a routerstatus, the IPv4 and IPv6 DirPorts are the same.*/
  1323. ipv6_dir_ap.port = TEST_IPV4_DIR_PORT;
  1324. /* Make a fake node. Even though it contains the fake_rs, a lookup won't
  1325. * find the node from the rs, because they're not in the hash table. */
  1326. node_t fake_node;
  1327. memset(&fake_node, 0, sizeof(node_t));
  1328. fake_node.rs = &fake_rs;
  1329. /* Choose an address with IPv4 and IPv6 on */
  1330. memset(&mock_options, 0, sizeof(or_options_t));
  1331. mock_options.ClientUseIPv4 = 1;
  1332. mock_options.ClientUseIPv6 = 1;
  1333. mock_options.UseBridges = 0;
  1334. /* Preferring IPv4 */
  1335. mock_options.ClientPreferIPv6ORPort = 0;
  1336. mock_options.ClientPreferIPv6DirPort = 0;
  1337. /* Simulate the initialisation of fake_node.ipv6_preferred */
  1338. fake_node.ipv6_preferred = fascist_firewall_prefer_ipv6_orport(
  1339. &mock_options);
  1340. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 0, 1,
  1341. ipv4_or_ap);
  1342. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 1, 1,
  1343. ipv4_or_ap);
  1344. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
  1345. ipv4_dir_ap);
  1346. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
  1347. ipv4_dir_ap);
  1348. /* Auto (Preferring IPv4) */
  1349. mock_options.ClientPreferIPv6ORPort = -1;
  1350. mock_options.ClientPreferIPv6DirPort = -1;
  1351. /* Simulate the initialisation of fake_node.ipv6_preferred */
  1352. fake_node.ipv6_preferred = fascist_firewall_prefer_ipv6_orport(
  1353. &mock_options);
  1354. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 0, 1,
  1355. ipv4_or_ap);
  1356. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 1, 1,
  1357. ipv4_or_ap);
  1358. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
  1359. ipv4_dir_ap);
  1360. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
  1361. ipv4_dir_ap);
  1362. /* Preferring IPv6 */
  1363. mock_options.ClientPreferIPv6ORPort = 1;
  1364. mock_options.ClientPreferIPv6DirPort = 1;
  1365. /* Simulate the initialisation of fake_node.ipv6_preferred */
  1366. fake_node.ipv6_preferred = fascist_firewall_prefer_ipv6_orport(
  1367. &mock_options);
  1368. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 0, 1,
  1369. ipv6_or_ap);
  1370. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 1, 1,
  1371. ipv6_or_ap);
  1372. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
  1373. ipv6_dir_ap);
  1374. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
  1375. ipv6_dir_ap);
  1376. /* Preferring IPv4 OR / IPv6 Dir */
  1377. mock_options.ClientPreferIPv6ORPort = 0;
  1378. mock_options.ClientPreferIPv6DirPort = 1;
  1379. /* Simulate the initialisation of fake_node.ipv6_preferred */
  1380. fake_node.ipv6_preferred = fascist_firewall_prefer_ipv6_orport(
  1381. &mock_options);
  1382. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 0, 1,
  1383. ipv4_or_ap);
  1384. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 1, 1,
  1385. ipv4_or_ap);
  1386. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
  1387. ipv6_dir_ap);
  1388. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
  1389. ipv6_dir_ap);
  1390. /* Preferring IPv6 OR / IPv4 Dir */
  1391. mock_options.ClientPreferIPv6ORPort = 1;
  1392. mock_options.ClientPreferIPv6DirPort = 0;
  1393. /* Simulate the initialisation of fake_node.ipv6_preferred */
  1394. fake_node.ipv6_preferred = fascist_firewall_prefer_ipv6_orport(
  1395. &mock_options);
  1396. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 0, 1,
  1397. ipv6_or_ap);
  1398. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 1, 1,
  1399. ipv6_or_ap);
  1400. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
  1401. ipv4_dir_ap);
  1402. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
  1403. ipv4_dir_ap);
  1404. /* Choose an address with UseBridges on */
  1405. memset(&mock_options, 0, sizeof(or_options_t));
  1406. mock_options.UseBridges = 1;
  1407. mock_options.ClientUseIPv4 = 1;
  1408. mock_options.ClientUseIPv6 = 1;
  1409. /* Preferring IPv4 */
  1410. mock_options.ClientPreferIPv6ORPort = 0;
  1411. mock_options.ClientPreferIPv6DirPort = 0;
  1412. /* Simulate the initialisation of fake_node.ipv6_preferred */
  1413. fake_node.ipv6_preferred = fascist_firewall_prefer_ipv6_orport(
  1414. &mock_options);
  1415. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 0, 1,
  1416. ipv4_or_ap);
  1417. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 1, 1,
  1418. ipv4_or_ap);
  1419. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
  1420. ipv4_dir_ap);
  1421. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
  1422. ipv4_dir_ap);
  1423. /* Auto:
  1424. * - bridge clients prefer the configured bridge OR address from the node,
  1425. * (the configured address family sets node.ipv6_preferred)
  1426. * - other clients prefer IPv4 OR by default (see above),
  1427. * - all clients, including bridge clients, prefer IPv4 Dir by default.
  1428. */
  1429. mock_options.ClientPreferIPv6ORPort = -1;
  1430. mock_options.ClientPreferIPv6DirPort = -1;
  1431. /* Simulate the initialisation of fake_node.ipv6_preferred with a bridge
  1432. * configured with an IPv4 address */
  1433. fake_node.ipv6_preferred = 0;
  1434. CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_OR_CONNECTION, 0, 1, ipv4_or_ap);
  1435. CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_OR_CONNECTION, 1, 1, ipv4_or_ap);
  1436. CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
  1437. ipv4_dir_ap);
  1438. CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
  1439. ipv4_dir_ap);
  1440. /* Simulate the initialisation of fake_node.ipv6_preferred with a bridge
  1441. * configured with an IPv6 address */
  1442. fake_node.ipv6_preferred = 1;
  1443. CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_OR_CONNECTION, 0, 1, ipv6_or_ap);
  1444. CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_OR_CONNECTION, 1, 1, ipv6_or_ap);
  1445. CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
  1446. ipv4_dir_ap);
  1447. CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
  1448. ipv4_dir_ap);
  1449. /* When a rs has no node, it defaults to IPv4 under auto. */
  1450. CHECK_CHOSEN_ADDR_RS(fake_rs, FIREWALL_OR_CONNECTION, 0, 1, ipv4_or_ap);
  1451. CHECK_CHOSEN_ADDR_RS(fake_rs, FIREWALL_OR_CONNECTION, 1, 1, ipv4_or_ap);
  1452. CHECK_CHOSEN_ADDR_RS(fake_rs, FIREWALL_DIR_CONNECTION, 0, 1, ipv4_dir_ap);
  1453. CHECK_CHOSEN_ADDR_RS(fake_rs, FIREWALL_DIR_CONNECTION, 1, 1, ipv4_dir_ap);
  1454. /* Preferring IPv6 */
  1455. mock_options.ClientPreferIPv6ORPort = 1;
  1456. mock_options.ClientPreferIPv6DirPort = 1;
  1457. /* Simulate the initialisation of fake_node.ipv6_preferred */
  1458. fake_node.ipv6_preferred = fascist_firewall_prefer_ipv6_orport(
  1459. &mock_options);
  1460. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 0, 1,
  1461. ipv6_or_ap);
  1462. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 1, 1,
  1463. ipv6_or_ap);
  1464. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
  1465. ipv6_dir_ap);
  1466. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
  1467. ipv6_dir_ap);
  1468. /* In the default configuration (Auto / IPv6 off), bridge clients should
  1469. * use both IPv4 and IPv6, but only prefer IPv6 for bridges configured with
  1470. * an IPv6 address, regardless of ClientUseIPv6. (See above.) */
  1471. mock_options.ClientUseIPv6 = 0;
  1472. mock_options.ClientPreferIPv6ORPort = -1;
  1473. mock_options.ClientPreferIPv6DirPort = -1;
  1474. /* Simulate the initialisation of fake_node.ipv6_preferred with a bridge
  1475. * configured with an IPv4 address */
  1476. fake_node.ipv6_preferred = 0;
  1477. CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_OR_CONNECTION, 0, 1, ipv4_or_ap);
  1478. CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_OR_CONNECTION, 1, 1, ipv4_or_ap);
  1479. CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
  1480. ipv4_dir_ap);
  1481. CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
  1482. ipv4_dir_ap);
  1483. /* Simulate the initialisation of fake_node.ipv6_preferred with a bridge
  1484. * configured with an IPv6 address */
  1485. fake_node.ipv6_preferred = 1;
  1486. CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_OR_CONNECTION, 0, 1, ipv6_or_ap);
  1487. CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_OR_CONNECTION, 1, 1, ipv6_or_ap);
  1488. CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
  1489. ipv4_dir_ap);
  1490. CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
  1491. ipv4_dir_ap);
  1492. /* When a rs has no node, it defaults to IPv4 under auto. */
  1493. CHECK_CHOSEN_ADDR_RS(fake_rs, FIREWALL_OR_CONNECTION, 0, 1, ipv4_or_ap);
  1494. CHECK_CHOSEN_ADDR_RS(fake_rs, FIREWALL_OR_CONNECTION, 1, 1, ipv4_or_ap);
  1495. CHECK_CHOSEN_ADDR_RS(fake_rs, FIREWALL_DIR_CONNECTION, 0, 1, ipv4_dir_ap);
  1496. CHECK_CHOSEN_ADDR_RS(fake_rs, FIREWALL_DIR_CONNECTION, 1, 1, ipv4_dir_ap);
  1497. /* Choose an address with IPv4 on */
  1498. memset(&mock_options, 0, sizeof(or_options_t));
  1499. mock_options.ClientUseIPv4 = 1;
  1500. mock_options.ClientUseIPv6 = 0;
  1501. /* Simulate the initialisation of fake_node.ipv6_preferred */
  1502. fake_node.ipv6_preferred = fascist_firewall_prefer_ipv6_orport(
  1503. &mock_options);
  1504. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 0, 1,
  1505. ipv4_or_ap);
  1506. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 1, 1,
  1507. ipv4_or_ap);
  1508. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
  1509. ipv4_dir_ap);
  1510. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
  1511. ipv4_dir_ap);
  1512. /* Choose an address with IPv6 on */
  1513. memset(&mock_options, 0, sizeof(or_options_t));
  1514. mock_options.ClientUseIPv4 = 0;
  1515. mock_options.ClientUseIPv6 = 1;
  1516. /* Simulate the initialisation of fake_node.ipv6_preferred */
  1517. fake_node.ipv6_preferred = fascist_firewall_prefer_ipv6_orport(
  1518. &mock_options);
  1519. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 0, 1,
  1520. ipv6_or_ap);
  1521. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 1, 1,
  1522. ipv6_or_ap);
  1523. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
  1524. ipv6_dir_ap);
  1525. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
  1526. ipv6_dir_ap);
  1527. /* Choose an address with ClientUseIPv4 0.
  1528. * This means "use IPv6" regardless of the other settings. */
  1529. memset(&mock_options, 0, sizeof(or_options_t));
  1530. mock_options.ClientUseIPv4 = 0;
  1531. mock_options.ClientUseIPv6 = 0;
  1532. /* Simulate the initialisation of fake_node.ipv6_preferred */
  1533. fake_node.ipv6_preferred = fascist_firewall_prefer_ipv6_orport(
  1534. &mock_options);
  1535. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 0, 1,
  1536. ipv6_or_ap);
  1537. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 1, 1,
  1538. ipv6_or_ap);
  1539. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
  1540. ipv6_dir_ap);
  1541. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
  1542. ipv6_dir_ap);
  1543. /* Choose an address with ORPort_set 1 (server mode).
  1544. * This means "use IPv4" regardless of the other settings. */
  1545. memset(&mock_options, 0, sizeof(or_options_t));
  1546. mock_options.ORPort_set = 1;
  1547. mock_options.ClientUseIPv4 = 0;
  1548. mock_options.ClientUseIPv6 = 1;
  1549. mock_options.ClientPreferIPv6ORPort = 1;
  1550. mock_options.ClientPreferIPv6DirPort = 1;
  1551. /* Simulate the initialisation of fake_node.ipv6_preferred */
  1552. fake_node.ipv6_preferred = fascist_firewall_prefer_ipv6_orport(
  1553. &mock_options);
  1554. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 0, 1,
  1555. ipv4_or_ap);
  1556. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 1, 1,
  1557. ipv4_or_ap);
  1558. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
  1559. ipv4_dir_ap);
  1560. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
  1561. ipv4_dir_ap);
  1562. done:
  1563. UNMOCK(get_options);
  1564. }
  1565. #undef TEST_IPV4_ADDR_STR
  1566. #undef TEST_IPV6_ADDR_STR
  1567. #undef TEST_IPV4_OR_PORT
  1568. #undef TEST_IPV4_DIR_PORT
  1569. #undef TEST_IPV6_OR_PORT
  1570. #undef TEST_IPV6_DIR_PORT
  1571. #undef CHECK_CHOSEN_ADDR_RS
  1572. #undef CHECK_CHOSEN_ADDR_NODE
  1573. #undef CHECK_CHOSEN_ADDR_RN
  1574. struct testcase_t policy_tests[] = {
  1575. { "router_dump_exit_policy_to_string", test_dump_exit_policy_to_string, 0,
  1576. NULL, NULL },
  1577. { "general", test_policies_general, 0, NULL, NULL },
  1578. { "getinfo_helper_policies", test_policies_getinfo_helper_policies, 0, NULL,
  1579. NULL },
  1580. { "reject_exit_address", test_policies_reject_exit_address, 0, NULL, NULL },
  1581. { "reject_interface_address", test_policies_reject_interface_address, 0,
  1582. NULL, NULL },
  1583. { "reject_port_address", test_policies_reject_port_address, 0, NULL, NULL },
  1584. { "fascist_firewall_allows_address",
  1585. test_policies_fascist_firewall_allows_address, 0, NULL, NULL },
  1586. { "fascist_firewall_choose_address",
  1587. test_policies_fascist_firewall_choose_address, 0, NULL, NULL },
  1588. END_OF_TESTCASES
  1589. };