test_protover.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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: Rust */
  143. #if 0 // XXXXX
  144. smartlist_clear(lst);
  145. smartlist_add(lst, (void*) "Sleen=1-500");
  146. smartlist_add(lst, (void*) "Sleen=1000");
  147. result = protover_compute_vote(lst, 1);
  148. tt_str_op(result, OP_EQ, "");
  149. tor_free(result);
  150. #endif
  151. /* By adding two votes, C allows us to exceed the limit */
  152. smartlist_add(lst, (void*) "Sleen=1-65536");
  153. smartlist_add(lst, (void*) "Sleen=100000");
  154. result = protover_compute_vote(lst, 1);
  155. tt_str_op(result, OP_EQ, "Sleen=1-65536,100000");
  156. tor_free(result);
  157. /* Large integers */
  158. smartlist_clear(lst);
  159. smartlist_add(lst, (void*) "Sleen=4294967294");
  160. result = protover_compute_vote(lst, 1);
  161. tt_str_op(result, OP_EQ, "Sleen=4294967294");
  162. tor_free(result);
  163. /* This parses, but fails at the vote stage */
  164. smartlist_clear(lst);
  165. smartlist_add(lst, (void*) "Sleen=4294967295");
  166. result = protover_compute_vote(lst, 1);
  167. tt_str_op(result, OP_EQ, ""); // XXXX "Sleen=4294967295");
  168. tor_free(result);
  169. smartlist_clear(lst);
  170. smartlist_add(lst, (void*) "Sleen=4294967296");
  171. result = protover_compute_vote(lst, 1);
  172. tt_str_op(result, OP_EQ, "");
  173. tor_free(result);
  174. done:
  175. tor_free(result);
  176. smartlist_free(lst);
  177. }
  178. static void
  179. test_protover_all_supported(void *arg)
  180. {
  181. (void)arg;
  182. char *msg = NULL;
  183. tt_assert(protover_all_supported(NULL, &msg));
  184. tt_assert(msg == NULL);
  185. tt_assert(protover_all_supported("", &msg));
  186. tt_assert(msg == NULL);
  187. // Some things that we do support
  188. tt_assert(protover_all_supported("Link=3-4", &msg));
  189. tt_assert(msg == NULL);
  190. tt_assert(protover_all_supported("Link=3-4 Desc=2", &msg));
  191. tt_assert(msg == NULL);
  192. // Some things we don't support
  193. tt_assert(! protover_all_supported("Wombat=9", &msg));
  194. tt_str_op(msg, OP_EQ, "Wombat=9");
  195. tor_free(msg);
  196. tt_assert(! protover_all_supported("Link=999", &msg));
  197. tt_str_op(msg, OP_EQ, "Link=999");
  198. tor_free(msg);
  199. // Mix of things we support and things we don't
  200. tt_assert(! protover_all_supported("Link=3-4 Wombat=9", &msg));
  201. tt_str_op(msg, OP_EQ, "Wombat=9");
  202. tor_free(msg);
  203. tt_assert(! protover_all_supported("Link=3-999", &msg));
  204. tt_str_op(msg, OP_EQ, "Link=3-999");
  205. tor_free(msg);
  206. /* CPU/RAM DoS loop: Rust only */
  207. tt_assert(! protover_all_supported("Sleen=0-2147483648", &msg));
  208. tt_str_op(msg, OP_EQ, "Sleen=0-2147483648");
  209. tor_free(msg);
  210. /* Rust seems to experience an internal error here */
  211. tt_assert(! protover_all_supported("Sleen=0-4294967295", &msg));
  212. tt_str_op(msg, OP_EQ, "Sleen=0-4294967295");
  213. tor_free(msg);
  214. done:
  215. tor_free(msg);
  216. }
  217. static void
  218. test_protover_vote_roundtrip(void *args)
  219. {
  220. (void) args;
  221. static const struct {
  222. const char *input;
  223. const char *expected_output;
  224. } examples[] = {
  225. { "Fkrkljdsf", NULL },
  226. { "Zn=4294967295", NULL },
  227. { "Zn=4294967295-1", NULL },
  228. { "Zn=4294967293-4294967295", NULL },
  229. /* Will fail because of 4294967295. */
  230. { "Foo=1,3 Bar=3 Baz= Quux=9-12,14,15-16,900 Zn=0,4294967295",
  231. NULL },
  232. { "Foo=1,3 Bar=3 Baz= Quux=9-12,14,15-16,900 Zn=0,4294967294",
  233. "Bar=3 Foo=1,3 Quux=9-12,14-16,900 Zn=0,4294967294" },
  234. { "Zu16=0,65536", "Zu16=0,65536" },
  235. { "N-1=1,2", "N-1=1-2" },
  236. { "-1=4294967295", NULL },
  237. { "-1=3", "-1=3" },
  238. /* junk. */
  239. { "!!3@*", NULL },
  240. /* Missing equals sign */
  241. { "Link=4 Haprauxymatyve Desc=9", NULL },
  242. { "Link=4 Haprauxymatyve=7 Desc=9",
  243. "Desc=9 Haprauxymatyve=7 Link=4" },
  244. { "=10-11", NULL },
  245. { "X=10-11", "X=10-11" },
  246. { "Link=4 =3 Desc=9", NULL },
  247. { "Link=4 Z=3 Desc=9", "Desc=9 Link=4 Z=3" },
  248. { "Link=fred", NULL },
  249. { "Link=1,fred", NULL },
  250. { "Link=1,fred,3", NULL },
  251. { "Link=1,9-8,3", NULL },
  252. { "Faux=-0", NULL },
  253. { "Faux=0--0", NULL },
  254. // "These fail at the splitting stage in Rust, but the number parsing
  255. // stage in C."
  256. { "Faux=-1", NULL },
  257. { "Faux=-1-3", NULL },
  258. { "Faux=1--1", NULL },
  259. /* Large integers */
  260. { "Link=4294967296", NULL },
  261. /* Large range */
  262. { "Sleen=1-501", "Sleen=1-501" },
  263. { "Sleen=1-65537", NULL },
  264. /* CPU/RAM DoS Loop: Rust only. */
  265. { "Sleen=0-2147483648", NULL },
  266. /* Rust seems to experience an internal error here. */
  267. { "Sleen=0-4294967295", NULL },
  268. };
  269. unsigned u;
  270. smartlist_t *votes = smartlist_new();
  271. char *result = NULL;
  272. for (u = 0; u < ARRAY_LENGTH(examples); ++u) {
  273. const char *input = examples[u].input;
  274. const char *expected_output = examples[u].expected_output;
  275. smartlist_add(votes, (void*)input);
  276. result = protover_compute_vote(votes, 1);
  277. if (expected_output != NULL) {
  278. tt_str_op(result, OP_EQ, expected_output);
  279. } else {
  280. tt_str_op(result, OP_EQ, "");
  281. }
  282. smartlist_clear(votes);
  283. tor_free(result);
  284. }
  285. done:
  286. smartlist_free(votes);
  287. tor_free(result);
  288. }
  289. #define PV_TEST(name, flags) \
  290. { #name, test_protover_ ##name, (flags), NULL, NULL }
  291. struct testcase_t protover_tests[] = {
  292. PV_TEST(parse, 0),
  293. PV_TEST(parse_fail, 0),
  294. PV_TEST(vote, 0),
  295. PV_TEST(all_supported, 0),
  296. PV_TEST(vote_roundtrip, 0),
  297. END_OF_TESTCASES
  298. };