test_policy.c 21 KB

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