Merge branch 'maint-0.2.9' into maint-0.3.3
[tor.git] / src / test / test_router.c
blob84473822a2344ef89b7d2df1779940bf26bea3f4
1 /* Copyright (c) 2017, The Tor Project, Inc. */
2 /* Copyright (c) 2017, isis agora lovecruft */
3 /* See LICENSE for licensing information */
5 /**
6 * \file test_router.c
7 * \brief Unittests for code in src/or/router.c
8 **/
10 #include "or.h"
11 #include "config.h"
12 #include "crypto_curve25519.h"
13 #include "crypto_ed25519.h"
14 #include "hibernate.h"
15 #include "main.h"
16 #include "rephist.h"
17 #include "router.h"
18 #include "routerlist.h"
20 /* Test suite stuff */
21 #include "test.h"
22 #include "log_test_helpers.h"
24 NS_DECL(const routerinfo_t *, router_get_my_routerinfo, (void));
26 static routerinfo_t* mock_routerinfo;
28 static const routerinfo_t*
29 NS(router_get_my_routerinfo)(void)
31 crypto_pk_t* ident_key;
32 crypto_pk_t* tap_key;
33 time_t now;
35 if (!mock_routerinfo) {
36 /* Mock the published timestamp, otherwise router_dump_router_to_string()
37 * will poop its pants. */
38 time(&now);
40 /* We'll need keys, or router_dump_router_to_string() would return NULL. */
41 ident_key = pk_generate(0);
42 tap_key = pk_generate(0);
44 tor_assert(ident_key != NULL);
45 tor_assert(tap_key != NULL);
47 mock_routerinfo = tor_malloc_zero(sizeof(routerinfo_t));
48 mock_routerinfo->nickname = tor_strdup("ConlonNancarrow");
49 mock_routerinfo->addr = 123456789;
50 mock_routerinfo->or_port = 443;
51 mock_routerinfo->platform = tor_strdup("unittest");
52 mock_routerinfo->cache_info.published_on = now;
53 mock_routerinfo->identity_pkey = crypto_pk_dup_key(ident_key);
54 mock_routerinfo->onion_pkey = crypto_pk_dup_key(tap_key);
55 mock_routerinfo->bandwidthrate = 9001;
56 mock_routerinfo->bandwidthburst = 9002;
59 return mock_routerinfo;
62 /* If no distribution option was set, then check_bridge_distribution_setting()
63 * should have set it to "any". */
64 static void
65 test_router_dump_router_to_string_no_bridge_distribution_method(void *arg)
67 const char* needle = "bridge-distribution-request any";
68 or_options_t* options = get_options_mutable();
69 routerinfo_t* router = NULL;
70 curve25519_keypair_t ntor_keypair;
71 ed25519_keypair_t signing_keypair;
72 char* desc = NULL;
73 char* found = NULL;
74 (void)arg;
76 NS_MOCK(router_get_my_routerinfo);
78 options->ORPort_set = 1;
79 options->BridgeRelay = 1;
81 /* Generate keys which router_dump_router_to_string() expects to exist. */
82 tt_int_op(0, ==, curve25519_keypair_generate(&ntor_keypair, 0));
83 tt_int_op(0, ==, ed25519_keypair_generate(&signing_keypair, 0));
85 /* Set up part of our routerinfo_t so that we don't trigger any other
86 * assertions in router_dump_router_to_string(). */
87 router = (routerinfo_t*)router_get_my_routerinfo();
88 tt_ptr_op(router, !=, NULL);
90 router->onion_curve25519_pkey = &ntor_keypair.pubkey;
92 /* Generate our server descriptor and ensure that the substring
93 * "bridge-distribution-request any" occurs somewhere within it. */
94 desc = router_dump_router_to_string(router,
95 router->identity_pkey,
96 router->onion_pkey,
97 &ntor_keypair,
98 &signing_keypair);
99 tt_ptr_op(desc, !=, NULL);
100 found = strstr(desc, needle);
101 tt_ptr_op(found, !=, NULL);
103 done:
104 NS_UNMOCK(router_get_my_routerinfo);
106 tor_free(desc);
109 static routerinfo_t *mock_router_get_my_routerinfo_result = NULL;
111 static const routerinfo_t *
112 mock_router_get_my_routerinfo(void)
114 return mock_router_get_my_routerinfo_result;
117 static long
118 mock_get_uptime_3h(void)
120 return 3*60*60;
123 static long
124 mock_get_uptime_1d(void)
126 return 24*60*60;
129 static int
130 mock_rep_hist_bandwidth_assess(void)
132 return 20001;
135 static int
136 mock_we_are_not_hibernating(void)
138 return 0;
141 static int
142 mock_we_are_hibernating(void)
144 return 0;
147 static void
148 test_router_check_descriptor_bandwidth_changed(void *arg)
150 (void)arg;
151 routerinfo_t routerinfo;
152 memset(&routerinfo, 0, sizeof(routerinfo));
153 mock_router_get_my_routerinfo_result = NULL;
155 MOCK(we_are_hibernating, mock_we_are_not_hibernating);
156 MOCK(router_get_my_routerinfo, mock_router_get_my_routerinfo);
157 mock_router_get_my_routerinfo_result = &routerinfo;
159 /* When uptime is less than 24h, no previous bandwidth, no last_changed
160 * Uptime: 10800, last_changed: 0, Previous bw: 0, Current bw: 0 */
161 routerinfo.bandwidthcapacity = 0;
162 MOCK(get_uptime, mock_get_uptime_3h);
163 setup_full_capture_of_logs(LOG_INFO);
164 check_descriptor_bandwidth_changed(time(NULL));
165 expect_log_msg_not_containing(
166 "Measured bandwidth has changed; rebuilding descriptor.");
167 teardown_capture_of_logs();
169 /* When uptime is less than 24h, previous bandwidth,
170 * last_changed more than 3h ago
171 * Uptime: 10800, last_changed: 0, Previous bw: 10000, Current bw: 0 */
172 routerinfo.bandwidthcapacity = 10000;
173 setup_full_capture_of_logs(LOG_INFO);
174 check_descriptor_bandwidth_changed(time(NULL));
175 expect_log_msg_containing(
176 "Measured bandwidth has changed; rebuilding descriptor.");
177 teardown_capture_of_logs();
179 /* When uptime is less than 24h, previous bandwidth,
180 * last_changed more than 3h ago, and hibernating
181 * Uptime: 10800, last_changed: 0, Previous bw: 10000, Current bw: 0 */
183 UNMOCK(we_are_hibernating);
184 MOCK(we_are_hibernating, mock_we_are_hibernating);
185 routerinfo.bandwidthcapacity = 10000;
186 setup_full_capture_of_logs(LOG_INFO);
187 check_descriptor_bandwidth_changed(time(NULL));
188 expect_log_msg_not_containing(
189 "Measured bandwidth has changed; rebuilding descriptor.");
190 teardown_capture_of_logs();
191 UNMOCK(we_are_hibernating);
192 MOCK(we_are_hibernating, mock_we_are_not_hibernating);
194 /* When uptime is less than 24h, last_changed is not more than 3h ago
195 * Uptime: 10800, last_changed: x, Previous bw: 10000, Current bw: 0 */
196 setup_full_capture_of_logs(LOG_INFO);
197 check_descriptor_bandwidth_changed(time(NULL));
198 expect_log_msg_not_containing(
199 "Measured bandwidth has changed; rebuilding descriptor.");
200 teardown_capture_of_logs();
202 /* When uptime is less than 24h and bandwidthcapacity does not change
203 * Uptime: 10800, last_changed: x, Previous bw: 10000, Current bw: 20001 */
204 MOCK(rep_hist_bandwidth_assess, mock_rep_hist_bandwidth_assess);
205 setup_full_capture_of_logs(LOG_INFO);
206 check_descriptor_bandwidth_changed(time(NULL) + 6*60*60 + 1);
207 expect_log_msg_containing(
208 "Measured bandwidth has changed; rebuilding descriptor.");
209 UNMOCK(get_uptime);
210 UNMOCK(rep_hist_bandwidth_assess);
211 teardown_capture_of_logs();
213 /* When uptime is more than 24h */
214 MOCK(get_uptime, mock_get_uptime_1d);
215 setup_full_capture_of_logs(LOG_INFO);
216 check_descriptor_bandwidth_changed(time(NULL));
217 expect_log_msg_not_containing(
218 "Measured bandwidth has changed; rebuilding descriptor.");
219 teardown_capture_of_logs();
221 done:
222 UNMOCK(get_uptime);
223 UNMOCK(router_get_my_routerinfo);
224 UNMOCK(we_are_hibernating);
227 #define ROUTER_TEST(name, flags) \
228 { #name, test_router_ ## name, flags, NULL, NULL }
230 struct testcase_t router_tests[] = {
231 ROUTER_TEST(dump_router_to_string_no_bridge_distribution_method, TT_FORK),
232 ROUTER_TEST(check_descriptor_bandwidth_changed, TT_FORK),
233 END_OF_TESTCASES