Fix a clang unitialized-var warning
[tor.git] / src / test / test_entrynodes.c
blob43cc394888d093b33ad22919db13236de062bcd3
1 /* Copyright (c) 2014-2017, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 #include "orconfig.h"
6 #define CIRCUITLIST_PRIVATE
7 #define STATEFILE_PRIVATE
8 #define ENTRYNODES_PRIVATE
9 #define ROUTERLIST_PRIVATE
10 #define DIRECTORY_PRIVATE
12 #include "or.h"
13 #include "test.h"
15 #include "bridges.h"
16 #include "circuitlist.h"
17 #include "config.h"
18 #include "confparse.h"
19 #include "directory.h"
20 #include "entrynodes.h"
21 #include "nodelist.h"
22 #include "networkstatus.h"
23 #include "policies.h"
24 #include "routerlist.h"
25 #include "routerparse.h"
26 #include "routerset.h"
27 #include "statefile.h"
28 #include "util.h"
30 #include "test_helpers.h"
31 #include "log_test_helpers.h"
33 /* TODO:
34 * choose_random_entry() test with state set.
36 * parse_state() tests with more than one guards.
38 * More tests for set_from_config(): Multiple nodes, use fingerprints,
39 * use country codes.
42 /** Dummy Tor state used in unittests. */
43 static or_state_t *dummy_state = NULL;
44 static or_state_t *
45 get_or_state_replacement(void)
47 return dummy_state;
50 static networkstatus_t *dummy_consensus = NULL;
52 static smartlist_t *big_fake_net_nodes = NULL;
54 static smartlist_t *
55 bfn_mock_nodelist_get_list(void)
57 return big_fake_net_nodes;
60 static networkstatus_t *
61 bfn_mock_networkstatus_get_live_consensus(time_t now)
63 (void)now;
64 return dummy_consensus;
67 static const node_t *
68 bfn_mock_node_get_by_id(const char *id)
70 SMARTLIST_FOREACH(big_fake_net_nodes, node_t *, n,
71 if (fast_memeq(n->identity, id, 20))
72 return n);
74 return NULL;
77 /* Unittest cleanup function: Cleanup the fake network. */
78 static int
79 big_fake_network_cleanup(const struct testcase_t *testcase, void *ptr)
81 (void) testcase;
82 (void) ptr;
84 if (big_fake_net_nodes) {
85 SMARTLIST_FOREACH(big_fake_net_nodes, node_t *, n, {
86 tor_free(n->rs);
87 tor_free(n->md);
88 tor_free(n);
89 });
90 smartlist_free(big_fake_net_nodes);
93 UNMOCK(nodelist_get_list);
94 UNMOCK(node_get_by_id);
95 UNMOCK(get_or_state);
96 UNMOCK(networkstatus_get_live_consensus);
97 or_state_free(dummy_state);
98 dummy_state = NULL;
99 tor_free(dummy_consensus);
101 return 1; /* NOP */
104 /* Unittest setup function: Setup a fake network. */
105 static void *
106 big_fake_network_setup(const struct testcase_t *testcase)
108 int i;
110 /* These are minimal node_t objects that only contain the aspects of node_t
111 * that we need for entrynodes.c. */
112 const int N_NODES = 271;
114 big_fake_net_nodes = smartlist_new();
115 for (i = 0; i < N_NODES; ++i) {
116 node_t *n = tor_malloc_zero(sizeof(node_t));
117 n->md = tor_malloc_zero(sizeof(microdesc_t));
119 crypto_rand(n->identity, sizeof(n->identity));
120 n->rs = tor_malloc_zero(sizeof(routerstatus_t));
122 memcpy(n->rs->identity_digest, n->identity, DIGEST_LEN);
124 n->is_running = n->is_valid = n->is_fast = n->is_stable = 1;
126 /* Note: all these guards have the same address, so you'll need to
127 * disable EnforceDistinctSubnets when a restriction is applied. */
128 n->rs->addr = 0x04020202;
129 n->rs->or_port = 1234;
130 n->rs->is_v2_dir = 1;
131 n->rs->has_bandwidth = 1;
132 n->rs->bandwidth_kb = 30;
134 /* Make a random nickname for each node */
136 char nickname_binary[8];
137 crypto_rand(nickname_binary, sizeof(nickname_binary));
138 base64_encode(n->rs->nickname, sizeof(n->rs->nickname),
139 nickname_binary, sizeof(nickname_binary), 0);
142 /* Call half of the nodes a possible guard. */
143 if (i % 2 == 0) {
144 n->is_possible_guard = 1;
145 n->rs->guardfraction_percentage = 100;
146 n->rs->has_guardfraction = 1;
149 smartlist_add(big_fake_net_nodes, n);
152 dummy_state = tor_malloc_zero(sizeof(or_state_t));
153 dummy_consensus = tor_malloc_zero(sizeof(networkstatus_t));
154 dummy_consensus->valid_after = approx_time() - 3600;
155 dummy_consensus->valid_until = approx_time() + 3600;
157 MOCK(nodelist_get_list, bfn_mock_nodelist_get_list);
158 MOCK(node_get_by_id, bfn_mock_node_get_by_id);
159 MOCK(get_or_state,
160 get_or_state_replacement);
161 MOCK(networkstatus_get_live_consensus,
162 bfn_mock_networkstatus_get_live_consensus);
163 /* Return anything but NULL (it's interpreted as test fail) */
164 return (void*)testcase;
167 static time_t
168 mock_randomize_time_no_randomization(time_t a, time_t b)
170 (void) b;
171 return a;
174 static or_options_t mocked_options;
176 static const or_options_t *
177 mock_get_options(void)
179 return &mocked_options;
182 #define TEST_IPV4_ADDR "123.45.67.89"
183 #define TEST_IPV6_ADDR "[1234:5678:90ab:cdef::]"
185 static void
186 test_node_preferred_orport(void *arg)
188 (void)arg;
189 tor_addr_t ipv4_addr;
190 const uint16_t ipv4_port = 4444;
191 tor_addr_t ipv6_addr;
192 const uint16_t ipv6_port = 6666;
193 routerinfo_t node_ri;
194 node_t node;
195 tor_addr_port_t ap;
197 /* Setup options */
198 memset(&mocked_options, 0, sizeof(mocked_options));
199 /* We don't test ClientPreferIPv6ORPort here, because it's used in
200 * nodelist_set_consensus to setup node.ipv6_preferred, which we set
201 * directly. */
202 MOCK(get_options, mock_get_options);
204 /* Setup IP addresses */
205 tor_addr_parse(&ipv4_addr, TEST_IPV4_ADDR);
206 tor_addr_parse(&ipv6_addr, TEST_IPV6_ADDR);
208 /* Setup node_ri */
209 memset(&node_ri, 0, sizeof(node_ri));
210 node_ri.addr = tor_addr_to_ipv4h(&ipv4_addr);
211 node_ri.or_port = ipv4_port;
212 tor_addr_copy(&node_ri.ipv6_addr, &ipv6_addr);
213 node_ri.ipv6_orport = ipv6_port;
215 /* Setup node */
216 memset(&node, 0, sizeof(node));
217 node.ri = &node_ri;
219 /* Check the preferred address is IPv4 if we're only using IPv4, regardless
220 * of whether we prefer it or not */
221 mocked_options.ClientUseIPv4 = 1;
222 mocked_options.ClientUseIPv6 = 0;
223 node.ipv6_preferred = 0;
224 node_get_pref_orport(&node, &ap);
225 tt_assert(tor_addr_eq(&ap.addr, &ipv4_addr));
226 tt_assert(ap.port == ipv4_port);
228 node.ipv6_preferred = 1;
229 node_get_pref_orport(&node, &ap);
230 tt_assert(tor_addr_eq(&ap.addr, &ipv4_addr));
231 tt_assert(ap.port == ipv4_port);
233 /* Check the preferred address is IPv4 if we're using IPv4 and IPv6, but
234 * don't prefer the IPv6 address */
235 mocked_options.ClientUseIPv4 = 1;
236 mocked_options.ClientUseIPv6 = 1;
237 node.ipv6_preferred = 0;
238 node_get_pref_orport(&node, &ap);
239 tt_assert(tor_addr_eq(&ap.addr, &ipv4_addr));
240 tt_assert(ap.port == ipv4_port);
242 /* Check the preferred address is IPv6 if we prefer it and
243 * ClientUseIPv6 is 1, regardless of ClientUseIPv4 */
244 mocked_options.ClientUseIPv4 = 1;
245 mocked_options.ClientUseIPv6 = 1;
246 node.ipv6_preferred = 1;
247 node_get_pref_orport(&node, &ap);
248 tt_assert(tor_addr_eq(&ap.addr, &ipv6_addr));
249 tt_assert(ap.port == ipv6_port);
251 mocked_options.ClientUseIPv4 = 0;
252 node_get_pref_orport(&node, &ap);
253 tt_assert(tor_addr_eq(&ap.addr, &ipv6_addr));
254 tt_assert(ap.port == ipv6_port);
256 /* Check the preferred address is IPv6 if we don't prefer it, but
257 * ClientUseIPv4 is 0 */
258 mocked_options.ClientUseIPv4 = 0;
259 mocked_options.ClientUseIPv6 = 1;
260 node.ipv6_preferred = fascist_firewall_prefer_ipv6_orport(&mocked_options);
261 node_get_pref_orport(&node, &ap);
262 tt_assert(tor_addr_eq(&ap.addr, &ipv6_addr));
263 tt_assert(ap.port == ipv6_port);
265 done:
266 UNMOCK(get_options);
269 static void
270 test_entry_guard_describe(void *arg)
272 (void)arg;
273 entry_guard_t g;
274 memset(&g, 0, sizeof(g));
275 strlcpy(g.nickname, "okefenokee", sizeof(g.nickname));
276 memcpy(g.identity, "theforestprimeval---", DIGEST_LEN);
278 tt_str_op(entry_guard_describe(&g), OP_EQ,
279 "okefenokee ($746865666F726573747072696D6576616C2D2D2D)");
281 done:
285 static void
286 test_entry_guard_randomize_time(void *arg)
288 const time_t now = 1479153573;
289 const int delay = 86400;
290 const int N = 1000;
291 (void)arg;
293 time_t t;
294 int i;
295 for (i = 0; i < N; ++i) {
296 t = randomize_time(now, delay);
297 tt_int_op(t, OP_LE, now);
298 tt_int_op(t, OP_GE, now-delay);
301 /* now try the corner cases */
302 for (i = 0; i < N; ++i) {
303 t = randomize_time(100, delay);
304 tt_int_op(t, OP_GE, 1);
305 tt_int_op(t, OP_LE, 100);
307 t = randomize_time(0, delay);
308 tt_int_op(t, OP_EQ, 1);
311 done:
315 static void
316 test_entry_guard_encode_for_state_minimal(void *arg)
318 (void) arg;
319 entry_guard_t *eg = tor_malloc_zero(sizeof(entry_guard_t));
321 eg->selection_name = tor_strdup("wubwub");
322 memcpy(eg->identity, "plurpyflurpyslurpydo", DIGEST_LEN);
323 eg->sampled_on_date = 1479081600;
324 eg->confirmed_idx = -1;
326 char *s = NULL;
327 s = entry_guard_encode_for_state(eg);
329 tt_str_op(s, OP_EQ,
330 "in=wubwub "
331 "rsa_id=706C75727079666C75727079736C75727079646F "
332 "sampled_on=2016-11-14T00:00:00 "
333 "listed=0");
335 done:
336 entry_guard_free(eg);
337 tor_free(s);
340 static void
341 test_entry_guard_encode_for_state_maximal(void *arg)
343 (void) arg;
344 entry_guard_t *eg = tor_malloc_zero(sizeof(entry_guard_t));
346 strlcpy(eg->nickname, "Fred", sizeof(eg->nickname));
347 eg->selection_name = tor_strdup("default");
348 memcpy(eg->identity, "plurpyflurpyslurpydo", DIGEST_LEN);
349 eg->bridge_addr = tor_malloc_zero(sizeof(tor_addr_port_t));
350 tor_addr_from_ipv4h(&eg->bridge_addr->addr, 0x08080404);
351 eg->bridge_addr->port = 9999;
352 eg->sampled_on_date = 1479081600;
353 eg->sampled_by_version = tor_strdup("1.2.3");
354 eg->unlisted_since_date = 1479081645;
355 eg->currently_listed = 1;
356 eg->confirmed_on_date = 1479081690;
357 eg->confirmed_idx = 333;
358 eg->extra_state_fields = tor_strdup("and the green grass grew all around");
360 char *s = NULL;
361 s = entry_guard_encode_for_state(eg);
363 tt_str_op(s, OP_EQ,
364 "in=default "
365 "rsa_id=706C75727079666C75727079736C75727079646F "
366 "bridge_addr=8.8.4.4:9999 "
367 "nickname=Fred "
368 "sampled_on=2016-11-14T00:00:00 "
369 "sampled_by=1.2.3 "
370 "unlisted_since=2016-11-14T00:00:45 "
371 "listed=1 "
372 "confirmed_on=2016-11-14T00:01:30 "
373 "confirmed_idx=333 "
374 "and the green grass grew all around");
376 done:
377 entry_guard_free(eg);
378 tor_free(s);
381 static void
382 test_entry_guard_parse_from_state_minimal(void *arg)
384 (void)arg;
385 char *mem_op_hex_tmp = NULL;
386 entry_guard_t *eg = NULL;
387 time_t t = approx_time();
389 eg = entry_guard_parse_from_state(
390 "in=default_plus "
391 "rsa_id=596f75206d6179206e656564206120686f626279");
392 tt_assert(eg);
394 tt_str_op(eg->selection_name, OP_EQ, "default_plus");
395 test_mem_op_hex(eg->identity, OP_EQ,
396 "596f75206d6179206e656564206120686f626279");
397 tt_str_op(eg->nickname, OP_EQ, "$596F75206D6179206E656564206120686F626279");
398 tt_ptr_op(eg->bridge_addr, OP_EQ, NULL);
399 tt_i64_op(eg->sampled_on_date, OP_GE, t);
400 tt_i64_op(eg->sampled_on_date, OP_LE, t+86400);
401 tt_i64_op(eg->unlisted_since_date, OP_EQ, 0);
402 tt_ptr_op(eg->sampled_by_version, OP_EQ, NULL);
403 tt_int_op(eg->currently_listed, OP_EQ, 0);
404 tt_i64_op(eg->confirmed_on_date, OP_EQ, 0);
405 tt_int_op(eg->confirmed_idx, OP_EQ, -1);
407 tt_int_op(eg->last_tried_to_connect, OP_EQ, 0);
408 tt_int_op(eg->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
410 done:
411 entry_guard_free(eg);
412 tor_free(mem_op_hex_tmp);
415 static void
416 test_entry_guard_parse_from_state_maximal(void *arg)
418 (void)arg;
419 char *mem_op_hex_tmp = NULL;
420 entry_guard_t *eg = NULL;
422 eg = entry_guard_parse_from_state(
423 "in=fred "
424 "rsa_id=706C75727079666C75727079736C75727079646F "
425 "bridge_addr=[1::3]:9999 "
426 "nickname=Fred "
427 "sampled_on=2016-11-14T00:00:00 "
428 "sampled_by=1.2.3 "
429 "unlisted_since=2016-11-14T00:00:45 "
430 "listed=1 "
431 "confirmed_on=2016-11-14T00:01:30 "
432 "confirmed_idx=333 "
433 "and the green grass grew all around "
434 "rsa_id=all,around");
435 tt_assert(eg);
437 test_mem_op_hex(eg->identity, OP_EQ,
438 "706C75727079666C75727079736C75727079646F");
439 tt_str_op(fmt_addr(&eg->bridge_addr->addr), OP_EQ, "1::3");
440 tt_int_op(eg->bridge_addr->port, OP_EQ, 9999);
441 tt_str_op(eg->nickname, OP_EQ, "Fred");
442 tt_i64_op(eg->sampled_on_date, OP_EQ, 1479081600);
443 tt_i64_op(eg->unlisted_since_date, OP_EQ, 1479081645);
444 tt_str_op(eg->sampled_by_version, OP_EQ, "1.2.3");
445 tt_int_op(eg->currently_listed, OP_EQ, 1);
446 tt_i64_op(eg->confirmed_on_date, OP_EQ, 1479081690);
447 tt_int_op(eg->confirmed_idx, OP_EQ, 333);
448 tt_str_op(eg->extra_state_fields, OP_EQ,
449 "and the green grass grew all around rsa_id=all,around");
451 tt_int_op(eg->last_tried_to_connect, OP_EQ, 0);
452 tt_int_op(eg->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
454 done:
455 entry_guard_free(eg);
456 tor_free(mem_op_hex_tmp);
459 static void
460 test_entry_guard_parse_from_state_failure(void *arg)
462 (void)arg;
463 entry_guard_t *eg = NULL;
465 /* no selection */
466 eg = entry_guard_parse_from_state(
467 "rsa_id=596f75206d6179206e656564206120686f626270");
468 tt_ptr_op(eg, OP_EQ, NULL);
470 /* no RSA ID. */
471 eg = entry_guard_parse_from_state("in=default nickname=Fred");
472 tt_ptr_op(eg, OP_EQ, NULL);
474 /* Bad RSA ID: bad character. */
475 eg = entry_guard_parse_from_state(
476 "in=default "
477 "rsa_id=596f75206d6179206e656564206120686f62627q");
478 tt_ptr_op(eg, OP_EQ, NULL);
480 /* Bad RSA ID: too long.*/
481 eg = entry_guard_parse_from_state(
482 "in=default "
483 "rsa_id=596f75206d6179206e656564206120686f6262703");
484 tt_ptr_op(eg, OP_EQ, NULL);
486 /* Bad RSA ID: too short.*/
487 eg = entry_guard_parse_from_state(
488 "in=default "
489 "rsa_id=596f75206d6179206e65656420612");
490 tt_ptr_op(eg, OP_EQ, NULL);
492 done:
493 entry_guard_free(eg);
496 static void
497 test_entry_guard_parse_from_state_partial_failure(void *arg)
499 (void)arg;
500 char *mem_op_hex_tmp = NULL;
501 entry_guard_t *eg = NULL;
502 time_t t = approx_time();
504 eg = entry_guard_parse_from_state(
505 "in=default "
506 "rsa_id=706C75727079666C75727079736C75727079646F "
507 "bridge_addr=1.2.3.3.4:5 "
508 "nickname=FredIsANodeWithAStrangeNicknameThatIsTooLong "
509 "sampled_on=2016-11-14T00:00:99 "
510 "sampled_by=1.2.3 stuff in the middle "
511 "unlisted_since=2016-xx-14T00:00:45 "
512 "listed=0 "
513 "confirmed_on=2016-11-14T00:01:30zz "
514 "confirmed_idx=idx "
515 "and the green grass grew all around "
516 "rsa_id=all,around");
517 tt_assert(eg);
519 test_mem_op_hex(eg->identity, OP_EQ,
520 "706C75727079666C75727079736C75727079646F");
521 tt_str_op(eg->nickname, OP_EQ, "FredIsANodeWithAStrangeNicknameThatIsTooL");
522 tt_ptr_op(eg->bridge_addr, OP_EQ, NULL);
523 tt_i64_op(eg->sampled_on_date, OP_EQ, t);
524 tt_i64_op(eg->unlisted_since_date, OP_EQ, 0);
525 tt_str_op(eg->sampled_by_version, OP_EQ, "1.2.3");
526 tt_int_op(eg->currently_listed, OP_EQ, 0);
527 tt_i64_op(eg->confirmed_on_date, OP_EQ, 0);
528 tt_int_op(eg->confirmed_idx, OP_EQ, -1);
529 tt_str_op(eg->extra_state_fields, OP_EQ,
530 "stuff in the middle and the green grass grew all around "
531 "rsa_id=all,around");
533 tt_int_op(eg->last_tried_to_connect, OP_EQ, 0);
534 tt_int_op(eg->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
536 done:
537 entry_guard_free(eg);
538 tor_free(mem_op_hex_tmp);
541 static int
542 mock_entry_guard_is_listed(guard_selection_t *gs, const entry_guard_t *guard)
544 (void)gs;
545 (void)guard;
546 return 1;
549 static void
550 test_entry_guard_parse_from_state_full(void *arg)
552 (void)arg;
553 /* Here's a state I made while testing. The identities and locations for
554 * the bridges are redacted. */
555 const char STATE[] =
556 "Guard in=default rsa_id=214F44BD5B638E8C817D47FF7C97397790BF0345 "
557 "nickname=TotallyNinja sampled_on=2016-11-12T19:32:49 "
558 "sampled_by=0.3.0.0-alpha-dev "
559 "listed=1\n"
560 "Guard in=default rsa_id=052900AB0EA3ED54BAB84AE8A99E74E8693CE2B2 "
561 "nickname=5OfNovember sampled_on=2016-11-20T04:32:05 "
562 "sampled_by=0.3.0.0-alpha-dev "
563 "listed=1 confirmed_on=2016-11-22T08:13:28 confirmed_idx=0 "
564 "pb_circ_attempts=4.000000 pb_circ_successes=2.000000 "
565 "pb_successful_circuits_closed=2.000000\n"
566 "Guard in=default rsa_id=7B700C0C207EBD0002E00F499BE265519AC3C25A "
567 "nickname=dc6jgk11 sampled_on=2016-11-28T11:50:13 "
568 "sampled_by=0.3.0.0-alpha-dev "
569 "listed=1 confirmed_on=2016-11-24T08:45:30 confirmed_idx=4 "
570 "pb_circ_attempts=5.000000 pb_circ_successes=5.000000 "
571 "pb_successful_circuits_closed=5.000000\n"
572 "Guard in=wobblesome rsa_id=7B700C0C207EBD0002E00F499BE265519AC3C25A "
573 "nickname=dc6jgk11 sampled_on=2016-11-28T11:50:13 "
574 "sampled_by=0.3.0.0-alpha-dev "
575 "listed=1\n"
576 "Guard in=default rsa_id=E9025AD60D86875D5F11548D536CC6AF60F0EF5E "
577 "nickname=maibrunn sampled_on=2016-11-25T22:36:38 "
578 "sampled_by=0.3.0.0-alpha-dev listed=1\n"
579 "Guard in=default rsa_id=DCD30B90BA3A792DA75DC54A327EF353FB84C38E "
580 "nickname=Unnamed sampled_on=2016-11-25T14:34:00 "
581 "sampled_by=0.3.0.0-alpha-dev listed=1\n"
582 "Guard in=bridges rsa_id=8FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2E "
583 "bridge_addr=24.1.1.1:443 sampled_on=2016-11-25T06:44:14 "
584 "sampled_by=0.3.0.0-alpha-dev listed=1 "
585 "confirmed_on=2016-11-29T10:36:06 confirmed_idx=0 "
586 "pb_circ_attempts=8.000000 pb_circ_successes=8.000000 "
587 "pb_successful_circuits_closed=13.000000\n"
588 "Guard in=bridges rsa_id=5800000000000000000000000000000000000000 "
589 "bridge_addr=37.218.246.143:28366 "
590 "sampled_on=2016-11-18T15:07:34 sampled_by=0.3.0.0-alpha-dev listed=1\n";
592 config_line_t *lines = NULL;
593 or_state_t *state = tor_malloc_zero(sizeof(or_state_t));
594 int r = config_get_lines(STATE, &lines, 0);
595 char *msg = NULL;
596 smartlist_t *text = smartlist_new();
597 char *joined = NULL;
599 // So nodes aren't expired. This is Tue, 13 Dec 2016 09:37:14 GMT
600 update_approx_time(1481621834);
602 MOCK(entry_guard_is_listed, mock_entry_guard_is_listed);
604 dummy_state = state;
605 MOCK(get_or_state,
606 get_or_state_replacement);
608 tt_int_op(r, OP_EQ, 0);
609 tt_assert(lines);
611 state->Guard = lines;
613 /* Try it first without setting the result. */
614 r = entry_guards_parse_state(state, 0, &msg);
615 tt_int_op(r, OP_EQ, 0);
616 guard_selection_t *gs_br =
617 get_guard_selection_by_name("bridges", GS_TYPE_BRIDGE, 0);
618 tt_ptr_op(gs_br, OP_EQ, NULL);
620 r = entry_guards_parse_state(state, 1, &msg);
621 tt_int_op(r, OP_EQ, 0);
622 gs_br = get_guard_selection_by_name("bridges", GS_TYPE_BRIDGE, 0);
623 guard_selection_t *gs_df =
624 get_guard_selection_by_name("default", GS_TYPE_NORMAL, 0);
625 guard_selection_t *gs_wb =
626 get_guard_selection_by_name("wobblesome", GS_TYPE_NORMAL, 0);
628 tt_assert(gs_br);
629 tt_assert(gs_df);
630 tt_assert(gs_wb);
632 tt_int_op(smartlist_len(gs_df->sampled_entry_guards), OP_EQ, 5);
633 tt_int_op(smartlist_len(gs_br->sampled_entry_guards), OP_EQ, 2);
634 tt_int_op(smartlist_len(gs_wb->sampled_entry_guards), OP_EQ, 1);
636 /* Try again; make sure it doesn't double-add the guards. */
637 r = entry_guards_parse_state(state, 1, &msg);
638 tt_int_op(r, OP_EQ, 0);
639 gs_br = get_guard_selection_by_name("bridges", GS_TYPE_BRIDGE, 0);
640 gs_df = get_guard_selection_by_name("default", GS_TYPE_NORMAL, 0);
641 tt_assert(gs_br);
642 tt_assert(gs_df);
643 tt_int_op(smartlist_len(gs_df->sampled_entry_guards), OP_EQ, 5);
644 tt_int_op(smartlist_len(gs_br->sampled_entry_guards), OP_EQ, 2);
646 /* Re-encode; it should be the same... almost. */
648 /* (Make a guard nonpersistent first) */
649 entry_guard_t *g = smartlist_get(gs_df->sampled_entry_guards, 0);
650 g->is_persistent = 0;
652 config_free_lines(lines);
653 lines = state->Guard = NULL; // to prevent double-free.
654 entry_guards_update_state(state);
655 tt_assert(state->Guard);
656 lines = state->Guard;
658 config_line_t *ln;
659 for (ln = lines; ln; ln = ln->next) {
660 smartlist_add_asprintf(text, "%s %s\n",ln->key, ln->value);
662 joined = smartlist_join_strings(text, "", 0, NULL);
663 tt_str_op(joined, OP_EQ,
664 "Guard in=default rsa_id=052900AB0EA3ED54BAB84AE8A99E74E8693CE2B2 "
665 "nickname=5OfNovember sampled_on=2016-11-20T04:32:05 "
666 "sampled_by=0.3.0.0-alpha-dev "
667 "listed=1 confirmed_on=2016-11-22T08:13:28 confirmed_idx=0 "
668 "pb_circ_attempts=4.000000 pb_circ_successes=2.000000 "
669 "pb_successful_circuits_closed=2.000000\n"
670 "Guard in=default rsa_id=7B700C0C207EBD0002E00F499BE265519AC3C25A "
671 "nickname=dc6jgk11 sampled_on=2016-11-28T11:50:13 "
672 "sampled_by=0.3.0.0-alpha-dev "
673 "listed=1 confirmed_on=2016-11-24T08:45:30 confirmed_idx=1 "
674 "pb_circ_attempts=5.000000 pb_circ_successes=5.000000 "
675 "pb_successful_circuits_closed=5.000000\n"
676 "Guard in=default rsa_id=E9025AD60D86875D5F11548D536CC6AF60F0EF5E "
677 "nickname=maibrunn sampled_on=2016-11-25T22:36:38 "
678 "sampled_by=0.3.0.0-alpha-dev listed=1\n"
679 "Guard in=default rsa_id=DCD30B90BA3A792DA75DC54A327EF353FB84C38E "
680 "nickname=Unnamed sampled_on=2016-11-25T14:34:00 "
681 "sampled_by=0.3.0.0-alpha-dev listed=1\n"
682 "Guard in=wobblesome rsa_id=7B700C0C207EBD0002E00F499BE265519AC3C25A "
683 "nickname=dc6jgk11 sampled_on=2016-11-28T11:50:13 "
684 "sampled_by=0.3.0.0-alpha-dev "
685 "listed=1\n"
686 "Guard in=bridges rsa_id=8FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2E "
687 "bridge_addr=24.1.1.1:443 sampled_on=2016-11-25T06:44:14 "
688 "sampled_by=0.3.0.0-alpha-dev listed=1 "
689 "confirmed_on=2016-11-29T10:36:06 confirmed_idx=0 "
690 "pb_circ_attempts=8.000000 pb_circ_successes=8.000000 "
691 "pb_successful_circuits_closed=13.000000\n"
692 "Guard in=bridges rsa_id=5800000000000000000000000000000000000000 "
693 "bridge_addr=37.218.246.143:28366 "
694 "sampled_on=2016-11-18T15:07:34 sampled_by=0.3.0.0-alpha-dev listed=1\n");
696 done:
697 config_free_lines(lines);
698 tor_free(state);
699 tor_free(msg);
700 UNMOCK(get_or_state);
701 UNMOCK(entry_guard_is_listed);
702 SMARTLIST_FOREACH(text, char *, cp, tor_free(cp));
703 smartlist_free(text);
704 tor_free(joined);
707 static void
708 test_entry_guard_parse_from_state_broken(void *arg)
710 (void)arg;
711 /* Here's a variation on the previous state. Every line but the first is
712 * busted somehow. */
713 const char STATE[] =
714 /* Okay. */
715 "Guard in=default rsa_id=214F44BD5B638E8C817D47FF7C97397790BF0345 "
716 "nickname=TotallyNinja sampled_on=2016-11-12T19:32:49 "
717 "sampled_by=0.3.0.0-alpha-dev "
718 "listed=1\n"
719 /* No selection listed. */
720 "Guard rsa_id=052900AB0EA3ED54BAB84AE8A99E74E8693CE2B2 "
721 "nickname=5OfNovember sampled_on=2016-11-20T04:32:05 "
722 "sampled_by=0.3.0.0-alpha-dev "
723 "listed=1 confirmed_on=2016-11-22T08:13:28 confirmed_idx=0 "
724 "pb_circ_attempts=4.000000 pb_circ_successes=2.000000 "
725 "pb_successful_circuits_closed=2.000000\n"
726 /* Selection is "legacy"!! */
727 "Guard in=legacy rsa_id=7B700C0C207EBD0002E00F499BE265519AC3C25A "
728 "nickname=dc6jgk11 sampled_on=2016-11-28T11:50:13 "
729 "sampled_by=0.3.0.0-alpha-dev "
730 "listed=1 confirmed_on=2016-11-24T08:45:30 confirmed_idx=4 "
731 "pb_circ_attempts=5.000000 pb_circ_successes=5.000000 "
732 "pb_successful_circuits_closed=5.000000\n";
734 config_line_t *lines = NULL;
735 or_state_t *state = tor_malloc_zero(sizeof(or_state_t));
736 int r = config_get_lines(STATE, &lines, 0);
737 char *msg = NULL;
739 dummy_state = state;
740 MOCK(get_or_state,
741 get_or_state_replacement);
743 tt_int_op(r, OP_EQ, 0);
744 tt_assert(lines);
746 state->Guard = lines;
748 /* First, no-set case. we should get an error. */
749 r = entry_guards_parse_state(state, 0, &msg);
750 tt_int_op(r, OP_LT, 0);
751 tt_ptr_op(msg, OP_NE, NULL);
752 /* And we shouldn't have made anything. */
753 guard_selection_t *gs_df =
754 get_guard_selection_by_name("default", GS_TYPE_NORMAL, 0);
755 tt_ptr_op(gs_df, OP_EQ, NULL);
756 tor_free(msg);
758 /* Now see about the set case (which shouldn't happen IRL) */
759 r = entry_guards_parse_state(state, 1, &msg);
760 tt_int_op(r, OP_LT, 0);
761 tt_ptr_op(msg, OP_NE, NULL);
762 gs_df = get_guard_selection_by_name("default", GS_TYPE_NORMAL, 0);
763 tt_ptr_op(gs_df, OP_NE, NULL);
764 tt_int_op(smartlist_len(gs_df->sampled_entry_guards), OP_EQ, 1);
766 done:
767 config_free_lines(lines);
768 tor_free(state);
769 tor_free(msg);
770 UNMOCK(get_or_state);
773 static void
774 test_entry_guard_get_guard_selection_by_name(void *arg)
776 (void)arg;
777 guard_selection_t *gs1, *gs2, *gs3;
779 gs1 = get_guard_selection_by_name("unlikely", GS_TYPE_NORMAL, 0);
780 tt_ptr_op(gs1, OP_EQ, NULL);
781 gs1 = get_guard_selection_by_name("unlikely", GS_TYPE_NORMAL, 1);
782 tt_ptr_op(gs1, OP_NE, NULL);
783 gs2 = get_guard_selection_by_name("unlikely", GS_TYPE_NORMAL, 1);
784 tt_assert(gs2 == gs1);
785 gs2 = get_guard_selection_by_name("unlikely", GS_TYPE_NORMAL, 0);
786 tt_assert(gs2 == gs1);
788 gs2 = get_guard_selection_by_name("implausible", GS_TYPE_NORMAL, 0);
789 tt_ptr_op(gs2, OP_EQ, NULL);
790 gs2 = get_guard_selection_by_name("implausible", GS_TYPE_NORMAL, 1);
791 tt_ptr_op(gs2, OP_NE, NULL);
792 tt_assert(gs2 != gs1);
793 gs3 = get_guard_selection_by_name("implausible", GS_TYPE_NORMAL, 0);
794 tt_assert(gs3 == gs2);
796 gs3 = get_guard_selection_by_name("default", GS_TYPE_NORMAL, 0);
797 tt_ptr_op(gs3, OP_EQ, NULL);
798 gs3 = get_guard_selection_by_name("default", GS_TYPE_NORMAL, 1);
799 tt_ptr_op(gs3, OP_NE, NULL);
800 tt_assert(gs3 != gs2);
801 tt_assert(gs3 != gs1);
802 tt_assert(gs3 == get_guard_selection_info());
804 done:
805 entry_guards_free_all();
808 static void
809 test_entry_guard_choose_selection_initial(void *arg)
811 /* Tests for picking our initial guard selection (based on having had
812 * no previous selection */
813 (void)arg;
814 guard_selection_type_t type = GS_TYPE_INFER;
815 const char *name = choose_guard_selection(get_options(),
816 dummy_consensus, NULL, &type);
817 tt_str_op(name, OP_EQ, "default");
818 tt_int_op(type, OP_EQ, GS_TYPE_NORMAL);
820 /* If we're using bridges, we get the bridge selection. */
821 get_options_mutable()->UseBridges = 1;
822 name = choose_guard_selection(get_options(),
823 dummy_consensus, NULL, &type);
824 tt_str_op(name, OP_EQ, "bridges");
825 tt_int_op(type, OP_EQ, GS_TYPE_BRIDGE);
826 get_options_mutable()->UseBridges = 0;
828 /* If we discard >99% of our guards, though, we should be in the restricted
829 * set. */
830 tt_assert(get_options_mutable()->EntryNodes == NULL);
831 get_options_mutable()->EntryNodes = routerset_new();
832 routerset_parse(get_options_mutable()->EntryNodes, "1.0.0.0/8", "foo");
833 name = choose_guard_selection(get_options(),
834 dummy_consensus, NULL, &type);
835 tt_str_op(name, OP_EQ, "restricted");
836 tt_int_op(type, OP_EQ, GS_TYPE_RESTRICTED);
838 done:
842 static void
843 test_entry_guard_add_single_guard(void *arg)
845 (void)arg;
846 guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
848 /* 1: Add a single guard to the sample. */
849 node_t *n1 = smartlist_get(big_fake_net_nodes, 0);
850 time_t now = approx_time();
851 tt_assert(n1->is_possible_guard == 1);
852 entry_guard_t *g1 = entry_guard_add_to_sample(gs, n1);
853 tt_assert(g1);
855 /* Make sure its fields look right. */
856 tt_mem_op(n1->identity, OP_EQ, g1->identity, DIGEST_LEN);
857 tt_i64_op(g1->sampled_on_date, OP_GE, now - 12*86400);
858 tt_i64_op(g1->sampled_on_date, OP_LE, now);
859 tt_str_op(g1->sampled_by_version, OP_EQ, VERSION);
860 tt_uint_op(g1->currently_listed, OP_EQ, 1);
861 tt_i64_op(g1->confirmed_on_date, OP_EQ, 0);
862 tt_int_op(g1->confirmed_idx, OP_EQ, -1);
863 tt_int_op(g1->last_tried_to_connect, OP_EQ, 0);
864 tt_uint_op(g1->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
865 tt_i64_op(g1->failing_since, OP_EQ, 0);
866 tt_uint_op(g1->is_filtered_guard, OP_EQ, 1);
867 tt_uint_op(g1->is_usable_filtered_guard, OP_EQ, 1);
868 tt_uint_op(g1->is_primary, OP_EQ, 0);
869 tt_ptr_op(g1->extra_state_fields, OP_EQ, NULL);
871 /* Make sure it got added. */
872 tt_int_op(1, OP_EQ, smartlist_len(gs->sampled_entry_guards));
873 tt_ptr_op(g1, OP_EQ, smartlist_get(gs->sampled_entry_guards, 0));
874 tt_ptr_op(g1, OP_EQ, get_sampled_guard_with_id(gs, (uint8_t*)n1->identity));
875 const uint8_t bad_id[20] = {0};
876 tt_ptr_op(NULL, OP_EQ, get_sampled_guard_with_id(gs, bad_id));
878 done:
879 guard_selection_free(gs);
882 static void
883 test_entry_guard_node_filter(void *arg)
885 (void)arg;
886 guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
887 bridge_line_t *bl = NULL;
889 /* Initialize a bunch of node objects that are all guards. */
890 #define NUM 7
891 node_t *n[NUM];
892 entry_guard_t *g[NUM];
893 int i;
894 for (i=0; i < NUM; ++i) {
895 n[i] = smartlist_get(big_fake_net_nodes, i*2); // even ones are guards.
896 g[i] = entry_guard_add_to_sample(gs, n[i]);
898 // everything starts out filtered-in
899 tt_uint_op(g[i]->is_filtered_guard, OP_EQ, 1);
900 tt_uint_op(g[i]->is_usable_filtered_guard, OP_EQ, 1);
902 tt_int_op(num_reachable_filtered_guards(gs, NULL), OP_EQ, NUM);
904 /* Make sure refiltering doesn't hurt */
905 entry_guards_update_filtered_sets(gs);
906 for (i = 0; i < NUM; ++i) {
907 tt_uint_op(g[i]->is_filtered_guard, OP_EQ, 1);
908 tt_uint_op(g[i]->is_usable_filtered_guard, OP_EQ, 1);
910 tt_int_op(num_reachable_filtered_guards(gs, NULL), OP_EQ, NUM);
912 /* Now start doing things to make the guards get filtered out, 1 by 1. */
914 /* 0: Not listed. */
915 g[0]->currently_listed = 0;
917 /* 1: path bias says this guard is maybe eeeevil. */
918 g[1]->pb.path_bias_disabled = 1;
920 /* 2: Unreachable address. */
921 n[2]->rs->addr = 0;
923 /* 3: ExcludeNodes */
924 n[3]->rs->addr = 0x90902020;
925 routerset_free(get_options_mutable()->ExcludeNodes);
926 get_options_mutable()->ExcludeNodes = routerset_new();
927 routerset_parse(get_options_mutable()->ExcludeNodes, "144.144.0.0/16", "");
929 /* 4: Bridge. */
930 get_options_mutable()->UseBridges = 1;
931 sweep_bridge_list();
932 bl = tor_malloc_zero(sizeof(bridge_line_t));
933 tor_addr_from_ipv4h(&bl->addr, n[4]->rs->addr);
934 bl->port = n[4]->rs->or_port;
935 memcpy(bl->digest, n[4]->identity, 20);
936 bridge_add_from_config(bl);
937 bl = NULL; // prevent free.
938 get_options_mutable()->UseBridges = 0;
940 /* 5: Unreachable. This stays in the filter, but isn't in usable-filtered */
941 g[5]->last_tried_to_connect = approx_time(); // prevent retry.
942 g[5]->is_reachable = GUARD_REACHABLE_NO;
944 /* 6: no change. */
946 /* Now refilter and inspect. */
947 entry_guards_update_filtered_sets(gs);
948 for (i = 0; i < NUM; ++i) {
949 tt_assert(g[i]->is_filtered_guard == (i == 5 || i == 6));
950 tt_assert(g[i]->is_usable_filtered_guard == (i == 6));
952 tt_int_op(num_reachable_filtered_guards(gs, NULL), OP_EQ, 1);
954 /* Now make sure we have no live consensus, and no nodes. Nothing should
955 * pass the filter any more. */
956 tor_free(dummy_consensus);
957 dummy_consensus = NULL;
958 SMARTLIST_FOREACH(big_fake_net_nodes, node_t *, node, {
959 memset(node->identity, 0xff, 20);
961 entry_guards_update_filtered_sets(gs);
962 for (i = 0; i < NUM; ++i) {
963 tt_uint_op(g[i]->is_filtered_guard, OP_EQ, 0);
964 tt_uint_op(g[i]->is_usable_filtered_guard, OP_EQ, 0);
966 tt_int_op(num_reachable_filtered_guards(gs, NULL), OP_EQ, 0);
968 done:
969 guard_selection_free(gs);
970 tor_free(bl);
971 #undef NUM
974 static void
975 test_entry_guard_expand_sample(void *arg)
977 (void)arg;
978 guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
979 digestmap_t *node_by_id = digestmap_new();
981 entry_guard_t *guard = entry_guards_expand_sample(gs);
982 tt_assert(guard); // the last guard returned.
984 // Every sampled guard here should be filtered and reachable for now.
985 tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ,
986 num_reachable_filtered_guards(gs, NULL));
988 /* Make sure we got the right number. */
989 tt_int_op(DFLT_MIN_FILTERED_SAMPLE_SIZE, OP_EQ,
990 num_reachable_filtered_guards(gs, NULL));
992 // Make sure everything we got was from our fake node list, and everything
993 // was unique.
994 SMARTLIST_FOREACH_BEGIN(gs->sampled_entry_guards, entry_guard_t *, g) {
995 const node_t *n = bfn_mock_node_get_by_id(g->identity);
996 tt_assert(n);
997 tt_ptr_op(NULL, OP_EQ, digestmap_get(node_by_id, g->identity));
998 digestmap_set(node_by_id, g->identity, (void*) n);
999 int idx = smartlist_pos(big_fake_net_nodes, n);
1000 // The even ones are the guards; make sure we got guards.
1001 tt_int_op(idx & 1, OP_EQ, 0);
1002 } SMARTLIST_FOREACH_END(g);
1004 // Nothing became unusable/unfiltered, so a subsequent expand should
1005 // make no changes.
1006 guard = entry_guards_expand_sample(gs);
1007 tt_ptr_op(guard, OP_EQ, NULL); // no guard was added.
1008 tt_int_op(DFLT_MIN_FILTERED_SAMPLE_SIZE, OP_EQ,
1009 num_reachable_filtered_guards(gs, NULL));
1011 // Make a few guards unreachable.
1012 guard = smartlist_get(gs->sampled_entry_guards, 0);
1013 guard->is_usable_filtered_guard = 0;
1014 guard = smartlist_get(gs->sampled_entry_guards, 1);
1015 guard->is_usable_filtered_guard = 0;
1016 guard = smartlist_get(gs->sampled_entry_guards, 2);
1017 guard->is_usable_filtered_guard = 0;
1018 tt_int_op(DFLT_MIN_FILTERED_SAMPLE_SIZE - 3, OP_EQ,
1019 num_reachable_filtered_guards(gs, NULL));
1021 // This time, expanding the sample will add some more guards.
1022 guard = entry_guards_expand_sample(gs);
1023 tt_assert(guard); // no guard was added.
1024 tt_int_op(DFLT_MIN_FILTERED_SAMPLE_SIZE, OP_EQ,
1025 num_reachable_filtered_guards(gs, NULL));
1026 tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ,
1027 num_reachable_filtered_guards(gs, NULL)+3);
1029 // Still idempotent.
1030 guard = entry_guards_expand_sample(gs);
1031 tt_ptr_op(guard, OP_EQ, NULL); // no guard was added.
1032 tt_int_op(DFLT_MIN_FILTERED_SAMPLE_SIZE, OP_EQ,
1033 num_reachable_filtered_guards(gs, NULL));
1035 // Now, do a nasty trick: tell the filter to exclude 31/32 of the guards.
1036 // This will cause the sample size to get reeeeally huge, while the
1037 // filtered sample size grows only slowly.
1038 routerset_free(get_options_mutable()->ExcludeNodes);
1039 get_options_mutable()->ExcludeNodes = routerset_new();
1040 routerset_parse(get_options_mutable()->ExcludeNodes, "144.144.0.0/16", "");
1041 SMARTLIST_FOREACH(big_fake_net_nodes, node_t *, n, {
1042 if (n_sl_idx % 64 != 0) {
1043 n->rs->addr = 0x90903030;
1046 entry_guards_update_filtered_sets(gs);
1048 // Surely (p ~ 1-2**-60), one of our guards has been excluded.
1049 tt_int_op(num_reachable_filtered_guards(gs, NULL), OP_LT,
1050 DFLT_MIN_FILTERED_SAMPLE_SIZE);
1052 // Try to regenerate the guards.
1053 guard = entry_guards_expand_sample(gs);
1054 tt_assert(guard); // no guard was added.
1056 /* this time, it's possible that we didn't add enough sampled guards. */
1057 tt_int_op(num_reachable_filtered_guards(gs, NULL), OP_LE,
1058 DFLT_MIN_FILTERED_SAMPLE_SIZE);
1059 /* but we definitely didn't exceed the sample maximum. */
1060 const int n_guards = 271 / 2;
1061 tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_LE,
1062 (int)(n_guards * .3));
1064 done:
1065 guard_selection_free(gs);
1066 digestmap_free(node_by_id, NULL);
1069 static void
1070 test_entry_guard_expand_sample_small_net(void *arg)
1072 (void)arg;
1073 guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
1075 /* Fun corner case: not enough guards to make up our whole sample size. */
1076 SMARTLIST_FOREACH(big_fake_net_nodes, node_t *, n, {
1077 if (n_sl_idx >= 15) {
1078 tor_free(n->rs);
1079 tor_free(n->md);
1080 tor_free(n);
1081 SMARTLIST_DEL_CURRENT(big_fake_net_nodes, n);
1082 } else {
1083 n->rs->addr = 0; // make the filter reject this.
1087 entry_guard_t *guard = entry_guards_expand_sample(gs);
1088 tt_assert(guard); // the last guard returned -- some guard was added.
1089 // half the nodes are guards, so we have 8 guards left. The set
1090 // is small, so we sampled everything.
1091 tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, 8);
1092 tt_int_op(num_reachable_filtered_guards(gs, NULL), OP_EQ, 0);
1093 done:
1094 guard_selection_free(gs);
1097 static void
1098 test_entry_guard_update_from_consensus_status(void *arg)
1100 /* Here we're going to have some nodes become un-guardy, and say we got a
1101 * new consensus. This should cause those nodes to get detected as
1102 * unreachable. */
1104 (void)arg;
1105 int i;
1106 time_t start = approx_time();
1107 guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
1108 networkstatus_t *ns_tmp = NULL;
1110 /* Don't randomly backdate stuff; it will make correctness harder to check.*/
1111 MOCK(randomize_time, mock_randomize_time_no_randomization);
1113 /* First, sample some guards. */
1114 entry_guards_expand_sample(gs);
1115 int n_sampled_pre = smartlist_len(gs->sampled_entry_guards);
1116 int n_filtered_pre = num_reachable_filtered_guards(gs, NULL);
1117 tt_i64_op(n_sampled_pre, OP_EQ, n_filtered_pre);
1118 tt_i64_op(n_sampled_pre, OP_GT, 10);
1120 /* At this point, it should be a no-op to do this: */
1121 sampled_guards_update_from_consensus(gs);
1123 /* Now let's make some of our guards become unlisted. The easiest way to
1124 * do that would be to take away their guard flag. */
1125 for (i = 0; i < 5; ++i) {
1126 entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
1127 node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
1128 n->is_possible_guard = 0;
1131 update_approx_time(start + 30);
1133 /* try this with no live networkstatus. Nothing should happen! */
1134 ns_tmp = dummy_consensus;
1135 dummy_consensus = NULL;
1136 sampled_guards_update_from_consensus(gs);
1137 tt_i64_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_sampled_pre);
1138 tt_i64_op(num_reachable_filtered_guards(gs, NULL), OP_EQ, n_filtered_pre);
1139 /* put the networkstatus back. */
1140 dummy_consensus = ns_tmp;
1141 ns_tmp = NULL;
1144 /* Now those guards should become unlisted, and drop off the filter, but
1145 * stay in the sample. */
1146 update_approx_time(start + 60);
1147 sampled_guards_update_from_consensus(gs);
1149 tt_i64_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_sampled_pre);
1150 tt_i64_op(num_reachable_filtered_guards(gs, NULL), OP_EQ, n_filtered_pre-5);
1151 for (i = 0; i < 5; ++i) {
1152 entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
1153 tt_assert(! g->currently_listed);
1154 tt_i64_op(g->unlisted_since_date, OP_EQ, start+60);
1156 for (i = 5; i < n_sampled_pre; ++i) {
1157 entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
1158 tt_assert(g->currently_listed);
1159 tt_i64_op(g->unlisted_since_date, OP_EQ, 0);
1162 /* Now re-list one, and remove one completely. */
1164 entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 0);
1165 node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
1166 n->is_possible_guard = 1;
1169 /* try removing the node, to make sure we don't crash on an absent node
1171 entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 5);
1172 node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
1173 smartlist_remove(big_fake_net_nodes, n);
1174 tor_free(n->rs);
1175 tor_free(n->md);
1176 tor_free(n);
1178 update_approx_time(start + 300);
1179 sampled_guards_update_from_consensus(gs);
1181 /* guards 1..5 are now unlisted; 0,6,7.. are listed. */
1182 tt_i64_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_sampled_pre);
1183 for (i = 1; i < 6; ++i) {
1184 entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
1185 tt_assert(! g->currently_listed);
1186 if (i == 5)
1187 tt_i64_op(g->unlisted_since_date, OP_EQ, start+300);
1188 else
1189 tt_i64_op(g->unlisted_since_date, OP_EQ, start+60);
1191 for (i = 0; i < n_sampled_pre; i = (!i) ? 6 : i+1) { /* 0,6,7,8, ... */
1192 entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
1193 tt_assert(g->currently_listed);
1194 tt_i64_op(g->unlisted_since_date, OP_EQ, 0);
1197 done:
1198 tor_free(ns_tmp); /* in case we couldn't put it back */
1199 guard_selection_free(gs);
1200 UNMOCK(randomize_time);
1203 static void
1204 test_entry_guard_update_from_consensus_repair(void *arg)
1206 /* Here we'll make sure that our code to repair the unlisted-since
1207 * times is correct. */
1209 (void)arg;
1210 int i;
1211 time_t start = approx_time();
1212 guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
1214 /* Don't randomly backdate stuff; it will make correctness harder to check.*/
1215 MOCK(randomize_time, mock_randomize_time_no_randomization);
1217 /* First, sample some guards. */
1218 entry_guards_expand_sample(gs);
1219 int n_sampled_pre = smartlist_len(gs->sampled_entry_guards);
1220 int n_filtered_pre = num_reachable_filtered_guards(gs, NULL);
1221 tt_i64_op(n_sampled_pre, OP_EQ, n_filtered_pre);
1222 tt_i64_op(n_sampled_pre, OP_GT, 10);
1224 /* Now corrupt the list a bit. Call some unlisted-since-never, and some
1225 * listed-and-unlisted-since-a-time. */
1226 update_approx_time(start + 300);
1227 for (i = 0; i < 3; ++i) {
1228 /* these will get a date. */
1229 entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
1230 node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
1231 n->is_possible_guard = 0;
1232 g->currently_listed = 0;
1234 for (i = 3; i < 6; ++i) {
1235 /* these will become listed. */
1236 entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
1237 g->unlisted_since_date = start+100;
1239 setup_full_capture_of_logs(LOG_WARN);
1240 sampled_guards_update_from_consensus(gs);
1241 expect_log_msg_containing(
1242 "was listed, but with unlisted_since_date set");
1243 expect_log_msg_containing(
1244 "was unlisted, but with unlisted_since_date unset");
1245 teardown_capture_of_logs();
1247 tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_sampled_pre);
1248 tt_int_op(num_reachable_filtered_guards(gs, NULL), OP_EQ, n_filtered_pre-3);
1249 for (i = 3; i < n_sampled_pre; ++i) {
1250 /* these will become listed. */
1251 entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
1252 if (i < 3) {
1253 tt_assert(! g->currently_listed);
1254 tt_i64_op(g->unlisted_since_date, OP_EQ, start+300);
1255 } else {
1256 tt_assert(g->currently_listed);
1257 tt_i64_op(g->unlisted_since_date, OP_EQ, 0);
1261 done:
1262 teardown_capture_of_logs();
1263 guard_selection_free(gs);
1264 UNMOCK(randomize_time);
1267 static void
1268 test_entry_guard_update_from_consensus_remove(void *arg)
1270 /* Now let's check the logic responsible for removing guards from the
1271 * sample entirely. */
1273 (void)arg;
1274 //int i;
1275 guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
1276 smartlist_t *keep_ids = smartlist_new();
1277 smartlist_t *remove_ids = smartlist_new();
1279 /* Don't randomly backdate stuff; it will make correctness harder to check.*/
1280 MOCK(randomize_time, mock_randomize_time_no_randomization);
1282 /* First, sample some guards. */
1283 entry_guards_expand_sample(gs);
1284 int n_sampled_pre = smartlist_len(gs->sampled_entry_guards);
1285 int n_filtered_pre = num_reachable_filtered_guards(gs, NULL);
1286 tt_i64_op(n_sampled_pre, OP_EQ, n_filtered_pre);
1287 tt_i64_op(n_sampled_pre, OP_GT, 10);
1289 const time_t one_day_ago = approx_time() - 1*24*60*60;
1290 const time_t one_year_ago = approx_time() - 365*24*60*60;
1291 const time_t two_years_ago = approx_time() - 2*365*24*60*60;
1292 /* 0: unlisted for a day. (keep this) */
1294 entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 0);
1295 node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
1296 n->is_possible_guard = 0;
1297 g->currently_listed = 0;
1298 g->unlisted_since_date = one_day_ago;
1299 smartlist_add(keep_ids, tor_memdup(g->identity, 20));
1301 /* 1: unlisted for a year. (remove this) */
1303 entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 1);
1304 node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
1305 n->is_possible_guard = 0;
1306 g->currently_listed = 0;
1307 g->unlisted_since_date = one_year_ago;
1308 smartlist_add(remove_ids, tor_memdup(g->identity, 20));
1310 /* 2: added a day ago, never confirmed. (keep this) */
1312 entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 2);
1313 g->sampled_on_date = one_day_ago;
1314 smartlist_add(keep_ids, tor_memdup(g->identity, 20));
1316 /* 3: added a year ago, never confirmed. (remove this) */
1318 entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 3);
1319 g->sampled_on_date = one_year_ago;
1320 smartlist_add(remove_ids, tor_memdup(g->identity, 20));
1322 /* 4: added two year ago, confirmed yesterday, primary. (keep this.) */
1324 entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 4);
1325 g->sampled_on_date = one_year_ago;
1326 g->confirmed_on_date = one_day_ago;
1327 g->confirmed_idx = 0;
1328 g->is_primary = 1;
1329 smartlist_add(gs->confirmed_entry_guards, g);
1330 smartlist_add(gs->primary_entry_guards, g);
1331 smartlist_add(keep_ids, tor_memdup(g->identity, 20));
1333 /* 5: added two years ago, confirmed a year ago, primary. (remove this) */
1335 entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 5);
1336 g->sampled_on_date = two_years_ago;
1337 g->confirmed_on_date = one_year_ago;
1338 g->confirmed_idx = 1;
1339 g->is_primary = 1;
1340 smartlist_add(gs->confirmed_entry_guards, g);
1341 smartlist_add(gs->primary_entry_guards, g);
1342 smartlist_add(remove_ids, tor_memdup(g->identity, 20));
1345 sampled_guards_update_from_consensus(gs);
1347 /* Did we remove the right ones? */
1348 SMARTLIST_FOREACH(keep_ids, uint8_t *, id, {
1349 tt_assert(get_sampled_guard_with_id(gs, id) != NULL);
1351 SMARTLIST_FOREACH(remove_ids, uint8_t *, id, {
1352 tt_want(get_sampled_guard_with_id(gs, id) == NULL);
1355 /* Did we remove the right number? */
1356 tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_sampled_pre - 3);
1358 done:
1359 guard_selection_free(gs);
1360 UNMOCK(randomize_time);
1361 SMARTLIST_FOREACH(keep_ids, char *, cp, tor_free(cp));
1362 SMARTLIST_FOREACH(remove_ids, char *, cp, tor_free(cp));
1363 smartlist_free(keep_ids);
1364 smartlist_free(remove_ids);
1367 static void
1368 test_entry_guard_confirming_guards(void *arg)
1370 (void)arg;
1371 /* Now let's check the logic responsible for manipulating the list
1372 * of confirmed guards */
1373 guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
1374 MOCK(randomize_time, mock_randomize_time_no_randomization);
1376 /* Create the sample. */
1377 entry_guards_expand_sample(gs);
1379 /* Confirm a few guards. */
1380 time_t start = approx_time();
1381 entry_guard_t *g1 = smartlist_get(gs->sampled_entry_guards, 0);
1382 entry_guard_t *g2 = smartlist_get(gs->sampled_entry_guards, 1);
1383 entry_guard_t *g3 = smartlist_get(gs->sampled_entry_guards, 8);
1384 make_guard_confirmed(gs, g2);
1385 update_approx_time(start + 10);
1386 make_guard_confirmed(gs, g1);
1387 make_guard_confirmed(gs, g3);
1389 /* Were the correct dates and indices fed in? */
1390 tt_int_op(g1->confirmed_idx, OP_EQ, 1);
1391 tt_int_op(g2->confirmed_idx, OP_EQ, 0);
1392 tt_int_op(g3->confirmed_idx, OP_EQ, 2);
1393 tt_i64_op(g1->confirmed_on_date, OP_EQ, start+10);
1394 tt_i64_op(g2->confirmed_on_date, OP_EQ, start);
1395 tt_i64_op(g3->confirmed_on_date, OP_EQ, start+10);
1396 tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 0), OP_EQ, g2);
1397 tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 1), OP_EQ, g1);
1398 tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 2), OP_EQ, g3);
1400 /* Now make sure we can regenerate the confirmed_entry_guards list. */
1401 smartlist_clear(gs->confirmed_entry_guards);
1402 g2->confirmed_idx = 0;
1403 g1->confirmed_idx = 10;
1404 g3->confirmed_idx = 100;
1405 entry_guards_update_confirmed(gs);
1406 tt_int_op(g1->confirmed_idx, OP_EQ, 1);
1407 tt_int_op(g2->confirmed_idx, OP_EQ, 0);
1408 tt_int_op(g3->confirmed_idx, OP_EQ, 2);
1409 tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 0), OP_EQ, g2);
1410 tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 1), OP_EQ, g1);
1411 tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 2), OP_EQ, g3);
1413 /* Now make sure we can regenerate the confirmed_entry_guards list if
1414 * the indices are messed up. */
1415 g1->confirmed_idx = g2->confirmed_idx = g3->confirmed_idx = 999;
1416 smartlist_clear(gs->confirmed_entry_guards);
1417 entry_guards_update_confirmed(gs);
1418 tt_int_op(g1->confirmed_idx, OP_GE, 0);
1419 tt_int_op(g2->confirmed_idx, OP_GE, 0);
1420 tt_int_op(g3->confirmed_idx, OP_GE, 0);
1421 tt_int_op(g1->confirmed_idx, OP_LE, 2);
1422 tt_int_op(g2->confirmed_idx, OP_LE, 2);
1423 tt_int_op(g3->confirmed_idx, OP_LE, 2);
1424 g1 = smartlist_get(gs->confirmed_entry_guards, 0);
1425 g2 = smartlist_get(gs->confirmed_entry_guards, 1);
1426 g3 = smartlist_get(gs->confirmed_entry_guards, 2);
1427 tt_int_op(g1->confirmed_idx, OP_EQ, 0);
1428 tt_int_op(g2->confirmed_idx, OP_EQ, 1);
1429 tt_int_op(g3->confirmed_idx, OP_EQ, 2);
1430 tt_assert(g1 != g2);
1431 tt_assert(g1 != g3);
1432 tt_assert(g2 != g3);
1434 done:
1435 UNMOCK(randomize_time);
1436 guard_selection_free(gs);
1439 static void
1440 test_entry_guard_sample_reachable_filtered(void *arg)
1442 (void)arg;
1443 guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
1444 entry_guards_expand_sample(gs);
1445 const int N = 10000;
1446 bitarray_t *selected = NULL;
1447 int i, j;
1449 /* We've got a sampled list now; let's make one non-usable-filtered; some
1450 * confirmed, some primary, some pending.
1452 int n_guards = smartlist_len(gs->sampled_entry_guards);
1453 tt_int_op(n_guards, OP_GT, 10);
1454 entry_guard_t *g;
1455 g = smartlist_get(gs->sampled_entry_guards, 0);
1456 g->is_pending = 1;
1457 g = smartlist_get(gs->sampled_entry_guards, 1);
1458 make_guard_confirmed(gs, g);
1459 g = smartlist_get(gs->sampled_entry_guards, 2);
1460 g->is_primary = 1;
1461 g = smartlist_get(gs->sampled_entry_guards, 3);
1462 g->pb.path_bias_disabled = 1;
1464 entry_guards_update_filtered_sets(gs);
1465 gs->primary_guards_up_to_date = 1;
1466 tt_int_op(num_reachable_filtered_guards(gs, NULL), OP_EQ, n_guards - 1);
1467 tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_guards);
1469 // +1 since the one we made disabled will make another one get added.
1470 ++n_guards;
1472 /* Try a bunch of selections. */
1473 const struct {
1474 int flag; int idx;
1475 } tests[] = {
1476 { 0, -1 },
1477 { SAMPLE_EXCLUDE_CONFIRMED, 1 },
1478 { SAMPLE_EXCLUDE_PRIMARY|SAMPLE_NO_UPDATE_PRIMARY, 2 },
1479 { SAMPLE_EXCLUDE_PENDING, 0 },
1480 { -1, -1},
1483 for (j = 0; tests[j].flag >= 0; ++j) {
1484 selected = bitarray_init_zero(n_guards);
1485 const int excluded_flags = tests[j].flag;
1486 const int excluded_idx = tests[j].idx;
1487 for (i = 0; i < N; ++i) {
1488 g = sample_reachable_filtered_entry_guards(gs, NULL, excluded_flags);
1489 tor_assert(g);
1490 int pos = smartlist_pos(gs->sampled_entry_guards, g);
1491 tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_guards);
1492 tt_int_op(pos, OP_GE, 0);
1493 tt_int_op(pos, OP_LT, n_guards);
1494 bitarray_set(selected, pos);
1496 for (i = 0; i < n_guards; ++i) {
1497 const int should_be_set = (i != excluded_idx &&
1498 i != 3); // filtered out.
1499 tt_int_op(!!bitarray_is_set(selected, i), OP_EQ, should_be_set);
1501 bitarray_free(selected);
1502 selected = NULL;
1505 done:
1506 guard_selection_free(gs);
1507 bitarray_free(selected);
1510 static void
1511 test_entry_guard_sample_reachable_filtered_empty(void *arg)
1513 (void)arg;
1514 guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
1515 /* What if we try to sample from a set of 0? */
1516 SMARTLIST_FOREACH(big_fake_net_nodes, node_t *, n,
1517 n->is_possible_guard = 0);
1519 entry_guard_t *g = sample_reachable_filtered_entry_guards(gs, NULL, 0);
1520 tt_ptr_op(g, OP_EQ, NULL);
1522 done:
1523 guard_selection_free(gs);
1526 static void
1527 test_entry_guard_retry_unreachable(void *arg)
1529 (void)arg;
1530 guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
1532 entry_guards_expand_sample(gs);
1533 /* Let's say that we have two guards, and they're down.
1535 time_t start = approx_time();
1536 entry_guard_t *g1 = smartlist_get(gs->sampled_entry_guards, 0);
1537 entry_guard_t *g2 = smartlist_get(gs->sampled_entry_guards, 1);
1538 entry_guard_t *g3 = smartlist_get(gs->sampled_entry_guards, 2);
1539 g1->is_reachable = GUARD_REACHABLE_NO;
1540 g2->is_reachable = GUARD_REACHABLE_NO;
1541 g1->is_primary = 1;
1542 g1->failing_since = g2->failing_since = start;
1543 g1->last_tried_to_connect = g2->last_tried_to_connect = start;
1545 /* Wait 5 minutes. Nothing will get retried. */
1546 update_approx_time(start + 5 * 60);
1547 entry_guard_consider_retry(g1);
1548 entry_guard_consider_retry(g2);
1549 entry_guard_consider_retry(g3); // just to make sure this doesn't crash.
1550 tt_int_op(g1->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
1551 tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
1552 tt_int_op(g3->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
1554 /* After 30 min, the primary one gets retried */
1555 update_approx_time(start + 35 * 60);
1556 entry_guard_consider_retry(g1);
1557 entry_guard_consider_retry(g2);
1558 tt_int_op(g1->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
1559 tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
1561 g1->is_reachable = GUARD_REACHABLE_NO;
1562 g1->last_tried_to_connect = start + 55*60;
1564 /* After 1 hour, we'll retry the nonprimary one. */
1565 update_approx_time(start + 61 * 60);
1566 entry_guard_consider_retry(g1);
1567 entry_guard_consider_retry(g2);
1568 tt_int_op(g1->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
1569 tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
1571 g2->is_reachable = GUARD_REACHABLE_NO;
1572 g2->last_tried_to_connect = start + 61*60;
1574 /* And then the primary one again. */
1575 update_approx_time(start + 66 * 60);
1576 entry_guard_consider_retry(g1);
1577 entry_guard_consider_retry(g2);
1578 tt_int_op(g1->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
1579 tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
1581 done:
1582 guard_selection_free(gs);
1585 static void
1586 test_entry_guard_manage_primary(void *arg)
1588 (void)arg;
1589 guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
1590 smartlist_t *prev_guards = smartlist_new();
1592 /* If no guards are confirmed, we should pick a few reachable guards and
1593 * call them all primary. But not confirmed.*/
1594 entry_guards_update_primary(gs);
1595 int n_primary = smartlist_len(gs->primary_entry_guards);
1596 tt_int_op(n_primary, OP_GE, 1);
1597 SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, g, {
1598 tt_assert(g->is_primary);
1599 tt_assert(g->confirmed_idx == -1);
1602 /* Calling it a second time should leave the guards unchanged. */
1603 smartlist_add_all(prev_guards, gs->primary_entry_guards);
1604 entry_guards_update_primary(gs);
1605 tt_int_op(smartlist_len(gs->primary_entry_guards), OP_EQ, n_primary);
1606 SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, g, {
1607 tt_ptr_op(g, OP_EQ, smartlist_get(prev_guards, g_sl_idx));
1610 /* If we have one confirmed guard, that guards becomes the first primary
1611 * guard, and the other primary guards get kept. */
1613 /* find a non-primary guard... */
1614 entry_guard_t *confirmed = NULL;
1615 SMARTLIST_FOREACH(gs->sampled_entry_guards, entry_guard_t *, g, {
1616 if (! g->is_primary) {
1617 confirmed = g;
1618 break;
1621 tt_assert(confirmed);
1622 /* make it confirmed. */
1623 make_guard_confirmed(gs, confirmed);
1624 /* update the list... */
1625 smartlist_clear(prev_guards);
1626 smartlist_add_all(prev_guards, gs->primary_entry_guards);
1627 entry_guards_update_primary(gs);
1629 /* and see what's primary now! */
1630 tt_int_op(smartlist_len(gs->primary_entry_guards), OP_EQ, n_primary);
1631 tt_ptr_op(smartlist_get(gs->primary_entry_guards, 0), OP_EQ, confirmed);
1632 SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, g, {
1633 tt_assert(g->is_primary);
1634 if (g_sl_idx == 0)
1635 continue;
1636 tt_ptr_op(g, OP_EQ, smartlist_get(prev_guards, g_sl_idx - 1));
1639 entry_guard_t *prev_last_guard = smartlist_get(prev_guards, n_primary-1);
1640 tt_assert(! prev_last_guard->is_primary);
1643 /* Calling it a fourth time should leave the guards unchanged. */
1644 smartlist_clear(prev_guards);
1645 smartlist_add_all(prev_guards, gs->primary_entry_guards);
1646 entry_guards_update_primary(gs);
1647 tt_int_op(smartlist_len(gs->primary_entry_guards), OP_EQ, n_primary);
1648 SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, g, {
1649 tt_ptr_op(g, OP_EQ, smartlist_get(prev_guards, g_sl_idx));
1652 /* Do some dirinfo checks */
1654 /* Check that we have all required dirinfo for the primaries (that's done
1655 * in big_fake_network_setup()) */
1656 char *dir_info_str =
1657 guard_selection_get_err_str_if_dir_info_missing(gs, 0, 0, 0);
1658 tt_assert(!dir_info_str);
1660 /* Now artificially remove the first primary's descriptor and re-check */
1661 entry_guard_t *first_primary;
1662 first_primary = smartlist_get(gs->primary_entry_guards, 0);
1663 /* Change the first primary's identity digest so that the mocked functions
1664 * can't find its descriptor */
1665 memset(first_primary->identity, 9, sizeof(first_primary->identity));
1666 dir_info_str =guard_selection_get_err_str_if_dir_info_missing(gs, 1, 2, 3);
1667 tt_str_op(dir_info_str, OP_EQ,
1668 "We're missing descriptors for 1/2 of our primary entry guards "
1669 "(total microdescriptors: 2/3).");
1670 tor_free(dir_info_str);
1673 done:
1674 guard_selection_free(gs);
1675 smartlist_free(prev_guards);
1678 static void
1679 test_entry_guard_guard_preferred(void *arg)
1681 (void) arg;
1682 entry_guard_t *g1 = tor_malloc_zero(sizeof(entry_guard_t));
1683 entry_guard_t *g2 = tor_malloc_zero(sizeof(entry_guard_t));
1685 g1->confirmed_idx = g2->confirmed_idx = -1;
1686 g1->last_tried_to_connect = approx_time();
1687 g2->last_tried_to_connect = approx_time();
1689 tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g1, g1));
1691 /* Neither is pending; priorities equal. */
1692 tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g2, g1));
1693 tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g1, g2));
1695 /* If one is pending, the pending one has higher priority */
1696 g1->is_pending = 1;
1697 tt_int_op(1, OP_EQ, entry_guard_has_higher_priority(g1, g2));
1698 tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g2, g1));
1700 /* If both are pending, and last_tried_to_connect is equal:
1701 priorities equal */
1702 g2->is_pending = 1;
1703 tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g2, g1));
1704 tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g1, g2));
1706 /* One had a connection that startied earlier: it has higher priority. */
1707 g2->last_tried_to_connect -= 10;
1708 tt_int_op(1, OP_EQ, entry_guard_has_higher_priority(g2, g1));
1709 tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g1, g2));
1711 /* Now, say that g1 is confirmed. It will get higher priority. */
1712 g1->confirmed_idx = 5;
1713 tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g2, g1));
1714 tt_int_op(1, OP_EQ, entry_guard_has_higher_priority(g1, g2));
1716 /* But if g2 was confirmed first, it will get priority */
1717 g2->confirmed_idx = 2;
1718 tt_int_op(1, OP_EQ, entry_guard_has_higher_priority(g2, g1));
1719 tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g1, g2));
1721 done:
1722 tor_free(g1);
1723 tor_free(g2);
1726 static void
1727 test_entry_guard_select_for_circuit_no_confirmed(void *arg)
1729 /* Simpler cases: no gaurds are confirmed yet. */
1730 (void)arg;
1731 guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
1732 entry_guard_restriction_t *rst = NULL;
1734 /* simple starting configuration */
1735 entry_guards_update_primary(gs);
1736 unsigned state = 9999;
1738 entry_guard_t *g = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC,
1739 NULL, &state);
1741 tt_assert(g);
1742 tt_assert(g->is_primary);
1743 tt_int_op(g->confirmed_idx, OP_EQ, -1);
1744 tt_uint_op(g->is_pending, OP_EQ, 0); // primary implies non-pending.
1745 tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
1746 tt_i64_op(g->last_tried_to_connect, OP_EQ, approx_time());
1748 // If we do that again, we should get the same guard.
1749 entry_guard_t *g2 = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC,
1750 NULL, &state);
1751 tt_ptr_op(g2, OP_EQ, g);
1753 // if we mark that guard down, we should get a different primary guard.
1754 // auto-retry it.
1755 g->is_reachable = GUARD_REACHABLE_NO;
1756 g->failing_since = approx_time() - 10;
1757 g->last_tried_to_connect = approx_time() - 10;
1758 state = 9999;
1759 g2 = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL, &state);
1760 tt_ptr_op(g2, OP_NE, g);
1761 tt_assert(g2);
1762 tt_assert(g2->is_primary);
1763 tt_int_op(g2->confirmed_idx, OP_EQ, -1);
1764 tt_uint_op(g2->is_pending, OP_EQ, 0); // primary implies non-pending.
1765 tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
1766 tt_i64_op(g2->last_tried_to_connect, OP_EQ, approx_time());
1768 // If we say that the first primary guard was last tried a long time ago, we
1769 // should get an automatic retry on it.
1770 g->failing_since = approx_time() - 72*60*60;
1771 g->last_tried_to_connect = approx_time() - 72*60*60;
1772 state = 9999;
1773 g2 = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL, &state);
1774 tt_ptr_op(g2, OP_EQ, g);
1775 tt_assert(g2);
1776 tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
1777 tt_i64_op(g2->last_tried_to_connect, OP_EQ, approx_time());
1778 tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
1780 // And if we mark ALL the primary guards down, we should get another guard
1781 // at random.
1782 SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, guard, {
1783 guard->is_reachable = GUARD_REACHABLE_NO;
1784 guard->last_tried_to_connect = approx_time() - 5;
1785 guard->failing_since = approx_time() - 30;
1787 state = 9999;
1788 g2 = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL, &state);
1789 tt_assert(g2);
1790 tt_assert(!g2->is_primary);
1791 tt_int_op(g2->confirmed_idx, OP_EQ, -1);
1792 tt_uint_op(g2->is_pending, OP_EQ, 1);
1793 tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
1794 tt_i64_op(g2->last_tried_to_connect, OP_EQ, approx_time());
1795 tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
1797 // As a bonus, maybe we should be retrying the primary guards. Let's say so.
1798 mark_primary_guards_maybe_reachable(gs);
1799 SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, guard, {
1800 tt_int_op(guard->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
1801 tt_assert(guard->is_usable_filtered_guard == 1);
1802 // no change to these fields.
1803 tt_i64_op(guard->last_tried_to_connect, OP_EQ, approx_time() - 5);
1804 tt_i64_op(guard->failing_since, OP_EQ, approx_time() - 30);
1807 /* Let's try again and we should get the first primary guard again */
1808 g = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL, &state);
1809 tt_ptr_op(g, OP_EQ, smartlist_get(gs->primary_entry_guards, 0));
1810 g2 = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL, &state);
1811 tt_ptr_op(g2, OP_EQ, g);
1813 /* But if we impose a restriction, we don't get the same guard */
1814 rst = guard_create_exit_restriction((uint8_t*)g->identity);
1815 g2 = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, rst, &state);
1816 tt_ptr_op(g2, OP_NE, g);
1818 done:
1819 guard_selection_free(gs);
1820 entry_guard_restriction_free(rst);
1823 static void
1824 test_entry_guard_select_for_circuit_confirmed(void *arg)
1826 /* Case 2: if all the primary guards are down, and there are more confirmed
1827 guards, we use a confirmed guard. */
1828 (void)arg;
1829 int i;
1830 entry_guard_restriction_t *rst = NULL;
1831 guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
1832 const int N_CONFIRMED = 10;
1834 /* slightly more complicated simple starting configuration */
1835 entry_guards_update_primary(gs);
1836 for (i = 0; i < N_CONFIRMED; ++i) {
1837 entry_guard_t *guard = smartlist_get(gs->sampled_entry_guards, i);
1838 make_guard_confirmed(gs, guard);
1840 entry_guards_update_primary(gs); // rebuild the primary list.
1842 unsigned state = 9999;
1844 // As above, this gives us a primary guard.
1845 entry_guard_t *g = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC,
1846 NULL, &state);
1847 tt_assert(g);
1848 tt_assert(g->is_primary);
1849 tt_int_op(g->confirmed_idx, OP_EQ, 0);
1850 tt_uint_op(g->is_pending, OP_EQ, 0); // primary implies non-pending.
1851 tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
1852 tt_i64_op(g->last_tried_to_connect, OP_EQ, approx_time());
1853 tt_ptr_op(g, OP_EQ, smartlist_get(gs->primary_entry_guards, 0));
1855 // But if we mark all the primary guards down...
1856 SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, guard, {
1857 guard->last_tried_to_connect = approx_time();
1858 entry_guards_note_guard_failure(gs, guard);
1861 // ... we should get a confirmed guard.
1862 state = 9999;
1863 g = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL, &state);
1864 tt_assert(g);
1865 tt_assert(! g->is_primary);
1866 tt_int_op(g->confirmed_idx, OP_EQ, smartlist_len(gs->primary_entry_guards));
1867 tt_assert(g->is_pending);
1868 tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
1869 tt_i64_op(g->last_tried_to_connect, OP_EQ, approx_time());
1871 // And if we try again, we should get a different confirmed guard, since
1872 // that one is pending.
1873 state = 9999;
1874 entry_guard_t *g2 = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC,
1875 NULL, &state);
1876 tt_assert(g2);
1877 tt_assert(! g2->is_primary);
1878 tt_ptr_op(g2, OP_NE, g);
1879 tt_int_op(g2->confirmed_idx, OP_EQ,
1880 smartlist_len(gs->primary_entry_guards)+1);
1881 tt_assert(g2->is_pending);
1882 tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
1883 tt_i64_op(g2->last_tried_to_connect, OP_EQ, approx_time());
1885 // If we say that the next confirmed guard in order is excluded, and
1886 // we disable EnforceDistinctSubnets, we get the guard AFTER the
1887 // one we excluded.
1888 get_options_mutable()->EnforceDistinctSubnets = 0;
1889 g = smartlist_get(gs->confirmed_entry_guards,
1890 smartlist_len(gs->primary_entry_guards)+2);
1891 rst = guard_create_exit_restriction((uint8_t*)g->identity);
1892 g2 = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, rst, &state);
1893 tt_ptr_op(g2, OP_NE, NULL);
1894 tt_ptr_op(g2, OP_NE, g);
1895 tt_int_op(g2->confirmed_idx, OP_EQ,
1896 smartlist_len(gs->primary_entry_guards)+3);
1898 // If we make every confirmed guard become pending then we start poking
1899 // other guards.
1900 const int n_remaining_confirmed =
1901 N_CONFIRMED - 3 - smartlist_len(gs->primary_entry_guards);
1902 for (i = 0; i < n_remaining_confirmed; ++i) {
1903 g = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL, &state);
1904 tt_int_op(g->confirmed_idx, OP_GE, 0);
1905 tt_assert(g);
1907 state = 9999;
1908 g = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL, &state);
1909 tt_assert(g);
1910 tt_assert(g->is_pending);
1911 tt_int_op(g->confirmed_idx, OP_EQ, -1);
1913 // If we EnforceDistinctSubnets and apply a restriction, we get
1914 // nothing, since we put all of the nodes in the same /16.
1915 // Regression test for bug 22753/TROVE-2017-006.
1916 get_options_mutable()->EnforceDistinctSubnets = 1;
1917 g = smartlist_get(gs->confirmed_entry_guards, 0);
1918 memcpy(rst->exclude_id, g->identity, DIGEST_LEN);
1919 g2 = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, rst, &state);
1920 tt_ptr_op(g2, OP_EQ, NULL);
1922 done:
1923 guard_selection_free(gs);
1924 entry_guard_restriction_free(rst);
1927 static void
1928 test_entry_guard_select_for_circuit_highlevel_primary(void *arg)
1930 /* Play around with selecting primary guards for circuits and markign
1931 * them up and down */
1932 (void)arg;
1933 guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
1935 time_t start = approx_time();
1937 const node_t *node = NULL;
1938 circuit_guard_state_t *guard = NULL;
1939 entry_guard_t *g;
1940 guard_usable_t u;
1942 * Make sure that the pick-for-circuit API basically works. We'll get
1943 * a primary guard, so it'll be usable on completion.
1945 int r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
1946 &node, &guard);
1948 tt_int_op(r, OP_EQ, 0);
1949 tt_assert(node);
1950 tt_assert(guard);
1951 tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
1952 g = entry_guard_handle_get(guard->guard);
1953 tt_assert(g);
1954 tt_mem_op(g->identity, OP_EQ, node->identity, DIGEST_LEN);
1955 tt_int_op(g->is_primary, OP_EQ, 1);
1956 tt_i64_op(g->last_tried_to_connect, OP_EQ, start);
1957 tt_int_op(g->confirmed_idx, OP_EQ, -1);
1959 /* Call that circuit successful. */
1960 update_approx_time(start+15);
1961 u = entry_guard_succeeded(&guard);
1962 tt_int_op(u, OP_EQ, GUARD_USABLE_NOW); /* We can use it now. */
1963 tt_assert(guard);
1964 tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_COMPLETE);
1965 g = entry_guard_handle_get(guard->guard);
1966 tt_assert(g);
1967 tt_int_op(g->is_reachable, OP_EQ, GUARD_REACHABLE_YES);
1968 tt_int_op(g->confirmed_idx, OP_EQ, 0);
1970 circuit_guard_state_free(guard);
1971 guard = NULL;
1972 node = NULL;
1973 g = NULL;
1975 /* Try again. We'll also get a primary guard this time. (The same one,
1976 in fact.) But this time, we'll say the connection has failed. */
1977 update_approx_time(start+35);
1978 r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
1979 &node, &guard);
1980 tt_int_op(r, OP_EQ, 0);
1981 tt_assert(node);
1982 tt_assert(guard);
1983 tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
1984 tt_i64_op(guard->state_set_at, OP_EQ, start+35);
1985 g = entry_guard_handle_get(guard->guard);
1986 tt_assert(g);
1987 tt_mem_op(g->identity, OP_EQ, node->identity, DIGEST_LEN);
1988 tt_int_op(g->is_primary, OP_EQ, 1);
1989 tt_i64_op(g->last_tried_to_connect, OP_EQ, start+35);
1990 tt_int_op(g->confirmed_idx, OP_EQ, 0); // same one.
1992 /* It's failed! What will happen to our poor guard? */
1993 update_approx_time(start+45);
1994 entry_guard_failed(&guard);
1995 tt_assert(guard);
1996 tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_DEAD);
1997 tt_i64_op(guard->state_set_at, OP_EQ, start+45);
1998 g = entry_guard_handle_get(guard->guard);
1999 tt_assert(g);
2000 tt_int_op(g->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
2001 tt_i64_op(g->failing_since, OP_EQ, start+45);
2002 tt_int_op(g->confirmed_idx, OP_EQ, 0); // still confirmed.
2004 circuit_guard_state_free(guard);
2005 guard = NULL;
2006 node = NULL;
2007 entry_guard_t *g_prev = g;
2008 g = NULL;
2010 /* Now try a third time. Since the other one is down, we'll get a different
2011 * (still primary) guard.
2013 update_approx_time(start+60);
2014 r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
2015 &node, &guard);
2016 tt_int_op(r, OP_EQ, 0);
2017 tt_assert(node);
2018 tt_assert(guard);
2019 tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
2020 g = entry_guard_handle_get(guard->guard);
2021 tt_assert(g);
2022 tt_ptr_op(g, OP_NE, g_prev);
2023 tt_mem_op(g->identity, OP_EQ, node->identity, DIGEST_LEN);
2024 tt_mem_op(g->identity, OP_NE, g_prev->identity, DIGEST_LEN);
2025 tt_int_op(g->is_primary, OP_EQ, 1);
2026 tt_i64_op(g->last_tried_to_connect, OP_EQ, start+60);
2027 tt_int_op(g->confirmed_idx, OP_EQ, -1); // not confirmd now.
2029 /* Call this one up; watch it get confirmed. */
2030 update_approx_time(start+90);
2031 u = entry_guard_succeeded(&guard);
2032 tt_int_op(u, OP_EQ, GUARD_USABLE_NOW);
2033 tt_assert(guard);
2034 tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_COMPLETE);
2035 g = entry_guard_handle_get(guard->guard);
2036 tt_assert(g);
2037 tt_int_op(g->is_reachable, OP_EQ, GUARD_REACHABLE_YES);
2038 tt_int_op(g->confirmed_idx, OP_EQ, 1);
2040 done:
2041 guard_selection_free(gs);
2042 circuit_guard_state_free(guard);
2045 static void
2046 test_entry_guard_select_for_circuit_highlevel_confirm_other(void *arg)
2048 (void) arg;
2049 const int N_PRIMARY = DFLT_N_PRIMARY_GUARDS;
2051 /* At the start, we have no confirmed guards. We'll mark the primary guards
2052 * down, then confirm something else. As soon as we do, it should become
2053 * primary, and we should get it next time. */
2055 time_t start = approx_time();
2056 guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
2057 circuit_guard_state_t *guard = NULL;
2058 int i, r;
2059 const node_t *node = NULL;
2060 guard_usable_t u;
2062 /* Declare that we're on the internet. */
2063 entry_guards_note_internet_connectivity(gs);
2065 /* Primary guards are down! */
2066 for (i = 0; i < N_PRIMARY; ++i) {
2067 r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
2068 &node, &guard);
2069 tt_assert(node);
2070 tt_assert(guard);
2071 tt_int_op(r, OP_EQ, 0);
2072 tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
2073 entry_guard_failed(&guard);
2074 circuit_guard_state_free(guard);
2075 guard = NULL;
2076 node = NULL;
2079 /* Next guard should be non-primary. */
2080 node = NULL;
2081 r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
2082 &node, &guard);
2083 tt_assert(node);
2084 tt_assert(guard);
2085 tt_int_op(r, OP_EQ, 0);
2086 entry_guard_t *g = entry_guard_handle_get(guard->guard);
2087 tt_assert(g);
2088 tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
2089 tt_int_op(g->confirmed_idx, OP_EQ, -1);
2090 tt_int_op(g->is_primary, OP_EQ, 0);
2091 tt_int_op(g->is_pending, OP_EQ, 1);
2092 (void)start;
2094 u = entry_guard_succeeded(&guard);
2095 /* We're on the internet (by fiat), so this guard will get called "confirmed"
2096 * and should immediately become primary.
2098 tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_COMPLETE);
2099 tt_assert(u == GUARD_USABLE_NOW);
2100 tt_int_op(g->confirmed_idx, OP_EQ, 0);
2101 tt_int_op(g->is_primary, OP_EQ, 1);
2102 tt_int_op(g->is_pending, OP_EQ, 0);
2104 done:
2105 guard_selection_free(gs);
2106 circuit_guard_state_free(guard);
2109 static void
2110 test_entry_guard_select_for_circuit_highlevel_primary_retry(void *arg)
2112 (void) arg;
2113 const int N_PRIMARY = DFLT_N_PRIMARY_GUARDS;
2115 /* At the start, we have no confirmed guards. We'll mark the primary guards
2116 * down, then confirm something else. As soon as we do, it should become
2117 * primary, and we should get it next time. */
2119 time_t start = approx_time();
2120 guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
2121 circuit_guard_state_t *guard = NULL, *guard2 = NULL;
2122 int i, r;
2123 const node_t *node = NULL;
2124 entry_guard_t *g;
2125 guard_usable_t u;
2127 /* Declare that we're on the internet. */
2128 entry_guards_note_internet_connectivity(gs);
2130 /* Make primary guards confirmed (so they won't be superseded by a later
2131 * guard), then mark them down. */
2132 for (i = 0; i < N_PRIMARY; ++i) {
2133 r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
2134 &node, &guard);
2135 tt_assert(node);
2136 tt_assert(guard);
2137 tt_int_op(r, OP_EQ, 0);
2138 tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
2139 g = entry_guard_handle_get(guard->guard);
2140 make_guard_confirmed(gs, g);
2141 tt_int_op(g->is_primary, OP_EQ, 1);
2142 entry_guard_failed(&guard);
2143 circuit_guard_state_free(guard);
2144 tt_int_op(g->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
2145 guard = NULL;
2146 node = NULL;
2149 /* Get another guard that we might try. */
2150 r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
2151 &node, &guard);
2152 tt_assert(node);
2153 tt_assert(guard);
2154 tt_int_op(r, OP_EQ, 0);
2155 tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
2156 g = entry_guard_handle_get(guard->guard);
2157 tt_int_op(g->is_primary, OP_EQ, 0);
2159 tt_assert(entry_guards_all_primary_guards_are_down(gs));
2161 /* And an hour has passed ... */
2162 update_approx_time(start + 3600);
2164 /* Say that guard has succeeded! */
2165 u = entry_guard_succeeded(&guard);
2166 tt_int_op(u, OP_EQ, GUARD_MAYBE_USABLE_LATER);
2167 tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD);
2168 g = entry_guard_handle_get(guard->guard);
2170 /* The primary guards should have been marked up! */
2171 SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, pg, {
2172 tt_int_op(pg->is_primary, OP_EQ, 1);
2173 tt_ptr_op(g, OP_NE, pg);
2174 tt_int_op(pg->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
2177 /* Have a circuit to a primary guard succeed. */
2178 r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
2179 &node, &guard2);
2180 tt_int_op(r, OP_EQ, 0);
2181 tt_int_op(guard2->state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
2182 u = entry_guard_succeeded(&guard2);
2183 tt_assert(u == GUARD_USABLE_NOW);
2184 tt_int_op(guard2->state, OP_EQ, GUARD_CIRC_STATE_COMPLETE);
2186 tt_assert(! entry_guards_all_primary_guards_are_down(gs));
2188 done:
2189 guard_selection_free(gs);
2190 circuit_guard_state_free(guard);
2191 circuit_guard_state_free(guard2);
2194 static void
2195 test_entry_guard_select_and_cancel(void *arg)
2197 (void) arg;
2198 const int N_PRIMARY = DFLT_N_PRIMARY_GUARDS;
2199 int i,r;
2200 const node_t *node = NULL;
2201 circuit_guard_state_t *guard;
2202 guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
2203 entry_guard_t *g;
2205 /* Once more, we mark all the primary guards down. */
2206 entry_guards_note_internet_connectivity(gs);
2207 for (i = 0; i < N_PRIMARY; ++i) {
2208 r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
2209 &node, &guard);
2210 tt_int_op(r, OP_EQ, 0);
2211 tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
2212 g = entry_guard_handle_get(guard->guard);
2213 tt_int_op(g->is_primary, OP_EQ, 1);
2214 tt_int_op(g->is_pending, OP_EQ, 0);
2215 make_guard_confirmed(gs, g);
2216 entry_guard_failed(&guard);
2217 circuit_guard_state_free(guard);
2218 guard = NULL;
2219 node = NULL;
2222 tt_assert(entry_guards_all_primary_guards_are_down(gs));
2224 /* Now get another guard we could try... */
2225 r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
2226 &node, &guard);
2227 tt_assert(node);
2228 tt_assert(guard);
2229 tt_int_op(r, OP_EQ, 0);
2230 tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
2231 g = entry_guard_handle_get(guard->guard);
2232 tt_int_op(g->is_primary, OP_EQ, 0);
2233 tt_int_op(g->is_pending, OP_EQ, 1);
2235 /* Whoops! We should never have asked for this guard. Cancel the request! */
2236 entry_guard_cancel(&guard);
2237 tt_ptr_op(guard, OP_EQ, NULL);
2238 tt_int_op(g->is_primary, OP_EQ, 0);
2239 tt_int_op(g->is_pending, OP_EQ, 0);
2241 done:
2242 guard_selection_free(gs);
2243 circuit_guard_state_free(guard);
2246 static void
2247 test_entry_guard_drop_guards(void *arg)
2249 (void) arg;
2250 int r;
2251 const node_t *node = NULL;
2252 circuit_guard_state_t *guard;
2253 guard_selection_t *gs = get_guard_selection_info();
2255 // Pick a guard, to get things set up.
2256 r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
2257 &node, &guard);
2258 tt_int_op(r, OP_EQ, 0);
2259 tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_GE,
2260 DFLT_MIN_FILTERED_SAMPLE_SIZE);
2261 tt_ptr_op(gs, OP_EQ, get_guard_selection_info());
2263 // Drop all the guards! (This is a bad idea....)
2264 remove_all_entry_guards_for_guard_selection(gs);
2265 gs = get_guard_selection_info();
2266 tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, 0);
2267 tt_int_op(smartlist_len(gs->primary_entry_guards), OP_EQ, 0);
2268 tt_int_op(smartlist_len(gs->confirmed_entry_guards), OP_EQ, 0);
2270 done:
2271 circuit_guard_state_free(guard);
2272 guard_selection_free(gs);
2275 /* Unit test setup function: Create a fake network, and set everything up
2276 * for testing the upgrade-a-waiting-circuit code. */
2277 typedef struct {
2278 guard_selection_t *gs;
2279 time_t start;
2280 circuit_guard_state_t *guard1_state;
2281 circuit_guard_state_t *guard2_state;
2282 entry_guard_t *guard1;
2283 entry_guard_t *guard2;
2284 origin_circuit_t *circ1;
2285 origin_circuit_t *circ2;
2286 smartlist_t *all_origin_circuits;
2287 } upgrade_circuits_data_t;
2288 static void *
2289 upgrade_circuits_setup(const struct testcase_t *testcase)
2291 upgrade_circuits_data_t *data = tor_malloc_zero(sizeof(*data));
2292 guard_selection_t *gs = data->gs =
2293 guard_selection_new("default", GS_TYPE_NORMAL);
2294 circuit_guard_state_t *guard;
2295 const node_t *node;
2296 entry_guard_t *g;
2297 int i;
2298 const int N_PRIMARY = DFLT_N_PRIMARY_GUARDS;
2299 const char *argument = testcase->setup_data;
2300 const int make_circ1_succeed = strstr(argument, "c1-done") != NULL;
2301 const int make_circ2_succeed = strstr(argument, "c2-done") != NULL;
2303 big_fake_network_setup(testcase);
2305 /* We're going to set things up in a state where a circuit will be ready to
2306 * be upgraded. Each test can make a single change (or not) that should
2307 * block the upgrade.
2310 /* First, make all the primary guards confirmed, and down. */
2311 data->start = approx_time();
2312 entry_guards_note_internet_connectivity(gs);
2313 for (i = 0; i < N_PRIMARY; ++i) {
2314 entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL, &node, &guard);
2315 g = entry_guard_handle_get(guard->guard);
2316 make_guard_confirmed(gs, g);
2317 entry_guard_failed(&guard);
2318 circuit_guard_state_free(guard);
2321 /* Grab another couple of guards */
2322 data->all_origin_circuits = smartlist_new();
2324 update_approx_time(data->start + 27);
2325 entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
2326 &node, &data->guard1_state);
2327 origin_circuit_t *circ;
2328 data->circ1 = circ = origin_circuit_new();
2329 circ->base_.purpose = CIRCUIT_PURPOSE_C_GENERAL;
2330 circ->guard_state = data->guard1_state;
2331 smartlist_add(data->all_origin_circuits, circ);
2333 update_approx_time(data->start + 30);
2334 entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
2335 &node, &data->guard2_state);
2336 data->circ2 = circ = origin_circuit_new();
2337 circ->base_.purpose = CIRCUIT_PURPOSE_C_GENERAL;
2338 circ->guard_state = data->guard2_state;
2339 smartlist_add(data->all_origin_circuits, circ);
2341 data->guard1 = entry_guard_handle_get(data->guard1_state->guard);
2342 data->guard2 = entry_guard_handle_get(data->guard2_state->guard);
2343 tor_assert(data->guard1 != data->guard2);
2344 tor_assert(data->guard1_state->state ==
2345 GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
2346 tor_assert(data->guard2_state->state ==
2347 GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
2349 guard_usable_t r;
2350 update_approx_time(data->start + 32);
2351 if (make_circ1_succeed) {
2352 r = entry_guard_succeeded(&data->guard1_state);
2353 tor_assert(r == GUARD_MAYBE_USABLE_LATER);
2354 tor_assert(data->guard1_state->state ==
2355 GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD);
2357 update_approx_time(data->start + 33);
2358 if (make_circ2_succeed) {
2359 r = entry_guard_succeeded(&data->guard2_state);
2360 tor_assert(r == GUARD_MAYBE_USABLE_LATER);
2361 tor_assert(data->guard2_state->state ==
2362 GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD);
2365 return data;
2367 static int
2368 upgrade_circuits_cleanup(const struct testcase_t *testcase, void *ptr)
2370 upgrade_circuits_data_t *data = ptr;
2371 // circuit_guard_state_free(data->guard1_state); // held in circ1
2372 // circuit_guard_state_free(data->guard2_state); // held in circ2
2373 guard_selection_free(data->gs);
2374 smartlist_free(data->all_origin_circuits);
2375 circuit_free(TO_CIRCUIT(data->circ1));
2376 circuit_free(TO_CIRCUIT(data->circ2));
2377 tor_free(data);
2378 return big_fake_network_cleanup(testcase, NULL);
2381 static void
2382 test_entry_guard_upgrade_a_circuit(void *arg)
2384 upgrade_circuits_data_t *data = arg;
2386 /* This is the easy case: we have no COMPLETED circuits, all the
2387 * primary guards are down, we have two WAITING circuits: one will
2388 * get upgraded to COMPLETED! (The one that started first.)
2391 smartlist_t *result = smartlist_new();
2392 int r;
2393 r = entry_guards_upgrade_waiting_circuits(data->gs,
2394 data->all_origin_circuits,
2395 result);
2396 tt_int_op(r, OP_EQ, 1);
2397 tt_int_op(smartlist_len(result), OP_EQ, 1);
2398 origin_circuit_t *oc = smartlist_get(result, 0);
2400 /* circ1 was started first, so we'll get told to ugrade it... */
2401 tt_ptr_op(oc, OP_EQ, data->circ1);
2403 /* And the guard state should be complete */
2404 tt_ptr_op(data->guard1_state, OP_NE, NULL);
2405 tt_int_op(data->guard1_state->state, OP_EQ, GUARD_CIRC_STATE_COMPLETE);
2407 done:
2408 smartlist_free(result);
2411 static void
2412 test_entry_guard_upgrade_blocked_by_live_primary_guards(void *arg)
2414 upgrade_circuits_data_t *data = arg;
2416 /* If any primary guards might be up, we can't upgrade any waiting
2417 * circuits.
2419 mark_primary_guards_maybe_reachable(data->gs);
2421 smartlist_t *result = smartlist_new();
2422 int r;
2423 setup_capture_of_logs(LOG_DEBUG);
2424 r = entry_guards_upgrade_waiting_circuits(data->gs,
2425 data->all_origin_circuits,
2426 result);
2427 tt_int_op(r, OP_EQ, 0);
2428 tt_int_op(smartlist_len(result), OP_EQ, 0);
2429 expect_log_msg_containing("not all primary guards were definitely down.");
2431 done:
2432 teardown_capture_of_logs();
2433 smartlist_free(result);
2436 static void
2437 test_entry_guard_upgrade_blocked_by_lack_of_waiting_circuits(void *arg)
2439 upgrade_circuits_data_t *data = arg;
2441 /* If no circuits are waiting, we can't upgrade anything. (The test
2442 * setup in this case was told not to make any of the circuits "waiting".)
2444 smartlist_t *result = smartlist_new();
2445 int r;
2446 setup_capture_of_logs(LOG_DEBUG);
2447 r = entry_guards_upgrade_waiting_circuits(data->gs,
2448 data->all_origin_circuits,
2449 result);
2450 tt_int_op(r, OP_EQ, 0);
2451 tt_int_op(smartlist_len(result), OP_EQ, 0);
2452 expect_log_msg_containing("Considered upgrading guard-stalled circuits, "
2453 "but didn't find any.");
2455 done:
2456 teardown_capture_of_logs();
2457 smartlist_free(result);
2460 static void
2461 test_entry_guard_upgrade_blocked_by_better_circ_complete(void *arg)
2463 upgrade_circuits_data_t *data = arg;
2465 /* We'll run through the logic of upgrade_a_circuit below...
2466 * and then try again to make sure that circ2 isn't also upgraded.
2469 smartlist_t *result = smartlist_new();
2470 int r;
2471 r = entry_guards_upgrade_waiting_circuits(data->gs,
2472 data->all_origin_circuits,
2473 result);
2474 tt_int_op(r, OP_EQ, 1);
2475 tt_int_op(smartlist_len(result), OP_EQ, 1);
2476 origin_circuit_t *oc = smartlist_get(result, 0);
2477 tt_ptr_op(oc, OP_EQ, data->circ1);
2478 tt_ptr_op(data->guard1_state, OP_NE, NULL);
2479 tt_int_op(data->guard1_state->state, OP_EQ, GUARD_CIRC_STATE_COMPLETE);
2481 /* Now, try again. Make sure that circ2 isn't upgraded. */
2482 smartlist_clear(result);
2483 setup_capture_of_logs(LOG_DEBUG);
2484 r = entry_guards_upgrade_waiting_circuits(data->gs,
2485 data->all_origin_circuits,
2486 result);
2487 tt_int_op(r, OP_EQ, 0);
2488 tt_int_op(smartlist_len(result), OP_EQ, 0);
2489 expect_log_msg_containing("At least one complete circuit had higher "
2490 "priority, so not upgrading.");
2492 done:
2493 teardown_capture_of_logs();
2494 smartlist_free(result);
2497 static void
2498 test_entry_guard_upgrade_not_blocked_by_restricted_circ_complete(void *arg)
2500 upgrade_circuits_data_t *data = arg;
2502 /* Once more, let circ1 become complete. But this time, we'll claim
2503 * that circ2 was restricted to not use the same guard as circ1. */
2504 data->guard2_state->restrictions =
2505 guard_create_exit_restriction((uint8_t*)data->guard1->identity);
2507 smartlist_t *result = smartlist_new();
2508 int r;
2509 r = entry_guards_upgrade_waiting_circuits(data->gs,
2510 data->all_origin_circuits,
2511 result);
2512 tt_int_op(r, OP_EQ, 1);
2513 tt_int_op(smartlist_len(result), OP_EQ, 1);
2514 origin_circuit_t *oc = smartlist_get(result, 0);
2515 tt_ptr_op(oc, OP_EQ, data->circ1);
2516 tt_ptr_op(data->guard1_state, OP_NE, NULL);
2517 tt_int_op(data->guard1_state->state, OP_EQ, GUARD_CIRC_STATE_COMPLETE);
2519 /* Now, we try again. Since circ2 has a restriction that circ1 doesn't obey,
2520 * circ2 _is_ eligible for upgrade. */
2521 smartlist_clear(result);
2522 r = entry_guards_upgrade_waiting_circuits(data->gs,
2523 data->all_origin_circuits,
2524 result);
2525 tt_int_op(r, OP_EQ, 1);
2526 tt_int_op(smartlist_len(result), OP_EQ, 1);
2527 origin_circuit_t *oc2 = smartlist_get(result, 0);
2528 tt_ptr_op(oc2, OP_EQ, data->circ2);
2530 done:
2531 smartlist_free(result);
2534 static void
2535 test_entry_guard_upgrade_not_blocked_by_worse_circ_complete(void *arg)
2537 upgrade_circuits_data_t *data = arg;
2538 smartlist_t *result = smartlist_new();
2539 /* here we manually make circ2 COMPLETE, and make sure that circ1
2540 * gets made complete anyway, since guard1 has higher priority
2542 update_approx_time(data->start + 300);
2543 data->guard2_state->state = GUARD_CIRC_STATE_COMPLETE;
2544 data->guard2_state->state_set_at = approx_time();
2545 update_approx_time(data->start + 301);
2547 /* Now, try again. Make sure that circ1 is approved. */
2548 int r;
2549 r = entry_guards_upgrade_waiting_circuits(data->gs,
2550 data->all_origin_circuits,
2551 result);
2552 tt_int_op(r, OP_EQ, 1);
2553 tt_int_op(smartlist_len(result), OP_EQ, 1);
2554 origin_circuit_t *oc = smartlist_get(result, 0);
2555 tt_ptr_op(oc, OP_EQ, data->circ1);
2557 done:
2558 smartlist_free(result);
2561 static void
2562 test_entry_guard_upgrade_blocked_by_better_circ_pending(void *arg)
2564 upgrade_circuits_data_t *data = arg;
2566 /* circ2 is done, but circ1 is still pending. Since circ1 is better,
2567 * we won't upgrade circ2. */
2569 /* XXXX Prop271 -- this is a kludge. I'm making sure circ1 _is_ better,
2570 * by messing with the guards' confirmed_idx */
2571 make_guard_confirmed(data->gs, data->guard1);
2573 int tmp;
2574 tmp = data->guard1->confirmed_idx;
2575 data->guard1->confirmed_idx = data->guard2->confirmed_idx;
2576 data->guard2->confirmed_idx = tmp;
2579 smartlist_t *result = smartlist_new();
2580 setup_capture_of_logs(LOG_DEBUG);
2581 int r;
2582 r = entry_guards_upgrade_waiting_circuits(data->gs,
2583 data->all_origin_circuits,
2584 result);
2585 tt_int_op(r, OP_EQ, 0);
2586 tt_int_op(smartlist_len(result), OP_EQ, 0);
2587 expect_log_msg_containing("but 1 pending circuit(s) had higher guard "
2588 "priority, so not upgrading.");
2590 done:
2591 teardown_capture_of_logs();
2592 smartlist_free(result);
2595 static void
2596 test_entry_guard_upgrade_not_blocked_by_restricted_circ_pending(void *arg)
2598 upgrade_circuits_data_t *data = arg;
2599 /* circ2 is done, but circ1 is still pending. But when there is a
2600 restriction on circ2 that circ1 can't satisfy, circ1 can't block
2601 circ2. */
2603 /* XXXX Prop271 -- this is a kludge. I'm making sure circ1 _is_ better,
2604 * by messing with the guards' confirmed_idx */
2605 make_guard_confirmed(data->gs, data->guard1);
2607 int tmp;
2608 tmp = data->guard1->confirmed_idx;
2609 data->guard1->confirmed_idx = data->guard2->confirmed_idx;
2610 data->guard2->confirmed_idx = tmp;
2613 data->guard2_state->restrictions =
2614 guard_create_exit_restriction((uint8_t*)data->guard1->identity);
2616 smartlist_t *result = smartlist_new();
2617 int r;
2618 r = entry_guards_upgrade_waiting_circuits(data->gs,
2619 data->all_origin_circuits,
2620 result);
2621 tt_int_op(r, OP_EQ, 1);
2622 tt_int_op(smartlist_len(result), OP_EQ, 1);
2623 origin_circuit_t *oc = smartlist_get(result, 0);
2624 tt_ptr_op(oc, OP_EQ, data->circ2);
2626 done:
2627 smartlist_free(result);
2630 static void
2631 test_entry_guard_upgrade_not_blocked_by_worse_circ_pending(void *arg)
2633 upgrade_circuits_data_t *data = arg;
2635 /* circ1 is done, but circ2 is still pending. Since circ1 is better,
2636 * we will upgrade it. */
2637 smartlist_t *result = smartlist_new();
2638 int r;
2639 r = entry_guards_upgrade_waiting_circuits(data->gs,
2640 data->all_origin_circuits,
2641 result);
2642 tt_int_op(r, OP_EQ, 1);
2643 tt_int_op(smartlist_len(result), OP_EQ, 1);
2644 origin_circuit_t *oc = smartlist_get(result, 0);
2645 tt_ptr_op(oc, OP_EQ, data->circ1);
2647 done:
2648 smartlist_free(result);
2651 static void
2652 test_enty_guard_should_expire_waiting(void *arg)
2654 (void)arg;
2655 circuit_guard_state_t *fake_state = tor_malloc_zero(sizeof(*fake_state));
2656 /* We'll leave "guard" unset -- it won't matter here. */
2658 /* No state? Can't expire. */
2659 tt_assert(! entry_guard_state_should_expire(NULL));
2661 /* Let's try one that expires. */
2662 fake_state->state = GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD;
2663 fake_state->state_set_at =
2664 approx_time() - DFLT_NONPRIMARY_GUARD_IDLE_TIMEOUT - 1;
2666 tt_assert(entry_guard_state_should_expire(fake_state));
2668 /* But it wouldn't expire if we changed the state. */
2669 fake_state->state = GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD;
2670 tt_assert(! entry_guard_state_should_expire(fake_state));
2672 /* And it wouldn't have expired a few seconds ago. */
2673 fake_state->state = GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD;
2674 fake_state->state_set_at =
2675 approx_time() - DFLT_NONPRIMARY_GUARD_IDLE_TIMEOUT + 5;
2676 tt_assert(! entry_guard_state_should_expire(fake_state));
2678 done:
2679 tor_free(fake_state);
2682 static void
2683 mock_directory_initiate_request(directory_request_t *req)
2685 if (req->guard_state) {
2686 circuit_guard_state_free(req->guard_state);
2690 static networkstatus_t *mock_ns_val = NULL;
2691 static networkstatus_t *
2692 mock_ns_get_by_flavor(consensus_flavor_t f)
2694 (void)f;
2695 return mock_ns_val;
2698 /** Test that when we fetch microdescriptors we skip guards that have
2699 * previously failed to serve us needed microdescriptors. */
2700 static void
2701 test_entry_guard_outdated_dirserver_exclusion(void *arg)
2703 int retval;
2704 response_handler_args_t *args = NULL;
2705 dir_connection_t *conn = NULL;
2706 (void) arg;
2708 /* Test prep: Make a new guard selection */
2709 guard_selection_t *gs = get_guard_selection_by_name("default",
2710 GS_TYPE_NORMAL, 1);
2712 /* ... we want to use entry guards */
2713 or_options_t *options = get_options_mutable();
2714 options->UseEntryGuards = 1;
2715 options->UseBridges = 0;
2717 /* ... prepare some md digests we want to download in the future */
2718 smartlist_t *digests = smartlist_new();
2719 const char *prose = "unhurried and wise, we perceive.";
2720 for (int i = 0; i < 20; i++) {
2721 smartlist_add(digests, (char*)prose);
2724 /* ... now mock some functions */
2725 mock_ns_val = tor_malloc_zero(sizeof(networkstatus_t));
2726 MOCK(networkstatus_get_latest_consensus_by_flavor, mock_ns_get_by_flavor);
2727 MOCK(directory_initiate_request, mock_directory_initiate_request);
2729 /* Test logic:
2730 * 0. Create a proper guard set and primary guard list.
2731 * 1. Pretend to fail microdescriptor fetches from all the primary guards.
2732 * 2. Order another microdescriptor fetch and make sure that primary guards
2733 * get skipped since they failed previous fetches.
2736 { /* Setup primary guard list */
2737 int i;
2738 entry_guards_update_primary(gs);
2739 for (i = 0; i < DFLT_N_PRIMARY_GUARDS; ++i) {
2740 entry_guard_t *guard = smartlist_get(gs->sampled_entry_guards, i);
2741 make_guard_confirmed(gs, guard);
2743 entry_guards_update_primary(gs);
2747 /* Fail microdesc fetches with all the primary guards */
2748 args = tor_malloc_zero(sizeof(response_handler_args_t));
2749 args->status_code = 404;
2750 args->reason = NULL;
2751 args->body = NULL;
2752 args->body_len = 0;
2754 conn = tor_malloc_zero(sizeof(dir_connection_t));
2755 conn->requested_resource = tor_strdup("d/jlinblackorigami");
2756 conn->base_.purpose = DIR_PURPOSE_FETCH_MICRODESC;
2758 /* Pretend to fail fetches with all primary guards */
2759 SMARTLIST_FOREACH_BEGIN(gs->primary_entry_guards,const entry_guard_t *,g) {
2760 memcpy(conn->identity_digest, g->identity, DIGEST_LEN);
2762 retval = handle_response_fetch_microdesc(conn, args);
2763 tt_int_op(retval, OP_EQ, 0);
2764 } SMARTLIST_FOREACH_END(g);
2768 /* Now order the final md download */
2769 setup_full_capture_of_logs(LOG_INFO);
2770 initiate_descriptor_downloads(NULL, DIR_PURPOSE_FETCH_MICRODESC,
2771 digests, 3, 7, 0);
2773 /* ... and check that because we failed to fetch microdescs from all our
2774 * primaries, we didnt end up selecting a primary for fetching dir info */
2775 expect_log_msg_containing("No primary or confirmed guards available.");
2776 teardown_capture_of_logs();
2779 done:
2780 smartlist_free(digests);
2781 tor_free(args);
2782 if (conn) {
2783 tor_free(conn->requested_resource);
2784 tor_free(conn);
2788 static const struct testcase_setup_t big_fake_network = {
2789 big_fake_network_setup, big_fake_network_cleanup
2792 static const struct testcase_setup_t upgrade_circuits = {
2793 upgrade_circuits_setup, upgrade_circuits_cleanup
2796 #define BFN_TEST(name) \
2797 { #name, test_entry_guard_ ## name, TT_FORK, &big_fake_network, NULL }
2799 #define UPGRADE_TEST(name, arg) \
2800 { #name, test_entry_guard_ ## name, TT_FORK, &upgrade_circuits, \
2801 (void*)(arg) }
2803 struct testcase_t entrynodes_tests[] = {
2804 { "node_preferred_orport",
2805 test_node_preferred_orport,
2806 0, NULL, NULL },
2807 { "entry_guard_describe", test_entry_guard_describe, 0, NULL, NULL },
2808 { "randomize_time", test_entry_guard_randomize_time, 0, NULL, NULL },
2809 { "encode_for_state_minimal",
2810 test_entry_guard_encode_for_state_minimal, 0, NULL, NULL },
2811 { "encode_for_state_maximal",
2812 test_entry_guard_encode_for_state_maximal, 0, NULL, NULL },
2813 { "parse_from_state_minimal",
2814 test_entry_guard_parse_from_state_minimal, 0, NULL, NULL },
2815 { "parse_from_state_maximal",
2816 test_entry_guard_parse_from_state_maximal, 0, NULL, NULL },
2817 { "parse_from_state_failure",
2818 test_entry_guard_parse_from_state_failure, 0, NULL, NULL },
2819 { "parse_from_state_partial_failure",
2820 test_entry_guard_parse_from_state_partial_failure, 0, NULL, NULL },
2821 { "parse_from_state_full",
2822 test_entry_guard_parse_from_state_full, TT_FORK, NULL, NULL },
2823 { "parse_from_state_broken",
2824 test_entry_guard_parse_from_state_broken, TT_FORK, NULL, NULL },
2825 { "get_guard_selection_by_name",
2826 test_entry_guard_get_guard_selection_by_name, TT_FORK, NULL, NULL },
2827 BFN_TEST(choose_selection_initial),
2828 BFN_TEST(add_single_guard),
2829 BFN_TEST(node_filter),
2830 BFN_TEST(expand_sample),
2831 BFN_TEST(expand_sample_small_net),
2832 BFN_TEST(update_from_consensus_status),
2833 BFN_TEST(update_from_consensus_repair),
2834 BFN_TEST(update_from_consensus_remove),
2835 BFN_TEST(confirming_guards),
2836 BFN_TEST(sample_reachable_filtered),
2837 BFN_TEST(sample_reachable_filtered_empty),
2838 BFN_TEST(retry_unreachable),
2839 BFN_TEST(manage_primary),
2840 { "guard_preferred", test_entry_guard_guard_preferred, TT_FORK, NULL, NULL },
2841 BFN_TEST(select_for_circuit_no_confirmed),
2842 BFN_TEST(select_for_circuit_confirmed),
2843 BFN_TEST(select_for_circuit_highlevel_primary),
2844 BFN_TEST(select_for_circuit_highlevel_confirm_other),
2845 BFN_TEST(select_for_circuit_highlevel_primary_retry),
2846 BFN_TEST(select_and_cancel),
2847 BFN_TEST(drop_guards),
2848 BFN_TEST(outdated_dirserver_exclusion),
2850 UPGRADE_TEST(upgrade_a_circuit, "c1-done c2-done"),
2851 UPGRADE_TEST(upgrade_blocked_by_live_primary_guards, "c1-done c2-done"),
2852 UPGRADE_TEST(upgrade_blocked_by_lack_of_waiting_circuits, ""),
2853 UPGRADE_TEST(upgrade_blocked_by_better_circ_complete, "c1-done c2-done"),
2854 UPGRADE_TEST(upgrade_not_blocked_by_restricted_circ_complete,
2855 "c1-done c2-done"),
2856 UPGRADE_TEST(upgrade_not_blocked_by_worse_circ_complete, "c1-done c2-done"),
2857 UPGRADE_TEST(upgrade_blocked_by_better_circ_pending, "c2-done"),
2858 UPGRADE_TEST(upgrade_not_blocked_by_restricted_circ_pending,
2859 "c2-done"),
2860 UPGRADE_TEST(upgrade_not_blocked_by_worse_circ_pending, "c1-done"),
2861 { "should_expire_waiting", test_enty_guard_should_expire_waiting, TT_FORK,
2862 NULL, NULL },
2864 END_OF_TESTCASES