Line data Source code
1 : /* Copyright (c) 2018-2021, The Tor Project, Inc. */
2 : /* See LICENSE for licensing information */
3 :
4 : #include "orconfig.h"
5 :
6 : #define VOTEFLAGS_PRIVATE
7 :
8 : #include "core/or/or.h"
9 :
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"
15 :
16 : #include "app/config/config.h"
17 :
18 : #include "test/test.h"
19 : #include "test/opts_test_helpers.h"
20 :
21 : typedef struct {
22 : time_t now;
23 : routerinfo_t ri;
24 : node_t node;
25 :
26 : routerstatus_t expected;
27 : } flag_vote_test_cfg_t;
28 :
29 : static void
30 3 : setup_cfg(flag_vote_test_cfg_t *c)
31 : {
32 3 : memset(c, 0, sizeof(*c));
33 :
34 3 : c->now = approx_time();
35 :
36 3 : c->ri.nickname = (char *) "testing100";
37 3 : strlcpy(c->expected.nickname, "testing100", sizeof(c->expected.nickname));
38 :
39 3 : memset(c->ri.cache_info.identity_digest, 0xff, DIGEST_LEN);
40 3 : memset(c->ri.cache_info.signed_descriptor_digest, 0xee, DIGEST_LEN);
41 :
42 3 : c->ri.cache_info.published_on = c->now - 100;
43 3 : c->expected.published_on = c->now - 100;
44 :
45 3 : tor_addr_from_ipv4h(&c->ri.ipv4_addr, 0x7f010105);
46 3 : tor_addr_from_ipv4h(&c->expected.ipv4_addr, 0x7f010105);
47 3 : c->ri.ipv4_orport = 9090;
48 3 : c->expected.ipv4_orport = 9090;
49 :
50 3 : tor_addr_make_null(&c->ri.ipv6_addr, AF_INET6);
51 3 : tor_addr_make_null(&c->expected.ipv6_addr, AF_INET6);
52 :
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 3 : c->expected.is_fast = 1;
56 3 : c->expected.is_stable = 1;
57 3 : }
58 :
59 : static bool
60 6 : check_result(flag_vote_test_cfg_t *c)
61 : {
62 6 : bool result = false;
63 6 : routerstatus_t rs;
64 6 : memset(&rs, 0, sizeof(rs));
65 6 : dirauth_set_routerstatus_from_routerinfo(&rs, &c->node, &c->ri, c->now, 0);
66 :
67 6 : tt_i64_op(rs.published_on, OP_EQ, c->expected.published_on);
68 6 : tt_str_op(rs.nickname, OP_EQ, c->expected.nickname);
69 :
70 : // identity_digest and descriptor_digest are not set here.
71 :
72 6 : tt_assert(tor_addr_eq(&rs.ipv4_addr, &c->expected.ipv4_addr));
73 6 : tt_uint_op(rs.ipv4_orport, OP_EQ, c->expected.ipv4_orport);
74 6 : tt_uint_op(rs.ipv4_dirport, OP_EQ, c->expected.ipv4_dirport);
75 :
76 6 : tt_assert(tor_addr_eq(&rs.ipv6_addr, &c->expected.ipv6_addr));
77 6 : tt_uint_op(rs.ipv6_orport, OP_EQ, c->expected.ipv6_orport);
78 :
79 : #define FLAG(flagname) \
80 : tt_uint_op(rs.flagname, OP_EQ, c->expected.flagname)
81 :
82 6 : FLAG(is_authority);
83 6 : FLAG(is_exit);
84 6 : FLAG(is_stable);
85 6 : FLAG(is_fast);
86 6 : FLAG(is_flagged_running);
87 6 : FLAG(is_named);
88 6 : FLAG(is_unnamed);
89 6 : FLAG(is_valid);
90 6 : FLAG(is_possible_guard);
91 6 : FLAG(is_bad_exit);
92 6 : FLAG(is_hs_dir);
93 6 : FLAG(is_v2_dir);
94 6 : FLAG(is_staledesc);
95 6 : FLAG(has_bandwidth);
96 6 : FLAG(has_exitsummary);
97 6 : FLAG(bw_is_unmeasured);
98 :
99 : result = true;
100 :
101 6 : done:
102 6 : return result;
103 : }
104 :
105 : static void
106 1 : test_voting_flags_minimal(void *arg)
107 : {
108 1 : flag_vote_test_cfg_t *cfg = arg;
109 1 : (void) check_result(cfg);
110 1 : }
111 :
112 : static void
113 1 : test_voting_flags_ipv6(void *arg)
114 : {
115 1 : flag_vote_test_cfg_t *cfg = arg;
116 :
117 1 : tt_assert(tor_addr_parse(&cfg->ri.ipv6_addr, "f00::b42") == AF_INET6);
118 1 : cfg->ri.ipv6_orport = 9091;
119 : // no change in expected results, since we aren't set up with ipv6
120 : // connectivity.
121 1 : if (!check_result(cfg))
122 0 : goto done;
123 :
124 1 : get_dirauth_options(get_options_mutable())->AuthDirHasIPv6Connectivity = 1;
125 : // no change in expected results, since last_reachable6 won't be set.
126 1 : if (!check_result(cfg))
127 0 : goto done;
128 :
129 1 : cfg->node.last_reachable6 = cfg->now - 10;
130 : // now that lastreachable6 is set, we expect to see the result.
131 1 : tt_assert(tor_addr_parse(&cfg->expected.ipv6_addr, "f00::b42") == AF_INET6);
132 1 : cfg->expected.ipv6_orport = 9091;
133 1 : if (!check_result(cfg))
134 : goto done;
135 1 : done:
136 1 : ;
137 1 : }
138 :
139 : static void
140 1 : test_voting_flags_staledesc(void *arg)
141 : {
142 1 : flag_vote_test_cfg_t *cfg = arg;
143 1 : time_t now = cfg->now;
144 :
145 1 : cfg->ri.cache_info.published_on = now - DESC_IS_STALE_INTERVAL + 10;
146 1 : cfg->expected.published_on = now - DESC_IS_STALE_INTERVAL + 10;
147 : // no change in expectations for is_staledesc
148 1 : if (!check_result(cfg))
149 0 : goto done;
150 :
151 1 : cfg->ri.cache_info.published_on = now - DESC_IS_STALE_INTERVAL - 10;
152 1 : cfg->expected.published_on = now - DESC_IS_STALE_INTERVAL - 10;
153 1 : cfg->expected.is_staledesc = 1;
154 1 : if (!check_result(cfg))
155 : goto done;
156 :
157 1 : done:
158 1 : ;
159 1 : }
160 :
161 : static void *
162 3 : setup_voting_flags_test(const struct testcase_t *testcase)
163 : {
164 3 : (void)testcase;
165 3 : flag_vote_test_cfg_t *cfg = tor_malloc_zero(sizeof(*cfg));
166 3 : setup_cfg(cfg);
167 3 : return cfg;
168 : }
169 :
170 : static int
171 3 : teardown_voting_flags_test(const struct testcase_t *testcase, void *arg)
172 : {
173 3 : (void)testcase;
174 3 : flag_vote_test_cfg_t *cfg = arg;
175 3 : tor_free(cfg);
176 3 : return 1;
177 : }
178 :
179 : static const struct testcase_setup_t voting_flags_setup = {
180 : .setup_fn = setup_voting_flags_test,
181 : .cleanup_fn = teardown_voting_flags_test,
182 : };
183 :
184 : #define T(name,flags) \
185 : { #name, test_voting_flags_##name, (flags), &voting_flags_setup, NULL }
186 :
187 : struct testcase_t voting_flags_tests[] = {
188 : T(minimal, 0),
189 : T(ipv6, TT_FORK),
190 : // TODO: Add more of these tests.
191 : T(staledesc, TT_FORK),
192 : END_OF_TESTCASES
193 : };
|