test_policy.c 95 KB

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