test_protover.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /* Copyright (c) 2016, 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_assert(elts == NULL);
  74. /* Missing equals sign in an entry */
  75. elts = parse_protocol_list("Link=4 Haprauxymatyve Desc=9");
  76. tt_assert(elts == NULL);
  77. /* Missing word. */
  78. elts = parse_protocol_list("Link=4 =3 Desc=9");
  79. tt_assert(elts == NULL);
  80. /* Broken numbers */
  81. elts = parse_protocol_list("Link=fred");
  82. tt_assert(elts == NULL);
  83. elts = parse_protocol_list("Link=1,fred");
  84. tt_assert(elts == NULL);
  85. elts = parse_protocol_list("Link=1,fred,3");
  86. tt_assert(elts == NULL);
  87. /* Broken range */
  88. elts = parse_protocol_list("Link=1,9-8,3");
  89. tt_assert(elts == NULL);
  90. done:
  91. ;
  92. }
  93. static void
  94. test_protover_vote(void *arg)
  95. {
  96. (void) arg;
  97. smartlist_t *lst = smartlist_new();
  98. char *result = protover_compute_vote(lst, 1);
  99. tt_str_op(result, OP_EQ, "");
  100. tor_free(result);
  101. smartlist_add(lst, (void*) "Foo=1-10,500 Bar=1,3-7,8");
  102. result = protover_compute_vote(lst, 1);
  103. tt_str_op(result, OP_EQ, "Bar=1,3-8 Foo=1-10,500");
  104. tor_free(result);
  105. smartlist_add(lst, (void*) "Quux=123-456,78 Bar=2-6,8 Foo=9");
  106. result = protover_compute_vote(lst, 1);
  107. tt_str_op(result, OP_EQ, "Bar=1-8 Foo=1-10,500 Quux=78,123-456");
  108. tor_free(result);
  109. result = protover_compute_vote(lst, 2);
  110. tt_str_op(result, OP_EQ, "Bar=3-6,8 Foo=9");
  111. tor_free(result);
  112. /* High threshold */
  113. result = protover_compute_vote(lst, 3);
  114. tt_str_op(result, OP_EQ, "");
  115. tor_free(result);
  116. /* Bad votes: the result must be empty */
  117. smartlist_clear(lst);
  118. smartlist_add(lst, (void*) "Faux=10-5");
  119. result = protover_compute_vote(lst, 1);
  120. tt_str_op(result, OP_EQ, "");
  121. tor_free(result);
  122. /* This fails, since "-0" is not valid. */
  123. smartlist_clear(lst);
  124. smartlist_add(lst, (void*) "Faux=-0");
  125. result = protover_compute_vote(lst, 1);
  126. tt_str_op(result, OP_EQ, "");
  127. tor_free(result);
  128. /* Vote large protover lists that are just below the threshold */
  129. /* Just below the threshold: Rust */
  130. smartlist_clear(lst);
  131. smartlist_add(lst, (void*) "Sleen=1-500");
  132. result = protover_compute_vote(lst, 1);
  133. tt_str_op(result, OP_EQ, "Sleen=1-500");
  134. tor_free(result);
  135. /* Just below the threshold: C */
  136. smartlist_clear(lst);
  137. smartlist_add(lst, (void*) "Sleen=1-65536");
  138. result = protover_compute_vote(lst, 1);
  139. tt_str_op(result, OP_EQ, "Sleen=1-65536");
  140. tor_free(result);
  141. /* Large protover lists that exceed the threshold */
  142. /* By adding two votes, C allows us to exceed the limit */
  143. smartlist_add(lst, (void*) "Sleen=1-65536");
  144. smartlist_add(lst, (void*) "Sleen=100000");
  145. result = protover_compute_vote(lst, 1);
  146. tt_str_op(result, OP_EQ, "Sleen=1-65536,100000");
  147. tor_free(result);
  148. /* Large integers */
  149. smartlist_clear(lst);
  150. smartlist_add(lst, (void*) "Sleen=4294967294");
  151. result = protover_compute_vote(lst, 1);
  152. tt_str_op(result, OP_EQ, "Sleen=4294967294");
  153. tor_free(result);
  154. /* This parses, but fails at the vote stage */
  155. smartlist_clear(lst);
  156. smartlist_add(lst, (void*) "Sleen=4294967295");
  157. result = protover_compute_vote(lst, 1);
  158. tt_str_op(result, OP_EQ, "");
  159. tor_free(result);
  160. smartlist_clear(lst);
  161. smartlist_add(lst, (void*) "Sleen=4294967296");
  162. result = protover_compute_vote(lst, 1);
  163. tt_str_op(result, OP_EQ, "");
  164. tor_free(result);
  165. done:
  166. tor_free(result);
  167. smartlist_free(lst);
  168. }
  169. static void
  170. test_protover_all_supported(void *arg)
  171. {
  172. (void)arg;
  173. char *msg = NULL;
  174. tt_assert(protover_all_supported(NULL, &msg));
  175. tt_assert(msg == NULL);
  176. tt_assert(protover_all_supported("", &msg));
  177. tt_assert(msg == NULL);
  178. // Some things that we do support
  179. tt_assert(protover_all_supported("Link=3-4", &msg));
  180. tt_assert(msg == NULL);
  181. tt_assert(protover_all_supported("Link=3-4 Desc=2", &msg));
  182. tt_assert(msg == NULL);
  183. // Some things we don't support
  184. tt_assert(! protover_all_supported("Wombat=9", &msg));
  185. tt_str_op(msg, OP_EQ, "Wombat=9");
  186. tor_free(msg);
  187. tt_assert(! protover_all_supported("Link=999", &msg));
  188. tt_str_op(msg, OP_EQ, "Link=999");
  189. tor_free(msg);
  190. // Mix of things we support and things we don't
  191. tt_assert(! protover_all_supported("Link=3-4 Wombat=9", &msg));
  192. tt_str_op(msg, OP_EQ, "Wombat=9");
  193. tor_free(msg);
  194. tt_assert(! protover_all_supported("Link=3-999", &msg));
  195. tt_str_op(msg, OP_EQ, "Link=3-999");
  196. tor_free(msg);
  197. /* CPU/RAM DoS loop: Rust only */
  198. tt_assert(! protover_all_supported("Sleen=0-2147483648", &msg));
  199. tt_str_op(msg, OP_EQ, "Sleen=0-2147483648");
  200. tor_free(msg);
  201. /* This case is allowed. */
  202. tt_assert(! protover_all_supported("Sleen=0-4294967294", &msg));
  203. tt_str_op(msg, OP_EQ, "Sleen=0-4294967294");
  204. tor_free(msg);
  205. /* If we get an unparseable list, we say "yes, that's supported." */
  206. #ifndef HAVE_RUST
  207. // XXXX let's make this section unconditional: rust should behave the
  208. // XXXX same as C here!
  209. tor_capture_bugs_(1);
  210. tt_assert(protover_all_supported("Fribble", &msg));
  211. tt_ptr_op(msg, OP_EQ, NULL);
  212. tor_end_capture_bugs_();
  213. /* This case is forbidden. Since it came from a protover_all_supported,
  214. * it can trigger a bug message. */
  215. tor_capture_bugs_(1);
  216. tt_assert(protover_all_supported("Sleen=0-4294967295", &msg));
  217. tt_ptr_op(msg, OP_EQ, NULL);
  218. tor_free(msg);
  219. tor_end_capture_bugs_();
  220. #endif
  221. done:
  222. tor_end_capture_bugs_();
  223. tor_free(msg);
  224. }
  225. static void
  226. test_protover_vote_roundtrip(void *args)
  227. {
  228. (void) args;
  229. static const struct {
  230. const char *input;
  231. const char *expected_output;
  232. } examples[] = {
  233. { "Fkrkljdsf", NULL },
  234. { "Zn=4294967295", NULL },
  235. { "Zn=4294967295-1", NULL },
  236. { "Zn=4294967293-4294967295", NULL },
  237. /* Will fail because of 4294967295. */
  238. { "Foo=1,3 Bar=3 Baz= Quux=9-12,14,15-16,900 Zn=0,4294967295",
  239. NULL },
  240. { "Foo=1,3 Bar=3 Baz= Quux=9-12,14,15-16,900 Zn=0,4294967294",
  241. "Bar=3 Foo=1,3 Quux=9-12,14-16,900 Zn=0,4294967294" },
  242. { "Zu16=0,65536", "Zu16=0,65536" },
  243. { "N-1=1,2", "N-1=1-2" },
  244. { "-1=4294967295", NULL },
  245. { "-1=3", "-1=3" },
  246. /* junk. */
  247. { "!!3@*", NULL },
  248. /* Missing equals sign */
  249. { "Link=4 Haprauxymatyve Desc=9", NULL },
  250. { "Link=4 Haprauxymatyve=7 Desc=9",
  251. "Desc=9 Haprauxymatyve=7 Link=4" },
  252. { "=10-11", NULL },
  253. { "X=10-11", "X=10-11" },
  254. { "Link=4 =3 Desc=9", NULL },
  255. { "Link=4 Z=3 Desc=9", "Desc=9 Link=4 Z=3" },
  256. { "Link=fred", NULL },
  257. { "Link=1,fred", NULL },
  258. { "Link=1,fred,3", NULL },
  259. { "Link=1,9-8,3", NULL },
  260. { "Faux=-0", NULL },
  261. { "Faux=0--0", NULL },
  262. // "These fail at the splitting stage in Rust, but the number parsing
  263. // stage in C."
  264. { "Faux=-1", NULL },
  265. { "Faux=-1-3", NULL },
  266. { "Faux=1--1", NULL },
  267. /* Large integers */
  268. { "Link=4294967296", NULL },
  269. /* Large range */
  270. { "Sleen=1-501", "Sleen=1-501" },
  271. { "Sleen=1-65537", NULL },
  272. /* CPU/RAM DoS Loop: Rust only. */
  273. { "Sleen=0-2147483648", NULL },
  274. /* Rust seems to experience an internal error here. */
  275. { "Sleen=0-4294967295", NULL },
  276. };
  277. unsigned u;
  278. smartlist_t *votes = smartlist_new();
  279. char *result = NULL;
  280. for (u = 0; u < ARRAY_LENGTH(examples); ++u) {
  281. const char *input = examples[u].input;
  282. const char *expected_output = examples[u].expected_output;
  283. smartlist_add(votes, (void*)input);
  284. result = protover_compute_vote(votes, 1);
  285. if (expected_output != NULL) {
  286. tt_str_op(result, OP_EQ, expected_output);
  287. } else {
  288. tt_str_op(result, OP_EQ, "");
  289. }
  290. smartlist_clear(votes);
  291. tor_free(result);
  292. }
  293. done:
  294. smartlist_free(votes);
  295. tor_free(result);
  296. }
  297. #define PV_TEST(name, flags) \
  298. { #name, test_protover_ ##name, (flags), NULL, NULL }
  299. struct testcase_t protover_tests[] = {
  300. PV_TEST(parse, 0),
  301. PV_TEST(parse_fail, 0),
  302. PV_TEST(vote, 0),
  303. PV_TEST(all_supported, 0),
  304. PV_TEST(vote_roundtrip, 0),
  305. END_OF_TESTCASES
  306. };