test_protover.c 21 KB

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