test_protover.c 21 KB

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