test_policy.c 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349
  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> with
  30. * <b>options</b>, and make sure that policies_summarize() produces the string
  31. * <b>expected_summary</b> from it when called with family. */
  32. static void
  33. test_policy_summary_helper_family_flags(const char *policy_str,
  34. const char *expected_summary,
  35. sa_family_t family,
  36. exit_policy_parser_cfg_t options)
  37. {
  38. config_line_t line;
  39. smartlist_t *policy = smartlist_new();
  40. char *summary = NULL;
  41. char *summary_after = NULL;
  42. int r;
  43. short_policy_t *short_policy = NULL;
  44. int success = 0;
  45. line.key = (char*)"foo";
  46. line.value = (char *)policy_str;
  47. line.next = NULL;
  48. r = policies_parse_exit_policy(&line, &policy,
  49. options, NULL);
  50. tt_int_op(r,OP_EQ, 0);
  51. summary = policy_summarize(policy, family);
  52. tt_assert(summary != NULL);
  53. tt_str_op(summary,OP_EQ, expected_summary);
  54. short_policy = parse_short_policy(summary);
  55. tt_assert(short_policy);
  56. summary_after = write_short_policy(short_policy);
  57. tt_str_op(summary,OP_EQ, summary_after);
  58. success = 1;
  59. done:
  60. /* If we don't print the flags on failure, it's very hard to diagnose bugs */
  61. if (!success)
  62. TT_DECLARE("CTXT", ("\n IPv%d\n Options: %x\n Policy: %s",
  63. family == AF_INET ? 4 : 6, options, policy_str));
  64. tor_free(summary_after);
  65. tor_free(summary);
  66. if (policy)
  67. addr_policy_list_free(policy);
  68. short_policy_free(short_policy);
  69. }
  70. /** Like test_policy_summary_helper_family_flags, but tries all the different
  71. * flag combinations */
  72. static void
  73. test_policy_summary_helper_family(const char *policy_str,
  74. const char *expected_summary,
  75. sa_family_t family)
  76. {
  77. for (exit_policy_parser_cfg_t opt = 0;
  78. opt <= EXIT_POLICY_OPTION_ALL;
  79. opt++) {
  80. if (family == AF_INET6 && !(opt & EXIT_POLICY_IPV6_ENABLED))
  81. /* Skip the test: IPv6 addresses need IPv6 enabled */
  82. continue;
  83. if (opt & EXIT_POLICY_REJECT_LOCAL_INTERFACES)
  84. /* Skip the test: local interfaces are machine-specific */
  85. continue;
  86. test_policy_summary_helper_family_flags(policy_str, expected_summary,
  87. family, opt);
  88. }
  89. }
  90. /** Like test_policy_summary_helper_family, but uses expected_summary for
  91. * both IPv4 and IPv6. */
  92. static void
  93. test_policy_summary_helper(const char *policy_str,
  94. const char *expected_summary)
  95. {
  96. test_policy_summary_helper_family(policy_str, expected_summary, AF_INET);
  97. test_policy_summary_helper_family(policy_str, expected_summary, AF_INET6);
  98. }
  99. /** Like test_policy_summary_helper_family, but uses expected_summary4 for
  100. * IPv4 and expected_summary6 for IPv6. */
  101. static void
  102. test_policy_summary_helper6(const char *policy_str,
  103. const char *expected_summary4,
  104. const char *expected_summary6)
  105. {
  106. test_policy_summary_helper_family(policy_str, expected_summary4, AF_INET);
  107. test_policy_summary_helper_family(policy_str, expected_summary6, AF_INET6);
  108. }
  109. /** Run unit tests for generating summary lines of exit policies */
  110. static void
  111. test_policies_general(void *arg)
  112. {
  113. int i;
  114. smartlist_t *policy = NULL, *policy2 = NULL, *policy3 = NULL,
  115. *policy4 = NULL, *policy5 = NULL, *policy6 = NULL,
  116. *policy7 = NULL, *policy8 = NULL, *policy9 = NULL,
  117. *policy10 = NULL, *policy11 = NULL, *policy12 = NULL;
  118. addr_policy_t *p;
  119. tor_addr_t tar, tar2;
  120. smartlist_t *addr_list = NULL;
  121. config_line_t line;
  122. smartlist_t *sm = NULL;
  123. char *policy_str = NULL;
  124. short_policy_t *short_parsed = NULL;
  125. int malformed_list = -1;
  126. (void)arg;
  127. policy = smartlist_new();
  128. p = router_parse_addr_policy_item_from_string("reject 192.168.0.0/16:*", -1,
  129. &malformed_list);
  130. tt_assert(p != NULL);
  131. tt_int_op(ADDR_POLICY_REJECT,OP_EQ, p->policy_type);
  132. tor_addr_from_ipv4h(&tar, 0xc0a80000u);
  133. tt_int_op(0,OP_EQ, tor_addr_compare(&p->addr, &tar, CMP_EXACT));
  134. tt_int_op(16,OP_EQ, p->maskbits);
  135. tt_int_op(1,OP_EQ, p->prt_min);
  136. tt_int_op(65535,OP_EQ, p->prt_max);
  137. smartlist_add(policy, p);
  138. tor_addr_from_ipv4h(&tar, 0x01020304u);
  139. tt_assert(ADDR_POLICY_ACCEPTED ==
  140. compare_tor_addr_to_addr_policy(&tar, 2, policy));
  141. tor_addr_make_unspec(&tar);
  142. tt_assert(ADDR_POLICY_PROBABLY_ACCEPTED ==
  143. compare_tor_addr_to_addr_policy(&tar, 2, policy));
  144. tor_addr_from_ipv4h(&tar, 0xc0a80102);
  145. tt_assert(ADDR_POLICY_REJECTED ==
  146. compare_tor_addr_to_addr_policy(&tar, 2, policy));
  147. tt_int_op(0, OP_EQ, policies_parse_exit_policy(NULL, &policy2,
  148. EXIT_POLICY_IPV6_ENABLED |
  149. EXIT_POLICY_REJECT_PRIVATE |
  150. EXIT_POLICY_ADD_DEFAULT, NULL));
  151. tt_assert(policy2);
  152. tor_addr_from_ipv4h(&tar, 0x0306090cu);
  153. tor_addr_parse(&tar2, "[2000::1234]");
  154. addr_list = smartlist_new();
  155. smartlist_add(addr_list, &tar);
  156. smartlist_add(addr_list, &tar2);
  157. tt_int_op(0, OP_EQ, policies_parse_exit_policy(NULL, &policy12,
  158. EXIT_POLICY_IPV6_ENABLED |
  159. EXIT_POLICY_REJECT_PRIVATE |
  160. EXIT_POLICY_ADD_DEFAULT,
  161. addr_list));
  162. smartlist_free(addr_list);
  163. addr_list = NULL;
  164. tt_assert(policy12);
  165. policy3 = smartlist_new();
  166. p = router_parse_addr_policy_item_from_string("reject *:*", -1,
  167. &malformed_list);
  168. tt_assert(p != NULL);
  169. smartlist_add(policy3, p);
  170. p = router_parse_addr_policy_item_from_string("accept *:*", -1,
  171. &malformed_list);
  172. tt_assert(p != NULL);
  173. smartlist_add(policy3, p);
  174. policy4 = smartlist_new();
  175. p = router_parse_addr_policy_item_from_string("accept *:443", -1,
  176. &malformed_list);
  177. tt_assert(p != NULL);
  178. smartlist_add(policy4, p);
  179. p = router_parse_addr_policy_item_from_string("accept *:443", -1,
  180. &malformed_list);
  181. tt_assert(p != NULL);
  182. smartlist_add(policy4, p);
  183. policy5 = smartlist_new();
  184. p = router_parse_addr_policy_item_from_string("reject 0.0.0.0/8:*", -1,
  185. &malformed_list);
  186. tt_assert(p != NULL);
  187. smartlist_add(policy5, p);
  188. p = router_parse_addr_policy_item_from_string("reject 169.254.0.0/16:*", -1,
  189. &malformed_list);
  190. tt_assert(p != NULL);
  191. smartlist_add(policy5, p);
  192. p = router_parse_addr_policy_item_from_string("reject 127.0.0.0/8:*", -1,
  193. &malformed_list);
  194. tt_assert(p != NULL);
  195. smartlist_add(policy5, p);
  196. p = router_parse_addr_policy_item_from_string("reject 192.168.0.0/16:*",
  197. -1, &malformed_list);
  198. tt_assert(p != NULL);
  199. smartlist_add(policy5, p);
  200. p = router_parse_addr_policy_item_from_string("reject 10.0.0.0/8:*", -1,
  201. &malformed_list);
  202. tt_assert(p != NULL);
  203. smartlist_add(policy5, p);
  204. p = router_parse_addr_policy_item_from_string("reject 172.16.0.0/12:*", -1,
  205. &malformed_list);
  206. tt_assert(p != NULL);
  207. smartlist_add(policy5, p);
  208. p = router_parse_addr_policy_item_from_string("reject 80.190.250.90:*", -1,
  209. &malformed_list);
  210. tt_assert(p != NULL);
  211. smartlist_add(policy5, p);
  212. p = router_parse_addr_policy_item_from_string("reject *:1-65534", -1,
  213. &malformed_list);
  214. tt_assert(p != NULL);
  215. smartlist_add(policy5, p);
  216. p = router_parse_addr_policy_item_from_string("reject *:65535", -1,
  217. &malformed_list);
  218. tt_assert(p != NULL);
  219. smartlist_add(policy5, p);
  220. p = router_parse_addr_policy_item_from_string("accept *:1-65535", -1,
  221. &malformed_list);
  222. tt_assert(p != NULL);
  223. smartlist_add(policy5, p);
  224. policy6 = smartlist_new();
  225. p = router_parse_addr_policy_item_from_string("accept 43.3.0.0/9:*", -1,
  226. &malformed_list);
  227. tt_assert(p != NULL);
  228. smartlist_add(policy6, p);
  229. policy7 = smartlist_new();
  230. p = router_parse_addr_policy_item_from_string("accept 0.0.0.0/8:*", -1,
  231. &malformed_list);
  232. tt_assert(p != NULL);
  233. smartlist_add(policy7, p);
  234. tt_int_op(0, OP_EQ, policies_parse_exit_policy(NULL, &policy8,
  235. EXIT_POLICY_IPV6_ENABLED |
  236. EXIT_POLICY_REJECT_PRIVATE |
  237. EXIT_POLICY_ADD_DEFAULT,
  238. NULL));
  239. tt_assert(policy8);
  240. tt_int_op(0, OP_EQ, policies_parse_exit_policy(NULL, &policy9,
  241. EXIT_POLICY_REJECT_PRIVATE |
  242. EXIT_POLICY_ADD_DEFAULT,
  243. NULL));
  244. tt_assert(policy9);
  245. /* accept6 * and reject6 * produce IPv6 wildcards only */
  246. policy10 = smartlist_new();
  247. p = router_parse_addr_policy_item_from_string("accept6 *:*", -1,
  248. &malformed_list);
  249. tt_assert(p != NULL);
  250. smartlist_add(policy10, p);
  251. policy11 = smartlist_new();
  252. p = router_parse_addr_policy_item_from_string("reject6 *:*", -1,
  253. &malformed_list);
  254. tt_assert(p != NULL);
  255. smartlist_add(policy11, p);
  256. tt_assert(!exit_policy_is_general_exit(policy));
  257. tt_assert(exit_policy_is_general_exit(policy2));
  258. tt_assert(!exit_policy_is_general_exit(NULL));
  259. tt_assert(!exit_policy_is_general_exit(policy3));
  260. tt_assert(!exit_policy_is_general_exit(policy4));
  261. tt_assert(!exit_policy_is_general_exit(policy5));
  262. tt_assert(!exit_policy_is_general_exit(policy6));
  263. tt_assert(!exit_policy_is_general_exit(policy7));
  264. tt_assert(exit_policy_is_general_exit(policy8));
  265. tt_assert(exit_policy_is_general_exit(policy9));
  266. tt_assert(!exit_policy_is_general_exit(policy10));
  267. tt_assert(!exit_policy_is_general_exit(policy11));
  268. tt_assert(cmp_addr_policies(policy, policy2));
  269. tt_assert(cmp_addr_policies(policy, NULL));
  270. tt_assert(!cmp_addr_policies(policy2, policy2));
  271. tt_assert(!cmp_addr_policies(NULL, NULL));
  272. tt_assert(!policy_is_reject_star(policy2, AF_INET, 1));
  273. tt_assert(policy_is_reject_star(policy, AF_INET, 1));
  274. tt_assert(policy_is_reject_star(policy10, AF_INET, 1));
  275. tt_assert(!policy_is_reject_star(policy10, AF_INET6, 1));
  276. tt_assert(policy_is_reject_star(policy11, AF_INET, 1));
  277. tt_assert(policy_is_reject_star(policy11, AF_INET6, 1));
  278. tt_assert(policy_is_reject_star(NULL, AF_INET, 1));
  279. tt_assert(policy_is_reject_star(NULL, AF_INET6, 1));
  280. tt_assert(!policy_is_reject_star(NULL, AF_INET, 0));
  281. tt_assert(!policy_is_reject_star(NULL, AF_INET6, 0));
  282. addr_policy_list_free(policy);
  283. policy = NULL;
  284. /* make sure assume_action works */
  285. malformed_list = 0;
  286. p = router_parse_addr_policy_item_from_string("127.0.0.1",
  287. ADDR_POLICY_ACCEPT,
  288. &malformed_list);
  289. tt_assert(p);
  290. addr_policy_free(p);
  291. tt_assert(!malformed_list);
  292. p = router_parse_addr_policy_item_from_string("127.0.0.1:*",
  293. ADDR_POLICY_ACCEPT,
  294. &malformed_list);
  295. tt_assert(p);
  296. addr_policy_free(p);
  297. tt_assert(!malformed_list);
  298. p = router_parse_addr_policy_item_from_string("[::]",
  299. ADDR_POLICY_ACCEPT,
  300. &malformed_list);
  301. tt_assert(p);
  302. addr_policy_free(p);
  303. tt_assert(!malformed_list);
  304. p = router_parse_addr_policy_item_from_string("[::]:*",
  305. ADDR_POLICY_ACCEPT,
  306. &malformed_list);
  307. tt_assert(p);
  308. addr_policy_free(p);
  309. tt_assert(!malformed_list);
  310. p = router_parse_addr_policy_item_from_string("[face::b]",
  311. ADDR_POLICY_ACCEPT,
  312. &malformed_list);
  313. tt_assert(p);
  314. addr_policy_free(p);
  315. tt_assert(!malformed_list);
  316. p = router_parse_addr_policy_item_from_string("[b::aaaa]",
  317. ADDR_POLICY_ACCEPT,
  318. &malformed_list);
  319. tt_assert(p);
  320. addr_policy_free(p);
  321. tt_assert(!malformed_list);
  322. p = router_parse_addr_policy_item_from_string("*",
  323. ADDR_POLICY_ACCEPT,
  324. &malformed_list);
  325. tt_assert(p);
  326. addr_policy_free(p);
  327. tt_assert(!malformed_list);
  328. p = router_parse_addr_policy_item_from_string("*4",
  329. ADDR_POLICY_ACCEPT,
  330. &malformed_list);
  331. tt_assert(p);
  332. addr_policy_free(p);
  333. tt_assert(!malformed_list);
  334. p = router_parse_addr_policy_item_from_string("*6",
  335. ADDR_POLICY_ACCEPT,
  336. &malformed_list);
  337. tt_assert(p);
  338. addr_policy_free(p);
  339. tt_assert(!malformed_list);
  340. /* These are all ambiguous IPv6 addresses, it's good that we reject them */
  341. p = router_parse_addr_policy_item_from_string("acce::abcd",
  342. ADDR_POLICY_ACCEPT,
  343. &malformed_list);
  344. tt_assert(!p);
  345. tt_assert(malformed_list);
  346. malformed_list = 0;
  347. p = router_parse_addr_policy_item_from_string("7:1234",
  348. ADDR_POLICY_ACCEPT,
  349. &malformed_list);
  350. tt_assert(!p);
  351. tt_assert(malformed_list);
  352. malformed_list = 0;
  353. p = router_parse_addr_policy_item_from_string("::",
  354. ADDR_POLICY_ACCEPT,
  355. &malformed_list);
  356. tt_assert(!p);
  357. tt_assert(malformed_list);
  358. malformed_list = 0;
  359. /* make sure compacting logic works. */
  360. policy = NULL;
  361. line.key = (char*)"foo";
  362. line.value = (char*)"accept *:80,reject private:*,reject *:*";
  363. line.next = NULL;
  364. tt_int_op(0, OP_EQ, policies_parse_exit_policy(&line,&policy,
  365. EXIT_POLICY_IPV6_ENABLED |
  366. EXIT_POLICY_ADD_DEFAULT, NULL));
  367. tt_assert(policy);
  368. //test_streq(policy->string, "accept *:80");
  369. //test_streq(policy->next->string, "reject *:*");
  370. tt_int_op(smartlist_len(policy),OP_EQ, 4);
  371. /* test policy summaries */
  372. /* check if we properly ignore private IP addresses */
  373. test_policy_summary_helper("reject 192.168.0.0/16:*,"
  374. "reject 0.0.0.0/8:*,"
  375. "reject 10.0.0.0/8:*,"
  376. "accept *:10-30,"
  377. "accept *:90,"
  378. "reject *:*",
  379. "accept 10-30,90");
  380. /* check all accept policies, and proper counting of rejects */
  381. test_policy_summary_helper("reject 11.0.0.0/9:80,"
  382. "reject 12.0.0.0/9:80,"
  383. "reject 13.0.0.0/9:80,"
  384. "reject 14.0.0.0/9:80,"
  385. "accept *:*", "accept 1-65535");
  386. test_policy_summary_helper("reject 11.0.0.0/9:80,"
  387. "reject 12.0.0.0/9:80,"
  388. "reject 13.0.0.0/9:80,"
  389. "reject 14.0.0.0/9:80,"
  390. "reject 15.0.0.0:81,"
  391. "accept *:*", "accept 1-65535");
  392. test_policy_summary_helper6("reject 11.0.0.0/9:80,"
  393. "reject 12.0.0.0/9:80,"
  394. "reject 13.0.0.0/9:80,"
  395. "reject 14.0.0.0/9:80,"
  396. "reject 15.0.0.0:80,"
  397. "accept *:*",
  398. "reject 80",
  399. "accept 1-65535");
  400. /* no exits */
  401. test_policy_summary_helper("accept 11.0.0.0/9:80,"
  402. "reject *:*",
  403. "reject 1-65535");
  404. /* port merging */
  405. test_policy_summary_helper("accept *:80,"
  406. "accept *:81,"
  407. "accept *:100-110,"
  408. "accept *:111,"
  409. "reject *:*",
  410. "accept 80-81,100-111");
  411. /* border ports */
  412. test_policy_summary_helper("accept *:1,"
  413. "accept *:3,"
  414. "accept *:65535,"
  415. "reject *:*",
  416. "accept 1,3,65535");
  417. /* holes */
  418. test_policy_summary_helper("accept *:1,"
  419. "accept *:3,"
  420. "accept *:5,"
  421. "accept *:7,"
  422. "reject *:*",
  423. "accept 1,3,5,7");
  424. test_policy_summary_helper("reject *:1,"
  425. "reject *:3,"
  426. "reject *:5,"
  427. "reject *:7,"
  428. "accept *:*",
  429. "reject 1,3,5,7");
  430. /* long policies */
  431. /* standard long policy on many exits */
  432. test_policy_summary_helper("accept *:20-23,"
  433. "accept *:43,"
  434. "accept *:53,"
  435. "accept *:79-81,"
  436. "accept *:88,"
  437. "accept *:110,"
  438. "accept *:143,"
  439. "accept *:194,"
  440. "accept *:220,"
  441. "accept *:389,"
  442. "accept *:443,"
  443. "accept *:464,"
  444. "accept *:531,"
  445. "accept *:543-544,"
  446. "accept *:554,"
  447. "accept *:563,"
  448. "accept *:636,"
  449. "accept *:706,"
  450. "accept *:749,"
  451. "accept *:873,"
  452. "accept *:902-904,"
  453. "accept *:981,"
  454. "accept *:989-995,"
  455. "accept *:1194,"
  456. "accept *:1220,"
  457. "accept *:1293,"
  458. "accept *:1500,"
  459. "accept *:1533,"
  460. "accept *:1677,"
  461. "accept *:1723,"
  462. "accept *:1755,"
  463. "accept *:1863,"
  464. "accept *:2082,"
  465. "accept *:2083,"
  466. "accept *:2086-2087,"
  467. "accept *:2095-2096,"
  468. "accept *:2102-2104,"
  469. "accept *:3128,"
  470. "accept *:3389,"
  471. "accept *:3690,"
  472. "accept *:4321,"
  473. "accept *:4643,"
  474. "accept *:5050,"
  475. "accept *:5190,"
  476. "accept *:5222-5223,"
  477. "accept *:5228,"
  478. "accept *:5900,"
  479. "accept *:6660-6669,"
  480. "accept *:6679,"
  481. "accept *:6697,"
  482. "accept *:8000,"
  483. "accept *:8008,"
  484. "accept *:8074,"
  485. "accept *:8080,"
  486. "accept *:8087-8088,"
  487. "accept *:8332-8333,"
  488. "accept *:8443,"
  489. "accept *:8888,"
  490. "accept *:9418,"
  491. "accept *:9999,"
  492. "accept *:10000,"
  493. "accept *:11371,"
  494. "accept *:12350,"
  495. "accept *:19294,"
  496. "accept *:19638,"
  497. "accept *:23456,"
  498. "accept *:33033,"
  499. "accept *:64738,"
  500. "reject *:*",
  501. "accept 20-23,43,53,79-81,88,110,143,194,220,389,"
  502. "443,464,531,543-544,554,563,636,706,749,873,"
  503. "902-904,981,989-995,1194,1220,1293,1500,1533,"
  504. "1677,1723,1755,1863,2082-2083,2086-2087,"
  505. "2095-2096,2102-2104,3128,3389,3690,4321,4643,"
  506. "5050,5190,5222-5223,5228,5900,6660-6669,6679,"
  507. "6697,8000,8008,8074,8080,8087-8088,8332-8333,"
  508. "8443,8888,9418,9999-10000,11371,12350,19294,"
  509. "19638,23456,33033,64738");
  510. /* short policy with configured addresses */
  511. test_policy_summary_helper("reject 149.56.1.1:*,"
  512. "reject [2607:5300:1:1::1:0]:*,"
  513. "accept *:80,"
  514. "accept *:443,"
  515. "reject *:*",
  516. "accept 80,443");
  517. /* short policy with configured and local interface addresses */
  518. test_policy_summary_helper("reject 149.56.1.0:*,"
  519. "reject 149.56.1.1:*,"
  520. "reject 149.56.1.2:*,"
  521. "reject 149.56.1.3:*,"
  522. "reject 149.56.1.4:*,"
  523. "reject 149.56.1.5:*,"
  524. "reject 149.56.1.6:*,"
  525. "reject 149.56.1.7:*,"
  526. "reject [2607:5300:1:1::1:0]:*,"
  527. "reject [2607:5300:1:1::1:1]:*,"
  528. "reject [2607:5300:1:1::1:2]:*,"
  529. "reject [2607:5300:1:1::1:3]:*,"
  530. "reject [2607:5300:1:1::2:0]:*,"
  531. "reject [2607:5300:1:1::2:1]:*,"
  532. "reject [2607:5300:1:1::2:2]:*,"
  533. "reject [2607:5300:1:1::2:3]:*,"
  534. "accept *:80,"
  535. "accept *:443,"
  536. "reject *:*",
  537. "accept 80,443");
  538. /* short policy with configured netblocks */
  539. test_policy_summary_helper("reject 149.56.0.0/16,"
  540. "reject6 2607:5300::/32,"
  541. "accept *:80,"
  542. "accept *:443,"
  543. "reject *:*",
  544. "accept 80,443");
  545. /* short policy with large netblocks that count as a rejection */
  546. test_policy_summary_helper("reject 149.0.0.0/6,"
  547. "reject6 2600::/6,"
  548. "accept *:80,"
  549. "accept *:443,"
  550. "reject *:*",
  551. "reject 1-65535");
  552. /* longest possible policy
  553. * (1-2,4-5,... is longer, but gets reduced to 3,6,... )
  554. * Going all the way to 65535 is incredibly slow, so we just go slightly
  555. * more than the expected length */
  556. test_policy_summary_helper("accept *:1,"
  557. "accept *:3,"
  558. "accept *:5,"
  559. "accept *:7,"
  560. "accept *:9,"
  561. "accept *:11,"
  562. "accept *:13,"
  563. "accept *:15,"
  564. "accept *:17,"
  565. "accept *:19,"
  566. "accept *:21,"
  567. "accept *:23,"
  568. "accept *:25,"
  569. "accept *:27,"
  570. "accept *:29,"
  571. "accept *:31,"
  572. "accept *:33,"
  573. "accept *:35,"
  574. "accept *:37,"
  575. "accept *:39,"
  576. "accept *:41,"
  577. "accept *:43,"
  578. "accept *:45,"
  579. "accept *:47,"
  580. "accept *:49,"
  581. "accept *:51,"
  582. "accept *:53,"
  583. "accept *:55,"
  584. "accept *:57,"
  585. "accept *:59,"
  586. "accept *:61,"
  587. "accept *:63,"
  588. "accept *:65,"
  589. "accept *:67,"
  590. "accept *:69,"
  591. "accept *:71,"
  592. "accept *:73,"
  593. "accept *:75,"
  594. "accept *:77,"
  595. "accept *:79,"
  596. "accept *:81,"
  597. "accept *:83,"
  598. "accept *:85,"
  599. "accept *:87,"
  600. "accept *:89,"
  601. "accept *:91,"
  602. "accept *:93,"
  603. "accept *:95,"
  604. "accept *:97,"
  605. "accept *:99,"
  606. "accept *:101,"
  607. "accept *:103,"
  608. "accept *:105,"
  609. "accept *:107,"
  610. "accept *:109,"
  611. "accept *:111,"
  612. "accept *:113,"
  613. "accept *:115,"
  614. "accept *:117,"
  615. "accept *:119,"
  616. "accept *:121,"
  617. "accept *:123,"
  618. "accept *:125,"
  619. "accept *:127,"
  620. "accept *:129,"
  621. "accept *:131,"
  622. "accept *:133,"
  623. "accept *:135,"
  624. "accept *:137,"
  625. "accept *:139,"
  626. "accept *:141,"
  627. "accept *:143,"
  628. "accept *:145,"
  629. "accept *:147,"
  630. "accept *:149,"
  631. "accept *:151,"
  632. "accept *:153,"
  633. "accept *:155,"
  634. "accept *:157,"
  635. "accept *:159,"
  636. "accept *:161,"
  637. "accept *:163,"
  638. "accept *:165,"
  639. "accept *:167,"
  640. "accept *:169,"
  641. "accept *:171,"
  642. "accept *:173,"
  643. "accept *:175,"
  644. "accept *:177,"
  645. "accept *:179,"
  646. "accept *:181,"
  647. "accept *:183,"
  648. "accept *:185,"
  649. "accept *:187,"
  650. "accept *:189,"
  651. "accept *:191,"
  652. "accept *:193,"
  653. "accept *:195,"
  654. "accept *:197,"
  655. "accept *:199,"
  656. "accept *:201,"
  657. "accept *:203,"
  658. "accept *:205,"
  659. "accept *:207,"
  660. "accept *:209,"
  661. "accept *:211,"
  662. "accept *:213,"
  663. "accept *:215,"
  664. "accept *:217,"
  665. "accept *:219,"
  666. "accept *:221,"
  667. "accept *:223,"
  668. "accept *:225,"
  669. "accept *:227,"
  670. "accept *:229,"
  671. "accept *:231,"
  672. "accept *:233,"
  673. "accept *:235,"
  674. "accept *:237,"
  675. "accept *:239,"
  676. "accept *:241,"
  677. "accept *:243,"
  678. "accept *:245,"
  679. "accept *:247,"
  680. "accept *:249,"
  681. "accept *:251,"
  682. "accept *:253,"
  683. "accept *:255,"
  684. "accept *:257,"
  685. "accept *:259,"
  686. "accept *:261,"
  687. "accept *:263,"
  688. "accept *:265,"
  689. "accept *:267,"
  690. "accept *:269,"
  691. "accept *:271,"
  692. "accept *:273,"
  693. "accept *:275,"
  694. "accept *:277,"
  695. "accept *:279,"
  696. "accept *:281,"
  697. "accept *:283,"
  698. "accept *:285,"
  699. "accept *:287,"
  700. "accept *:289,"
  701. "accept *:291,"
  702. "accept *:293,"
  703. "accept *:295,"
  704. "accept *:297,"
  705. "accept *:299,"
  706. "accept *:301,"
  707. "accept *:303,"
  708. "accept *:305,"
  709. "accept *:307,"
  710. "accept *:309,"
  711. "accept *:311,"
  712. "accept *:313,"
  713. "accept *:315,"
  714. "accept *:317,"
  715. "accept *:319,"
  716. "accept *:321,"
  717. "accept *:323,"
  718. "accept *:325,"
  719. "accept *:327,"
  720. "accept *:329,"
  721. "accept *:331,"
  722. "accept *:333,"
  723. "accept *:335,"
  724. "accept *:337,"
  725. "accept *:339,"
  726. "accept *:341,"
  727. "accept *:343,"
  728. "accept *:345,"
  729. "accept *:347,"
  730. "accept *:349,"
  731. "accept *:351,"
  732. "accept *:353,"
  733. "accept *:355,"
  734. "accept *:357,"
  735. "accept *:359,"
  736. "accept *:361,"
  737. "accept *:363,"
  738. "accept *:365,"
  739. "accept *:367,"
  740. "accept *:369,"
  741. "accept *:371,"
  742. "accept *:373,"
  743. "accept *:375,"
  744. "accept *:377,"
  745. "accept *:379,"
  746. "accept *:381,"
  747. "accept *:383,"
  748. "accept *:385,"
  749. "accept *:387,"
  750. "accept *:389,"
  751. "accept *:391,"
  752. "accept *:393,"
  753. "accept *:395,"
  754. "accept *:397,"
  755. "accept *:399,"
  756. "accept *:401,"
  757. "accept *:403,"
  758. "accept *:405,"
  759. "accept *:407,"
  760. "accept *:409,"
  761. "accept *:411,"
  762. "accept *:413,"
  763. "accept *:415,"
  764. "accept *:417,"
  765. "accept *:419,"
  766. "accept *:421,"
  767. "accept *:423,"
  768. "accept *:425,"
  769. "accept *:427,"
  770. "accept *:429,"
  771. "accept *:431,"
  772. "accept *:433,"
  773. "accept *:435,"
  774. "accept *:437,"
  775. "accept *:439,"
  776. "accept *:441,"
  777. "accept *:443,"
  778. "accept *:445,"
  779. "accept *:447,"
  780. "accept *:449,"
  781. "accept *:451,"
  782. "accept *:453,"
  783. "accept *:455,"
  784. "accept *:457,"
  785. "accept *:459,"
  786. "accept *:461,"
  787. "accept *:463,"
  788. "accept *:465,"
  789. "accept *:467,"
  790. "accept *:469,"
  791. "accept *:471,"
  792. "accept *:473,"
  793. "accept *:475,"
  794. "accept *:477,"
  795. "accept *:479,"
  796. "accept *:481,"
  797. "accept *:483,"
  798. "accept *:485,"
  799. "accept *:487,"
  800. "accept *:489,"
  801. "accept *:491,"
  802. "accept *:493,"
  803. "accept *:495,"
  804. "accept *:497,"
  805. "accept *:499,"
  806. "accept *:501,"
  807. "accept *:503,"
  808. "accept *:505,"
  809. "accept *:507,"
  810. "accept *:509,"
  811. "accept *:511,"
  812. "accept *:513,"
  813. "accept *:515,"
  814. "accept *:517,"
  815. "accept *:519,"
  816. "accept *:521,"
  817. "accept *:523,"
  818. "accept *:525,"
  819. "accept *:527,"
  820. "accept *:529,"
  821. "reject *:*",
  822. "accept 1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,"
  823. "31,33,35,37,39,41,43,45,47,49,51,53,55,57,59,61,"
  824. "63,65,67,69,71,73,75,77,79,81,83,85,87,89,91,93,"
  825. "95,97,99,101,103,105,107,109,111,113,115,117,"
  826. "119,121,123,125,127,129,131,133,135,137,139,141,"
  827. "143,145,147,149,151,153,155,157,159,161,163,165,"
  828. "167,169,171,173,175,177,179,181,183,185,187,189,"
  829. "191,193,195,197,199,201,203,205,207,209,211,213,"
  830. "215,217,219,221,223,225,227,229,231,233,235,237,"
  831. "239,241,243,245,247,249,251,253,255,257,259,261,"
  832. "263,265,267,269,271,273,275,277,279,281,283,285,"
  833. "287,289,291,293,295,297,299,301,303,305,307,309,"
  834. "311,313,315,317,319,321,323,325,327,329,331,333,"
  835. "335,337,339,341,343,345,347,349,351,353,355,357,"
  836. "359,361,363,365,367,369,371,373,375,377,379,381,"
  837. "383,385,387,389,391,393,395,397,399,401,403,405,"
  838. "407,409,411,413,415,417,419,421,423,425,427,429,"
  839. "431,433,435,437,439,441,443,445,447,449,451,453,"
  840. "455,457,459,461,463,465,467,469,471,473,475,477,"
  841. "479,481,483,485,487,489,491,493,495,497,499,501,"
  842. "503,505,507,509,511,513,515,517,519,521,523");
  843. /* Short policies with unrecognized formats should get accepted. */
  844. test_short_policy_parse("accept fred,2,3-5", "accept 2,3-5");
  845. test_short_policy_parse("accept 2,fred,3", "accept 2,3");
  846. test_short_policy_parse("accept 2,fred,3,bob", "accept 2,3");
  847. test_short_policy_parse("accept 2,-3,500-600", "accept 2,500-600");
  848. /* Short policies with nil entries are accepted too. */
  849. test_short_policy_parse("accept 1,,3", "accept 1,3");
  850. test_short_policy_parse("accept 100-200,,", "accept 100-200");
  851. test_short_policy_parse("reject ,1-10,,,,30-40", "reject 1-10,30-40");
  852. /* Try parsing various broken short policies */
  853. #define TT_BAD_SHORT_POLICY(s) \
  854. do { \
  855. tt_ptr_op(NULL, OP_EQ, (short_parsed = parse_short_policy((s)))); \
  856. } while (0)
  857. TT_BAD_SHORT_POLICY("accept 200-199");
  858. TT_BAD_SHORT_POLICY("");
  859. TT_BAD_SHORT_POLICY("rejekt 1,2,3");
  860. TT_BAD_SHORT_POLICY("reject ");
  861. TT_BAD_SHORT_POLICY("reject");
  862. TT_BAD_SHORT_POLICY("rej");
  863. TT_BAD_SHORT_POLICY("accept 2,3,100000");
  864. TT_BAD_SHORT_POLICY("accept 2,3x,4");
  865. TT_BAD_SHORT_POLICY("accept 2,3x,4");
  866. TT_BAD_SHORT_POLICY("accept 2-");
  867. TT_BAD_SHORT_POLICY("accept 2-x");
  868. TT_BAD_SHORT_POLICY("accept 1-,3");
  869. TT_BAD_SHORT_POLICY("accept 1-,3");
  870. /* Make sure that IPv4 addresses are ignored in accept6/reject6 lines. */
  871. p = router_parse_addr_policy_item_from_string("accept6 1.2.3.4:*", -1,
  872. &malformed_list);
  873. tt_assert(p == NULL);
  874. tt_assert(!malformed_list);
  875. p = router_parse_addr_policy_item_from_string("reject6 2.4.6.0/24:*", -1,
  876. &malformed_list);
  877. tt_assert(p == NULL);
  878. tt_assert(!malformed_list);
  879. p = router_parse_addr_policy_item_from_string("accept6 *4:*", -1,
  880. &malformed_list);
  881. tt_assert(p == NULL);
  882. tt_assert(!malformed_list);
  883. /* Make sure malformed policies are detected as such. */
  884. p = router_parse_addr_policy_item_from_string("bad_token *4:*", -1,
  885. &malformed_list);
  886. tt_assert(p == NULL);
  887. tt_assert(malformed_list);
  888. p = router_parse_addr_policy_item_from_string("accept6 **:*", -1,
  889. &malformed_list);
  890. tt_assert(p == NULL);
  891. tt_assert(malformed_list);
  892. p = router_parse_addr_policy_item_from_string("accept */15:*", -1,
  893. &malformed_list);
  894. tt_assert(p == NULL);
  895. tt_assert(malformed_list);
  896. p = router_parse_addr_policy_item_from_string("reject6 */:*", -1,
  897. &malformed_list);
  898. tt_assert(p == NULL);
  899. tt_assert(malformed_list);
  900. p = router_parse_addr_policy_item_from_string("accept 127.0.0.1/33:*", -1,
  901. &malformed_list);
  902. tt_assert(p == NULL);
  903. tt_assert(malformed_list);
  904. p = router_parse_addr_policy_item_from_string("accept6 [::1]/129:*", -1,
  905. &malformed_list);
  906. tt_assert(p == NULL);
  907. tt_assert(malformed_list);
  908. p = router_parse_addr_policy_item_from_string("reject 8.8.8.8/-1:*", -1,
  909. &malformed_list);
  910. tt_assert(p == NULL);
  911. tt_assert(malformed_list);
  912. p = router_parse_addr_policy_item_from_string("reject 8.8.4.4:10-5", -1,
  913. &malformed_list);
  914. tt_assert(p == NULL);
  915. tt_assert(malformed_list);
  916. p = router_parse_addr_policy_item_from_string("reject 1.2.3.4:-1", -1,
  917. &malformed_list);
  918. tt_assert(p == NULL);
  919. tt_assert(malformed_list);
  920. /* Test a too-long policy. */
  921. {
  922. char *policy_strng = NULL;
  923. smartlist_t *chunks = smartlist_new();
  924. smartlist_add(chunks, tor_strdup("accept "));
  925. for (i=1; i<10000; ++i)
  926. smartlist_add_asprintf(chunks, "%d,", i);
  927. smartlist_add(chunks, tor_strdup("20000"));
  928. policy_strng = smartlist_join_strings(chunks, "", 0, NULL);
  929. SMARTLIST_FOREACH(chunks, char *, ch, tor_free(ch));
  930. smartlist_free(chunks);
  931. short_parsed = parse_short_policy(policy_strng);/* shouldn't be accepted */
  932. tor_free(policy_strng);
  933. tt_ptr_op(NULL, OP_EQ, short_parsed);
  934. }
  935. /* truncation ports */
  936. sm = smartlist_new();
  937. for (i=1; i<2000; i+=2) {
  938. char buf[POLICY_BUF_LEN];
  939. tor_snprintf(buf, sizeof(buf), "reject *:%d", i);
  940. smartlist_add(sm, tor_strdup(buf));
  941. }
  942. smartlist_add(sm, tor_strdup("accept *:*"));
  943. policy_str = smartlist_join_strings(sm, ",", 0, NULL);
  944. test_policy_summary_helper( policy_str,
  945. "accept 2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,"
  946. "46,48,50,52,54,56,58,60,62,64,66,68,70,72,74,76,78,80,82,84,86,88,90,"
  947. "92,94,96,98,100,102,104,106,108,110,112,114,116,118,120,122,124,126,128,"
  948. "130,132,134,136,138,140,142,144,146,148,150,152,154,156,158,160,162,164,"
  949. "166,168,170,172,174,176,178,180,182,184,186,188,190,192,194,196,198,200,"
  950. "202,204,206,208,210,212,214,216,218,220,222,224,226,228,230,232,234,236,"
  951. "238,240,242,244,246,248,250,252,254,256,258,260,262,264,266,268,270,272,"
  952. "274,276,278,280,282,284,286,288,290,292,294,296,298,300,302,304,306,308,"
  953. "310,312,314,316,318,320,322,324,326,328,330,332,334,336,338,340,342,344,"
  954. "346,348,350,352,354,356,358,360,362,364,366,368,370,372,374,376,378,380,"
  955. "382,384,386,388,390,392,394,396,398,400,402,404,406,408,410,412,414,416,"
  956. "418,420,422,424,426,428,430,432,434,436,438,440,442,444,446,448,450,452,"
  957. "454,456,458,460,462,464,466,468,470,472,474,476,478,480,482,484,486,488,"
  958. "490,492,494,496,498,500,502,504,506,508,510,512,514,516,518,520,522");
  959. done:
  960. addr_policy_list_free(policy);
  961. addr_policy_list_free(policy2);
  962. addr_policy_list_free(policy3);
  963. addr_policy_list_free(policy4);
  964. addr_policy_list_free(policy5);
  965. addr_policy_list_free(policy6);
  966. addr_policy_list_free(policy7);
  967. addr_policy_list_free(policy8);
  968. addr_policy_list_free(policy9);
  969. addr_policy_list_free(policy10);
  970. addr_policy_list_free(policy11);
  971. addr_policy_list_free(policy12);
  972. tor_free(policy_str);
  973. if (sm) {
  974. SMARTLIST_FOREACH(sm, char *, s, tor_free(s));
  975. smartlist_free(sm);
  976. }
  977. short_policy_free(short_parsed);
  978. }
  979. /** Helper: Check that policy_list contains address */
  980. static int
  981. test_policy_has_address_helper(const smartlist_t *policy_list,
  982. const tor_addr_t *addr)
  983. {
  984. int found = 0;
  985. tt_assert(policy_list);
  986. tt_assert(addr);
  987. SMARTLIST_FOREACH_BEGIN(policy_list, addr_policy_t*, p) {
  988. if (tor_addr_eq(&p->addr, addr)) {
  989. found = 1;
  990. }
  991. } SMARTLIST_FOREACH_END(p);
  992. return found;
  993. done:
  994. return 0;
  995. }
  996. #define TEST_IPV4_ADDR (0x01020304)
  997. #define TEST_IPV6_ADDR ("2002::abcd")
  998. /** Run unit tests for rejecting the configured addresses on this exit relay
  999. * using policies_parse_exit_policy_reject_private */
  1000. static void
  1001. test_policies_reject_exit_address(void *arg)
  1002. {
  1003. smartlist_t *policy = NULL;
  1004. tor_addr_t ipv4_addr, ipv6_addr;
  1005. smartlist_t *ipv4_list, *ipv6_list, *both_list, *dupl_list;
  1006. (void)arg;
  1007. tor_addr_from_ipv4h(&ipv4_addr, TEST_IPV4_ADDR);
  1008. tor_addr_parse(&ipv6_addr, TEST_IPV6_ADDR);
  1009. ipv4_list = smartlist_new();
  1010. ipv6_list = smartlist_new();
  1011. both_list = smartlist_new();
  1012. dupl_list = smartlist_new();
  1013. smartlist_add(ipv4_list, &ipv4_addr);
  1014. smartlist_add(both_list, &ipv4_addr);
  1015. smartlist_add(dupl_list, &ipv4_addr);
  1016. smartlist_add(dupl_list, &ipv4_addr);
  1017. smartlist_add(dupl_list, &ipv4_addr);
  1018. smartlist_add(ipv6_list, &ipv6_addr);
  1019. smartlist_add(both_list, &ipv6_addr);
  1020. smartlist_add(dupl_list, &ipv6_addr);
  1021. smartlist_add(dupl_list, &ipv6_addr);
  1022. /* IPv4-Only Exits */
  1023. /* test that IPv4 addresses are rejected on an IPv4-only exit */
  1024. policies_parse_exit_policy_reject_private(&policy, 0, ipv4_list, 0, 0);
  1025. tt_assert(policy);
  1026. tt_assert(smartlist_len(policy) == 1);
  1027. tt_assert(test_policy_has_address_helper(policy, &ipv4_addr));
  1028. addr_policy_list_free(policy);
  1029. policy = NULL;
  1030. /* test that IPv6 addresses are NOT rejected on an IPv4-only exit
  1031. * (all IPv6 addresses are rejected by policies_parse_exit_policy_internal
  1032. * on IPv4-only exits, so policies_parse_exit_policy_reject_private doesn't
  1033. * need to do anything) */
  1034. policies_parse_exit_policy_reject_private(&policy, 0, ipv6_list, 0, 0);
  1035. tt_assert(policy == NULL);
  1036. /* test that only IPv4 addresses are rejected on an IPv4-only exit */
  1037. policies_parse_exit_policy_reject_private(&policy, 0, both_list, 0, 0);
  1038. tt_assert(policy);
  1039. tt_assert(smartlist_len(policy) == 1);
  1040. tt_assert(test_policy_has_address_helper(policy, &ipv4_addr));
  1041. addr_policy_list_free(policy);
  1042. policy = NULL;
  1043. /* Test that lists with duplicate entries produce the same results */
  1044. policies_parse_exit_policy_reject_private(&policy, 0, dupl_list, 0, 0);
  1045. tt_assert(policy);
  1046. tt_assert(smartlist_len(policy) == 1);
  1047. tt_assert(test_policy_has_address_helper(policy, &ipv4_addr));
  1048. addr_policy_list_free(policy);
  1049. policy = NULL;
  1050. /* IPv4/IPv6 Exits */
  1051. /* test that IPv4 addresses are rejected on an IPv4/IPv6 exit */
  1052. policies_parse_exit_policy_reject_private(&policy, 1, ipv4_list, 0, 0);
  1053. tt_assert(policy);
  1054. tt_assert(smartlist_len(policy) == 1);
  1055. tt_assert(test_policy_has_address_helper(policy, &ipv4_addr));
  1056. addr_policy_list_free(policy);
  1057. policy = NULL;
  1058. /* test that IPv6 addresses are rejected on an IPv4/IPv6 exit */
  1059. policies_parse_exit_policy_reject_private(&policy, 1, ipv6_list, 0, 0);
  1060. tt_assert(policy);
  1061. tt_assert(smartlist_len(policy) == 1);
  1062. tt_assert(test_policy_has_address_helper(policy, &ipv6_addr));
  1063. addr_policy_list_free(policy);
  1064. policy = NULL;
  1065. /* test that IPv4 and IPv6 addresses are rejected on an IPv4/IPv6 exit */
  1066. policies_parse_exit_policy_reject_private(&policy, 1, both_list, 0, 0);
  1067. tt_assert(policy);
  1068. tt_assert(smartlist_len(policy) == 2);
  1069. tt_assert(test_policy_has_address_helper(policy, &ipv4_addr));
  1070. tt_assert(test_policy_has_address_helper(policy, &ipv6_addr));
  1071. addr_policy_list_free(policy);
  1072. policy = NULL;
  1073. /* Test that lists with duplicate entries produce the same results */
  1074. policies_parse_exit_policy_reject_private(&policy, 1, dupl_list, 0, 0);
  1075. tt_assert(policy);
  1076. tt_assert(smartlist_len(policy) == 2);
  1077. tt_assert(test_policy_has_address_helper(policy, &ipv4_addr));
  1078. tt_assert(test_policy_has_address_helper(policy, &ipv6_addr));
  1079. addr_policy_list_free(policy);
  1080. policy = NULL;
  1081. done:
  1082. addr_policy_list_free(policy);
  1083. smartlist_free(ipv4_list);
  1084. smartlist_free(ipv6_list);
  1085. smartlist_free(both_list);
  1086. smartlist_free(dupl_list);
  1087. }
  1088. static smartlist_t *test_configured_ports = NULL;
  1089. /** Returns test_configured_ports */
  1090. static const smartlist_t *
  1091. mock_get_configured_ports(void)
  1092. {
  1093. return test_configured_ports;
  1094. }
  1095. /** Run unit tests for rejecting publicly routable configured port addresses
  1096. * on this exit relay using policies_parse_exit_policy_reject_private */
  1097. static void
  1098. test_policies_reject_port_address(void *arg)
  1099. {
  1100. smartlist_t *policy = NULL;
  1101. port_cfg_t *ipv4_port = NULL;
  1102. port_cfg_t *ipv6_port = NULL;
  1103. (void)arg;
  1104. test_configured_ports = smartlist_new();
  1105. ipv4_port = port_cfg_new(0);
  1106. tor_addr_from_ipv4h(&ipv4_port->addr, TEST_IPV4_ADDR);
  1107. smartlist_add(test_configured_ports, ipv4_port);
  1108. ipv6_port = port_cfg_new(0);
  1109. tor_addr_parse(&ipv6_port->addr, TEST_IPV6_ADDR);
  1110. smartlist_add(test_configured_ports, ipv6_port);
  1111. MOCK(get_configured_ports, mock_get_configured_ports);
  1112. /* test that an IPv4 port is rejected on an IPv4-only exit, but an IPv6 port
  1113. * is NOT rejected (all IPv6 addresses are rejected by
  1114. * policies_parse_exit_policy_internal on IPv4-only exits, so
  1115. * policies_parse_exit_policy_reject_private doesn't need to do anything
  1116. * with IPv6 addresses on IPv4-only exits) */
  1117. policies_parse_exit_policy_reject_private(&policy, 0, NULL, 0, 1);
  1118. tt_assert(policy);
  1119. tt_assert(smartlist_len(policy) == 1);
  1120. tt_assert(test_policy_has_address_helper(policy, &ipv4_port->addr));
  1121. addr_policy_list_free(policy);
  1122. policy = NULL;
  1123. /* test that IPv4 and IPv6 ports are rejected on an IPv4/IPv6 exit */
  1124. policies_parse_exit_policy_reject_private(&policy, 1, NULL, 0, 1);
  1125. tt_assert(policy);
  1126. tt_assert(smartlist_len(policy) == 2);
  1127. tt_assert(test_policy_has_address_helper(policy, &ipv4_port->addr));
  1128. tt_assert(test_policy_has_address_helper(policy, &ipv6_port->addr));
  1129. addr_policy_list_free(policy);
  1130. policy = NULL;
  1131. done:
  1132. addr_policy_list_free(policy);
  1133. if (test_configured_ports) {
  1134. SMARTLIST_FOREACH(test_configured_ports,
  1135. port_cfg_t *, p, port_cfg_free(p));
  1136. smartlist_free(test_configured_ports);
  1137. test_configured_ports = NULL;
  1138. }
  1139. UNMOCK(get_configured_ports);
  1140. }
  1141. static smartlist_t *mock_ipv4_addrs = NULL;
  1142. static smartlist_t *mock_ipv6_addrs = NULL;
  1143. /* mock get_interface_address6_list, returning a deep copy of the template
  1144. * address list ipv4_interface_address_list or ipv6_interface_address_list */
  1145. static smartlist_t *
  1146. mock_get_interface_address6_list(int severity,
  1147. sa_family_t family,
  1148. int include_internal)
  1149. {
  1150. (void)severity;
  1151. (void)include_internal;
  1152. smartlist_t *clone_list = smartlist_new();
  1153. smartlist_t *template_list = NULL;
  1154. if (family == AF_INET) {
  1155. template_list = mock_ipv4_addrs;
  1156. } else if (family == AF_INET6) {
  1157. template_list = mock_ipv6_addrs;
  1158. } else {
  1159. return NULL;
  1160. }
  1161. tt_assert(template_list);
  1162. SMARTLIST_FOREACH_BEGIN(template_list, tor_addr_t *, src_addr) {
  1163. tor_addr_t *dest_addr = tor_malloc(sizeof(tor_addr_t));
  1164. memset(dest_addr, 0, sizeof(*dest_addr));
  1165. tor_addr_copy_tight(dest_addr, src_addr);
  1166. smartlist_add(clone_list, dest_addr);
  1167. } SMARTLIST_FOREACH_END(src_addr);
  1168. return clone_list;
  1169. done:
  1170. free_interface_address6_list(clone_list);
  1171. return NULL;
  1172. }
  1173. /** Run unit tests for rejecting publicly routable interface addresses on this
  1174. * exit relay using policies_parse_exit_policy_reject_private */
  1175. static void
  1176. test_policies_reject_interface_address(void *arg)
  1177. {
  1178. smartlist_t *policy = NULL;
  1179. smartlist_t *public_ipv4_addrs =
  1180. get_interface_address6_list(LOG_INFO, AF_INET, 0);
  1181. smartlist_t *public_ipv6_addrs =
  1182. get_interface_address6_list(LOG_INFO, AF_INET6, 0);
  1183. tor_addr_t ipv4_addr, ipv6_addr;
  1184. (void)arg;
  1185. /* test that no addresses are rejected when none are supplied/requested */
  1186. policies_parse_exit_policy_reject_private(&policy, 0, NULL, 0, 0);
  1187. tt_assert(policy == NULL);
  1188. /* test that only IPv4 interface addresses are rejected on an IPv4-only exit
  1189. * (and allow for duplicates)
  1190. */
  1191. policies_parse_exit_policy_reject_private(&policy, 0, NULL, 1, 0);
  1192. if (policy) {
  1193. tt_assert(smartlist_len(policy) <= smartlist_len(public_ipv4_addrs));
  1194. addr_policy_list_free(policy);
  1195. policy = NULL;
  1196. }
  1197. /* test that IPv4 and IPv6 interface addresses are rejected on an IPv4/IPv6
  1198. * exit (and allow for duplicates) */
  1199. policies_parse_exit_policy_reject_private(&policy, 1, NULL, 1, 0);
  1200. if (policy) {
  1201. tt_assert(smartlist_len(policy) <= (smartlist_len(public_ipv4_addrs)
  1202. + smartlist_len(public_ipv6_addrs)));
  1203. addr_policy_list_free(policy);
  1204. policy = NULL;
  1205. }
  1206. /* Now do it all again, but mocked */
  1207. tor_addr_from_ipv4h(&ipv4_addr, TEST_IPV4_ADDR);
  1208. mock_ipv4_addrs = smartlist_new();
  1209. smartlist_add(mock_ipv4_addrs, (void *)&ipv4_addr);
  1210. tor_addr_parse(&ipv6_addr, TEST_IPV6_ADDR);
  1211. mock_ipv6_addrs = smartlist_new();
  1212. smartlist_add(mock_ipv6_addrs, (void *)&ipv6_addr);
  1213. MOCK(get_interface_address6_list, mock_get_interface_address6_list);
  1214. /* test that no addresses are rejected when none are supplied/requested */
  1215. policies_parse_exit_policy_reject_private(&policy, 0, NULL, 0, 0);
  1216. tt_assert(policy == NULL);
  1217. /* test that only IPv4 interface addresses are rejected on an IPv4-only exit
  1218. */
  1219. policies_parse_exit_policy_reject_private(&policy, 0, NULL, 1, 0);
  1220. tt_assert(policy);
  1221. tt_assert(smartlist_len(policy) == smartlist_len(mock_ipv4_addrs));
  1222. addr_policy_list_free(policy);
  1223. policy = NULL;
  1224. /* test that IPv4 and IPv6 interface addresses are rejected on an IPv4/IPv6
  1225. * exit */
  1226. policies_parse_exit_policy_reject_private(&policy, 1, NULL, 1, 0);
  1227. tt_assert(policy);
  1228. tt_assert(smartlist_len(policy) == (smartlist_len(mock_ipv4_addrs)
  1229. + smartlist_len(mock_ipv6_addrs)));
  1230. addr_policy_list_free(policy);
  1231. policy = NULL;
  1232. done:
  1233. addr_policy_list_free(policy);
  1234. free_interface_address6_list(public_ipv4_addrs);
  1235. free_interface_address6_list(public_ipv6_addrs);
  1236. UNMOCK(get_interface_address6_list);
  1237. /* we don't use free_interface_address6_list on these lists because their
  1238. * address pointers are stack-based */
  1239. smartlist_free(mock_ipv4_addrs);
  1240. smartlist_free(mock_ipv6_addrs);
  1241. }
  1242. #undef TEST_IPV4_ADDR
  1243. #undef TEST_IPV6_ADDR
  1244. static void
  1245. test_dump_exit_policy_to_string(void *arg)
  1246. {
  1247. char *ep;
  1248. addr_policy_t *policy_entry;
  1249. int malformed_list = -1;
  1250. routerinfo_t *ri = tor_malloc_zero(sizeof(routerinfo_t));
  1251. (void)arg;
  1252. ri->policy_is_reject_star = 1;
  1253. ri->exit_policy = NULL; // expecting "reject *:*"
  1254. ep = router_dump_exit_policy_to_string(ri,1,1);
  1255. tt_str_op("reject *:*",OP_EQ, ep);
  1256. tor_free(ep);
  1257. ri->exit_policy = smartlist_new();
  1258. ri->policy_is_reject_star = 0;
  1259. policy_entry = router_parse_addr_policy_item_from_string("accept *:*", -1,
  1260. &malformed_list);
  1261. smartlist_add(ri->exit_policy,policy_entry);
  1262. ep = router_dump_exit_policy_to_string(ri,1,1);
  1263. tt_str_op("accept *:*",OP_EQ, ep);
  1264. tor_free(ep);
  1265. policy_entry = router_parse_addr_policy_item_from_string("reject *:25", -1,
  1266. &malformed_list);
  1267. smartlist_add(ri->exit_policy,policy_entry);
  1268. ep = router_dump_exit_policy_to_string(ri,1,1);
  1269. tt_str_op("accept *:*\nreject *:25",OP_EQ, ep);
  1270. tor_free(ep);
  1271. policy_entry =
  1272. router_parse_addr_policy_item_from_string("reject 8.8.8.8:*", -1,
  1273. &malformed_list);
  1274. smartlist_add(ri->exit_policy,policy_entry);
  1275. ep = router_dump_exit_policy_to_string(ri,1,1);
  1276. tt_str_op("accept *:*\nreject *:25\nreject 8.8.8.8:*",OP_EQ, ep);
  1277. tor_free(ep);
  1278. policy_entry =
  1279. router_parse_addr_policy_item_from_string("reject6 [FC00::]/7:*", -1,
  1280. &malformed_list);
  1281. smartlist_add(ri->exit_policy,policy_entry);
  1282. ep = router_dump_exit_policy_to_string(ri,1,1);
  1283. tt_str_op("accept *:*\nreject *:25\nreject 8.8.8.8:*\n"
  1284. "reject6 [fc00::]/7:*",OP_EQ, ep);
  1285. tor_free(ep);
  1286. policy_entry =
  1287. router_parse_addr_policy_item_from_string("accept6 [c000::]/3:*", -1,
  1288. &malformed_list);
  1289. smartlist_add(ri->exit_policy,policy_entry);
  1290. ep = router_dump_exit_policy_to_string(ri,1,1);
  1291. tt_str_op("accept *:*\nreject *:25\nreject 8.8.8.8:*\n"
  1292. "reject6 [fc00::]/7:*\naccept6 [c000::]/3:*",OP_EQ, ep);
  1293. done:
  1294. if (ri->exit_policy) {
  1295. SMARTLIST_FOREACH(ri->exit_policy, addr_policy_t *,
  1296. entry, addr_policy_free(entry));
  1297. smartlist_free(ri->exit_policy);
  1298. }
  1299. tor_free(ri);
  1300. tor_free(ep);
  1301. }
  1302. static routerinfo_t *mock_desc_routerinfo = NULL;
  1303. static const routerinfo_t *
  1304. mock_router_get_my_routerinfo(void)
  1305. {
  1306. return mock_desc_routerinfo;
  1307. }
  1308. #define DEFAULT_POLICY_STRING "reject *:*"
  1309. #define TEST_IPV4_ADDR (0x02040608)
  1310. #define TEST_IPV6_ADDR ("2003::ef01")
  1311. static or_options_t mock_options;
  1312. static const or_options_t *
  1313. mock_get_options(void)
  1314. {
  1315. return &mock_options;
  1316. }
  1317. /** Run unit tests for generating summary lines of exit policies */
  1318. static void
  1319. test_policies_getinfo_helper_policies(void *arg)
  1320. {
  1321. (void)arg;
  1322. int rv = 0;
  1323. size_t ipv4_len = 0, ipv6_len = 0;
  1324. char *answer = NULL;
  1325. const char *errmsg = NULL;
  1326. routerinfo_t mock_my_routerinfo;
  1327. memset(&mock_my_routerinfo, 0, sizeof(mock_my_routerinfo));
  1328. rv = getinfo_helper_policies(NULL, "exit-policy/default", &answer, &errmsg);
  1329. tt_assert(rv == 0);
  1330. tt_assert(answer != NULL);
  1331. tt_assert(strlen(answer) > 0);
  1332. tor_free(answer);
  1333. rv = getinfo_helper_policies(NULL, "exit-policy/reject-private/default",
  1334. &answer, &errmsg);
  1335. tt_assert(rv == 0);
  1336. tt_assert(answer != NULL);
  1337. tt_assert(strlen(answer) > 0);
  1338. tor_free(answer);
  1339. memset(&mock_my_routerinfo, 0, sizeof(routerinfo_t));
  1340. MOCK(router_get_my_routerinfo, mock_router_get_my_routerinfo);
  1341. mock_my_routerinfo.exit_policy = smartlist_new();
  1342. mock_desc_routerinfo = &mock_my_routerinfo;
  1343. memset(&mock_options, 0, sizeof(or_options_t));
  1344. MOCK(get_options, mock_get_options);
  1345. rv = getinfo_helper_policies(NULL, "exit-policy/reject-private/relay",
  1346. &answer, &errmsg);
  1347. tt_assert(rv == 0);
  1348. tt_assert(answer != NULL);
  1349. tt_assert(strlen(answer) == 0);
  1350. tor_free(answer);
  1351. rv = getinfo_helper_policies(NULL, "exit-policy/ipv4", &answer,
  1352. &errmsg);
  1353. tt_assert(rv == 0);
  1354. tt_assert(answer != NULL);
  1355. ipv4_len = strlen(answer);
  1356. tt_assert(ipv4_len == 0 || ipv4_len == strlen(DEFAULT_POLICY_STRING));
  1357. tt_assert(ipv4_len == 0 || !strcasecmp(answer, DEFAULT_POLICY_STRING));
  1358. tor_free(answer);
  1359. rv = getinfo_helper_policies(NULL, "exit-policy/ipv6", &answer,
  1360. &errmsg);
  1361. tt_assert(rv == 0);
  1362. tt_assert(answer != NULL);
  1363. ipv6_len = strlen(answer);
  1364. tt_assert(ipv6_len == 0 || ipv6_len == strlen(DEFAULT_POLICY_STRING));
  1365. tt_assert(ipv6_len == 0 || !strcasecmp(answer, DEFAULT_POLICY_STRING));
  1366. tor_free(answer);
  1367. rv = getinfo_helper_policies(NULL, "exit-policy/full", &answer,
  1368. &errmsg);
  1369. tt_assert(rv == 0);
  1370. tt_assert(answer != NULL);
  1371. /* It's either empty or it's the default */
  1372. tt_assert(strlen(answer) == 0 || !strcasecmp(answer, DEFAULT_POLICY_STRING));
  1373. tor_free(answer);
  1374. mock_my_routerinfo.addr = TEST_IPV4_ADDR;
  1375. tor_addr_parse(&mock_my_routerinfo.ipv6_addr, TEST_IPV6_ADDR);
  1376. append_exit_policy_string(&mock_my_routerinfo.exit_policy, "accept *4:*");
  1377. append_exit_policy_string(&mock_my_routerinfo.exit_policy, "reject *6:*");
  1378. mock_options.IPv6Exit = 1;
  1379. tor_addr_from_ipv4h(&mock_options.OutboundBindAddressIPv4_, TEST_IPV4_ADDR);
  1380. tor_addr_parse(&mock_options.OutboundBindAddressIPv6_, TEST_IPV6_ADDR);
  1381. mock_options.ExitPolicyRejectPrivate = 1;
  1382. mock_options.ExitPolicyRejectLocalInterfaces = 1;
  1383. rv = getinfo_helper_policies(NULL, "exit-policy/reject-private/relay",
  1384. &answer, &errmsg);
  1385. tt_assert(rv == 0);
  1386. tt_assert(answer != NULL);
  1387. tt_assert(strlen(answer) > 0);
  1388. tor_free(answer);
  1389. mock_options.ExitPolicyRejectPrivate = 1;
  1390. mock_options.ExitPolicyRejectLocalInterfaces = 0;
  1391. rv = getinfo_helper_policies(NULL, "exit-policy/reject-private/relay",
  1392. &answer, &errmsg);
  1393. tt_assert(rv == 0);
  1394. tt_assert(answer != NULL);
  1395. tt_assert(strlen(answer) > 0);
  1396. tor_free(answer);
  1397. mock_options.ExitPolicyRejectPrivate = 0;
  1398. mock_options.ExitPolicyRejectLocalInterfaces = 1;
  1399. rv = getinfo_helper_policies(NULL, "exit-policy/reject-private/relay",
  1400. &answer, &errmsg);
  1401. tt_assert(rv == 0);
  1402. tt_assert(answer != NULL);
  1403. tt_assert(strlen(answer) > 0);
  1404. tor_free(answer);
  1405. mock_options.ExitPolicyRejectPrivate = 0;
  1406. mock_options.ExitPolicyRejectLocalInterfaces = 0;
  1407. rv = getinfo_helper_policies(NULL, "exit-policy/reject-private/relay",
  1408. &answer, &errmsg);
  1409. tt_assert(rv == 0);
  1410. tt_assert(answer != NULL);
  1411. tt_assert(strlen(answer) == 0);
  1412. tor_free(answer);
  1413. rv = getinfo_helper_policies(NULL, "exit-policy/ipv4", &answer,
  1414. &errmsg);
  1415. tt_assert(rv == 0);
  1416. tt_assert(answer != NULL);
  1417. ipv4_len = strlen(answer);
  1418. tt_assert(ipv4_len > 0);
  1419. tor_free(answer);
  1420. rv = getinfo_helper_policies(NULL, "exit-policy/ipv6", &answer,
  1421. &errmsg);
  1422. tt_assert(rv == 0);
  1423. tt_assert(answer != NULL);
  1424. ipv6_len = strlen(answer);
  1425. tt_assert(ipv6_len > 0);
  1426. tor_free(answer);
  1427. rv = getinfo_helper_policies(NULL, "exit-policy/full", &answer,
  1428. &errmsg);
  1429. tt_assert(rv == 0);
  1430. tt_assert(answer != NULL);
  1431. tt_assert(strlen(answer) > 0);
  1432. tt_assert(strlen(answer) == ipv4_len + ipv6_len + 1);
  1433. tor_free(answer);
  1434. done:
  1435. tor_free(answer);
  1436. UNMOCK(get_options);
  1437. UNMOCK(router_get_my_routerinfo);
  1438. addr_policy_list_free(mock_my_routerinfo.exit_policy);
  1439. }
  1440. #undef DEFAULT_POLICY_STRING
  1441. #undef TEST_IPV4_ADDR
  1442. #undef TEST_IPV6_ADDR
  1443. #define TEST_IPV4_ADDR_STR "1.2.3.4"
  1444. #define TEST_IPV6_ADDR_STR "[1002::4567]"
  1445. #define REJECT_IPv4_FINAL_STR "reject 0.0.0.0/0:*"
  1446. #define REJECT_IPv6_FINAL_STR "reject [::]/0:*"
  1447. #define OTHER_IPV4_ADDR_STR "6.7.8.9"
  1448. #define OTHER_IPV6_ADDR_STR "[afff::]"
  1449. /** Run unit tests for fascist_firewall_allows_address */
  1450. static void
  1451. test_policies_fascist_firewall_allows_address(void *arg)
  1452. {
  1453. (void)arg;
  1454. tor_addr_t ipv4_addr, ipv6_addr, r_ipv4_addr, r_ipv6_addr;
  1455. tor_addr_t n_ipv4_addr, n_ipv6_addr;
  1456. const uint16_t port = 1234;
  1457. smartlist_t *policy = NULL;
  1458. smartlist_t *e_policy = NULL;
  1459. addr_policy_t *item = NULL;
  1460. int malformed_list = 0;
  1461. /* Setup the options and the items in the policies */
  1462. memset(&mock_options, 0, sizeof(or_options_t));
  1463. MOCK(get_options, mock_get_options);
  1464. policy = smartlist_new();
  1465. item = router_parse_addr_policy_item_from_string("accept "
  1466. TEST_IPV4_ADDR_STR ":*",
  1467. ADDR_POLICY_ACCEPT,
  1468. &malformed_list);
  1469. tt_assert(item);
  1470. tt_assert(!malformed_list);
  1471. smartlist_add(policy, item);
  1472. item = router_parse_addr_policy_item_from_string("accept "
  1473. TEST_IPV6_ADDR_STR,
  1474. ADDR_POLICY_ACCEPT,
  1475. &malformed_list);
  1476. tt_assert(item);
  1477. tt_assert(!malformed_list);
  1478. smartlist_add(policy, item);
  1479. /* Normally, policy_expand_unspec would do this for us */
  1480. item = router_parse_addr_policy_item_from_string(REJECT_IPv4_FINAL_STR,
  1481. ADDR_POLICY_ACCEPT,
  1482. &malformed_list);
  1483. tt_assert(item);
  1484. tt_assert(!malformed_list);
  1485. smartlist_add(policy, item);
  1486. item = router_parse_addr_policy_item_from_string(REJECT_IPv6_FINAL_STR,
  1487. ADDR_POLICY_ACCEPT,
  1488. &malformed_list);
  1489. tt_assert(item);
  1490. tt_assert(!malformed_list);
  1491. smartlist_add(policy, item);
  1492. item = NULL;
  1493. e_policy = smartlist_new();
  1494. /*
  1495. char *polstr = policy_dump_to_string(policy, 1, 1);
  1496. printf("%s\n", polstr);
  1497. tor_free(polstr);
  1498. */
  1499. /* Parse the addresses */
  1500. tor_addr_parse(&ipv4_addr, TEST_IPV4_ADDR_STR);
  1501. tor_addr_parse(&ipv6_addr, TEST_IPV6_ADDR_STR);
  1502. tor_addr_parse(&r_ipv4_addr, OTHER_IPV4_ADDR_STR);
  1503. tor_addr_parse(&r_ipv6_addr, OTHER_IPV6_ADDR_STR);
  1504. tor_addr_make_null(&n_ipv4_addr, AF_INET);
  1505. tor_addr_make_null(&n_ipv6_addr, AF_INET6);
  1506. /* Test the function's address matching with IPv4 and IPv6 on */
  1507. memset(&mock_options, 0, sizeof(or_options_t));
  1508. mock_options.ClientUseIPv4 = 1;
  1509. mock_options.ClientUseIPv6 = 1;
  1510. mock_options.UseBridges = 0;
  1511. tt_assert(fascist_firewall_allows_address(&ipv4_addr, port, policy, 0, 0)
  1512. == 1);
  1513. tt_assert(fascist_firewall_allows_address(&ipv6_addr, port, policy, 0, 0)
  1514. == 1);
  1515. tt_assert(fascist_firewall_allows_address(&r_ipv4_addr, port, policy, 0, 0)
  1516. == 0);
  1517. tt_assert(fascist_firewall_allows_address(&r_ipv6_addr, port, policy, 0, 0)
  1518. == 0);
  1519. /* Preferring IPv4 */
  1520. tt_assert(fascist_firewall_allows_address(&ipv4_addr, port, policy, 1, 0)
  1521. == 1);
  1522. tt_assert(fascist_firewall_allows_address(&ipv6_addr, port, policy, 1, 0)
  1523. == 0);
  1524. tt_assert(fascist_firewall_allows_address(&r_ipv4_addr, port, policy, 1, 0)
  1525. == 0);
  1526. tt_assert(fascist_firewall_allows_address(&r_ipv6_addr, port, policy, 1, 0)
  1527. == 0);
  1528. /* Preferring IPv6 */
  1529. tt_assert(fascist_firewall_allows_address(&ipv4_addr, port, policy, 1, 1)
  1530. == 0);
  1531. tt_assert(fascist_firewall_allows_address(&ipv6_addr, port, policy, 1, 1)
  1532. == 1);
  1533. tt_assert(fascist_firewall_allows_address(&r_ipv4_addr, port, policy, 1, 1)
  1534. == 0);
  1535. tt_assert(fascist_firewall_allows_address(&r_ipv6_addr, port, policy, 1, 1)
  1536. == 0);
  1537. /* Test the function's address matching with UseBridges on */
  1538. memset(&mock_options, 0, sizeof(or_options_t));
  1539. mock_options.ClientUseIPv4 = 1;
  1540. mock_options.ClientUseIPv6 = 1;
  1541. mock_options.UseBridges = 1;
  1542. tt_assert(fascist_firewall_allows_address(&ipv4_addr, port, policy, 0, 0)
  1543. == 1);
  1544. tt_assert(fascist_firewall_allows_address(&ipv6_addr, port, policy, 0, 0)
  1545. == 1);
  1546. tt_assert(fascist_firewall_allows_address(&r_ipv4_addr, port, policy, 0, 0)
  1547. == 0);
  1548. tt_assert(fascist_firewall_allows_address(&r_ipv6_addr, port, policy, 0, 0)
  1549. == 0);
  1550. /* Preferring IPv4 */
  1551. tt_assert(fascist_firewall_allows_address(&ipv4_addr, port, policy, 1, 0)
  1552. == 1);
  1553. tt_assert(fascist_firewall_allows_address(&ipv6_addr, port, policy, 1, 0)
  1554. == 0);
  1555. tt_assert(fascist_firewall_allows_address(&r_ipv4_addr, port, policy, 1, 0)
  1556. == 0);
  1557. tt_assert(fascist_firewall_allows_address(&r_ipv6_addr, port, policy, 1, 0)
  1558. == 0);
  1559. /* Preferring IPv6 */
  1560. tt_assert(fascist_firewall_allows_address(&ipv4_addr, port, policy, 1, 1)
  1561. == 0);
  1562. tt_assert(fascist_firewall_allows_address(&ipv6_addr, port, policy, 1, 1)
  1563. == 1);
  1564. tt_assert(fascist_firewall_allows_address(&r_ipv4_addr, port, policy, 1, 1)
  1565. == 0);
  1566. tt_assert(fascist_firewall_allows_address(&r_ipv6_addr, port, policy, 1, 1)
  1567. == 0);
  1568. /* bridge clients always use IPv6, regardless of ClientUseIPv6 */
  1569. mock_options.ClientUseIPv4 = 1;
  1570. mock_options.ClientUseIPv6 = 0;
  1571. tt_assert(fascist_firewall_allows_address(&ipv4_addr, port, policy, 0, 0)
  1572. == 1);
  1573. tt_assert(fascist_firewall_allows_address(&ipv6_addr, port, policy, 0, 0)
  1574. == 1);
  1575. tt_assert(fascist_firewall_allows_address(&r_ipv4_addr, port, policy, 0, 0)
  1576. == 0);
  1577. tt_assert(fascist_firewall_allows_address(&r_ipv6_addr, port, policy, 0, 0)
  1578. == 0);
  1579. /* Test the function's address matching with IPv4 on */
  1580. memset(&mock_options, 0, sizeof(or_options_t));
  1581. mock_options.ClientUseIPv4 = 1;
  1582. mock_options.ClientUseIPv6 = 0;
  1583. mock_options.UseBridges = 0;
  1584. tt_assert(fascist_firewall_allows_address(&ipv4_addr, port, policy, 0, 0)
  1585. == 1);
  1586. tt_assert(fascist_firewall_allows_address(&ipv6_addr, port, policy, 0, 0)
  1587. == 0);
  1588. tt_assert(fascist_firewall_allows_address(&r_ipv4_addr, port, policy, 0, 0)
  1589. == 0);
  1590. tt_assert(fascist_firewall_allows_address(&r_ipv6_addr, port, policy, 0, 0)
  1591. == 0);
  1592. /* Test the function's address matching with IPv6 on */
  1593. memset(&mock_options, 0, sizeof(or_options_t));
  1594. mock_options.ClientUseIPv4 = 0;
  1595. mock_options.ClientUseIPv6 = 1;
  1596. mock_options.UseBridges = 0;
  1597. tt_assert(fascist_firewall_allows_address(&ipv4_addr, port, policy, 0, 0)
  1598. == 0);
  1599. tt_assert(fascist_firewall_allows_address(&ipv6_addr, port, policy, 0, 0)
  1600. == 1);
  1601. tt_assert(fascist_firewall_allows_address(&r_ipv4_addr, port, policy, 0, 0)
  1602. == 0);
  1603. tt_assert(fascist_firewall_allows_address(&r_ipv6_addr, port, policy, 0, 0)
  1604. == 0);
  1605. /* Test the function's address matching with ClientUseIPv4 0.
  1606. * This means "use IPv6" regardless of the other settings. */
  1607. memset(&mock_options, 0, sizeof(or_options_t));
  1608. mock_options.ClientUseIPv4 = 0;
  1609. mock_options.ClientUseIPv6 = 0;
  1610. mock_options.UseBridges = 0;
  1611. tt_assert(fascist_firewall_allows_address(&ipv4_addr, port, policy, 0, 0)
  1612. == 0);
  1613. tt_assert(fascist_firewall_allows_address(&ipv6_addr, port, policy, 0, 0)
  1614. == 1);
  1615. tt_assert(fascist_firewall_allows_address(&r_ipv4_addr, port, policy, 0, 0)
  1616. == 0);
  1617. tt_assert(fascist_firewall_allows_address(&r_ipv6_addr, port, policy, 0, 0)
  1618. == 0);
  1619. /* Test the function's address matching for unusual inputs */
  1620. memset(&mock_options, 0, sizeof(or_options_t));
  1621. mock_options.ClientUseIPv4 = 1;
  1622. mock_options.ClientUseIPv6 = 1;
  1623. mock_options.UseBridges = 1;
  1624. /* NULL and tor_addr_is_null addresses are rejected */
  1625. tt_assert(fascist_firewall_allows_address(NULL, port, policy, 0, 0) == 0);
  1626. tt_assert(fascist_firewall_allows_address(&n_ipv4_addr, port, policy, 0, 0)
  1627. == 0);
  1628. tt_assert(fascist_firewall_allows_address(&n_ipv6_addr, port, policy, 0, 0)
  1629. == 0);
  1630. /* zero ports are rejected */
  1631. tt_assert(fascist_firewall_allows_address(&ipv4_addr, 0, policy, 0, 0)
  1632. == 0);
  1633. tt_assert(fascist_firewall_allows_address(&ipv6_addr, 0, policy, 0, 0)
  1634. == 0);
  1635. /* NULL and empty policies accept everything */
  1636. tt_assert(fascist_firewall_allows_address(&ipv4_addr, port, NULL, 0, 0)
  1637. == 1);
  1638. tt_assert(fascist_firewall_allows_address(&ipv6_addr, port, NULL, 0, 0)
  1639. == 1);
  1640. tt_assert(fascist_firewall_allows_address(&ipv4_addr, port, e_policy, 0, 0)
  1641. == 1);
  1642. tt_assert(fascist_firewall_allows_address(&ipv6_addr, port, e_policy, 0, 0)
  1643. == 1);
  1644. done:
  1645. addr_policy_free(item);
  1646. addr_policy_list_free(policy);
  1647. addr_policy_list_free(e_policy);
  1648. UNMOCK(get_options);
  1649. }
  1650. #undef REJECT_IPv4_FINAL_STR
  1651. #undef REJECT_IPv6_FINAL_STR
  1652. #undef OTHER_IPV4_ADDR_STR
  1653. #undef OTHER_IPV6_ADDR_STR
  1654. #define TEST_IPV4_OR_PORT 1234
  1655. #define TEST_IPV4_DIR_PORT 2345
  1656. #define TEST_IPV6_OR_PORT 61234
  1657. #define TEST_IPV6_DIR_PORT 62345
  1658. /* Check that fascist_firewall_choose_address_rs() returns the expected
  1659. * results. */
  1660. #define CHECK_CHOSEN_ADDR_RS(fake_rs, fw_connection, pref_only, expect_rv, \
  1661. expect_ap) \
  1662. STMT_BEGIN \
  1663. tor_addr_port_t chosen_rs_ap; \
  1664. tor_addr_make_null(&chosen_rs_ap.addr, AF_INET); \
  1665. chosen_rs_ap.port = 0; \
  1666. tt_int_op(fascist_firewall_choose_address_rs(&(fake_rs), \
  1667. (fw_connection), \
  1668. (pref_only), \
  1669. &chosen_rs_ap), \
  1670. OP_EQ, (expect_rv)); \
  1671. tt_assert(tor_addr_eq(&(expect_ap).addr, &chosen_rs_ap.addr)); \
  1672. tt_int_op((expect_ap).port, OP_EQ, chosen_rs_ap.port); \
  1673. STMT_END
  1674. /* Check that fascist_firewall_choose_address_node() returns the expected
  1675. * results. */
  1676. #define CHECK_CHOSEN_ADDR_NODE(fake_node, fw_connection, pref_only, \
  1677. expect_rv, expect_ap) \
  1678. STMT_BEGIN \
  1679. tor_addr_port_t chosen_node_ap; \
  1680. tor_addr_make_null(&chosen_node_ap.addr, AF_INET); \
  1681. chosen_node_ap.port = 0; \
  1682. tt_int_op(fascist_firewall_choose_address_node(&(fake_node), \
  1683. (fw_connection), \
  1684. (pref_only), \
  1685. &chosen_node_ap), \
  1686. OP_EQ, (expect_rv)); \
  1687. tt_assert(tor_addr_eq(&(expect_ap).addr, &chosen_node_ap.addr)); \
  1688. tt_int_op((expect_ap).port, OP_EQ, chosen_node_ap.port); \
  1689. STMT_END
  1690. /* Check that fascist_firewall_choose_address_rs and
  1691. * fascist_firewall_choose_address_node() both return the expected results. */
  1692. #define CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, fw_connection, pref_only, \
  1693. expect_rv, expect_ap) \
  1694. STMT_BEGIN \
  1695. CHECK_CHOSEN_ADDR_RS(fake_rs, fw_connection, pref_only, expect_rv, \
  1696. expect_ap); \
  1697. CHECK_CHOSEN_ADDR_NODE(fake_node, fw_connection, pref_only, expect_rv, \
  1698. expect_ap); \
  1699. STMT_END
  1700. /** Run unit tests for fascist_firewall_choose_address */
  1701. static void
  1702. test_policies_fascist_firewall_choose_address(void *arg)
  1703. {
  1704. (void)arg;
  1705. tor_addr_port_t ipv4_or_ap, ipv4_dir_ap, ipv6_or_ap, ipv6_dir_ap;
  1706. tor_addr_port_t n_ipv4_ap, n_ipv6_ap;
  1707. /* Setup the options */
  1708. memset(&mock_options, 0, sizeof(or_options_t));
  1709. MOCK(get_options, mock_get_options);
  1710. /* Parse the addresses */
  1711. tor_addr_parse(&ipv4_or_ap.addr, TEST_IPV4_ADDR_STR);
  1712. ipv4_or_ap.port = TEST_IPV4_OR_PORT;
  1713. tor_addr_parse(&ipv4_dir_ap.addr, TEST_IPV4_ADDR_STR);
  1714. ipv4_dir_ap.port = TEST_IPV4_DIR_PORT;
  1715. tor_addr_parse(&ipv6_or_ap.addr, TEST_IPV6_ADDR_STR);
  1716. ipv6_or_ap.port = TEST_IPV6_OR_PORT;
  1717. tor_addr_parse(&ipv6_dir_ap.addr, TEST_IPV6_ADDR_STR);
  1718. ipv6_dir_ap.port = TEST_IPV6_DIR_PORT;
  1719. tor_addr_make_null(&n_ipv4_ap.addr, AF_INET);
  1720. n_ipv4_ap.port = 0;
  1721. tor_addr_make_null(&n_ipv6_ap.addr, AF_INET6);
  1722. n_ipv6_ap.port = 0;
  1723. /* Sanity check fascist_firewall_choose_address with IPv4 and IPv6 on */
  1724. memset(&mock_options, 0, sizeof(or_options_t));
  1725. mock_options.ClientUseIPv4 = 1;
  1726. mock_options.ClientUseIPv6 = 1;
  1727. mock_options.UseBridges = 0;
  1728. /* Prefer IPv4 */
  1729. tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &ipv6_or_ap, 1,
  1730. FIREWALL_OR_CONNECTION, 0, 0)
  1731. == &ipv4_or_ap);
  1732. tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &ipv6_or_ap, 1,
  1733. FIREWALL_OR_CONNECTION, 1, 0)
  1734. == &ipv4_or_ap);
  1735. tt_assert(fascist_firewall_choose_address(&ipv4_dir_ap, &ipv6_dir_ap, 1,
  1736. FIREWALL_DIR_CONNECTION, 0, 0)
  1737. == &ipv4_dir_ap);
  1738. tt_assert(fascist_firewall_choose_address(&ipv4_dir_ap, &ipv6_dir_ap, 1,
  1739. FIREWALL_DIR_CONNECTION, 1, 0)
  1740. == &ipv4_dir_ap);
  1741. /* Prefer IPv6 */
  1742. tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &ipv6_or_ap, 0,
  1743. FIREWALL_OR_CONNECTION, 0, 1)
  1744. == &ipv6_or_ap);
  1745. tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &ipv6_or_ap, 0,
  1746. FIREWALL_OR_CONNECTION, 1, 1)
  1747. == &ipv6_or_ap);
  1748. tt_assert(fascist_firewall_choose_address(&ipv4_dir_ap, &ipv6_dir_ap, 0,
  1749. FIREWALL_DIR_CONNECTION, 0, 1)
  1750. == &ipv6_dir_ap);
  1751. tt_assert(fascist_firewall_choose_address(&ipv4_dir_ap, &ipv6_dir_ap, 0,
  1752. FIREWALL_DIR_CONNECTION, 1, 1)
  1753. == &ipv6_dir_ap);
  1754. /* Unusual inputs */
  1755. /* null preferred OR addresses */
  1756. tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &n_ipv6_ap, 0,
  1757. FIREWALL_OR_CONNECTION, 0, 1)
  1758. == &ipv4_or_ap);
  1759. tt_assert(fascist_firewall_choose_address(&n_ipv4_ap, &ipv6_or_ap, 1,
  1760. FIREWALL_OR_CONNECTION, 0, 0)
  1761. == &ipv6_or_ap);
  1762. /* null both OR addresses */
  1763. tt_assert(fascist_firewall_choose_address(&n_ipv4_ap, &n_ipv6_ap, 0,
  1764. FIREWALL_OR_CONNECTION, 0, 1)
  1765. == NULL);
  1766. tt_assert(fascist_firewall_choose_address(&n_ipv4_ap, &n_ipv6_ap, 1,
  1767. FIREWALL_OR_CONNECTION, 0, 0)
  1768. == NULL);
  1769. /* null preferred Dir addresses */
  1770. tt_assert(fascist_firewall_choose_address(&ipv4_dir_ap, &n_ipv6_ap, 0,
  1771. FIREWALL_DIR_CONNECTION, 0, 1)
  1772. == &ipv4_dir_ap);
  1773. tt_assert(fascist_firewall_choose_address(&n_ipv4_ap, &ipv6_dir_ap, 1,
  1774. FIREWALL_DIR_CONNECTION, 0, 0)
  1775. == &ipv6_dir_ap);
  1776. /* null both Dir addresses */
  1777. tt_assert(fascist_firewall_choose_address(&n_ipv4_ap, &n_ipv6_ap, 0,
  1778. FIREWALL_DIR_CONNECTION, 0, 1)
  1779. == NULL);
  1780. tt_assert(fascist_firewall_choose_address(&n_ipv4_ap, &n_ipv6_ap, 1,
  1781. FIREWALL_DIR_CONNECTION, 0, 0)
  1782. == NULL);
  1783. /* Prefer IPv4 but want IPv6 (contradictory) */
  1784. tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &ipv6_or_ap, 0,
  1785. FIREWALL_OR_CONNECTION, 0, 0)
  1786. == &ipv4_or_ap);
  1787. tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &ipv6_or_ap, 0,
  1788. FIREWALL_OR_CONNECTION, 1, 0)
  1789. == &ipv4_or_ap);
  1790. /* Prefer IPv6 but want IPv4 (contradictory) */
  1791. tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &ipv6_or_ap, 1,
  1792. FIREWALL_OR_CONNECTION, 0, 1)
  1793. == &ipv6_or_ap);
  1794. tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &ipv6_or_ap, 1,
  1795. FIREWALL_OR_CONNECTION, 1, 1)
  1796. == &ipv6_or_ap);
  1797. /* Make a fake rs. There will be no corresponding node.
  1798. * This is what happens when there's no consensus and we're bootstrapping
  1799. * from authorities / fallbacks. */
  1800. routerstatus_t fake_rs;
  1801. memset(&fake_rs, 0, sizeof(routerstatus_t));
  1802. /* In a routerstatus, the OR and Dir addresses are the same */
  1803. fake_rs.addr = tor_addr_to_ipv4h(&ipv4_or_ap.addr);
  1804. fake_rs.or_port = ipv4_or_ap.port;
  1805. fake_rs.dir_port = ipv4_dir_ap.port;
  1806. tor_addr_copy(&fake_rs.ipv6_addr, &ipv6_or_ap.addr);
  1807. fake_rs.ipv6_orport = ipv6_or_ap.port;
  1808. /* In a routerstatus, the IPv4 and IPv6 DirPorts are the same.*/
  1809. ipv6_dir_ap.port = TEST_IPV4_DIR_PORT;
  1810. /* Make a fake node. Even though it contains the fake_rs, a lookup won't
  1811. * find the node from the rs, because they're not in the hash table. */
  1812. node_t fake_node;
  1813. memset(&fake_node, 0, sizeof(node_t));
  1814. fake_node.rs = &fake_rs;
  1815. /* Choose an address with IPv4 and IPv6 on */
  1816. memset(&mock_options, 0, sizeof(or_options_t));
  1817. mock_options.ClientUseIPv4 = 1;
  1818. mock_options.ClientUseIPv6 = 1;
  1819. mock_options.UseBridges = 0;
  1820. /* Preferring IPv4 */
  1821. mock_options.ClientPreferIPv6ORPort = 0;
  1822. mock_options.ClientPreferIPv6DirPort = 0;
  1823. /* Simulate the initialisation of fake_node.ipv6_preferred */
  1824. fake_node.ipv6_preferred = fascist_firewall_prefer_ipv6_orport(
  1825. &mock_options);
  1826. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 0, 1,
  1827. ipv4_or_ap);
  1828. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 1, 1,
  1829. ipv4_or_ap);
  1830. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
  1831. ipv4_dir_ap);
  1832. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
  1833. ipv4_dir_ap);
  1834. /* Auto (Preferring IPv4) */
  1835. mock_options.ClientPreferIPv6ORPort = -1;
  1836. mock_options.ClientPreferIPv6DirPort = -1;
  1837. /* Simulate the initialisation of fake_node.ipv6_preferred */
  1838. fake_node.ipv6_preferred = fascist_firewall_prefer_ipv6_orport(
  1839. &mock_options);
  1840. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 0, 1,
  1841. ipv4_or_ap);
  1842. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 1, 1,
  1843. ipv4_or_ap);
  1844. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
  1845. ipv4_dir_ap);
  1846. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
  1847. ipv4_dir_ap);
  1848. /* Preferring IPv6 */
  1849. mock_options.ClientPreferIPv6ORPort = 1;
  1850. mock_options.ClientPreferIPv6DirPort = 1;
  1851. /* Simulate the initialisation of fake_node.ipv6_preferred */
  1852. fake_node.ipv6_preferred = fascist_firewall_prefer_ipv6_orport(
  1853. &mock_options);
  1854. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 0, 1,
  1855. ipv6_or_ap);
  1856. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 1, 1,
  1857. ipv6_or_ap);
  1858. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
  1859. ipv6_dir_ap);
  1860. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
  1861. ipv6_dir_ap);
  1862. /* Preferring IPv4 OR / IPv6 Dir */
  1863. mock_options.ClientPreferIPv6ORPort = 0;
  1864. mock_options.ClientPreferIPv6DirPort = 1;
  1865. /* Simulate the initialisation of fake_node.ipv6_preferred */
  1866. fake_node.ipv6_preferred = fascist_firewall_prefer_ipv6_orport(
  1867. &mock_options);
  1868. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 0, 1,
  1869. ipv4_or_ap);
  1870. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 1, 1,
  1871. ipv4_or_ap);
  1872. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
  1873. ipv6_dir_ap);
  1874. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
  1875. ipv6_dir_ap);
  1876. /* Preferring IPv6 OR / IPv4 Dir */
  1877. mock_options.ClientPreferIPv6ORPort = 1;
  1878. mock_options.ClientPreferIPv6DirPort = 0;
  1879. /* Simulate the initialisation of fake_node.ipv6_preferred */
  1880. fake_node.ipv6_preferred = fascist_firewall_prefer_ipv6_orport(
  1881. &mock_options);
  1882. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 0, 1,
  1883. ipv6_or_ap);
  1884. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 1, 1,
  1885. ipv6_or_ap);
  1886. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
  1887. ipv4_dir_ap);
  1888. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
  1889. ipv4_dir_ap);
  1890. /* Choose an address with UseBridges on */
  1891. memset(&mock_options, 0, sizeof(or_options_t));
  1892. mock_options.UseBridges = 1;
  1893. mock_options.ClientUseIPv4 = 1;
  1894. mock_options.ClientUseIPv6 = 1;
  1895. /* Preferring IPv4 */
  1896. mock_options.ClientPreferIPv6ORPort = 0;
  1897. mock_options.ClientPreferIPv6DirPort = 0;
  1898. /* Simulate the initialisation of fake_node.ipv6_preferred */
  1899. fake_node.ipv6_preferred = fascist_firewall_prefer_ipv6_orport(
  1900. &mock_options);
  1901. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 0, 1,
  1902. ipv4_or_ap);
  1903. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 1, 1,
  1904. ipv4_or_ap);
  1905. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
  1906. ipv4_dir_ap);
  1907. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
  1908. ipv4_dir_ap);
  1909. /* Auto:
  1910. * - bridge clients prefer the configured bridge OR address from the node,
  1911. * (the configured address family sets node.ipv6_preferred)
  1912. * - other clients prefer IPv4 OR by default (see above),
  1913. * - all clients, including bridge clients, prefer IPv4 Dir by default.
  1914. */
  1915. mock_options.ClientPreferIPv6ORPort = -1;
  1916. mock_options.ClientPreferIPv6DirPort = -1;
  1917. /* Simulate the initialisation of fake_node.ipv6_preferred with a bridge
  1918. * configured with an IPv4 address */
  1919. fake_node.ipv6_preferred = 0;
  1920. CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_OR_CONNECTION, 0, 1, ipv4_or_ap);
  1921. CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_OR_CONNECTION, 1, 1, ipv4_or_ap);
  1922. CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
  1923. ipv4_dir_ap);
  1924. CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
  1925. ipv4_dir_ap);
  1926. /* Simulate the initialisation of fake_node.ipv6_preferred with a bridge
  1927. * configured with an IPv6 address */
  1928. fake_node.ipv6_preferred = 1;
  1929. CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_OR_CONNECTION, 0, 1, ipv6_or_ap);
  1930. CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_OR_CONNECTION, 1, 1, ipv6_or_ap);
  1931. CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
  1932. ipv4_dir_ap);
  1933. CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
  1934. ipv4_dir_ap);
  1935. /* When a rs has no node, it defaults to IPv4 under auto. */
  1936. CHECK_CHOSEN_ADDR_RS(fake_rs, FIREWALL_OR_CONNECTION, 0, 1, ipv4_or_ap);
  1937. CHECK_CHOSEN_ADDR_RS(fake_rs, FIREWALL_OR_CONNECTION, 1, 1, ipv4_or_ap);
  1938. CHECK_CHOSEN_ADDR_RS(fake_rs, FIREWALL_DIR_CONNECTION, 0, 1, ipv4_dir_ap);
  1939. CHECK_CHOSEN_ADDR_RS(fake_rs, FIREWALL_DIR_CONNECTION, 1, 1, ipv4_dir_ap);
  1940. /* Preferring IPv6 */
  1941. mock_options.ClientPreferIPv6ORPort = 1;
  1942. mock_options.ClientPreferIPv6DirPort = 1;
  1943. /* Simulate the initialisation of fake_node.ipv6_preferred */
  1944. fake_node.ipv6_preferred = fascist_firewall_prefer_ipv6_orport(
  1945. &mock_options);
  1946. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 0, 1,
  1947. ipv6_or_ap);
  1948. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 1, 1,
  1949. ipv6_or_ap);
  1950. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
  1951. ipv6_dir_ap);
  1952. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
  1953. ipv6_dir_ap);
  1954. /* In the default configuration (Auto / IPv6 off), bridge clients should
  1955. * use both IPv4 and IPv6, but only prefer IPv6 for bridges configured with
  1956. * an IPv6 address, regardless of ClientUseIPv6. (See above.) */
  1957. mock_options.ClientUseIPv6 = 0;
  1958. mock_options.ClientPreferIPv6ORPort = -1;
  1959. mock_options.ClientPreferIPv6DirPort = -1;
  1960. /* Simulate the initialisation of fake_node.ipv6_preferred with a bridge
  1961. * configured with an IPv4 address */
  1962. fake_node.ipv6_preferred = 0;
  1963. CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_OR_CONNECTION, 0, 1, ipv4_or_ap);
  1964. CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_OR_CONNECTION, 1, 1, ipv4_or_ap);
  1965. CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
  1966. ipv4_dir_ap);
  1967. CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
  1968. ipv4_dir_ap);
  1969. /* Simulate the initialisation of fake_node.ipv6_preferred with a bridge
  1970. * configured with an IPv6 address */
  1971. fake_node.ipv6_preferred = 1;
  1972. CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_OR_CONNECTION, 0, 1, ipv6_or_ap);
  1973. CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_OR_CONNECTION, 1, 1, ipv6_or_ap);
  1974. CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
  1975. ipv4_dir_ap);
  1976. CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
  1977. ipv4_dir_ap);
  1978. /* When a rs has no node, it defaults to IPv4 under auto. */
  1979. CHECK_CHOSEN_ADDR_RS(fake_rs, FIREWALL_OR_CONNECTION, 0, 1, ipv4_or_ap);
  1980. CHECK_CHOSEN_ADDR_RS(fake_rs, FIREWALL_OR_CONNECTION, 1, 1, ipv4_or_ap);
  1981. CHECK_CHOSEN_ADDR_RS(fake_rs, FIREWALL_DIR_CONNECTION, 0, 1, ipv4_dir_ap);
  1982. CHECK_CHOSEN_ADDR_RS(fake_rs, FIREWALL_DIR_CONNECTION, 1, 1, ipv4_dir_ap);
  1983. /* Choose an address with IPv4 on */
  1984. memset(&mock_options, 0, sizeof(or_options_t));
  1985. mock_options.ClientUseIPv4 = 1;
  1986. mock_options.ClientUseIPv6 = 0;
  1987. /* Simulate the initialisation of fake_node.ipv6_preferred */
  1988. fake_node.ipv6_preferred = fascist_firewall_prefer_ipv6_orport(
  1989. &mock_options);
  1990. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 0, 1,
  1991. ipv4_or_ap);
  1992. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 1, 1,
  1993. ipv4_or_ap);
  1994. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
  1995. ipv4_dir_ap);
  1996. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
  1997. ipv4_dir_ap);
  1998. /* Choose an address with IPv6 on */
  1999. memset(&mock_options, 0, sizeof(or_options_t));
  2000. mock_options.ClientUseIPv4 = 0;
  2001. mock_options.ClientUseIPv6 = 1;
  2002. /* Simulate the initialisation of fake_node.ipv6_preferred */
  2003. fake_node.ipv6_preferred = fascist_firewall_prefer_ipv6_orport(
  2004. &mock_options);
  2005. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 0, 1,
  2006. ipv6_or_ap);
  2007. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 1, 1,
  2008. ipv6_or_ap);
  2009. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
  2010. ipv6_dir_ap);
  2011. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
  2012. ipv6_dir_ap);
  2013. /* Choose an address with ClientUseIPv4 0.
  2014. * This means "use IPv6" regardless of the other settings. */
  2015. memset(&mock_options, 0, sizeof(or_options_t));
  2016. mock_options.ClientUseIPv4 = 0;
  2017. mock_options.ClientUseIPv6 = 0;
  2018. /* Simulate the initialisation of fake_node.ipv6_preferred */
  2019. fake_node.ipv6_preferred = fascist_firewall_prefer_ipv6_orport(
  2020. &mock_options);
  2021. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 0, 1,
  2022. ipv6_or_ap);
  2023. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 1, 1,
  2024. ipv6_or_ap);
  2025. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
  2026. ipv6_dir_ap);
  2027. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
  2028. ipv6_dir_ap);
  2029. /* Choose an address with ORPort_set 1 (server mode).
  2030. * This means "use IPv4" regardless of the other settings. */
  2031. memset(&mock_options, 0, sizeof(or_options_t));
  2032. mock_options.ORPort_set = 1;
  2033. mock_options.ClientUseIPv4 = 0;
  2034. mock_options.ClientUseIPv6 = 1;
  2035. mock_options.ClientPreferIPv6ORPort = 1;
  2036. mock_options.ClientPreferIPv6DirPort = 1;
  2037. /* Simulate the initialisation of fake_node.ipv6_preferred */
  2038. fake_node.ipv6_preferred = fascist_firewall_prefer_ipv6_orport(
  2039. &mock_options);
  2040. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 0, 1,
  2041. ipv4_or_ap);
  2042. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 1, 1,
  2043. ipv4_or_ap);
  2044. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
  2045. ipv4_dir_ap);
  2046. CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
  2047. ipv4_dir_ap);
  2048. done:
  2049. UNMOCK(get_options);
  2050. }
  2051. #undef TEST_IPV4_ADDR_STR
  2052. #undef TEST_IPV6_ADDR_STR
  2053. #undef TEST_IPV4_OR_PORT
  2054. #undef TEST_IPV4_DIR_PORT
  2055. #undef TEST_IPV6_OR_PORT
  2056. #undef TEST_IPV6_DIR_PORT
  2057. #undef CHECK_CHOSEN_ADDR_RS
  2058. #undef CHECK_CHOSEN_ADDR_NODE
  2059. #undef CHECK_CHOSEN_ADDR_RN
  2060. struct testcase_t policy_tests[] = {
  2061. { "router_dump_exit_policy_to_string", test_dump_exit_policy_to_string, 0,
  2062. NULL, NULL },
  2063. { "general", test_policies_general, 0, NULL, NULL },
  2064. { "getinfo_helper_policies", test_policies_getinfo_helper_policies, 0, NULL,
  2065. NULL },
  2066. { "reject_exit_address", test_policies_reject_exit_address, 0, NULL, NULL },
  2067. { "reject_interface_address", test_policies_reject_interface_address, 0,
  2068. NULL, NULL },
  2069. { "reject_port_address", test_policies_reject_port_address, 0, NULL, NULL },
  2070. { "fascist_firewall_allows_address",
  2071. test_policies_fascist_firewall_allows_address, 0, NULL, NULL },
  2072. { "fascist_firewall_choose_address",
  2073. test_policies_fascist_firewall_choose_address, 0, NULL, NULL },
  2074. END_OF_TESTCASES
  2075. };