test_protover.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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. tor_capture_bugs_(1);
  263. tt_assert(protover_all_supported(
  264. "DoSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  265. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  266. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  267. "aaaaaaaaaaaa=1-65536", &msg));
  268. tor_end_capture_bugs_();
  269. done:
  270. tor_end_capture_bugs_();
  271. tor_free(msg);
  272. }
  273. static void
  274. test_protover_list_supports_protocol_returns_true(void *arg)
  275. {
  276. (void)arg;
  277. const char *protocols = "Link=1";
  278. int is_supported = protocol_list_supports_protocol(protocols, PRT_LINK, 1);
  279. tt_int_op(is_supported, OP_EQ, 1);
  280. done:
  281. ;
  282. }
  283. static void
  284. test_protover_list_supports_protocol_for_unsupported_returns_false(void *arg)
  285. {
  286. (void)arg;
  287. const char *protocols = "Link=1";
  288. int is_supported = protocol_list_supports_protocol(protocols, PRT_LINK, 10);
  289. tt_int_op(is_supported, OP_EQ, 0);
  290. done:
  291. ;
  292. }
  293. static void
  294. test_protover_supports_version(void *arg)
  295. {
  296. (void)arg;
  297. tt_assert(protocol_list_supports_protocol("Link=3-6", PRT_LINK, 3));
  298. tt_assert(protocol_list_supports_protocol("Link=3-6", PRT_LINK, 6));
  299. tt_assert(!protocol_list_supports_protocol("Link=3-6", PRT_LINK, 7));
  300. tt_assert(!protocol_list_supports_protocol("Link=3-6", PRT_LINKAUTH, 3));
  301. tt_assert(!protocol_list_supports_protocol("Link=4-6 LinkAuth=3",
  302. PRT_LINKAUTH, 2));
  303. tt_assert(protocol_list_supports_protocol("Link=4-6 LinkAuth=3",
  304. PRT_LINKAUTH, 3));
  305. tt_assert(!protocol_list_supports_protocol("Link=4-6 LinkAuth=3",
  306. PRT_LINKAUTH, 4));
  307. tt_assert(!protocol_list_supports_protocol_or_later("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, 3));
  311. tt_assert(protocol_list_supports_protocol_or_later("Link=4-6 LinkAuth=3",
  312. PRT_LINKAUTH, 2));
  313. tt_assert(!protocol_list_supports_protocol_or_later("Link=4-6 LinkAuth=3",
  314. PRT_DESC, 2));
  315. done:
  316. ;
  317. }
  318. /* This could be MAX_PROTOCOLS_TO_EXPAND, but that's not exposed by protover */
  319. #define MAX_PROTOCOLS_TO_TEST 1024
  320. /* LinkAuth and Relay protocol versions.
  321. * Hard-coded here, because they are not in the code, or not exposed in the
  322. * headers. */
  323. #define PROTOVER_LINKAUTH_V1 1
  324. #define PROTOVER_LINKAUTH_V3 3
  325. #define PROTOVER_RELAY_V1 1
  326. #define PROTOVER_RELAY_V2 2
  327. /* Highest supported HSv2 introduce protocol version.
  328. * Hard-coded here, because it does not appear anywhere in the code.
  329. * It's not clear if we actually support version 2, see #25068. */
  330. #define PROTOVER_HSINTRO_V2 3
  331. /* HSv2 Rend and HSDir protocol versions.
  332. * Hard-coded here, because they do not appear anywhere in the code. */
  333. #define PROTOVER_HS_RENDEZVOUS_POINT_V2 1
  334. #define PROTOVER_HSDIR_V2 1
  335. /* DirCache, Desc, Microdesc, and Cons protocol versions.
  336. * Hard-coded here, because they do not appear anywhere in the code. */
  337. #define PROTOVER_DIRCACHE_V1 1
  338. #define PROTOVER_DIRCACHE_V2 2
  339. #define PROTOVER_DESC_V1 1
  340. #define PROTOVER_DESC_V2 2
  341. #define PROTOVER_MICRODESC_V1 1
  342. #define PROTOVER_MICRODESC_V2 2
  343. #define PROTOVER_CONS_V1 1
  344. #define PROTOVER_CONS_V2 2
  345. /* Make sure we haven't forgotten any supported protocols */
  346. static void
  347. test_protover_supported_protocols(void *arg)
  348. {
  349. (void)arg;
  350. const char *supported_protocols = protover_get_supported_protocols();
  351. /* Test for new Link in the code, that hasn't been added to supported
  352. * protocols */
  353. tt_assert(protocol_list_supports_protocol(supported_protocols,
  354. PRT_LINK,
  355. MAX_LINK_PROTO));
  356. for (uint16_t i = 0; i < MAX_PROTOCOLS_TO_TEST; i++) {
  357. if (is_or_protocol_version_known(i)) {
  358. tt_assert(protocol_list_supports_protocol(supported_protocols,
  359. PRT_LINK,
  360. i));
  361. }
  362. }
  363. /* Legacy LinkAuth does not appear anywhere in the code. */
  364. tt_assert(protocol_list_supports_protocol(supported_protocols,
  365. PRT_LINKAUTH,
  366. PROTOVER_LINKAUTH_V1));
  367. /* Latest LinkAuth is not exposed in the headers. */
  368. tt_assert(protocol_list_supports_protocol(supported_protocols,
  369. PRT_LINKAUTH,
  370. PROTOVER_LINKAUTH_V3));
  371. /* Is there any way to test for new LinkAuth? */
  372. /* Relay protovers do not appear anywhere in the code. */
  373. tt_assert(protocol_list_supports_protocol(supported_protocols,
  374. PRT_RELAY,
  375. PROTOVER_RELAY_V1));
  376. tt_assert(protocol_list_supports_protocol(supported_protocols,
  377. PRT_RELAY,
  378. PROTOVER_RELAY_V2));
  379. /* Is there any way to test for new Relay? */
  380. /* We could test legacy HSIntro by calling rend_service_update_descriptor(),
  381. * and checking the protocols field. But that's unlikely to change, so
  382. * we just use a hard-coded value. */
  383. tt_assert(protocol_list_supports_protocol(supported_protocols,
  384. PRT_HSINTRO,
  385. PROTOVER_HSINTRO_V2));
  386. /* Test for HSv3 HSIntro */
  387. tt_assert(protocol_list_supports_protocol(supported_protocols,
  388. PRT_HSINTRO,
  389. PROTOVER_HS_INTRO_V3));
  390. /* Is there any way to test for new HSIntro? */
  391. /* Legacy HSRend does not appear anywhere in the code. */
  392. tt_assert(protocol_list_supports_protocol(supported_protocols,
  393. PRT_HSREND,
  394. PROTOVER_HS_RENDEZVOUS_POINT_V2));
  395. /* Test for HSv3 HSRend */
  396. tt_assert(protocol_list_supports_protocol(supported_protocols,
  397. PRT_HSREND,
  398. PROTOVER_HS_RENDEZVOUS_POINT_V3));
  399. /* Is there any way to test for new HSRend? */
  400. /* Legacy HSDir does not appear anywhere in the code. */
  401. tt_assert(protocol_list_supports_protocol(supported_protocols,
  402. PRT_HSDIR,
  403. PROTOVER_HSDIR_V2));
  404. /* Test for HSv3 HSDir */
  405. tt_assert(protocol_list_supports_protocol(supported_protocols,
  406. PRT_HSDIR,
  407. PROTOVER_HSDIR_V3));
  408. /* Is there any way to test for new HSDir? */
  409. /* No DirCache versions appear anywhere in the code. */
  410. tt_assert(protocol_list_supports_protocol(supported_protocols,
  411. PRT_DIRCACHE,
  412. PROTOVER_DIRCACHE_V1));
  413. tt_assert(protocol_list_supports_protocol(supported_protocols,
  414. PRT_DIRCACHE,
  415. PROTOVER_DIRCACHE_V2));
  416. /* Is there any way to test for new DirCache? */
  417. /* No Desc versions appear anywhere in the code. */
  418. tt_assert(protocol_list_supports_protocol(supported_protocols,
  419. PRT_DESC,
  420. PROTOVER_DESC_V1));
  421. tt_assert(protocol_list_supports_protocol(supported_protocols,
  422. PRT_DESC,
  423. PROTOVER_DESC_V2));
  424. /* Is there any way to test for new Desc? */
  425. /* No Microdesc versions appear anywhere in the code. */
  426. tt_assert(protocol_list_supports_protocol(supported_protocols,
  427. PRT_MICRODESC,
  428. PROTOVER_MICRODESC_V1));
  429. tt_assert(protocol_list_supports_protocol(supported_protocols,
  430. PRT_MICRODESC,
  431. PROTOVER_MICRODESC_V2));
  432. /* Is there any way to test for new Microdesc? */
  433. /* No Cons versions appear anywhere in the code. */
  434. tt_assert(protocol_list_supports_protocol(supported_protocols,
  435. PRT_CONS,
  436. PROTOVER_CONS_V1));
  437. tt_assert(protocol_list_supports_protocol(supported_protocols,
  438. PRT_CONS,
  439. PROTOVER_CONS_V2));
  440. /* Is there any way to test for new Cons? */
  441. done:
  442. ;
  443. }
  444. static void
  445. test_protover_vote_roundtrip(void *args)
  446. {
  447. (void) args;
  448. static const struct {
  449. const char *input;
  450. const char *expected_output;
  451. } examples[] = {
  452. { "Fkrkljdsf", NULL },
  453. { "Zn=4294967295", NULL },
  454. { "Zn=4294967295-1", NULL },
  455. { "Zn=4294967293-4294967295", NULL },
  456. /* Will fail because of 4294967295. */
  457. { "Foo=1,3 Bar=3 Baz= Quux=9-12,14,15-16,900 Zn=0,4294967295",
  458. NULL },
  459. { "Foo=1,3 Bar=3 Baz= Quux=9-12,14,15-16,900 Zn=0,4294967294",
  460. "Bar=3 Foo=1,3 Quux=9-12,14-16,900 Zn=0,4294967294" },
  461. { "Zu16=0,65536", "Zu16=0,65536" },
  462. { "N-1=1,2", "N-1=1-2" },
  463. { "-1=4294967295", NULL },
  464. { "-1=3", "-1=3" },
  465. /* junk. */
  466. { "!!3@*", NULL },
  467. /* Missing equals sign */
  468. { "Link=4 Haprauxymatyve Desc=9", NULL },
  469. { "Link=4 Haprauxymatyve=7 Desc=9",
  470. "Desc=9 Haprauxymatyve=7 Link=4" },
  471. { "=10-11", NULL },
  472. { "X=10-11", "X=10-11" },
  473. { "Link=4 =3 Desc=9", NULL },
  474. { "Link=4 Z=3 Desc=9", "Desc=9 Link=4 Z=3" },
  475. { "Link=fred", NULL },
  476. { "Link=1,fred", NULL },
  477. { "Link=1,fred,3", NULL },
  478. { "Link=1,9-8,3", NULL },
  479. { "Faux=-0", NULL },
  480. { "Faux=0--0", NULL },
  481. { "Faux=-1", NULL },
  482. { "Faux=-1-3", NULL },
  483. { "Faux=1--1", NULL },
  484. /* Large integers */
  485. { "Link=4294967296", NULL },
  486. /* Large range */
  487. { "Sleen=1-501", "Sleen=1-501" },
  488. { "Sleen=1-65537", NULL },
  489. /* Both C/Rust implementations should be able to handle this mild DoS. */
  490. { "Sleen=0-2147483648", NULL },
  491. /* Rust tests are built in debug mode, so ints are bounds-checked. */
  492. { "Sleen=0-4294967295", NULL },
  493. };
  494. unsigned u;
  495. smartlist_t *votes = smartlist_new();
  496. char *result = NULL;
  497. for (u = 0; u < ARRAY_LENGTH(examples); ++u) {
  498. const char *input = examples[u].input;
  499. const char *expected_output = examples[u].expected_output;
  500. smartlist_add(votes, (void*)input);
  501. result = protover_compute_vote(votes, 1);
  502. if (expected_output != NULL) {
  503. tt_str_op(result, OP_EQ, expected_output);
  504. } else {
  505. tt_str_op(result, OP_EQ, "");
  506. }
  507. smartlist_clear(votes);
  508. tor_free(result);
  509. }
  510. done:
  511. smartlist_free(votes);
  512. tor_free(result);
  513. }
  514. #define PV_TEST(name, flags) \
  515. { #name, test_protover_ ##name, (flags), NULL, NULL }
  516. struct testcase_t protover_tests[] = {
  517. PV_TEST(parse, 0),
  518. PV_TEST(parse_fail, 0),
  519. PV_TEST(vote, 0),
  520. PV_TEST(all_supported, 0),
  521. PV_TEST(list_supports_protocol_for_unsupported_returns_false, 0),
  522. PV_TEST(list_supports_protocol_returns_true, 0),
  523. PV_TEST(supports_version, 0),
  524. PV_TEST(supported_protocols, 0),
  525. PV_TEST(vote_roundtrip, 0),
  526. END_OF_TESTCASES
  527. };