Merge branch 'fix-changelogs' into 'main'
[tor.git] / src / test / test_entrynodes.c
blob7184e49c8c03186e553e734fde91eb2bd55acaa7
1 /* Copyright (c) 2014-2021, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 #include "orconfig.h"
6 #define CIRCUITLIST_PRIVATE
7 #define CIRCUITBUILD_PRIVATE
8 #define CONFIG_PRIVATE
9 #define STATEFILE_PRIVATE
10 #define ENTRYNODES_PRIVATE
11 #define ROUTERLIST_PRIVATE
12 #define DIRCLIENT_PRIVATE
14 #include "core/or/or.h"
15 #include "test/test.h"
17 #include "feature/client/bridges.h"
18 #include "core/or/circuitlist.h"
19 #include "core/or/circuitbuild.h"
20 #include "app/config/config.h"
21 #include "lib/confmgt/confmgt.h"
22 #include "lib/crypt_ops/crypto_rand.h"
23 #include "feature/dircommon/directory.h"
24 #include "feature/dirclient/dirclient.h"
25 #include "feature/client/entrynodes.h"
26 #include "feature/nodelist/nodelist.h"
27 #include "feature/nodelist/networkstatus.h"
28 #include "core/or/policies.h"
29 #include "feature/nodelist/routerlist.h"
30 #include "feature/nodelist/routerset.h"
31 #include "app/config/statefile.h"
33 #include "core/or/cpath_build_state_st.h"
34 #include "core/or/crypt_path_st.h"
35 #include "feature/dircommon/dir_connection_st.h"
36 #include "feature/nodelist/microdesc_st.h"
37 #include "feature/nodelist/networkstatus_st.h"
38 #include "feature/nodelist/node_st.h"
39 #include "core/or/origin_circuit_st.h"
40 #include "app/config/or_state_st.h"
41 #include "feature/nodelist/routerinfo_st.h"
42 #include "feature/nodelist/routerstatus_st.h"
44 #include "test/test_helpers.h"
45 #include "test/log_test_helpers.h"
47 #include "lib/container/bloomfilt.h"
48 #include "lib/encoding/confline.h"
50 /* TODO:
51 * choose_random_entry() test with state set.
53 * parse_state() tests with more than one guards.
55 * More tests for set_from_config(): Multiple nodes, use fingerprints,
56 * use country codes.
59 /** Dummy Tor state used in unittests. */
60 static or_state_t *dummy_state = NULL;
61 static or_state_t *
62 get_or_state_replacement(void)
64 return dummy_state;
67 static networkstatus_t *dummy_consensus = NULL;
69 static smartlist_t *big_fake_net_nodes = NULL;
71 static const smartlist_t *
72 bfn_mock_nodelist_get_list(void)
74 return big_fake_net_nodes;
77 static networkstatus_t *
78 bfn_mock_networkstatus_get_reasonably_live_consensus(time_t now, int flavor)
80 (void)now;
81 (void)flavor;
82 return dummy_consensus;
85 static const node_t *
86 bfn_mock_node_get_by_id(const char *id)
88 SMARTLIST_FOREACH(big_fake_net_nodes, node_t *, n,
89 if (fast_memeq(n->identity, id, 20))
90 return n);
92 return NULL;
95 static int
96 mock_router_have_minimum_dir_info(void)
98 return 1;
101 /* Helper function to free a test node. */
102 static void
103 test_node_free(node_t *n)
105 tor_free(n->rs);
106 tor_free(n->md->onion_curve25519_pkey);
107 short_policy_free(n->md->exit_policy);
108 tor_free(n->md);
109 tor_free(n);
112 /* Unittest cleanup function: Cleanup the fake network. */
113 static int
114 big_fake_network_cleanup(const struct testcase_t *testcase, void *ptr)
116 (void) testcase;
117 (void) ptr;
119 if (big_fake_net_nodes) {
120 SMARTLIST_FOREACH(big_fake_net_nodes, node_t *, n, {
121 test_node_free(n);
123 smartlist_free(big_fake_net_nodes);
126 UNMOCK(nodelist_get_list);
127 UNMOCK(node_get_by_id);
128 UNMOCK(get_or_state);
129 UNMOCK(networkstatus_get_reasonably_live_consensus);
130 or_state_free(dummy_state);
131 dummy_state = NULL;
132 tor_free(dummy_consensus);
134 return 1; /* NOP */
137 #define REASONABLY_FUTURE " reasonably-future"
138 #define REASONABLY_PAST " reasonably-past"
140 /* Unittest setup function: Setup a fake network. */
141 static void *
142 big_fake_network_setup(const struct testcase_t *testcase)
144 int i;
146 /* These are minimal node_t objects that only contain the aspects of node_t
147 * that we need for entrynodes.c. */
148 const int N_NODES = 271;
150 const char *argument = testcase->setup_data;
151 int reasonably_future_consensus = 0, reasonably_past_consensus = 0;
152 if (argument) {
153 reasonably_future_consensus = strstr(argument, REASONABLY_FUTURE) != NULL;
154 reasonably_past_consensus = strstr(argument, REASONABLY_PAST) != NULL;
157 big_fake_net_nodes = smartlist_new();
158 for (i = 0; i < N_NODES; ++i) {
159 curve25519_secret_key_t curve25519_secret_key;
161 node_t *n = tor_malloc_zero(sizeof(node_t));
162 n->md = tor_malloc_zero(sizeof(microdesc_t));
164 /* Generate curve25519 key for this node */
165 n->md->onion_curve25519_pkey =
166 tor_malloc_zero(sizeof(curve25519_public_key_t));
167 curve25519_secret_key_generate(&curve25519_secret_key, 0);
168 curve25519_public_key_generate(n->md->onion_curve25519_pkey,
169 &curve25519_secret_key);
171 crypto_rand(n->identity, sizeof(n->identity));
172 n->rs = tor_malloc_zero(sizeof(routerstatus_t));
174 memcpy(n->rs->identity_digest, n->identity, DIGEST_LEN);
176 n->is_running = n->is_valid = n->is_fast = n->is_stable = 1;
178 /* Note: all these guards have the same address, so you'll need to
179 * disable EnforceDistinctSubnets when a restriction is applied. */
180 tor_addr_from_ipv4h(&n->rs->ipv4_addr, 0x04020202);
181 n->rs->ipv4_orport = 1234;
182 n->rs->is_v2_dir = 1;
183 n->rs->has_bandwidth = 1;
184 n->rs->bandwidth_kb = 30;
186 /* Make a random nickname for each node */
188 char nickname_binary[8];
189 crypto_rand(nickname_binary, sizeof(nickname_binary));
190 base32_encode(n->rs->nickname, sizeof(n->rs->nickname),
191 nickname_binary, sizeof(nickname_binary));
194 /* Call half of the nodes a possible guard. */
195 if (i % 2 == 0) {
196 n->is_possible_guard = 1;
197 n->rs->guardfraction_percentage = 100;
198 n->rs->has_guardfraction = 1;
199 n->rs->is_possible_guard = 1;
202 /* Make some of these nodes a possible exit */
203 if (i % 7 == 0) {
204 n->md->exit_policy = parse_short_policy("accept 443");
207 n->nodelist_idx = smartlist_len(big_fake_net_nodes);
208 smartlist_add(big_fake_net_nodes, n);
211 dummy_state = or_state_new();
212 dummy_consensus = tor_malloc_zero(sizeof(networkstatus_t));
213 if (reasonably_future_consensus) {
214 /* Make the dummy consensus valid in 6 hours, and expiring in 7 hours. */
215 dummy_consensus->valid_after = approx_time() + 6*3600;
216 dummy_consensus->valid_until = approx_time() + 7*3600;
217 } else if (reasonably_past_consensus) {
218 /* Make the dummy consensus valid from 16 hours ago, but expired 12 hours
219 * ago. */
220 dummy_consensus->valid_after = approx_time() - 16*3600;
221 dummy_consensus->valid_until = approx_time() - 12*3600;
222 } else {
223 /* Make the dummy consensus valid for an hour either side of now. */
224 dummy_consensus->valid_after = approx_time() - 3600;
225 dummy_consensus->valid_until = approx_time() + 3600;
228 MOCK(nodelist_get_list, bfn_mock_nodelist_get_list);
229 MOCK(node_get_by_id, bfn_mock_node_get_by_id);
230 MOCK(get_or_state,
231 get_or_state_replacement);
232 MOCK(networkstatus_get_reasonably_live_consensus,
233 bfn_mock_networkstatus_get_reasonably_live_consensus);
234 /* Return anything but NULL (it's interpreted as test fail) */
235 return (void*)testcase;
238 static time_t
239 mock_randomize_time_no_randomization(time_t a, time_t b)
241 (void) b;
242 return a;
245 static or_options_t *mocked_options;
247 static const or_options_t *
248 mock_get_options(void)
250 return mocked_options;
253 #define TEST_IPV4_ADDR "123.45.67.89"
254 #define TEST_IPV6_ADDR "[1234:5678:90ab:cdef::]"
256 static void
257 test_node_preferred_orport(void *arg)
259 (void)arg;
260 tor_addr_t ipv4_addr;
261 const uint16_t ipv4_port = 4444;
262 tor_addr_t ipv6_addr;
263 const uint16_t ipv6_port = 6666;
264 routerinfo_t node_ri;
265 node_t node;
266 tor_addr_port_t ap;
268 /* Setup options */
269 mocked_options = options_new();
270 /* We don't test ClientPreferIPv6ORPort here, because it's used in
271 * nodelist_set_consensus to setup node.ipv6_preferred, which we set
272 * directly. */
273 MOCK(get_options, mock_get_options);
275 /* Setup IP addresses */
276 tor_addr_parse(&ipv4_addr, TEST_IPV4_ADDR);
277 tor_addr_parse(&ipv6_addr, TEST_IPV6_ADDR);
279 /* Setup node_ri */
280 memset(&node_ri, 0, sizeof(node_ri));
281 tor_addr_copy(&node_ri.ipv4_addr, &ipv4_addr);
282 node_ri.ipv4_orport = ipv4_port;
283 tor_addr_copy(&node_ri.ipv6_addr, &ipv6_addr);
284 node_ri.ipv6_orport = ipv6_port;
286 /* Setup node */
287 memset(&node, 0, sizeof(node));
288 node.ri = &node_ri;
290 /* Check the preferred address is IPv4 if we're only using IPv4, regardless
291 * of whether we prefer it or not */
292 mocked_options->ClientUseIPv4 = 1;
293 mocked_options->ClientUseIPv6 = 0;
294 node.ipv6_preferred = 0;
295 node_get_pref_orport(&node, &ap);
296 tt_assert(tor_addr_eq(&ap.addr, &ipv4_addr));
297 tt_assert(ap.port == ipv4_port);
299 node.ipv6_preferred = 1;
300 node_get_pref_orport(&node, &ap);
301 tt_assert(tor_addr_eq(&ap.addr, &ipv4_addr));
302 tt_assert(ap.port == ipv4_port);
304 /* Check the preferred address is IPv4 if we're using IPv4 and IPv6, but
305 * don't prefer the IPv6 address */
306 mocked_options->ClientUseIPv4 = 1;
307 mocked_options->ClientUseIPv6 = 1;
308 node.ipv6_preferred = 0;
309 node_get_pref_orport(&node, &ap);
310 tt_assert(tor_addr_eq(&ap.addr, &ipv4_addr));
311 tt_assert(ap.port == ipv4_port);
313 /* Check the preferred address is IPv6 if we prefer it and
314 * ClientUseIPv6 is 1, regardless of ClientUseIPv4 */
315 mocked_options->ClientUseIPv4 = 1;
316 mocked_options->ClientUseIPv6 = 1;
317 node.ipv6_preferred = 1;
318 node_get_pref_orport(&node, &ap);
319 tt_assert(tor_addr_eq(&ap.addr, &ipv6_addr));
320 tt_assert(ap.port == ipv6_port);
322 mocked_options->ClientUseIPv4 = 0;
323 node_get_pref_orport(&node, &ap);
324 tt_assert(tor_addr_eq(&ap.addr, &ipv6_addr));
325 tt_assert(ap.port == ipv6_port);
327 /* Check the preferred address is IPv6 if we don't prefer it, but
328 * ClientUseIPv4 is 0 */
329 mocked_options->ClientUseIPv4 = 0;
330 mocked_options->ClientUseIPv6 = 1;
331 node.ipv6_preferred = reachable_addr_prefer_ipv6_orport(mocked_options);
332 node_get_pref_orport(&node, &ap);
333 tt_assert(tor_addr_eq(&ap.addr, &ipv6_addr));
334 tt_assert(ap.port == ipv6_port);
336 done:
337 or_options_free(mocked_options);
338 UNMOCK(get_options);
341 static void
342 test_entry_guard_describe(void *arg)
344 (void)arg;
345 entry_guard_t g;
346 memset(&g, 0, sizeof(g));
347 strlcpy(g.nickname, "okefenokee", sizeof(g.nickname));
348 memcpy(g.identity, "theforestprimeval---", DIGEST_LEN);
350 tt_str_op(entry_guard_describe(&g), OP_EQ,
351 "okefenokee ($746865666F726573747072696D6576616C2D2D2D)");
353 done:
357 static void
358 test_entry_guard_randomize_time(void *arg)
360 const time_t now = 1479153573;
361 const int delay = 86400;
362 const int N = 1000;
363 (void)arg;
365 time_t t;
366 int i;
367 for (i = 0; i < N; ++i) {
368 t = randomize_time(now, delay);
369 tt_int_op(t, OP_LE, now);
370 tt_int_op(t, OP_GE, now-delay);
373 /* now try the corner cases */
374 for (i = 0; i < N; ++i) {
375 t = randomize_time(100, delay);
376 tt_int_op(t, OP_GE, 1);
377 tt_int_op(t, OP_LE, 100);
379 t = randomize_time(0, delay);
380 tt_int_op(t, OP_EQ, 1);
383 done:
387 static void
388 test_entry_guard_encode_for_state_minimal(void *arg)
390 (void) arg;
391 entry_guard_t *eg = tor_malloc_zero(sizeof(entry_guard_t));
393 eg->selection_name = tor_strdup("wubwub");
394 memcpy(eg->identity, "plurpyflurpyslurpydo", DIGEST_LEN);
395 eg->sampled_on_date = 1479081600;
396 eg->confirmed_idx = -1;
398 char *s = NULL;
399 s = entry_guard_encode_for_state(eg, 0);
401 tt_str_op(s, OP_EQ,
402 "in=wubwub "
403 "rsa_id=706C75727079666C75727079736C75727079646F "
404 "sampled_on=2016-11-14T00:00:00 "
405 "sampled_idx=0 "
406 "listed=0");
408 done:
409 entry_guard_free(eg);
410 tor_free(s);
413 static void
414 test_entry_guard_encode_for_state_maximal(void *arg)
416 (void) arg;
417 entry_guard_t *eg = tor_malloc_zero(sizeof(entry_guard_t));
419 strlcpy(eg->nickname, "Fred", sizeof(eg->nickname));
420 eg->selection_name = tor_strdup("default");
421 memcpy(eg->identity, "plurpyflurpyslurpydo", DIGEST_LEN);
422 eg->bridge_addr = tor_malloc_zero(sizeof(tor_addr_port_t));
423 tor_addr_from_ipv4h(&eg->bridge_addr->addr, 0x08080404);
424 eg->bridge_addr->port = 9999;
425 eg->sampled_on_date = 1479081600;
426 eg->sampled_by_version = tor_strdup("1.2.3");
427 eg->unlisted_since_date = 1479081645;
428 eg->currently_listed = 1;
429 eg->confirmed_on_date = 1479081690;
430 eg->confirmed_idx = 333;
431 eg->sampled_idx = 42;
432 eg->extra_state_fields = tor_strdup("and the green grass grew all around");
434 char *s = NULL;
435 s = entry_guard_encode_for_state(eg, 0);
437 tt_str_op(s, OP_EQ,
438 "in=default "
439 "rsa_id=706C75727079666C75727079736C75727079646F "
440 "bridge_addr=8.8.4.4:9999 "
441 "nickname=Fred "
442 "sampled_on=2016-11-14T00:00:00 "
443 "sampled_idx=0 "
444 "sampled_by=1.2.3 "
445 "unlisted_since=2016-11-14T00:00:45 "
446 "listed=1 "
447 "confirmed_on=2016-11-14T00:01:30 "
448 "confirmed_idx=333 "
449 "and the green grass grew all around");
451 done:
452 entry_guard_free(eg);
453 tor_free(s);
456 static void
457 test_entry_guard_parse_from_state_minimal(void *arg)
459 (void)arg;
460 char *mem_op_hex_tmp = NULL;
461 entry_guard_t *eg = NULL;
462 time_t t = approx_time();
464 eg = entry_guard_parse_from_state(
465 "in=default_plus "
466 "rsa_id=596f75206d6179206e656564206120686f626279");
467 tt_assert(eg);
469 tt_str_op(eg->selection_name, OP_EQ, "default_plus");
470 test_mem_op_hex(eg->identity, OP_EQ,
471 "596f75206d6179206e656564206120686f626279");
472 tt_str_op(eg->nickname, OP_EQ, "$596F75206D6179206E656564206120686F626279");
473 tt_ptr_op(eg->bridge_addr, OP_EQ, NULL);
474 tt_i64_op(eg->sampled_on_date, OP_GE, t);
475 tt_i64_op(eg->sampled_on_date, OP_LE, t+86400);
476 tt_i64_op(eg->unlisted_since_date, OP_EQ, 0);
477 tt_ptr_op(eg->sampled_by_version, OP_EQ, NULL);
478 tt_int_op(eg->currently_listed, OP_EQ, 0);
479 tt_i64_op(eg->confirmed_on_date, OP_EQ, 0);
480 tt_int_op(eg->confirmed_idx, OP_EQ, -1);
482 tt_int_op(eg->last_tried_to_connect, OP_EQ, 0);
483 tt_int_op(eg->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
485 done:
486 entry_guard_free(eg);
487 tor_free(mem_op_hex_tmp);
490 static void
491 test_entry_guard_parse_from_state_maximal(void *arg)
493 (void)arg;
494 char *mem_op_hex_tmp = NULL;
495 entry_guard_t *eg = NULL;
497 eg = entry_guard_parse_from_state(
498 "in=fred "
499 "rsa_id=706C75727079666C75727079736C75727079646F "
500 "bridge_addr=[1::3]:9999 "
501 "nickname=Fred "
502 "sampled_on=2016-11-14T00:00:00 "
503 "sampled_by=1.2.3 "
504 "unlisted_since=2016-11-14T00:00:45 "
505 "listed=1 "
506 "confirmed_on=2016-11-14T00:01:30 "
507 "confirmed_idx=333 "
508 "and the green grass grew all around "
509 "rsa_id=all,around");
510 tt_assert(eg);
512 test_mem_op_hex(eg->identity, OP_EQ,
513 "706C75727079666C75727079736C75727079646F");
514 tt_str_op(fmt_addr(&eg->bridge_addr->addr), OP_EQ, "1::3");
515 tt_int_op(eg->bridge_addr->port, OP_EQ, 9999);
516 tt_str_op(eg->nickname, OP_EQ, "Fred");
517 tt_i64_op(eg->sampled_on_date, OP_EQ, 1479081600);
518 tt_i64_op(eg->unlisted_since_date, OP_EQ, 1479081645);
519 tt_str_op(eg->sampled_by_version, OP_EQ, "1.2.3");
520 tt_int_op(eg->currently_listed, OP_EQ, 1);
521 tt_i64_op(eg->confirmed_on_date, OP_EQ, 1479081690);
522 tt_int_op(eg->confirmed_idx, OP_EQ, 333);
523 tt_str_op(eg->extra_state_fields, OP_EQ,
524 "and the green grass grew all around rsa_id=all,around");
526 tt_int_op(eg->last_tried_to_connect, OP_EQ, 0);
527 tt_int_op(eg->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
529 done:
530 entry_guard_free(eg);
531 tor_free(mem_op_hex_tmp);
534 static void
535 test_entry_guard_parse_from_state_failure(void *arg)
537 (void)arg;
538 entry_guard_t *eg = NULL;
540 /* no selection */
541 eg = entry_guard_parse_from_state(
542 "rsa_id=596f75206d6179206e656564206120686f626270");
543 tt_ptr_op(eg, OP_EQ, NULL);
545 /* no RSA ID. */
546 eg = entry_guard_parse_from_state("in=default nickname=Fred");
547 tt_ptr_op(eg, OP_EQ, NULL);
549 /* Bad RSA ID: bad character. */
550 eg = entry_guard_parse_from_state(
551 "in=default "
552 "rsa_id=596f75206d6179206e656564206120686f62627q");
553 tt_ptr_op(eg, OP_EQ, NULL);
555 /* Bad RSA ID: too long.*/
556 eg = entry_guard_parse_from_state(
557 "in=default "
558 "rsa_id=596f75206d6179206e656564206120686f6262703");
559 tt_ptr_op(eg, OP_EQ, NULL);
561 /* Bad RSA ID: too short.*/
562 eg = entry_guard_parse_from_state(
563 "in=default "
564 "rsa_id=596f75206d6179206e65656420612");
565 tt_ptr_op(eg, OP_EQ, NULL);
567 done:
568 entry_guard_free(eg);
571 static void
572 test_entry_guard_parse_from_state_partial_failure(void *arg)
574 (void)arg;
575 char *mem_op_hex_tmp = NULL;
576 entry_guard_t *eg = NULL;
577 time_t t = approx_time();
579 eg = entry_guard_parse_from_state(
580 "in=default "
581 "rsa_id=706C75727079666C75727079736C75727079646F "
582 "bridge_addr=1.2.3.3.4:5 "
583 "nickname=FredIsANodeWithAStrangeNicknameThatIsTooLong "
584 "sampled_on=2016-11-14T00:00:99 "
585 "sampled_by=1.2.3 stuff in the middle "
586 "unlisted_since=2016-xx-14T00:00:45 "
587 "listed=0 "
588 "confirmed_on=2016-11-14T00:01:30zz "
589 "confirmed_idx=idx "
590 "and the green grass grew all around "
591 "rsa_id=all,around");
592 tt_assert(eg);
594 test_mem_op_hex(eg->identity, OP_EQ,
595 "706C75727079666C75727079736C75727079646F");
596 tt_str_op(eg->nickname, OP_EQ, "FredIsANodeWithAStrangeNicknameThatIsTooL");
597 tt_ptr_op(eg->bridge_addr, OP_EQ, NULL);
598 tt_i64_op(eg->sampled_on_date, OP_EQ, t);
599 tt_i64_op(eg->unlisted_since_date, OP_EQ, 0);
600 tt_str_op(eg->sampled_by_version, OP_EQ, "1.2.3");
601 tt_int_op(eg->currently_listed, OP_EQ, 0);
602 tt_i64_op(eg->confirmed_on_date, OP_EQ, 0);
603 tt_int_op(eg->confirmed_idx, OP_EQ, -1);
604 tt_str_op(eg->extra_state_fields, OP_EQ,
605 "stuff in the middle and the green grass grew all around "
606 "rsa_id=all,around");
608 tt_int_op(eg->last_tried_to_connect, OP_EQ, 0);
609 tt_int_op(eg->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
611 done:
612 entry_guard_free(eg);
613 tor_free(mem_op_hex_tmp);
616 static int
617 mock_entry_guard_is_listed(guard_selection_t *gs, const entry_guard_t *guard)
619 (void)gs;
620 (void)guard;
621 return 1;
624 static void
625 test_entry_guard_parse_from_state_full(void *arg)
627 (void)arg;
628 /* Here's a state I made while testing. The identities and locations for
629 * the bridges are redacted. */
630 const char STATE[] =
631 "Guard in=default rsa_id=214F44BD5B638E8C817D47FF7C97397790BF0345 "
632 "nickname=TotallyNinja sampled_on=2016-11-12T19:32:49 "
633 "sampled_idx=0 "
634 "sampled_by=0.3.0.0-alpha-dev "
635 "listed=1\n"
636 "Guard in=default rsa_id=052900AB0EA3ED54BAB84AE8A99E74E8693CE2B2 "
637 "nickname=5OfNovember sampled_on=2016-11-20T04:32:05 "
638 "sampled_idx=1 "
639 "sampled_by=0.3.0.0-alpha-dev "
640 "listed=1 confirmed_on=2016-11-22T08:13:28 confirmed_idx=0 "
641 "pb_circ_attempts=4.000000 pb_circ_successes=2.000000 "
642 "pb_successful_circuits_closed=2.000000\n"
643 "Guard in=default rsa_id=7B700C0C207EBD0002E00F499BE265519AC3C25A "
644 "nickname=dc6jgk11 sampled_on=2016-11-28T11:50:13 "
645 "sampled_idx=2 "
646 "sampled_by=0.3.0.0-alpha-dev "
647 "listed=1 confirmed_on=2016-11-24T08:45:30 confirmed_idx=4 "
648 "pb_circ_attempts=5.000000 pb_circ_successes=5.000000 "
649 "pb_successful_circuits_closed=5.000000\n"
650 "Guard in=wobblesome rsa_id=7B700C0C207EBD0002E00F499BE265519AC3C25A "
651 "nickname=dc6jgk11 sampled_on=2016-11-28T11:50:13 "
652 "sampled_idx=0 "
653 "sampled_by=0.3.0.0-alpha-dev "
654 "listed=1\n"
655 "Guard in=default rsa_id=E9025AD60D86875D5F11548D536CC6AF60F0EF5E "
656 "nickname=maibrunn sampled_on=2016-11-25T22:36:38 "
657 "sampled_idx=3 "
658 "sampled_by=0.3.0.0-alpha-dev listed=1\n"
659 "Guard in=default rsa_id=DCD30B90BA3A792DA75DC54A327EF353FB84C38E "
660 "nickname=Unnamed sampled_on=2016-11-25T14:34:00 "
661 "sampled_idx=10 "
662 "sampled_by=0.3.0.0-alpha-dev listed=1\n"
663 "Guard in=bridges rsa_id=8FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2E "
664 "bridge_addr=24.1.1.1:443 sampled_on=2016-11-25T06:44:14 "
665 "sampled_idx=0 "
666 "sampled_by=0.3.0.0-alpha-dev listed=1 "
667 "confirmed_on=2016-11-29T10:36:06 confirmed_idx=0 "
668 "pb_circ_attempts=8.000000 pb_circ_successes=8.000000 "
669 "pb_successful_circuits_closed=13.000000\n"
670 "Guard in=bridges rsa_id=5800000000000000000000000000000000000000 "
671 "bridge_addr=37.218.246.143:28366 "
672 "sampled_on=2016-11-18T15:07:34 sampled_idx=1 "
673 "sampled_by=0.3.0.0-alpha-dev listed=1\n";
675 config_line_t *lines = NULL;
676 or_state_t *state = tor_malloc_zero(sizeof(or_state_t));
677 int r = config_get_lines(STATE, &lines, 0);
678 char *msg = NULL;
679 smartlist_t *text = smartlist_new();
680 char *joined = NULL;
682 // So nodes aren't expired. This is Tue, 13 Dec 2016 09:37:14 GMT
683 update_approx_time(1481621834);
685 MOCK(entry_guard_is_listed, mock_entry_guard_is_listed);
687 dummy_state = state;
688 MOCK(get_or_state,
689 get_or_state_replacement);
691 tt_int_op(r, OP_EQ, 0);
692 tt_assert(lines);
694 state->Guard = lines;
696 /* Try it first without setting the result. */
697 r = entry_guards_parse_state(state, 0, &msg);
698 tt_int_op(r, OP_EQ, 0);
699 guard_selection_t *gs_br =
700 get_guard_selection_by_name("bridges", GS_TYPE_BRIDGE, 0);
701 tt_ptr_op(gs_br, OP_EQ, NULL);
703 r = entry_guards_parse_state(state, 1, &msg);
704 tt_int_op(r, OP_EQ, 0);
705 gs_br = get_guard_selection_by_name("bridges", GS_TYPE_BRIDGE, 0);
706 guard_selection_t *gs_df =
707 get_guard_selection_by_name("default", GS_TYPE_NORMAL, 0);
708 guard_selection_t *gs_wb =
709 get_guard_selection_by_name("wobblesome", GS_TYPE_NORMAL, 0);
711 tt_assert(gs_br);
712 tt_assert(gs_df);
713 tt_assert(gs_wb);
715 tt_int_op(smartlist_len(gs_df->sampled_entry_guards), OP_EQ, 5);
716 tt_int_op(smartlist_len(gs_br->sampled_entry_guards), OP_EQ, 2);
717 tt_int_op(smartlist_len(gs_wb->sampled_entry_guards), OP_EQ, 1);
719 /* Try again; make sure it doesn't double-add the guards. */
720 r = entry_guards_parse_state(state, 1, &msg);
721 tt_int_op(r, OP_EQ, 0);
722 gs_br = get_guard_selection_by_name("bridges", GS_TYPE_BRIDGE, 0);
723 gs_df = get_guard_selection_by_name("default", GS_TYPE_NORMAL, 0);
724 tt_assert(gs_br);
725 tt_assert(gs_df);
726 tt_int_op(smartlist_len(gs_df->sampled_entry_guards), OP_EQ, 5);
727 tt_int_op(smartlist_len(gs_br->sampled_entry_guards), OP_EQ, 2);
729 /* Re-encode; it should be the same... almost. */
731 /* (Make a guard nonpersistent first) */
732 entry_guard_t *g = smartlist_get(gs_df->sampled_entry_guards, 0);
733 g->is_persistent = 0;
735 config_free_lines(lines);
736 lines = state->Guard = NULL; // to prevent double-free.
737 entry_guards_update_state(state);
738 tt_assert(state->Guard);
739 lines = state->Guard;
741 config_line_t *ln;
742 for (ln = lines; ln; ln = ln->next) {
743 smartlist_add_asprintf(text, "%s %s\n",ln->key, ln->value);
745 joined = smartlist_join_strings(text, "", 0, NULL);
746 tt_str_op(joined, OP_EQ,
747 "Guard in=default rsa_id=052900AB0EA3ED54BAB84AE8A99E74E8693CE2B2 "
748 "nickname=5OfNovember sampled_on=2016-11-20T04:32:05 "
749 "sampled_idx=0 "
750 "sampled_by=0.3.0.0-alpha-dev "
751 "listed=1 confirmed_on=2016-11-22T08:13:28 confirmed_idx=0 "
752 "pb_circ_attempts=4.000000 pb_circ_successes=2.000000 "
753 "pb_successful_circuits_closed=2.000000\n"
754 "Guard in=default rsa_id=7B700C0C207EBD0002E00F499BE265519AC3C25A "
755 "nickname=dc6jgk11 sampled_on=2016-11-28T11:50:13 "
756 "sampled_idx=1 "
757 "sampled_by=0.3.0.0-alpha-dev "
758 "listed=1 confirmed_on=2016-11-24T08:45:30 confirmed_idx=1 "
759 "pb_circ_attempts=5.000000 pb_circ_successes=5.000000 "
760 "pb_successful_circuits_closed=5.000000\n"
761 "Guard in=default rsa_id=E9025AD60D86875D5F11548D536CC6AF60F0EF5E "
762 "nickname=maibrunn sampled_on=2016-11-25T22:36:38 "
763 "sampled_idx=2 "
764 "sampled_by=0.3.0.0-alpha-dev listed=1\n"
765 "Guard in=default rsa_id=DCD30B90BA3A792DA75DC54A327EF353FB84C38E "
766 "nickname=Unnamed sampled_on=2016-11-25T14:34:00 "
767 "sampled_idx=3 "
768 "sampled_by=0.3.0.0-alpha-dev listed=1\n"
769 "Guard in=wobblesome rsa_id=7B700C0C207EBD0002E00F499BE265519AC3C25A "
770 "nickname=dc6jgk11 sampled_on=2016-11-28T11:50:13 "
771 "sampled_idx=0 "
772 "sampled_by=0.3.0.0-alpha-dev "
773 "listed=1\n"
774 "Guard in=bridges rsa_id=8FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2E "
775 "bridge_addr=24.1.1.1:443 sampled_on=2016-11-25T06:44:14 "
776 "sampled_idx=0 "
777 "sampled_by=0.3.0.0-alpha-dev listed=1 "
778 "confirmed_on=2016-11-29T10:36:06 confirmed_idx=0 "
779 "pb_circ_attempts=8.000000 pb_circ_successes=8.000000 "
780 "pb_successful_circuits_closed=13.000000\n"
781 "Guard in=bridges rsa_id=5800000000000000000000000000000000000000 "
782 "bridge_addr=37.218.246.143:28366 "
783 "sampled_on=2016-11-18T15:07:34 sampled_idx=1 "
784 "sampled_by=0.3.0.0-alpha-dev listed=1\n");
786 done:
787 config_free_lines(lines);
788 tor_free(state);
789 tor_free(msg);
790 UNMOCK(get_or_state);
791 UNMOCK(entry_guard_is_listed);
792 SMARTLIST_FOREACH(text, char *, cp, tor_free(cp));
793 smartlist_free(text);
794 tor_free(joined);
797 static void
798 test_entry_guard_parse_from_state_broken(void *arg)
800 (void)arg;
801 /* Here's a variation on the previous state. Every line but the first is
802 * busted somehow. */
803 const char STATE[] =
804 /* Okay. */
805 "Guard in=default rsa_id=214F44BD5B638E8C817D47FF7C97397790BF0345 "
806 "nickname=TotallyNinja sampled_on=2016-11-12T19:32:49 "
807 "sampled_by=0.3.0.0-alpha-dev "
808 "listed=1\n"
809 /* No selection listed. */
810 "Guard rsa_id=052900AB0EA3ED54BAB84AE8A99E74E8693CE2B2 "
811 "nickname=5OfNovember sampled_on=2016-11-20T04:32:05 "
812 "sampled_by=0.3.0.0-alpha-dev "
813 "listed=1 confirmed_on=2016-11-22T08:13:28 confirmed_idx=0 "
814 "pb_circ_attempts=4.000000 pb_circ_successes=2.000000 "
815 "pb_successful_circuits_closed=2.000000\n"
816 /* Selection is "legacy"!! */
817 "Guard in=legacy rsa_id=7B700C0C207EBD0002E00F499BE265519AC3C25A "
818 "nickname=dc6jgk11 sampled_on=2016-11-28T11:50:13 "
819 "sampled_by=0.3.0.0-alpha-dev "
820 "listed=1 confirmed_on=2016-11-24T08:45:30 confirmed_idx=4 "
821 "pb_circ_attempts=5.000000 pb_circ_successes=5.000000 "
822 "pb_successful_circuits_closed=5.000000\n";
824 config_line_t *lines = NULL;
825 or_state_t *state = tor_malloc_zero(sizeof(or_state_t));
826 int r = config_get_lines(STATE, &lines, 0);
827 char *msg = NULL;
829 dummy_state = state;
830 MOCK(get_or_state,
831 get_or_state_replacement);
833 tt_int_op(r, OP_EQ, 0);
834 tt_assert(lines);
836 state->Guard = lines;
838 /* First, no-set case. we should get an error. */
839 r = entry_guards_parse_state(state, 0, &msg);
840 tt_int_op(r, OP_LT, 0);
841 tt_ptr_op(msg, OP_NE, NULL);
842 /* And we shouldn't have made anything. */
843 guard_selection_t *gs_df =
844 get_guard_selection_by_name("default", GS_TYPE_NORMAL, 0);
845 tt_ptr_op(gs_df, OP_EQ, NULL);
846 tor_free(msg);
848 /* Now see about the set case (which shouldn't happen IRL) */
849 r = entry_guards_parse_state(state, 1, &msg);
850 tt_int_op(r, OP_LT, 0);
851 tt_ptr_op(msg, OP_NE, NULL);
852 gs_df = get_guard_selection_by_name("default", GS_TYPE_NORMAL, 0);
853 tt_ptr_op(gs_df, OP_NE, NULL);
854 tt_int_op(smartlist_len(gs_df->sampled_entry_guards), OP_EQ, 1);
856 done:
857 config_free_lines(lines);
858 tor_free(state);
859 tor_free(msg);
860 UNMOCK(get_or_state);
863 static void
864 test_entry_guard_get_guard_selection_by_name(void *arg)
866 (void)arg;
867 guard_selection_t *gs1, *gs2, *gs3;
869 gs1 = get_guard_selection_by_name("unlikely", GS_TYPE_NORMAL, 0);
870 tt_ptr_op(gs1, OP_EQ, NULL);
871 gs1 = get_guard_selection_by_name("unlikely", GS_TYPE_NORMAL, 1);
872 tt_ptr_op(gs1, OP_NE, NULL);
873 gs2 = get_guard_selection_by_name("unlikely", GS_TYPE_NORMAL, 1);
874 tt_assert(gs2 == gs1);
875 gs2 = get_guard_selection_by_name("unlikely", GS_TYPE_NORMAL, 0);
876 tt_assert(gs2 == gs1);
878 gs2 = get_guard_selection_by_name("implausible", GS_TYPE_NORMAL, 0);
879 tt_ptr_op(gs2, OP_EQ, NULL);
880 gs2 = get_guard_selection_by_name("implausible", GS_TYPE_NORMAL, 1);
881 tt_ptr_op(gs2, OP_NE, NULL);
882 tt_assert(gs2 != gs1);
883 gs3 = get_guard_selection_by_name("implausible", GS_TYPE_NORMAL, 0);
884 tt_assert(gs3 == gs2);
886 gs3 = get_guard_selection_by_name("default", GS_TYPE_NORMAL, 0);
887 tt_ptr_op(gs3, OP_EQ, NULL);
888 gs3 = get_guard_selection_by_name("default", GS_TYPE_NORMAL, 1);
889 tt_ptr_op(gs3, OP_NE, NULL);
890 tt_assert(gs3 != gs2);
891 tt_assert(gs3 != gs1);
892 tt_assert(gs3 == get_guard_selection_info());
894 done:
895 entry_guards_free_all();
898 static void
899 test_entry_guard_choose_selection_initial(void *arg)
901 /* Tests for picking our initial guard selection (based on having had
902 * no previous selection */
903 (void)arg;
904 guard_selection_type_t type = GS_TYPE_INFER;
905 const char *name = choose_guard_selection(get_options(),
906 dummy_consensus, NULL, &type);
907 tt_str_op(name, OP_EQ, "default");
908 tt_int_op(type, OP_EQ, GS_TYPE_NORMAL);
910 /* If we're using bridges, we get the bridge selection. */
911 get_options_mutable()->UseBridges = 1;
912 name = choose_guard_selection(get_options(),
913 dummy_consensus, NULL, &type);
914 tt_str_op(name, OP_EQ, "bridges");
915 tt_int_op(type, OP_EQ, GS_TYPE_BRIDGE);
916 get_options_mutable()->UseBridges = 0;
918 /* If we discard >99% of our guards, though, we should be in the restricted
919 * set. */
920 tt_assert(get_options_mutable()->EntryNodes == NULL);
921 get_options_mutable()->EntryNodes = routerset_new();
922 routerset_parse(get_options_mutable()->EntryNodes, "1.0.0.0/8", "foo");
923 name = choose_guard_selection(get_options(),
924 dummy_consensus, NULL, &type);
925 tt_str_op(name, OP_EQ, "restricted");
926 tt_int_op(type, OP_EQ, GS_TYPE_RESTRICTED);
928 done:
932 static void
933 test_entry_guard_add_single_guard(void *arg)
935 (void)arg;
936 guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
938 /* 1: Add a single guard to the sample. */
939 node_t *n1 = smartlist_get(big_fake_net_nodes, 0);
940 time_t now = approx_time();
941 tt_assert(n1->is_possible_guard == 1);
942 entry_guard_t *g1 = entry_guard_add_to_sample(gs, n1);
943 tt_assert(g1);
945 /* Make sure its fields look right. */
946 tt_mem_op(n1->identity, OP_EQ, g1->identity, DIGEST_LEN);
947 tt_i64_op(g1->sampled_on_date, OP_GE, now - 12*86400);
948 tt_i64_op(g1->sampled_on_date, OP_LE, now);
949 tt_str_op(g1->sampled_by_version, OP_EQ, VERSION);
950 tt_uint_op(g1->currently_listed, OP_EQ, 1);
951 tt_i64_op(g1->confirmed_on_date, OP_EQ, 0);
952 tt_int_op(g1->confirmed_idx, OP_EQ, -1);
953 tt_int_op(g1->last_tried_to_connect, OP_EQ, 0);
954 tt_uint_op(g1->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
955 tt_i64_op(g1->failing_since, OP_EQ, 0);
956 tt_uint_op(g1->is_filtered_guard, OP_EQ, 1);
957 tt_uint_op(g1->is_usable_filtered_guard, OP_EQ, 1);
958 tt_uint_op(g1->is_primary, OP_EQ, 0);
959 tt_ptr_op(g1->extra_state_fields, OP_EQ, NULL);
961 /* Make sure it got added. */
962 tt_int_op(1, OP_EQ, smartlist_len(gs->sampled_entry_guards));
963 tt_ptr_op(g1, OP_EQ, smartlist_get(gs->sampled_entry_guards, 0));
964 tt_ptr_op(g1, OP_EQ, get_sampled_guard_with_id(gs, (uint8_t*)n1->identity));
965 const uint8_t bad_id[20] = {0};
966 tt_ptr_op(NULL, OP_EQ, get_sampled_guard_with_id(gs, bad_id));
968 done:
969 guard_selection_free(gs);
972 static void
973 test_entry_guard_node_filter(void *arg)
975 (void)arg;
976 guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
977 bridge_line_t *bl = NULL;
979 /* Initialize a bunch of node objects that are all guards. */
980 #define NUM 7
981 node_t *n[NUM];
982 entry_guard_t *g[NUM];
983 int i;
984 for (i=0; i < NUM; ++i) {
985 n[i] = smartlist_get(big_fake_net_nodes, i*2); // even ones are guards.
986 g[i] = entry_guard_add_to_sample(gs, n[i]);
988 // everything starts out filtered-in
989 tt_uint_op(g[i]->is_filtered_guard, OP_EQ, 1);
990 tt_uint_op(g[i]->is_usable_filtered_guard, OP_EQ, 1);
992 tt_int_op(num_reachable_filtered_guards(gs, NULL), OP_EQ, NUM);
994 /* Make sure refiltering doesn't hurt */
995 entry_guards_update_filtered_sets(gs);
996 for (i = 0; i < NUM; ++i) {
997 tt_uint_op(g[i]->is_filtered_guard, OP_EQ, 1);
998 tt_uint_op(g[i]->is_usable_filtered_guard, OP_EQ, 1);
1000 tt_int_op(num_reachable_filtered_guards(gs, NULL), OP_EQ, NUM);
1002 /* Now start doing things to make the guards get filtered out, 1 by 1. */
1004 /* 0: Not listed. */
1005 g[0]->currently_listed = 0;
1007 /* 1: path bias says this guard is maybe eeeevil. */
1008 g[1]->pb.path_bias_disabled = 1;
1010 /* 2: Unreachable address. */
1011 tor_addr_make_unspec(&n[2]->rs->ipv4_addr);
1013 /* 3: ExcludeNodes */
1014 tor_addr_from_ipv4h(&n[3]->rs->ipv4_addr, 0x90902020);
1015 routerset_free(get_options_mutable()->ExcludeNodes);
1016 get_options_mutable()->ExcludeNodes = routerset_new();
1017 routerset_parse(get_options_mutable()->ExcludeNodes, "144.144.0.0/16", "");
1019 /* 4: Bridge. */
1020 get_options_mutable()->UseBridges = 1;
1021 sweep_bridge_list();
1022 bl = tor_malloc_zero(sizeof(bridge_line_t));
1023 tor_addr_copy(&bl->addr, &n[4]->rs->ipv4_addr);
1024 bl->port = n[4]->rs->ipv4_orport;
1025 memcpy(bl->digest, n[4]->identity, 20);
1026 bridge_add_from_config(bl);
1027 bl = NULL; // prevent free.
1028 get_options_mutable()->UseBridges = 0;
1030 /* 5: Unreachable. This stays in the filter, but isn't in usable-filtered */
1031 g[5]->last_tried_to_connect = approx_time(); // prevent retry.
1032 g[5]->is_reachable = GUARD_REACHABLE_NO;
1034 /* 6: no change. */
1036 /* Now refilter and inspect. */
1037 entry_guards_update_filtered_sets(gs);
1038 for (i = 0; i < NUM; ++i) {
1039 tt_assert(g[i]->is_filtered_guard == (i == 5 || i == 6));
1040 tt_assert(g[i]->is_usable_filtered_guard == (i == 6));
1042 tt_int_op(num_reachable_filtered_guards(gs, NULL), OP_EQ, 1);
1044 /* Now make sure we have no live consensus, and no nodes. Nothing should
1045 * pass the filter any more. */
1046 tor_free(dummy_consensus);
1047 dummy_consensus = NULL;
1048 SMARTLIST_FOREACH(big_fake_net_nodes, node_t *, node, {
1049 memset(node->identity, 0xff, 20);
1051 entry_guards_update_filtered_sets(gs);
1052 for (i = 0; i < NUM; ++i) {
1053 tt_uint_op(g[i]->is_filtered_guard, OP_EQ, 0);
1054 tt_uint_op(g[i]->is_usable_filtered_guard, OP_EQ, 0);
1056 tt_int_op(num_reachable_filtered_guards(gs, NULL), OP_EQ, 0);
1058 done:
1059 guard_selection_free(gs);
1060 tor_free(bl);
1061 #undef NUM
1064 static void
1065 test_entry_guard_expand_sample(void *arg)
1067 (void)arg;
1068 guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
1069 digestmap_t *node_by_id = digestmap_new();
1071 entry_guard_t *guard = entry_guards_expand_sample(gs);
1072 tt_assert(guard); // the last guard returned.
1074 // Every sampled guard here should be filtered and reachable for now.
1075 tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ,
1076 num_reachable_filtered_guards(gs, NULL));
1078 /* Make sure we got the right number. */
1079 tt_int_op(DFLT_MIN_FILTERED_SAMPLE_SIZE, OP_EQ,
1080 num_reachable_filtered_guards(gs, NULL));
1082 // Make sure everything we got was from our fake node list, and everything
1083 // was unique.
1084 SMARTLIST_FOREACH_BEGIN(gs->sampled_entry_guards, entry_guard_t *, g) {
1085 const node_t *n = bfn_mock_node_get_by_id(g->identity);
1086 tt_assert(n);
1087 tt_ptr_op(NULL, OP_EQ, digestmap_get(node_by_id, g->identity));
1088 digestmap_set(node_by_id, g->identity, (void*) n);
1089 int idx = smartlist_pos(big_fake_net_nodes, n);
1090 // The even ones are the guards; make sure we got guards.
1091 tt_int_op(idx & 1, OP_EQ, 0);
1092 } SMARTLIST_FOREACH_END(g);
1094 // Nothing became unusable/unfiltered, so a subsequent expand should
1095 // make no changes.
1096 guard = entry_guards_expand_sample(gs);
1097 tt_ptr_op(guard, OP_EQ, NULL); // no guard was added.
1098 tt_int_op(DFLT_MIN_FILTERED_SAMPLE_SIZE, OP_EQ,
1099 num_reachable_filtered_guards(gs, NULL));
1101 // Make a few guards unreachable.
1102 guard = smartlist_get(gs->sampled_entry_guards, 0);
1103 guard->is_usable_filtered_guard = 0;
1104 guard = smartlist_get(gs->sampled_entry_guards, 1);
1105 guard->is_usable_filtered_guard = 0;
1106 guard = smartlist_get(gs->sampled_entry_guards, 2);
1107 guard->is_usable_filtered_guard = 0;
1108 tt_int_op(DFLT_MIN_FILTERED_SAMPLE_SIZE - 3, OP_EQ,
1109 num_reachable_filtered_guards(gs, NULL));
1111 // This time, expanding the sample will add some more guards.
1112 guard = entry_guards_expand_sample(gs);
1113 tt_assert(guard); // no guard was added.
1114 tt_int_op(DFLT_MIN_FILTERED_SAMPLE_SIZE, OP_EQ,
1115 num_reachable_filtered_guards(gs, NULL));
1116 tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ,
1117 num_reachable_filtered_guards(gs, NULL)+3);
1119 // Still idempotent.
1120 guard = entry_guards_expand_sample(gs);
1121 tt_ptr_op(guard, OP_EQ, NULL); // no guard was added.
1122 tt_int_op(DFLT_MIN_FILTERED_SAMPLE_SIZE, OP_EQ,
1123 num_reachable_filtered_guards(gs, NULL));
1125 // Now, do a nasty trick: tell the filter to exclude 31/32 of the guards.
1126 // This will cause the sample size to get reeeeally huge, while the
1127 // filtered sample size grows only slowly.
1128 routerset_free(get_options_mutable()->ExcludeNodes);
1129 get_options_mutable()->ExcludeNodes = routerset_new();
1130 routerset_parse(get_options_mutable()->ExcludeNodes, "144.144.0.0/16", "");
1131 SMARTLIST_FOREACH(big_fake_net_nodes, node_t *, n, {
1132 if (n_sl_idx % 64 != 0) {
1133 tor_addr_from_ipv4h(&n->rs->ipv4_addr, 0x90903030);
1136 entry_guards_update_filtered_sets(gs);
1138 // Surely (p ~ 1-2**-60), one of our guards has been excluded.
1139 tt_int_op(num_reachable_filtered_guards(gs, NULL), OP_LT,
1140 DFLT_MIN_FILTERED_SAMPLE_SIZE);
1142 // Try to regenerate the guards.
1143 guard = entry_guards_expand_sample(gs);
1144 tt_assert(guard); // no guard was added.
1146 /* this time, it's possible that we didn't add enough sampled guards. */
1147 tt_int_op(num_reachable_filtered_guards(gs, NULL), OP_LE,
1148 DFLT_MIN_FILTERED_SAMPLE_SIZE);
1149 /* but we definitely didn't exceed the sample maximum. */
1150 const int n_guards = 271 / 2;
1151 tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_LE,
1152 (int)(n_guards * .3));
1154 done:
1155 guard_selection_free(gs);
1156 digestmap_free(node_by_id, NULL);
1159 static void
1160 test_entry_guard_expand_sample_small_net(void *arg)
1162 (void)arg;
1163 guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
1165 /* Fun corner case: not enough guards to make up our whole sample size. */
1166 SMARTLIST_FOREACH(big_fake_net_nodes, node_t *, n, {
1167 if (n_sl_idx >= 15) {
1168 test_node_free(n);
1169 SMARTLIST_DEL_CURRENT(big_fake_net_nodes, n);
1170 } else {
1171 tor_addr_make_unspec(&n->rs->ipv4_addr); // make the filter reject this.
1175 entry_guard_t *guard = entry_guards_expand_sample(gs);
1176 tt_assert(guard); // the last guard returned -- some guard was added.
1177 // half the nodes are guards, so we have 8 guards left. The set
1178 // is small, so we sampled everything.
1179 tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, 8);
1180 tt_int_op(num_reachable_filtered_guards(gs, NULL), OP_EQ, 0);
1181 done:
1182 guard_selection_free(gs);
1185 static void
1186 test_entry_guard_update_from_consensus_status(void *arg)
1188 /* Here we're going to have some nodes become un-guardy, and say we got a
1189 * new consensus. This should cause those nodes to get detected as
1190 * unreachable. */
1192 (void)arg;
1193 int i;
1194 time_t start = approx_time();
1195 guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
1196 networkstatus_t *ns_tmp = NULL;
1198 /* Don't randomly backdate stuff; it will make correctness harder to check.*/
1199 MOCK(randomize_time, mock_randomize_time_no_randomization);
1201 /* First, sample some guards. */
1202 entry_guards_expand_sample(gs);
1203 int n_sampled_pre = smartlist_len(gs->sampled_entry_guards);
1204 int n_filtered_pre = num_reachable_filtered_guards(gs, NULL);
1205 tt_i64_op(n_sampled_pre, OP_EQ, n_filtered_pre);
1206 tt_i64_op(n_sampled_pre, OP_GT, 10);
1208 /* At this point, it should be a no-op to do this: */
1209 sampled_guards_update_from_consensus(gs);
1211 /* Now let's make some of our guards become unlisted. The easiest way to
1212 * do that would be to take away their guard flag. */
1213 for (i = 0; i < 5; ++i) {
1214 entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
1215 node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
1216 tt_assert(n);
1217 n->is_possible_guard = 0;
1220 update_approx_time(start + 30);
1222 /* try this with no live networkstatus. Nothing should happen! */
1223 ns_tmp = dummy_consensus;
1224 dummy_consensus = NULL;
1225 sampled_guards_update_from_consensus(gs);
1226 tt_i64_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_sampled_pre);
1227 tt_i64_op(num_reachable_filtered_guards(gs, NULL), OP_EQ, n_filtered_pre);
1228 /* put the networkstatus back. */
1229 dummy_consensus = ns_tmp;
1230 ns_tmp = NULL;
1233 /* Now those guards should become unlisted, and drop off the filter, but
1234 * stay in the sample. */
1235 update_approx_time(start + 60);
1236 sampled_guards_update_from_consensus(gs);
1238 tt_i64_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_sampled_pre);
1239 tt_i64_op(num_reachable_filtered_guards(gs, NULL), OP_EQ, n_filtered_pre-5);
1240 for (i = 0; i < 5; ++i) {
1241 entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
1242 tt_assert(! g->currently_listed);
1243 tt_i64_op(g->unlisted_since_date, OP_EQ, start+60);
1245 for (i = 5; i < n_sampled_pre; ++i) {
1246 entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
1247 tt_assert(g->currently_listed);
1248 tt_i64_op(g->unlisted_since_date, OP_EQ, 0);
1251 /* Now re-list one, and remove one completely. */
1253 entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 0);
1254 node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
1255 tt_assert(n);
1256 n->is_possible_guard = 1;
1259 /* try removing the node, to make sure we don't crash on an absent node
1261 entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 5);
1262 node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
1263 tt_assert(n);
1264 smartlist_remove(big_fake_net_nodes, n);
1265 test_node_free(n);
1267 update_approx_time(start + 300);
1268 sampled_guards_update_from_consensus(gs);
1270 /* guards 1..5 are now unlisted; 0,6,7.. are listed. */
1271 tt_i64_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_sampled_pre);
1272 for (i = 1; i < 6; ++i) {
1273 entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
1274 tt_assert(! g->currently_listed);
1275 if (i == 5)
1276 tt_i64_op(g->unlisted_since_date, OP_EQ, start+300);
1277 else
1278 tt_i64_op(g->unlisted_since_date, OP_EQ, start+60);
1280 for (i = 0; i < n_sampled_pre; i = (!i) ? 6 : i+1) { /* 0,6,7,8, ... */
1281 entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
1282 tt_assert(g->currently_listed);
1283 tt_i64_op(g->unlisted_since_date, OP_EQ, 0);
1286 done:
1287 tor_free(ns_tmp); /* in case we couldn't put it back */
1288 guard_selection_free(gs);
1289 UNMOCK(randomize_time);
1292 static void
1293 test_entry_guard_update_from_consensus_repair(void *arg)
1295 /* Here we'll make sure that our code to repair the unlisted-since
1296 * times is correct. */
1298 (void)arg;
1299 int i;
1300 time_t start = approx_time();
1301 guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
1303 /* Don't randomly backdate stuff; it will make correctness harder to check.*/
1304 MOCK(randomize_time, mock_randomize_time_no_randomization);
1306 /* First, sample some guards. */
1307 entry_guards_expand_sample(gs);
1308 int n_sampled_pre = smartlist_len(gs->sampled_entry_guards);
1309 int n_filtered_pre = num_reachable_filtered_guards(gs, NULL);
1310 tt_i64_op(n_sampled_pre, OP_EQ, n_filtered_pre);
1311 tt_i64_op(n_sampled_pre, OP_GT, 10);
1313 /* Now corrupt the list a bit. Call some unlisted-since-never, and some
1314 * listed-and-unlisted-since-a-time. */
1315 update_approx_time(start + 300);
1316 for (i = 0; i < 3; ++i) {
1317 /* these will get a date. */
1318 entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
1319 node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
1320 tt_assert(n);
1321 n->is_possible_guard = 0;
1322 g->currently_listed = 0;
1324 for (i = 3; i < 6; ++i) {
1325 /* these will become listed. */
1326 entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
1327 g->unlisted_since_date = start+100;
1329 setup_full_capture_of_logs(LOG_WARN);
1330 sampled_guards_update_from_consensus(gs);
1331 expect_log_msg_containing(
1332 "was listed, but with unlisted_since_date set");
1333 expect_log_msg_containing(
1334 "was unlisted, but with unlisted_since_date unset");
1335 teardown_capture_of_logs();
1337 tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_sampled_pre);
1338 tt_int_op(num_reachable_filtered_guards(gs, NULL), OP_EQ, n_filtered_pre-3);
1339 for (i = 3; i < n_sampled_pre; ++i) {
1340 /* these will become listed. */
1341 entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
1342 if (i < 3) {
1343 tt_assert(! g->currently_listed);
1344 tt_i64_op(g->unlisted_since_date, OP_EQ, start+300);
1345 } else {
1346 tt_assert(g->currently_listed);
1347 tt_i64_op(g->unlisted_since_date, OP_EQ, 0);
1351 done:
1352 teardown_capture_of_logs();
1353 guard_selection_free(gs);
1354 UNMOCK(randomize_time);
1357 static void
1358 test_entry_guard_update_from_consensus_remove(void *arg)
1360 /* Now let's check the logic responsible for removing guards from the
1361 * sample entirely. */
1363 (void)arg;
1364 //int i;
1365 guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
1366 smartlist_t *keep_ids = smartlist_new();
1367 smartlist_t *remove_ids = smartlist_new();
1369 /* Don't randomly backdate stuff; it will make correctness harder to check.*/
1370 MOCK(randomize_time, mock_randomize_time_no_randomization);
1372 /* First, sample some guards. */
1373 entry_guards_expand_sample(gs);
1374 int n_sampled_pre = smartlist_len(gs->sampled_entry_guards);
1375 int n_filtered_pre = num_reachable_filtered_guards(gs, NULL);
1376 tt_i64_op(n_sampled_pre, OP_EQ, n_filtered_pre);
1377 tt_i64_op(n_sampled_pre, OP_GT, 10);
1379 const time_t one_day_ago = approx_time() - 1*24*60*60;
1380 const time_t one_year_ago = approx_time() - 365*24*60*60;
1381 const time_t two_years_ago = approx_time() - 2*365*24*60*60;
1382 /* 0: unlisted for a day. (keep this) */
1384 entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 0);
1385 node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
1386 tt_assert(n);
1387 n->is_possible_guard = 0;
1388 g->currently_listed = 0;
1389 g->unlisted_since_date = one_day_ago;
1390 smartlist_add(keep_ids, tor_memdup(g->identity, 20));
1392 /* 1: unlisted for a year. (remove this) */
1394 entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 1);
1395 node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
1396 tt_assert(n);
1397 n->is_possible_guard = 0;
1398 g->currently_listed = 0;
1399 g->unlisted_since_date = one_year_ago;
1400 smartlist_add(remove_ids, tor_memdup(g->identity, 20));
1402 /* 2: added a day ago, never confirmed. (keep this) */
1404 entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 2);
1405 g->sampled_on_date = one_day_ago;
1406 smartlist_add(keep_ids, tor_memdup(g->identity, 20));
1408 /* 3: added a year ago, never confirmed. (remove this) */
1410 entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 3);
1411 g->sampled_on_date = one_year_ago;
1412 smartlist_add(remove_ids, tor_memdup(g->identity, 20));
1414 /* 4: added two year ago, confirmed yesterday, primary. (keep this.) */
1416 entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 4);
1417 g->sampled_on_date = one_year_ago;
1418 g->confirmed_on_date = one_day_ago;
1419 g->confirmed_idx = 0;
1420 g->is_primary = 1;
1421 smartlist_add(gs->confirmed_entry_guards, g);
1422 smartlist_add(gs->primary_entry_guards, g);
1423 smartlist_add(keep_ids, tor_memdup(g->identity, 20));
1425 /* 5: added two years ago, confirmed a year ago, primary. (remove this) */
1427 entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 5);
1428 g->sampled_on_date = two_years_ago;
1429 g->confirmed_on_date = one_year_ago;
1430 g->confirmed_idx = 1;
1431 g->is_primary = 1;
1432 smartlist_add(gs->confirmed_entry_guards, g);
1433 smartlist_add(gs->primary_entry_guards, g);
1434 smartlist_add(remove_ids, tor_memdup(g->identity, 20));
1437 sampled_guards_update_from_consensus(gs);
1439 /* Did we remove the right ones? */
1440 SMARTLIST_FOREACH(keep_ids, uint8_t *, id, {
1441 tt_assert(get_sampled_guard_with_id(gs, id) != NULL);
1443 SMARTLIST_FOREACH(remove_ids, uint8_t *, id, {
1444 tt_want(get_sampled_guard_with_id(gs, id) == NULL);
1447 /* Did we remove the right number? */
1448 tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_sampled_pre - 3);
1450 done:
1451 guard_selection_free(gs);
1452 UNMOCK(randomize_time);
1453 SMARTLIST_FOREACH(keep_ids, char *, cp, tor_free(cp));
1454 SMARTLIST_FOREACH(remove_ids, char *, cp, tor_free(cp));
1455 smartlist_free(keep_ids);
1456 smartlist_free(remove_ids);
1459 static void
1460 test_entry_guard_confirming_guards(void *arg)
1462 (void)arg;
1463 /* Now let's check the logic responsible for manipulating the list
1464 * of confirmed guards */
1465 guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
1466 MOCK(randomize_time, mock_randomize_time_no_randomization);
1468 /* Create the sample. */
1469 entry_guards_expand_sample(gs);
1471 /* Confirm a few guards. */
1472 time_t start = approx_time();
1473 entry_guard_t *g1 = smartlist_get(gs->sampled_entry_guards, 0);
1474 entry_guard_t *g2 = smartlist_get(gs->sampled_entry_guards, 1);
1475 entry_guard_t *g3 = smartlist_get(gs->sampled_entry_guards, 8);
1476 make_guard_confirmed(gs, g2);
1477 update_approx_time(start + 10);
1478 make_guard_confirmed(gs, g1);
1479 make_guard_confirmed(gs, g3);
1481 /* Were the correct dates and indices fed in? */
1482 tt_int_op(g1->confirmed_idx, OP_EQ, 1);
1483 tt_int_op(g2->confirmed_idx, OP_EQ, 0);
1484 tt_int_op(g3->confirmed_idx, OP_EQ, 2);
1485 tt_i64_op(g1->confirmed_on_date, OP_EQ, start+10);
1486 tt_i64_op(g2->confirmed_on_date, OP_EQ, start);
1487 tt_i64_op(g3->confirmed_on_date, OP_EQ, start+10);
1488 tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 0), OP_EQ, g1);
1489 tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 1), OP_EQ, g2);
1490 tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 2), OP_EQ, g3);
1492 /* Now make sure we can regenerate the confirmed_entry_guards list. */
1493 smartlist_clear(gs->confirmed_entry_guards);
1494 g2->confirmed_idx = 0;
1495 g1->confirmed_idx = 10;
1496 g3->confirmed_idx = 100;
1497 entry_guards_update_confirmed(gs);
1498 tt_int_op(g1->confirmed_idx, OP_EQ, 1);
1499 tt_int_op(g2->confirmed_idx, OP_EQ, 0);
1500 tt_int_op(g3->confirmed_idx, OP_EQ, 2);
1501 tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 0), OP_EQ, g1);
1502 tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 1), OP_EQ, g2);
1503 tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 2), OP_EQ, g3);
1505 /* Now make sure we can regenerate the confirmed_entry_guards list if
1506 * the indices are messed up. */
1507 g1->confirmed_idx = g2->confirmed_idx = g3->confirmed_idx = 999;
1508 smartlist_clear(gs->confirmed_entry_guards);
1509 entry_guards_update_confirmed(gs);
1510 tt_int_op(g1->confirmed_idx, OP_GE, 0);
1511 tt_int_op(g2->confirmed_idx, OP_GE, 0);
1512 tt_int_op(g3->confirmed_idx, OP_GE, 0);
1513 tt_int_op(g1->confirmed_idx, OP_LE, 2);
1514 tt_int_op(g2->confirmed_idx, OP_LE, 2);
1515 tt_int_op(g3->confirmed_idx, OP_LE, 2);
1516 g1 = smartlist_get(gs->confirmed_entry_guards, 0);
1517 g2 = smartlist_get(gs->confirmed_entry_guards, 1);
1518 g3 = smartlist_get(gs->confirmed_entry_guards, 2);
1519 tt_int_op(g1->sampled_idx, OP_EQ, 0);
1520 tt_int_op(g2->sampled_idx, OP_EQ, 1);
1521 tt_int_op(g3->sampled_idx, OP_EQ, 8);
1522 tt_assert(g1 != g2);
1523 tt_assert(g1 != g3);
1524 tt_assert(g2 != g3);
1526 done:
1527 UNMOCK(randomize_time);
1528 guard_selection_free(gs);
1531 static void
1532 test_entry_guard_sample_reachable_filtered(void *arg)
1534 (void)arg;
1535 guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
1536 entry_guards_expand_sample(gs);
1538 /* We've got a sampled list now; let's make one non-usable-filtered; some
1539 * confirmed, some primary, some pending.
1541 int n_guards = smartlist_len(gs->sampled_entry_guards);
1542 tt_int_op(n_guards, OP_GT, 10);
1543 entry_guard_t *g;
1544 g = smartlist_get(gs->sampled_entry_guards, 0);
1545 g->is_pending = 1;
1546 g = smartlist_get(gs->sampled_entry_guards, 1);
1547 make_guard_confirmed(gs, g);
1548 g = smartlist_get(gs->sampled_entry_guards, 2);
1549 g->is_primary = 1;
1550 g = smartlist_get(gs->sampled_entry_guards, 3);
1551 g->pb.path_bias_disabled = 1;
1553 entry_guards_update_filtered_sets(gs);
1554 gs->primary_guards_up_to_date = 1;
1555 tt_int_op(num_reachable_filtered_guards(gs, NULL), OP_EQ, n_guards - 1);
1556 tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_guards);
1558 // +1 since the one we made disabled will make another one get added.
1559 ++n_guards;
1561 /* Try a bunch of selections. */
1562 const struct {
1563 int flag; int idx;
1564 } tests[] = {
1565 { 0, -1 },
1566 { SAMPLE_EXCLUDE_CONFIRMED, 1 },
1567 { SAMPLE_EXCLUDE_PRIMARY|SAMPLE_NO_UPDATE_PRIMARY, 2 },
1568 { SAMPLE_EXCLUDE_PENDING, 0 },
1569 { -1, -1},
1571 int j;
1572 for (j = 0; tests[j].flag >= 0; ++j) {
1573 const int excluded_flags = tests[j].flag;
1574 const int excluded_idx = tests[j].idx;
1575 g = first_reachable_filtered_entry_guard(gs, NULL, excluded_flags);
1576 tor_assert(g);
1577 int pos = smartlist_pos(gs->sampled_entry_guards, g);
1578 tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_guards);
1579 const int should_be_set = (pos != excluded_idx &&
1580 pos != 3); // filtered out.
1581 tt_int_op(1, OP_EQ, should_be_set);
1584 done:
1585 guard_selection_free(gs);
1588 static void
1589 test_entry_guard_sample_reachable_filtered_empty(void *arg)
1591 (void)arg;
1592 guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
1593 /* What if we try to sample from a set of 0? */
1594 SMARTLIST_FOREACH(big_fake_net_nodes, node_t *, n,
1595 n->is_possible_guard = 0);
1597 entry_guard_t *g = first_reachable_filtered_entry_guard(gs, NULL, 0);
1598 tt_ptr_op(g, OP_EQ, NULL);
1600 done:
1601 guard_selection_free(gs);
1604 static void
1605 test_entry_guard_retry_unreachable(void *arg)
1607 (void)arg;
1608 guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
1610 entry_guards_expand_sample(gs);
1611 /* Let's say that we have two guards, and they're down.
1613 time_t start = approx_time();
1614 entry_guard_t *g1 = smartlist_get(gs->sampled_entry_guards, 0);
1615 entry_guard_t *g2 = smartlist_get(gs->sampled_entry_guards, 1);
1616 entry_guard_t *g3 = smartlist_get(gs->sampled_entry_guards, 2);
1617 g1->is_reachable = GUARD_REACHABLE_NO;
1618 g2->is_reachable = GUARD_REACHABLE_NO;
1619 g1->is_primary = 1;
1620 g1->failing_since = g2->failing_since = start;
1621 g1->last_tried_to_connect = g2->last_tried_to_connect = start;
1623 /* Wait 5 minutes. Nothing will get retried. */
1624 update_approx_time(start + 5 * 60);
1625 entry_guard_consider_retry(g1);
1626 entry_guard_consider_retry(g2);
1627 entry_guard_consider_retry(g3); // just to make sure this doesn't crash.
1628 tt_int_op(g1->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
1629 tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
1630 tt_int_op(g3->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
1632 /* After 30 min, the primary one gets retried */
1633 update_approx_time(start + 35 * 60);
1634 entry_guard_consider_retry(g1);
1635 entry_guard_consider_retry(g2);
1636 tt_int_op(g1->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
1637 tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
1639 g1->is_reachable = GUARD_REACHABLE_NO;
1640 g1->last_tried_to_connect = start + 55*60;
1642 /* After 1 hour, we'll retry the nonprimary one. */
1643 update_approx_time(start + 61 * 60);
1644 entry_guard_consider_retry(g1);
1645 entry_guard_consider_retry(g2);
1646 tt_int_op(g1->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
1647 tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
1649 g2->is_reachable = GUARD_REACHABLE_NO;
1650 g2->last_tried_to_connect = start + 61*60;
1652 /* And then the primary one again. */
1653 update_approx_time(start + 66 * 60);
1654 entry_guard_consider_retry(g1);
1655 entry_guard_consider_retry(g2);
1656 tt_int_op(g1->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
1657 tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
1659 done:
1660 guard_selection_free(gs);
1663 static void
1664 test_entry_guard_manage_primary(void *arg)
1666 (void)arg;
1667 guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
1668 smartlist_t *prev_guards = smartlist_new();
1670 /* If no guards are confirmed, we should pick a few reachable guards and
1671 * call them all primary. But not confirmed.*/
1672 entry_guards_update_primary(gs);
1673 int n_primary = smartlist_len(gs->primary_entry_guards);
1674 tt_int_op(n_primary, OP_GE, 1);
1675 SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, g, {
1676 tt_assert(g->is_primary);
1677 tt_assert(g->confirmed_idx == -1);
1680 /* Calling it a second time should leave the guards unchanged. */
1681 smartlist_add_all(prev_guards, gs->primary_entry_guards);
1682 entry_guards_update_primary(gs);
1683 tt_int_op(smartlist_len(gs->primary_entry_guards), OP_EQ, n_primary);
1684 SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, g, {
1685 tt_ptr_op(g, OP_EQ, smartlist_get(prev_guards, g_sl_idx));
1689 * If we have one confirmed guard, that guards becomes the first primary
1690 * only if its sampled_idx is smaller
1691 * */
1693 /* find a non-primary guard... it should have a sampled_idx higher than
1694 * existing primary guards */
1695 entry_guard_t *confirmed = NULL;
1696 SMARTLIST_FOREACH(gs->sampled_entry_guards, entry_guard_t *, g, {
1697 if (! g->is_primary) {
1698 confirmed = g;
1699 break;
1702 tt_assert(confirmed);
1703 /* make it confirmed. */
1704 make_guard_confirmed(gs, confirmed);
1705 /* update the list... */
1706 smartlist_clear(prev_guards);
1707 smartlist_add_all(prev_guards, gs->primary_entry_guards);
1708 entry_guards_update_primary(gs);
1710 /* the confirmed guard should be at the end of the primary list! Hopefully,
1711 * one of the primary guards with a lower sampled_idx will confirm soon :)
1712 * Doing this won't make the client switches between primaries depending on
1713 * the order of confirming events */
1714 tt_int_op(smartlist_len(gs->primary_entry_guards), OP_EQ, n_primary);
1715 tt_ptr_op(smartlist_get(gs->primary_entry_guards,
1716 smartlist_len(gs->primary_entry_guards)-1), OP_EQ, confirmed);
1718 entry_guard_t *prev_last_guard = smartlist_get(prev_guards, n_primary-1);
1719 tt_assert(! prev_last_guard->is_primary);
1722 /* Calling it a fourth time should leave the guards unchanged. */
1723 smartlist_clear(prev_guards);
1724 smartlist_add_all(prev_guards, gs->primary_entry_guards);
1725 entry_guards_update_primary(gs);
1726 tt_int_op(smartlist_len(gs->primary_entry_guards), OP_EQ, n_primary);
1727 SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, g, {
1728 tt_ptr_op(g, OP_EQ, smartlist_get(prev_guards, g_sl_idx));
1731 /* Do some dirinfo checks */
1733 /* Check that we have all required dirinfo for the primaries (that's done
1734 * in big_fake_network_setup()) */
1735 char *dir_info_str =
1736 guard_selection_get_err_str_if_dir_info_missing(gs, 0, 0, 0);
1737 tt_assert(!dir_info_str);
1739 /* Now artificially remove the first primary's descriptor and re-check */
1740 entry_guard_t *first_primary;
1741 first_primary = smartlist_get(gs->primary_entry_guards, 0);
1742 /* Change the first primary's identity digest so that the mocked functions
1743 * can't find its descriptor */
1744 memset(first_primary->identity, 9, sizeof(first_primary->identity));
1745 dir_info_str =guard_selection_get_err_str_if_dir_info_missing(gs, 1, 2, 3);
1746 tt_str_op(dir_info_str, OP_EQ,
1747 "We're missing descriptors for 1/2 of our primary entry guards "
1748 "(total microdescriptors: 2/3). That's ok. We will try to fetch "
1749 "missing descriptors soon.");
1750 tor_free(dir_info_str);
1753 done:
1754 guard_selection_free(gs);
1755 smartlist_free(prev_guards);
1758 static void
1759 test_entry_guard_guard_preferred(void *arg)
1761 (void) arg;
1762 entry_guard_t *g1 = tor_malloc_zero(sizeof(entry_guard_t));
1763 entry_guard_t *g2 = tor_malloc_zero(sizeof(entry_guard_t));
1765 g1->confirmed_idx = g2->confirmed_idx = -1;
1766 g1->last_tried_to_connect = approx_time();
1767 g2->last_tried_to_connect = approx_time();
1769 tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g1, g1));
1771 /* Neither is pending; priorities equal. */
1772 tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g2, g1));
1773 tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g1, g2));
1775 /* If one is pending, the pending one has higher priority */
1776 g1->is_pending = 1;
1777 tt_int_op(1, OP_EQ, entry_guard_has_higher_priority(g1, g2));
1778 tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g2, g1));
1780 /* If both are pending, and last_tried_to_connect is equal:
1781 priorities equal */
1782 g2->is_pending = 1;
1783 tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g2, g1));
1784 tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g1, g2));
1786 /* One had a connection that startied earlier: it has higher priority. */
1787 g2->last_tried_to_connect -= 10;
1788 tt_int_op(1, OP_EQ, entry_guard_has_higher_priority(g2, g1));
1789 tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g1, g2));
1791 /* Now, say that g1 is confirmed. It will get higher priority. */
1792 g1->confirmed_idx = 5;
1793 tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g2, g1));
1794 tt_int_op(1, OP_EQ, entry_guard_has_higher_priority(g1, g2));
1796 /* But if g2 was confirmed first, it will get priority */
1797 g2->confirmed_idx = 2;
1798 tt_int_op(1, OP_EQ, entry_guard_has_higher_priority(g2, g1));
1799 tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g1, g2));
1801 done:
1802 tor_free(g1);
1803 tor_free(g2);
1806 static void
1807 test_entry_guard_correct_cascading_order(void *arg)
1809 (void)arg;
1810 smartlist_t *old_primary_guards = smartlist_new();
1811 guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
1812 entry_guards_expand_sample(gs);
1813 /** First, a test in which the primary guards need be pulled from different
1814 * lists to fill up the primary list -- this may happen, if for example, not
1815 * enough guards have confirmed yet */
1816 entry_guard_t *g;
1817 /** just one confirmed */
1818 g = smartlist_get(gs->sampled_entry_guards, 2);
1819 make_guard_confirmed(gs, g);
1820 entry_guards_update_primary(gs);
1821 g = smartlist_get(gs->primary_entry_guards, 0);
1822 tt_int_op(g->sampled_idx, OP_EQ, 0);
1823 g = smartlist_get(gs->primary_entry_guards, 1);
1824 tt_int_op(g->sampled_idx, OP_EQ, 1);
1825 g = smartlist_get(gs->primary_entry_guards, 2);
1826 tt_int_op(g->sampled_idx, OP_EQ, 2);
1828 /** Now the primaries get all confirmed, and the primary list should not
1829 * change */
1830 make_guard_confirmed(gs, smartlist_get(gs->primary_entry_guards, 0));
1831 make_guard_confirmed(gs, smartlist_get(gs->primary_entry_guards, 1));
1832 smartlist_add_all(old_primary_guards, gs->primary_entry_guards);
1833 entry_guards_update_primary(gs);
1834 smartlist_ptrs_eq(gs->primary_entry_guards, old_primary_guards);
1835 /** the confirmed guards should also have the same set of guards, in the same
1836 * order :-) */
1837 smartlist_ptrs_eq(gs->confirmed_entry_guards, gs->primary_entry_guards);
1838 /** Now select a guard for a circuit, and make sure it is the first primary
1839 * guard */
1840 unsigned state = 9999;
1841 g = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL, &state);
1842 tt_ptr_op(g, OP_EQ, smartlist_get(gs->primary_entry_guards, 0));
1843 /** Now, let's mark this guard as unreachable and let's update the lists */
1844 g->is_reachable = GUARD_REACHABLE_NO;
1845 g->failing_since = approx_time() - 10;
1846 g->last_tried_to_connect = approx_time() - 10;
1847 state = 9999;
1848 entry_guards_update_primary(gs);
1849 g = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL, &state);
1850 /** we should have switched to the next one is sampled order */
1851 tt_int_op(g->sampled_idx, OP_EQ, 1);
1852 done:
1853 smartlist_free(old_primary_guards);
1854 guard_selection_free(gs);
1857 static void
1858 test_entry_guard_select_for_circuit_no_confirmed(void *arg)
1860 /* Simpler cases: no gaurds are confirmed yet. */
1861 (void)arg;
1862 guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
1863 entry_guard_restriction_t *rst = NULL;
1865 /* simple starting configuration */
1866 entry_guards_update_primary(gs);
1867 unsigned state = 9999;
1869 entry_guard_t *g = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC,
1870 NULL, &state);
1872 tt_assert(g);
1873 tt_assert(g->is_primary);
1874 tt_int_op(g->confirmed_idx, OP_EQ, -1);
1875 tt_uint_op(g->is_pending, OP_EQ, 0); // primary implies non-pending.
1876 tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
1877 tt_i64_op(g->last_tried_to_connect, OP_EQ, approx_time());
1879 // If we do that again, we should get the same guard.
1880 entry_guard_t *g2 = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC,
1881 NULL, &state);
1882 tt_ptr_op(g2, OP_EQ, g);
1884 // if we mark that guard down, we should get a different primary guard.
1885 // auto-retry it.
1886 g->is_reachable = GUARD_REACHABLE_NO;
1887 g->failing_since = approx_time() - 10;
1888 g->last_tried_to_connect = approx_time() - 10;
1889 state = 9999;
1890 g2 = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL, &state);
1891 tt_ptr_op(g2, OP_NE, g);
1892 tt_assert(g2);
1893 tt_assert(g2->is_primary);
1894 tt_int_op(g2->confirmed_idx, OP_EQ, -1);
1895 tt_uint_op(g2->is_pending, OP_EQ, 0); // primary implies non-pending.
1896 tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
1897 tt_i64_op(g2->last_tried_to_connect, OP_EQ, approx_time());
1899 // If we say that the first primary guard was last tried a long time ago, we
1900 // should get an automatic retry on it.
1901 g->failing_since = approx_time() - 72*60*60;
1902 g->last_tried_to_connect = approx_time() - 72*60*60;
1903 state = 9999;
1904 g2 = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL, &state);
1905 tt_ptr_op(g2, OP_EQ, g);
1906 tt_assert(g2);
1907 tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
1908 tt_i64_op(g2->last_tried_to_connect, OP_EQ, approx_time());
1909 tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
1911 // And if we mark ALL the primary guards down, we should get another guard
1912 // at random.
1913 SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, guard, {
1914 guard->is_reachable = GUARD_REACHABLE_NO;
1915 guard->last_tried_to_connect = approx_time() - 5;
1916 guard->failing_since = approx_time() - 30;
1918 state = 9999;
1919 g2 = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL, &state);
1920 tt_assert(g2);
1921 tt_assert(!g2->is_primary);
1922 tt_int_op(g2->confirmed_idx, OP_EQ, -1);
1923 tt_uint_op(g2->is_pending, OP_EQ, 1);
1924 tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
1925 tt_i64_op(g2->last_tried_to_connect, OP_EQ, approx_time());
1926 tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
1928 // As a bonus, maybe we should be retrying the primary guards. Let's say so.
1929 mark_primary_guards_maybe_reachable(gs);
1930 SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, guard, {
1931 tt_int_op(guard->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
1932 tt_assert(guard->is_usable_filtered_guard == 1);
1933 // no change to these fields.
1934 tt_i64_op(guard->last_tried_to_connect, OP_EQ, approx_time() - 5);
1935 tt_i64_op(guard->failing_since, OP_EQ, approx_time() - 30);
1938 /* Let's try again and we should get the first primary guard again */
1939 g = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL, &state);
1940 tt_ptr_op(g, OP_EQ, smartlist_get(gs->primary_entry_guards, 0));
1941 g2 = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL, &state);
1942 tt_ptr_op(g2, OP_EQ, g);
1944 /* But if we impose a restriction, we don't get the same guard */
1945 get_options_mutable()->EnforceDistinctSubnets = 0;
1946 rst = guard_create_exit_restriction((uint8_t*)g->identity);
1947 g2 = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, rst, &state);
1948 tt_assert(g2);
1949 tt_ptr_op(g2, OP_NE, g);
1951 done:
1952 guard_selection_free(gs);
1953 entry_guard_restriction_free(rst);
1956 static void
1957 test_entry_guard_select_for_circuit_confirmed(void *arg)
1959 /* Case 2: if all the primary guards are down, and there are more confirmed
1960 guards, we use a confirmed guard. */
1961 (void)arg;
1962 int i;
1963 entry_guard_restriction_t *rst = NULL;
1964 guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
1965 const int N_CONFIRMED = 10;
1967 /* slightly more complicated simple starting configuration */
1968 entry_guards_update_primary(gs);
1969 for (i = 0; i < N_CONFIRMED; ++i) {
1970 entry_guard_t *guard = smartlist_get(gs->sampled_entry_guards, i);
1971 make_guard_confirmed(gs, guard);
1973 entry_guards_update_primary(gs); // rebuild the primary list.
1975 unsigned state = 9999;
1977 // As above, this gives us a primary guard.
1978 entry_guard_t *g = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC,
1979 NULL, &state);
1980 tt_assert(g);
1981 tt_assert(g->is_primary);
1982 tt_int_op(g->confirmed_idx, OP_EQ, 0);
1983 tt_uint_op(g->is_pending, OP_EQ, 0); // primary implies non-pending.
1984 tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
1985 tt_i64_op(g->last_tried_to_connect, OP_EQ, approx_time());
1986 tt_ptr_op(g, OP_EQ, smartlist_get(gs->primary_entry_guards, 0));
1988 // But if we mark all the primary guards down...
1989 SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, guard, {
1990 guard->last_tried_to_connect = approx_time();
1991 entry_guards_note_guard_failure(gs, guard);
1994 // ... we should get a confirmed guard.
1995 state = 9999;
1996 g = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL, &state);
1997 tt_assert(g);
1998 tt_assert(! g->is_primary);
1999 tt_int_op(g->confirmed_idx, OP_EQ, smartlist_len(gs->primary_entry_guards));
2000 tt_assert(g->is_pending);
2001 tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
2002 tt_i64_op(g->last_tried_to_connect, OP_EQ, approx_time());
2004 // And if we try again, we should get a different confirmed guard, since
2005 // that one is pending.
2006 state = 9999;
2007 entry_guard_t *g2 = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC,
2008 NULL, &state);
2009 tt_assert(g2);
2010 tt_assert(! g2->is_primary);
2011 tt_ptr_op(g2, OP_NE, g);
2012 tt_int_op(g2->confirmed_idx, OP_EQ,
2013 smartlist_len(gs->primary_entry_guards)+1);
2014 tt_assert(g2->is_pending);
2015 tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
2016 tt_i64_op(g2->last_tried_to_connect, OP_EQ, approx_time());
2018 // If we say that the next confirmed guard in order is excluded, and
2019 // we disable EnforceDistinctSubnets, we get the guard AFTER the
2020 // one we excluded.
2021 get_options_mutable()->EnforceDistinctSubnets = 0;
2022 g = smartlist_get(gs->confirmed_entry_guards,
2023 smartlist_len(gs->primary_entry_guards)+2);
2024 rst = guard_create_exit_restriction((uint8_t*)g->identity);
2025 g2 = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, rst, &state);
2026 tt_ptr_op(g2, OP_NE, NULL);
2027 tt_ptr_op(g2, OP_NE, g);
2028 tt_int_op(g2->confirmed_idx, OP_EQ,
2029 smartlist_len(gs->primary_entry_guards)+3);
2031 // If we make every confirmed guard become pending then we start poking
2032 // other guards.
2033 const int n_remaining_confirmed =
2034 N_CONFIRMED - 3 - smartlist_len(gs->primary_entry_guards);
2035 for (i = 0; i < n_remaining_confirmed; ++i) {
2036 g = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL, &state);
2037 tt_int_op(g->confirmed_idx, OP_GE, 0);
2038 tt_assert(g);
2040 state = 9999;
2041 g = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL, &state);
2042 tt_assert(g);
2043 tt_assert(g->is_pending);
2044 tt_int_op(g->confirmed_idx, OP_EQ, -1);
2046 // If we EnforceDistinctSubnets and apply a restriction, we get
2047 // nothing, since we put all of the nodes in the same /16.
2048 // Regression test for bug 22753/TROVE-2017-006.
2049 get_options_mutable()->EnforceDistinctSubnets = 1;
2050 g = smartlist_get(gs->confirmed_entry_guards, 0);
2051 memcpy(rst->exclude_id, g->identity, DIGEST_LEN);
2052 g2 = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, rst, &state);
2053 tt_ptr_op(g2, OP_EQ, NULL);
2055 done:
2056 guard_selection_free(gs);
2057 entry_guard_restriction_free(rst);
2060 static void
2061 test_entry_guard_select_for_circuit_highlevel_primary(void *arg)
2063 /* Play around with selecting primary guards for circuits and markign
2064 * them up and down */
2065 (void)arg;
2066 guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
2068 time_t start = approx_time();
2070 const node_t *node = NULL;
2071 circuit_guard_state_t *guard = NULL;
2072 entry_guard_t *g;
2073 guard_usable_t u;
2075 * Make sure that the pick-for-circuit API basically works. We'll get
2076 * a primary guard, so it'll be usable on completion.
2078 int r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
2079 &node, &guard);
2081 tt_int_op(r, OP_EQ, 0);
2082 tt_assert(node);
2083 tt_assert(guard);
2084 tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
2085 g = entry_guard_handle_get(guard->guard);
2086 tt_assert(g);
2087 tt_mem_op(g->identity, OP_EQ, node->identity, DIGEST_LEN);
2088 tt_int_op(g->is_primary, OP_EQ, 1);
2089 tt_i64_op(g->last_tried_to_connect, OP_EQ, start);
2090 tt_int_op(g->confirmed_idx, OP_EQ, -1);
2092 /* Call that circuit successful. */
2093 update_approx_time(start+15);
2094 u = entry_guard_succeeded(&guard);
2095 tt_int_op(u, OP_EQ, GUARD_USABLE_NOW); /* We can use it now. */
2096 tt_assert(guard);
2097 tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_COMPLETE);
2098 g = entry_guard_handle_get(guard->guard);
2099 tt_assert(g);
2100 tt_int_op(g->is_reachable, OP_EQ, GUARD_REACHABLE_YES);
2101 tt_int_op(g->confirmed_idx, OP_EQ, 0);
2103 circuit_guard_state_free(guard);
2104 guard = NULL;
2105 node = NULL;
2106 g = NULL;
2108 /* Try again. We'll also get a primary guard this time. (The same one,
2109 in fact.) But this time, we'll say the connection has failed. */
2110 update_approx_time(start+35);
2111 r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
2112 &node, &guard);
2113 tt_int_op(r, OP_EQ, 0);
2114 tt_assert(node);
2115 tt_assert(guard);
2116 tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
2117 tt_i64_op(guard->state_set_at, OP_EQ, start+35);
2118 g = entry_guard_handle_get(guard->guard);
2119 tt_assert(g);
2120 tt_mem_op(g->identity, OP_EQ, node->identity, DIGEST_LEN);
2121 tt_int_op(g->is_primary, OP_EQ, 1);
2122 tt_i64_op(g->last_tried_to_connect, OP_EQ, start+35);
2123 tt_int_op(g->confirmed_idx, OP_EQ, 0); // same one.
2125 /* It's failed! What will happen to our poor guard? */
2126 update_approx_time(start+45);
2127 entry_guard_failed(&guard);
2128 tt_assert(guard);
2129 tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_DEAD);
2130 tt_i64_op(guard->state_set_at, OP_EQ, start+45);
2131 g = entry_guard_handle_get(guard->guard);
2132 tt_assert(g);
2133 tt_int_op(g->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
2134 tt_i64_op(g->failing_since, OP_EQ, start+45);
2135 tt_int_op(g->confirmed_idx, OP_EQ, 0); // still confirmed.
2137 circuit_guard_state_free(guard);
2138 guard = NULL;
2139 node = NULL;
2140 entry_guard_t *g_prev = g;
2141 g = NULL;
2143 /* Now try a third time. Since the other one is down, we'll get a different
2144 * (still primary) guard.
2146 update_approx_time(start+60);
2147 r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
2148 &node, &guard);
2149 tt_int_op(r, OP_EQ, 0);
2150 tt_assert(node);
2151 tt_assert(guard);
2152 tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
2153 g = entry_guard_handle_get(guard->guard);
2154 tt_assert(g);
2155 tt_ptr_op(g, OP_NE, g_prev);
2156 tt_mem_op(g->identity, OP_EQ, node->identity, DIGEST_LEN);
2157 tt_mem_op(g->identity, OP_NE, g_prev->identity, DIGEST_LEN);
2158 tt_int_op(g->is_primary, OP_EQ, 1);
2159 tt_i64_op(g->last_tried_to_connect, OP_EQ, start+60);
2160 tt_int_op(g->confirmed_idx, OP_EQ, -1); // not confirmed now.
2162 /* Call this one up; watch it get confirmed. */
2163 update_approx_time(start+90);
2164 u = entry_guard_succeeded(&guard);
2165 tt_int_op(u, OP_EQ, GUARD_USABLE_NOW);
2166 tt_assert(guard);
2167 tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_COMPLETE);
2168 g = entry_guard_handle_get(guard->guard);
2169 tt_assert(g);
2170 tt_int_op(g->is_reachable, OP_EQ, GUARD_REACHABLE_YES);
2171 tt_int_op(g->confirmed_idx, OP_EQ, 1);
2173 done:
2174 guard_selection_free(gs);
2175 circuit_guard_state_free(guard);
2178 static void
2179 test_entry_guard_select_for_circuit_highlevel_confirm_other(void *arg)
2181 (void) arg;
2182 const int N_PRIMARY = DFLT_N_PRIMARY_GUARDS;
2184 /* At the start, we have no confirmed guards. We'll mark the primary guards
2185 * down, then confirm something else. As soon as we do, it should become
2186 * primary, and we should get it next time. */
2188 time_t start = approx_time();
2189 guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
2190 circuit_guard_state_t *guard = NULL;
2191 int i, r;
2192 const node_t *node = NULL;
2193 guard_usable_t u;
2195 /* Declare that we're on the internet. */
2196 entry_guards_note_internet_connectivity(gs);
2198 /* Primary guards are down! */
2199 for (i = 0; i < N_PRIMARY; ++i) {
2200 r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
2201 &node, &guard);
2202 tt_assert(node);
2203 tt_assert(guard);
2204 tt_int_op(r, OP_EQ, 0);
2205 tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
2206 entry_guard_failed(&guard);
2207 circuit_guard_state_free(guard);
2208 guard = NULL;
2209 node = NULL;
2212 /* Next guard should be non-primary. */
2213 node = NULL;
2214 r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
2215 &node, &guard);
2216 tt_assert(node);
2217 tt_assert(guard);
2218 tt_int_op(r, OP_EQ, 0);
2219 entry_guard_t *g = entry_guard_handle_get(guard->guard);
2220 tt_assert(g);
2221 tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
2222 tt_int_op(g->confirmed_idx, OP_EQ, -1);
2223 tt_int_op(g->is_primary, OP_EQ, 0);
2224 tt_int_op(g->is_pending, OP_EQ, 1);
2225 (void)start;
2227 u = entry_guard_succeeded(&guard);
2228 /* We're on the internet (by fiat), so this guard will get called "confirmed"
2229 * and should immediately become primary.
2231 tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_COMPLETE);
2232 tt_assert(u == GUARD_USABLE_NOW);
2233 tt_int_op(g->confirmed_idx, OP_EQ, 0);
2234 tt_int_op(g->is_primary, OP_EQ, 1);
2235 tt_int_op(g->is_pending, OP_EQ, 0);
2237 done:
2238 guard_selection_free(gs);
2239 circuit_guard_state_free(guard);
2242 static void
2243 test_entry_guard_select_for_circuit_highlevel_primary_retry(void *arg)
2245 (void) arg;
2246 const int N_PRIMARY = DFLT_N_PRIMARY_GUARDS;
2248 /* At the start, we have no confirmed guards. We'll mark the primary guards
2249 * down, then confirm something else. As soon as we do, it should become
2250 * primary, and we should get it next time. */
2252 time_t start = approx_time();
2253 guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
2254 circuit_guard_state_t *guard = NULL, *guard2 = NULL;
2255 int i, r;
2256 const node_t *node = NULL;
2257 entry_guard_t *g;
2258 guard_usable_t u;
2260 /* Declare that we're on the internet. */
2261 entry_guards_note_internet_connectivity(gs);
2263 /* Make primary guards confirmed (so they won't be superseded by a later
2264 * guard), then mark them down. */
2265 for (i = 0; i < N_PRIMARY; ++i) {
2266 r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
2267 &node, &guard);
2268 tt_assert(node);
2269 tt_assert(guard);
2270 tt_int_op(r, OP_EQ, 0);
2271 tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
2272 g = entry_guard_handle_get(guard->guard);
2273 make_guard_confirmed(gs, g);
2274 tt_int_op(g->is_primary, OP_EQ, 1);
2275 entry_guard_failed(&guard);
2276 circuit_guard_state_free(guard);
2277 tt_int_op(g->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
2278 guard = NULL;
2279 node = NULL;
2282 /* Get another guard that we might try. */
2283 r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
2284 &node, &guard);
2285 tt_assert(node);
2286 tt_assert(guard);
2287 tt_int_op(r, OP_EQ, 0);
2288 tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
2289 g = entry_guard_handle_get(guard->guard);
2290 tt_int_op(g->is_primary, OP_EQ, 0);
2292 tt_assert(entry_guards_all_primary_guards_are_down(gs));
2294 /* And an hour has passed ... */
2295 update_approx_time(start + 3600);
2297 /* Say that guard has succeeded! */
2298 u = entry_guard_succeeded(&guard);
2299 tt_int_op(u, OP_EQ, GUARD_MAYBE_USABLE_LATER);
2300 tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD);
2301 g = entry_guard_handle_get(guard->guard);
2303 /* The primary guards should have been marked up! */
2304 SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, pg, {
2305 tt_int_op(pg->is_primary, OP_EQ, 1);
2306 tt_ptr_op(g, OP_NE, pg);
2307 tt_int_op(pg->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
2310 /* Have a circuit to a primary guard succeed. */
2311 r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
2312 &node, &guard2);
2313 tt_int_op(r, OP_EQ, 0);
2314 tt_int_op(guard2->state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
2315 u = entry_guard_succeeded(&guard2);
2316 tt_assert(u == GUARD_USABLE_NOW);
2317 tt_int_op(guard2->state, OP_EQ, GUARD_CIRC_STATE_COMPLETE);
2319 tt_assert(! entry_guards_all_primary_guards_are_down(gs));
2321 done:
2322 guard_selection_free(gs);
2323 circuit_guard_state_free(guard);
2324 circuit_guard_state_free(guard2);
2327 static void
2328 test_entry_guard_select_and_cancel(void *arg)
2330 (void) arg;
2331 const int N_PRIMARY = DFLT_N_PRIMARY_GUARDS;
2332 int i,r;
2333 const node_t *node = NULL;
2334 circuit_guard_state_t *guard;
2335 guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
2336 entry_guard_t *g;
2338 /* Once more, we mark all the primary guards down. */
2339 entry_guards_note_internet_connectivity(gs);
2340 for (i = 0; i < N_PRIMARY; ++i) {
2341 r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
2342 &node, &guard);
2343 tt_int_op(r, OP_EQ, 0);
2344 tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
2345 g = entry_guard_handle_get(guard->guard);
2346 tt_int_op(g->is_primary, OP_EQ, 1);
2347 tt_int_op(g->is_pending, OP_EQ, 0);
2348 make_guard_confirmed(gs, g);
2349 entry_guard_failed(&guard);
2350 circuit_guard_state_free(guard);
2351 guard = NULL;
2352 node = NULL;
2355 tt_assert(entry_guards_all_primary_guards_are_down(gs));
2357 /* Now get another guard we could try... */
2358 r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
2359 &node, &guard);
2360 tt_assert(node);
2361 tt_assert(guard);
2362 tt_int_op(r, OP_EQ, 0);
2363 tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
2364 g = entry_guard_handle_get(guard->guard);
2365 tt_int_op(g->is_primary, OP_EQ, 0);
2366 tt_int_op(g->is_pending, OP_EQ, 1);
2368 /* Whoops! We should never have asked for this guard. Cancel the request! */
2369 entry_guard_cancel(&guard);
2370 tt_ptr_op(guard, OP_EQ, NULL);
2371 tt_int_op(g->is_primary, OP_EQ, 0);
2372 tt_int_op(g->is_pending, OP_EQ, 0);
2374 done:
2375 guard_selection_free(gs);
2376 circuit_guard_state_free(guard);
2379 static void
2380 test_entry_guard_drop_guards(void *arg)
2382 (void) arg;
2383 int r;
2384 const node_t *node = NULL;
2385 circuit_guard_state_t *guard;
2386 guard_selection_t *gs = get_guard_selection_info();
2388 // Pick a guard, to get things set up.
2389 r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
2390 &node, &guard);
2391 tt_int_op(r, OP_EQ, 0);
2392 tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_GE,
2393 DFLT_MIN_FILTERED_SAMPLE_SIZE);
2394 tt_ptr_op(gs, OP_EQ, get_guard_selection_info());
2396 // Drop all the guards! (This is a bad idea....)
2397 remove_all_entry_guards_for_guard_selection(gs);
2398 gs = get_guard_selection_info();
2399 tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, 0);
2400 tt_int_op(smartlist_len(gs->primary_entry_guards), OP_EQ, 0);
2401 tt_int_op(smartlist_len(gs->confirmed_entry_guards), OP_EQ, 0);
2403 done:
2404 circuit_guard_state_free(guard);
2405 guard_selection_free(gs);
2408 /* Unit test setup function: Create a fake network, and set everything up
2409 * for testing the upgrade-a-waiting-circuit code. */
2410 typedef struct {
2411 guard_selection_t *gs;
2412 time_t start;
2413 circuit_guard_state_t *guard1_state;
2414 circuit_guard_state_t *guard2_state;
2415 entry_guard_t *guard1;
2416 entry_guard_t *guard2;
2417 origin_circuit_t *circ1;
2418 origin_circuit_t *circ2;
2419 smartlist_t *all_origin_circuits;
2420 } upgrade_circuits_data_t;
2421 static void *
2422 upgrade_circuits_setup(const struct testcase_t *testcase)
2424 upgrade_circuits_data_t *data = tor_malloc_zero(sizeof(*data));
2425 guard_selection_t *gs = data->gs =
2426 guard_selection_new("default", GS_TYPE_NORMAL);
2427 circuit_guard_state_t *guard;
2428 const node_t *node;
2429 entry_guard_t *g;
2430 int i;
2431 const int N_PRIMARY = DFLT_N_PRIMARY_GUARDS;
2432 const char *argument = testcase->setup_data;
2433 const int make_circ1_succeed = strstr(argument, "c1-done") != NULL;
2434 const int make_circ2_succeed = strstr(argument, "c2-done") != NULL;
2436 big_fake_network_setup(testcase);
2438 /* We're going to set things up in a state where a circuit will be ready to
2439 * be upgraded. Each test can make a single change (or not) that should
2440 * block the upgrade.
2443 /* First, make all the primary guards confirmed, and down. */
2444 data->start = approx_time();
2445 entry_guards_note_internet_connectivity(gs);
2446 for (i = 0; i < N_PRIMARY; ++i) {
2447 entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL, &node, &guard);
2448 g = entry_guard_handle_get(guard->guard);
2449 make_guard_confirmed(gs, g);
2450 entry_guard_failed(&guard);
2451 circuit_guard_state_free(guard);
2454 /* Grab another couple of guards */
2455 data->all_origin_circuits = smartlist_new();
2457 update_approx_time(data->start + 27);
2458 entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
2459 &node, &data->guard1_state);
2460 origin_circuit_t *circ;
2461 data->circ1 = circ = origin_circuit_new();
2462 circ->base_.purpose = CIRCUIT_PURPOSE_C_GENERAL;
2463 circ->guard_state = data->guard1_state;
2464 smartlist_add(data->all_origin_circuits, circ);
2466 update_approx_time(data->start + 30);
2467 entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
2468 &node, &data->guard2_state);
2469 data->circ2 = circ = origin_circuit_new();
2470 circ->base_.purpose = CIRCUIT_PURPOSE_C_GENERAL;
2471 circ->guard_state = data->guard2_state;
2472 smartlist_add(data->all_origin_circuits, circ);
2474 data->guard1 = entry_guard_handle_get(data->guard1_state->guard);
2475 data->guard2 = entry_guard_handle_get(data->guard2_state->guard);
2476 tor_assert(data->guard1 != data->guard2);
2477 tor_assert(data->guard1_state->state ==
2478 GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
2479 tor_assert(data->guard2_state->state ==
2480 GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
2482 guard_usable_t r;
2483 update_approx_time(data->start + 32);
2484 if (make_circ1_succeed) {
2485 r = entry_guard_succeeded(&data->guard1_state);
2486 tor_assert(r == GUARD_MAYBE_USABLE_LATER);
2487 tor_assert(data->guard1_state->state ==
2488 GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD);
2490 update_approx_time(data->start + 33);
2491 if (make_circ2_succeed) {
2492 r = entry_guard_succeeded(&data->guard2_state);
2493 tor_assert(r == GUARD_MAYBE_USABLE_LATER);
2494 tor_assert(data->guard2_state->state ==
2495 GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD);
2498 return data;
2500 static int
2501 upgrade_circuits_cleanup(const struct testcase_t *testcase, void *ptr)
2503 upgrade_circuits_data_t *data = ptr;
2504 // circuit_guard_state_free(data->guard1_state); // held in circ1
2505 // circuit_guard_state_free(data->guard2_state); // held in circ2
2506 guard_selection_free(data->gs);
2507 smartlist_free(data->all_origin_circuits);
2508 circuit_free_(TO_CIRCUIT(data->circ1));
2509 circuit_free_(TO_CIRCUIT(data->circ2));
2510 tor_free(data);
2511 return big_fake_network_cleanup(testcase, NULL);
2514 static void
2515 test_entry_guard_upgrade_a_circuit(void *arg)
2517 upgrade_circuits_data_t *data = arg;
2519 /* This is the easy case: we have no COMPLETED circuits, all the
2520 * primary guards are down, we have two WAITING circuits: one will
2521 * get upgraded to COMPLETED! (The one that started first.)
2524 smartlist_t *result = smartlist_new();
2525 int r;
2526 r = entry_guards_upgrade_waiting_circuits(data->gs,
2527 data->all_origin_circuits,
2528 result);
2529 tt_int_op(r, OP_EQ, 1);
2530 tt_int_op(smartlist_len(result), OP_EQ, 1);
2531 origin_circuit_t *oc = smartlist_get(result, 0);
2533 /* circ1 was started first, so we'll get told to ugrade it... */
2534 tt_ptr_op(oc, OP_EQ, data->circ1);
2536 /* And the guard state should be complete */
2537 tt_ptr_op(data->guard1_state, OP_NE, NULL);
2538 tt_int_op(data->guard1_state->state, OP_EQ, GUARD_CIRC_STATE_COMPLETE);
2540 done:
2541 smartlist_free(result);
2544 static void
2545 test_entry_guard_upgrade_blocked_by_live_primary_guards(void *arg)
2547 upgrade_circuits_data_t *data = arg;
2549 /* If any primary guards might be up, we can't upgrade any waiting
2550 * circuits.
2552 mark_primary_guards_maybe_reachable(data->gs);
2554 smartlist_t *result = smartlist_new();
2555 int r;
2556 setup_capture_of_logs(LOG_DEBUG);
2557 r = entry_guards_upgrade_waiting_circuits(data->gs,
2558 data->all_origin_circuits,
2559 result);
2560 tt_int_op(r, OP_EQ, 0);
2561 tt_int_op(smartlist_len(result), OP_EQ, 0);
2562 expect_log_msg_containing("not all primary guards were definitely down.");
2564 done:
2565 teardown_capture_of_logs();
2566 smartlist_free(result);
2569 static void
2570 test_entry_guard_upgrade_blocked_by_lack_of_waiting_circuits(void *arg)
2572 upgrade_circuits_data_t *data = arg;
2574 /* If no circuits are waiting, we can't upgrade anything. (The test
2575 * setup in this case was told not to make any of the circuits "waiting".)
2577 smartlist_t *result = smartlist_new();
2578 int r;
2579 setup_capture_of_logs(LOG_DEBUG);
2580 r = entry_guards_upgrade_waiting_circuits(data->gs,
2581 data->all_origin_circuits,
2582 result);
2583 tt_int_op(r, OP_EQ, 0);
2584 tt_int_op(smartlist_len(result), OP_EQ, 0);
2585 expect_log_msg_containing("Considered upgrading guard-stalled circuits, "
2586 "but didn't find any.");
2588 done:
2589 teardown_capture_of_logs();
2590 smartlist_free(result);
2593 static void
2594 test_entry_guard_upgrade_blocked_by_better_circ_complete(void *arg)
2596 upgrade_circuits_data_t *data = arg;
2598 /* We'll run through the logic of upgrade_a_circuit below...
2599 * and then try again to make sure that circ2 isn't also upgraded.
2602 smartlist_t *result = smartlist_new();
2603 int r;
2604 r = entry_guards_upgrade_waiting_circuits(data->gs,
2605 data->all_origin_circuits,
2606 result);
2607 tt_int_op(r, OP_EQ, 1);
2608 tt_int_op(smartlist_len(result), OP_EQ, 1);
2609 origin_circuit_t *oc = smartlist_get(result, 0);
2610 tt_ptr_op(oc, OP_EQ, data->circ1);
2611 tt_ptr_op(data->guard1_state, OP_NE, NULL);
2612 tt_int_op(data->guard1_state->state, OP_EQ, GUARD_CIRC_STATE_COMPLETE);
2614 /* Now, try again. Make sure that circ2 isn't upgraded. */
2615 smartlist_clear(result);
2616 setup_capture_of_logs(LOG_DEBUG);
2617 r = entry_guards_upgrade_waiting_circuits(data->gs,
2618 data->all_origin_circuits,
2619 result);
2620 tt_int_op(r, OP_EQ, 0);
2621 tt_int_op(smartlist_len(result), OP_EQ, 0);
2622 expect_log_msg_containing("At least one complete circuit had higher "
2623 "priority, so not upgrading.");
2625 done:
2626 teardown_capture_of_logs();
2627 smartlist_free(result);
2630 static void
2631 test_entry_guard_upgrade_not_blocked_by_restricted_circ_complete(void *arg)
2633 upgrade_circuits_data_t *data = arg;
2635 /* Once more, let circ1 become complete. But this time, we'll claim
2636 * that circ2 was restricted to not use the same guard as circ1. */
2637 data->guard2_state->restrictions =
2638 guard_create_exit_restriction((uint8_t*)data->guard1->identity);
2640 smartlist_t *result = smartlist_new();
2641 int r;
2642 r = entry_guards_upgrade_waiting_circuits(data->gs,
2643 data->all_origin_circuits,
2644 result);
2645 tt_int_op(r, OP_EQ, 1);
2646 tt_int_op(smartlist_len(result), OP_EQ, 1);
2647 origin_circuit_t *oc = smartlist_get(result, 0);
2648 tt_ptr_op(oc, OP_EQ, data->circ1);
2649 tt_ptr_op(data->guard1_state, OP_NE, NULL);
2650 tt_int_op(data->guard1_state->state, OP_EQ, GUARD_CIRC_STATE_COMPLETE);
2652 /* Now, we try again. Since circ2 has a restriction that circ1 doesn't obey,
2653 * circ2 _is_ eligible for upgrade. */
2654 smartlist_clear(result);
2655 r = entry_guards_upgrade_waiting_circuits(data->gs,
2656 data->all_origin_circuits,
2657 result);
2658 tt_int_op(r, OP_EQ, 1);
2659 tt_int_op(smartlist_len(result), OP_EQ, 1);
2660 origin_circuit_t *oc2 = smartlist_get(result, 0);
2661 tt_ptr_op(oc2, OP_EQ, data->circ2);
2663 done:
2664 smartlist_free(result);
2667 static void
2668 test_entry_guard_upgrade_not_blocked_by_worse_circ_complete(void *arg)
2670 upgrade_circuits_data_t *data = arg;
2671 smartlist_t *result = smartlist_new();
2672 /* here we manually make circ2 COMPLETE, and make sure that circ1
2673 * gets made complete anyway, since guard1 has higher priority
2675 update_approx_time(data->start + 300);
2676 data->guard2_state->state = GUARD_CIRC_STATE_COMPLETE;
2677 data->guard2_state->state_set_at = approx_time();
2678 update_approx_time(data->start + 301);
2680 /* Now, try again. Make sure that circ1 is approved. */
2681 int r;
2682 r = entry_guards_upgrade_waiting_circuits(data->gs,
2683 data->all_origin_circuits,
2684 result);
2685 tt_int_op(r, OP_EQ, 1);
2686 tt_int_op(smartlist_len(result), OP_EQ, 1);
2687 origin_circuit_t *oc = smartlist_get(result, 0);
2688 tt_ptr_op(oc, OP_EQ, data->circ1);
2690 done:
2691 smartlist_free(result);
2694 static void
2695 test_entry_guard_upgrade_blocked_by_better_circ_pending(void *arg)
2697 upgrade_circuits_data_t *data = arg;
2699 /* circ2 is done, but circ1 is still pending. Since circ1 is better,
2700 * we won't upgrade circ2. */
2702 /* XXXX Prop271 -- this is a kludge. I'm making sure circ1 _is_ better,
2703 * by messing with the guards' confirmed_idx */
2704 make_guard_confirmed(data->gs, data->guard1);
2706 int tmp;
2707 tmp = data->guard1->confirmed_idx;
2708 data->guard1->confirmed_idx = data->guard2->confirmed_idx;
2709 data->guard2->confirmed_idx = tmp;
2712 smartlist_t *result = smartlist_new();
2713 setup_capture_of_logs(LOG_DEBUG);
2714 int r;
2715 r = entry_guards_upgrade_waiting_circuits(data->gs,
2716 data->all_origin_circuits,
2717 result);
2718 tt_int_op(r, OP_EQ, 0);
2719 tt_int_op(smartlist_len(result), OP_EQ, 0);
2720 expect_log_msg_containing("but 1 pending circuit(s) had higher guard "
2721 "priority, so not upgrading.");
2723 done:
2724 teardown_capture_of_logs();
2725 smartlist_free(result);
2728 static void
2729 test_entry_guard_upgrade_not_blocked_by_restricted_circ_pending(void *arg)
2731 upgrade_circuits_data_t *data = arg;
2732 /* circ2 is done, but circ1 is still pending. But when there is a
2733 restriction on circ2 that circ1 can't satisfy, circ1 can't block
2734 circ2. */
2736 /* XXXX Prop271 -- this is a kludge. I'm making sure circ1 _is_ better,
2737 * by messing with the guards' confirmed_idx */
2738 make_guard_confirmed(data->gs, data->guard1);
2740 int tmp;
2741 tmp = data->guard1->confirmed_idx;
2742 data->guard1->confirmed_idx = data->guard2->confirmed_idx;
2743 data->guard2->confirmed_idx = tmp;
2746 data->guard2_state->restrictions =
2747 guard_create_exit_restriction((uint8_t*)data->guard1->identity);
2749 smartlist_t *result = smartlist_new();
2750 int r;
2751 r = entry_guards_upgrade_waiting_circuits(data->gs,
2752 data->all_origin_circuits,
2753 result);
2754 tt_int_op(r, OP_EQ, 1);
2755 tt_int_op(smartlist_len(result), OP_EQ, 1);
2756 origin_circuit_t *oc = smartlist_get(result, 0);
2757 tt_ptr_op(oc, OP_EQ, data->circ2);
2759 done:
2760 smartlist_free(result);
2763 static void
2764 test_entry_guard_upgrade_not_blocked_by_worse_circ_pending(void *arg)
2766 upgrade_circuits_data_t *data = arg;
2768 /* circ1 is done, but circ2 is still pending. Since circ1 is better,
2769 * we will upgrade it. */
2770 smartlist_t *result = smartlist_new();
2771 int r;
2772 r = entry_guards_upgrade_waiting_circuits(data->gs,
2773 data->all_origin_circuits,
2774 result);
2775 tt_int_op(r, OP_EQ, 1);
2776 tt_int_op(smartlist_len(result), OP_EQ, 1);
2777 origin_circuit_t *oc = smartlist_get(result, 0);
2778 tt_ptr_op(oc, OP_EQ, data->circ1);
2780 done:
2781 smartlist_free(result);
2784 static void
2785 test_entry_guard_should_expire_waiting(void *arg)
2787 (void)arg;
2788 circuit_guard_state_t *fake_state = tor_malloc_zero(sizeof(*fake_state));
2789 /* We'll leave "guard" unset -- it won't matter here. */
2791 /* No state? Can't expire. */
2792 tt_assert(! entry_guard_state_should_expire(NULL));
2794 /* Let's try one that expires. */
2795 fake_state->state = GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD;
2796 fake_state->state_set_at =
2797 approx_time() - DFLT_NONPRIMARY_GUARD_IDLE_TIMEOUT - 1;
2799 tt_assert(entry_guard_state_should_expire(fake_state));
2801 /* But it wouldn't expire if we changed the state. */
2802 fake_state->state = GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD;
2803 tt_assert(! entry_guard_state_should_expire(fake_state));
2805 /* And it wouldn't have expired a few seconds ago. */
2806 fake_state->state = GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD;
2807 fake_state->state_set_at =
2808 approx_time() - DFLT_NONPRIMARY_GUARD_IDLE_TIMEOUT + 5;
2809 tt_assert(! entry_guard_state_should_expire(fake_state));
2811 done:
2812 tor_free(fake_state);
2815 /** Test that the number of primary guards can be controlled using torrc */
2816 static void
2817 test_entry_guard_number_of_primaries(void *arg)
2819 (void) arg;
2821 /* Get default value */
2822 tt_int_op(get_n_primary_guards(), OP_EQ, DFLT_N_PRIMARY_GUARDS);
2824 /* Set number of primaries using torrc */
2825 get_options_mutable()->NumPrimaryGuards = 42;
2826 tt_int_op(get_n_primary_guards(), OP_EQ, 42);
2828 done:
2832 static void
2833 mock_directory_initiate_request(directory_request_t *req)
2835 if (req->guard_state) {
2836 circuit_guard_state_free(req->guard_state);
2840 static networkstatus_t *mock_ns_val = NULL;
2841 static networkstatus_t *
2842 mock_ns_get_by_flavor(consensus_flavor_t f)
2844 (void)f;
2845 return mock_ns_val;
2848 /** Test that when we fetch microdescriptors we skip guards that have
2849 * previously failed to serve us needed microdescriptors. */
2850 static void
2851 test_entry_guard_outdated_dirserver_exclusion(void *arg)
2853 int retval;
2854 response_handler_args_t *args = NULL;
2855 dir_connection_t *conn = NULL;
2856 (void) arg;
2858 /* Test prep: Make a new guard selection */
2859 guard_selection_t *gs = get_guard_selection_by_name("default",
2860 GS_TYPE_NORMAL, 1);
2862 /* ... we want to use entry guards */
2863 or_options_t *options = get_options_mutable();
2864 options->UseEntryGuards = 1;
2865 options->UseBridges = 0;
2867 /* ... prepare some md digests we want to download in the future */
2868 smartlist_t *digests = smartlist_new();
2869 const char *prose = "unhurried and wise, we perceive.";
2870 for (int i = 0; i < 20; i++) {
2871 smartlist_add(digests, (char*)prose);
2874 tt_int_op(smartlist_len(digests), OP_EQ, 20);
2876 /* ... now mock some functions */
2877 mock_ns_val = tor_malloc_zero(sizeof(networkstatus_t));
2878 MOCK(networkstatus_get_latest_consensus_by_flavor, mock_ns_get_by_flavor);
2879 MOCK(directory_initiate_request, mock_directory_initiate_request);
2881 /* Test logic:
2882 * 0. Create a proper guard set and primary guard list.
2883 * 1. Pretend to fail microdescriptor fetches from all the primary guards.
2884 * 2. Order another microdescriptor fetch and make sure that primary guards
2885 * get skipped since they failed previous fetches.
2888 { /* Setup primary guard list */
2889 int i;
2890 entry_guards_update_primary(gs);
2891 for (i = 0; i < DFLT_N_PRIMARY_GUARDS; ++i) {
2892 entry_guard_t *guard = smartlist_get(gs->sampled_entry_guards, i);
2893 make_guard_confirmed(gs, guard);
2895 entry_guards_update_primary(gs);
2899 /* Fail microdesc fetches with all the primary guards */
2900 args = tor_malloc_zero(sizeof(response_handler_args_t));
2901 args->status_code = 404;
2902 args->reason = NULL;
2903 args->body = NULL;
2904 args->body_len = 0;
2906 conn = tor_malloc_zero(sizeof(dir_connection_t));
2907 conn->requested_resource = tor_strdup("d/jlinblackorigami");
2908 conn->base_.purpose = DIR_PURPOSE_FETCH_MICRODESC;
2910 /* Pretend to fail fetches with all primary guards */
2911 SMARTLIST_FOREACH_BEGIN(gs->primary_entry_guards,const entry_guard_t *,g) {
2912 memcpy(conn->identity_digest, g->identity, DIGEST_LEN);
2914 retval = handle_response_fetch_microdesc(conn, args);
2915 tt_int_op(retval, OP_EQ, 0);
2916 } SMARTLIST_FOREACH_END(g);
2920 /* Now order the final md download */
2921 setup_full_capture_of_logs(LOG_INFO);
2922 initiate_descriptor_downloads(NULL, DIR_PURPOSE_FETCH_MICRODESC,
2923 digests, 3, 7, 0);
2925 /* ... and check that because we failed to fetch microdescs from all our
2926 * primaries, we didn't end up selecting a primary for fetching dir info */
2927 expect_log_msg_containing("No primary or confirmed guards available.");
2928 teardown_capture_of_logs();
2931 done:
2932 UNMOCK(networkstatus_get_latest_consensus_by_flavor);
2933 UNMOCK(directory_initiate_request);
2934 smartlist_free(digests);
2935 tor_free(mock_ns_val);
2936 tor_free(args);
2937 if (conn) {
2938 tor_free(conn->requested_resource);
2939 tor_free(conn);
2943 /** Test helper to extend the <b>oc</b> circuit path <b>n</b> times and then
2944 * ensure that the circuit is now complete. */
2945 static void
2946 helper_extend_circuit_path_n_times(origin_circuit_t *oc, int n)
2948 int retval;
2949 int i;
2951 /* Extend path n times */
2952 for (i = 0 ; i < n ; i++) {
2953 retval = onion_extend_cpath(oc);
2954 tt_int_op(retval, OP_EQ, 0);
2955 tt_int_op(circuit_get_cpath_len(oc), OP_EQ, i+1);
2958 /* Now do it one last time and see that circ is complete */
2959 retval = onion_extend_cpath(oc);
2960 tt_int_op(retval, OP_EQ, 1);
2962 done:
2966 /** Test for basic Tor path selection. Makes sure we build 3-hop circuits. */
2967 static void
2968 test_entry_guard_basic_path_selection(void *arg)
2970 (void) arg;
2972 int retval;
2974 /* Enable entry guards */
2975 or_options_t *options = get_options_mutable();
2976 options->UseEntryGuards = 1;
2978 /* disables /16 check since all nodes have the same addr... */
2979 options->EnforceDistinctSubnets = 0;
2981 /* Create our circuit */
2982 circuit_t *circ = dummy_origin_circuit_new(30);
2983 origin_circuit_t *oc = TO_ORIGIN_CIRCUIT(circ);
2984 oc->build_state = tor_malloc_zero(sizeof(cpath_build_state_t));
2986 /* First pick the exit and pin it on the build_state */
2987 retval = onion_pick_cpath_exit(oc, NULL, 0);
2988 tt_int_op(retval, OP_EQ, 0);
2990 /* Extend path 3 times. First we pick guard, then middle, then exit. */
2991 helper_extend_circuit_path_n_times(oc, 3);
2993 done:
2994 circuit_free_(circ);
2997 /** Test helper to build an L2 and L3 vanguard list. The vanguard lists
2998 * produced should be completely disjoint. */
2999 static void
3000 helper_setup_vanguard_list(or_options_t *options)
3002 int i = 0;
3004 /* Add some nodes to the vanguard L2 list */
3005 options->HSLayer2Nodes = routerset_new();
3006 for (i = 0; i < 10 ; i += 2) {
3007 node_t *vanguard_node = smartlist_get(big_fake_net_nodes, i);
3008 tt_assert(vanguard_node->is_possible_guard);
3009 routerset_parse(options->HSLayer2Nodes, vanguard_node->rs->nickname, "l2");
3011 /* also add some nodes to vanguard L3 list
3012 * (L2 list and L3 list should be disjoint for this test to work) */
3013 options->HSLayer3Nodes = routerset_new();
3014 for (i = 10; i < 20 ; i += 2) {
3015 node_t *vanguard_node = smartlist_get(big_fake_net_nodes, i);
3016 tt_assert(vanguard_node->is_possible_guard);
3017 routerset_parse(options->HSLayer3Nodes, vanguard_node->rs->nickname, "l3");
3020 done:
3024 /** Test to ensure that vanguard path selection works properly. Ensures that
3025 * default vanguard circuits are 4 hops, and that path selection works
3026 * correctly given the vanguard settings. */
3027 static void
3028 test_entry_guard_vanguard_path_selection(void *arg)
3030 (void) arg;
3032 int retval;
3034 /* Enable entry guards */
3035 or_options_t *options = get_options_mutable();
3036 options->UseEntryGuards = 1;
3038 /* XXX disables /16 check */
3039 options->EnforceDistinctSubnets = 0;
3041 /* Setup our vanguard list */
3042 helper_setup_vanguard_list(options);
3044 /* Create our circuit */
3045 circuit_t *circ = dummy_origin_circuit_new(30);
3046 origin_circuit_t *oc = TO_ORIGIN_CIRCUIT(circ);
3047 oc->build_state = tor_malloc_zero(sizeof(cpath_build_state_t));
3048 oc->build_state->is_internal = 1;
3050 /* Switch circuit purpose to vanguards */
3051 circ->purpose = CIRCUIT_PURPOSE_HS_VANGUARDS;
3053 /* First pick the exit and pin it on the build_state */
3054 tt_int_op(oc->build_state->desired_path_len, OP_EQ, 0);
3055 retval = onion_pick_cpath_exit(oc, NULL, 0);
3056 tt_int_op(retval, OP_EQ, 0);
3058 /* Ensure that vanguards make 4-hop circuits by default */
3059 tt_int_op(oc->build_state->desired_path_len, OP_EQ, 4);
3061 /* Extend path as many times as needed to have complete circ. */
3062 helper_extend_circuit_path_n_times(oc, oc->build_state->desired_path_len);
3064 /* Test that the cpath linked list is set correctly. */
3065 crypt_path_t *l1_node = oc->cpath;
3066 crypt_path_t *l2_node = l1_node->next;
3067 crypt_path_t *l3_node = l2_node->next;
3068 crypt_path_t *l4_node = l3_node->next;
3069 crypt_path_t *l1_node_again = l4_node->next;
3070 tt_ptr_op(l1_node, OP_EQ, l1_node_again);
3072 /* Test that L2 is indeed HSLayer2Node */
3073 retval = routerset_contains_extendinfo(options->HSLayer2Nodes,
3074 l2_node->extend_info);
3075 tt_int_op(retval, OP_EQ, 4);
3076 /* test that L3 node is _not_ contained in HSLayer2Node */
3077 retval = routerset_contains_extendinfo(options->HSLayer2Nodes,
3078 l3_node->extend_info);
3079 tt_int_op(retval, OP_LT, 4);
3081 /* Test that L3 is indeed HSLayer3Node */
3082 retval = routerset_contains_extendinfo(options->HSLayer3Nodes,
3083 l3_node->extend_info);
3084 tt_int_op(retval, OP_EQ, 4);
3085 /* test that L2 node is _not_ contained in HSLayer3Node */
3086 retval = routerset_contains_extendinfo(options->HSLayer3Nodes,
3087 l2_node->extend_info);
3088 tt_int_op(retval, OP_LT, 4);
3090 /* TODO: Test that L1 can be the same as exit. To test this we need start
3091 enforcing EnforceDistinctSubnets again, which means that we need to give
3092 each test node a different address which currently breaks some tests. */
3094 done:
3095 circuit_free_(circ);
3098 static void
3099 test_entry_guard_layer2_guards(void *arg)
3101 (void) arg;
3102 MOCK(router_have_minimum_dir_info, mock_router_have_minimum_dir_info);
3104 /* First check the enable/disable switch */
3105 get_options_mutable()->VanguardsLiteEnabled = 0;
3106 tt_int_op(vanguards_lite_is_enabled(), OP_EQ, 0);
3108 get_options_mutable()->VanguardsLiteEnabled = 1;
3109 tt_int_op(vanguards_lite_is_enabled(), OP_EQ, 1);
3111 get_options_mutable()->VanguardsLiteEnabled = -1;
3112 tt_int_op(vanguards_lite_is_enabled(), OP_EQ, 1);
3114 /* OK now let's move to actual testing */
3116 /* Remove restrictions to route around Big Fake Network restrictions */
3117 get_options_mutable()->EnforceDistinctSubnets = 0;
3119 /* Create the L2 guardset */
3120 maintain_layer2_guards();
3122 const routerset_t *l2_guards = get_layer2_guards();
3123 tt_assert(l2_guards);
3124 tt_int_op(routerset_len(l2_guards), OP_EQ, 4);
3126 done:
3127 UNMOCK(router_have_minimum_dir_info);
3130 static const struct testcase_setup_t big_fake_network = {
3131 big_fake_network_setup, big_fake_network_cleanup
3134 static const struct testcase_setup_t upgrade_circuits = {
3135 upgrade_circuits_setup, upgrade_circuits_cleanup
3138 #ifndef COCCI
3139 #define NO_PREFIX_TEST(name) \
3140 { #name, test_ ## name, 0, NULL, NULL }
3142 #define EN_TEST_BASE(name, fork, setup, arg) \
3143 { #name, test_entry_guard_ ## name, fork, setup, (void*)(arg) }
3145 #define EN_TEST(name) EN_TEST_BASE(name, 0, NULL, NULL)
3146 #define EN_TEST_FORK(name) EN_TEST_BASE(name, TT_FORK, NULL, NULL)
3148 #define BFN_TEST(name) \
3149 EN_TEST_BASE(name, TT_FORK, &big_fake_network, NULL), \
3150 { #name "_reasonably_future", test_entry_guard_ ## name, TT_FORK, \
3151 &big_fake_network, (void*)(REASONABLY_FUTURE) }, \
3152 { #name "_reasonably_past", test_entry_guard_ ## name, TT_FORK, \
3153 &big_fake_network, (void*)(REASONABLY_PAST) }
3155 #define UPGRADE_TEST(name, arg) \
3156 EN_TEST_BASE(name, TT_FORK, &upgrade_circuits, arg), \
3157 { #name "_reasonably_future", test_entry_guard_ ## name, TT_FORK, \
3158 &upgrade_circuits, (void*)(arg REASONABLY_FUTURE) }, \
3159 { #name "_reasonably_past", test_entry_guard_ ## name, TT_FORK, \
3160 &upgrade_circuits, (void*)(arg REASONABLY_PAST) }
3161 #endif /* !defined(COCCI) */
3163 struct testcase_t entrynodes_tests[] = {
3164 NO_PREFIX_TEST(node_preferred_orport),
3165 NO_PREFIX_TEST(entry_guard_describe),
3167 EN_TEST(randomize_time),
3168 EN_TEST(encode_for_state_minimal),
3169 EN_TEST(encode_for_state_maximal),
3170 EN_TEST(parse_from_state_minimal),
3171 EN_TEST(parse_from_state_maximal),
3172 EN_TEST(parse_from_state_failure),
3173 EN_TEST(parse_from_state_partial_failure),
3175 EN_TEST_FORK(parse_from_state_full),
3176 EN_TEST_FORK(parse_from_state_broken),
3177 EN_TEST_FORK(get_guard_selection_by_name),
3178 EN_TEST_FORK(number_of_primaries),
3180 BFN_TEST(choose_selection_initial),
3181 BFN_TEST(add_single_guard),
3182 BFN_TEST(node_filter),
3183 BFN_TEST(expand_sample),
3184 BFN_TEST(expand_sample_small_net),
3185 BFN_TEST(update_from_consensus_status),
3186 BFN_TEST(update_from_consensus_repair),
3187 BFN_TEST(update_from_consensus_remove),
3188 BFN_TEST(confirming_guards),
3189 BFN_TEST(sample_reachable_filtered),
3190 BFN_TEST(sample_reachable_filtered_empty),
3191 BFN_TEST(retry_unreachable),
3192 BFN_TEST(manage_primary),
3193 BFN_TEST(correct_cascading_order),
3195 BFN_TEST(layer2_guards),
3197 EN_TEST_FORK(guard_preferred),
3199 BFN_TEST(select_for_circuit_no_confirmed),
3200 BFN_TEST(select_for_circuit_confirmed),
3201 BFN_TEST(select_for_circuit_highlevel_primary),
3202 BFN_TEST(select_for_circuit_highlevel_confirm_other),
3203 BFN_TEST(select_for_circuit_highlevel_primary_retry),
3204 BFN_TEST(select_and_cancel),
3205 BFN_TEST(drop_guards),
3206 BFN_TEST(outdated_dirserver_exclusion),
3207 BFN_TEST(basic_path_selection),
3208 BFN_TEST(vanguard_path_selection),
3210 UPGRADE_TEST(upgrade_a_circuit, "c1-done c2-done"),
3211 UPGRADE_TEST(upgrade_blocked_by_live_primary_guards, "c1-done c2-done"),
3212 UPGRADE_TEST(upgrade_blocked_by_lack_of_waiting_circuits, ""),
3213 UPGRADE_TEST(upgrade_blocked_by_better_circ_complete, "c1-done c2-done"),
3214 UPGRADE_TEST(upgrade_not_blocked_by_restricted_circ_complete,
3215 "c1-done c2-done"),
3216 UPGRADE_TEST(upgrade_not_blocked_by_worse_circ_complete, "c1-done c2-done"),
3217 UPGRADE_TEST(upgrade_blocked_by_better_circ_pending, "c2-done"),
3218 UPGRADE_TEST(upgrade_not_blocked_by_restricted_circ_pending,
3219 "c2-done"),
3220 UPGRADE_TEST(upgrade_not_blocked_by_worse_circ_pending, "c1-done"),
3222 EN_TEST_FORK(should_expire_waiting),
3224 END_OF_TESTCASES