test_policy.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  1. /* Copyright (c) 2013-2015, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include "or.h"
  4. #define CONFIG_PRIVATE
  5. #include "config.h"
  6. #include "router.h"
  7. #include "routerparse.h"
  8. #define POLICIES_PRIVATE
  9. #include "policies.h"
  10. #include "test.h"
  11. /* Helper: assert that short_policy parses and writes back out as itself,
  12. or as <b>expected</b> if that's provided. */
  13. static void
  14. test_short_policy_parse(const char *input,
  15. const char *expected)
  16. {
  17. short_policy_t *short_policy = NULL;
  18. char *out = NULL;
  19. if (expected == NULL)
  20. expected = input;
  21. short_policy = parse_short_policy(input);
  22. tt_assert(short_policy);
  23. out = write_short_policy(short_policy);
  24. tt_str_op(out, OP_EQ, expected);
  25. done:
  26. tor_free(out);
  27. short_policy_free(short_policy);
  28. }
  29. /** Helper: Parse the exit policy string in <b>policy_str</b>, and make sure
  30. * that policies_summarize() produces the string <b>expected_summary</b> from
  31. * it. */
  32. static void
  33. test_policy_summary_helper(const char *policy_str,
  34. const char *expected_summary)
  35. {
  36. config_line_t line;
  37. smartlist_t *policy = smartlist_new();
  38. char *summary = NULL;
  39. char *summary_after = NULL;
  40. int r;
  41. short_policy_t *short_policy = NULL;
  42. line.key = (char*)"foo";
  43. line.value = (char *)policy_str;
  44. line.next = NULL;
  45. r = policies_parse_exit_policy(&line, &policy,
  46. EXIT_POLICY_IPV6_ENABLED |
  47. EXIT_POLICY_ADD_DEFAULT, NULL);
  48. tt_int_op(r,OP_EQ, 0);
  49. summary = policy_summarize(policy, AF_INET);
  50. tt_assert(summary != NULL);
  51. tt_str_op(summary,OP_EQ, expected_summary);
  52. short_policy = parse_short_policy(summary);
  53. tt_assert(short_policy);
  54. summary_after = write_short_policy(short_policy);
  55. tt_str_op(summary,OP_EQ, summary_after);
  56. done:
  57. tor_free(summary_after);
  58. tor_free(summary);
  59. if (policy)
  60. addr_policy_list_free(policy);
  61. short_policy_free(short_policy);
  62. }
  63. /** Run unit tests for generating summary lines of exit policies */
  64. static void
  65. test_policies_general(void *arg)
  66. {
  67. int i;
  68. smartlist_t *policy = NULL, *policy2 = NULL, *policy3 = NULL,
  69. *policy4 = NULL, *policy5 = NULL, *policy6 = NULL,
  70. *policy7 = NULL, *policy8 = NULL, *policy9 = NULL,
  71. *policy10 = NULL, *policy11 = NULL, *policy12 = NULL;
  72. addr_policy_t *p;
  73. tor_addr_t tar, tar2;
  74. smartlist_t *addr_list = NULL;
  75. config_line_t line;
  76. smartlist_t *sm = NULL;
  77. char *policy_str = NULL;
  78. short_policy_t *short_parsed = NULL;
  79. int malformed_list = -1;
  80. (void)arg;
  81. policy = smartlist_new();
  82. p = router_parse_addr_policy_item_from_string("reject 192.168.0.0/16:*", -1,
  83. &malformed_list);
  84. tt_assert(p != NULL);
  85. tt_int_op(ADDR_POLICY_REJECT,OP_EQ, p->policy_type);
  86. tor_addr_from_ipv4h(&tar, 0xc0a80000u);
  87. tt_int_op(0,OP_EQ, tor_addr_compare(&p->addr, &tar, CMP_EXACT));
  88. tt_int_op(16,OP_EQ, p->maskbits);
  89. tt_int_op(1,OP_EQ, p->prt_min);
  90. tt_int_op(65535,OP_EQ, p->prt_max);
  91. smartlist_add(policy, p);
  92. tor_addr_from_ipv4h(&tar, 0x01020304u);
  93. tt_assert(ADDR_POLICY_ACCEPTED ==
  94. compare_tor_addr_to_addr_policy(&tar, 2, policy));
  95. tor_addr_make_unspec(&tar);
  96. tt_assert(ADDR_POLICY_PROBABLY_ACCEPTED ==
  97. compare_tor_addr_to_addr_policy(&tar, 2, policy));
  98. tor_addr_from_ipv4h(&tar, 0xc0a80102);
  99. tt_assert(ADDR_POLICY_REJECTED ==
  100. compare_tor_addr_to_addr_policy(&tar, 2, policy));
  101. tt_int_op(0, OP_EQ, policies_parse_exit_policy(NULL, &policy2,
  102. EXIT_POLICY_IPV6_ENABLED |
  103. EXIT_POLICY_REJECT_PRIVATE |
  104. EXIT_POLICY_ADD_DEFAULT, NULL));
  105. tt_assert(policy2);
  106. tor_addr_from_ipv4h(&tar, 0x0306090cu);
  107. tor_addr_parse(&tar2, "[2000::1234]");
  108. addr_list = smartlist_new();
  109. smartlist_add(addr_list, &tar);
  110. smartlist_add(addr_list, &tar2);
  111. tt_int_op(0, OP_EQ, policies_parse_exit_policy(NULL, &policy12,
  112. EXIT_POLICY_IPV6_ENABLED |
  113. EXIT_POLICY_REJECT_PRIVATE |
  114. EXIT_POLICY_ADD_DEFAULT,
  115. addr_list));
  116. smartlist_free(addr_list);
  117. addr_list = NULL;
  118. tt_assert(policy12);
  119. policy3 = smartlist_new();
  120. p = router_parse_addr_policy_item_from_string("reject *:*", -1,
  121. &malformed_list);
  122. tt_assert(p != NULL);
  123. smartlist_add(policy3, p);
  124. p = router_parse_addr_policy_item_from_string("accept *:*", -1,
  125. &malformed_list);
  126. tt_assert(p != NULL);
  127. smartlist_add(policy3, p);
  128. policy4 = smartlist_new();
  129. p = router_parse_addr_policy_item_from_string("accept *:443", -1,
  130. &malformed_list);
  131. tt_assert(p != NULL);
  132. smartlist_add(policy4, p);
  133. p = router_parse_addr_policy_item_from_string("accept *:443", -1,
  134. &malformed_list);
  135. tt_assert(p != NULL);
  136. smartlist_add(policy4, p);
  137. policy5 = smartlist_new();
  138. p = router_parse_addr_policy_item_from_string("reject 0.0.0.0/8:*", -1,
  139. &malformed_list);
  140. tt_assert(p != NULL);
  141. smartlist_add(policy5, p);
  142. p = router_parse_addr_policy_item_from_string("reject 169.254.0.0/16:*", -1,
  143. &malformed_list);
  144. tt_assert(p != NULL);
  145. smartlist_add(policy5, p);
  146. p = router_parse_addr_policy_item_from_string("reject 127.0.0.0/8:*", -1,
  147. &malformed_list);
  148. tt_assert(p != NULL);
  149. smartlist_add(policy5, p);
  150. p = router_parse_addr_policy_item_from_string("reject 192.168.0.0/16:*",
  151. -1, &malformed_list);
  152. tt_assert(p != NULL);
  153. smartlist_add(policy5, p);
  154. p = router_parse_addr_policy_item_from_string("reject 10.0.0.0/8:*", -1,
  155. &malformed_list);
  156. tt_assert(p != NULL);
  157. smartlist_add(policy5, p);
  158. p = router_parse_addr_policy_item_from_string("reject 172.16.0.0/12:*", -1,
  159. &malformed_list);
  160. tt_assert(p != NULL);
  161. smartlist_add(policy5, p);
  162. p = router_parse_addr_policy_item_from_string("reject 80.190.250.90:*", -1,
  163. &malformed_list);
  164. tt_assert(p != NULL);
  165. smartlist_add(policy5, p);
  166. p = router_parse_addr_policy_item_from_string("reject *:1-65534", -1,
  167. &malformed_list);
  168. tt_assert(p != NULL);
  169. smartlist_add(policy5, p);
  170. p = router_parse_addr_policy_item_from_string("reject *:65535", -1,
  171. &malformed_list);
  172. tt_assert(p != NULL);
  173. smartlist_add(policy5, p);
  174. p = router_parse_addr_policy_item_from_string("accept *:1-65535", -1,
  175. &malformed_list);
  176. tt_assert(p != NULL);
  177. smartlist_add(policy5, p);
  178. policy6 = smartlist_new();
  179. p = router_parse_addr_policy_item_from_string("accept 43.3.0.0/9:*", -1,
  180. &malformed_list);
  181. tt_assert(p != NULL);
  182. smartlist_add(policy6, p);
  183. policy7 = smartlist_new();
  184. p = router_parse_addr_policy_item_from_string("accept 0.0.0.0/8:*", -1,
  185. &malformed_list);
  186. tt_assert(p != NULL);
  187. smartlist_add(policy7, p);
  188. tt_int_op(0, OP_EQ, policies_parse_exit_policy(NULL, &policy8,
  189. EXIT_POLICY_IPV6_ENABLED |
  190. EXIT_POLICY_REJECT_PRIVATE |
  191. EXIT_POLICY_ADD_DEFAULT,
  192. NULL));
  193. tt_assert(policy8);
  194. tt_int_op(0, OP_EQ, policies_parse_exit_policy(NULL, &policy9,
  195. EXIT_POLICY_REJECT_PRIVATE |
  196. EXIT_POLICY_ADD_DEFAULT,
  197. NULL));
  198. tt_assert(policy9);
  199. /* accept6 * and reject6 * produce IPv6 wildcards only */
  200. policy10 = smartlist_new();
  201. p = router_parse_addr_policy_item_from_string("accept6 *:*", -1,
  202. &malformed_list);
  203. tt_assert(p != NULL);
  204. smartlist_add(policy10, p);
  205. policy11 = smartlist_new();
  206. p = router_parse_addr_policy_item_from_string("reject6 *:*", -1,
  207. &malformed_list);
  208. tt_assert(p != NULL);
  209. smartlist_add(policy11, p);
  210. tt_assert(!exit_policy_is_general_exit(policy));
  211. tt_assert(exit_policy_is_general_exit(policy2));
  212. tt_assert(!exit_policy_is_general_exit(NULL));
  213. tt_assert(!exit_policy_is_general_exit(policy3));
  214. tt_assert(!exit_policy_is_general_exit(policy4));
  215. tt_assert(!exit_policy_is_general_exit(policy5));
  216. tt_assert(!exit_policy_is_general_exit(policy6));
  217. tt_assert(!exit_policy_is_general_exit(policy7));
  218. tt_assert(exit_policy_is_general_exit(policy8));
  219. tt_assert(exit_policy_is_general_exit(policy9));
  220. tt_assert(!exit_policy_is_general_exit(policy10));
  221. tt_assert(!exit_policy_is_general_exit(policy11));
  222. tt_assert(cmp_addr_policies(policy, policy2));
  223. tt_assert(cmp_addr_policies(policy, NULL));
  224. tt_assert(!cmp_addr_policies(policy2, policy2));
  225. tt_assert(!cmp_addr_policies(NULL, NULL));
  226. tt_assert(!policy_is_reject_star(policy2, AF_INET));
  227. tt_assert(policy_is_reject_star(policy, AF_INET));
  228. tt_assert(policy_is_reject_star(policy10, AF_INET));
  229. tt_assert(!policy_is_reject_star(policy10, AF_INET6));
  230. tt_assert(policy_is_reject_star(policy11, AF_INET));
  231. tt_assert(policy_is_reject_star(policy11, AF_INET6));
  232. tt_assert(policy_is_reject_star(NULL, AF_INET));
  233. tt_assert(policy_is_reject_star(NULL, AF_INET6));
  234. addr_policy_list_free(policy);
  235. policy = NULL;
  236. /* make sure compacting logic works. */
  237. policy = NULL;
  238. line.key = (char*)"foo";
  239. line.value = (char*)"accept *:80,reject private:*,reject *:*";
  240. line.next = NULL;
  241. tt_int_op(0, OP_EQ, policies_parse_exit_policy(&line,&policy,
  242. EXIT_POLICY_IPV6_ENABLED |
  243. EXIT_POLICY_ADD_DEFAULT, NULL));
  244. tt_assert(policy);
  245. //test_streq(policy->string, "accept *:80");
  246. //test_streq(policy->next->string, "reject *:*");
  247. tt_int_op(smartlist_len(policy),OP_EQ, 4);
  248. /* test policy summaries */
  249. /* check if we properly ignore private IP addresses */
  250. test_policy_summary_helper("reject 192.168.0.0/16:*,"
  251. "reject 0.0.0.0/8:*,"
  252. "reject 10.0.0.0/8:*,"
  253. "accept *:10-30,"
  254. "accept *:90,"
  255. "reject *:*",
  256. "accept 10-30,90");
  257. /* check all accept policies, and proper counting of rejects */
  258. test_policy_summary_helper("reject 11.0.0.0/9:80,"
  259. "reject 12.0.0.0/9:80,"
  260. "reject 13.0.0.0/9:80,"
  261. "reject 14.0.0.0/9:80,"
  262. "accept *:*", "accept 1-65535");
  263. test_policy_summary_helper("reject 11.0.0.0/9:80,"
  264. "reject 12.0.0.0/9:80,"
  265. "reject 13.0.0.0/9:80,"
  266. "reject 14.0.0.0/9:80,"
  267. "reject 15.0.0.0:81,"
  268. "accept *:*", "accept 1-65535");
  269. test_policy_summary_helper("reject 11.0.0.0/9:80,"
  270. "reject 12.0.0.0/9:80,"
  271. "reject 13.0.0.0/9:80,"
  272. "reject 14.0.0.0/9:80,"
  273. "reject 15.0.0.0:80,"
  274. "accept *:*",
  275. "reject 80");
  276. /* no exits */
  277. test_policy_summary_helper("accept 11.0.0.0/9:80,"
  278. "reject *:*",
  279. "reject 1-65535");
  280. /* port merging */
  281. test_policy_summary_helper("accept *:80,"
  282. "accept *:81,"
  283. "accept *:100-110,"
  284. "accept *:111,"
  285. "reject *:*",
  286. "accept 80-81,100-111");
  287. /* border ports */
  288. test_policy_summary_helper("accept *:1,"
  289. "accept *:3,"
  290. "accept *:65535,"
  291. "reject *:*",
  292. "accept 1,3,65535");
  293. /* holes */
  294. test_policy_summary_helper("accept *:1,"
  295. "accept *:3,"
  296. "accept *:5,"
  297. "accept *:7,"
  298. "reject *:*",
  299. "accept 1,3,5,7");
  300. test_policy_summary_helper("reject *:1,"
  301. "reject *:3,"
  302. "reject *:5,"
  303. "reject *:7,"
  304. "accept *:*",
  305. "reject 1,3,5,7");
  306. /* Short policies with unrecognized formats should get accepted. */
  307. test_short_policy_parse("accept fred,2,3-5", "accept 2,3-5");
  308. test_short_policy_parse("accept 2,fred,3", "accept 2,3");
  309. test_short_policy_parse("accept 2,fred,3,bob", "accept 2,3");
  310. test_short_policy_parse("accept 2,-3,500-600", "accept 2,500-600");
  311. /* Short policies with nil entries are accepted too. */
  312. test_short_policy_parse("accept 1,,3", "accept 1,3");
  313. test_short_policy_parse("accept 100-200,,", "accept 100-200");
  314. test_short_policy_parse("reject ,1-10,,,,30-40", "reject 1-10,30-40");
  315. /* Try parsing various broken short policies */
  316. #define TT_BAD_SHORT_POLICY(s) \
  317. do { \
  318. tt_ptr_op(NULL, OP_EQ, (short_parsed = parse_short_policy((s)))); \
  319. } while (0)
  320. TT_BAD_SHORT_POLICY("accept 200-199");
  321. TT_BAD_SHORT_POLICY("");
  322. TT_BAD_SHORT_POLICY("rejekt 1,2,3");
  323. TT_BAD_SHORT_POLICY("reject ");
  324. TT_BAD_SHORT_POLICY("reject");
  325. TT_BAD_SHORT_POLICY("rej");
  326. TT_BAD_SHORT_POLICY("accept 2,3,100000");
  327. TT_BAD_SHORT_POLICY("accept 2,3x,4");
  328. TT_BAD_SHORT_POLICY("accept 2,3x,4");
  329. TT_BAD_SHORT_POLICY("accept 2-");
  330. TT_BAD_SHORT_POLICY("accept 2-x");
  331. TT_BAD_SHORT_POLICY("accept 1-,3");
  332. TT_BAD_SHORT_POLICY("accept 1-,3");
  333. /* Make sure that IPv4 addresses are ignored in accept6/reject6 lines. */
  334. p = router_parse_addr_policy_item_from_string("accept6 1.2.3.4:*", -1,
  335. &malformed_list);
  336. tt_assert(p == NULL);
  337. tt_assert(!malformed_list);
  338. p = router_parse_addr_policy_item_from_string("reject6 2.4.6.0/24:*", -1,
  339. &malformed_list);
  340. tt_assert(p == NULL);
  341. tt_assert(!malformed_list);
  342. p = router_parse_addr_policy_item_from_string("accept6 *4:*", -1,
  343. &malformed_list);
  344. tt_assert(p == NULL);
  345. tt_assert(!malformed_list);
  346. /* Make sure malformed policies are detected as such. */
  347. p = router_parse_addr_policy_item_from_string("bad_token *4:*", -1,
  348. &malformed_list);
  349. tt_assert(p == NULL);
  350. tt_assert(malformed_list);
  351. p = router_parse_addr_policy_item_from_string("accept6 **:*", -1,
  352. &malformed_list);
  353. tt_assert(p == NULL);
  354. tt_assert(malformed_list);
  355. p = router_parse_addr_policy_item_from_string("accept */15:*", -1,
  356. &malformed_list);
  357. tt_assert(p == NULL);
  358. tt_assert(malformed_list);
  359. p = router_parse_addr_policy_item_from_string("reject6 */:*", -1,
  360. &malformed_list);
  361. tt_assert(p == NULL);
  362. tt_assert(malformed_list);
  363. p = router_parse_addr_policy_item_from_string("accept 127.0.0.1/33:*", -1,
  364. &malformed_list);
  365. tt_assert(p == NULL);
  366. tt_assert(malformed_list);
  367. p = router_parse_addr_policy_item_from_string("accept6 [::1]/129:*", -1,
  368. &malformed_list);
  369. tt_assert(p == NULL);
  370. tt_assert(malformed_list);
  371. p = router_parse_addr_policy_item_from_string("reject 8.8.8.8/-1:*", -1,
  372. &malformed_list);
  373. tt_assert(p == NULL);
  374. tt_assert(malformed_list);
  375. p = router_parse_addr_policy_item_from_string("reject 8.8.4.4:10-5", -1,
  376. &malformed_list);
  377. tt_assert(p == NULL);
  378. tt_assert(malformed_list);
  379. p = router_parse_addr_policy_item_from_string("reject 1.2.3.4:-1", -1,
  380. &malformed_list);
  381. tt_assert(p == NULL);
  382. tt_assert(malformed_list);
  383. /* Test a too-long policy. */
  384. {
  385. int i;
  386. char *policy = NULL;
  387. smartlist_t *chunks = smartlist_new();
  388. smartlist_add(chunks, tor_strdup("accept "));
  389. for (i=1; i<10000; ++i)
  390. smartlist_add_asprintf(chunks, "%d,", i);
  391. smartlist_add(chunks, tor_strdup("20000"));
  392. policy = smartlist_join_strings(chunks, "", 0, NULL);
  393. SMARTLIST_FOREACH(chunks, char *, ch, tor_free(ch));
  394. smartlist_free(chunks);
  395. short_parsed = parse_short_policy(policy);/* shouldn't be accepted */
  396. tor_free(policy);
  397. tt_ptr_op(NULL, OP_EQ, short_parsed);
  398. }
  399. /* truncation ports */
  400. sm = smartlist_new();
  401. for (i=1; i<2000; i+=2) {
  402. char buf[POLICY_BUF_LEN];
  403. tor_snprintf(buf, sizeof(buf), "reject *:%d", i);
  404. smartlist_add(sm, tor_strdup(buf));
  405. }
  406. smartlist_add(sm, tor_strdup("accept *:*"));
  407. policy_str = smartlist_join_strings(sm, ",", 0, NULL);
  408. test_policy_summary_helper( policy_str,
  409. "accept 2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,"
  410. "46,48,50,52,54,56,58,60,62,64,66,68,70,72,74,76,78,80,82,84,86,88,90,"
  411. "92,94,96,98,100,102,104,106,108,110,112,114,116,118,120,122,124,126,128,"
  412. "130,132,134,136,138,140,142,144,146,148,150,152,154,156,158,160,162,164,"
  413. "166,168,170,172,174,176,178,180,182,184,186,188,190,192,194,196,198,200,"
  414. "202,204,206,208,210,212,214,216,218,220,222,224,226,228,230,232,234,236,"
  415. "238,240,242,244,246,248,250,252,254,256,258,260,262,264,266,268,270,272,"
  416. "274,276,278,280,282,284,286,288,290,292,294,296,298,300,302,304,306,308,"
  417. "310,312,314,316,318,320,322,324,326,328,330,332,334,336,338,340,342,344,"
  418. "346,348,350,352,354,356,358,360,362,364,366,368,370,372,374,376,378,380,"
  419. "382,384,386,388,390,392,394,396,398,400,402,404,406,408,410,412,414,416,"
  420. "418,420,422,424,426,428,430,432,434,436,438,440,442,444,446,448,450,452,"
  421. "454,456,458,460,462,464,466,468,470,472,474,476,478,480,482,484,486,488,"
  422. "490,492,494,496,498,500,502,504,506,508,510,512,514,516,518,520,522");
  423. done:
  424. addr_policy_list_free(policy);
  425. addr_policy_list_free(policy2);
  426. addr_policy_list_free(policy3);
  427. addr_policy_list_free(policy4);
  428. addr_policy_list_free(policy5);
  429. addr_policy_list_free(policy6);
  430. addr_policy_list_free(policy7);
  431. addr_policy_list_free(policy8);
  432. addr_policy_list_free(policy9);
  433. addr_policy_list_free(policy10);
  434. addr_policy_list_free(policy11);
  435. addr_policy_list_free(policy12);
  436. tor_free(policy_str);
  437. if (sm) {
  438. SMARTLIST_FOREACH(sm, char *, s, tor_free(s));
  439. smartlist_free(sm);
  440. }
  441. short_policy_free(short_parsed);
  442. }
  443. /** Helper: Check that policy_list contains address */
  444. static int
  445. test_policy_has_address_helper(const smartlist_t *policy_list,
  446. const tor_addr_t *addr)
  447. {
  448. int found = 0;
  449. tt_assert(policy_list);
  450. tt_assert(addr);
  451. SMARTLIST_FOREACH_BEGIN(policy_list, addr_policy_t*, p) {
  452. if (tor_addr_eq(&p->addr, addr)) {
  453. found = 1;
  454. }
  455. } SMARTLIST_FOREACH_END(p);
  456. return found;
  457. done:
  458. return 0;
  459. }
  460. #define TEST_IPV4_ADDR (0x01020304)
  461. #define TEST_IPV6_ADDR ("2002::abcd")
  462. /** Run unit tests for rejecting the configured addresses on this exit relay
  463. * using policies_parse_exit_policy_reject_private */
  464. static void
  465. test_policies_reject_exit_address(void *arg)
  466. {
  467. smartlist_t *policy = NULL;
  468. tor_addr_t ipv4_addr, ipv6_addr;
  469. smartlist_t *ipv4_list, *ipv6_list, *both_list, *dupl_list;
  470. (void)arg;
  471. tor_addr_from_ipv4h(&ipv4_addr, TEST_IPV4_ADDR);
  472. tor_addr_parse(&ipv6_addr, TEST_IPV6_ADDR);
  473. ipv4_list = smartlist_new();
  474. ipv6_list = smartlist_new();
  475. both_list = smartlist_new();
  476. dupl_list = smartlist_new();
  477. smartlist_add(ipv4_list, &ipv4_addr);
  478. smartlist_add(both_list, &ipv4_addr);
  479. smartlist_add(dupl_list, &ipv4_addr);
  480. smartlist_add(dupl_list, &ipv4_addr);
  481. smartlist_add(dupl_list, &ipv4_addr);
  482. smartlist_add(ipv6_list, &ipv6_addr);
  483. smartlist_add(both_list, &ipv6_addr);
  484. smartlist_add(dupl_list, &ipv6_addr);
  485. smartlist_add(dupl_list, &ipv6_addr);
  486. /* IPv4-Only Exits */
  487. /* test that IPv4 addresses are rejected on an IPv4-only exit */
  488. policies_parse_exit_policy_reject_private(&policy, 0, ipv4_list, 0, 0);
  489. tt_assert(policy);
  490. tt_assert(smartlist_len(policy) == 1);
  491. tt_assert(test_policy_has_address_helper(policy, &ipv4_addr));
  492. addr_policy_list_free(policy);
  493. policy = NULL;
  494. /* test that IPv6 addresses are NOT rejected on an IPv4-only exit
  495. * (all IPv6 addresses are rejected by policies_parse_exit_policy_internal
  496. * on IPv4-only exits, so policies_parse_exit_policy_reject_private doesn't
  497. * need to do anything) */
  498. policies_parse_exit_policy_reject_private(&policy, 0, ipv6_list, 0, 0);
  499. tt_assert(policy == NULL);
  500. /* test that only IPv4 addresses are rejected on an IPv4-only exit */
  501. policies_parse_exit_policy_reject_private(&policy, 0, both_list, 0, 0);
  502. tt_assert(policy);
  503. tt_assert(smartlist_len(policy) == 1);
  504. tt_assert(test_policy_has_address_helper(policy, &ipv4_addr));
  505. addr_policy_list_free(policy);
  506. policy = NULL;
  507. /* Test that lists with duplicate entries produce the same results */
  508. policies_parse_exit_policy_reject_private(&policy, 0, dupl_list, 0, 0);
  509. tt_assert(policy);
  510. tt_assert(smartlist_len(policy) == 1);
  511. tt_assert(test_policy_has_address_helper(policy, &ipv4_addr));
  512. addr_policy_list_free(policy);
  513. policy = NULL;
  514. /* IPv4/IPv6 Exits */
  515. /* test that IPv4 addresses are rejected on an IPv4/IPv6 exit */
  516. policies_parse_exit_policy_reject_private(&policy, 1, ipv4_list, 0, 0);
  517. tt_assert(policy);
  518. tt_assert(smartlist_len(policy) == 1);
  519. tt_assert(test_policy_has_address_helper(policy, &ipv4_addr));
  520. addr_policy_list_free(policy);
  521. policy = NULL;
  522. /* test that IPv6 addresses are rejected on an IPv4/IPv6 exit */
  523. policies_parse_exit_policy_reject_private(&policy, 1, ipv6_list, 0, 0);
  524. tt_assert(policy);
  525. tt_assert(smartlist_len(policy) == 1);
  526. tt_assert(test_policy_has_address_helper(policy, &ipv6_addr));
  527. addr_policy_list_free(policy);
  528. policy = NULL;
  529. /* test that IPv4 and IPv6 addresses are rejected on an IPv4/IPv6 exit */
  530. policies_parse_exit_policy_reject_private(&policy, 1, both_list, 0, 0);
  531. tt_assert(policy);
  532. tt_assert(smartlist_len(policy) == 2);
  533. tt_assert(test_policy_has_address_helper(policy, &ipv4_addr));
  534. tt_assert(test_policy_has_address_helper(policy, &ipv6_addr));
  535. addr_policy_list_free(policy);
  536. policy = NULL;
  537. /* Test that lists with duplicate entries produce the same results */
  538. policies_parse_exit_policy_reject_private(&policy, 1, dupl_list, 0, 0);
  539. tt_assert(policy);
  540. tt_assert(smartlist_len(policy) == 2);
  541. tt_assert(test_policy_has_address_helper(policy, &ipv4_addr));
  542. tt_assert(test_policy_has_address_helper(policy, &ipv6_addr));
  543. addr_policy_list_free(policy);
  544. policy = NULL;
  545. done:
  546. addr_policy_list_free(policy);
  547. smartlist_free(ipv4_list);
  548. smartlist_free(ipv6_list);
  549. smartlist_free(both_list);
  550. smartlist_free(dupl_list);
  551. }
  552. static smartlist_t *test_configured_ports = NULL;
  553. const smartlist_t *mock_get_configured_ports(void);
  554. /** Returns test_configured_ports */
  555. const smartlist_t *
  556. mock_get_configured_ports(void)
  557. {
  558. return test_configured_ports;
  559. }
  560. /** Run unit tests for rejecting publicly routable configured port addresses
  561. * on this exit relay using policies_parse_exit_policy_reject_private */
  562. static void
  563. test_policies_reject_port_address(void *arg)
  564. {
  565. smartlist_t *policy = NULL;
  566. port_cfg_t *ipv4_port = NULL;
  567. port_cfg_t *ipv6_port = NULL;
  568. (void)arg;
  569. test_configured_ports = smartlist_new();
  570. ipv4_port = port_cfg_new(0);
  571. tor_addr_from_ipv4h(&ipv4_port->addr, TEST_IPV4_ADDR);
  572. smartlist_add(test_configured_ports, ipv4_port);
  573. ipv6_port = port_cfg_new(0);
  574. tor_addr_parse(&ipv6_port->addr, TEST_IPV6_ADDR);
  575. smartlist_add(test_configured_ports, ipv6_port);
  576. MOCK(get_configured_ports, mock_get_configured_ports);
  577. /* test that an IPv4 port is rejected on an IPv4-only exit, but an IPv6 port
  578. * is NOT rejected (all IPv6 addresses are rejected by
  579. * policies_parse_exit_policy_internal on IPv4-only exits, so
  580. * policies_parse_exit_policy_reject_private doesn't need to do anything
  581. * with IPv6 addresses on IPv4-only exits) */
  582. policies_parse_exit_policy_reject_private(&policy, 0, NULL, 0, 1);
  583. tt_assert(policy);
  584. tt_assert(smartlist_len(policy) == 1);
  585. tt_assert(test_policy_has_address_helper(policy, &ipv4_port->addr));
  586. addr_policy_list_free(policy);
  587. policy = NULL;
  588. /* test that IPv4 and IPv6 ports are rejected on an IPv4/IPv6 exit */
  589. policies_parse_exit_policy_reject_private(&policy, 1, NULL, 0, 1);
  590. tt_assert(policy);
  591. tt_assert(smartlist_len(policy) == 2);
  592. tt_assert(test_policy_has_address_helper(policy, &ipv4_port->addr));
  593. tt_assert(test_policy_has_address_helper(policy, &ipv6_port->addr));
  594. addr_policy_list_free(policy);
  595. policy = NULL;
  596. done:
  597. addr_policy_list_free(policy);
  598. if (test_configured_ports) {
  599. SMARTLIST_FOREACH(test_configured_ports,
  600. port_cfg_t *, p, port_cfg_free(p));
  601. smartlist_free(test_configured_ports);
  602. test_configured_ports = NULL;
  603. }
  604. UNMOCK(get_configured_ports);
  605. }
  606. smartlist_t *mock_ipv4_addrs = NULL;
  607. smartlist_t *mock_ipv6_addrs = NULL;
  608. /* mock get_interface_address6_list, returning a deep copy of the template
  609. * address list ipv4_interface_address_list or ipv6_interface_address_list */
  610. static smartlist_t *
  611. mock_get_interface_address6_list(int severity,
  612. sa_family_t family,
  613. int include_internal)
  614. {
  615. (void)severity;
  616. (void)include_internal;
  617. smartlist_t *clone_list = smartlist_new();
  618. smartlist_t *template_list = NULL;
  619. if (family == AF_INET) {
  620. template_list = mock_ipv4_addrs;
  621. } else if (family == AF_INET6) {
  622. template_list = mock_ipv6_addrs;
  623. } else {
  624. return NULL;
  625. }
  626. tt_assert(template_list);
  627. SMARTLIST_FOREACH_BEGIN(template_list, tor_addr_t *, src_addr) {
  628. tor_addr_t *dest_addr = malloc(sizeof(tor_addr_t));
  629. memset(dest_addr, 0, sizeof(*dest_addr));
  630. tor_addr_copy_tight(dest_addr, src_addr);
  631. smartlist_add(clone_list, dest_addr);
  632. } SMARTLIST_FOREACH_END(src_addr);
  633. return clone_list;
  634. done:
  635. free_interface_address6_list(clone_list);
  636. return NULL;
  637. }
  638. /** Run unit tests for rejecting publicly routable interface addresses on this
  639. * exit relay using policies_parse_exit_policy_reject_private */
  640. static void
  641. test_policies_reject_interface_address(void *arg)
  642. {
  643. smartlist_t *policy = NULL;
  644. smartlist_t *public_ipv4_addrs =
  645. get_interface_address6_list(LOG_INFO, AF_INET, 0);
  646. smartlist_t *public_ipv6_addrs =
  647. get_interface_address6_list(LOG_INFO, AF_INET6, 0);
  648. tor_addr_t ipv4_addr, ipv6_addr;
  649. (void)arg;
  650. /* test that no addresses are rejected when none are supplied/requested */
  651. policies_parse_exit_policy_reject_private(&policy, 0, NULL, 0, 0);
  652. tt_assert(policy == NULL);
  653. /* test that only IPv4 interface addresses are rejected on an IPv4-only exit
  654. * (and allow for duplicates)
  655. */
  656. policies_parse_exit_policy_reject_private(&policy, 0, NULL, 1, 0);
  657. if (policy) {
  658. tt_assert(smartlist_len(policy) <= smartlist_len(public_ipv4_addrs));
  659. addr_policy_list_free(policy);
  660. policy = NULL;
  661. }
  662. /* test that IPv4 and IPv6 interface addresses are rejected on an IPv4/IPv6
  663. * exit (and allow for duplicates) */
  664. policies_parse_exit_policy_reject_private(&policy, 1, NULL, 1, 0);
  665. if (policy) {
  666. tt_assert(smartlist_len(policy) <= (smartlist_len(public_ipv4_addrs)
  667. + smartlist_len(public_ipv6_addrs)));
  668. addr_policy_list_free(policy);
  669. policy = NULL;
  670. }
  671. /* Now do it all again, but mocked */
  672. tor_addr_from_ipv4h(&ipv4_addr, TEST_IPV4_ADDR);
  673. mock_ipv4_addrs = smartlist_new();
  674. smartlist_add(mock_ipv4_addrs, (void *)&ipv4_addr);
  675. tor_addr_parse(&ipv6_addr, TEST_IPV6_ADDR);
  676. mock_ipv6_addrs = smartlist_new();
  677. smartlist_add(mock_ipv6_addrs, (void *)&ipv6_addr);
  678. MOCK(get_interface_address6_list, mock_get_interface_address6_list);
  679. /* test that no addresses are rejected when none are supplied/requested */
  680. policies_parse_exit_policy_reject_private(&policy, 0, NULL, 0, 0);
  681. tt_assert(policy == NULL);
  682. /* test that only IPv4 interface addresses are rejected on an IPv4-only exit
  683. */
  684. policies_parse_exit_policy_reject_private(&policy, 0, NULL, 1, 0);
  685. tt_assert(policy);
  686. tt_assert(smartlist_len(policy) == smartlist_len(mock_ipv4_addrs));
  687. addr_policy_list_free(policy);
  688. policy = NULL;
  689. /* test that IPv4 and IPv6 interface addresses are rejected on an IPv4/IPv6
  690. * exit */
  691. policies_parse_exit_policy_reject_private(&policy, 1, NULL, 1, 0);
  692. tt_assert(policy);
  693. tt_assert(smartlist_len(policy) == (smartlist_len(mock_ipv4_addrs)
  694. + smartlist_len(mock_ipv6_addrs)));
  695. addr_policy_list_free(policy);
  696. policy = NULL;
  697. done:
  698. addr_policy_list_free(policy);
  699. free_interface_address6_list(public_ipv4_addrs);
  700. free_interface_address6_list(public_ipv6_addrs);
  701. UNMOCK(get_interface_address6_list);
  702. /* we don't use free_interface_address6_list on these lists because their
  703. * address pointers are stack-based */
  704. smartlist_free(mock_ipv4_addrs);
  705. smartlist_free(mock_ipv6_addrs);
  706. }
  707. #undef TEST_IPV4_ADDR
  708. #undef TEST_IPV6_ADDR
  709. static void
  710. test_dump_exit_policy_to_string(void *arg)
  711. {
  712. char *ep;
  713. addr_policy_t *policy_entry;
  714. int malformed_list = -1;
  715. routerinfo_t *ri = tor_malloc_zero(sizeof(routerinfo_t));
  716. (void)arg;
  717. ri->policy_is_reject_star = 1;
  718. ri->exit_policy = NULL; // expecting "reject *:*"
  719. ep = router_dump_exit_policy_to_string(ri,1,1);
  720. tt_str_op("reject *:*",OP_EQ, ep);
  721. tor_free(ep);
  722. ri->exit_policy = smartlist_new();
  723. ri->policy_is_reject_star = 0;
  724. policy_entry = router_parse_addr_policy_item_from_string("accept *:*", -1,
  725. &malformed_list);
  726. smartlist_add(ri->exit_policy,policy_entry);
  727. ep = router_dump_exit_policy_to_string(ri,1,1);
  728. tt_str_op("accept *:*",OP_EQ, ep);
  729. tor_free(ep);
  730. policy_entry = router_parse_addr_policy_item_from_string("reject *:25", -1,
  731. &malformed_list);
  732. smartlist_add(ri->exit_policy,policy_entry);
  733. ep = router_dump_exit_policy_to_string(ri,1,1);
  734. tt_str_op("accept *:*\nreject *:25",OP_EQ, ep);
  735. tor_free(ep);
  736. policy_entry =
  737. router_parse_addr_policy_item_from_string("reject 8.8.8.8:*", -1,
  738. &malformed_list);
  739. smartlist_add(ri->exit_policy,policy_entry);
  740. ep = router_dump_exit_policy_to_string(ri,1,1);
  741. tt_str_op("accept *:*\nreject *:25\nreject 8.8.8.8:*",OP_EQ, ep);
  742. tor_free(ep);
  743. policy_entry =
  744. router_parse_addr_policy_item_from_string("reject6 [FC00::]/7:*", -1,
  745. &malformed_list);
  746. smartlist_add(ri->exit_policy,policy_entry);
  747. ep = router_dump_exit_policy_to_string(ri,1,1);
  748. tt_str_op("accept *:*\nreject *:25\nreject 8.8.8.8:*\n"
  749. "reject6 [fc00::]/7:*",OP_EQ, ep);
  750. tor_free(ep);
  751. policy_entry =
  752. router_parse_addr_policy_item_from_string("accept6 [c000::]/3:*", -1,
  753. &malformed_list);
  754. smartlist_add(ri->exit_policy,policy_entry);
  755. ep = router_dump_exit_policy_to_string(ri,1,1);
  756. tt_str_op("accept *:*\nreject *:25\nreject 8.8.8.8:*\n"
  757. "reject6 [fc00::]/7:*\naccept6 [c000::]/3:*",OP_EQ, ep);
  758. done:
  759. if (ri->exit_policy) {
  760. SMARTLIST_FOREACH(ri->exit_policy, addr_policy_t *,
  761. entry, addr_policy_free(entry));
  762. smartlist_free(ri->exit_policy);
  763. }
  764. tor_free(ri);
  765. tor_free(ep);
  766. }
  767. static routerinfo_t *mock_desc_routerinfo = NULL;
  768. static const routerinfo_t *
  769. mock_router_get_my_routerinfo(void)
  770. {
  771. return mock_desc_routerinfo;
  772. }
  773. #define DEFAULT_POLICY_STRING "reject *:*"
  774. #define TEST_IPV4_ADDR (0x02040608)
  775. #define TEST_IPV6_ADDR ("2003::ef01")
  776. static or_options_t mock_options;
  777. static const or_options_t *
  778. mock_get_options(void)
  779. {
  780. return &mock_options;
  781. }
  782. /** Run unit tests for generating summary lines of exit policies */
  783. static void
  784. test_policies_getinfo_helper_policies(void *arg)
  785. {
  786. (void)arg;
  787. int rv = 0;
  788. size_t ipv4_len = 0, ipv6_len = 0;
  789. char *answer = NULL;
  790. const char *errmsg = NULL;
  791. routerinfo_t mock_my_routerinfo;
  792. rv = getinfo_helper_policies(NULL, "exit-policy/default", &answer, &errmsg);
  793. tt_assert(rv == 0);
  794. tt_assert(answer != NULL);
  795. tt_assert(strlen(answer) > 0);
  796. tor_free(answer);
  797. rv = getinfo_helper_policies(NULL, "exit-policy/reject-private/default",
  798. &answer, &errmsg);
  799. tt_assert(rv == 0);
  800. tt_assert(answer != NULL);
  801. tt_assert(strlen(answer) > 0);
  802. tor_free(answer);
  803. memset(&mock_my_routerinfo, 0, sizeof(routerinfo_t));
  804. MOCK(router_get_my_routerinfo, mock_router_get_my_routerinfo);
  805. mock_my_routerinfo.exit_policy = smartlist_new();
  806. mock_desc_routerinfo = &mock_my_routerinfo;
  807. memset(&mock_options, 0, sizeof(or_options_t));
  808. MOCK(get_options, mock_get_options);
  809. rv = getinfo_helper_policies(NULL, "exit-policy/reject-private/relay",
  810. &answer, &errmsg);
  811. tt_assert(rv == 0);
  812. tt_assert(answer != NULL);
  813. tt_assert(strlen(answer) == 0);
  814. tor_free(answer);
  815. rv = getinfo_helper_policies(NULL, "exit-policy/ipv4", &answer,
  816. &errmsg);
  817. tt_assert(rv == 0);
  818. tt_assert(answer != NULL);
  819. ipv4_len = strlen(answer);
  820. tt_assert(ipv4_len == 0 || ipv4_len == strlen(DEFAULT_POLICY_STRING));
  821. tt_assert(ipv4_len == 0 || !strcasecmp(answer, DEFAULT_POLICY_STRING));
  822. tor_free(answer);
  823. rv = getinfo_helper_policies(NULL, "exit-policy/ipv6", &answer,
  824. &errmsg);
  825. tt_assert(rv == 0);
  826. tt_assert(answer != NULL);
  827. ipv6_len = strlen(answer);
  828. tt_assert(ipv6_len == 0 || ipv6_len == strlen(DEFAULT_POLICY_STRING));
  829. tt_assert(ipv6_len == 0 || !strcasecmp(answer, DEFAULT_POLICY_STRING));
  830. tor_free(answer);
  831. rv = getinfo_helper_policies(NULL, "exit-policy/full", &answer,
  832. &errmsg);
  833. tt_assert(rv == 0);
  834. tt_assert(answer != NULL);
  835. /* It's either empty or it's the default */
  836. tt_assert(strlen(answer) == 0 || !strcasecmp(answer, DEFAULT_POLICY_STRING));
  837. tor_free(answer);
  838. mock_my_routerinfo.addr = TEST_IPV4_ADDR;
  839. tor_addr_parse(&mock_my_routerinfo.ipv6_addr, TEST_IPV6_ADDR);
  840. append_exit_policy_string(&mock_my_routerinfo.exit_policy, "accept *4:*");
  841. append_exit_policy_string(&mock_my_routerinfo.exit_policy, "reject *6:*");
  842. mock_options.IPv6Exit = 1;
  843. tor_addr_from_ipv4h(&mock_options.OutboundBindAddressIPv4_, TEST_IPV4_ADDR);
  844. tor_addr_parse(&mock_options.OutboundBindAddressIPv6_, TEST_IPV6_ADDR);
  845. rv = getinfo_helper_policies(NULL, "exit-policy/reject-private/relay",
  846. &answer, &errmsg);
  847. tt_assert(rv == 0);
  848. tt_assert(answer != NULL);
  849. tt_assert(strlen(answer) > 0);
  850. tor_free(answer);
  851. rv = getinfo_helper_policies(NULL, "exit-policy/ipv4", &answer,
  852. &errmsg);
  853. tt_assert(rv == 0);
  854. tt_assert(answer != NULL);
  855. ipv4_len = strlen(answer);
  856. tt_assert(ipv4_len > 0);
  857. tor_free(answer);
  858. rv = getinfo_helper_policies(NULL, "exit-policy/ipv6", &answer,
  859. &errmsg);
  860. tt_assert(rv == 0);
  861. tt_assert(answer != NULL);
  862. ipv6_len = strlen(answer);
  863. tt_assert(ipv6_len > 0);
  864. tor_free(answer);
  865. rv = getinfo_helper_policies(NULL, "exit-policy/full", &answer,
  866. &errmsg);
  867. tt_assert(rv == 0);
  868. tt_assert(answer != NULL);
  869. tt_assert(strlen(answer) > 0);
  870. tt_assert(strlen(answer) == ipv4_len + ipv6_len + 1);
  871. tor_free(answer);
  872. done:
  873. tor_free(answer);
  874. UNMOCK(get_options);
  875. UNMOCK(router_get_my_routerinfo);
  876. smartlist_free(mock_my_routerinfo.exit_policy);
  877. }
  878. #undef DEFAULT_POLICY_STRING
  879. #undef TEST_IPV4_ADDR
  880. #undef TEST_IPV6_ADDR
  881. struct testcase_t policy_tests[] = {
  882. { "router_dump_exit_policy_to_string", test_dump_exit_policy_to_string, 0,
  883. NULL, NULL },
  884. { "general", test_policies_general, 0, NULL, NULL },
  885. { "getinfo_helper_policies", test_policies_getinfo_helper_policies, 0, NULL,
  886. NULL },
  887. { "reject_exit_address", test_policies_reject_exit_address, 0, NULL, NULL },
  888. { "reject_interface_address", test_policies_reject_interface_address, 0,
  889. NULL, NULL },
  890. { "reject_port_address", test_policies_reject_port_address, 0, NULL, NULL },
  891. END_OF_TESTCASES
  892. };