test_policy.c 70 KB

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