test_policy.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673
  1. /* Copyright (c) 2013-2015, 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. const smartlist_t *mock_get_configured_ports(void);
  629. /** Returns test_configured_ports */
  630. const smartlist_t *
  631. mock_get_configured_ports(void)
  632. {
  633. return test_configured_ports;
  634. }
  635. /** Run unit tests for rejecting publicly routable configured port addresses
  636. * on this exit relay using policies_parse_exit_policy_reject_private */
  637. static void
  638. test_policies_reject_port_address(void *arg)
  639. {
  640. smartlist_t *policy = NULL;
  641. port_cfg_t *ipv4_port = NULL;
  642. port_cfg_t *ipv6_port = NULL;
  643. (void)arg;
  644. test_configured_ports = smartlist_new();
  645. ipv4_port = port_cfg_new(0);
  646. tor_addr_from_ipv4h(&ipv4_port->addr, TEST_IPV4_ADDR);
  647. smartlist_add(test_configured_ports, ipv4_port);
  648. ipv6_port = port_cfg_new(0);
  649. tor_addr_parse(&ipv6_port->addr, TEST_IPV6_ADDR);
  650. smartlist_add(test_configured_ports, ipv6_port);
  651. MOCK(get_configured_ports, mock_get_configured_ports);
  652. /* test that an IPv4 port is rejected on an IPv4-only exit, but an IPv6 port
  653. * is NOT rejected (all IPv6 addresses are rejected by
  654. * policies_parse_exit_policy_internal on IPv4-only exits, so
  655. * policies_parse_exit_policy_reject_private doesn't need to do anything
  656. * with IPv6 addresses on IPv4-only exits) */
  657. policies_parse_exit_policy_reject_private(&policy, 0, NULL, 0, 1);
  658. tt_assert(policy);
  659. tt_assert(smartlist_len(policy) == 1);
  660. tt_assert(test_policy_has_address_helper(policy, &ipv4_port->addr));
  661. addr_policy_list_free(policy);
  662. policy = NULL;
  663. /* test that IPv4 and IPv6 ports are rejected on an IPv4/IPv6 exit */
  664. policies_parse_exit_policy_reject_private(&policy, 1, NULL, 0, 1);
  665. tt_assert(policy);
  666. tt_assert(smartlist_len(policy) == 2);
  667. tt_assert(test_policy_has_address_helper(policy, &ipv4_port->addr));
  668. tt_assert(test_policy_has_address_helper(policy, &ipv6_port->addr));
  669. addr_policy_list_free(policy);
  670. policy = NULL;
  671. done:
  672. addr_policy_list_free(policy);
  673. if (test_configured_ports) {
  674. SMARTLIST_FOREACH(test_configured_ports,
  675. port_cfg_t *, p, port_cfg_free(p));
  676. smartlist_free(test_configured_ports);
  677. test_configured_ports = NULL;
  678. }
  679. UNMOCK(get_configured_ports);
  680. }
  681. smartlist_t *mock_ipv4_addrs = NULL;
  682. smartlist_t *mock_ipv6_addrs = NULL;
  683. /* mock get_interface_address6_list, returning a deep copy of the template
  684. * address list ipv4_interface_address_list or ipv6_interface_address_list */
  685. static smartlist_t *
  686. mock_get_interface_address6_list(int severity,
  687. sa_family_t family,
  688. int include_internal)
  689. {
  690. (void)severity;
  691. (void)include_internal;
  692. smartlist_t *clone_list = smartlist_new();
  693. smartlist_t *template_list = NULL;
  694. if (family == AF_INET) {
  695. template_list = mock_ipv4_addrs;
  696. } else if (family == AF_INET6) {
  697. template_list = mock_ipv6_addrs;
  698. } else {
  699. return NULL;
  700. }
  701. tt_assert(template_list);
  702. SMARTLIST_FOREACH_BEGIN(template_list, tor_addr_t *, src_addr) {
  703. tor_addr_t *dest_addr = malloc(sizeof(tor_addr_t));
  704. memset(dest_addr, 0, sizeof(*dest_addr));
  705. tor_addr_copy_tight(dest_addr, src_addr);
  706. smartlist_add(clone_list, dest_addr);
  707. } SMARTLIST_FOREACH_END(src_addr);
  708. return clone_list;
  709. done:
  710. free_interface_address6_list(clone_list);
  711. return NULL;
  712. }
  713. /** Run unit tests for rejecting publicly routable interface addresses on this
  714. * exit relay using policies_parse_exit_policy_reject_private */
  715. static void
  716. test_policies_reject_interface_address(void *arg)
  717. {
  718. smartlist_t *policy = NULL;
  719. smartlist_t *public_ipv4_addrs =
  720. get_interface_address6_list(LOG_INFO, AF_INET, 0);
  721. smartlist_t *public_ipv6_addrs =
  722. get_interface_address6_list(LOG_INFO, AF_INET6, 0);
  723. tor_addr_t ipv4_addr, ipv6_addr;
  724. (void)arg;
  725. /* test that no addresses are rejected when none are supplied/requested */
  726. policies_parse_exit_policy_reject_private(&policy, 0, NULL, 0, 0);
  727. tt_assert(policy == NULL);
  728. /* test that only IPv4 interface addresses are rejected on an IPv4-only exit
  729. * (and allow for duplicates)
  730. */
  731. policies_parse_exit_policy_reject_private(&policy, 0, NULL, 1, 0);
  732. if (policy) {
  733. tt_assert(smartlist_len(policy) <= smartlist_len(public_ipv4_addrs));
  734. addr_policy_list_free(policy);
  735. policy = NULL;
  736. }
  737. /* test that IPv4 and IPv6 interface addresses are rejected on an IPv4/IPv6
  738. * exit (and allow for duplicates) */
  739. policies_parse_exit_policy_reject_private(&policy, 1, NULL, 1, 0);
  740. if (policy) {
  741. tt_assert(smartlist_len(policy) <= (smartlist_len(public_ipv4_addrs)
  742. + smartlist_len(public_ipv6_addrs)));
  743. addr_policy_list_free(policy);
  744. policy = NULL;
  745. }
  746. /* Now do it all again, but mocked */
  747. tor_addr_from_ipv4h(&ipv4_addr, TEST_IPV4_ADDR);
  748. mock_ipv4_addrs = smartlist_new();
  749. smartlist_add(mock_ipv4_addrs, (void *)&ipv4_addr);
  750. tor_addr_parse(&ipv6_addr, TEST_IPV6_ADDR);
  751. mock_ipv6_addrs = smartlist_new();
  752. smartlist_add(mock_ipv6_addrs, (void *)&ipv6_addr);
  753. MOCK(get_interface_address6_list, mock_get_interface_address6_list);
  754. /* test that no addresses are rejected when none are supplied/requested */
  755. policies_parse_exit_policy_reject_private(&policy, 0, NULL, 0, 0);
  756. tt_assert(policy == NULL);
  757. /* test that only IPv4 interface addresses are rejected on an IPv4-only exit
  758. */
  759. policies_parse_exit_policy_reject_private(&policy, 0, NULL, 1, 0);
  760. tt_assert(policy);
  761. tt_assert(smartlist_len(policy) == smartlist_len(mock_ipv4_addrs));
  762. addr_policy_list_free(policy);
  763. policy = NULL;
  764. /* test that IPv4 and IPv6 interface addresses are rejected on an IPv4/IPv6
  765. * exit */
  766. policies_parse_exit_policy_reject_private(&policy, 1, NULL, 1, 0);
  767. tt_assert(policy);
  768. tt_assert(smartlist_len(policy) == (smartlist_len(mock_ipv4_addrs)
  769. + smartlist_len(mock_ipv6_addrs)));
  770. addr_policy_list_free(policy);
  771. policy = NULL;
  772. done:
  773. addr_policy_list_free(policy);
  774. free_interface_address6_list(public_ipv4_addrs);
  775. free_interface_address6_list(public_ipv6_addrs);
  776. UNMOCK(get_interface_address6_list);
  777. /* we don't use free_interface_address6_list on these lists because their
  778. * address pointers are stack-based */
  779. smartlist_free(mock_ipv4_addrs);
  780. smartlist_free(mock_ipv6_addrs);
  781. }
  782. #undef TEST_IPV4_ADDR
  783. #undef TEST_IPV6_ADDR
  784. static void
  785. test_dump_exit_policy_to_string(void *arg)
  786. {
  787. char *ep;
  788. addr_policy_t *policy_entry;
  789. int malformed_list = -1;
  790. routerinfo_t *ri = tor_malloc_zero(sizeof(routerinfo_t));
  791. (void)arg;
  792. ri->policy_is_reject_star = 1;
  793. ri->exit_policy = NULL; // expecting "reject *:*"
  794. ep = router_dump_exit_policy_to_string(ri,1,1);
  795. tt_str_op("reject *:*",OP_EQ, ep);
  796. tor_free(ep);
  797. ri->exit_policy = smartlist_new();
  798. ri->policy_is_reject_star = 0;
  799. policy_entry = router_parse_addr_policy_item_from_string("accept *:*", -1,
  800. &malformed_list);
  801. smartlist_add(ri->exit_policy,policy_entry);
  802. ep = router_dump_exit_policy_to_string(ri,1,1);
  803. tt_str_op("accept *:*",OP_EQ, ep);
  804. tor_free(ep);
  805. policy_entry = router_parse_addr_policy_item_from_string("reject *:25", -1,
  806. &malformed_list);
  807. smartlist_add(ri->exit_policy,policy_entry);
  808. ep = router_dump_exit_policy_to_string(ri,1,1);
  809. tt_str_op("accept *:*\nreject *:25",OP_EQ, ep);
  810. tor_free(ep);
  811. policy_entry =
  812. router_parse_addr_policy_item_from_string("reject 8.8.8.8:*", -1,
  813. &malformed_list);
  814. smartlist_add(ri->exit_policy,policy_entry);
  815. ep = router_dump_exit_policy_to_string(ri,1,1);
  816. tt_str_op("accept *:*\nreject *:25\nreject 8.8.8.8:*",OP_EQ, ep);
  817. tor_free(ep);
  818. policy_entry =
  819. router_parse_addr_policy_item_from_string("reject6 [FC00::]/7:*", -1,
  820. &malformed_list);
  821. smartlist_add(ri->exit_policy,policy_entry);
  822. ep = router_dump_exit_policy_to_string(ri,1,1);
  823. tt_str_op("accept *:*\nreject *:25\nreject 8.8.8.8:*\n"
  824. "reject6 [fc00::]/7:*",OP_EQ, ep);
  825. tor_free(ep);
  826. policy_entry =
  827. router_parse_addr_policy_item_from_string("accept6 [c000::]/3:*", -1,
  828. &malformed_list);
  829. smartlist_add(ri->exit_policy,policy_entry);
  830. ep = router_dump_exit_policy_to_string(ri,1,1);
  831. tt_str_op("accept *:*\nreject *:25\nreject 8.8.8.8:*\n"
  832. "reject6 [fc00::]/7:*\naccept6 [c000::]/3:*",OP_EQ, ep);
  833. done:
  834. if (ri->exit_policy) {
  835. SMARTLIST_FOREACH(ri->exit_policy, addr_policy_t *,
  836. entry, addr_policy_free(entry));
  837. smartlist_free(ri->exit_policy);
  838. }
  839. tor_free(ri);
  840. tor_free(ep);
  841. }
  842. static routerinfo_t *mock_desc_routerinfo = NULL;
  843. static const routerinfo_t *
  844. mock_router_get_my_routerinfo(void)
  845. {
  846. return mock_desc_routerinfo;
  847. }
  848. #define DEFAULT_POLICY_STRING "reject *:*"
  849. #define TEST_IPV4_ADDR (0x02040608)
  850. #define TEST_IPV6_ADDR ("2003::ef01")
  851. static or_options_t mock_options;
  852. static const or_options_t *
  853. mock_get_options(void)
  854. {
  855. return &mock_options;
  856. }
  857. /** Run unit tests for generating summary lines of exit policies */
  858. static void
  859. test_policies_getinfo_helper_policies(void *arg)
  860. {
  861. (void)arg;
  862. int rv = 0;
  863. size_t ipv4_len = 0, ipv6_len = 0;
  864. char *answer = NULL;
  865. const char *errmsg = NULL;
  866. routerinfo_t 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. /** Run unit tests for fascist_firewall_choose_address */
  1173. static void
  1174. test_policies_fascist_firewall_choose_address(void *arg)
  1175. {
  1176. (void)arg;
  1177. tor_addr_port_t ipv4_or_ap, ipv4_dir_ap, ipv6_or_ap, ipv6_dir_ap;
  1178. tor_addr_port_t n_ipv4_ap, n_ipv6_ap;
  1179. /* Setup the options */
  1180. memset(&mock_options, 0, sizeof(or_options_t));
  1181. MOCK(get_options, mock_get_options);
  1182. /* Parse the addresses */
  1183. tor_addr_parse(&ipv4_or_ap.addr, TEST_IPV4_ADDR_STR);
  1184. ipv4_or_ap.port = TEST_IPV4_OR_PORT;
  1185. tor_addr_parse(&ipv4_dir_ap.addr, TEST_IPV4_ADDR_STR);
  1186. ipv4_dir_ap.port = TEST_IPV4_DIR_PORT;
  1187. tor_addr_parse(&ipv6_or_ap.addr, TEST_IPV6_ADDR_STR);
  1188. ipv6_or_ap.port = TEST_IPV6_OR_PORT;
  1189. tor_addr_parse(&ipv6_dir_ap.addr, TEST_IPV6_ADDR_STR);
  1190. ipv6_dir_ap.port = TEST_IPV6_DIR_PORT;
  1191. tor_addr_make_null(&n_ipv4_ap.addr, AF_INET);
  1192. n_ipv4_ap.port = 0;
  1193. tor_addr_make_null(&n_ipv6_ap.addr, AF_INET6);
  1194. n_ipv6_ap.port = 0;
  1195. /* Choose an address with IPv4 and IPv6 on */
  1196. memset(&mock_options, 0, sizeof(or_options_t));
  1197. mock_options.ClientUseIPv4 = 1;
  1198. mock_options.ClientUseIPv6 = 1;
  1199. mock_options.UseBridges = 0;
  1200. /* Preferring IPv4 */
  1201. mock_options.ClientPreferIPv6ORPort = 0;
  1202. mock_options.ClientPreferIPv6DirPort = 0;
  1203. tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &ipv6_or_ap, 0,
  1204. FIREWALL_OR_CONNECTION, 0)
  1205. == &ipv4_or_ap);
  1206. tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &ipv6_or_ap, 0,
  1207. FIREWALL_OR_CONNECTION, 1)
  1208. == &ipv4_or_ap);
  1209. tt_assert(fascist_firewall_choose_address(&ipv4_dir_ap, &ipv6_dir_ap, 0,
  1210. FIREWALL_DIR_CONNECTION, 0)
  1211. == &ipv4_dir_ap);
  1212. tt_assert(fascist_firewall_choose_address(&ipv4_dir_ap, &ipv6_dir_ap, 0,
  1213. FIREWALL_DIR_CONNECTION, 1)
  1214. == &ipv4_dir_ap);
  1215. /* Auto (Preferring IPv4) */
  1216. mock_options.ClientPreferIPv6ORPort = -1;
  1217. mock_options.ClientPreferIPv6DirPort = -1;
  1218. tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &ipv6_or_ap, 0,
  1219. FIREWALL_OR_CONNECTION, 0)
  1220. == &ipv4_or_ap);
  1221. tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &ipv6_or_ap, 0,
  1222. FIREWALL_OR_CONNECTION, 1)
  1223. == &ipv4_or_ap);
  1224. tt_assert(fascist_firewall_choose_address(&ipv4_dir_ap, &ipv6_dir_ap, 0,
  1225. FIREWALL_DIR_CONNECTION, 0)
  1226. == &ipv4_dir_ap);
  1227. tt_assert(fascist_firewall_choose_address(&ipv4_dir_ap, &ipv6_dir_ap, 0,
  1228. FIREWALL_DIR_CONNECTION, 1)
  1229. == &ipv4_dir_ap);
  1230. /* Preferring IPv6 */
  1231. mock_options.ClientPreferIPv6ORPort = 1;
  1232. mock_options.ClientPreferIPv6DirPort = 1;
  1233. tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &ipv6_or_ap, 0,
  1234. FIREWALL_OR_CONNECTION, 0)
  1235. == &ipv6_or_ap);
  1236. tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &ipv6_or_ap, 0,
  1237. FIREWALL_OR_CONNECTION, 1)
  1238. == &ipv6_or_ap);
  1239. tt_assert(fascist_firewall_choose_address(&ipv4_dir_ap, &ipv6_dir_ap, 0,
  1240. FIREWALL_DIR_CONNECTION, 0)
  1241. == &ipv6_dir_ap);
  1242. tt_assert(fascist_firewall_choose_address(&ipv4_dir_ap, &ipv6_dir_ap, 0,
  1243. FIREWALL_DIR_CONNECTION, 1)
  1244. == &ipv6_dir_ap);
  1245. /* Preferring IPv4 OR / IPv6 Dir */
  1246. mock_options.ClientPreferIPv6ORPort = 0;
  1247. mock_options.ClientPreferIPv6DirPort = 1;
  1248. tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &ipv6_or_ap, 0,
  1249. FIREWALL_OR_CONNECTION, 0)
  1250. == &ipv4_or_ap);
  1251. tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &ipv6_or_ap, 0,
  1252. FIREWALL_OR_CONNECTION, 1)
  1253. == &ipv4_or_ap);
  1254. tt_assert(fascist_firewall_choose_address(&ipv4_dir_ap, &ipv6_dir_ap, 0,
  1255. FIREWALL_DIR_CONNECTION, 0)
  1256. == &ipv6_dir_ap);
  1257. tt_assert(fascist_firewall_choose_address(&ipv4_dir_ap, &ipv6_dir_ap, 0,
  1258. FIREWALL_DIR_CONNECTION, 1)
  1259. == &ipv6_dir_ap);
  1260. /* Preferring IPv6 OR / IPv4 Dir */
  1261. mock_options.ClientPreferIPv6ORPort = 1;
  1262. mock_options.ClientPreferIPv6DirPort = 0;
  1263. tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &ipv6_or_ap, 0,
  1264. FIREWALL_OR_CONNECTION, 0)
  1265. == &ipv6_or_ap);
  1266. tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &ipv6_or_ap, 0,
  1267. FIREWALL_OR_CONNECTION, 1)
  1268. == &ipv6_or_ap);
  1269. tt_assert(fascist_firewall_choose_address(&ipv4_dir_ap, &ipv6_dir_ap, 0,
  1270. FIREWALL_DIR_CONNECTION, 0)
  1271. == &ipv4_dir_ap);
  1272. tt_assert(fascist_firewall_choose_address(&ipv4_dir_ap, &ipv6_dir_ap, 0,
  1273. FIREWALL_DIR_CONNECTION, 1)
  1274. == &ipv4_dir_ap);
  1275. /* Choose an address with UseBridges on */
  1276. memset(&mock_options, 0, sizeof(or_options_t));
  1277. mock_options.UseBridges = 1;
  1278. mock_options.ClientUseIPv4 = 1;
  1279. mock_options.ClientUseIPv6 = 1;
  1280. /* Preferring IPv4 */
  1281. mock_options.ClientPreferIPv6ORPort = 0;
  1282. mock_options.ClientPreferIPv6DirPort = 0;
  1283. tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &ipv6_or_ap, 0,
  1284. FIREWALL_OR_CONNECTION, 0)
  1285. == &ipv4_or_ap);
  1286. tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &ipv6_or_ap, 0,
  1287. FIREWALL_OR_CONNECTION, 1)
  1288. == &ipv4_or_ap);
  1289. tt_assert(fascist_firewall_choose_address(&ipv4_dir_ap, &ipv6_dir_ap, 0,
  1290. FIREWALL_DIR_CONNECTION, 0)
  1291. == &ipv4_dir_ap);
  1292. tt_assert(fascist_firewall_choose_address(&ipv4_dir_ap, &ipv6_dir_ap, 0,
  1293. FIREWALL_DIR_CONNECTION, 1)
  1294. == &ipv4_dir_ap);
  1295. /* Auto (Preferring IPv6 for bridge clients) */
  1296. mock_options.ClientPreferIPv6ORPort = -1;
  1297. mock_options.ClientPreferIPv6DirPort = -1;
  1298. tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &ipv6_or_ap, 0,
  1299. FIREWALL_OR_CONNECTION, 0)
  1300. == &ipv6_or_ap);
  1301. tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &ipv6_or_ap, 0,
  1302. FIREWALL_OR_CONNECTION, 1)
  1303. == &ipv6_or_ap);
  1304. tt_assert(fascist_firewall_choose_address(&ipv4_dir_ap, &ipv6_dir_ap, 0,
  1305. FIREWALL_DIR_CONNECTION, 0)
  1306. == &ipv6_dir_ap);
  1307. tt_assert(fascist_firewall_choose_address(&ipv4_dir_ap, &ipv6_dir_ap, 0,
  1308. FIREWALL_DIR_CONNECTION, 1)
  1309. == &ipv6_dir_ap);
  1310. /* Preferring IPv6 */
  1311. mock_options.ClientPreferIPv6ORPort = 1;
  1312. mock_options.ClientPreferIPv6DirPort = 1;
  1313. tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &ipv6_or_ap, 0,
  1314. FIREWALL_OR_CONNECTION, 0)
  1315. == &ipv6_or_ap);
  1316. tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &ipv6_or_ap, 0,
  1317. FIREWALL_OR_CONNECTION, 1)
  1318. == &ipv6_or_ap);
  1319. tt_assert(fascist_firewall_choose_address(&ipv4_dir_ap, &ipv6_dir_ap, 0,
  1320. FIREWALL_DIR_CONNECTION, 0)
  1321. == &ipv6_dir_ap);
  1322. tt_assert(fascist_firewall_choose_address(&ipv4_dir_ap, &ipv6_dir_ap, 0,
  1323. FIREWALL_DIR_CONNECTION, 1)
  1324. == &ipv6_dir_ap);
  1325. /* In the default configuration (Auto / IPv6 off), bridge clients should
  1326. * still use and prefer IPv6 regardless of ClientUseIPv6. */
  1327. mock_options.ClientUseIPv6 = 0;
  1328. mock_options.ClientPreferIPv6ORPort = -1;
  1329. mock_options.ClientPreferIPv6DirPort = -1;
  1330. tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &ipv6_or_ap, 0,
  1331. FIREWALL_OR_CONNECTION, 0)
  1332. == &ipv6_or_ap);
  1333. tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &ipv6_or_ap, 0,
  1334. FIREWALL_OR_CONNECTION, 1)
  1335. == &ipv6_or_ap);
  1336. tt_assert(fascist_firewall_choose_address(&ipv4_dir_ap, &ipv6_dir_ap, 0,
  1337. FIREWALL_DIR_CONNECTION, 0)
  1338. == &ipv6_dir_ap);
  1339. tt_assert(fascist_firewall_choose_address(&ipv4_dir_ap, &ipv6_dir_ap, 0,
  1340. FIREWALL_DIR_CONNECTION, 1)
  1341. == &ipv6_dir_ap);
  1342. /* Choose an address with IPv4 on */
  1343. memset(&mock_options, 0, sizeof(or_options_t));
  1344. mock_options.ClientUseIPv4 = 1;
  1345. mock_options.ClientUseIPv6 = 0;
  1346. mock_options.UseBridges = 0;
  1347. tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &ipv6_or_ap, 0,
  1348. FIREWALL_OR_CONNECTION, 0)
  1349. == &ipv4_or_ap);
  1350. tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &ipv6_or_ap, 0,
  1351. FIREWALL_OR_CONNECTION, 1)
  1352. == &ipv4_or_ap);
  1353. tt_assert(fascist_firewall_choose_address(&ipv4_dir_ap, &ipv6_dir_ap, 0,
  1354. FIREWALL_DIR_CONNECTION, 0)
  1355. == &ipv4_dir_ap);
  1356. tt_assert(fascist_firewall_choose_address(&ipv4_dir_ap, &ipv6_dir_ap, 0,
  1357. FIREWALL_DIR_CONNECTION, 1)
  1358. == &ipv4_dir_ap);
  1359. /* Choose an address with IPv6 on */
  1360. memset(&mock_options, 0, sizeof(or_options_t));
  1361. mock_options.ClientUseIPv4 = 0;
  1362. mock_options.ClientUseIPv6 = 1;
  1363. mock_options.UseBridges = 0;
  1364. tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &ipv6_or_ap, 0,
  1365. FIREWALL_OR_CONNECTION, 0)
  1366. == &ipv6_or_ap);
  1367. tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &ipv6_or_ap, 0,
  1368. FIREWALL_OR_CONNECTION, 1)
  1369. == &ipv6_or_ap);
  1370. tt_assert(fascist_firewall_choose_address(&ipv4_dir_ap, &ipv6_dir_ap, 0,
  1371. FIREWALL_DIR_CONNECTION, 0)
  1372. == &ipv6_dir_ap);
  1373. tt_assert(fascist_firewall_choose_address(&ipv4_dir_ap, &ipv6_dir_ap, 0,
  1374. FIREWALL_DIR_CONNECTION, 1)
  1375. == &ipv6_dir_ap);
  1376. /* Choose an address with ClientUseIPv4 0.
  1377. * This means "use IPv6" regardless of the other settings. */
  1378. memset(&mock_options, 0, sizeof(or_options_t));
  1379. mock_options.ClientUseIPv4 = 0;
  1380. mock_options.ClientUseIPv6 = 0;
  1381. mock_options.UseBridges = 0;
  1382. tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &ipv6_or_ap, 0,
  1383. FIREWALL_OR_CONNECTION, 0)
  1384. == &ipv6_or_ap);
  1385. tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &ipv6_or_ap, 0,
  1386. FIREWALL_OR_CONNECTION, 1)
  1387. == &ipv6_or_ap);
  1388. tt_assert(fascist_firewall_choose_address(&ipv4_dir_ap, &ipv6_dir_ap, 0,
  1389. FIREWALL_DIR_CONNECTION, 0)
  1390. == &ipv6_dir_ap);
  1391. tt_assert(fascist_firewall_choose_address(&ipv4_dir_ap, &ipv6_dir_ap, 0,
  1392. FIREWALL_DIR_CONNECTION, 1)
  1393. == &ipv6_dir_ap);
  1394. /* Choose from unusual inputs */
  1395. memset(&mock_options, 0, sizeof(or_options_t));
  1396. mock_options.ClientUseIPv4 = 1;
  1397. mock_options.ClientUseIPv6 = 1;
  1398. mock_options.UseBridges = 1;
  1399. tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &n_ipv6_ap, 0,
  1400. FIREWALL_OR_CONNECTION, 0)
  1401. == &ipv4_or_ap);
  1402. tt_assert(fascist_firewall_choose_address(&n_ipv4_ap, &ipv6_or_ap, 0,
  1403. FIREWALL_OR_CONNECTION, 0)
  1404. == &ipv6_or_ap);
  1405. tt_assert(fascist_firewall_choose_address(&n_ipv4_ap, &n_ipv6_ap, 0,
  1406. FIREWALL_OR_CONNECTION, 0)
  1407. == NULL);
  1408. tt_assert(fascist_firewall_choose_address(&ipv4_dir_ap, &n_ipv6_ap, 0,
  1409. FIREWALL_DIR_CONNECTION, 0)
  1410. == &ipv4_dir_ap);
  1411. tt_assert(fascist_firewall_choose_address(&n_ipv4_ap, &ipv6_dir_ap, 0,
  1412. FIREWALL_DIR_CONNECTION, 0)
  1413. == &ipv6_dir_ap);
  1414. tt_assert(fascist_firewall_choose_address(&n_ipv4_ap, &n_ipv6_ap, 0,
  1415. FIREWALL_DIR_CONNECTION, 0)
  1416. == NULL);
  1417. done:
  1418. UNMOCK(get_options);
  1419. }
  1420. #undef TEST_IPV4_ADDR_STR
  1421. #undef TEST_IPV6_ADDR_STR
  1422. #undef TEST_IPV4_OR_PORT
  1423. #undef TEST_IPV4_DIR_PORT
  1424. #undef TEST_IPV6_OR_PORT
  1425. #undef TEST_IPV6_DIR_PORT
  1426. struct testcase_t policy_tests[] = {
  1427. { "router_dump_exit_policy_to_string", test_dump_exit_policy_to_string, 0,
  1428. NULL, NULL },
  1429. { "general", test_policies_general, 0, NULL, NULL },
  1430. { "getinfo_helper_policies", test_policies_getinfo_helper_policies, 0, NULL,
  1431. NULL },
  1432. { "reject_exit_address", test_policies_reject_exit_address, 0, NULL, NULL },
  1433. { "reject_interface_address", test_policies_reject_interface_address, 0,
  1434. NULL, NULL },
  1435. { "reject_port_address", test_policies_reject_port_address, 0, NULL, NULL },
  1436. { "fascist_firewall_allows_address",
  1437. test_policies_fascist_firewall_allows_address, 0, NULL, NULL },
  1438. { "fascist_firewall_choose_address",
  1439. test_policies_fascist_firewall_choose_address, 0, NULL, NULL },
  1440. END_OF_TESTCASES
  1441. };