test_policy.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /* Copyright (c) 2013, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include "or.h"
  4. #include "router.h"
  5. #include "routerparse.h"
  6. #include "policies.h"
  7. #include "test.h"
  8. /* Helper: assert that short_policy parses and writes back out as itself,
  9. or as <b>expected</b> if that's provided. */
  10. static void
  11. test_short_policy_parse(const char *input,
  12. const char *expected)
  13. {
  14. short_policy_t *short_policy = NULL;
  15. char *out = NULL;
  16. if (expected == NULL)
  17. expected = input;
  18. short_policy = parse_short_policy(input);
  19. tt_assert(short_policy);
  20. out = write_short_policy(short_policy);
  21. tt_str_op(out, ==, expected);
  22. done:
  23. tor_free(out);
  24. short_policy_free(short_policy);
  25. }
  26. /** Helper: Parse the exit policy string in <b>policy_str</b>, and make sure
  27. * that policies_summarize() produces the string <b>expected_summary</b> from
  28. * it. */
  29. static void
  30. test_policy_summary_helper(const char *policy_str,
  31. const char *expected_summary)
  32. {
  33. config_line_t line;
  34. smartlist_t *policy = smartlist_new();
  35. char *summary = NULL;
  36. char *summary_after = NULL;
  37. int r;
  38. short_policy_t *short_policy = NULL;
  39. line.key = (char*)"foo";
  40. line.value = (char *)policy_str;
  41. line.next = NULL;
  42. r = policies_parse_exit_policy(&line, &policy, 1, 0, 0, 1);
  43. test_eq(r, 0);
  44. summary = policy_summarize(policy, AF_INET);
  45. test_assert(summary != NULL);
  46. test_streq(summary, expected_summary);
  47. short_policy = parse_short_policy(summary);
  48. tt_assert(short_policy);
  49. summary_after = write_short_policy(short_policy);
  50. test_streq(summary, summary_after);
  51. done:
  52. tor_free(summary_after);
  53. tor_free(summary);
  54. if (policy)
  55. addr_policy_list_free(policy);
  56. short_policy_free(short_policy);
  57. }
  58. /** Run unit tests for generating summary lines of exit policies */
  59. static void
  60. test_policies_general(void *arg)
  61. {
  62. int i;
  63. smartlist_t *policy = NULL, *policy2 = NULL, *policy3 = NULL,
  64. *policy4 = NULL, *policy5 = NULL, *policy6 = NULL,
  65. *policy7 = NULL;
  66. addr_policy_t *p;
  67. tor_addr_t tar;
  68. config_line_t line;
  69. smartlist_t *sm = NULL;
  70. char *policy_str = NULL;
  71. short_policy_t *short_parsed = NULL;
  72. (void)arg;
  73. policy = smartlist_new();
  74. p = router_parse_addr_policy_item_from_string("reject 192.168.0.0/16:*",-1);
  75. test_assert(p != NULL);
  76. test_eq(ADDR_POLICY_REJECT, p->policy_type);
  77. tor_addr_from_ipv4h(&tar, 0xc0a80000u);
  78. test_eq(0, tor_addr_compare(&p->addr, &tar, CMP_EXACT));
  79. test_eq(16, p->maskbits);
  80. test_eq(1, p->prt_min);
  81. test_eq(65535, p->prt_max);
  82. smartlist_add(policy, p);
  83. tor_addr_from_ipv4h(&tar, 0x01020304u);
  84. test_assert(ADDR_POLICY_ACCEPTED ==
  85. compare_tor_addr_to_addr_policy(&tar, 2, policy));
  86. tor_addr_make_unspec(&tar);
  87. test_assert(ADDR_POLICY_PROBABLY_ACCEPTED ==
  88. compare_tor_addr_to_addr_policy(&tar, 2, policy));
  89. tor_addr_from_ipv4h(&tar, 0xc0a80102);
  90. test_assert(ADDR_POLICY_REJECTED ==
  91. compare_tor_addr_to_addr_policy(&tar, 2, policy));
  92. test_assert(0 == policies_parse_exit_policy(NULL, &policy2, 1, 1, 0, 1));
  93. test_assert(policy2);
  94. policy3 = smartlist_new();
  95. p = router_parse_addr_policy_item_from_string("reject *:*",-1);
  96. test_assert(p != NULL);
  97. smartlist_add(policy3, p);
  98. p = router_parse_addr_policy_item_from_string("accept *:*",-1);
  99. test_assert(p != NULL);
  100. smartlist_add(policy3, p);
  101. policy4 = smartlist_new();
  102. p = router_parse_addr_policy_item_from_string("accept *:443",-1);
  103. test_assert(p != NULL);
  104. smartlist_add(policy4, p);
  105. p = router_parse_addr_policy_item_from_string("accept *:443",-1);
  106. test_assert(p != NULL);
  107. smartlist_add(policy4, p);
  108. policy5 = smartlist_new();
  109. p = router_parse_addr_policy_item_from_string("reject 0.0.0.0/8:*",-1);
  110. test_assert(p != NULL);
  111. smartlist_add(policy5, p);
  112. p = router_parse_addr_policy_item_from_string("reject 169.254.0.0/16:*",-1);
  113. test_assert(p != NULL);
  114. smartlist_add(policy5, p);
  115. p = router_parse_addr_policy_item_from_string("reject 127.0.0.0/8:*",-1);
  116. test_assert(p != NULL);
  117. smartlist_add(policy5, p);
  118. p = router_parse_addr_policy_item_from_string("reject 192.168.0.0/16:*",-1);
  119. test_assert(p != NULL);
  120. smartlist_add(policy5, p);
  121. p = router_parse_addr_policy_item_from_string("reject 10.0.0.0/8:*",-1);
  122. test_assert(p != NULL);
  123. smartlist_add(policy5, p);
  124. p = router_parse_addr_policy_item_from_string("reject 172.16.0.0/12:*",-1);
  125. test_assert(p != NULL);
  126. smartlist_add(policy5, p);
  127. p = router_parse_addr_policy_item_from_string("reject 80.190.250.90:*",-1);
  128. test_assert(p != NULL);
  129. smartlist_add(policy5, p);
  130. p = router_parse_addr_policy_item_from_string("reject *:1-65534",-1);
  131. test_assert(p != NULL);
  132. smartlist_add(policy5, p);
  133. p = router_parse_addr_policy_item_from_string("reject *:65535",-1);
  134. test_assert(p != NULL);
  135. smartlist_add(policy5, p);
  136. p = router_parse_addr_policy_item_from_string("accept *:1-65535",-1);
  137. test_assert(p != NULL);
  138. smartlist_add(policy5, p);
  139. policy6 = smartlist_new();
  140. p = router_parse_addr_policy_item_from_string("accept 43.3.0.0/9:*",-1);
  141. test_assert(p != NULL);
  142. smartlist_add(policy6, p);
  143. policy7 = smartlist_new();
  144. p = router_parse_addr_policy_item_from_string("accept 0.0.0.0/8:*",-1);
  145. test_assert(p != NULL);
  146. smartlist_add(policy7, p);
  147. test_assert(!exit_policy_is_general_exit(policy));
  148. test_assert(exit_policy_is_general_exit(policy2));
  149. test_assert(!exit_policy_is_general_exit(NULL));
  150. test_assert(!exit_policy_is_general_exit(policy3));
  151. test_assert(!exit_policy_is_general_exit(policy4));
  152. test_assert(!exit_policy_is_general_exit(policy5));
  153. test_assert(!exit_policy_is_general_exit(policy6));
  154. test_assert(!exit_policy_is_general_exit(policy7));
  155. test_assert(cmp_addr_policies(policy, policy2));
  156. test_assert(cmp_addr_policies(policy, NULL));
  157. test_assert(!cmp_addr_policies(policy2, policy2));
  158. test_assert(!cmp_addr_policies(NULL, NULL));
  159. test_assert(!policy_is_reject_star(policy2, AF_INET));
  160. test_assert(policy_is_reject_star(policy, AF_INET));
  161. test_assert(policy_is_reject_star(NULL, AF_INET));
  162. addr_policy_list_free(policy);
  163. policy = NULL;
  164. /* make sure compacting logic works. */
  165. policy = NULL;
  166. line.key = (char*)"foo";
  167. line.value = (char*)"accept *:80,reject private:*,reject *:*";
  168. line.next = NULL;
  169. test_assert(0 == policies_parse_exit_policy(&line, &policy, 1, 0, 0, 1));
  170. test_assert(policy);
  171. //test_streq(policy->string, "accept *:80");
  172. //test_streq(policy->next->string, "reject *:*");
  173. test_eq(smartlist_len(policy), 4);
  174. /* test policy summaries */
  175. /* check if we properly ignore private IP addresses */
  176. test_policy_summary_helper("reject 192.168.0.0/16:*,"
  177. "reject 0.0.0.0/8:*,"
  178. "reject 10.0.0.0/8:*,"
  179. "accept *:10-30,"
  180. "accept *:90,"
  181. "reject *:*",
  182. "accept 10-30,90");
  183. /* check all accept policies, and proper counting of rejects */
  184. test_policy_summary_helper("reject 11.0.0.0/9:80,"
  185. "reject 12.0.0.0/9:80,"
  186. "reject 13.0.0.0/9:80,"
  187. "reject 14.0.0.0/9:80,"
  188. "accept *:*", "accept 1-65535");
  189. test_policy_summary_helper("reject 11.0.0.0/9:80,"
  190. "reject 12.0.0.0/9:80,"
  191. "reject 13.0.0.0/9:80,"
  192. "reject 14.0.0.0/9:80,"
  193. "reject 15.0.0.0:81,"
  194. "accept *:*", "accept 1-65535");
  195. test_policy_summary_helper("reject 11.0.0.0/9:80,"
  196. "reject 12.0.0.0/9:80,"
  197. "reject 13.0.0.0/9:80,"
  198. "reject 14.0.0.0/9:80,"
  199. "reject 15.0.0.0:80,"
  200. "accept *:*",
  201. "reject 80");
  202. /* no exits */
  203. test_policy_summary_helper("accept 11.0.0.0/9:80,"
  204. "reject *:*",
  205. "reject 1-65535");
  206. /* port merging */
  207. test_policy_summary_helper("accept *:80,"
  208. "accept *:81,"
  209. "accept *:100-110,"
  210. "accept *:111,"
  211. "reject *:*",
  212. "accept 80-81,100-111");
  213. /* border ports */
  214. test_policy_summary_helper("accept *:1,"
  215. "accept *:3,"
  216. "accept *:65535,"
  217. "reject *:*",
  218. "accept 1,3,65535");
  219. /* holes */
  220. test_policy_summary_helper("accept *:1,"
  221. "accept *:3,"
  222. "accept *:5,"
  223. "accept *:7,"
  224. "reject *:*",
  225. "accept 1,3,5,7");
  226. test_policy_summary_helper("reject *:1,"
  227. "reject *:3,"
  228. "reject *:5,"
  229. "reject *:7,"
  230. "accept *:*",
  231. "reject 1,3,5,7");
  232. /* Short policies with unrecognized formats should get accepted. */
  233. test_short_policy_parse("accept fred,2,3-5", "accept 2,3-5");
  234. test_short_policy_parse("accept 2,fred,3", "accept 2,3");
  235. test_short_policy_parse("accept 2,fred,3,bob", "accept 2,3");
  236. test_short_policy_parse("accept 2,-3,500-600", "accept 2,500-600");
  237. /* Short policies with nil entries are accepted too. */
  238. test_short_policy_parse("accept 1,,3", "accept 1,3");
  239. test_short_policy_parse("accept 100-200,,", "accept 100-200");
  240. test_short_policy_parse("reject ,1-10,,,,30-40", "reject 1-10,30-40");
  241. /* Try parsing various broken short policies */
  242. #define TT_BAD_SHORT_POLICY(s) \
  243. do { \
  244. tt_ptr_op(NULL, ==, (short_parsed = parse_short_policy((s)))); \
  245. } while (0)
  246. TT_BAD_SHORT_POLICY("accept 200-199");
  247. TT_BAD_SHORT_POLICY("");
  248. TT_BAD_SHORT_POLICY("rejekt 1,2,3");
  249. TT_BAD_SHORT_POLICY("reject ");
  250. TT_BAD_SHORT_POLICY("reject");
  251. TT_BAD_SHORT_POLICY("rej");
  252. TT_BAD_SHORT_POLICY("accept 2,3,100000");
  253. TT_BAD_SHORT_POLICY("accept 2,3x,4");
  254. TT_BAD_SHORT_POLICY("accept 2,3x,4");
  255. TT_BAD_SHORT_POLICY("accept 2-");
  256. TT_BAD_SHORT_POLICY("accept 2-x");
  257. TT_BAD_SHORT_POLICY("accept 1-,3");
  258. TT_BAD_SHORT_POLICY("accept 1-,3");
  259. /* Test a too-long policy. */
  260. {
  261. int i;
  262. char *policy = NULL;
  263. smartlist_t *chunks = smartlist_new();
  264. smartlist_add(chunks, tor_strdup("accept "));
  265. for (i=1; i<10000; ++i)
  266. smartlist_add_asprintf(chunks, "%d,", i);
  267. smartlist_add(chunks, tor_strdup("20000"));
  268. policy = smartlist_join_strings(chunks, "", 0, NULL);
  269. SMARTLIST_FOREACH(chunks, char *, ch, tor_free(ch));
  270. smartlist_free(chunks);
  271. short_parsed = parse_short_policy(policy);/* shouldn't be accepted */
  272. tor_free(policy);
  273. tt_ptr_op(NULL, ==, short_parsed);
  274. }
  275. /* truncation ports */
  276. sm = smartlist_new();
  277. for (i=1; i<2000; i+=2) {
  278. char buf[POLICY_BUF_LEN];
  279. tor_snprintf(buf, sizeof(buf), "reject *:%d", i);
  280. smartlist_add(sm, tor_strdup(buf));
  281. }
  282. smartlist_add(sm, tor_strdup("accept *:*"));
  283. policy_str = smartlist_join_strings(sm, ",", 0, NULL);
  284. test_policy_summary_helper( policy_str,
  285. "accept 2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,"
  286. "46,48,50,52,54,56,58,60,62,64,66,68,70,72,74,76,78,80,82,84,86,88,90,"
  287. "92,94,96,98,100,102,104,106,108,110,112,114,116,118,120,122,124,126,128,"
  288. "130,132,134,136,138,140,142,144,146,148,150,152,154,156,158,160,162,164,"
  289. "166,168,170,172,174,176,178,180,182,184,186,188,190,192,194,196,198,200,"
  290. "202,204,206,208,210,212,214,216,218,220,222,224,226,228,230,232,234,236,"
  291. "238,240,242,244,246,248,250,252,254,256,258,260,262,264,266,268,270,272,"
  292. "274,276,278,280,282,284,286,288,290,292,294,296,298,300,302,304,306,308,"
  293. "310,312,314,316,318,320,322,324,326,328,330,332,334,336,338,340,342,344,"
  294. "346,348,350,352,354,356,358,360,362,364,366,368,370,372,374,376,378,380,"
  295. "382,384,386,388,390,392,394,396,398,400,402,404,406,408,410,412,414,416,"
  296. "418,420,422,424,426,428,430,432,434,436,438,440,442,444,446,448,450,452,"
  297. "454,456,458,460,462,464,466,468,470,472,474,476,478,480,482,484,486,488,"
  298. "490,492,494,496,498,500,502,504,506,508,510,512,514,516,518,520,522");
  299. done:
  300. addr_policy_list_free(policy);
  301. addr_policy_list_free(policy2);
  302. addr_policy_list_free(policy3);
  303. addr_policy_list_free(policy4);
  304. addr_policy_list_free(policy5);
  305. addr_policy_list_free(policy6);
  306. addr_policy_list_free(policy7);
  307. tor_free(policy_str);
  308. if (sm) {
  309. SMARTLIST_FOREACH(sm, char *, s, tor_free(s));
  310. smartlist_free(sm);
  311. }
  312. short_policy_free(short_parsed);
  313. }
  314. static void
  315. test_dump_exit_policy_to_string(void *arg)
  316. {
  317. char *ep;
  318. addr_policy_t *policy_entry;
  319. routerinfo_t *ri = tor_malloc_zero(sizeof(routerinfo_t));
  320. (void)arg;
  321. ri->policy_is_reject_star = 1;
  322. ri->exit_policy = NULL; // expecting "reject *:*"
  323. ep = router_dump_exit_policy_to_string(ri,1,1);
  324. test_streq("reject *:*",ep);
  325. tor_free(ep);
  326. ri->exit_policy = smartlist_new();
  327. ri->policy_is_reject_star = 0;
  328. policy_entry = router_parse_addr_policy_item_from_string("accept *:*",-1);
  329. smartlist_add(ri->exit_policy,policy_entry);
  330. ep = router_dump_exit_policy_to_string(ri,1,1);
  331. test_streq("accept *:*",ep);
  332. tor_free(ep);
  333. policy_entry = router_parse_addr_policy_item_from_string("reject *:25",-1);
  334. smartlist_add(ri->exit_policy,policy_entry);
  335. ep = router_dump_exit_policy_to_string(ri,1,1);
  336. test_streq("accept *:*\nreject *:25",ep);
  337. tor_free(ep);
  338. policy_entry =
  339. router_parse_addr_policy_item_from_string("reject 8.8.8.8:*",-1);
  340. smartlist_add(ri->exit_policy,policy_entry);
  341. ep = router_dump_exit_policy_to_string(ri,1,1);
  342. test_streq("accept *:*\nreject *:25\nreject 8.8.8.8:*",ep);
  343. tor_free(ep);
  344. policy_entry =
  345. router_parse_addr_policy_item_from_string("reject6 [FC00::]/7:*",-1);
  346. smartlist_add(ri->exit_policy,policy_entry);
  347. ep = router_dump_exit_policy_to_string(ri,1,1);
  348. test_streq("accept *:*\nreject *:25\nreject 8.8.8.8:*\n"
  349. "reject6 [fc00::]/7:*",ep);
  350. tor_free(ep);
  351. policy_entry =
  352. router_parse_addr_policy_item_from_string("accept6 [c000::]/3:*",-1);
  353. smartlist_add(ri->exit_policy,policy_entry);
  354. ep = router_dump_exit_policy_to_string(ri,1,1);
  355. test_streq("accept *:*\nreject *:25\nreject 8.8.8.8:*\n"
  356. "reject6 [fc00::]/7:*\naccept6 [c000::]/3:*",ep);
  357. done:
  358. if (ri->exit_policy) {
  359. SMARTLIST_FOREACH(ri->exit_policy, addr_policy_t *,
  360. entry, addr_policy_free(entry));
  361. smartlist_free(ri->exit_policy);
  362. }
  363. tor_free(ri);
  364. tor_free(ep);
  365. }
  366. struct testcase_t policy_tests[] = {
  367. { "router_dump_exit_policy_to_string", test_dump_exit_policy_to_string, 0,
  368. NULL, NULL },
  369. { "general", test_policies_general, 0, NULL, NULL },
  370. END_OF_TESTCASES
  371. };