Remove unused function: dns_randfn_() in dns.c.
[tor.git] / src / test / test_routerlist.c
blob95c9176faadb8efcd21f576d88815d1a3c25f9da
1 /* Copyright (c) 2014-2019, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 #include "orconfig.h"
5 #include <math.h>
6 #include <time.h>
8 #define CONNECTION_PRIVATE
9 #define DIRCLIENT_PRIVATE
10 #define DIRVOTE_PRIVATE
11 #define ENTRYNODES_PRIVATE
12 #define HIBERNATE_PRIVATE
13 #define NETWORKSTATUS_PRIVATE
14 #define ROUTERLIST_PRIVATE
15 #define NODE_SELECT_PRIVATE
16 #define TOR_UNIT_TESTING
17 #include "core/or/or.h"
18 #include "app/config/config.h"
19 #include "core/mainloop/connection.h"
20 #include "feature/control/control.h"
21 #include "lib/crypt_ops/crypto_rand.h"
22 #include "feature/dircommon/directory.h"
23 #include "feature/dirclient/dirclient.h"
24 #include "feature/dirauth/dirvote.h"
25 #include "feature/client/entrynodes.h"
26 #include "feature/hibernate/hibernate.h"
27 #include "feature/nodelist/microdesc.h"
28 #include "feature/nodelist/networkstatus.h"
29 #include "feature/nodelist/nodelist.h"
30 #include "core/or/policies.h"
31 #include "feature/relay/router.h"
32 #include "feature/nodelist/authcert.h"
33 #include "feature/nodelist/node_select.h"
34 #include "feature/nodelist/routerlist.h"
35 #include "feature/nodelist/routerset.h"
36 #include "feature/dirparse/authcert_parse.h"
37 #include "feature/dirparse/ns_parse.h"
38 #include "feature/dirauth/shared_random.h"
39 #include "app/config/statefile.h"
41 #include "feature/nodelist/authority_cert_st.h"
42 #include "feature/dircommon/dir_connection_st.h"
43 #include "feature/nodelist/networkstatus_st.h"
44 #include "feature/nodelist/node_st.h"
45 #include "app/config/or_state_st.h"
46 #include "feature/nodelist/routerstatus_st.h"
48 #include "lib/encoding/confline.h"
49 #include "lib/container/buffers.h"
51 #include "test/test.h"
52 #include "test/test_dir_common.h"
53 #include "test/log_test_helpers.h"
55 void construct_consensus(char **consensus_text_md, time_t now);
57 static authority_cert_t *mock_cert;
59 static authority_cert_t *
60 get_my_v3_authority_cert_m(void)
62 tor_assert(mock_cert);
63 return mock_cert;
66 /* 4 digests + 3 sep + pre + post + NULL */
67 static char output[4*BASE64_DIGEST256_LEN+3+2+2+1];
69 static void
70 mock_get_from_dirserver(uint8_t dir_purpose, uint8_t router_purpose,
71 const char *resource, int pds_flags,
72 download_want_authority_t want_authority)
74 (void)dir_purpose;
75 (void)router_purpose;
76 (void)pds_flags;
77 (void)want_authority;
78 tt_assert(resource);
79 strlcpy(output, resource, sizeof(output));
80 done:
84 static void
85 test_routerlist_initiate_descriptor_downloads(void *arg)
87 const char *prose = "unhurried and wise, we perceive.";
88 smartlist_t *digests = smartlist_new();
89 (void)arg;
91 for (int i = 0; i < 20; i++) {
92 smartlist_add(digests, (char*)prose);
95 MOCK(directory_get_from_dirserver, mock_get_from_dirserver);
96 initiate_descriptor_downloads(NULL, DIR_PURPOSE_FETCH_MICRODESC,
97 digests, 3, 7, 0);
98 UNMOCK(directory_get_from_dirserver);
100 tt_str_op(output, OP_EQ, "d/"
101 "dW5odXJyaWVkIGFuZCB3aXNlLCB3ZSBwZXJjZWl2ZS4-"
102 "dW5odXJyaWVkIGFuZCB3aXNlLCB3ZSBwZXJjZWl2ZS4-"
103 "dW5odXJyaWVkIGFuZCB3aXNlLCB3ZSBwZXJjZWl2ZS4-"
104 "dW5odXJyaWVkIGFuZCB3aXNlLCB3ZSBwZXJjZWl2ZS4"
105 ".z");
107 done:
108 smartlist_free(digests);
111 static int count = 0;
113 static void
114 mock_initiate_descriptor_downloads(const routerstatus_t *source,
115 int purpose, smartlist_t *digests,
116 int lo, int hi, int pds_flags)
118 (void)source;
119 (void)purpose;
120 (void)digests;
121 (void)pds_flags;
122 (void)hi;
123 (void)lo;
124 count += 1;
127 static void
128 test_routerlist_launch_descriptor_downloads(void *arg)
130 smartlist_t *downloadable = smartlist_new();
131 time_t now = time(NULL);
132 char *cp;
133 (void)arg;
135 for (int i = 0; i < 100; i++) {
136 cp = tor_malloc(DIGEST256_LEN);
137 tt_assert(cp);
138 crypto_rand(cp, DIGEST256_LEN);
139 smartlist_add(downloadable, cp);
142 MOCK(initiate_descriptor_downloads, mock_initiate_descriptor_downloads);
143 launch_descriptor_downloads(DIR_PURPOSE_FETCH_MICRODESC, downloadable,
144 NULL, now);
145 tt_int_op(3, OP_EQ, count);
146 UNMOCK(initiate_descriptor_downloads);
148 done:
149 SMARTLIST_FOREACH(downloadable, char *, cp1, tor_free(cp1));
150 smartlist_free(downloadable);
153 void
154 construct_consensus(char **consensus_text_md, time_t now)
156 networkstatus_t *vote = NULL;
157 networkstatus_t *v1 = NULL, *v2 = NULL, *v3 = NULL;
158 networkstatus_voter_info_t *voter = NULL;
159 authority_cert_t *cert1=NULL, *cert2=NULL, *cert3=NULL;
160 crypto_pk_t *sign_skey_1=NULL, *sign_skey_2=NULL, *sign_skey_3=NULL;
161 crypto_pk_t *sign_skey_leg=NULL;
162 smartlist_t *votes = NULL;
163 int n_vrs;
165 tt_assert(!dir_common_authority_pk_init(&cert1, &cert2, &cert3,
166 &sign_skey_1, &sign_skey_2,
167 &sign_skey_3));
168 sign_skey_leg = pk_generate(4);
170 dir_common_construct_vote_1(&vote, cert1, sign_skey_1,
171 &dir_common_gen_routerstatus_for_v3ns,
172 &v1, &n_vrs, now, 1);
173 networkstatus_vote_free(vote);
174 tt_assert(v1);
175 tt_int_op(n_vrs, OP_EQ, 4);
176 tt_int_op(smartlist_len(v1->routerstatus_list), OP_EQ, 4);
178 dir_common_construct_vote_2(&vote, cert2, sign_skey_2,
179 &dir_common_gen_routerstatus_for_v3ns,
180 &v2, &n_vrs, now, 1);
181 networkstatus_vote_free(vote);
182 tt_assert(v2);
183 tt_int_op(n_vrs, OP_EQ, 4);
184 tt_int_op(smartlist_len(v2->routerstatus_list), OP_EQ, 4);
186 dir_common_construct_vote_3(&vote, cert3, sign_skey_3,
187 &dir_common_gen_routerstatus_for_v3ns,
188 &v3, &n_vrs, now, 1);
190 tt_assert(v3);
191 tt_int_op(n_vrs, OP_EQ, 4);
192 tt_int_op(smartlist_len(v3->routerstatus_list), OP_EQ, 4);
193 networkstatus_vote_free(vote);
194 votes = smartlist_new();
195 smartlist_add(votes, v1);
196 smartlist_add(votes, v2);
197 smartlist_add(votes, v3);
199 *consensus_text_md = networkstatus_compute_consensus(votes, 3,
200 cert1->identity_key,
201 sign_skey_1,
202 "AAAAAAAAAAAAAAAAAAAA",
203 sign_skey_leg,
204 FLAV_MICRODESC);
206 tt_assert(*consensus_text_md);
208 done:
209 tor_free(voter);
210 networkstatus_vote_free(v1);
211 networkstatus_vote_free(v2);
212 networkstatus_vote_free(v3);
213 smartlist_free(votes);
214 authority_cert_free(cert1);
215 authority_cert_free(cert2);
216 authority_cert_free(cert3);
217 crypto_pk_free(sign_skey_1);
218 crypto_pk_free(sign_skey_2);
219 crypto_pk_free(sign_skey_3);
220 crypto_pk_free(sign_skey_leg);
223 static int mock_usable_consensus_flavor_value = FLAV_NS;
225 static int
226 mock_usable_consensus_flavor(void)
228 return mock_usable_consensus_flavor_value;
231 static void
232 test_router_pick_directory_server_impl(void *arg)
234 (void)arg;
236 networkstatus_t *con_md = NULL;
237 char *consensus_text_md = NULL;
238 int flags = PDS_IGNORE_FASCISTFIREWALL|PDS_RETRY_IF_NO_SERVERS;
239 or_options_t *options = get_options_mutable();
240 const routerstatus_t *rs = NULL;
241 options->UseMicrodescriptors = 1;
242 char *router1_id = NULL, *router2_id = NULL, *router3_id = NULL;
243 node_t *node_router1 = NULL, *node_router2 = NULL, *node_router3 = NULL;
244 config_line_t *policy_line = NULL;
245 time_t now = time(NULL);
246 int tmp_dirport1, tmp_dirport3;
248 (void)arg;
250 MOCK(usable_consensus_flavor, mock_usable_consensus_flavor);
252 /* With no consensus, we must be bootstrapping, regardless of time or flavor
254 mock_usable_consensus_flavor_value = FLAV_NS;
255 tt_assert(networkstatus_consensus_is_bootstrapping(now));
256 tt_assert(networkstatus_consensus_is_bootstrapping(now + 2000));
257 tt_assert(networkstatus_consensus_is_bootstrapping(now + 2*24*60*60));
258 tt_assert(networkstatus_consensus_is_bootstrapping(now - 2*24*60*60));
260 mock_usable_consensus_flavor_value = FLAV_MICRODESC;
261 tt_assert(networkstatus_consensus_is_bootstrapping(now));
262 tt_assert(networkstatus_consensus_is_bootstrapping(now + 2000));
263 tt_assert(networkstatus_consensus_is_bootstrapping(now + 2*24*60*60));
264 tt_assert(networkstatus_consensus_is_bootstrapping(now - 2*24*60*60));
266 /* Init SR subsystem. */
267 MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
268 mock_cert = authority_cert_parse_from_string(AUTHORITY_CERT_1, NULL);
269 sr_init(0);
270 UNMOCK(get_my_v3_authority_cert);
272 /* No consensus available, fail early */
273 rs = router_pick_directory_server_impl(V3_DIRINFO, (const int) 0, NULL);
274 tt_ptr_op(rs, OP_EQ, NULL);
276 construct_consensus(&consensus_text_md, now);
277 tt_assert(consensus_text_md);
278 con_md = networkstatus_parse_vote_from_string(consensus_text_md, NULL,
279 NS_TYPE_CONSENSUS);
280 tt_assert(con_md);
281 tt_int_op(con_md->flavor,OP_EQ, FLAV_MICRODESC);
282 tt_assert(con_md->routerstatus_list);
283 tt_int_op(smartlist_len(con_md->routerstatus_list), OP_EQ, 3);
284 tt_assert(!networkstatus_set_current_consensus_from_ns(con_md,
285 "microdesc"));
287 /* If the consensus time or flavor doesn't match, we are still
288 * bootstrapping */
289 mock_usable_consensus_flavor_value = FLAV_NS;
290 tt_assert(networkstatus_consensus_is_bootstrapping(now));
291 tt_assert(networkstatus_consensus_is_bootstrapping(now + 2000));
292 tt_assert(networkstatus_consensus_is_bootstrapping(now + 2*24*60*60));
293 tt_assert(networkstatus_consensus_is_bootstrapping(now - 2*24*60*60));
295 /* With a valid consensus for the current time and flavor, we stop
296 * bootstrapping, even if we have no certificates */
297 mock_usable_consensus_flavor_value = FLAV_MICRODESC;
298 tt_assert(!networkstatus_consensus_is_bootstrapping(now + 2000));
299 tt_assert(!networkstatus_consensus_is_bootstrapping(con_md->valid_after));
300 tt_assert(!networkstatus_consensus_is_bootstrapping(con_md->valid_until));
301 tt_assert(!networkstatus_consensus_is_bootstrapping(con_md->valid_until
302 + 24*60*60));
303 /* These times are outside the test validity period */
304 tt_assert(networkstatus_consensus_is_bootstrapping(now));
305 tt_assert(networkstatus_consensus_is_bootstrapping(now + 2*24*60*60));
306 tt_assert(networkstatus_consensus_is_bootstrapping(now - 2*24*60*60));
308 nodelist_set_consensus(con_md);
309 nodelist_assert_ok();
311 rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
312 /* We should not fail now we have a consensus and routerstatus_list
313 * and nodelist are populated. */
314 tt_ptr_op(rs, OP_NE, NULL);
316 /* Manipulate the nodes so we get the dir server we expect */
317 router1_id = tor_malloc(DIGEST_LEN);
318 memset(router1_id, TEST_DIR_ROUTER_ID_1, DIGEST_LEN);
319 router2_id = tor_malloc(DIGEST_LEN);
320 memset(router2_id, TEST_DIR_ROUTER_ID_2, DIGEST_LEN);
321 router3_id = tor_malloc(DIGEST_LEN);
322 memset(router3_id, TEST_DIR_ROUTER_ID_3, DIGEST_LEN);
324 node_router1 = node_get_mutable_by_id(router1_id);
325 node_router2 = node_get_mutable_by_id(router2_id);
326 node_router3 = node_get_mutable_by_id(router3_id);
328 node_router1->is_possible_guard = 1;
330 node_router1->is_running = 0;
331 node_router3->is_running = 0;
332 rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
333 tt_ptr_op(rs, OP_NE, NULL);
334 tt_assert(tor_memeq(rs->identity_digest, router2_id, DIGEST_LEN));
335 rs = NULL;
336 node_router1->is_running = 1;
337 node_router3->is_running = 1;
339 node_router1->rs->is_v2_dir = 0;
340 node_router3->rs->is_v2_dir = 0;
341 tmp_dirport1 = node_router1->rs->dir_port;
342 tmp_dirport3 = node_router3->rs->dir_port;
343 node_router1->rs->dir_port = 0;
344 node_router3->rs->dir_port = 0;
345 rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
346 tt_ptr_op(rs, OP_NE, NULL);
347 tt_assert(tor_memeq(rs->identity_digest, router2_id, DIGEST_LEN));
348 rs = NULL;
349 node_router1->rs->is_v2_dir = 1;
350 node_router3->rs->is_v2_dir = 1;
351 node_router1->rs->dir_port = tmp_dirport1;
352 node_router3->rs->dir_port = tmp_dirport3;
354 node_router1->is_valid = 0;
355 node_router3->is_valid = 0;
356 rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
357 tt_ptr_op(rs, OP_NE, NULL);
358 tt_assert(tor_memeq(rs->identity_digest, router2_id, DIGEST_LEN));
359 rs = NULL;
360 node_router1->is_valid = 1;
361 node_router3->is_valid = 1;
363 /* Manipulate overloaded */
365 node_router2->rs->last_dir_503_at = now;
366 node_router3->rs->last_dir_503_at = now;
367 rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
368 tt_ptr_op(rs, OP_NE, NULL);
369 tt_assert(tor_memeq(rs->identity_digest, router1_id, DIGEST_LEN));
370 node_router2->rs->last_dir_503_at = 0;
371 node_router3->rs->last_dir_503_at = 0;
373 /* Set a Fascist firewall */
374 flags &= ~ PDS_IGNORE_FASCISTFIREWALL;
375 policy_line = tor_malloc_zero(sizeof(config_line_t));
376 policy_line->key = tor_strdup("ReachableORAddresses");
377 policy_line->value = tor_strdup("accept *:442, reject *:*");
378 options->ReachableORAddresses = policy_line;
379 policies_parse_from_options(options);
381 node_router1->rs->or_port = 444;
382 node_router2->rs->or_port = 443;
383 node_router3->rs->or_port = 442;
384 rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
385 tt_ptr_op(rs, OP_NE, NULL);
386 tt_assert(tor_memeq(rs->identity_digest, router3_id, DIGEST_LEN));
387 node_router1->rs->or_port = 442;
388 node_router2->rs->or_port = 443;
389 node_router3->rs->or_port = 444;
390 rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
391 tt_ptr_op(rs, OP_NE, NULL);
392 tt_assert(tor_memeq(rs->identity_digest, router1_id, DIGEST_LEN));
394 /* Fascist firewall and overloaded */
395 node_router1->rs->or_port = 442;
396 node_router2->rs->or_port = 443;
397 node_router3->rs->or_port = 442;
398 node_router3->rs->last_dir_503_at = now;
399 rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
400 tt_ptr_op(rs, OP_NE, NULL);
401 tt_assert(tor_memeq(rs->identity_digest, router1_id, DIGEST_LEN));
402 node_router3->rs->last_dir_503_at = 0;
404 /* Fascists against OR and Dir */
405 policy_line = tor_malloc_zero(sizeof(config_line_t));
406 policy_line->key = tor_strdup("ReachableAddresses");
407 policy_line->value = tor_strdup("accept *:80, reject *:*");
408 options->ReachableDirAddresses = policy_line;
409 policies_parse_from_options(options);
410 node_router1->rs->or_port = 442;
411 node_router2->rs->or_port = 441;
412 node_router3->rs->or_port = 443;
413 node_router1->rs->dir_port = 80;
414 node_router2->rs->dir_port = 80;
415 node_router3->rs->dir_port = 81;
416 node_router1->rs->last_dir_503_at = now;
417 rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
418 tt_ptr_op(rs, OP_NE, NULL);
419 tt_assert(tor_memeq(rs->identity_digest, router1_id, DIGEST_LEN));
420 node_router1->rs->last_dir_503_at = 0;
422 done:
423 UNMOCK(usable_consensus_flavor);
425 if (router1_id)
426 tor_free(router1_id);
427 if (router2_id)
428 tor_free(router2_id);
429 if (router3_id)
430 tor_free(router3_id);
431 if (options->ReachableORAddresses ||
432 options->ReachableDirAddresses)
433 policies_free_all();
434 tor_free(consensus_text_md);
435 networkstatus_vote_free(con_md);
438 static or_state_t *dummy_state = NULL;
439 static or_state_t *
440 get_or_state_replacement(void)
442 return dummy_state;
445 static void
446 mock_directory_initiate_request(directory_request_t *req)
448 (void)req;
449 return;
452 static circuit_guard_state_t *
453 mock_circuit_guard_state_new(entry_guard_t *guard, unsigned state,
454 entry_guard_restriction_t *rst)
456 (void) guard;
457 (void) state;
458 (void) rst;
459 return NULL;
462 /** Test that we will use our directory guards to fetch mds even if we don't
463 * have any dirinfo (tests bug #23862). */
464 static void
465 test_directory_guard_fetch_with_no_dirinfo(void *arg)
467 int retval;
468 char *consensus_text_md = NULL;
469 or_options_t *options = get_options_mutable();
470 time_t now = time(NULL);
472 (void) arg;
474 hibernate_set_state_for_testing_(HIBERNATE_STATE_LIVE);
476 /* Initialize the SRV subsystem */
477 MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
478 mock_cert = authority_cert_parse_from_string(AUTHORITY_CERT_1, NULL);
479 sr_init(0);
480 UNMOCK(get_my_v3_authority_cert);
482 /* Initialize the entry node configuration from the ticket */
483 options->UseEntryGuards = 1;
484 options->StrictNodes = 1;
485 get_options_mutable()->EntryNodes = routerset_new();
486 routerset_parse(get_options_mutable()->EntryNodes,
487 "2121212121212121212121212121212121212121", "foo");
489 /* Mock some functions */
490 dummy_state = tor_malloc_zero(sizeof(or_state_t));
491 MOCK(get_or_state, get_or_state_replacement);
492 MOCK(directory_initiate_request, mock_directory_initiate_request);
493 /* we need to mock this one to avoid memleaks */
494 MOCK(circuit_guard_state_new, mock_circuit_guard_state_new);
496 /* Call guards_update_all() to simulate loading our state file (see
497 * entry_guards_load_guards_from_state() and ticket #23989). */
498 guards_update_all();
500 /* Test logic: Simulate the arrival of a new consensus when we have no
501 * dirinfo at all. Tor will need to fetch the mds from the consensus. Make
502 * sure that Tor will use the specified entry guard instead of relying on the
503 * fallback directories. */
505 /* Fixup the dirconn that will deliver the consensus */
506 dir_connection_t *conn = dir_connection_new(AF_INET);
507 tor_addr_from_ipv4h(&conn->base_.addr, 0x7f000001);
508 conn->base_.port = 8800;
509 TO_CONN(conn)->address = tor_strdup("127.0.0.1");
510 conn->base_.purpose = DIR_PURPOSE_FETCH_CONSENSUS;
511 conn->requested_resource = tor_strdup("ns");
513 /* Construct a consensus */
514 construct_consensus(&consensus_text_md, now);
515 tt_assert(consensus_text_md);
517 /* Place the consensus in the dirconn */
518 response_handler_args_t args;
519 memset(&args, 0, sizeof(response_handler_args_t));
520 args.status_code = 200;
521 args.body = consensus_text_md;
522 args.body_len = strlen(consensus_text_md);
524 /* Update approx time so that the consensus is considered live */
525 update_approx_time(now+1010);
527 setup_capture_of_logs(LOG_DEBUG);
529 /* Now handle the consensus */
530 retval = handle_response_fetch_consensus(conn, &args);
531 tt_int_op(retval, OP_EQ, 0);
533 /* Make sure that our primary guard was chosen */
534 expect_log_msg_containing("Selected primary guard router3");
536 done:
537 tor_free(consensus_text_md);
538 tor_free(dummy_state);
539 connection_free_minimal(TO_CONN(conn));
540 entry_guards_free_all();
541 teardown_capture_of_logs();
544 static connection_t *mocked_connection = NULL;
546 /* Mock connection_get_by_type_addr_port_purpose by returning
547 * mocked_connection. */
548 static connection_t *
549 mock_connection_get_by_type_addr_port_purpose(int type,
550 const tor_addr_t *addr,
551 uint16_t port, int purpose)
553 (void)type;
554 (void)addr;
555 (void)port;
556 (void)purpose;
558 return mocked_connection;
561 #define TEST_ADDR_STR "127.0.0.1"
562 #define TEST_DIR_PORT 12345
564 static void
565 test_routerlist_router_is_already_dir_fetching(void *arg)
567 (void)arg;
568 tor_addr_port_t test_ap, null_addr_ap, zero_port_ap;
570 /* Setup */
571 tor_addr_parse(&test_ap.addr, TEST_ADDR_STR);
572 test_ap.port = TEST_DIR_PORT;
573 tor_addr_make_null(&null_addr_ap.addr, AF_INET6);
574 null_addr_ap.port = TEST_DIR_PORT;
575 tor_addr_parse(&zero_port_ap.addr, TEST_ADDR_STR);
576 zero_port_ap.port = 0;
577 MOCK(connection_get_by_type_addr_port_purpose,
578 mock_connection_get_by_type_addr_port_purpose);
580 /* Test that we never get 1 from a NULL connection */
581 mocked_connection = NULL;
582 tt_int_op(router_is_already_dir_fetching(&test_ap, 1, 1), OP_EQ, 0);
583 tt_int_op(router_is_already_dir_fetching(&test_ap, 1, 0), OP_EQ, 0);
584 tt_int_op(router_is_already_dir_fetching(&test_ap, 0, 1), OP_EQ, 0);
585 /* We always expect 0 in these cases */
586 tt_int_op(router_is_already_dir_fetching(&test_ap, 0, 0), OP_EQ, 0);
587 tt_int_op(router_is_already_dir_fetching(NULL, 1, 1), OP_EQ, 0);
588 tt_int_op(router_is_already_dir_fetching(&null_addr_ap, 1, 1), OP_EQ, 0);
589 tt_int_op(router_is_already_dir_fetching(&zero_port_ap, 1, 1), OP_EQ, 0);
591 /* Test that we get 1 with a connection in the appropriate circumstances */
592 mocked_connection = connection_new(CONN_TYPE_DIR, AF_INET);
593 tt_int_op(router_is_already_dir_fetching(&test_ap, 1, 1), OP_EQ, 1);
594 tt_int_op(router_is_already_dir_fetching(&test_ap, 1, 0), OP_EQ, 1);
595 tt_int_op(router_is_already_dir_fetching(&test_ap, 0, 1), OP_EQ, 1);
597 /* Test that we get 0 even with a connection in the appropriate
598 * circumstances */
599 tt_int_op(router_is_already_dir_fetching(&test_ap, 0, 0), OP_EQ, 0);
600 tt_int_op(router_is_already_dir_fetching(NULL, 1, 1), OP_EQ, 0);
601 tt_int_op(router_is_already_dir_fetching(&null_addr_ap, 1, 1), OP_EQ, 0);
602 tt_int_op(router_is_already_dir_fetching(&zero_port_ap, 1, 1), OP_EQ, 0);
604 done:
605 /* If a connection is never set up, connection_free chokes on it. */
606 if (mocked_connection) {
607 buf_free(mocked_connection->inbuf);
608 buf_free(mocked_connection->outbuf);
610 tor_free(mocked_connection);
611 UNMOCK(connection_get_by_type_addr_port_purpose);
614 #undef TEST_ADDR_STR
615 #undef TEST_DIR_PORT
617 static long mock_apparent_skew = 0;
619 /** Store apparent_skew and assert that the other arguments are as
620 * expected. */
621 static void
622 mock_clock_skew_warning(const connection_t *conn, long apparent_skew,
623 int trusted, log_domain_mask_t domain,
624 const char *received, const char *source)
626 (void)conn;
627 mock_apparent_skew = apparent_skew;
628 tt_int_op(trusted, OP_EQ, 1);
629 tt_int_op(domain, OP_EQ, LD_GENERAL);
630 tt_str_op(received, OP_EQ, "microdesc flavor consensus");
631 tt_str_op(source, OP_EQ, "CONSENSUS");
632 done:
636 /** Do common setup for test_timely_consensus() and
637 * test_early_consensus(). Call networkstatus_set_current_consensus()
638 * on a constructed consensus and with an appropriately-modified
639 * approx_time. Callers expect presence or absence of appropriate log
640 * messages and control events. */
641 static int
642 test_skew_common(void *arg, time_t now, unsigned long *offset)
644 char *consensus = NULL;
645 int retval = 0;
647 *offset = strtoul(arg, NULL, 10);
649 /* Initialize the SRV subsystem */
650 MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
651 mock_cert = authority_cert_parse_from_string(AUTHORITY_CERT_1, NULL);
652 sr_init(0);
653 UNMOCK(get_my_v3_authority_cert);
655 construct_consensus(&consensus, now);
656 tt_assert(consensus);
658 update_approx_time(now + *offset);
660 mock_apparent_skew = 0;
661 /* Caller will call UNMOCK() */
662 MOCK(clock_skew_warning, mock_clock_skew_warning);
663 /* Caller will call teardown_capture_of_logs() */
664 setup_capture_of_logs(LOG_WARN);
665 retval = networkstatus_set_current_consensus(consensus, "microdesc", 0,
666 NULL);
668 done:
669 tor_free(consensus);
670 return retval;
673 /** Test non-early consensus */
674 static void
675 test_timely_consensus(void *arg)
677 time_t now = time(NULL);
678 unsigned long offset = 0;
679 int retval = 0;
681 retval = test_skew_common(arg, now, &offset);
682 (void)offset;
683 expect_no_log_msg_containing("behind the time published in the consensus");
684 tt_int_op(retval, OP_EQ, 0);
685 tt_int_op(mock_apparent_skew, OP_EQ, 0);
686 done:
687 teardown_capture_of_logs();
688 UNMOCK(clock_skew_warning);
691 /** Test early consensus */
692 static void
693 test_early_consensus(void *arg)
695 time_t now = time(NULL);
696 unsigned long offset = 0;
697 int retval = 0;
699 retval = test_skew_common(arg, now, &offset);
700 /* Can't use expect_single_log_msg() because of unrecognized authorities */
701 expect_log_msg_containing("behind the time published in the consensus");
702 tt_int_op(retval, OP_EQ, 0);
703 /* This depends on construct_consensus() setting valid_after=now+1000 */
704 tt_int_op(mock_apparent_skew, OP_EQ, offset - 1000);
705 done:
706 teardown_capture_of_logs();
707 UNMOCK(clock_skew_warning);
710 /** Test warn_early_consensus(), expecting no warning */
711 static void
712 test_warn_early_consensus_no(const networkstatus_t *c, time_t now,
713 long offset)
715 mock_apparent_skew = 0;
716 setup_capture_of_logs(LOG_WARN);
717 warn_early_consensus(c, "microdesc", now + offset);
718 expect_no_log_msg_containing("behind the time published in the consensus");
719 tt_int_op(mock_apparent_skew, OP_EQ, 0);
720 done:
721 teardown_capture_of_logs();
724 /** Test warn_early_consensus(), expecting a warning */
725 static void
726 test_warn_early_consensus_yes(const networkstatus_t *c, time_t now,
727 long offset)
729 mock_apparent_skew = 0;
730 setup_capture_of_logs(LOG_WARN);
731 warn_early_consensus(c, "microdesc", now + offset);
732 /* Can't use expect_single_log_msg() because of unrecognized authorities */
733 expect_log_msg_containing("behind the time published in the consensus");
734 tt_int_op(mock_apparent_skew, OP_EQ, offset);
735 done:
736 teardown_capture_of_logs();
740 * Test warn_early_consensus() directly, checking both the non-warning
741 * case (consensus is not early) and the warning case (consensus is
742 * early). Depends on EARLY_CONSENSUS_NOTICE_SKEW=60.
744 static void
745 test_warn_early_consensus(void *arg)
747 networkstatus_t *c = NULL;
748 time_t now = time(NULL);
750 (void)arg;
751 c = tor_malloc_zero(sizeof *c);
752 c->valid_after = now;
753 c->dist_seconds = 300;
754 mock_apparent_skew = 0;
755 MOCK(clock_skew_warning, mock_clock_skew_warning);
756 test_warn_early_consensus_no(c, now, 60);
757 test_warn_early_consensus_no(c, now, 0);
758 test_warn_early_consensus_no(c, now, -60);
759 test_warn_early_consensus_no(c, now, -360);
760 test_warn_early_consensus_yes(c, now, -361);
761 test_warn_early_consensus_yes(c, now, -600);
762 UNMOCK(clock_skew_warning);
763 tor_free(c);
766 #define NODE(name, flags) \
767 { #name, test_routerlist_##name, (flags), NULL, NULL }
768 #define ROUTER(name,flags) \
769 { #name, test_router_##name, (flags), NULL, NULL }
771 #define TIMELY(name, arg) \
772 { name, test_timely_consensus, TT_FORK, &passthrough_setup, \
773 (char *)(arg) }
774 #define EARLY(name, arg) \
775 { name, test_early_consensus, TT_FORK, &passthrough_setup, \
776 (char *)(arg) }
778 struct testcase_t routerlist_tests[] = {
779 NODE(initiate_descriptor_downloads, 0),
780 NODE(launch_descriptor_downloads, 0),
781 NODE(router_is_already_dir_fetching, TT_FORK),
782 ROUTER(pick_directory_server_impl, TT_FORK),
783 { "directory_guard_fetch_with_no_dirinfo",
784 test_directory_guard_fetch_with_no_dirinfo, TT_FORK, NULL, NULL },
785 /* These depend on construct_consensus() setting
786 * valid_after=now+1000 and dist_seconds=250 */
787 TIMELY("timely_consensus1", "1010"),
788 TIMELY("timely_consensus2", "1000"),
789 TIMELY("timely_consensus3", "690"),
790 EARLY("early_consensus1", "689"),
791 { "warn_early_consensus", test_warn_early_consensus, 0, NULL, NULL },
792 END_OF_TESTCASES