test_policy.c 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883
  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, 1));
  227. tt_assert(policy_is_reject_star(policy, AF_INET, 1));
  228. tt_assert(policy_is_reject_star(policy10, AF_INET, 1));
  229. tt_assert(!policy_is_reject_star(policy10, AF_INET6, 1));
  230. tt_assert(policy_is_reject_star(policy11, AF_INET, 1));
  231. tt_assert(policy_is_reject_star(policy11, AF_INET6, 1));
  232. tt_assert(policy_is_reject_star(NULL, AF_INET, 1));
  233. tt_assert(policy_is_reject_star(NULL, AF_INET6, 1));
  234. tt_assert(!policy_is_reject_star(NULL, AF_INET, 0));
  235. tt_assert(!policy_is_reject_star(NULL, AF_INET6, 0));
  236. addr_policy_list_free(policy);
  237. policy = NULL;
  238. /* make sure assume_action works */
  239. malformed_list = 0;
  240. p = router_parse_addr_policy_item_from_string("127.0.0.1",
  241. ADDR_POLICY_ACCEPT,
  242. &malformed_list);
  243. tt_assert(p);
  244. addr_policy_free(p);
  245. tt_assert(!malformed_list);
  246. p = router_parse_addr_policy_item_from_string("127.0.0.1:*",
  247. ADDR_POLICY_ACCEPT,
  248. &malformed_list);
  249. tt_assert(p);
  250. addr_policy_free(p);
  251. tt_assert(!malformed_list);
  252. p = router_parse_addr_policy_item_from_string("[::]",
  253. ADDR_POLICY_ACCEPT,
  254. &malformed_list);
  255. tt_assert(p);
  256. addr_policy_free(p);
  257. tt_assert(!malformed_list);
  258. p = router_parse_addr_policy_item_from_string("[::]:*",
  259. ADDR_POLICY_ACCEPT,
  260. &malformed_list);
  261. tt_assert(p);
  262. addr_policy_free(p);
  263. tt_assert(!malformed_list);
  264. p = router_parse_addr_policy_item_from_string("[face::b]",
  265. ADDR_POLICY_ACCEPT,
  266. &malformed_list);
  267. tt_assert(p);
  268. addr_policy_free(p);
  269. tt_assert(!malformed_list);
  270. p = router_parse_addr_policy_item_from_string("[b::aaaa]",
  271. ADDR_POLICY_ACCEPT,
  272. &malformed_list);
  273. tt_assert(p);
  274. addr_policy_free(p);
  275. tt_assert(!malformed_list);
  276. p = router_parse_addr_policy_item_from_string("*",
  277. ADDR_POLICY_ACCEPT,
  278. &malformed_list);
  279. tt_assert(p);
  280. addr_policy_free(p);
  281. tt_assert(!malformed_list);
  282. p = router_parse_addr_policy_item_from_string("*4",
  283. ADDR_POLICY_ACCEPT,
  284. &malformed_list);
  285. tt_assert(p);
  286. addr_policy_free(p);
  287. tt_assert(!malformed_list);
  288. p = router_parse_addr_policy_item_from_string("*6",
  289. ADDR_POLICY_ACCEPT,
  290. &malformed_list);
  291. tt_assert(p);
  292. addr_policy_free(p);
  293. tt_assert(!malformed_list);
  294. /* These are all ambiguous IPv6 addresses, it's good that we reject them */
  295. p = router_parse_addr_policy_item_from_string("acce::abcd",
  296. ADDR_POLICY_ACCEPT,
  297. &malformed_list);
  298. tt_assert(!p);
  299. tt_assert(malformed_list);
  300. malformed_list = 0;
  301. p = router_parse_addr_policy_item_from_string("7:1234",
  302. ADDR_POLICY_ACCEPT,
  303. &malformed_list);
  304. tt_assert(!p);
  305. tt_assert(malformed_list);
  306. malformed_list = 0;
  307. p = router_parse_addr_policy_item_from_string("::",
  308. ADDR_POLICY_ACCEPT,
  309. &malformed_list);
  310. tt_assert(!p);
  311. tt_assert(malformed_list);
  312. malformed_list = 0;
  313. /* make sure compacting logic works. */
  314. policy = NULL;
  315. line.key = (char*)"foo";
  316. line.value = (char*)"accept *:80,reject private:*,reject *:*";
  317. line.next = NULL;
  318. tt_int_op(0, OP_EQ, policies_parse_exit_policy(&line,&policy,
  319. EXIT_POLICY_IPV6_ENABLED |
  320. EXIT_POLICY_ADD_DEFAULT, NULL));
  321. tt_assert(policy);
  322. //test_streq(policy->string, "accept *:80");
  323. //test_streq(policy->next->string, "reject *:*");
  324. tt_int_op(smartlist_len(policy),OP_EQ, 4);
  325. /* test policy summaries */
  326. /* check if we properly ignore private IP addresses */
  327. test_policy_summary_helper("reject 192.168.0.0/16:*,"
  328. "reject 0.0.0.0/8:*,"
  329. "reject 10.0.0.0/8:*,"
  330. "accept *:10-30,"
  331. "accept *:90,"
  332. "reject *:*",
  333. "accept 10-30,90");
  334. /* check all accept policies, and proper counting of rejects */
  335. test_policy_summary_helper("reject 11.0.0.0/9:80,"
  336. "reject 12.0.0.0/9:80,"
  337. "reject 13.0.0.0/9:80,"
  338. "reject 14.0.0.0/9:80,"
  339. "accept *:*", "accept 1-65535");
  340. test_policy_summary_helper("reject 11.0.0.0/9:80,"
  341. "reject 12.0.0.0/9:80,"
  342. "reject 13.0.0.0/9:80,"
  343. "reject 14.0.0.0/9:80,"
  344. "reject 15.0.0.0:81,"
  345. "accept *:*", "accept 1-65535");
  346. test_policy_summary_helper("reject 11.0.0.0/9:80,"
  347. "reject 12.0.0.0/9:80,"
  348. "reject 13.0.0.0/9:80,"
  349. "reject 14.0.0.0/9:80,"
  350. "reject 15.0.0.0:80,"
  351. "accept *:*",
  352. "reject 80");
  353. /* no exits */
  354. test_policy_summary_helper("accept 11.0.0.0/9:80,"
  355. "reject *:*",
  356. "reject 1-65535");
  357. /* port merging */
  358. test_policy_summary_helper("accept *:80,"
  359. "accept *:81,"
  360. "accept *:100-110,"
  361. "accept *:111,"
  362. "reject *:*",
  363. "accept 80-81,100-111");
  364. /* border ports */
  365. test_policy_summary_helper("accept *:1,"
  366. "accept *:3,"
  367. "accept *:65535,"
  368. "reject *:*",
  369. "accept 1,3,65535");
  370. /* holes */
  371. test_policy_summary_helper("accept *:1,"
  372. "accept *:3,"
  373. "accept *:5,"
  374. "accept *:7,"
  375. "reject *:*",
  376. "accept 1,3,5,7");
  377. test_policy_summary_helper("reject *:1,"
  378. "reject *:3,"
  379. "reject *:5,"
  380. "reject *:7,"
  381. "accept *:*",
  382. "reject 1,3,5,7");
  383. /* Short policies with unrecognized formats should get accepted. */
  384. test_short_policy_parse("accept fred,2,3-5", "accept 2,3-5");
  385. test_short_policy_parse("accept 2,fred,3", "accept 2,3");
  386. test_short_policy_parse("accept 2,fred,3,bob", "accept 2,3");
  387. test_short_policy_parse("accept 2,-3,500-600", "accept 2,500-600");
  388. /* Short policies with nil entries are accepted too. */
  389. test_short_policy_parse("accept 1,,3", "accept 1,3");
  390. test_short_policy_parse("accept 100-200,,", "accept 100-200");
  391. test_short_policy_parse("reject ,1-10,,,,30-40", "reject 1-10,30-40");
  392. /* Try parsing various broken short policies */
  393. #define TT_BAD_SHORT_POLICY(s) \
  394. do { \
  395. tt_ptr_op(NULL, OP_EQ, (short_parsed = parse_short_policy((s)))); \
  396. } while (0)
  397. TT_BAD_SHORT_POLICY("accept 200-199");
  398. TT_BAD_SHORT_POLICY("");
  399. TT_BAD_SHORT_POLICY("rejekt 1,2,3");
  400. TT_BAD_SHORT_POLICY("reject ");
  401. TT_BAD_SHORT_POLICY("reject");
  402. TT_BAD_SHORT_POLICY("rej");
  403. TT_BAD_SHORT_POLICY("accept 2,3,100000");
  404. TT_BAD_SHORT_POLICY("accept 2,3x,4");
  405. TT_BAD_SHORT_POLICY("accept 2,3x,4");
  406. TT_BAD_SHORT_POLICY("accept 2-");
  407. TT_BAD_SHORT_POLICY("accept 2-x");
  408. TT_BAD_SHORT_POLICY("accept 1-,3");
  409. TT_BAD_SHORT_POLICY("accept 1-,3");
  410. /* Make sure that IPv4 addresses are ignored in accept6/reject6 lines. */
  411. p = router_parse_addr_policy_item_from_string("accept6 1.2.3.4:*", -1,
  412. &malformed_list);
  413. tt_assert(p == NULL);
  414. tt_assert(!malformed_list);
  415. p = router_parse_addr_policy_item_from_string("reject6 2.4.6.0/24:*", -1,
  416. &malformed_list);
  417. tt_assert(p == NULL);
  418. tt_assert(!malformed_list);
  419. p = router_parse_addr_policy_item_from_string("accept6 *4:*", -1,
  420. &malformed_list);
  421. tt_assert(p == NULL);
  422. tt_assert(!malformed_list);
  423. /* Make sure malformed policies are detected as such. */
  424. p = router_parse_addr_policy_item_from_string("bad_token *4:*", -1,
  425. &malformed_list);
  426. tt_assert(p == NULL);
  427. tt_assert(malformed_list);
  428. p = router_parse_addr_policy_item_from_string("accept6 **:*", -1,
  429. &malformed_list);
  430. tt_assert(p == NULL);
  431. tt_assert(malformed_list);
  432. p = router_parse_addr_policy_item_from_string("accept */15:*", -1,
  433. &malformed_list);
  434. tt_assert(p == NULL);
  435. tt_assert(malformed_list);
  436. p = router_parse_addr_policy_item_from_string("reject6 */:*", -1,
  437. &malformed_list);
  438. tt_assert(p == NULL);
  439. tt_assert(malformed_list);
  440. p = router_parse_addr_policy_item_from_string("accept 127.0.0.1/33:*", -1,
  441. &malformed_list);
  442. tt_assert(p == NULL);
  443. tt_assert(malformed_list);
  444. p = router_parse_addr_policy_item_from_string("accept6 [::1]/129:*", -1,
  445. &malformed_list);
  446. tt_assert(p == NULL);
  447. tt_assert(malformed_list);
  448. p = router_parse_addr_policy_item_from_string("reject 8.8.8.8/-1:*", -1,
  449. &malformed_list);
  450. tt_assert(p == NULL);
  451. tt_assert(malformed_list);
  452. p = router_parse_addr_policy_item_from_string("reject 8.8.4.4:10-5", -1,
  453. &malformed_list);
  454. tt_assert(p == NULL);
  455. tt_assert(malformed_list);
  456. p = router_parse_addr_policy_item_from_string("reject 1.2.3.4:-1", -1,
  457. &malformed_list);
  458. tt_assert(p == NULL);
  459. tt_assert(malformed_list);
  460. /* Test a too-long policy. */
  461. {
  462. char *policy_strng = NULL;
  463. smartlist_t *chunks = smartlist_new();
  464. smartlist_add(chunks, tor_strdup("accept "));
  465. for (i=1; i<10000; ++i)
  466. smartlist_add_asprintf(chunks, "%d,", i);
  467. smartlist_add(chunks, tor_strdup("20000"));
  468. policy_strng = smartlist_join_strings(chunks, "", 0, NULL);
  469. SMARTLIST_FOREACH(chunks, char *, ch, tor_free(ch));
  470. smartlist_free(chunks);
  471. short_parsed = parse_short_policy(policy_strng);/* shouldn't be accepted */
  472. tor_free(policy_strng);
  473. tt_ptr_op(NULL, OP_EQ, short_parsed);
  474. }
  475. /* truncation ports */
  476. sm = smartlist_new();
  477. for (i=1; i<2000; i+=2) {
  478. char buf[POLICY_BUF_LEN];
  479. tor_snprintf(buf, sizeof(buf), "reject *:%d", i);
  480. smartlist_add(sm, tor_strdup(buf));
  481. }
  482. smartlist_add(sm, tor_strdup("accept *:*"));
  483. policy_str = smartlist_join_strings(sm, ",", 0, NULL);
  484. test_policy_summary_helper( policy_str,
  485. "accept 2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,"
  486. "46,48,50,52,54,56,58,60,62,64,66,68,70,72,74,76,78,80,82,84,86,88,90,"
  487. "92,94,96,98,100,102,104,106,108,110,112,114,116,118,120,122,124,126,128,"
  488. "130,132,134,136,138,140,142,144,146,148,150,152,154,156,158,160,162,164,"
  489. "166,168,170,172,174,176,178,180,182,184,186,188,190,192,194,196,198,200,"
  490. "202,204,206,208,210,212,214,216,218,220,222,224,226,228,230,232,234,236,"
  491. "238,240,242,244,246,248,250,252,254,256,258,260,262,264,266,268,270,272,"
  492. "274,276,278,280,282,284,286,288,290,292,294,296,298,300,302,304,306,308,"
  493. "310,312,314,316,318,320,322,324,326,328,330,332,334,336,338,340,342,344,"
  494. "346,348,350,352,354,356,358,360,362,364,366,368,370,372,374,376,378,380,"
  495. "382,384,386,388,390,392,394,396,398,400,402,404,406,408,410,412,414,416,"
  496. "418,420,422,424,426,428,430,432,434,436,438,440,442,444,446,448,450,452,"
  497. "454,456,458,460,462,464,466,468,470,472,474,476,478,480,482,484,486,488,"
  498. "490,492,494,496,498,500,502,504,506,508,510,512,514,516,518,520,522");
  499. done:
  500. addr_policy_list_free(policy);
  501. addr_policy_list_free(policy2);
  502. addr_policy_list_free(policy3);
  503. addr_policy_list_free(policy4);
  504. addr_policy_list_free(policy5);
  505. addr_policy_list_free(policy6);
  506. addr_policy_list_free(policy7);
  507. addr_policy_list_free(policy8);
  508. addr_policy_list_free(policy9);
  509. addr_policy_list_free(policy10);
  510. addr_policy_list_free(policy11);
  511. addr_policy_list_free(policy12);
  512. tor_free(policy_str);
  513. if (sm) {
  514. SMARTLIST_FOREACH(sm, char *, s, tor_free(s));
  515. smartlist_free(sm);
  516. }
  517. short_policy_free(short_parsed);
  518. }
  519. /** Helper: Check that policy_list contains address */
  520. static int
  521. test_policy_has_address_helper(const smartlist_t *policy_list,
  522. const tor_addr_t *addr)
  523. {
  524. int found = 0;
  525. tt_assert(policy_list);
  526. tt_assert(addr);
  527. SMARTLIST_FOREACH_BEGIN(policy_list, addr_policy_t*, p) {
  528. if (tor_addr_eq(&p->addr, addr)) {
  529. found = 1;
  530. }
  531. } SMARTLIST_FOREACH_END(p);
  532. return found;
  533. done:
  534. return 0;
  535. }
  536. #define TEST_IPV4_ADDR (0x01020304)
  537. #define TEST_IPV6_ADDR ("2002::abcd")
  538. /** Run unit tests for rejecting the configured addresses on this exit relay
  539. * using policies_parse_exit_policy_reject_private */
  540. static void
  541. test_policies_reject_exit_address(void *arg)
  542. {
  543. smartlist_t *policy = NULL;
  544. tor_addr_t ipv4_addr, ipv6_addr;
  545. smartlist_t *ipv4_list, *ipv6_list, *both_list, *dupl_list;
  546. (void)arg;
  547. tor_addr_from_ipv4h(&ipv4_addr, TEST_IPV4_ADDR);
  548. tor_addr_parse(&ipv6_addr, TEST_IPV6_ADDR);
  549. ipv4_list = smartlist_new();
  550. ipv6_list = smartlist_new();
  551. both_list = smartlist_new();
  552. dupl_list = smartlist_new();
  553. smartlist_add(ipv4_list, &ipv4_addr);
  554. smartlist_add(both_list, &ipv4_addr);
  555. smartlist_add(dupl_list, &ipv4_addr);
  556. smartlist_add(dupl_list, &ipv4_addr);
  557. smartlist_add(dupl_list, &ipv4_addr);
  558. smartlist_add(ipv6_list, &ipv6_addr);
  559. smartlist_add(both_list, &ipv6_addr);
  560. smartlist_add(dupl_list, &ipv6_addr);
  561. smartlist_add(dupl_list, &ipv6_addr);
  562. /* IPv4-Only Exits */
  563. /* test that IPv4 addresses are rejected on an IPv4-only exit */
  564. policies_parse_exit_policy_reject_private(&policy, 0, ipv4_list, 0, 0);
  565. tt_assert(policy);
  566. tt_assert(smartlist_len(policy) == 1);
  567. tt_assert(test_policy_has_address_helper(policy, &ipv4_addr));
  568. addr_policy_list_free(policy);
  569. policy = NULL;
  570. /* test that IPv6 addresses are NOT rejected on an IPv4-only exit
  571. * (all IPv6 addresses are rejected by policies_parse_exit_policy_internal
  572. * on IPv4-only exits, so policies_parse_exit_policy_reject_private doesn't
  573. * need to do anything) */
  574. policies_parse_exit_policy_reject_private(&policy, 0, ipv6_list, 0, 0);
  575. tt_assert(policy == NULL);
  576. /* test that only IPv4 addresses are rejected on an IPv4-only exit */
  577. policies_parse_exit_policy_reject_private(&policy, 0, both_list, 0, 0);
  578. tt_assert(policy);
  579. tt_assert(smartlist_len(policy) == 1);
  580. tt_assert(test_policy_has_address_helper(policy, &ipv4_addr));
  581. addr_policy_list_free(policy);
  582. policy = NULL;
  583. /* Test that lists with duplicate entries produce the same results */
  584. policies_parse_exit_policy_reject_private(&policy, 0, dupl_list, 0, 0);
  585. tt_assert(policy);
  586. tt_assert(smartlist_len(policy) == 1);
  587. tt_assert(test_policy_has_address_helper(policy, &ipv4_addr));
  588. addr_policy_list_free(policy);
  589. policy = NULL;
  590. /* IPv4/IPv6 Exits */
  591. /* test that IPv4 addresses are rejected on an IPv4/IPv6 exit */
  592. policies_parse_exit_policy_reject_private(&policy, 1, ipv4_list, 0, 0);
  593. tt_assert(policy);
  594. tt_assert(smartlist_len(policy) == 1);
  595. tt_assert(test_policy_has_address_helper(policy, &ipv4_addr));
  596. addr_policy_list_free(policy);
  597. policy = NULL;
  598. /* test that IPv6 addresses are rejected on an IPv4/IPv6 exit */
  599. policies_parse_exit_policy_reject_private(&policy, 1, ipv6_list, 0, 0);
  600. tt_assert(policy);
  601. tt_assert(smartlist_len(policy) == 1);
  602. tt_assert(test_policy_has_address_helper(policy, &ipv6_addr));
  603. addr_policy_list_free(policy);
  604. policy = NULL;
  605. /* test that IPv4 and IPv6 addresses are rejected on an IPv4/IPv6 exit */
  606. policies_parse_exit_policy_reject_private(&policy, 1, both_list, 0, 0);
  607. tt_assert(policy);
  608. tt_assert(smartlist_len(policy) == 2);
  609. tt_assert(test_policy_has_address_helper(policy, &ipv4_addr));
  610. tt_assert(test_policy_has_address_helper(policy, &ipv6_addr));
  611. addr_policy_list_free(policy);
  612. policy = NULL;
  613. /* Test that lists with duplicate entries produce the same results */
  614. policies_parse_exit_policy_reject_private(&policy, 1, dupl_list, 0, 0);
  615. tt_assert(policy);
  616. tt_assert(smartlist_len(policy) == 2);
  617. tt_assert(test_policy_has_address_helper(policy, &ipv4_addr));
  618. tt_assert(test_policy_has_address_helper(policy, &ipv6_addr));
  619. addr_policy_list_free(policy);
  620. policy = NULL;
  621. done:
  622. addr_policy_list_free(policy);
  623. smartlist_free(ipv4_list);
  624. smartlist_free(ipv6_list);
  625. smartlist_free(both_list);
  626. smartlist_free(dupl_list);
  627. }
  628. static smartlist_t *test_configured_ports = NULL;
  629. /** Returns test_configured_ports */
  630. static 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. static smartlist_t *mock_ipv4_addrs = NULL;
  682. static 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 = tor_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. memset(&mock_my_routerinfo, 0, sizeof(mock_my_routerinfo));
  868. rv = getinfo_helper_policies(NULL, "exit-policy/default", &answer, &errmsg);
  869. tt_assert(rv == 0);
  870. tt_assert(answer != NULL);
  871. tt_assert(strlen(answer) > 0);
  872. tor_free(answer);
  873. rv = getinfo_helper_policies(NULL, "exit-policy/reject-private/default",
  874. &answer, &errmsg);
  875. tt_assert(rv == 0);
  876. tt_assert(answer != NULL);
  877. tt_assert(strlen(answer) > 0);
  878. tor_free(answer);
  879. memset(&mock_my_routerinfo, 0, sizeof(routerinfo_t));
  880. MOCK(router_get_my_routerinfo, mock_router_get_my_routerinfo);
  881. mock_my_routerinfo.exit_policy = smartlist_new();
  882. mock_desc_routerinfo = &mock_my_routerinfo;
  883. memset(&mock_options, 0, sizeof(or_options_t));
  884. MOCK(get_options, mock_get_options);
  885. rv = getinfo_helper_policies(NULL, "exit-policy/reject-private/relay",
  886. &answer, &errmsg);
  887. tt_assert(rv == 0);
  888. tt_assert(answer != NULL);
  889. tt_assert(strlen(answer) == 0);
  890. tor_free(answer);
  891. rv = getinfo_helper_policies(NULL, "exit-policy/ipv4", &answer,
  892. &errmsg);
  893. tt_assert(rv == 0);
  894. tt_assert(answer != NULL);
  895. ipv4_len = strlen(answer);
  896. tt_assert(ipv4_len == 0 || ipv4_len == strlen(DEFAULT_POLICY_STRING));
  897. tt_assert(ipv4_len == 0 || !strcasecmp(answer, DEFAULT_POLICY_STRING));
  898. tor_free(answer);
  899. rv = getinfo_helper_policies(NULL, "exit-policy/ipv6", &answer,
  900. &errmsg);
  901. tt_assert(rv == 0);
  902. tt_assert(answer != NULL);
  903. ipv6_len = strlen(answer);
  904. tt_assert(ipv6_len == 0 || ipv6_len == strlen(DEFAULT_POLICY_STRING));
  905. tt_assert(ipv6_len == 0 || !strcasecmp(answer, DEFAULT_POLICY_STRING));
  906. tor_free(answer);
  907. rv = getinfo_helper_policies(NULL, "exit-policy/full", &answer,
  908. &errmsg);
  909. tt_assert(rv == 0);
  910. tt_assert(answer != NULL);
  911. /* It's either empty or it's the default */
  912. tt_assert(strlen(answer) == 0 || !strcasecmp(answer, DEFAULT_POLICY_STRING));
  913. tor_free(answer);
  914. mock_my_routerinfo.addr = TEST_IPV4_ADDR;
  915. tor_addr_parse(&mock_my_routerinfo.ipv6_addr, TEST_IPV6_ADDR);
  916. append_exit_policy_string(&mock_my_routerinfo.exit_policy, "accept *4:*");
  917. append_exit_policy_string(&mock_my_routerinfo.exit_policy, "reject *6:*");
  918. mock_options.IPv6Exit = 1;
  919. tor_addr_from_ipv4h(&mock_options.OutboundBindAddressIPv4_, TEST_IPV4_ADDR);
  920. tor_addr_parse(&mock_options.OutboundBindAddressIPv6_, TEST_IPV6_ADDR);
  921. mock_options.ExitPolicyRejectPrivate = 1;
  922. mock_options.ExitPolicyRejectLocalInterfaces = 1;
  923. rv = getinfo_helper_policies(NULL, "exit-policy/reject-private/relay",
  924. &answer, &errmsg);
  925. tt_assert(rv == 0);
  926. tt_assert(answer != NULL);
  927. tt_assert(strlen(answer) > 0);
  928. tor_free(answer);
  929. mock_options.ExitPolicyRejectPrivate = 1;
  930. mock_options.ExitPolicyRejectLocalInterfaces = 0;
  931. rv = getinfo_helper_policies(NULL, "exit-policy/reject-private/relay",
  932. &answer, &errmsg);
  933. tt_assert(rv == 0);
  934. tt_assert(answer != NULL);
  935. tt_assert(strlen(answer) > 0);
  936. tor_free(answer);
  937. mock_options.ExitPolicyRejectPrivate = 0;
  938. mock_options.ExitPolicyRejectLocalInterfaces = 1;
  939. rv = getinfo_helper_policies(NULL, "exit-policy/reject-private/relay",
  940. &answer, &errmsg);
  941. tt_assert(rv == 0);
  942. tt_assert(answer != NULL);
  943. tt_assert(strlen(answer) > 0);
  944. tor_free(answer);
  945. mock_options.ExitPolicyRejectPrivate = 0;
  946. mock_options.ExitPolicyRejectLocalInterfaces = 0;
  947. rv = getinfo_helper_policies(NULL, "exit-policy/reject-private/relay",
  948. &answer, &errmsg);
  949. tt_assert(rv == 0);
  950. tt_assert(answer != NULL);
  951. tt_assert(strlen(answer) == 0);
  952. tor_free(answer);
  953. rv = getinfo_helper_policies(NULL, "exit-policy/ipv4", &answer,
  954. &errmsg);
  955. tt_assert(rv == 0);
  956. tt_assert(answer != NULL);
  957. ipv4_len = strlen(answer);
  958. tt_assert(ipv4_len > 0);
  959. tor_free(answer);
  960. rv = getinfo_helper_policies(NULL, "exit-policy/ipv6", &answer,
  961. &errmsg);
  962. tt_assert(rv == 0);
  963. tt_assert(answer != NULL);
  964. ipv6_len = strlen(answer);
  965. tt_assert(ipv6_len > 0);
  966. tor_free(answer);
  967. rv = getinfo_helper_policies(NULL, "exit-policy/full", &answer,
  968. &errmsg);
  969. tt_assert(rv == 0);
  970. tt_assert(answer != NULL);
  971. tt_assert(strlen(answer) > 0);
  972. tt_assert(strlen(answer) == ipv4_len + ipv6_len + 1);
  973. tor_free(answer);
  974. done:
  975. tor_free(answer);
  976. UNMOCK(get_options);
  977. UNMOCK(router_get_my_routerinfo);
  978. addr_policy_list_free(mock_my_routerinfo.exit_policy);
  979. }
  980. #undef DEFAULT_POLICY_STRING
  981. #undef TEST_IPV4_ADDR
  982. #undef TEST_IPV6_ADDR
  983. #define TEST_IPV4_ADDR_STR "1.2.3.4"
  984. #define TEST_IPV6_ADDR_STR "[1002::4567]"
  985. #define REJECT_IPv4_FINAL_STR "reject 0.0.0.0/0:*"
  986. #define REJECT_IPv6_FINAL_STR "reject [::]/0:*"
  987. #define OTHER_IPV4_ADDR_STR "6.7.8.9"
  988. #define OTHER_IPV6_ADDR_STR "[afff::]"
  989. /** Run unit tests for fascist_firewall_allows_address */
  990. static void
  991. test_policies_fascist_firewall_allows_address(void *arg)
  992. {
  993. (void)arg;
  994. tor_addr_t ipv4_addr, ipv6_addr, r_ipv4_addr, r_ipv6_addr;
  995. tor_addr_t n_ipv4_addr, n_ipv6_addr;
  996. const uint16_t port = 1234;
  997. smartlist_t *policy = NULL;
  998. smartlist_t *e_policy = NULL;
  999. addr_policy_t *item = NULL;
  1000. int malformed_list = 0;
  1001. /* Setup the options and the items in the policies */
  1002. memset(&mock_options, 0, sizeof(or_options_t));
  1003. MOCK(get_options, mock_get_options);
  1004. policy = smartlist_new();
  1005. item = router_parse_addr_policy_item_from_string("accept "
  1006. TEST_IPV4_ADDR_STR ":*",
  1007. ADDR_POLICY_ACCEPT,
  1008. &malformed_list);
  1009. tt_assert(item);
  1010. tt_assert(!malformed_list);
  1011. smartlist_add(policy, item);
  1012. item = router_parse_addr_policy_item_from_string("accept "
  1013. TEST_IPV6_ADDR_STR,
  1014. ADDR_POLICY_ACCEPT,
  1015. &malformed_list);
  1016. tt_assert(item);
  1017. tt_assert(!malformed_list);
  1018. smartlist_add(policy, item);
  1019. /* Normally, policy_expand_unspec would do this for us */
  1020. item = router_parse_addr_policy_item_from_string(REJECT_IPv4_FINAL_STR,
  1021. ADDR_POLICY_ACCEPT,
  1022. &malformed_list);
  1023. tt_assert(item);
  1024. tt_assert(!malformed_list);
  1025. smartlist_add(policy, item);
  1026. item = router_parse_addr_policy_item_from_string(REJECT_IPv6_FINAL_STR,
  1027. ADDR_POLICY_ACCEPT,
  1028. &malformed_list);
  1029. tt_assert(item);
  1030. tt_assert(!malformed_list);
  1031. smartlist_add(policy, item);
  1032. item = NULL;
  1033. e_policy = smartlist_new();
  1034. /*
  1035. char *polstr = policy_dump_to_string(policy, 1, 1);
  1036. printf("%s\n", polstr);
  1037. tor_free(polstr);
  1038. */
  1039. /* Parse the addresses */
  1040. tor_addr_parse(&ipv4_addr, TEST_IPV4_ADDR_STR);
  1041. tor_addr_parse(&ipv6_addr, TEST_IPV6_ADDR_STR);
  1042. tor_addr_parse(&r_ipv4_addr, OTHER_IPV4_ADDR_STR);
  1043. tor_addr_parse(&r_ipv6_addr, OTHER_IPV6_ADDR_STR);
  1044. tor_addr_make_null(&n_ipv4_addr, AF_INET);
  1045. tor_addr_make_null(&n_ipv6_addr, AF_INET6);
  1046. /* Test the function's address matching with IPv4 and IPv6 on */
  1047. memset(&mock_options, 0, sizeof(or_options_t));
  1048. mock_options.ClientUseIPv4 = 1;
  1049. mock_options.ClientUseIPv6 = 1;
  1050. mock_options.UseBridges = 0;
  1051. tt_assert(fascist_firewall_allows_address(&ipv4_addr, port, policy, 0, 0)
  1052. == 1);
  1053. tt_assert(fascist_firewall_allows_address(&ipv6_addr, port, policy, 0, 0)
  1054. == 1);
  1055. tt_assert(fascist_firewall_allows_address(&r_ipv4_addr, port, policy, 0, 0)
  1056. == 0);
  1057. tt_assert(fascist_firewall_allows_address(&r_ipv6_addr, port, policy, 0, 0)
  1058. == 0);
  1059. /* Preferring IPv4 */
  1060. tt_assert(fascist_firewall_allows_address(&ipv4_addr, port, policy, 1, 0)
  1061. == 1);
  1062. tt_assert(fascist_firewall_allows_address(&ipv6_addr, port, policy, 1, 0)
  1063. == 0);
  1064. tt_assert(fascist_firewall_allows_address(&r_ipv4_addr, port, policy, 1, 0)
  1065. == 0);
  1066. tt_assert(fascist_firewall_allows_address(&r_ipv6_addr, port, policy, 1, 0)
  1067. == 0);
  1068. /* Preferring IPv6 */
  1069. tt_assert(fascist_firewall_allows_address(&ipv4_addr, port, policy, 1, 1)
  1070. == 0);
  1071. tt_assert(fascist_firewall_allows_address(&ipv6_addr, port, policy, 1, 1)
  1072. == 1);
  1073. tt_assert(fascist_firewall_allows_address(&r_ipv4_addr, port, policy, 1, 1)
  1074. == 0);
  1075. tt_assert(fascist_firewall_allows_address(&r_ipv6_addr, port, policy, 1, 1)
  1076. == 0);
  1077. /* Test the function's address matching with UseBridges on */
  1078. memset(&mock_options, 0, sizeof(or_options_t));
  1079. mock_options.ClientUseIPv4 = 1;
  1080. mock_options.ClientUseIPv6 = 1;
  1081. mock_options.UseBridges = 1;
  1082. tt_assert(fascist_firewall_allows_address(&ipv4_addr, port, policy, 0, 0)
  1083. == 1);
  1084. tt_assert(fascist_firewall_allows_address(&ipv6_addr, port, policy, 0, 0)
  1085. == 1);
  1086. tt_assert(fascist_firewall_allows_address(&r_ipv4_addr, port, policy, 0, 0)
  1087. == 0);
  1088. tt_assert(fascist_firewall_allows_address(&r_ipv6_addr, port, policy, 0, 0)
  1089. == 0);
  1090. /* Preferring IPv4 */
  1091. tt_assert(fascist_firewall_allows_address(&ipv4_addr, port, policy, 1, 0)
  1092. == 1);
  1093. tt_assert(fascist_firewall_allows_address(&ipv6_addr, port, policy, 1, 0)
  1094. == 0);
  1095. tt_assert(fascist_firewall_allows_address(&r_ipv4_addr, port, policy, 1, 0)
  1096. == 0);
  1097. tt_assert(fascist_firewall_allows_address(&r_ipv6_addr, port, policy, 1, 0)
  1098. == 0);
  1099. /* Preferring IPv6 */
  1100. tt_assert(fascist_firewall_allows_address(&ipv4_addr, port, policy, 1, 1)
  1101. == 0);
  1102. tt_assert(fascist_firewall_allows_address(&ipv6_addr, port, policy, 1, 1)
  1103. == 1);
  1104. tt_assert(fascist_firewall_allows_address(&r_ipv4_addr, port, policy, 1, 1)
  1105. == 0);
  1106. tt_assert(fascist_firewall_allows_address(&r_ipv6_addr, port, policy, 1, 1)
  1107. == 0);
  1108. /* bridge clients always use IPv6, regardless of ClientUseIPv6 */
  1109. mock_options.ClientUseIPv4 = 1;
  1110. mock_options.ClientUseIPv6 = 0;
  1111. tt_assert(fascist_firewall_allows_address(&ipv4_addr, port, policy, 0, 0)
  1112. == 1);
  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 IPv4 on */
  1120. memset(&mock_options, 0, sizeof(or_options_t));
  1121. mock_options.ClientUseIPv4 = 1;
  1122. mock_options.ClientUseIPv6 = 0;
  1123. mock_options.UseBridges = 0;
  1124. tt_assert(fascist_firewall_allows_address(&ipv4_addr, port, policy, 0, 0)
  1125. == 1);
  1126. tt_assert(fascist_firewall_allows_address(&ipv6_addr, port, policy, 0, 0)
  1127. == 0);
  1128. tt_assert(fascist_firewall_allows_address(&r_ipv4_addr, port, policy, 0, 0)
  1129. == 0);
  1130. tt_assert(fascist_firewall_allows_address(&r_ipv6_addr, port, policy, 0, 0)
  1131. == 0);
  1132. /* Test the function's address matching with IPv6 on */
  1133. memset(&mock_options, 0, sizeof(or_options_t));
  1134. mock_options.ClientUseIPv4 = 0;
  1135. mock_options.ClientUseIPv6 = 1;
  1136. mock_options.UseBridges = 0;
  1137. tt_assert(fascist_firewall_allows_address(&ipv4_addr, port, policy, 0, 0)
  1138. == 0);
  1139. tt_assert(fascist_firewall_allows_address(&ipv6_addr, port, policy, 0, 0)
  1140. == 1);
  1141. tt_assert(fascist_firewall_allows_address(&r_ipv4_addr, port, policy, 0, 0)
  1142. == 0);
  1143. tt_assert(fascist_firewall_allows_address(&r_ipv6_addr, port, policy, 0, 0)
  1144. == 0);
  1145. /* Test the function's address matching with ClientUseIPv4 0.
  1146. * This means "use IPv6" regardless of the other settings. */
  1147. memset(&mock_options, 0, sizeof(or_options_t));
  1148. mock_options.ClientUseIPv4 = 0;
  1149. mock_options.ClientUseIPv6 = 0;
  1150. mock_options.UseBridges = 0;
  1151. tt_assert(fascist_firewall_allows_address(&ipv4_addr, port, policy, 0, 0)
  1152. == 0);
  1153. tt_assert(fascist_firewall_allows_address(&ipv6_addr, port, policy, 0, 0)
  1154. == 1);
  1155. tt_assert(fascist_firewall_allows_address(&r_ipv4_addr, port, policy, 0, 0)
  1156. == 0);
  1157. tt_assert(fascist_firewall_allows_address(&r_ipv6_addr, port, policy, 0, 0)
  1158. == 0);
  1159. /* Test the function's address matching for unusual inputs */
  1160. memset(&mock_options, 0, sizeof(or_options_t));
  1161. mock_options.ClientUseIPv4 = 1;
  1162. mock_options.ClientUseIPv6 = 1;
  1163. mock_options.UseBridges = 1;
  1164. /* NULL and tor_addr_is_null addresses are rejected */
  1165. tt_assert(fascist_firewall_allows_address(NULL, port, policy, 0, 0) == 0);
  1166. tt_assert(fascist_firewall_allows_address(&n_ipv4_addr, port, policy, 0, 0)
  1167. == 0);
  1168. tt_assert(fascist_firewall_allows_address(&n_ipv6_addr, port, policy, 0, 0)
  1169. == 0);
  1170. /* zero ports are rejected */
  1171. tt_assert(fascist_firewall_allows_address(&ipv4_addr, 0, policy, 0, 0)
  1172. == 0);
  1173. tt_assert(fascist_firewall_allows_address(&ipv6_addr, 0, policy, 0, 0)
  1174. == 0);
  1175. /* NULL and empty policies accept everything */
  1176. tt_assert(fascist_firewall_allows_address(&ipv4_addr, port, NULL, 0, 0)
  1177. == 1);
  1178. tt_assert(fascist_firewall_allows_address(&ipv6_addr, port, NULL, 0, 0)
  1179. == 1);
  1180. tt_assert(fascist_firewall_allows_address(&ipv4_addr, port, e_policy, 0, 0)
  1181. == 1);
  1182. tt_assert(fascist_firewall_allows_address(&ipv6_addr, port, e_policy, 0, 0)
  1183. == 1);
  1184. done:
  1185. addr_policy_free(item);
  1186. addr_policy_list_free(policy);
  1187. addr_policy_list_free(e_policy);
  1188. UNMOCK(get_options);
  1189. }
  1190. #undef REJECT_IPv4_FINAL_STR
  1191. #undef REJECT_IPv6_FINAL_STR
  1192. #undef OTHER_IPV4_ADDR_STR
  1193. #undef OTHER_IPV6_ADDR_STR
  1194. #define TEST_IPV4_OR_PORT 1234
  1195. #define TEST_IPV4_DIR_PORT 2345
  1196. #define TEST_IPV6_OR_PORT 61234
  1197. #define TEST_IPV6_DIR_PORT 62345
  1198. /* Check that fascist_firewall_choose_address_rs() returns the expected
  1199. * results. */
  1200. #define CHECK_CHOSEN_ADDR_RS(fake_rs, fw_connection, pref_only, expect_rv, \
  1201. expect_ap) \
  1202. STMT_BEGIN \
  1203. tor_addr_port_t chosen_rs_ap; \
  1204. tor_addr_make_null(&chosen_rs_ap.addr, AF_INET); \
  1205. chosen_rs_ap.port = 0; \
  1206. tt_int_op(fascist_firewall_choose_address_rs(&(fake_rs), \
  1207. (fw_connection), \
  1208. (pref_only), \
  1209. &chosen_rs_ap), \
  1210. OP_EQ, (expect_rv)); \
  1211. tt_assert(tor_addr_eq(&(expect_ap).addr, &chosen_rs_ap.addr)); \
  1212. tt_int_op((expect_ap).port, OP_EQ, chosen_rs_ap.port); \
  1213. STMT_END
  1214. /* Check that fascist_firewall_choose_address_node() returns the expected
  1215. * results. */
  1216. #define CHECK_CHOSEN_ADDR_NODE(fake_node, fw_connection, pref_only, \
  1217. expect_rv, expect_ap) \
  1218. STMT_BEGIN \
  1219. tor_addr_port_t chosen_node_ap; \
  1220. tor_addr_make_null(&chosen_node_ap.addr, AF_INET); \
  1221. chosen_node_ap.port = 0; \
  1222. tt_int_op(fascist_firewall_choose_address_node(&(fake_node), \
  1223. (fw_connection), \
  1224. (pref_only), \
  1225. &chosen_node_ap), \
  1226. OP_EQ, (expect_rv)); \
  1227. tt_assert(tor_addr_eq(&(expect_ap).addr, &chosen_node_ap.addr)); \
  1228. tt_int_op((expect_ap).port, OP_EQ, chosen_node_ap.port); \
  1229. STMT_END
  1230. /* Check that fascist_firewall_choose_address_rs and
  1231. * fascist_firewall_choose_address_node() both return the expected results. */
  1232. #define CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, fw_connection, pref_only, \
  1233. expect_rv, expect_ap) \
  1234. STMT_BEGIN \
  1235. CHECK_CHOSEN_ADDR_RS(fake_rs, fw_connection, pref_only, expect_rv, \
  1236. expect_ap); \
  1237. CHECK_CHOSEN_ADDR_NODE(fake_node, fw_connection, pref_only, expect_rv, \
  1238. expect_ap); \
  1239. STMT_END
  1240. /** Run unit tests for fascist_firewall_choose_address */
  1241. static void
  1242. test_policies_fascist_firewall_choose_address(void *arg)
  1243. {
  1244. (void)arg;
  1245. tor_addr_port_t ipv4_or_ap, ipv4_dir_ap, ipv6_or_ap, ipv6_dir_ap;
  1246. tor_addr_port_t n_ipv4_ap, n_ipv6_ap;
  1247. /* Setup the options */
  1248. memset(&mock_options, 0, sizeof(or_options_t));
  1249. MOCK(get_options, mock_get_options);
  1250. /* Parse the addresses */
  1251. tor_addr_parse(&ipv4_or_ap.addr, TEST_IPV4_ADDR_STR);
  1252. ipv4_or_ap.port = TEST_IPV4_OR_PORT;
  1253. tor_addr_parse(&ipv4_dir_ap.addr, TEST_IPV4_ADDR_STR);
  1254. ipv4_dir_ap.port = TEST_IPV4_DIR_PORT;
  1255. tor_addr_parse(&ipv6_or_ap.addr, TEST_IPV6_ADDR_STR);
  1256. ipv6_or_ap.port = TEST_IPV6_OR_PORT;
  1257. tor_addr_parse(&ipv6_dir_ap.addr, TEST_IPV6_ADDR_STR);
  1258. ipv6_dir_ap.port = TEST_IPV6_DIR_PORT;
  1259. tor_addr_make_null(&n_ipv4_ap.addr, AF_INET);
  1260. n_ipv4_ap.port = 0;
  1261. tor_addr_make_null(&n_ipv6_ap.addr, AF_INET6);
  1262. n_ipv6_ap.port = 0;
  1263. /* Sanity check fascist_firewall_choose_address with IPv4 and IPv6 on */
  1264. memset(&mock_options, 0, sizeof(or_options_t));
  1265. mock_options.ClientUseIPv4 = 1;
  1266. mock_options.ClientUseIPv6 = 1;
  1267. mock_options.UseBridges = 0;
  1268. /* Prefer IPv4 */
  1269. tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &ipv6_or_ap, 1,
  1270. FIREWALL_OR_CONNECTION, 0, 0)
  1271. == &ipv4_or_ap);
  1272. tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &ipv6_or_ap, 1,
  1273. FIREWALL_OR_CONNECTION, 1, 0)
  1274. == &ipv4_or_ap);
  1275. tt_assert(fascist_firewall_choose_address(&ipv4_dir_ap, &ipv6_dir_ap, 1,
  1276. FIREWALL_DIR_CONNECTION, 0, 0)
  1277. == &ipv4_dir_ap);
  1278. tt_assert(fascist_firewall_choose_address(&ipv4_dir_ap, &ipv6_dir_ap, 1,
  1279. FIREWALL_DIR_CONNECTION, 1, 0)
  1280. == &ipv4_dir_ap);
  1281. /* Prefer IPv6 */
  1282. tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &ipv6_or_ap, 0,
  1283. FIREWALL_OR_CONNECTION, 0, 1)
  1284. == &ipv6_or_ap);
  1285. tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &ipv6_or_ap, 0,
  1286. FIREWALL_OR_CONNECTION, 1, 1)
  1287. == &ipv6_or_ap);
  1288. tt_assert(fascist_firewall_choose_address(&ipv4_dir_ap, &ipv6_dir_ap, 0,
  1289. FIREWALL_DIR_CONNECTION, 0, 1)
  1290. == &ipv6_dir_ap);
  1291. tt_assert(fascist_firewall_choose_address(&ipv4_dir_ap, &ipv6_dir_ap, 0,
  1292. FIREWALL_DIR_CONNECTION, 1, 1)
  1293. == &ipv6_dir_ap);
  1294. /* Unusual inputs */
  1295. /* null preferred OR addresses */
  1296. tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &n_ipv6_ap, 0,
  1297. FIREWALL_OR_CONNECTION, 0, 1)
  1298. == &ipv4_or_ap);
  1299. tt_assert(fascist_firewall_choose_address(&n_ipv4_ap, &ipv6_or_ap, 1,
  1300. FIREWALL_OR_CONNECTION, 0, 0)
  1301. == &ipv6_or_ap);
  1302. /* null both OR addresses */
  1303. tt_assert(fascist_firewall_choose_address(&n_ipv4_ap, &n_ipv6_ap, 0,
  1304. FIREWALL_OR_CONNECTION, 0, 1)
  1305. == NULL);
  1306. tt_assert(fascist_firewall_choose_address(&n_ipv4_ap, &n_ipv6_ap, 1,
  1307. FIREWALL_OR_CONNECTION, 0, 0)
  1308. == NULL);
  1309. /* null preferred Dir addresses */
  1310. tt_assert(fascist_firewall_choose_address(&ipv4_dir_ap, &n_ipv6_ap, 0,
  1311. FIREWALL_DIR_CONNECTION, 0, 1)
  1312. == &ipv4_dir_ap);
  1313. tt_assert(fascist_firewall_choose_address(&n_ipv4_ap, &ipv6_dir_ap, 1,
  1314. FIREWALL_DIR_CONNECTION, 0, 0)
  1315. == &ipv6_dir_ap);
  1316. /* null both Dir addresses */
  1317. tt_assert(fascist_firewall_choose_address(&n_ipv4_ap, &n_ipv6_ap, 0,
  1318. FIREWALL_DIR_CONNECTION, 0, 1)
  1319. == NULL);
  1320. tt_assert(fascist_firewall_choose_address(&n_ipv4_ap, &n_ipv6_ap, 1,
  1321. FIREWALL_DIR_CONNECTION, 0, 0)
  1322. == NULL);
  1323. /* Prefer IPv4 but want IPv6 (contradictory) */
  1324. tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &ipv6_or_ap, 0,
  1325. FIREWALL_OR_CONNECTION, 0, 0)
  1326. == &ipv4_or_ap);
  1327. tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &ipv6_or_ap, 0,
  1328. FIREWALL_OR_CONNECTION, 1, 0)
  1329. == &ipv4_or_ap);
  1330. /* Prefer IPv6 but want IPv4 (contradictory) */
  1331. tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &ipv6_or_ap, 1,
  1332. FIREWALL_OR_CONNECTION, 0, 1)
  1333. == &ipv6_or_ap);
  1334. tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &ipv6_or_ap, 1,
  1335. FIREWALL_OR_CONNECTION, 1, 1)
  1336. == &ipv6_or_ap);
  1337. /* Make a fake rs. There will be no corresponding node.
  1338. * This is what happens when there's no consensus and we're bootstrapping
  1339. * from authorities / fallbacks. */
  1340. routerstatus_t fake_rs;
  1341. memset(&fake_rs, 0, sizeof(routerstatus_t));
  1342. /* In a routerstatus, the OR and Dir addresses are the same */
  1343. fake_rs.addr = tor_addr_to_ipv4h(&ipv4_or_ap.addr);
  1344. fake_rs.or_port = ipv4_or_ap.port;
  1345. fake_rs.dir_port = ipv4_dir_ap.port;
  1346. tor_addr_copy(&fake_rs.ipv6_addr, &ipv6_or_ap.addr);
  1347. fake_rs.ipv6_orport = ipv6_or_ap.port;
  1348. /* In a routerstatus, the IPv4 and IPv6 DirPorts are the same.*/
  1349. ipv6_dir_ap.port = TEST_IPV4_DIR_PORT;
  1350. /* Make a fake node. Even though it contains the fake_rs, a lookup won't
  1351. * find the node from the rs, because they're not in the hash table. */
  1352. node_t fake_node;
  1353. memset(&fake_node, 0, sizeof(node_t));
  1354. fake_node.rs = &fake_rs;
  1355. /* Choose an address with IPv4 and IPv6 on */
  1356. memset(&mock_options, 0, sizeof(or_options_t));
  1357. mock_options.ClientUseIPv4 = 1;
  1358. mock_options.ClientUseIPv6 = 1;
  1359. mock_options.UseBridges = 0;
  1360. /* Preferring IPv4 */
  1361. mock_options.ClientPreferIPv6ORPort = 0;
  1362. mock_options.ClientPreferIPv6DirPort = 0;
  1363. /* Simulate the initialisation of fake_node.ipv6_preferred */
  1364. fake_node.ipv6_preferred = fascist_firewall_prefer_ipv6_orport(
  1365. &mock_options);
  1366. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 0, 1,
  1367. ipv4_or_ap);
  1368. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 1, 1,
  1369. ipv4_or_ap);
  1370. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
  1371. ipv4_dir_ap);
  1372. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
  1373. ipv4_dir_ap);
  1374. /* Auto (Preferring IPv4) */
  1375. mock_options.ClientPreferIPv6ORPort = -1;
  1376. mock_options.ClientPreferIPv6DirPort = -1;
  1377. /* Simulate the initialisation of fake_node.ipv6_preferred */
  1378. fake_node.ipv6_preferred = fascist_firewall_prefer_ipv6_orport(
  1379. &mock_options);
  1380. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 0, 1,
  1381. ipv4_or_ap);
  1382. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 1, 1,
  1383. ipv4_or_ap);
  1384. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
  1385. ipv4_dir_ap);
  1386. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
  1387. ipv4_dir_ap);
  1388. /* Preferring IPv6 */
  1389. mock_options.ClientPreferIPv6ORPort = 1;
  1390. mock_options.ClientPreferIPv6DirPort = 1;
  1391. /* Simulate the initialisation of fake_node.ipv6_preferred */
  1392. fake_node.ipv6_preferred = fascist_firewall_prefer_ipv6_orport(
  1393. &mock_options);
  1394. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 0, 1,
  1395. ipv6_or_ap);
  1396. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 1, 1,
  1397. ipv6_or_ap);
  1398. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
  1399. ipv6_dir_ap);
  1400. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
  1401. ipv6_dir_ap);
  1402. /* Preferring IPv4 OR / IPv6 Dir */
  1403. mock_options.ClientPreferIPv6ORPort = 0;
  1404. mock_options.ClientPreferIPv6DirPort = 1;
  1405. /* Simulate the initialisation of fake_node.ipv6_preferred */
  1406. fake_node.ipv6_preferred = fascist_firewall_prefer_ipv6_orport(
  1407. &mock_options);
  1408. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 0, 1,
  1409. ipv4_or_ap);
  1410. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 1, 1,
  1411. ipv4_or_ap);
  1412. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
  1413. ipv6_dir_ap);
  1414. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
  1415. ipv6_dir_ap);
  1416. /* Preferring IPv6 OR / IPv4 Dir */
  1417. mock_options.ClientPreferIPv6ORPort = 1;
  1418. mock_options.ClientPreferIPv6DirPort = 0;
  1419. /* Simulate the initialisation of fake_node.ipv6_preferred */
  1420. fake_node.ipv6_preferred = fascist_firewall_prefer_ipv6_orport(
  1421. &mock_options);
  1422. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 0, 1,
  1423. ipv6_or_ap);
  1424. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 1, 1,
  1425. ipv6_or_ap);
  1426. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
  1427. ipv4_dir_ap);
  1428. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
  1429. ipv4_dir_ap);
  1430. /* Choose an address with UseBridges on */
  1431. memset(&mock_options, 0, sizeof(or_options_t));
  1432. mock_options.UseBridges = 1;
  1433. mock_options.ClientUseIPv4 = 1;
  1434. mock_options.ClientUseIPv6 = 1;
  1435. /* Preferring IPv4 */
  1436. mock_options.ClientPreferIPv6ORPort = 0;
  1437. mock_options.ClientPreferIPv6DirPort = 0;
  1438. /* Simulate the initialisation of fake_node.ipv6_preferred */
  1439. fake_node.ipv6_preferred = fascist_firewall_prefer_ipv6_orport(
  1440. &mock_options);
  1441. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 0, 1,
  1442. ipv4_or_ap);
  1443. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 1, 1,
  1444. ipv4_or_ap);
  1445. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
  1446. ipv4_dir_ap);
  1447. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
  1448. ipv4_dir_ap);
  1449. /* Auto:
  1450. * - bridge clients prefer the configured bridge OR address from the node,
  1451. * (the configured address family sets node.ipv6_preferred)
  1452. * - other clients prefer IPv4 OR by default (see above),
  1453. * - all clients, including bridge clients, prefer IPv4 Dir by default.
  1454. */
  1455. mock_options.ClientPreferIPv6ORPort = -1;
  1456. mock_options.ClientPreferIPv6DirPort = -1;
  1457. /* Simulate the initialisation of fake_node.ipv6_preferred with a bridge
  1458. * configured with an IPv4 address */
  1459. fake_node.ipv6_preferred = 0;
  1460. CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_OR_CONNECTION, 0, 1, ipv4_or_ap);
  1461. CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_OR_CONNECTION, 1, 1, ipv4_or_ap);
  1462. CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
  1463. ipv4_dir_ap);
  1464. CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
  1465. ipv4_dir_ap);
  1466. /* Simulate the initialisation of fake_node.ipv6_preferred with a bridge
  1467. * configured with an IPv6 address */
  1468. fake_node.ipv6_preferred = 1;
  1469. CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_OR_CONNECTION, 0, 1, ipv6_or_ap);
  1470. CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_OR_CONNECTION, 1, 1, ipv6_or_ap);
  1471. CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
  1472. ipv4_dir_ap);
  1473. CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
  1474. ipv4_dir_ap);
  1475. /* When a rs has no node, it defaults to IPv4 under auto. */
  1476. CHECK_CHOSEN_ADDR_RS(fake_rs, FIREWALL_OR_CONNECTION, 0, 1, ipv4_or_ap);
  1477. CHECK_CHOSEN_ADDR_RS(fake_rs, FIREWALL_OR_CONNECTION, 1, 1, ipv4_or_ap);
  1478. CHECK_CHOSEN_ADDR_RS(fake_rs, FIREWALL_DIR_CONNECTION, 0, 1, ipv4_dir_ap);
  1479. CHECK_CHOSEN_ADDR_RS(fake_rs, FIREWALL_DIR_CONNECTION, 1, 1, ipv4_dir_ap);
  1480. /* Preferring IPv6 */
  1481. mock_options.ClientPreferIPv6ORPort = 1;
  1482. mock_options.ClientPreferIPv6DirPort = 1;
  1483. /* Simulate the initialisation of fake_node.ipv6_preferred */
  1484. fake_node.ipv6_preferred = fascist_firewall_prefer_ipv6_orport(
  1485. &mock_options);
  1486. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 0, 1,
  1487. ipv6_or_ap);
  1488. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 1, 1,
  1489. ipv6_or_ap);
  1490. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
  1491. ipv6_dir_ap);
  1492. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
  1493. ipv6_dir_ap);
  1494. /* In the default configuration (Auto / IPv6 off), bridge clients should
  1495. * use both IPv4 and IPv6, but only prefer IPv6 for bridges configured with
  1496. * an IPv6 address, regardless of ClientUseIPv6. (See above.) */
  1497. mock_options.ClientUseIPv6 = 0;
  1498. mock_options.ClientPreferIPv6ORPort = -1;
  1499. mock_options.ClientPreferIPv6DirPort = -1;
  1500. /* Simulate the initialisation of fake_node.ipv6_preferred with a bridge
  1501. * configured with an IPv4 address */
  1502. fake_node.ipv6_preferred = 0;
  1503. CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_OR_CONNECTION, 0, 1, ipv4_or_ap);
  1504. CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_OR_CONNECTION, 1, 1, ipv4_or_ap);
  1505. CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
  1506. ipv4_dir_ap);
  1507. CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
  1508. ipv4_dir_ap);
  1509. /* Simulate the initialisation of fake_node.ipv6_preferred with a bridge
  1510. * configured with an IPv6 address */
  1511. fake_node.ipv6_preferred = 1;
  1512. CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_OR_CONNECTION, 0, 1, ipv6_or_ap);
  1513. CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_OR_CONNECTION, 1, 1, ipv6_or_ap);
  1514. CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
  1515. ipv4_dir_ap);
  1516. CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
  1517. ipv4_dir_ap);
  1518. /* When a rs has no node, it defaults to IPv4 under auto. */
  1519. CHECK_CHOSEN_ADDR_RS(fake_rs, FIREWALL_OR_CONNECTION, 0, 1, ipv4_or_ap);
  1520. CHECK_CHOSEN_ADDR_RS(fake_rs, FIREWALL_OR_CONNECTION, 1, 1, ipv4_or_ap);
  1521. CHECK_CHOSEN_ADDR_RS(fake_rs, FIREWALL_DIR_CONNECTION, 0, 1, ipv4_dir_ap);
  1522. CHECK_CHOSEN_ADDR_RS(fake_rs, FIREWALL_DIR_CONNECTION, 1, 1, ipv4_dir_ap);
  1523. /* Choose an address with IPv4 on */
  1524. memset(&mock_options, 0, sizeof(or_options_t));
  1525. mock_options.ClientUseIPv4 = 1;
  1526. mock_options.ClientUseIPv6 = 0;
  1527. /* Simulate the initialisation of fake_node.ipv6_preferred */
  1528. fake_node.ipv6_preferred = fascist_firewall_prefer_ipv6_orport(
  1529. &mock_options);
  1530. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 0, 1,
  1531. ipv4_or_ap);
  1532. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 1, 1,
  1533. ipv4_or_ap);
  1534. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
  1535. ipv4_dir_ap);
  1536. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
  1537. ipv4_dir_ap);
  1538. /* Choose an address with IPv6 on */
  1539. memset(&mock_options, 0, sizeof(or_options_t));
  1540. mock_options.ClientUseIPv4 = 0;
  1541. mock_options.ClientUseIPv6 = 1;
  1542. /* Simulate the initialisation of fake_node.ipv6_preferred */
  1543. fake_node.ipv6_preferred = fascist_firewall_prefer_ipv6_orport(
  1544. &mock_options);
  1545. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 0, 1,
  1546. ipv6_or_ap);
  1547. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 1, 1,
  1548. ipv6_or_ap);
  1549. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
  1550. ipv6_dir_ap);
  1551. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
  1552. ipv6_dir_ap);
  1553. /* Choose an address with ClientUseIPv4 0.
  1554. * This means "use IPv6" regardless of the other settings. */
  1555. memset(&mock_options, 0, sizeof(or_options_t));
  1556. mock_options.ClientUseIPv4 = 0;
  1557. mock_options.ClientUseIPv6 = 0;
  1558. /* Simulate the initialisation of fake_node.ipv6_preferred */
  1559. fake_node.ipv6_preferred = fascist_firewall_prefer_ipv6_orport(
  1560. &mock_options);
  1561. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 0, 1,
  1562. ipv6_or_ap);
  1563. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 1, 1,
  1564. ipv6_or_ap);
  1565. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
  1566. ipv6_dir_ap);
  1567. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
  1568. ipv6_dir_ap);
  1569. /* Choose an address with ORPort_set 1 (server mode).
  1570. * This means "use IPv4" regardless of the other settings. */
  1571. memset(&mock_options, 0, sizeof(or_options_t));
  1572. mock_options.ORPort_set = 1;
  1573. mock_options.ClientUseIPv4 = 0;
  1574. mock_options.ClientUseIPv6 = 1;
  1575. mock_options.ClientPreferIPv6ORPort = 1;
  1576. mock_options.ClientPreferIPv6DirPort = 1;
  1577. /* Simulate the initialisation of fake_node.ipv6_preferred */
  1578. fake_node.ipv6_preferred = fascist_firewall_prefer_ipv6_orport(
  1579. &mock_options);
  1580. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 0, 1,
  1581. ipv4_or_ap);
  1582. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 1, 1,
  1583. ipv4_or_ap);
  1584. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
  1585. ipv4_dir_ap);
  1586. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
  1587. ipv4_dir_ap);
  1588. done:
  1589. UNMOCK(get_options);
  1590. }
  1591. #undef TEST_IPV4_ADDR_STR
  1592. #undef TEST_IPV6_ADDR_STR
  1593. #undef TEST_IPV4_OR_PORT
  1594. #undef TEST_IPV4_DIR_PORT
  1595. #undef TEST_IPV6_OR_PORT
  1596. #undef TEST_IPV6_DIR_PORT
  1597. #undef CHECK_CHOSEN_ADDR_RS
  1598. #undef CHECK_CHOSEN_ADDR_NODE
  1599. #undef CHECK_CHOSEN_ADDR_RN
  1600. struct testcase_t policy_tests[] = {
  1601. { "router_dump_exit_policy_to_string", test_dump_exit_policy_to_string, 0,
  1602. NULL, NULL },
  1603. { "general", test_policies_general, 0, NULL, NULL },
  1604. { "getinfo_helper_policies", test_policies_getinfo_helper_policies, 0, NULL,
  1605. NULL },
  1606. { "reject_exit_address", test_policies_reject_exit_address, 0, NULL, NULL },
  1607. { "reject_interface_address", test_policies_reject_interface_address, 0,
  1608. NULL, NULL },
  1609. { "reject_port_address", test_policies_reject_port_address, 0, NULL, NULL },
  1610. { "fascist_firewall_allows_address",
  1611. test_policies_fascist_firewall_allows_address, 0, NULL, NULL },
  1612. { "fascist_firewall_choose_address",
  1613. test_policies_fascist_firewall_choose_address, 0, NULL, NULL },
  1614. END_OF_TESTCASES
  1615. };