test_policy.c 71 KB

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