test_policy.c 70 KB

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