Merge branch 'maint-0.4.6'
[tor.git] / src / test / test_protover.c
blob5ba7f1e9dded26acdf8f6e93df72f4e76633a26a
1 /* Copyright (c) 2016-2021, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 #define PROTOVER_PRIVATE
5 #define DIRVOTE_PRIVATE
7 #include "orconfig.h"
8 #include "test/test.h"
10 #include "lib/tls/tortls.h"
12 #include "core/or/or.h"
14 #include "core/or/connection_or.h"
15 #include "core/or/protover.h"
16 #include "core/or/versions.h"
18 #include "feature/dirauth/dirvote.h"
20 #include "feature/relay/relay_handshake.h"
22 static void
23 test_protover_parse(void *arg)
25 (void) arg;
26 char *re_encoded = NULL;
28 const char *orig = "Foo=1,3 Bar=3 Baz= Quux=9-12,14,15-16";
29 smartlist_t *elts = parse_protocol_list(orig);
31 tt_assert(elts);
32 tt_int_op(smartlist_len(elts), OP_EQ, 4);
34 const proto_entry_t *e;
35 e = smartlist_get(elts, 0);
36 tt_str_op(e->name, OP_EQ, "Foo");
37 tt_int_op(e->bitmask, OP_EQ, 0x0a);
39 e = smartlist_get(elts, 1);
40 tt_str_op(e->name, OP_EQ, "Bar");
41 tt_int_op(e->bitmask, OP_EQ, 0x08);
43 e = smartlist_get(elts, 2);
44 tt_str_op(e->name, OP_EQ, "Baz");
45 tt_int_op(e->bitmask, OP_EQ, 0x00);
47 e = smartlist_get(elts, 3);
48 tt_str_op(e->name, OP_EQ, "Quux");
49 tt_int_op(e->bitmask, OP_EQ, 0x1de00);
51 re_encoded = encode_protocol_list(elts);
52 tt_assert(re_encoded);
53 tt_str_op(re_encoded, OP_EQ, "Foo=1,3 Bar=3 Baz= Quux=9-12,14-16");
55 done:
56 if (elts)
57 SMARTLIST_FOREACH(elts, proto_entry_t *, ent, proto_entry_free(ent));
58 smartlist_free(elts);
59 tor_free(re_encoded);
62 static void
63 test_protover_parse_fail(void *arg)
65 (void)arg;
66 smartlist_t *elts;
68 /* random junk */
69 elts = parse_protocol_list("!!3@*");
70 tt_ptr_op(elts, OP_EQ, NULL);
72 /* Missing equals sign in an entry */
73 elts = parse_protocol_list("Link=4 Haprauxymatyve Desc=9");
74 tt_ptr_op(elts, OP_EQ, NULL);
76 /* Missing word. */
77 elts = parse_protocol_list("Link=4 =3 Desc=9");
78 tt_ptr_op(elts, OP_EQ, NULL);
80 /* Broken numbers */
81 elts = parse_protocol_list("Link=fred");
82 tt_ptr_op(elts, OP_EQ, NULL);
83 elts = parse_protocol_list("Link=1,fred");
84 tt_ptr_op(elts, OP_EQ, NULL);
85 elts = parse_protocol_list("Link=1,fred,3");
86 tt_ptr_op(elts, OP_EQ, NULL);
88 /* Broken range */
89 elts = parse_protocol_list("Link=1,9-8,3");
90 tt_ptr_op(elts, OP_EQ, NULL);
92 /* Protocol name too long */
93 elts = parse_protocol_list("DoSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
94 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
95 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
96 tt_ptr_op(elts, OP_EQ, NULL);
98 done:
102 static void
103 test_protover_vote(void *arg)
105 (void) arg;
107 smartlist_t *lst = smartlist_new();
108 char *result = protover_compute_vote(lst, 1);
110 tt_str_op(result, OP_EQ, "");
111 tor_free(result);
113 smartlist_add(lst, (void*) "Foo=1-10,63 Bar=1,3-7,8");
114 result = protover_compute_vote(lst, 1);
115 tt_str_op(result, OP_EQ, "Bar=1,3-8 Foo=1-10,63");
116 tor_free(result);
118 smartlist_add(lst, (void*) "Quux=12-45 Bar=2-6,8 Foo=9");
119 result = protover_compute_vote(lst, 1);
120 tt_str_op(result, OP_EQ, "Bar=1-8 Foo=1-10,63 Quux=12-45");
121 tor_free(result);
123 result = protover_compute_vote(lst, 2);
124 tt_str_op(result, OP_EQ, "Bar=3-6,8 Foo=9");
125 tor_free(result);
127 /* High threshold */
128 result = protover_compute_vote(lst, 3);
129 tt_str_op(result, OP_EQ, "");
130 tor_free(result);
132 /* Don't count double-voting. */
133 smartlist_clear(lst);
134 smartlist_add(lst, (void*) "Foo=1 Foo=1");
135 smartlist_add(lst, (void*) "Bar=1-2,2-3");
136 result = protover_compute_vote(lst, 2);
137 tt_str_op(result, OP_EQ, "");
138 tor_free(result);
140 /* Bad votes: the result must be empty */
141 smartlist_clear(lst);
142 smartlist_add(lst, (void*) "Faux=10-5");
143 result = protover_compute_vote(lst, 1);
144 tt_str_op(result, OP_EQ, "");
145 tor_free(result);
147 /* This fails, since "-0" is not valid. */
148 smartlist_clear(lst);
149 smartlist_add(lst, (void*) "Faux=-0");
150 result = protover_compute_vote(lst, 1);
151 tt_str_op(result, OP_EQ, "");
152 tor_free(result);
154 /* Vote large protover lists that are just below the threshold */
156 /* Just below the threshold: Rust */
157 smartlist_clear(lst);
158 smartlist_add(lst, (void*) "Sleen=1-50");
159 result = protover_compute_vote(lst, 1);
160 tt_str_op(result, OP_EQ, "Sleen=1-50");
161 tor_free(result);
163 /* Just below the threshold: C */
164 smartlist_clear(lst);
165 smartlist_add(lst, (void*) "Sleen=1-63");
166 result = protover_compute_vote(lst, 1);
167 tt_str_op(result, OP_EQ, "Sleen=1-63");
168 tor_free(result);
170 /* Protocol name too long */
171 smartlist_clear(lst);
172 smartlist_add(lst, (void*) "DoSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
173 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
174 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
175 result = protover_compute_vote(lst, 1);
176 tt_str_op(result, OP_EQ, "");
177 tor_free(result);
179 done:
180 tor_free(result);
181 smartlist_free(lst);
184 static void
185 test_protover_all_supported(void *arg)
187 (void)arg;
188 char *msg = NULL;
190 tt_assert(protover_all_supported(NULL, &msg));
191 tt_ptr_op(msg, OP_EQ, NULL);
193 tt_assert(protover_all_supported("", &msg));
194 tt_ptr_op(msg, OP_EQ, NULL);
196 // Some things that we do support
197 tt_assert(protover_all_supported("Link=3-4", &msg));
198 tt_ptr_op(msg, OP_EQ, NULL);
199 tt_assert(protover_all_supported("Link=3-4 Desc=2", &msg));
200 tt_ptr_op(msg, OP_EQ, NULL);
202 // Some things we don't support
203 tt_assert(! protover_all_supported("Wombat=9", NULL));
204 tt_assert(! protover_all_supported("Wombat=9", &msg));
205 tt_str_op(msg, OP_EQ, "Wombat=9");
206 tor_free(msg);
207 tt_assert(! protover_all_supported("Link=60", &msg));
208 tt_str_op(msg, OP_EQ, "Link=60");
209 tor_free(msg);
211 // Mix of things we support and things we don't
212 tt_assert(! protover_all_supported("Link=3-4 Wombat=9", &msg));
213 tt_str_op(msg, OP_EQ, "Wombat=9");
214 tor_free(msg);
216 /* Mix of things we support and don't support within a single protocol
217 * which we do support */
218 tt_assert(! protover_all_supported("Link=3-60", &msg));
219 tt_str_op(msg, OP_EQ, "Link=6-60");
220 tor_free(msg);
221 tt_assert(! protover_all_supported("Link=1-3,50-63", &msg));
222 tt_str_op(msg, OP_EQ, "Link=50-63");
223 tor_free(msg);
224 tt_assert(! protover_all_supported("Link=1-3,5-12", &msg));
225 tt_str_op(msg, OP_EQ, "Link=6-12");
226 tor_free(msg);
228 /* Mix of protocols we do support and some we don't, where the protocols
229 * we do support have some versions we don't support. */
230 tt_assert(! protover_all_supported("Link=1-3,5-12 Quokka=40-41", &msg));
231 tt_str_op(msg, OP_EQ, "Link=6-12 Quokka=40-41");
232 tor_free(msg);
234 /* If we get a (barely) valid (but unsupported list, we say "yes, that's
235 * supported." */
236 tt_assert(protover_all_supported("Fribble=", &msg));
237 tt_ptr_op(msg, OP_EQ, NULL);
239 #ifndef ALL_BUGS_ARE_FATAL
240 /* If we get a completely unparseable list, protover_all_supported should
241 * hit a fatal assertion for BUG(entries == NULL). */
242 tor_capture_bugs_(1);
243 tt_assert(protover_all_supported("Fribble", &msg));
244 tor_end_capture_bugs_();
246 /* If we get a completely unparseable list, protover_all_supported should
247 * hit a fatal assertion for BUG(entries == NULL). */
248 tor_capture_bugs_(1);
249 tt_assert(protover_all_supported("Sleen=1-4294967295", &msg));
250 tor_end_capture_bugs_();
251 #endif /* !defined(ALL_BUGS_ARE_FATAL) */
253 /* Protocol name too long */
254 #if !defined(ALL_BUGS_ARE_FATAL)
255 tor_capture_bugs_(1);
256 tt_assert(protover_all_supported(
257 "DoSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
258 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
259 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
260 "aaaaaaaaaaaa=1-65536", &msg));
261 tor_end_capture_bugs_();
262 #endif /* !defined(ALL_BUGS_ARE_FATAL) */
264 done:
265 tor_end_capture_bugs_();
266 tor_free(msg);
269 static void
270 test_protover_list_supports_protocol_returns_true(void *arg)
272 (void)arg;
274 const char *protocols = "Link=1";
275 int is_supported = protocol_list_supports_protocol(protocols, PRT_LINK, 1);
276 tt_int_op(is_supported, OP_EQ, 1);
278 done:
282 static void
283 test_protover_list_supports_protocol_for_unsupported_returns_false(void *arg)
285 (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);
291 done:
295 static void
296 test_protover_supports_version(void *arg)
298 (void)arg;
300 tt_assert(protocol_list_supports_protocol("Link=3-6", PRT_LINK, 3));
301 tt_assert(protocol_list_supports_protocol("Link=3-6", PRT_LINK, 6));
302 tt_assert(!protocol_list_supports_protocol("Link=3-6", PRT_LINK, 7));
303 tt_assert(!protocol_list_supports_protocol("Link=3-6", PRT_LINKAUTH, 3));
305 tt_assert(!protocol_list_supports_protocol("Link=4-6 LinkAuth=3",
306 PRT_LINKAUTH, 2));
307 tt_assert(protocol_list_supports_protocol("Link=4-6 LinkAuth=3",
308 PRT_LINKAUTH, 3));
309 tt_assert(!protocol_list_supports_protocol("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, 4));
313 tt_assert(protocol_list_supports_protocol_or_later("Link=4-6 LinkAuth=3",
314 PRT_LINKAUTH, 3));
315 tt_assert(protocol_list_supports_protocol_or_later("Link=4-6 LinkAuth=3",
316 PRT_LINKAUTH, 2));
318 tt_assert(!protocol_list_supports_protocol_or_later("Link=4-6 LinkAuth=3",
319 PRT_DESC, 2));
320 done:
324 /* This could be MAX_PROTOCOLS_TO_EXPAND, but that's not exposed by protover */
325 #define MAX_PROTOCOLS_TO_TEST 1024
327 /* LinkAuth and Relay protocol versions.
328 * Hard-coded here, because they are not in the code, or not exposed in the
329 * headers. */
330 #define PROTOVER_LINKAUTH_V1 1
331 #define PROTOVER_LINKAUTH_V2 2
332 #define PROTOVER_RELAY_V1 1
334 /* Deprecated HSIntro versions */
335 #define PROTOVER_HS_INTRO_DEPRECATED_1 1
336 #define PROTOVER_HS_INTRO_DEPRECATED_2 2
337 /* Highest supported HSv2 introduce protocol version.
338 * It's not clear if we actually support version 2, see #25068. */
339 #define PROTOVER_HS_INTRO_V2 3
341 /* HSv2 Rend and HSDir protocol versions. */
342 #define PROTOVER_HS_RENDEZVOUS_POINT_V2 1
343 #define PROTOVER_HSDIR_V2 2
345 /* DirCache, Desc, Microdesc, and Cons protocol versions. */
346 #define PROTOVER_DIRCACHE_V1 1
347 #define PROTOVER_DIRCACHE_V2 2
349 #define PROTOVER_DESC_V1 1
350 #define PROTOVER_DESC_V2 2
352 #define PROTOVER_MICRODESC_V1 1
353 #define PROTOVER_MICRODESC_V2 2
355 #define PROTOVER_CONS_V1 1
356 #define PROTOVER_CONS_V2 2
358 #define PROTOVER_PADDING_V1 1
360 #define PROTOVER_FLOWCTRL_V1 1
362 /* Make sure we haven't forgotten any supported protocols */
363 static void
364 test_protover_supported_protocols(void *arg)
366 (void)arg;
368 const char *supported_protocols = protover_get_supported_protocols();
370 /* Test for new Link in the code, that hasn't been added to supported
371 * protocols */
372 tt_assert(protocol_list_supports_protocol(supported_protocols,
373 PRT_LINK,
374 MAX_LINK_PROTO));
375 for (uint16_t i = 0; i < MAX_PROTOCOLS_TO_TEST; i++) {
376 tt_int_op(protocol_list_supports_protocol(supported_protocols,
377 PRT_LINK,
379 OP_EQ,
380 is_or_protocol_version_known(i));
383 /* Legacy LinkAuth is only supported on OpenSSL and similar. */
384 tt_int_op(protocol_list_supports_protocol(supported_protocols,
385 PRT_LINKAUTH,
386 PROTOVER_LINKAUTH_V1),
387 OP_EQ,
388 authchallenge_type_is_supported(AUTHTYPE_RSA_SHA256_TLSSECRET));
389 /* LinkAuth=2 is unused */
390 tt_assert(!protocol_list_supports_protocol(supported_protocols,
391 PRT_LINKAUTH,
392 PROTOVER_LINKAUTH_V2));
393 tt_assert(
394 protocol_list_supports_protocol(supported_protocols,
395 PRT_LINKAUTH,
396 PROTOVER_LINKAUTH_ED25519_HANDSHAKE));
398 /* Relay protovers do not appear anywhere in the code. */
399 tt_assert(protocol_list_supports_protocol(supported_protocols,
400 PRT_RELAY,
401 PROTOVER_RELAY_V1));
402 tt_assert(protocol_list_supports_protocol(supported_protocols,
403 PRT_RELAY,
404 PROTOVER_RELAY_EXTEND2));
405 tt_assert(protocol_list_supports_protocol(supported_protocols,
406 PRT_RELAY,
407 PROTOVER_RELAY_ACCEPT_IPV6));
408 tt_assert(protocol_list_supports_protocol(supported_protocols,
409 PRT_RELAY,
410 PROTOVER_RELAY_EXTEND_IPV6));
411 tt_assert(protocol_list_supports_protocol(supported_protocols,
412 PRT_RELAY,
413 PROTOVER_RELAY_CANONICAL_IPV6));
415 /* These HSIntro versions are deprecated */
416 tt_assert(!protocol_list_supports_protocol(supported_protocols,
417 PRT_HSINTRO,
418 PROTOVER_HS_INTRO_DEPRECATED_1));
419 tt_assert(!protocol_list_supports_protocol(supported_protocols,
420 PRT_HSINTRO,
421 PROTOVER_HS_INTRO_DEPRECATED_2));
422 /* We could test legacy HSIntro by calling rend_service_update_descriptor(),
423 * and checking the protocols field. But that's unlikely to change, so
424 * we just use a hard-coded value. */
425 tt_assert(protocol_list_supports_protocol(supported_protocols,
426 PRT_HSINTRO,
427 PROTOVER_HS_INTRO_V2));
428 /* Test for HSv3 HSIntro */
429 tt_assert(protocol_list_supports_protocol(supported_protocols,
430 PRT_HSINTRO,
431 PROTOVER_HS_INTRO_V3));
432 /* Test for HSIntro DoS */
433 tt_assert(protocol_list_supports_protocol(supported_protocols,
434 PRT_HSINTRO,
435 PROTOVER_HS_INTRO_DOS));
437 /* Legacy HSRend does not appear anywhere in the code. */
438 tt_assert(protocol_list_supports_protocol(supported_protocols,
439 PRT_HSREND,
440 PROTOVER_HS_RENDEZVOUS_POINT_V2));
441 /* Test for HSv3 HSRend */
442 tt_assert(protocol_list_supports_protocol(supported_protocols,
443 PRT_HSREND,
444 PROTOVER_HS_RENDEZVOUS_POINT_V3));
446 /* Legacy HSDir does not appear anywhere in the code. */
447 tt_assert(protocol_list_supports_protocol(supported_protocols,
448 PRT_HSDIR,
449 PROTOVER_HSDIR_V2));
450 /* Test for HSv3 HSDir */
451 tt_assert(protocol_list_supports_protocol(supported_protocols,
452 PRT_HSDIR,
453 PROTOVER_HSDIR_V3));
455 /* No DirCache versions appear anywhere in the code. */
456 tt_assert(protocol_list_supports_protocol(supported_protocols,
457 PRT_DIRCACHE,
458 PROTOVER_DIRCACHE_V2));
460 /* No Desc versions appear anywhere in the code. */
461 tt_assert(protocol_list_supports_protocol(supported_protocols,
462 PRT_DESC,
463 PROTOVER_DESC_V1));
464 tt_assert(protocol_list_supports_protocol(supported_protocols,
465 PRT_DESC,
466 PROTOVER_DESC_V2));
467 /* Is there any way to test for new Desc? */
469 /* No Microdesc versions appear anywhere in the code. */
470 tt_assert(protocol_list_supports_protocol(supported_protocols,
471 PRT_MICRODESC,
472 PROTOVER_MICRODESC_V1));
473 tt_assert(protocol_list_supports_protocol(supported_protocols,
474 PRT_MICRODESC,
475 PROTOVER_MICRODESC_V2));
477 /* No Cons versions appear anywhere in the code. */
478 tt_assert(protocol_list_supports_protocol(supported_protocols,
479 PRT_CONS,
480 PROTOVER_CONS_V1));
481 tt_assert(protocol_list_supports_protocol(supported_protocols,
482 PRT_CONS,
483 PROTOVER_CONS_V2));
485 /* Padding=1 is deprecated. */
486 tt_assert(!protocol_list_supports_protocol(supported_protocols,
487 PRT_PADDING,
488 PROTOVER_PADDING_V1));
489 tt_assert(protocol_list_supports_protocol(supported_protocols,
490 PRT_PADDING,
491 PROTOVER_HS_SETUP_PADDING));
493 /* FlowCtrl */
494 tt_assert(protocol_list_supports_protocol(supported_protocols,
495 PRT_FLOWCTRL,
496 PROTOVER_FLOWCTRL_V1));
498 done:
502 static void
503 test_protover_vote_roundtrip(void *args)
505 (void) args;
506 static const struct {
507 const char *input;
508 const char *expected_output;
509 } examples[] = {
510 { "Risqu\u00e9=1", NULL },
511 { ",,,=1", NULL },
512 { "\xc1=1", NULL },
513 { "Foo_Bar=1", NULL },
514 { "Fkrkljdsf", NULL },
515 { "Zn=4294967295", NULL },
516 { "Zn=4294967295-1", NULL },
517 { "Zn=4294967293-4294967295", NULL },
518 /* Will fail because of 4294967295. */
519 { "Foo=1,3 Bar=3 Baz= Quux=9-12,14,15-16,900 Zn=1,4294967295",
520 NULL },
521 { "Foo=1,3 Bar=3 Baz= Quux=9-12,14,15-16,50 Zn=1,42",
522 "Bar=3 Foo=1,3 Quux=9-12,14-16,50 Zn=1,42" },
523 { "Zu16=1,63", "Zu16=1,63" },
524 { "N-1=1,2", "N-1=1-2" },
525 { "-1=4294967295", NULL },
526 { "-1=3", "-1=3" },
527 { "Foo=,", NULL },
528 { "Foo=,1", NULL },
529 { "Foo=1,,3", NULL },
530 { "Foo=1,3,", NULL },
531 /* junk. */
532 { "!!3@*", NULL },
533 /* Missing equals sign */
534 { "Link=4 Haprauxymatyve Desc=9", NULL },
535 { "Link=4 Haprauxymatyve=7 Desc=9",
536 "Desc=9 Haprauxymatyve=7 Link=4" },
537 { "=10-11", NULL },
538 { "X=10-11", "X=10-11" },
539 { "Link=4 =3 Desc=9", NULL },
540 { "Link=4 Z=3 Desc=9", "Desc=9 Link=4 Z=3" },
541 { "Link=fred", NULL },
542 { "Link=1,fred", NULL },
543 { "Link=1,fred,3", NULL },
544 { "Link=1,9-8,3", NULL },
545 { "Faux=-0", NULL },
546 { "Faux=0--0", NULL },
547 { "Faux=-1", NULL },
548 { "Faux=-1-3", NULL },
549 { "Faux=1--1", NULL },
550 { "Link=1-2-", NULL },
551 { "Link=1-2-3", NULL },
552 { "Faux=1-2-", NULL },
553 { "Faux=1-2-3", NULL },
554 { "Link=\t1,3", NULL },
555 { "Link=1\n,3", NULL },
556 { "Faux=1,\r3", NULL },
557 { "Faux=1,3\f", NULL },
558 /* Large integers */
559 { "Link=4294967296", NULL },
560 /* Large range */
561 { "Sleen=1-63", "Sleen=1-63" },
562 { "Sleen=1-65537", NULL },
564 unsigned u;
565 smartlist_t *votes = smartlist_new();
566 char *result = NULL;
568 for (u = 0; u < ARRAY_LENGTH(examples); ++u) {
569 const char *input = examples[u].input;
570 const char *expected_output = examples[u].expected_output;
572 smartlist_add(votes, (void*)input);
573 result = protover_compute_vote(votes, 1);
574 if (expected_output != NULL) {
575 tt_str_op(result, OP_EQ, expected_output);
576 } else {
577 tt_str_op(result, OP_EQ, "");
580 smartlist_clear(votes);
581 tor_free(result);
584 done:
585 smartlist_free(votes);
586 tor_free(result);
589 static void
590 test_protover_vote_roundtrip_ours(void *args)
592 (void) args;
593 const char *examples[] = {
594 protover_get_supported_protocols(),
595 protover_get_recommended_client_protocols(),
596 protover_get_recommended_relay_protocols(),
597 protover_get_required_client_protocols(),
598 protover_get_required_relay_protocols(),
600 unsigned u;
601 smartlist_t *votes = smartlist_new();
602 char *result = NULL;
604 for (u = 0; u < ARRAY_LENGTH(examples); ++u) {
605 tt_assert(examples[u]);
606 const char *input = examples[u];
607 const char *expected_output = examples[u];
609 smartlist_add(votes, (void*)input);
610 result = protover_compute_vote(votes, 1);
611 if (expected_output != NULL) {
612 tt_str_op(result, OP_EQ, expected_output);
613 } else {
614 tt_str_op(result, OP_EQ, "");
617 smartlist_clear(votes);
618 tor_free(result);
621 done:
622 smartlist_free(votes);
623 tor_free(result);
626 /* Stringifies its argument.
627 * 4 -> "4" */
628 #define STR(x) #x
630 #ifdef COCCI
631 #define PROTOVER(proto_string, version_macro)
632 #else
633 /* Generate a protocol version string using proto_string and version_macro.
634 * PROTOVER("HSIntro", PROTOVER_HS_INTRO_DOS) -> "HSIntro" "=" "5"
635 * Uses two levels of macros to turn PROTOVER_HS_INTRO_DOS into "5".
637 #define PROTOVER(proto_string, version_macro) \
638 (proto_string "=" STR(version_macro))
639 #endif /* defined(COCCI) */
641 #define DEBUG_PROTOVER(flags) \
642 STMT_BEGIN \
643 log_debug(LD_GENERAL, \
644 "protovers:\n" \
645 "protocols_known: %d,\n" \
646 "supports_extend2_cells: %d,\n" \
647 "supports_accepting_ipv6_extends: %d,\n" \
648 "supports_initiating_ipv6_extends: %d,\n" \
649 "supports_canonical_ipv6_conns: %d,\n" \
650 "supports_ed25519_link_handshake_compat: %d,\n" \
651 "supports_ed25519_link_handshake_any: %d,\n" \
652 "supports_ed25519_hs_intro: %d,\n" \
653 "supports_establish_intro_dos_extension: %d,\n" \
654 "supports_v3_hsdir: %d,\n" \
655 "supports_v3_rendezvous_point: %d,\n" \
656 "supports_hs_setup_padding: %d.", \
657 (flags).protocols_known, \
658 (flags).supports_extend2_cells, \
659 (flags).supports_accepting_ipv6_extends, \
660 (flags).supports_initiating_ipv6_extends, \
661 (flags).supports_canonical_ipv6_conns, \
662 (flags).supports_ed25519_link_handshake_compat, \
663 (flags).supports_ed25519_link_handshake_any, \
664 (flags).supports_ed25519_hs_intro, \
665 (flags).supports_establish_intro_dos_extension, \
666 (flags).supports_v3_hsdir, \
667 (flags).supports_v3_rendezvous_point, \
668 (flags).supports_hs_setup_padding); \
669 STMT_END
671 /* Test that the proto_string version version_macro sets summary_flag. */
672 #define TEST_PROTOVER(proto_string, version_macro, summary_flag) \
673 STMT_BEGIN \
674 memset(&flags, 0, sizeof(flags)); \
675 summarize_protover_flags(&flags, \
676 PROTOVER(proto_string, version_macro), \
677 NULL); \
678 DEBUG_PROTOVER(flags); \
679 tt_int_op(flags.protocols_known, OP_EQ, 1); \
680 tt_int_op(flags.summary_flag, OP_EQ, 1); \
681 flags.protocols_known = 0; \
682 flags.summary_flag = 0; \
683 tt_mem_op(&flags, OP_EQ, &zero_flags, sizeof(flags)); \
684 STMT_END
686 static void
687 test_protover_summarize_flags(void *args)
689 (void) args;
690 char pv[30];
691 memset(&pv, 0, sizeof(pv));
693 protover_summary_cache_free_all();
695 protover_summary_flags_t zero_flags;
696 memset(&zero_flags, 0, sizeof(zero_flags));
697 protover_summary_flags_t flags;
699 memset(&flags, 0, sizeof(flags));
700 summarize_protover_flags(&flags, NULL, NULL);
701 DEBUG_PROTOVER(flags);
702 tt_mem_op(&flags, OP_EQ, &zero_flags, sizeof(flags));
704 memset(&flags, 0, sizeof(flags));
705 summarize_protover_flags(&flags, "", "");
706 DEBUG_PROTOVER(flags);
707 tt_mem_op(&flags, OP_EQ, &zero_flags, sizeof(flags));
709 /* Now check version exceptions */
711 /* EXTEND2 cell support */
712 memset(&flags, 0, sizeof(flags));
713 summarize_protover_flags(&flags, NULL, "Tor 0.2.4.8-alpha");
714 DEBUG_PROTOVER(flags);
715 tt_int_op(flags.protocols_known, OP_EQ, 1);
716 tt_int_op(flags.supports_extend2_cells, OP_EQ, 1);
717 /* Now clear those flags, and check the rest are zero */
718 flags.protocols_known = 0;
719 flags.supports_extend2_cells = 0;
720 tt_mem_op(&flags, OP_EQ, &zero_flags, sizeof(flags));
722 /* disabling HSDir v3 support for buggy versions */
723 memset(&flags, 0, sizeof(flags));
724 summarize_protover_flags(&flags,
725 PROTOVER("HSDir", PROTOVER_HSDIR_V3),
726 NULL);
727 DEBUG_PROTOVER(flags);
728 tt_int_op(flags.protocols_known, OP_EQ, 1);
729 tt_int_op(flags.supports_v3_hsdir, OP_EQ, 1);
730 /* Now clear those flags, and check the rest are zero */
731 flags.protocols_known = 0;
732 flags.supports_v3_hsdir = 0;
733 tt_mem_op(&flags, OP_EQ, &zero_flags, sizeof(flags));
735 memset(&flags, 0, sizeof(flags));
736 summarize_protover_flags(&flags,
737 PROTOVER("HSDir", PROTOVER_HSDIR_V3),
738 "Tor 0.3.0.7");
739 DEBUG_PROTOVER(flags);
740 tt_int_op(flags.protocols_known, OP_EQ, 1);
741 /* Now clear that flag, and check the rest are zero */
742 flags.protocols_known = 0;
743 tt_mem_op(&flags, OP_EQ, &zero_flags, sizeof(flags));
745 /* Now check standard summaries */
747 /* LinkAuth */
748 memset(&flags, 0, sizeof(flags));
749 summarize_protover_flags(&flags,
750 PROTOVER("LinkAuth",
751 PROTOVER_LINKAUTH_ED25519_HANDSHAKE),
752 NULL);
753 DEBUG_PROTOVER(flags);
754 tt_int_op(flags.protocols_known, OP_EQ, 1);
755 tt_int_op(flags.supports_ed25519_link_handshake_compat, OP_EQ, 1);
756 tt_int_op(flags.supports_ed25519_link_handshake_any, OP_EQ, 1);
757 /* Now clear those flags, and check the rest are zero */
758 flags.protocols_known = 0;
759 flags.supports_ed25519_link_handshake_compat = 0;
760 flags.supports_ed25519_link_handshake_any = 0;
761 tt_mem_op(&flags, OP_EQ, &zero_flags, sizeof(flags));
763 /* Test one greater */
764 memset(&flags, 0, sizeof(flags));
765 snprintf(pv, sizeof(pv),
766 "%s=%d", "LinkAuth", PROTOVER_LINKAUTH_ED25519_HANDSHAKE + 1);
767 summarize_protover_flags(&flags, pv, NULL);
768 DEBUG_PROTOVER(flags);
769 tt_int_op(flags.protocols_known, OP_EQ, 1);
770 tt_int_op(flags.supports_ed25519_link_handshake_compat, OP_EQ, 0);
771 tt_int_op(flags.supports_ed25519_link_handshake_any, OP_EQ, 1);
772 /* Now clear those flags, and check the rest are zero */
773 flags.protocols_known = 0;
774 flags.supports_ed25519_link_handshake_compat = 0;
775 flags.supports_ed25519_link_handshake_any = 0;
776 tt_mem_op(&flags, OP_EQ, &zero_flags, sizeof(flags));
778 /* Test one less */
779 memset(&flags, 0, sizeof(flags));
780 snprintf(pv, sizeof(pv),
781 "%s=%d", "LinkAuth", PROTOVER_LINKAUTH_ED25519_HANDSHAKE - 1);
782 summarize_protover_flags(&flags, pv, NULL);
783 DEBUG_PROTOVER(flags);
784 tt_int_op(flags.protocols_known, OP_EQ, 1);
785 tt_int_op(flags.supports_ed25519_link_handshake_compat, OP_EQ, 0);
786 tt_int_op(flags.supports_ed25519_link_handshake_any, OP_EQ, 0);
787 /* Now clear those flags, and check the rest are zero */
788 flags.protocols_known = 0;
789 flags.supports_ed25519_link_handshake_compat = 0;
790 flags.supports_ed25519_link_handshake_any = 0;
791 tt_mem_op(&flags, OP_EQ, &zero_flags, sizeof(flags));
793 /* We don't test "one more" and "one less" for each protocol version.
794 * But that could be a useful thing to add. */
796 /* Relay */
797 memset(&flags, 0, sizeof(flags));
798 /* This test relies on these versions being equal */
799 tt_int_op(PROTOVER_RELAY_EXTEND2, OP_EQ, PROTOVER_RELAY_ACCEPT_IPV6);
800 summarize_protover_flags(&flags,
801 PROTOVER("Relay", PROTOVER_RELAY_EXTEND2), NULL);
802 DEBUG_PROTOVER(flags);
803 tt_int_op(flags.protocols_known, OP_EQ, 1);
804 tt_int_op(flags.supports_extend2_cells, OP_EQ, 1);
805 tt_int_op(flags.supports_accepting_ipv6_extends, OP_EQ, 1);
806 /* Now clear those flags, and check the rest are zero */
807 flags.protocols_known = 0;
808 flags.supports_extend2_cells = 0;
809 flags.supports_accepting_ipv6_extends = 0;
810 tt_mem_op(&flags, OP_EQ, &zero_flags, sizeof(flags));
812 memset(&flags, 0, sizeof(flags));
813 /* This test relies on these versions being equal */
814 tt_int_op(PROTOVER_RELAY_EXTEND_IPV6, OP_EQ, PROTOVER_RELAY_CANONICAL_IPV6);
815 summarize_protover_flags(&flags,
816 PROTOVER("Relay", PROTOVER_RELAY_EXTEND_IPV6),
817 NULL);
818 DEBUG_PROTOVER(flags);
819 tt_int_op(flags.protocols_known, OP_EQ, 1);
820 tt_int_op(flags.supports_accepting_ipv6_extends, OP_EQ, 1);
821 tt_int_op(flags.supports_initiating_ipv6_extends, OP_EQ, 1);
822 tt_int_op(flags.supports_canonical_ipv6_conns, OP_EQ, 1);
823 /* Now clear those flags, and check the rest are zero */
824 flags.protocols_known = 0;
825 flags.supports_accepting_ipv6_extends = 0;
826 flags.supports_initiating_ipv6_extends = 0;
827 flags.supports_canonical_ipv6_conns = 0;
828 tt_mem_op(&flags, OP_EQ, &zero_flags, sizeof(flags));
830 TEST_PROTOVER("HSIntro", PROTOVER_HS_INTRO_V3,
831 supports_ed25519_hs_intro);
832 TEST_PROTOVER("HSIntro", PROTOVER_HS_INTRO_DOS,
833 supports_establish_intro_dos_extension);
835 TEST_PROTOVER("HSRend", PROTOVER_HS_RENDEZVOUS_POINT_V3,
836 supports_v3_rendezvous_point);
838 TEST_PROTOVER("HSDir", PROTOVER_HSDIR_V3,
839 supports_v3_hsdir);
841 TEST_PROTOVER("Padding", PROTOVER_HS_SETUP_PADDING,
842 supports_hs_setup_padding);
844 done:
848 #define PV_TEST(name, flags) \
849 { #name, test_protover_ ##name, (flags), NULL, NULL }
851 struct testcase_t protover_tests[] = {
852 PV_TEST(parse, 0),
853 PV_TEST(parse_fail, 0),
854 PV_TEST(vote, 0),
855 PV_TEST(all_supported, 0),
856 PV_TEST(list_supports_protocol_for_unsupported_returns_false, 0),
857 PV_TEST(list_supports_protocol_returns_true, 0),
858 PV_TEST(supports_version, 0),
859 PV_TEST(supported_protocols, 0),
860 PV_TEST(vote_roundtrip, 0),
861 PV_TEST(vote_roundtrip_ours, 0),
862 /* fork, because we memoize flags internally */
863 PV_TEST(summarize_flags, TT_FORK),
864 END_OF_TESTCASES