test_policy.c 16 KB

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