test_policy.c 19 KB

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