test_protover.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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. #include "or.h"
  8. #include "connection_or.h"
  9. static void
  10. test_protover_parse(void *arg)
  11. {
  12. (void) arg;
  13. #ifdef HAVE_RUST
  14. /** This test is disabled on rust builds, because it only exists to test
  15. * internal C functions. */
  16. tt_skip();
  17. done:
  18. ;
  19. #else
  20. char *re_encoded = NULL;
  21. const char *orig = "Foo=1,3 Bar=3 Baz= Quux=9-12,14,15-16,900";
  22. smartlist_t *elts = parse_protocol_list(orig);
  23. tt_assert(elts);
  24. tt_int_op(smartlist_len(elts), OP_EQ, 4);
  25. const proto_entry_t *e;
  26. const proto_range_t *r;
  27. e = smartlist_get(elts, 0);
  28. tt_str_op(e->name, OP_EQ, "Foo");
  29. tt_int_op(smartlist_len(e->ranges), OP_EQ, 2);
  30. {
  31. r = smartlist_get(e->ranges, 0);
  32. tt_int_op(r->low, OP_EQ, 1);
  33. tt_int_op(r->high, OP_EQ, 1);
  34. r = smartlist_get(e->ranges, 1);
  35. tt_int_op(r->low, OP_EQ, 3);
  36. tt_int_op(r->high, OP_EQ, 3);
  37. }
  38. e = smartlist_get(elts, 1);
  39. tt_str_op(e->name, OP_EQ, "Bar");
  40. tt_int_op(smartlist_len(e->ranges), OP_EQ, 1);
  41. {
  42. r = smartlist_get(e->ranges, 0);
  43. tt_int_op(r->low, OP_EQ, 3);
  44. tt_int_op(r->high, OP_EQ, 3);
  45. }
  46. e = smartlist_get(elts, 2);
  47. tt_str_op(e->name, OP_EQ, "Baz");
  48. tt_int_op(smartlist_len(e->ranges), OP_EQ, 0);
  49. e = smartlist_get(elts, 3);
  50. tt_str_op(e->name, OP_EQ, "Quux");
  51. tt_int_op(smartlist_len(e->ranges), OP_EQ, 4);
  52. {
  53. r = smartlist_get(e->ranges, 0);
  54. tt_int_op(r->low, OP_EQ, 9);
  55. tt_int_op(r->high, OP_EQ, 12);
  56. r = smartlist_get(e->ranges, 1);
  57. tt_int_op(r->low, OP_EQ, 14);
  58. tt_int_op(r->high, OP_EQ, 14);
  59. r = smartlist_get(e->ranges, 2);
  60. tt_int_op(r->low, OP_EQ, 15);
  61. tt_int_op(r->high, OP_EQ, 16);
  62. r = smartlist_get(e->ranges, 3);
  63. tt_int_op(r->low, OP_EQ, 900);
  64. tt_int_op(r->high, OP_EQ, 900);
  65. }
  66. re_encoded = encode_protocol_list(elts);
  67. tt_assert(re_encoded);
  68. tt_str_op(re_encoded, OP_EQ, orig);
  69. done:
  70. if (elts)
  71. SMARTLIST_FOREACH(elts, proto_entry_t *, ent, proto_entry_free(ent));
  72. smartlist_free(elts);
  73. tor_free(re_encoded);
  74. #endif
  75. }
  76. static void
  77. test_protover_parse_fail(void *arg)
  78. {
  79. (void)arg;
  80. #ifdef HAVE_RUST
  81. /** This test is disabled on rust builds, because it only exists to test
  82. * internal C functions. */
  83. tt_skip();
  84. #else
  85. smartlist_t *elts;
  86. /* random junk */
  87. elts = parse_protocol_list("!!3@*");
  88. tt_ptr_op(elts, OP_EQ, NULL);
  89. /* Missing equals sign in an entry */
  90. elts = parse_protocol_list("Link=4 Haprauxymatyve Desc=9");
  91. tt_ptr_op(elts, OP_EQ, NULL);
  92. /* Missing word. */
  93. elts = parse_protocol_list("Link=4 =3 Desc=9");
  94. tt_ptr_op(elts, OP_EQ, NULL);
  95. /* Broken numbers */
  96. elts = parse_protocol_list("Link=fred");
  97. tt_ptr_op(elts, OP_EQ, NULL);
  98. elts = parse_protocol_list("Link=1,fred");
  99. tt_ptr_op(elts, OP_EQ, NULL);
  100. elts = parse_protocol_list("Link=1,fred,3");
  101. tt_ptr_op(elts, OP_EQ, NULL);
  102. /* Broken range */
  103. elts = parse_protocol_list("Link=1,9-8,3");
  104. tt_ptr_op(elts, OP_EQ, NULL);
  105. #endif
  106. done:
  107. ;
  108. }
  109. static void
  110. test_protover_vote(void *arg)
  111. {
  112. (void) arg;
  113. smartlist_t *lst = smartlist_new();
  114. char *result = protover_compute_vote(lst, 1);
  115. tt_str_op(result, OP_EQ, "");
  116. tor_free(result);
  117. smartlist_add(lst, (void*) "Foo=1-10,500 Bar=1,3-7,8");
  118. result = protover_compute_vote(lst, 1);
  119. tt_str_op(result, OP_EQ, "Bar=1,3-8 Foo=1-10,500");
  120. tor_free(result);
  121. smartlist_add(lst, (void*) "Quux=123-456,78 Bar=2-6,8 Foo=9");
  122. result = protover_compute_vote(lst, 1);
  123. tt_str_op(result, OP_EQ, "Bar=1-8 Foo=1-10,500 Quux=78,123-456");
  124. tor_free(result);
  125. result = protover_compute_vote(lst, 2);
  126. tt_str_op(result, OP_EQ, "Bar=3-6,8 Foo=9");
  127. tor_free(result);
  128. /* High threshold */
  129. result = protover_compute_vote(lst, 3);
  130. tt_str_op(result, OP_EQ, "");
  131. tor_free(result);
  132. /* Bad votes: the result must be empty */
  133. smartlist_clear(lst);
  134. smartlist_add(lst, (void*) "Faux=10-5");
  135. result = protover_compute_vote(lst, 1);
  136. tt_str_op(result, OP_EQ, "");
  137. tor_free(result);
  138. /* This fails, since "-0" is not valid. */
  139. smartlist_clear(lst);
  140. smartlist_add(lst, (void*) "Faux=-0");
  141. result = protover_compute_vote(lst, 1);
  142. tt_str_op(result, OP_EQ, "");
  143. tor_free(result);
  144. /* Vote large protover lists that are just below the threshold */
  145. /* Just below the threshold: Rust */
  146. smartlist_clear(lst);
  147. smartlist_add(lst, (void*) "Sleen=1-500");
  148. result = protover_compute_vote(lst, 1);
  149. tt_str_op(result, OP_EQ, "Sleen=1-500");
  150. tor_free(result);
  151. /* Just below the threshold: C */
  152. smartlist_clear(lst);
  153. smartlist_add(lst, (void*) "Sleen=1-65536");
  154. result = protover_compute_vote(lst, 1);
  155. tt_str_op(result, OP_EQ, "Sleen=1-65536");
  156. tor_free(result);
  157. /* Large protover lists that exceed the threshold */
  158. /* By adding two votes, C allows us to exceed the limit */
  159. smartlist_add(lst, (void*) "Sleen=1-65536");
  160. smartlist_add(lst, (void*) "Sleen=100000");
  161. result = protover_compute_vote(lst, 1);
  162. tt_str_op(result, OP_EQ, "Sleen=1-65536,100000");
  163. tor_free(result);
  164. /* Large integers */
  165. smartlist_clear(lst);
  166. smartlist_add(lst, (void*) "Sleen=4294967294");
  167. result = protover_compute_vote(lst, 1);
  168. tt_str_op(result, OP_EQ, "Sleen=4294967294");
  169. tor_free(result);
  170. /* This parses, but fails at the vote stage */
  171. smartlist_clear(lst);
  172. smartlist_add(lst, (void*) "Sleen=4294967295");
  173. result = protover_compute_vote(lst, 1);
  174. tt_str_op(result, OP_EQ, "");
  175. tor_free(result);
  176. smartlist_clear(lst);
  177. smartlist_add(lst, (void*) "Sleen=4294967296");
  178. result = protover_compute_vote(lst, 1);
  179. tt_str_op(result, OP_EQ, "");
  180. tor_free(result);
  181. done:
  182. tor_free(result);
  183. smartlist_free(lst);
  184. }
  185. static void
  186. test_protover_all_supported(void *arg)
  187. {
  188. (void)arg;
  189. char *msg = NULL;
  190. tt_assert(protover_all_supported(NULL, &msg));
  191. tt_ptr_op(msg, OP_EQ, NULL);
  192. tt_assert(protover_all_supported("", &msg));
  193. tt_ptr_op(msg, OP_EQ, NULL);
  194. // Some things that we do support
  195. tt_assert(protover_all_supported("Link=3-4", &msg));
  196. tt_ptr_op(msg, OP_EQ, NULL);
  197. tt_assert(protover_all_supported("Link=3-4 Desc=2", &msg));
  198. tt_ptr_op(msg, OP_EQ, NULL);
  199. // Some things we don't support
  200. tt_assert(! protover_all_supported("Wombat=9", &msg));
  201. tt_str_op(msg, OP_EQ, "Wombat=9");
  202. tor_free(msg);
  203. tt_assert(! protover_all_supported("Link=999", &msg));
  204. tt_str_op(msg, OP_EQ, "Link=999");
  205. tor_free(msg);
  206. // Mix of things we support and things we don't
  207. tt_assert(! protover_all_supported("Link=3-4 Wombat=9", &msg));
  208. tt_str_op(msg, OP_EQ, "Wombat=9");
  209. tor_free(msg);
  210. /* Mix of things we support and don't support within a single protocol
  211. * which we do support */
  212. tt_assert(! protover_all_supported("Link=3-999", &msg));
  213. tt_str_op(msg, OP_EQ, "Link=6-999");
  214. tor_free(msg);
  215. tt_assert(! protover_all_supported("Link=1-3,345-666", &msg));
  216. tt_str_op(msg, OP_EQ, "Link=345-666");
  217. tor_free(msg);
  218. tt_assert(! protover_all_supported("Link=1-3,5-12", &msg));
  219. tt_str_op(msg, OP_EQ, "Link=6-12");
  220. tor_free(msg);
  221. /* Mix of protocols we do support and some we don't, where the protocols
  222. * we do support have some versions we don't support. */
  223. tt_assert(! protover_all_supported("Link=1-3,5-12 Quokka=9000-9001", &msg));
  224. tt_str_op(msg, OP_EQ, "Link=6-12 Quokka=9000-9001");
  225. tor_free(msg);
  226. /* We shouldn't be able to DoS ourselves parsing a large range. */
  227. tt_assert(! protover_all_supported("Sleen=0-2147483648", &msg));
  228. tt_str_op(msg, OP_EQ, "Sleen=0-2147483648");
  229. tor_free(msg);
  230. /* This case is allowed. */
  231. tt_assert(! protover_all_supported("Sleen=0-4294967294", &msg));
  232. tt_str_op(msg, OP_EQ, "Sleen=0-4294967294");
  233. tor_free(msg);
  234. /* If we get a (barely) valid (but unsupported list, we say "yes, that's
  235. * supported." */
  236. tt_assert(protover_all_supported("Fribble=", &msg));
  237. tt_ptr_op(msg, OP_EQ, NULL);
  238. /* If we get a completely unparseable list, protover_all_supported should
  239. * hit a fatal assertion for BUG(entries == NULL). */
  240. tor_capture_bugs_(1);
  241. tt_assert(protover_all_supported("Fribble", &msg));
  242. tor_end_capture_bugs_();
  243. /* If we get a completely unparseable list, protover_all_supported should
  244. * hit a fatal assertion for BUG(entries == NULL). */
  245. tor_capture_bugs_(1);
  246. tt_assert(protover_all_supported("Sleen=0-4294967295", &msg));
  247. tor_end_capture_bugs_();
  248. done:
  249. tor_end_capture_bugs_();
  250. tor_free(msg);
  251. }
  252. static void
  253. test_protover_list_supports_protocol_returns_true(void *arg)
  254. {
  255. (void)arg;
  256. const char *protocols = "Link=1";
  257. int is_supported = protocol_list_supports_protocol(protocols, PRT_LINK, 1);
  258. tt_int_op(is_supported, OP_EQ, 1);
  259. done:
  260. ;
  261. }
  262. static void
  263. test_protover_list_supports_protocol_for_unsupported_returns_false(void *arg)
  264. {
  265. (void)arg;
  266. const char *protocols = "Link=1";
  267. int is_supported = protocol_list_supports_protocol(protocols, PRT_LINK, 10);
  268. tt_int_op(is_supported, OP_EQ, 0);
  269. done:
  270. ;
  271. }
  272. static void
  273. test_protover_supports_version(void *arg)
  274. {
  275. (void)arg;
  276. tt_assert(protocol_list_supports_protocol("Link=3-6", PRT_LINK, 3));
  277. tt_assert(protocol_list_supports_protocol("Link=3-6", PRT_LINK, 6));
  278. tt_assert(!protocol_list_supports_protocol("Link=3-6", PRT_LINK, 7));
  279. tt_assert(!protocol_list_supports_protocol("Link=3-6", PRT_LINKAUTH, 3));
  280. tt_assert(!protocol_list_supports_protocol("Link=4-6 LinkAuth=3",
  281. PRT_LINKAUTH, 2));
  282. tt_assert(protocol_list_supports_protocol("Link=4-6 LinkAuth=3",
  283. PRT_LINKAUTH, 3));
  284. tt_assert(!protocol_list_supports_protocol("Link=4-6 LinkAuth=3",
  285. PRT_LINKAUTH, 4));
  286. tt_assert(!protocol_list_supports_protocol_or_later("Link=4-6 LinkAuth=3",
  287. PRT_LINKAUTH, 4));
  288. tt_assert(protocol_list_supports_protocol_or_later("Link=4-6 LinkAuth=3",
  289. PRT_LINKAUTH, 3));
  290. tt_assert(protocol_list_supports_protocol_or_later("Link=4-6 LinkAuth=3",
  291. PRT_LINKAUTH, 2));
  292. tt_assert(!protocol_list_supports_protocol_or_later("Link=4-6 LinkAuth=3",
  293. PRT_DESC, 2));
  294. done:
  295. ;
  296. }
  297. /* This could be MAX_PROTOCOLS_TO_EXPAND, but that's not exposed by protover */
  298. #define MAX_PROTOCOLS_TO_TEST 1024
  299. /* LinkAuth and Relay protocol versions.
  300. * Hard-coded here, because they are not in the code, or not exposed in the
  301. * headers. */
  302. #define PROTOVER_LINKAUTH_V1 1
  303. #define PROTOVER_LINKAUTH_V3 3
  304. #define PROTOVER_RELAY_V1 1
  305. #define PROTOVER_RELAY_V2 2
  306. /* Highest supported HSv2 introduce protocol version.
  307. * Hard-coded here, because it does not appear anywhere in the code.
  308. * It's not clear if we actually support version 2, see #25068. */
  309. #define PROTOVER_HSINTRO_V2 3
  310. /* HSv2 Rend and HSDir protocol versions.
  311. * Hard-coded here, because they do not appear anywhere in the code. */
  312. #define PROTOVER_HS_RENDEZVOUS_POINT_V2 1
  313. #define PROTOVER_HSDIR_V2 1
  314. /* DirCache, Desc, Microdesc, and Cons protocol versions.
  315. * Hard-coded here, because they do not appear anywhere in the code. */
  316. #define PROTOVER_DIRCACHE_V1 1
  317. #define PROTOVER_DIRCACHE_V2 2
  318. #define PROTOVER_DESC_V1 1
  319. #define PROTOVER_DESC_V2 2
  320. #define PROTOVER_MICRODESC_V1 1
  321. #define PROTOVER_MICRODESC_V2 2
  322. #define PROTOVER_CONS_V1 1
  323. #define PROTOVER_CONS_V2 2
  324. /* Make sure we haven't forgotten any supported protocols */
  325. static void
  326. test_protover_supported_protocols(void *arg)
  327. {
  328. (void)arg;
  329. const char *supported_protocols = protover_get_supported_protocols();
  330. /* Test for new Link in the code, that hasn't been added to supported
  331. * protocols */
  332. tt_assert(protocol_list_supports_protocol(supported_protocols,
  333. PRT_LINK,
  334. MAX_LINK_PROTO));
  335. for (uint16_t i = 0; i < MAX_PROTOCOLS_TO_TEST; i++) {
  336. if (is_or_protocol_version_known(i)) {
  337. tt_assert(protocol_list_supports_protocol(supported_protocols,
  338. PRT_LINK,
  339. i));
  340. }
  341. }
  342. /* Legacy LinkAuth does not appear anywhere in the code. */
  343. tt_assert(protocol_list_supports_protocol(supported_protocols,
  344. PRT_LINKAUTH,
  345. PROTOVER_LINKAUTH_V1));
  346. /* Latest LinkAuth is not exposed in the headers. */
  347. tt_assert(protocol_list_supports_protocol(supported_protocols,
  348. PRT_LINKAUTH,
  349. PROTOVER_LINKAUTH_V3));
  350. /* Is there any way to test for new LinkAuth? */
  351. /* Relay protovers do not appear anywhere in the code. */
  352. tt_assert(protocol_list_supports_protocol(supported_protocols,
  353. PRT_RELAY,
  354. PROTOVER_RELAY_V1));
  355. tt_assert(protocol_list_supports_protocol(supported_protocols,
  356. PRT_RELAY,
  357. PROTOVER_RELAY_V2));
  358. /* Is there any way to test for new Relay? */
  359. /* We could test legacy HSIntro by calling rend_service_update_descriptor(),
  360. * and checking the protocols field. But that's unlikely to change, so
  361. * we just use a hard-coded value. */
  362. tt_assert(protocol_list_supports_protocol(supported_protocols,
  363. PRT_HSINTRO,
  364. PROTOVER_HSINTRO_V2));
  365. /* Test for HSv3 HSIntro */
  366. tt_assert(protocol_list_supports_protocol(supported_protocols,
  367. PRT_HSINTRO,
  368. PROTOVER_HS_INTRO_V3));
  369. /* Is there any way to test for new HSIntro? */
  370. /* Legacy HSRend does not appear anywhere in the code. */
  371. tt_assert(protocol_list_supports_protocol(supported_protocols,
  372. PRT_HSREND,
  373. PROTOVER_HS_RENDEZVOUS_POINT_V2));
  374. /* Test for HSv3 HSRend */
  375. tt_assert(protocol_list_supports_protocol(supported_protocols,
  376. PRT_HSREND,
  377. PROTOVER_HS_RENDEZVOUS_POINT_V3));
  378. /* Is there any way to test for new HSRend? */
  379. /* Legacy HSDir does not appear anywhere in the code. */
  380. tt_assert(protocol_list_supports_protocol(supported_protocols,
  381. PRT_HSDIR,
  382. PROTOVER_HSDIR_V2));
  383. /* Test for HSv3 HSDir */
  384. tt_assert(protocol_list_supports_protocol(supported_protocols,
  385. PRT_HSDIR,
  386. PROTOVER_HSDIR_V3));
  387. /* Is there any way to test for new HSDir? */
  388. /* No DirCache versions appear anywhere in the code. */
  389. tt_assert(protocol_list_supports_protocol(supported_protocols,
  390. PRT_DIRCACHE,
  391. PROTOVER_DIRCACHE_V1));
  392. tt_assert(protocol_list_supports_protocol(supported_protocols,
  393. PRT_DIRCACHE,
  394. PROTOVER_DIRCACHE_V2));
  395. /* Is there any way to test for new DirCache? */
  396. /* No Desc versions appear anywhere in the code. */
  397. tt_assert(protocol_list_supports_protocol(supported_protocols,
  398. PRT_DESC,
  399. PROTOVER_DESC_V1));
  400. tt_assert(protocol_list_supports_protocol(supported_protocols,
  401. PRT_DESC,
  402. PROTOVER_DESC_V2));
  403. /* Is there any way to test for new Desc? */
  404. /* No Microdesc versions appear anywhere in the code. */
  405. tt_assert(protocol_list_supports_protocol(supported_protocols,
  406. PRT_MICRODESC,
  407. PROTOVER_MICRODESC_V1));
  408. tt_assert(protocol_list_supports_protocol(supported_protocols,
  409. PRT_MICRODESC,
  410. PROTOVER_MICRODESC_V2));
  411. /* Is there any way to test for new Microdesc? */
  412. /* No Cons versions appear anywhere in the code. */
  413. tt_assert(protocol_list_supports_protocol(supported_protocols,
  414. PRT_CONS,
  415. PROTOVER_CONS_V1));
  416. tt_assert(protocol_list_supports_protocol(supported_protocols,
  417. PRT_CONS,
  418. PROTOVER_CONS_V2));
  419. /* Is there any way to test for new Cons? */
  420. done:
  421. ;
  422. }
  423. static void
  424. test_protover_vote_roundtrip(void *args)
  425. {
  426. (void) args;
  427. static const struct {
  428. const char *input;
  429. const char *expected_output;
  430. } examples[] = {
  431. { "Fkrkljdsf", NULL },
  432. { "Zn=4294967295", NULL },
  433. { "Zn=4294967295-1", NULL },
  434. { "Zn=4294967293-4294967295", NULL },
  435. /* Will fail because of 4294967295. */
  436. { "Foo=1,3 Bar=3 Baz= Quux=9-12,14,15-16,900 Zn=0,4294967295",
  437. NULL },
  438. { "Foo=1,3 Bar=3 Baz= Quux=9-12,14,15-16,900 Zn=0,4294967294",
  439. "Bar=3 Foo=1,3 Quux=9-12,14-16,900 Zn=0,4294967294" },
  440. { "Zu16=0,65536", "Zu16=0,65536" },
  441. { "N-1=1,2", "N-1=1-2" },
  442. { "-1=4294967295", NULL },
  443. { "-1=3", "-1=3" },
  444. /* junk. */
  445. { "!!3@*", NULL },
  446. /* Missing equals sign */
  447. { "Link=4 Haprauxymatyve Desc=9", NULL },
  448. { "Link=4 Haprauxymatyve=7 Desc=9",
  449. "Desc=9 Haprauxymatyve=7 Link=4" },
  450. { "=10-11", NULL },
  451. { "X=10-11", "X=10-11" },
  452. { "Link=4 =3 Desc=9", NULL },
  453. { "Link=4 Z=3 Desc=9", "Desc=9 Link=4 Z=3" },
  454. { "Link=fred", NULL },
  455. { "Link=1,fred", NULL },
  456. { "Link=1,fred,3", NULL },
  457. { "Link=1,9-8,3", NULL },
  458. { "Faux=-0", NULL },
  459. { "Faux=0--0", NULL },
  460. { "Faux=-1", NULL },
  461. { "Faux=-1-3", NULL },
  462. { "Faux=1--1", NULL },
  463. /* Large integers */
  464. { "Link=4294967296", NULL },
  465. /* Large range */
  466. { "Sleen=1-501", "Sleen=1-501" },
  467. { "Sleen=1-65537", NULL },
  468. /* Both C/Rust implementations should be able to handle this mild DoS. */
  469. { "Sleen=0-2147483648", NULL },
  470. /* Rust tests are built in debug mode, so ints are bounds-checked. */
  471. { "Sleen=0-4294967295", NULL },
  472. };
  473. unsigned u;
  474. smartlist_t *votes = smartlist_new();
  475. char *result = NULL;
  476. for (u = 0; u < ARRAY_LENGTH(examples); ++u) {
  477. const char *input = examples[u].input;
  478. const char *expected_output = examples[u].expected_output;
  479. smartlist_add(votes, (void*)input);
  480. result = protover_compute_vote(votes, 1);
  481. if (expected_output != NULL) {
  482. tt_str_op(result, OP_EQ, expected_output);
  483. } else {
  484. tt_str_op(result, OP_EQ, "");
  485. }
  486. smartlist_clear(votes);
  487. tor_free(result);
  488. }
  489. done:
  490. smartlist_free(votes);
  491. tor_free(result);
  492. }
  493. #define PV_TEST(name, flags) \
  494. { #name, test_protover_ ##name, (flags), NULL, NULL }
  495. struct testcase_t protover_tests[] = {
  496. PV_TEST(parse, 0),
  497. PV_TEST(parse_fail, 0),
  498. PV_TEST(vote, 0),
  499. PV_TEST(all_supported, 0),
  500. PV_TEST(list_supports_protocol_for_unsupported_returns_false, 0),
  501. PV_TEST(list_supports_protocol_returns_true, 0),
  502. PV_TEST(supports_version, 0),
  503. PV_TEST(supported_protocols, 0),
  504. PV_TEST(vote_roundtrip, 0),
  505. END_OF_TESTCASES
  506. };