test_policy.c 15 KB

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