TOR_VEGAS: Implement Prop#324 TOR_VEGAS.
[tor.git] / src / test / test_voting_flags.c
blob4c5f3a3270bd6ce096c40a79f53201a43c43f607
1 /* Copyright (c) 2018-2021, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 #include "orconfig.h"
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"
21 typedef struct {
22 time_t now;
23 routerinfo_t ri;
24 node_t node;
26 routerstatus_t expected;
27 } flag_vote_test_cfg_t;
29 static void
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;
59 static bool
60 check_result(flag_vote_test_cfg_t *c)
62 bool result = false;
63 routerstatus_t rs;
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)
82 FLAG(is_authority);
83 FLAG(is_exit);
84 FLAG(is_stable);
85 FLAG(is_fast);
86 FLAG(is_flagged_running);
87 FLAG(is_named);
88 FLAG(is_unnamed);
89 FLAG(is_valid);
90 FLAG(is_possible_guard);
91 FLAG(is_bad_exit);
92 FLAG(is_hs_dir);
93 FLAG(is_v2_dir);
94 FLAG(is_staledesc);
95 FLAG(has_bandwidth);
96 FLAG(has_exitsummary);
97 FLAG(bw_is_unmeasured);
99 result = true;
101 done:
102 return result;
105 static void
106 test_voting_flags_minimal(void *arg)
108 flag_vote_test_cfg_t *cfg = arg;
109 (void) check_result(cfg);
112 static void
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
120 // connectivity.
121 if (!check_result(cfg))
122 goto done;
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))
127 goto done;
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))
134 goto done;
135 done:
139 static void
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))
149 goto done;
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))
155 goto done;
157 done:
161 static void *
162 setup_voting_flags_test(const struct testcase_t *testcase)
164 (void)testcase;
165 flag_vote_test_cfg_t *cfg = tor_malloc_zero(sizeof(*cfg));
166 setup_cfg(cfg);
167 return cfg;
170 static int
171 teardown_voting_flags_test(const struct testcase_t *testcase, void *arg)
173 (void)testcase;
174 flag_vote_test_cfg_t *cfg = arg;
175 tor_free(cfg);
176 return 1;
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[] = {
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