dirvote: Fix memleak when computing consensus
[tor.git] / src / test / test_voting_flags.c
bloba5b1248cc1bf9d06528d38882528e8ca546f8433
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;
44 tor_addr_from_ipv4h(&c->ri.ipv4_addr, 0x7f010105);
45 tor_addr_from_ipv4h(&c->expected.ipv4_addr, 0x7f010105);
46 c->ri.ipv4_orport = 9090;
47 c->expected.ipv4_orport = 9090;
49 tor_addr_make_null(&c->ri.ipv6_addr, AF_INET6);
50 tor_addr_make_null(&c->expected.ipv6_addr, AF_INET6);
52 // By default we have no loaded information about stability or speed,
53 // so we'll default to voting "yeah sure." on these two.
54 c->expected.is_fast = 1;
55 c->expected.is_stable = 1;
58 static bool
59 check_result(flag_vote_test_cfg_t *c)
61 bool result = false;
62 routerstatus_t rs;
63 memset(&rs, 0, sizeof(rs));
64 dirauth_set_routerstatus_from_routerinfo(&rs, &c->node, &c->ri, c->now,
65 0, 0);
67 tt_str_op(rs.nickname, OP_EQ, c->expected.nickname);
69 // identity_digest and descriptor_digest are not set here.
71 tt_assert(tor_addr_eq(&rs.ipv4_addr, &c->expected.ipv4_addr));
72 tt_uint_op(rs.ipv4_orport, OP_EQ, c->expected.ipv4_orport);
73 tt_uint_op(rs.ipv4_dirport, OP_EQ, c->expected.ipv4_dirport);
75 tt_assert(tor_addr_eq(&rs.ipv6_addr, &c->expected.ipv6_addr));
76 tt_uint_op(rs.ipv6_orport, OP_EQ, c->expected.ipv6_orport);
78 #define FLAG(flagname) \
79 tt_uint_op(rs.flagname, OP_EQ, c->expected.flagname)
81 FLAG(is_authority);
82 FLAG(is_exit);
83 FLAG(is_stable);
84 FLAG(is_fast);
85 FLAG(is_flagged_running);
86 FLAG(is_named);
87 FLAG(is_unnamed);
88 FLAG(is_valid);
89 FLAG(is_possible_guard);
90 FLAG(is_bad_exit);
91 FLAG(is_hs_dir);
92 FLAG(is_v2_dir);
93 FLAG(is_staledesc);
94 FLAG(has_bandwidth);
95 FLAG(has_exitsummary);
96 FLAG(bw_is_unmeasured);
98 result = true;
100 done:
101 return result;
104 static void
105 test_voting_flags_minimal(void *arg)
107 flag_vote_test_cfg_t *cfg = arg;
108 (void) check_result(cfg);
111 static void
112 test_voting_flags_ipv6(void *arg)
114 flag_vote_test_cfg_t *cfg = arg;
116 tt_assert(tor_addr_parse(&cfg->ri.ipv6_addr, "f00::b42") == AF_INET6);
117 cfg->ri.ipv6_orport = 9091;
118 // no change in expected results, since we aren't set up with ipv6
119 // connectivity.
120 if (!check_result(cfg))
121 goto done;
123 get_dirauth_options(get_options_mutable())->AuthDirHasIPv6Connectivity = 1;
124 // no change in expected results, since last_reachable6 won't be set.
125 if (!check_result(cfg))
126 goto done;
128 cfg->node.last_reachable6 = cfg->now - 10;
129 // now that lastreachable6 is set, we expect to see the result.
130 tt_assert(tor_addr_parse(&cfg->expected.ipv6_addr, "f00::b42") == AF_INET6);
131 cfg->expected.ipv6_orport = 9091;
132 if (!check_result(cfg))
133 goto done;
134 done:
138 static void
139 test_voting_flags_staledesc(void *arg)
141 flag_vote_test_cfg_t *cfg = arg;
142 time_t now = cfg->now;
144 cfg->ri.cache_info.published_on = now - DESC_IS_STALE_INTERVAL + 10;
145 // no change in expectations for is_staledesc
146 if (!check_result(cfg))
147 goto done;
149 cfg->ri.cache_info.published_on = now - DESC_IS_STALE_INTERVAL - 10;
150 cfg->expected.is_staledesc = 1;
151 if (!check_result(cfg))
152 goto done;
154 done:
158 static void *
159 setup_voting_flags_test(const struct testcase_t *testcase)
161 (void)testcase;
162 flag_vote_test_cfg_t *cfg = tor_malloc_zero(sizeof(*cfg));
163 setup_cfg(cfg);
164 return cfg;
167 static int
168 teardown_voting_flags_test(const struct testcase_t *testcase, void *arg)
170 (void)testcase;
171 flag_vote_test_cfg_t *cfg = arg;
172 tor_free(cfg);
173 return 1;
176 static const struct testcase_setup_t voting_flags_setup = {
177 .setup_fn = setup_voting_flags_test,
178 .cleanup_fn = teardown_voting_flags_test,
181 #define T(name,flags) \
182 { #name, test_voting_flags_##name, (flags), &voting_flags_setup, NULL }
184 struct testcase_t voting_flags_tests[] = {
185 T(minimal, 0),
186 T(ipv6, TT_FORK),
187 // TODO: Add more of these tests.
188 T(staledesc, TT_FORK),
189 END_OF_TESTCASES