1 /* Copyright (c) 2016-2021, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 #define PROTOVER_PRIVATE
5 #define DIRVOTE_PRIVATE
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"
23 test_protover_parse(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
);
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");
57 SMARTLIST_FOREACH(elts
, proto_entry_t
*, ent
, proto_entry_free(ent
));
63 test_protover_parse_fail(void *arg
)
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
);
77 elts
= parse_protocol_list("Link=4 =3 Desc=9");
78 tt_ptr_op(elts
, OP_EQ
, NULL
);
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
);
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
);
103 test_protover_vote(void *arg
)
107 smartlist_t
*lst
= smartlist_new();
108 char *result
= protover_compute_vote(lst
, 1);
110 tt_str_op(result
, OP_EQ
, "");
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");
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");
123 result
= protover_compute_vote(lst
, 2);
124 tt_str_op(result
, OP_EQ
, "Bar=3-6,8 Foo=9");
128 result
= protover_compute_vote(lst
, 3);
129 tt_str_op(result
, OP_EQ
, "");
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
, "");
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
, "");
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
, "");
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");
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");
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
, "");
185 test_protover_all_supported(void *arg
)
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");
207 tt_assert(! protover_all_supported("Link=60", &msg
));
208 tt_str_op(msg
, OP_EQ
, "Link=60");
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");
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");
221 tt_assert(! protover_all_supported("Link=1-3,50-63", &msg
));
222 tt_str_op(msg
, OP_EQ
, "Link=50-63");
224 tt_assert(! protover_all_supported("Link=1-3,5-12", &msg
));
225 tt_str_op(msg
, OP_EQ
, "Link=6-12");
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");
234 /* If we get a (barely) valid (but unsupported list, we say "yes, that's
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) */
265 tor_end_capture_bugs_();
270 test_protover_list_supports_protocol_returns_true(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);
283 test_protover_list_supports_protocol_for_unsupported_returns_false(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);
296 test_protover_supports_version(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",
307 tt_assert(protocol_list_supports_protocol("Link=4-6 LinkAuth=3",
309 tt_assert(!protocol_list_supports_protocol("Link=4-6 LinkAuth=3",
311 tt_assert(!protocol_list_supports_protocol_or_later("Link=4-6 LinkAuth=3",
313 tt_assert(protocol_list_supports_protocol_or_later("Link=4-6 LinkAuth=3",
315 tt_assert(protocol_list_supports_protocol_or_later("Link=4-6 LinkAuth=3",
318 tt_assert(!protocol_list_supports_protocol_or_later("Link=4-6 LinkAuth=3",
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
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 */
364 test_protover_supported_protocols(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
372 tt_assert(protocol_list_supports_protocol(supported_protocols
,
375 for (uint16_t i
= 0; i
< MAX_PROTOCOLS_TO_TEST
; i
++) {
376 tt_int_op(protocol_list_supports_protocol(supported_protocols
,
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
,
386 PROTOVER_LINKAUTH_V1
),
388 authchallenge_type_is_supported(AUTHTYPE_RSA_SHA256_TLSSECRET
));
389 /* LinkAuth=2 is unused */
390 tt_assert(!protocol_list_supports_protocol(supported_protocols
,
392 PROTOVER_LINKAUTH_V2
));
394 protocol_list_supports_protocol(supported_protocols
,
396 PROTOVER_LINKAUTH_ED25519_HANDSHAKE
));
398 /* Relay protovers do not appear anywhere in the code. */
399 tt_assert(protocol_list_supports_protocol(supported_protocols
,
402 tt_assert(protocol_list_supports_protocol(supported_protocols
,
404 PROTOVER_RELAY_EXTEND2
));
405 tt_assert(protocol_list_supports_protocol(supported_protocols
,
407 PROTOVER_RELAY_ACCEPT_IPV6
));
408 tt_assert(protocol_list_supports_protocol(supported_protocols
,
410 PROTOVER_RELAY_EXTEND_IPV6
));
411 tt_assert(protocol_list_supports_protocol(supported_protocols
,
413 PROTOVER_RELAY_CANONICAL_IPV6
));
415 /* These HSIntro versions are deprecated */
416 tt_assert(!protocol_list_supports_protocol(supported_protocols
,
418 PROTOVER_HS_INTRO_DEPRECATED_1
));
419 tt_assert(!protocol_list_supports_protocol(supported_protocols
,
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
,
427 PROTOVER_HS_INTRO_V2
));
428 /* Test for HSv3 HSIntro */
429 tt_assert(protocol_list_supports_protocol(supported_protocols
,
431 PROTOVER_HS_INTRO_V3
));
432 /* Test for HSIntro DoS */
433 tt_assert(protocol_list_supports_protocol(supported_protocols
,
435 PROTOVER_HS_INTRO_DOS
));
437 /* Legacy HSRend does not appear anywhere in the code. */
438 tt_assert(protocol_list_supports_protocol(supported_protocols
,
440 PROTOVER_HS_RENDEZVOUS_POINT_V2
));
441 /* Test for HSv3 HSRend */
442 tt_assert(protocol_list_supports_protocol(supported_protocols
,
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
,
450 /* Test for HSv3 HSDir */
451 tt_assert(protocol_list_supports_protocol(supported_protocols
,
455 /* No DirCache versions appear anywhere in the code. */
456 tt_assert(protocol_list_supports_protocol(supported_protocols
,
458 PROTOVER_DIRCACHE_V2
));
460 /* No Desc versions appear anywhere in the code. */
461 tt_assert(protocol_list_supports_protocol(supported_protocols
,
464 tt_assert(protocol_list_supports_protocol(supported_protocols
,
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
,
472 PROTOVER_MICRODESC_V1
));
473 tt_assert(protocol_list_supports_protocol(supported_protocols
,
475 PROTOVER_MICRODESC_V2
));
477 /* No Cons versions appear anywhere in the code. */
478 tt_assert(protocol_list_supports_protocol(supported_protocols
,
481 tt_assert(protocol_list_supports_protocol(supported_protocols
,
485 /* Padding=1 is deprecated. */
486 tt_assert(!protocol_list_supports_protocol(supported_protocols
,
488 PROTOVER_PADDING_V1
));
489 tt_assert(protocol_list_supports_protocol(supported_protocols
,
491 PROTOVER_HS_SETUP_PADDING
));
494 tt_assert(protocol_list_supports_protocol(supported_protocols
,
496 PROTOVER_FLOWCTRL_V1
));
503 test_protover_vote_roundtrip(void *args
)
506 static const struct {
508 const char *expected_output
;
510 { "Risqu\u00e9=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",
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
},
529 { "Foo=1,,3", NULL
},
530 { "Foo=1,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" },
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
},
546 { "Faux=0--0", 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
},
559 { "Link=4294967296", NULL
},
561 { "Sleen=1-63", "Sleen=1-63" },
562 { "Sleen=1-65537", NULL
},
565 smartlist_t
*votes
= smartlist_new();
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
);
577 tt_str_op(result
, OP_EQ
, "");
580 smartlist_clear(votes
);
585 smartlist_free(votes
);
590 test_protover_vote_roundtrip_ours(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(),
601 smartlist_t
*votes
= smartlist_new();
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
);
614 tt_str_op(result
, OP_EQ
, "");
617 smartlist_clear(votes
);
622 smartlist_free(votes
);
626 /* Stringifies its argument.
631 #define PROTOVER(proto_string, version_macro)
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) \
643 log_debug(LD_GENERAL, \
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); \
671 /* Test that the proto_string version version_macro sets summary_flag. */
672 #define TEST_PROTOVER(proto_string, version_macro, summary_flag) \
674 memset(&flags, 0, sizeof(flags)); \
675 summarize_protover_flags(&flags, \
676 PROTOVER(proto_string, version_macro), \
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)); \
687 test_protover_summarize_flags(void *args
)
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
),
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
),
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 */
748 memset(&flags
, 0, sizeof(flags
));
749 summarize_protover_flags(&flags
,
751 PROTOVER_LINKAUTH_ED25519_HANDSHAKE
),
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
));
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. */
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
),
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
,
841 TEST_PROTOVER("Padding", PROTOVER_HS_SETUP_PADDING
,
842 supports_hs_setup_padding
);
848 #define PV_TEST(name, flags) \
849 { #name, test_protover_ ##name, (flags), NULL, NULL }
851 struct testcase_t protover_tests
[] = {
853 PV_TEST(parse_fail
, 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
),