test_protover.c 20 KB

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