Merge branch 'maint-0.2.9' into maint-0.3.3
[tor.git] / src / test / test_routerlist.c
blobc19d66ef9d8df731ff31d07d9f829038e73ca016
1 /* Copyright (c) 2014-2017, 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 DIRECTORY_PRIVATE
10 #define DIRVOTE_PRIVATE
11 #define ENTRYNODES_PRIVATE
12 #define HIBERNATE_PRIVATE
13 #define NETWORKSTATUS_PRIVATE
14 #define ROUTERLIST_PRIVATE
15 #define TOR_UNIT_TESTING
16 #include "or.h"
17 #include "config.h"
18 #include "connection.h"
19 #include "container.h"
20 #include "control.h"
21 #include "directory.h"
22 #include "dirvote.h"
23 #include "entrynodes.h"
24 #include "hibernate.h"
25 #include "microdesc.h"
26 #include "networkstatus.h"
27 #include "nodelist.h"
28 #include "policies.h"
29 #include "router.h"
30 #include "routerlist.h"
31 #include "routerset.h"
32 #include "routerparse.h"
33 #include "shared_random.h"
34 #include "statefile.h"
35 #include "test.h"
36 #include "test_dir_common.h"
37 #include "log_test_helpers.h"
39 void construct_consensus(char **consensus_text_md);
41 static authority_cert_t *mock_cert;
43 static authority_cert_t *
44 get_my_v3_authority_cert_m(void)
46 tor_assert(mock_cert);
47 return mock_cert;
50 /* 4 digests + 3 sep + pre + post + NULL */
51 static char output[4*BASE64_DIGEST256_LEN+3+2+2+1];
53 static void
54 mock_get_from_dirserver(uint8_t dir_purpose, uint8_t router_purpose,
55 const char *resource, int pds_flags,
56 download_want_authority_t want_authority)
58 (void)dir_purpose;
59 (void)router_purpose;
60 (void)pds_flags;
61 (void)want_authority;
62 tt_assert(resource);
63 strlcpy(output, resource, sizeof(output));
64 done:
68 static void
69 test_routerlist_initiate_descriptor_downloads(void *arg)
71 const char *prose = "unhurried and wise, we perceive.";
72 smartlist_t *digests = smartlist_new();
73 (void)arg;
75 for (int i = 0; i < 20; i++) {
76 smartlist_add(digests, (char*)prose);
79 MOCK(directory_get_from_dirserver, mock_get_from_dirserver);
80 initiate_descriptor_downloads(NULL, DIR_PURPOSE_FETCH_MICRODESC,
81 digests, 3, 7, 0);
82 UNMOCK(directory_get_from_dirserver);
84 tt_str_op(output, OP_EQ, "d/"
85 "dW5odXJyaWVkIGFuZCB3aXNlLCB3ZSBwZXJjZWl2ZS4-"
86 "dW5odXJyaWVkIGFuZCB3aXNlLCB3ZSBwZXJjZWl2ZS4-"
87 "dW5odXJyaWVkIGFuZCB3aXNlLCB3ZSBwZXJjZWl2ZS4-"
88 "dW5odXJyaWVkIGFuZCB3aXNlLCB3ZSBwZXJjZWl2ZS4"
89 ".z");
91 done:
92 smartlist_free(digests);
95 static int count = 0;
97 static void
98 mock_initiate_descriptor_downloads(const routerstatus_t *source,
99 int purpose, smartlist_t *digests,
100 int lo, int hi, int pds_flags)
102 (void)source;
103 (void)purpose;
104 (void)digests;
105 (void)pds_flags;
106 (void)hi;
107 (void)lo;
108 count += 1;
111 static void
112 test_routerlist_launch_descriptor_downloads(void *arg)
114 smartlist_t *downloadable = smartlist_new();
115 time_t now = time(NULL);
116 char *cp;
117 (void)arg;
119 for (int i = 0; i < 100; i++) {
120 cp = tor_malloc(DIGEST256_LEN);
121 tt_assert(cp);
122 crypto_rand(cp, DIGEST256_LEN);
123 smartlist_add(downloadable, cp);
126 MOCK(initiate_descriptor_downloads, mock_initiate_descriptor_downloads);
127 launch_descriptor_downloads(DIR_PURPOSE_FETCH_MICRODESC, downloadable,
128 NULL, now);
129 tt_int_op(3, OP_EQ, count);
130 UNMOCK(initiate_descriptor_downloads);
132 done:
133 SMARTLIST_FOREACH(downloadable, char *, cp1, tor_free(cp1));
134 smartlist_free(downloadable);
137 void
138 construct_consensus(char **consensus_text_md)
140 networkstatus_t *vote = NULL;
141 networkstatus_t *v1 = NULL, *v2 = NULL, *v3 = NULL;
142 networkstatus_voter_info_t *voter = NULL;
143 authority_cert_t *cert1=NULL, *cert2=NULL, *cert3=NULL;
144 crypto_pk_t *sign_skey_1=NULL, *sign_skey_2=NULL, *sign_skey_3=NULL;
145 crypto_pk_t *sign_skey_leg=NULL;
146 time_t now = time(NULL);
147 smartlist_t *votes = NULL;
148 int n_vrs;
150 tt_assert(!dir_common_authority_pk_init(&cert1, &cert2, &cert3,
151 &sign_skey_1, &sign_skey_2,
152 &sign_skey_3));
153 sign_skey_leg = pk_generate(4);
155 dir_common_construct_vote_1(&vote, cert1, sign_skey_1,
156 &dir_common_gen_routerstatus_for_v3ns,
157 &v1, &n_vrs, now, 1);
158 networkstatus_vote_free(vote);
159 tt_assert(v1);
160 tt_int_op(n_vrs, OP_EQ, 4);
161 tt_int_op(smartlist_len(v1->routerstatus_list), OP_EQ, 4);
163 dir_common_construct_vote_2(&vote, cert2, sign_skey_2,
164 &dir_common_gen_routerstatus_for_v3ns,
165 &v2, &n_vrs, now, 1);
166 networkstatus_vote_free(vote);
167 tt_assert(v2);
168 tt_int_op(n_vrs, OP_EQ, 4);
169 tt_int_op(smartlist_len(v2->routerstatus_list), OP_EQ, 4);
171 dir_common_construct_vote_3(&vote, cert3, sign_skey_3,
172 &dir_common_gen_routerstatus_for_v3ns,
173 &v3, &n_vrs, now, 1);
175 tt_assert(v3);
176 tt_int_op(n_vrs, OP_EQ, 4);
177 tt_int_op(smartlist_len(v3->routerstatus_list), OP_EQ, 4);
178 networkstatus_vote_free(vote);
179 votes = smartlist_new();
180 smartlist_add(votes, v1);
181 smartlist_add(votes, v2);
182 smartlist_add(votes, v3);
184 *consensus_text_md = networkstatus_compute_consensus(votes, 3,
185 cert1->identity_key,
186 sign_skey_1,
187 "AAAAAAAAAAAAAAAAAAAA",
188 sign_skey_leg,
189 FLAV_MICRODESC);
191 tt_assert(*consensus_text_md);
193 done:
194 tor_free(voter);
195 networkstatus_vote_free(v1);
196 networkstatus_vote_free(v2);
197 networkstatus_vote_free(v3);
198 smartlist_free(votes);
199 authority_cert_free(cert1);
200 authority_cert_free(cert2);
201 authority_cert_free(cert3);
202 crypto_pk_free(sign_skey_1);
203 crypto_pk_free(sign_skey_2);
204 crypto_pk_free(sign_skey_3);
205 crypto_pk_free(sign_skey_leg);
208 static int mock_usable_consensus_flavor_value = FLAV_NS;
210 static int
211 mock_usable_consensus_flavor(void)
213 return mock_usable_consensus_flavor_value;
216 static void
217 test_router_pick_directory_server_impl(void *arg)
219 (void)arg;
221 networkstatus_t *con_md = NULL;
222 char *consensus_text_md = NULL;
223 int flags = PDS_IGNORE_FASCISTFIREWALL|PDS_RETRY_IF_NO_SERVERS;
224 or_options_t *options = get_options_mutable();
225 const routerstatus_t *rs = NULL;
226 options->UseMicrodescriptors = 1;
227 char *router1_id = NULL, *router2_id = NULL, *router3_id = NULL;
228 node_t *node_router1 = NULL, *node_router2 = NULL, *node_router3 = NULL;
229 config_line_t *policy_line = NULL;
230 time_t now = time(NULL);
231 int tmp_dirport1, tmp_dirport3;
233 (void)arg;
235 MOCK(usable_consensus_flavor, mock_usable_consensus_flavor);
237 /* With no consensus, we must be bootstrapping, regardless of time or flavor
239 mock_usable_consensus_flavor_value = FLAV_NS;
240 tt_assert(networkstatus_consensus_is_bootstrapping(now));
241 tt_assert(networkstatus_consensus_is_bootstrapping(now + 2000));
242 tt_assert(networkstatus_consensus_is_bootstrapping(now + 2*24*60*60));
243 tt_assert(networkstatus_consensus_is_bootstrapping(now - 2*24*60*60));
245 mock_usable_consensus_flavor_value = FLAV_MICRODESC;
246 tt_assert(networkstatus_consensus_is_bootstrapping(now));
247 tt_assert(networkstatus_consensus_is_bootstrapping(now + 2000));
248 tt_assert(networkstatus_consensus_is_bootstrapping(now + 2*24*60*60));
249 tt_assert(networkstatus_consensus_is_bootstrapping(now - 2*24*60*60));
251 /* Init SR subsystem. */
252 MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
253 mock_cert = authority_cert_parse_from_string(AUTHORITY_CERT_1, NULL);
254 sr_init(0);
255 UNMOCK(get_my_v3_authority_cert);
257 /* No consensus available, fail early */
258 rs = router_pick_directory_server_impl(V3_DIRINFO, (const int) 0, NULL);
259 tt_ptr_op(rs, OP_EQ, NULL);
261 construct_consensus(&consensus_text_md);
262 tt_assert(consensus_text_md);
263 con_md = networkstatus_parse_vote_from_string(consensus_text_md, NULL,
264 NS_TYPE_CONSENSUS);
265 tt_assert(con_md);
266 tt_int_op(con_md->flavor,OP_EQ, FLAV_MICRODESC);
267 tt_assert(con_md->routerstatus_list);
268 tt_int_op(smartlist_len(con_md->routerstatus_list), OP_EQ, 3);
269 tt_assert(!networkstatus_set_current_consensus_from_ns(con_md,
270 "microdesc"));
272 /* If the consensus time or flavor doesn't match, we are still
273 * bootstrapping */
274 mock_usable_consensus_flavor_value = FLAV_NS;
275 tt_assert(networkstatus_consensus_is_bootstrapping(now));
276 tt_assert(networkstatus_consensus_is_bootstrapping(now + 2000));
277 tt_assert(networkstatus_consensus_is_bootstrapping(now + 2*24*60*60));
278 tt_assert(networkstatus_consensus_is_bootstrapping(now - 2*24*60*60));
280 /* With a valid consensus for the current time and flavor, we stop
281 * bootstrapping, even if we have no certificates */
282 mock_usable_consensus_flavor_value = FLAV_MICRODESC;
283 tt_assert(!networkstatus_consensus_is_bootstrapping(now + 2000));
284 tt_assert(!networkstatus_consensus_is_bootstrapping(con_md->valid_after));
285 tt_assert(!networkstatus_consensus_is_bootstrapping(con_md->valid_until));
286 tt_assert(!networkstatus_consensus_is_bootstrapping(con_md->valid_until
287 + 24*60*60));
288 /* These times are outside the test validity period */
289 tt_assert(networkstatus_consensus_is_bootstrapping(now));
290 tt_assert(networkstatus_consensus_is_bootstrapping(now + 2*24*60*60));
291 tt_assert(networkstatus_consensus_is_bootstrapping(now - 2*24*60*60));
293 nodelist_set_consensus(con_md);
294 nodelist_assert_ok();
296 rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
297 /* We should not fail now we have a consensus and routerstatus_list
298 * and nodelist are populated. */
299 tt_ptr_op(rs, OP_NE, NULL);
301 /* Manipulate the nodes so we get the dir server we expect */
302 router1_id = tor_malloc(DIGEST_LEN);
303 memset(router1_id, TEST_DIR_ROUTER_ID_1, DIGEST_LEN);
304 router2_id = tor_malloc(DIGEST_LEN);
305 memset(router2_id, TEST_DIR_ROUTER_ID_2, DIGEST_LEN);
306 router3_id = tor_malloc(DIGEST_LEN);
307 memset(router3_id, TEST_DIR_ROUTER_ID_3, DIGEST_LEN);
309 node_router1 = node_get_mutable_by_id(router1_id);
310 node_router2 = node_get_mutable_by_id(router2_id);
311 node_router3 = node_get_mutable_by_id(router3_id);
313 node_router1->is_possible_guard = 1;
315 node_router1->is_running = 0;
316 node_router3->is_running = 0;
317 rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
318 tt_ptr_op(rs, OP_NE, NULL);
319 tt_assert(tor_memeq(rs->identity_digest, router2_id, DIGEST_LEN));
320 rs = NULL;
321 node_router1->is_running = 1;
322 node_router3->is_running = 1;
324 node_router1->rs->is_v2_dir = 0;
325 node_router3->rs->is_v2_dir = 0;
326 tmp_dirport1 = node_router1->rs->dir_port;
327 tmp_dirport3 = node_router3->rs->dir_port;
328 node_router1->rs->dir_port = 0;
329 node_router3->rs->dir_port = 0;
330 rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
331 tt_ptr_op(rs, OP_NE, NULL);
332 tt_assert(tor_memeq(rs->identity_digest, router2_id, DIGEST_LEN));
333 rs = NULL;
334 node_router1->rs->is_v2_dir = 1;
335 node_router3->rs->is_v2_dir = 1;
336 node_router1->rs->dir_port = tmp_dirport1;
337 node_router3->rs->dir_port = tmp_dirport3;
339 node_router1->is_valid = 0;
340 node_router3->is_valid = 0;
341 rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
342 tt_ptr_op(rs, OP_NE, NULL);
343 tt_assert(tor_memeq(rs->identity_digest, router2_id, DIGEST_LEN));
344 rs = NULL;
345 node_router1->is_valid = 1;
346 node_router3->is_valid = 1;
348 /* Manipulate overloaded */
350 node_router2->rs->last_dir_503_at = now;
351 node_router3->rs->last_dir_503_at = now;
352 rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
353 tt_ptr_op(rs, OP_NE, NULL);
354 tt_assert(tor_memeq(rs->identity_digest, router1_id, DIGEST_LEN));
355 node_router2->rs->last_dir_503_at = 0;
356 node_router3->rs->last_dir_503_at = 0;
358 /* Set a Fascist firewall */
359 flags &= ~ PDS_IGNORE_FASCISTFIREWALL;
360 policy_line = tor_malloc_zero(sizeof(config_line_t));
361 policy_line->key = tor_strdup("ReachableORAddresses");
362 policy_line->value = tor_strdup("accept *:442, reject *:*");
363 options->ReachableORAddresses = policy_line;
364 policies_parse_from_options(options);
366 node_router1->rs->or_port = 444;
367 node_router2->rs->or_port = 443;
368 node_router3->rs->or_port = 442;
369 rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
370 tt_ptr_op(rs, OP_NE, NULL);
371 tt_assert(tor_memeq(rs->identity_digest, router3_id, DIGEST_LEN));
372 node_router1->rs->or_port = 442;
373 node_router2->rs->or_port = 443;
374 node_router3->rs->or_port = 444;
375 rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
376 tt_ptr_op(rs, OP_NE, NULL);
377 tt_assert(tor_memeq(rs->identity_digest, router1_id, DIGEST_LEN));
379 /* Fascist firewall and overloaded */
380 node_router1->rs->or_port = 442;
381 node_router2->rs->or_port = 443;
382 node_router3->rs->or_port = 442;
383 node_router3->rs->last_dir_503_at = now;
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, router1_id, DIGEST_LEN));
387 node_router3->rs->last_dir_503_at = 0;
389 /* Fascists against OR and Dir */
390 policy_line = tor_malloc_zero(sizeof(config_line_t));
391 policy_line->key = tor_strdup("ReachableAddresses");
392 policy_line->value = tor_strdup("accept *:80, reject *:*");
393 options->ReachableDirAddresses = policy_line;
394 policies_parse_from_options(options);
395 node_router1->rs->or_port = 442;
396 node_router2->rs->or_port = 441;
397 node_router3->rs->or_port = 443;
398 node_router1->rs->dir_port = 80;
399 node_router2->rs->dir_port = 80;
400 node_router3->rs->dir_port = 81;
401 node_router1->rs->last_dir_503_at = now;
402 rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
403 tt_ptr_op(rs, OP_NE, NULL);
404 tt_assert(tor_memeq(rs->identity_digest, router1_id, DIGEST_LEN));
405 node_router1->rs->last_dir_503_at = 0;
407 done:
408 UNMOCK(usable_consensus_flavor);
410 if (router1_id)
411 tor_free(router1_id);
412 if (router2_id)
413 tor_free(router2_id);
414 if (router3_id)
415 tor_free(router3_id);
416 if (options->ReachableORAddresses ||
417 options->ReachableDirAddresses)
418 policies_free_all();
419 tor_free(consensus_text_md);
420 networkstatus_vote_free(con_md);
423 static or_state_t *dummy_state = NULL;
424 static or_state_t *
425 get_or_state_replacement(void)
427 return dummy_state;
430 static void
431 mock_directory_initiate_request(directory_request_t *req)
433 (void)req;
434 return;
437 static circuit_guard_state_t *
438 mock_circuit_guard_state_new(entry_guard_t *guard, unsigned state,
439 entry_guard_restriction_t *rst)
441 (void) guard;
442 (void) state;
443 (void) rst;
444 return NULL;
447 /** Test that we will use our directory guards to fetch mds even if we don't
448 * have any dirinfo (tests bug #23862). */
449 static void
450 test_directory_guard_fetch_with_no_dirinfo(void *arg)
452 int retval;
453 char *consensus_text_md = NULL;
454 or_options_t *options = get_options_mutable();
456 (void) arg;
458 hibernate_set_state_for_testing_(HIBERNATE_STATE_LIVE);
460 /* Initialize the SRV subsystem */
461 MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
462 mock_cert = authority_cert_parse_from_string(AUTHORITY_CERT_1, NULL);
463 sr_init(0);
464 UNMOCK(get_my_v3_authority_cert);
466 /* Initialize the entry node configuration from the ticket */
467 options->UseEntryGuards = 1;
468 options->StrictNodes = 1;
469 get_options_mutable()->EntryNodes = routerset_new();
470 routerset_parse(get_options_mutable()->EntryNodes,
471 "2121212121212121212121212121212121212121", "foo");
473 /* Mock some functions */
474 dummy_state = tor_malloc_zero(sizeof(or_state_t));
475 MOCK(get_or_state, get_or_state_replacement);
476 MOCK(directory_initiate_request, mock_directory_initiate_request);
477 /* we need to mock this one to avoid memleaks */
478 MOCK(circuit_guard_state_new, mock_circuit_guard_state_new);
480 /* Call guards_update_all() to simulate loading our state file (see
481 * entry_guards_load_guards_from_state() and ticket #23989). */
482 guards_update_all();
484 /* Test logic: Simulate the arrival of a new consensus when we have no
485 * dirinfo at all. Tor will need to fetch the mds from the consensus. Make
486 * sure that Tor will use the specified entry guard instead of relying on the
487 * fallback directories. */
489 /* Fixup the dirconn that will deliver the consensus */
490 dir_connection_t *conn = dir_connection_new(AF_INET);
491 tor_addr_from_ipv4h(&conn->base_.addr, 0x7f000001);
492 conn->base_.port = 8800;
493 TO_CONN(conn)->address = tor_strdup("127.0.0.1");
494 conn->base_.purpose = DIR_PURPOSE_FETCH_CONSENSUS;
495 conn->requested_resource = tor_strdup("ns");
497 /* Construct a consensus */
498 construct_consensus(&consensus_text_md);
499 tt_assert(consensus_text_md);
501 /* Place the consensus in the dirconn */
502 response_handler_args_t args;
503 memset(&args, 0, sizeof(response_handler_args_t));
504 args.status_code = 200;
505 args.body = consensus_text_md;
506 args.body_len = strlen(consensus_text_md);
508 /* Update approx time so that the consensus is considered live */
509 update_approx_time(time(NULL)+1010);
511 setup_capture_of_logs(LOG_DEBUG);
513 /* Now handle the consensus */
514 retval = handle_response_fetch_consensus(conn, &args);
515 tt_int_op(retval, OP_EQ, 0);
517 /* Make sure that our primary guard was chosen */
518 expect_log_msg_containing("Selected primary guard router3");
520 done:
521 tor_free(consensus_text_md);
522 tor_free(dummy_state);
523 connection_free_minimal(TO_CONN(conn));
524 entry_guards_free_all();
525 teardown_capture_of_logs();
528 static connection_t *mocked_connection = NULL;
530 /* Mock connection_get_by_type_addr_port_purpose by returning
531 * mocked_connection. */
532 static connection_t *
533 mock_connection_get_by_type_addr_port_purpose(int type,
534 const tor_addr_t *addr,
535 uint16_t port, int purpose)
537 (void)type;
538 (void)addr;
539 (void)port;
540 (void)purpose;
542 return mocked_connection;
545 #define TEST_ADDR_STR "127.0.0.1"
546 #define TEST_DIR_PORT 12345
548 static void
549 test_routerlist_router_is_already_dir_fetching(void *arg)
551 (void)arg;
552 tor_addr_port_t test_ap, null_addr_ap, zero_port_ap;
554 /* Setup */
555 tor_addr_parse(&test_ap.addr, TEST_ADDR_STR);
556 test_ap.port = TEST_DIR_PORT;
557 tor_addr_make_null(&null_addr_ap.addr, AF_INET6);
558 null_addr_ap.port = TEST_DIR_PORT;
559 tor_addr_parse(&zero_port_ap.addr, TEST_ADDR_STR);
560 zero_port_ap.port = 0;
561 MOCK(connection_get_by_type_addr_port_purpose,
562 mock_connection_get_by_type_addr_port_purpose);
564 /* Test that we never get 1 from a NULL connection */
565 mocked_connection = NULL;
566 tt_int_op(router_is_already_dir_fetching(&test_ap, 1, 1), OP_EQ, 0);
567 tt_int_op(router_is_already_dir_fetching(&test_ap, 1, 0), OP_EQ, 0);
568 tt_int_op(router_is_already_dir_fetching(&test_ap, 0, 1), OP_EQ, 0);
569 /* We always expect 0 in these cases */
570 tt_int_op(router_is_already_dir_fetching(&test_ap, 0, 0), OP_EQ, 0);
571 tt_int_op(router_is_already_dir_fetching(NULL, 1, 1), OP_EQ, 0);
572 tt_int_op(router_is_already_dir_fetching(&null_addr_ap, 1, 1), OP_EQ, 0);
573 tt_int_op(router_is_already_dir_fetching(&zero_port_ap, 1, 1), OP_EQ, 0);
575 /* Test that we get 1 with a connection in the appropriate circumstances */
576 mocked_connection = connection_new(CONN_TYPE_DIR, AF_INET);
577 tt_int_op(router_is_already_dir_fetching(&test_ap, 1, 1), OP_EQ, 1);
578 tt_int_op(router_is_already_dir_fetching(&test_ap, 1, 0), OP_EQ, 1);
579 tt_int_op(router_is_already_dir_fetching(&test_ap, 0, 1), OP_EQ, 1);
581 /* Test that we get 0 even with a connection in the appropriate
582 * circumstances */
583 tt_int_op(router_is_already_dir_fetching(&test_ap, 0, 0), OP_EQ, 0);
584 tt_int_op(router_is_already_dir_fetching(NULL, 1, 1), OP_EQ, 0);
585 tt_int_op(router_is_already_dir_fetching(&null_addr_ap, 1, 1), OP_EQ, 0);
586 tt_int_op(router_is_already_dir_fetching(&zero_port_ap, 1, 1), OP_EQ, 0);
588 done:
589 /* If a connection is never set up, connection_free chokes on it. */
590 if (mocked_connection) {
591 buf_free(mocked_connection->inbuf);
592 buf_free(mocked_connection->outbuf);
594 tor_free(mocked_connection);
595 UNMOCK(connection_get_by_type_addr_port_purpose);
598 #undef TEST_ADDR_STR
599 #undef TEST_DIR_PORT
601 #define NODE(name, flags) \
602 { #name, test_routerlist_##name, (flags), NULL, NULL }
603 #define ROUTER(name,flags) \
604 { #name, test_router_##name, (flags), NULL, NULL }
606 struct testcase_t routerlist_tests[] = {
607 NODE(initiate_descriptor_downloads, 0),
608 NODE(launch_descriptor_downloads, 0),
609 NODE(router_is_already_dir_fetching, TT_FORK),
610 ROUTER(pick_directory_server_impl, TT_FORK),
611 { "directory_guard_fetch_with_no_dirinfo",
612 test_directory_guard_fetch_with_no_dirinfo, TT_FORK, NULL, NULL },
613 END_OF_TESTCASES