test_protover.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /* Copyright (c) 2016-2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #define PROTOVER_PRIVATE
  4. #include "orconfig.h"
  5. #include "test.h"
  6. #include "protover.h"
  7. static void
  8. test_protover_parse(void *arg)
  9. {
  10. (void) arg;
  11. char *re_encoded = NULL;
  12. const char *orig = "Foo=1,3 Bar=3 Baz= Quux=9-12,14,15-16,900";
  13. smartlist_t *elts = parse_protocol_list(orig);
  14. tt_assert(elts);
  15. tt_int_op(smartlist_len(elts), OP_EQ, 4);
  16. const proto_entry_t *e;
  17. const proto_range_t *r;
  18. e = smartlist_get(elts, 0);
  19. tt_str_op(e->name, OP_EQ, "Foo");
  20. tt_int_op(smartlist_len(e->ranges), OP_EQ, 2);
  21. {
  22. r = smartlist_get(e->ranges, 0);
  23. tt_int_op(r->low, OP_EQ, 1);
  24. tt_int_op(r->high, OP_EQ, 1);
  25. r = smartlist_get(e->ranges, 1);
  26. tt_int_op(r->low, OP_EQ, 3);
  27. tt_int_op(r->high, OP_EQ, 3);
  28. }
  29. e = smartlist_get(elts, 1);
  30. tt_str_op(e->name, OP_EQ, "Bar");
  31. tt_int_op(smartlist_len(e->ranges), OP_EQ, 1);
  32. {
  33. r = smartlist_get(e->ranges, 0);
  34. tt_int_op(r->low, OP_EQ, 3);
  35. tt_int_op(r->high, OP_EQ, 3);
  36. }
  37. e = smartlist_get(elts, 2);
  38. tt_str_op(e->name, OP_EQ, "Baz");
  39. tt_int_op(smartlist_len(e->ranges), OP_EQ, 0);
  40. e = smartlist_get(elts, 3);
  41. tt_str_op(e->name, OP_EQ, "Quux");
  42. tt_int_op(smartlist_len(e->ranges), OP_EQ, 4);
  43. {
  44. r = smartlist_get(e->ranges, 0);
  45. tt_int_op(r->low, OP_EQ, 9);
  46. tt_int_op(r->high, OP_EQ, 12);
  47. r = smartlist_get(e->ranges, 1);
  48. tt_int_op(r->low, OP_EQ, 14);
  49. tt_int_op(r->high, OP_EQ, 14);
  50. r = smartlist_get(e->ranges, 2);
  51. tt_int_op(r->low, OP_EQ, 15);
  52. tt_int_op(r->high, OP_EQ, 16);
  53. r = smartlist_get(e->ranges, 3);
  54. tt_int_op(r->low, OP_EQ, 900);
  55. tt_int_op(r->high, OP_EQ, 900);
  56. }
  57. re_encoded = encode_protocol_list(elts);
  58. tt_assert(re_encoded);
  59. tt_str_op(re_encoded, OP_EQ, orig);
  60. done:
  61. if (elts)
  62. SMARTLIST_FOREACH(elts, proto_entry_t *, ent, proto_entry_free(ent));
  63. smartlist_free(elts);
  64. tor_free(re_encoded);
  65. }
  66. static void
  67. test_protover_parse_fail(void *arg)
  68. {
  69. (void)arg;
  70. smartlist_t *elts;
  71. /* random junk */
  72. elts = parse_protocol_list("!!3@*");
  73. tt_ptr_op(elts, OP_EQ, NULL);
  74. /* Missing equals sign in an entry */
  75. elts = parse_protocol_list("Link=4 Haprauxymatyve Desc=9");
  76. tt_ptr_op(elts, OP_EQ, NULL);
  77. /* Missing word. */
  78. elts = parse_protocol_list("Link=4 =3 Desc=9");
  79. tt_ptr_op(elts, OP_EQ, NULL);
  80. /* Broken numbers */
  81. elts = parse_protocol_list("Link=fred");
  82. tt_ptr_op(elts, OP_EQ, NULL);
  83. elts = parse_protocol_list("Link=1,fred");
  84. tt_ptr_op(elts, OP_EQ, NULL);
  85. elts = parse_protocol_list("Link=1,fred,3");
  86. tt_ptr_op(elts, OP_EQ, NULL);
  87. /* Broken range */
  88. elts = parse_protocol_list("Link=1,9-8,3");
  89. tt_ptr_op(elts, OP_EQ, NULL);
  90. /* Protocol name too long */
  91. elts = parse_protocol_list("DoSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  92. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  93. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
  94. tt_ptr_op(elts, OP_EQ, NULL);
  95. done:
  96. ;
  97. }
  98. static void
  99. test_protover_vote(void *arg)
  100. {
  101. (void) arg;
  102. smartlist_t *lst = smartlist_new();
  103. char *result = protover_compute_vote(lst, 1);
  104. tt_str_op(result, OP_EQ, "");
  105. tor_free(result);
  106. smartlist_add(lst, (void*) "Foo=1-10,500 Bar=1,3-7,8");
  107. result = protover_compute_vote(lst, 1);
  108. tt_str_op(result, OP_EQ, "Bar=1,3-8 Foo=1-10,500");
  109. tor_free(result);
  110. smartlist_add(lst, (void*) "Quux=123-456,78 Bar=2-6,8 Foo=9");
  111. result = protover_compute_vote(lst, 1);
  112. tt_str_op(result, OP_EQ, "Bar=1-8 Foo=1-10,500 Quux=78,123-456");
  113. tor_free(result);
  114. result = protover_compute_vote(lst, 2);
  115. tt_str_op(result, OP_EQ, "Bar=3-6,8 Foo=9");
  116. tor_free(result);
  117. /* High threshold */
  118. result = protover_compute_vote(lst, 3);
  119. tt_str_op(result, OP_EQ, "");
  120. tor_free(result);
  121. /* Bad votes: the result must be empty */
  122. smartlist_clear(lst);
  123. smartlist_add(lst, (void*) "Faux=10-5");
  124. result = protover_compute_vote(lst, 1);
  125. tt_str_op(result, OP_EQ, "");
  126. tor_free(result);
  127. /* This fails, since "-0" is not valid. */
  128. smartlist_clear(lst);
  129. smartlist_add(lst, (void*) "Faux=-0");
  130. result = protover_compute_vote(lst, 1);
  131. tt_str_op(result, OP_EQ, "");
  132. tor_free(result);
  133. /* Vote large protover lists that are just below the threshold */
  134. /* Just below the threshold: Rust */
  135. smartlist_clear(lst);
  136. smartlist_add(lst, (void*) "Sleen=1-500");
  137. result = protover_compute_vote(lst, 1);
  138. tt_str_op(result, OP_EQ, "Sleen=1-500");
  139. tor_free(result);
  140. /* Just below the threshold: C */
  141. smartlist_clear(lst);
  142. smartlist_add(lst, (void*) "Sleen=1-65536");
  143. result = protover_compute_vote(lst, 1);
  144. tt_str_op(result, OP_EQ, "Sleen=1-65536");
  145. tor_free(result);
  146. /* Large protover lists that exceed the threshold */
  147. /* By adding two votes, C allows us to exceed the limit */
  148. smartlist_add(lst, (void*) "Sleen=1-65536");
  149. smartlist_add(lst, (void*) "Sleen=100000");
  150. result = protover_compute_vote(lst, 1);
  151. tt_str_op(result, OP_EQ, "Sleen=1-65536,100000");
  152. tor_free(result);
  153. /* Large integers */
  154. smartlist_clear(lst);
  155. smartlist_add(lst, (void*) "Sleen=4294967294");
  156. result = protover_compute_vote(lst, 1);
  157. tt_str_op(result, OP_EQ, "Sleen=4294967294");
  158. tor_free(result);
  159. /* This parses, but fails at the vote stage */
  160. smartlist_clear(lst);
  161. smartlist_add(lst, (void*) "Sleen=4294967295");
  162. result = protover_compute_vote(lst, 1);
  163. tt_str_op(result, OP_EQ, "");
  164. tor_free(result);
  165. smartlist_clear(lst);
  166. smartlist_add(lst, (void*) "Sleen=4294967296");
  167. result = protover_compute_vote(lst, 1);
  168. tt_str_op(result, OP_EQ, "");
  169. tor_free(result);
  170. /* Protocol name too long */
  171. smartlist_clear(lst);
  172. smartlist_add(lst, (void*) "DoSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  173. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  174. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
  175. result = protover_compute_vote(lst, 1);
  176. tt_str_op(result, OP_EQ, "");
  177. tor_free(result);
  178. done:
  179. tor_free(result);
  180. smartlist_free(lst);
  181. }
  182. static void
  183. test_protover_all_supported(void *arg)
  184. {
  185. (void)arg;
  186. char *msg = NULL;
  187. tt_assert(protover_all_supported(NULL, &msg));
  188. tt_ptr_op(msg, OP_EQ, NULL);
  189. tt_assert(protover_all_supported("", &msg));
  190. tt_ptr_op(msg, OP_EQ, NULL);
  191. // Some things that we do support
  192. tt_assert(protover_all_supported("Link=3-4", &msg));
  193. tt_ptr_op(msg, OP_EQ, NULL);
  194. tt_assert(protover_all_supported("Link=3-4 Desc=2", &msg));
  195. tt_ptr_op(msg, OP_EQ, NULL);
  196. // Some things we don't support
  197. tt_assert(! protover_all_supported("Wombat=9", &msg));
  198. tt_str_op(msg, OP_EQ, "Wombat=9");
  199. tor_free(msg);
  200. tt_assert(! protover_all_supported("Link=999", &msg));
  201. tt_str_op(msg, OP_EQ, "Link=999");
  202. tor_free(msg);
  203. // Mix of things we support and things we don't
  204. tt_assert(! protover_all_supported("Link=3-4 Wombat=9", &msg));
  205. tt_str_op(msg, OP_EQ, "Wombat=9");
  206. tor_free(msg);
  207. tt_assert(! protover_all_supported("Link=3-999", &msg));
  208. tt_str_op(msg, OP_EQ, "Link=3-999");
  209. tor_free(msg);
  210. /* CPU/RAM DoS loop: Rust only */
  211. tt_assert(! protover_all_supported("Sleen=0-2147483648", &msg));
  212. tt_str_op(msg, OP_EQ, "Sleen=0-2147483648");
  213. tor_free(msg);
  214. /* This case is allowed. */
  215. tt_assert(! protover_all_supported("Sleen=0-4294967294", &msg));
  216. tt_str_op(msg, OP_EQ, "Sleen=0-4294967294");
  217. tor_free(msg);
  218. /* If we get an unparseable list, we say "yes, that's supported." */
  219. #ifndef HAVE_RUST
  220. // XXXX let's make this section unconditional: rust should behave the
  221. // XXXX same as C here!
  222. tor_capture_bugs_(1);
  223. tt_assert(protover_all_supported("Fribble", &msg));
  224. tt_ptr_op(msg, OP_EQ, NULL);
  225. tor_end_capture_bugs_();
  226. /* This case is forbidden. Since it came from a protover_all_supported,
  227. * it can trigger a bug message. */
  228. tor_capture_bugs_(1);
  229. tt_assert(protover_all_supported("Sleen=0-4294967295", &msg));
  230. tt_ptr_op(msg, OP_EQ, NULL);
  231. tor_free(msg);
  232. tor_end_capture_bugs_();
  233. #endif
  234. /* Protocol name too long */
  235. tor_capture_bugs_(1);
  236. tt_assert(protover_all_supported(
  237. "DoSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  238. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  239. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  240. "aaaaaaaaaaaa=1-65536", &msg));
  241. tor_end_capture_bugs_();
  242. done:
  243. tor_end_capture_bugs_();
  244. tor_free(msg);
  245. }
  246. static void
  247. test_protover_vote_roundtrip(void *args)
  248. {
  249. (void) args;
  250. static const struct {
  251. const char *input;
  252. const char *expected_output;
  253. } examples[] = {
  254. { "Fkrkljdsf", NULL },
  255. { "Zn=4294967295", NULL },
  256. { "Zn=4294967295-1", NULL },
  257. { "Zn=4294967293-4294967295", NULL },
  258. /* Will fail because of 4294967295. */
  259. { "Foo=1,3 Bar=3 Baz= Quux=9-12,14,15-16,900 Zn=0,4294967295",
  260. NULL },
  261. { "Foo=1,3 Bar=3 Baz= Quux=9-12,14,15-16,900 Zn=0,4294967294",
  262. "Bar=3 Foo=1,3 Quux=9-12,14-16,900 Zn=0,4294967294" },
  263. { "Zu16=0,65536", "Zu16=0,65536" },
  264. { "N-1=1,2", "N-1=1-2" },
  265. { "-1=4294967295", NULL },
  266. { "-1=3", "-1=3" },
  267. /* junk. */
  268. { "!!3@*", NULL },
  269. /* Missing equals sign */
  270. { "Link=4 Haprauxymatyve Desc=9", NULL },
  271. { "Link=4 Haprauxymatyve=7 Desc=9",
  272. "Desc=9 Haprauxymatyve=7 Link=4" },
  273. { "=10-11", NULL },
  274. { "X=10-11", "X=10-11" },
  275. { "Link=4 =3 Desc=9", NULL },
  276. { "Link=4 Z=3 Desc=9", "Desc=9 Link=4 Z=3" },
  277. { "Link=fred", NULL },
  278. { "Link=1,fred", NULL },
  279. { "Link=1,fred,3", NULL },
  280. { "Link=1,9-8,3", NULL },
  281. { "Faux=-0", NULL },
  282. { "Faux=0--0", NULL },
  283. // "These fail at the splitting stage in Rust, but the number parsing
  284. // stage in C."
  285. { "Faux=-1", NULL },
  286. { "Faux=-1-3", NULL },
  287. { "Faux=1--1", NULL },
  288. /* Large integers */
  289. { "Link=4294967296", NULL },
  290. /* Large range */
  291. { "Sleen=1-501", "Sleen=1-501" },
  292. { "Sleen=1-65537", NULL },
  293. /* CPU/RAM DoS Loop: Rust only. */
  294. { "Sleen=0-2147483648", NULL },
  295. /* Rust seems to experience an internal error here. */
  296. { "Sleen=0-4294967295", NULL },
  297. };
  298. unsigned u;
  299. smartlist_t *votes = smartlist_new();
  300. char *result = NULL;
  301. for (u = 0; u < ARRAY_LENGTH(examples); ++u) {
  302. const char *input = examples[u].input;
  303. const char *expected_output = examples[u].expected_output;
  304. smartlist_add(votes, (void*)input);
  305. result = protover_compute_vote(votes, 1);
  306. if (expected_output != NULL) {
  307. tt_str_op(result, OP_EQ, expected_output);
  308. } else {
  309. tt_str_op(result, OP_EQ, "");
  310. }
  311. smartlist_clear(votes);
  312. tor_free(result);
  313. }
  314. done:
  315. smartlist_free(votes);
  316. tor_free(result);
  317. }
  318. #define PV_TEST(name, flags) \
  319. { #name, test_protover_ ##name, (flags), NULL, NULL }
  320. struct testcase_t protover_tests[] = {
  321. PV_TEST(parse, 0),
  322. PV_TEST(parse_fail, 0),
  323. PV_TEST(vote, 0),
  324. PV_TEST(all_supported, 0),
  325. PV_TEST(vote_roundtrip, 0),
  326. END_OF_TESTCASES
  327. };