1 /* Copyright (c) 2018-2021, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
6 #define VOTEFLAGS_PRIVATE
8 #include "core/or/or.h"
10 #include "feature/dirauth/voteflags.h"
11 #include "feature/dirauth/dirauth_options_st.h"
12 #include "feature/nodelist/node_st.h"
13 #include "feature/nodelist/routerstatus_st.h"
14 #include "feature/nodelist/routerinfo_st.h"
16 #include "app/config/config.h"
18 #include "test/test.h"
19 #include "test/opts_test_helpers.h"
26 routerstatus_t expected
;
27 } flag_vote_test_cfg_t
;
30 setup_cfg(flag_vote_test_cfg_t
*c
)
32 memset(c
, 0, sizeof(*c
));
34 c
->now
= approx_time();
36 c
->ri
.nickname
= (char *) "testing100";
37 strlcpy(c
->expected
.nickname
, "testing100", sizeof(c
->expected
.nickname
));
39 memset(c
->ri
.cache_info
.identity_digest
, 0xff, DIGEST_LEN
);
40 memset(c
->ri
.cache_info
.signed_descriptor_digest
, 0xee, DIGEST_LEN
);
42 c
->ri
.cache_info
.published_on
= c
->now
- 100;
43 c
->expected
.published_on
= c
->now
- 100;
45 tor_addr_from_ipv4h(&c
->ri
.ipv4_addr
, 0x7f010105);
46 tor_addr_from_ipv4h(&c
->expected
.ipv4_addr
, 0x7f010105);
47 c
->ri
.ipv4_orport
= 9090;
48 c
->expected
.ipv4_orport
= 9090;
50 tor_addr_make_null(&c
->ri
.ipv6_addr
, AF_INET6
);
51 tor_addr_make_null(&c
->expected
.ipv6_addr
, AF_INET6
);
53 // By default we have no loaded information about stability or speed,
54 // so we'll default to voting "yeah sure." on these two.
55 c
->expected
.is_fast
= 1;
56 c
->expected
.is_stable
= 1;
60 check_result(flag_vote_test_cfg_t
*c
)
64 memset(&rs
, 0, sizeof(rs
));
65 dirauth_set_routerstatus_from_routerinfo(&rs
, &c
->node
, &c
->ri
, c
->now
, 0);
67 tt_i64_op(rs
.published_on
, OP_EQ
, c
->expected
.published_on
);
68 tt_str_op(rs
.nickname
, OP_EQ
, c
->expected
.nickname
);
70 // identity_digest and descriptor_digest are not set here.
72 tt_assert(tor_addr_eq(&rs
.ipv4_addr
, &c
->expected
.ipv4_addr
));
73 tt_uint_op(rs
.ipv4_orport
, OP_EQ
, c
->expected
.ipv4_orport
);
74 tt_uint_op(rs
.ipv4_dirport
, OP_EQ
, c
->expected
.ipv4_dirport
);
76 tt_assert(tor_addr_eq(&rs
.ipv6_addr
, &c
->expected
.ipv6_addr
));
77 tt_uint_op(rs
.ipv6_orport
, OP_EQ
, c
->expected
.ipv6_orport
);
79 #define FLAG(flagname) \
80 tt_uint_op(rs.flagname, OP_EQ, c->expected.flagname)
86 FLAG(is_flagged_running
);
90 FLAG(is_possible_guard
);
96 FLAG(has_exitsummary
);
97 FLAG(bw_is_unmeasured
);
106 test_voting_flags_minimal(void *arg
)
108 flag_vote_test_cfg_t
*cfg
= arg
;
109 (void) check_result(cfg
);
113 test_voting_flags_ipv6(void *arg
)
115 flag_vote_test_cfg_t
*cfg
= arg
;
117 tt_assert(tor_addr_parse(&cfg
->ri
.ipv6_addr
, "f00::b42") == AF_INET6
);
118 cfg
->ri
.ipv6_orport
= 9091;
119 // no change in expected results, since we aren't set up with ipv6
121 if (!check_result(cfg
))
124 get_dirauth_options(get_options_mutable())->AuthDirHasIPv6Connectivity
= 1;
125 // no change in expected results, since last_reachable6 won't be set.
126 if (!check_result(cfg
))
129 cfg
->node
.last_reachable6
= cfg
->now
- 10;
130 // now that lastreachable6 is set, we expect to see the result.
131 tt_assert(tor_addr_parse(&cfg
->expected
.ipv6_addr
, "f00::b42") == AF_INET6
);
132 cfg
->expected
.ipv6_orport
= 9091;
133 if (!check_result(cfg
))
140 test_voting_flags_staledesc(void *arg
)
142 flag_vote_test_cfg_t
*cfg
= arg
;
143 time_t now
= cfg
->now
;
145 cfg
->ri
.cache_info
.published_on
= now
- DESC_IS_STALE_INTERVAL
+ 10;
146 cfg
->expected
.published_on
= now
- DESC_IS_STALE_INTERVAL
+ 10;
147 // no change in expectations for is_staledesc
148 if (!check_result(cfg
))
151 cfg
->ri
.cache_info
.published_on
= now
- DESC_IS_STALE_INTERVAL
- 10;
152 cfg
->expected
.published_on
= now
- DESC_IS_STALE_INTERVAL
- 10;
153 cfg
->expected
.is_staledesc
= 1;
154 if (!check_result(cfg
))
162 setup_voting_flags_test(const struct testcase_t
*testcase
)
165 flag_vote_test_cfg_t
*cfg
= tor_malloc_zero(sizeof(*cfg
));
171 teardown_voting_flags_test(const struct testcase_t
*testcase
, void *arg
)
174 flag_vote_test_cfg_t
*cfg
= arg
;
179 static const struct testcase_setup_t voting_flags_setup
= {
180 .setup_fn
= setup_voting_flags_test
,
181 .cleanup_fn
= teardown_voting_flags_test
,
184 #define T(name,flags) \
185 { #name, test_voting_flags_##name, (flags), &voting_flags_setup, NULL }
187 struct testcase_t voting_flags_tests
[] = {
190 // TODO: Add more of these tests.
191 T(staledesc
, TT_FORK
),