1 /* Copyright (c) 2014-2020, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
6 #define CIRCUITLIST_PRIVATE
7 #define CIRCUITBUILD_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"
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,
59 /** Dummy Tor state used in unittests. */
60 static or_state_t
*dummy_state
= NULL
;
62 get_or_state_replacement(void)
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
)
82 return dummy_consensus
;
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))
95 /* Helper function to free a test node. */
97 test_node_free(node_t
*n
)
100 tor_free(n
->md
->onion_curve25519_pkey
);
101 short_policy_free(n
->md
->exit_policy
);
106 /* Unittest cleanup function: Cleanup the fake network. */
108 big_fake_network_cleanup(const struct testcase_t
*testcase
, void *ptr
)
113 if (big_fake_net_nodes
) {
114 SMARTLIST_FOREACH(big_fake_net_nodes
, node_t
*, n
, {
117 smartlist_free(big_fake_net_nodes
);
120 UNMOCK(nodelist_get_list
);
121 UNMOCK(node_get_by_id
);
122 UNMOCK(get_or_state
);
123 UNMOCK(networkstatus_get_reasonably_live_consensus
);
124 or_state_free(dummy_state
);
126 tor_free(dummy_consensus
);
131 #define REASONABLY_FUTURE " reasonably-future"
132 #define REASONABLY_PAST " reasonably-past"
134 /* Unittest setup function: Setup a fake network. */
136 big_fake_network_setup(const struct testcase_t
*testcase
)
140 /* These are minimal node_t objects that only contain the aspects of node_t
141 * that we need for entrynodes.c. */
142 const int N_NODES
= 271;
144 const char *argument
= testcase
->setup_data
;
145 int reasonably_future_consensus
= 0, reasonably_past_consensus
= 0;
147 reasonably_future_consensus
= strstr(argument
, REASONABLY_FUTURE
) != NULL
;
148 reasonably_past_consensus
= strstr(argument
, REASONABLY_PAST
) != NULL
;
151 big_fake_net_nodes
= smartlist_new();
152 for (i
= 0; i
< N_NODES
; ++i
) {
153 curve25519_secret_key_t curve25519_secret_key
;
155 node_t
*n
= tor_malloc_zero(sizeof(node_t
));
156 n
->md
= tor_malloc_zero(sizeof(microdesc_t
));
158 /* Generate curve25519 key for this node */
159 n
->md
->onion_curve25519_pkey
=
160 tor_malloc_zero(sizeof(curve25519_public_key_t
));
161 curve25519_secret_key_generate(&curve25519_secret_key
, 0);
162 curve25519_public_key_generate(n
->md
->onion_curve25519_pkey
,
163 &curve25519_secret_key
);
165 crypto_rand(n
->identity
, sizeof(n
->identity
));
166 n
->rs
= tor_malloc_zero(sizeof(routerstatus_t
));
168 memcpy(n
->rs
->identity_digest
, n
->identity
, DIGEST_LEN
);
170 n
->is_running
= n
->is_valid
= n
->is_fast
= n
->is_stable
= 1;
172 /* Note: all these guards have the same address, so you'll need to
173 * disable EnforceDistinctSubnets when a restriction is applied. */
174 tor_addr_from_ipv4h(&n
->rs
->ipv4_addr
, 0x04020202);
175 n
->rs
->ipv4_orport
= 1234;
176 n
->rs
->is_v2_dir
= 1;
177 n
->rs
->has_bandwidth
= 1;
178 n
->rs
->bandwidth_kb
= 30;
180 /* Make a random nickname for each node */
182 char nickname_binary
[8];
183 crypto_rand(nickname_binary
, sizeof(nickname_binary
));
184 base32_encode(n
->rs
->nickname
, sizeof(n
->rs
->nickname
),
185 nickname_binary
, sizeof(nickname_binary
));
188 /* Call half of the nodes a possible guard. */
190 n
->is_possible_guard
= 1;
191 n
->rs
->guardfraction_percentage
= 100;
192 n
->rs
->has_guardfraction
= 1;
193 n
->rs
->is_possible_guard
= 1;
196 /* Make some of these nodes a possible exit */
198 n
->md
->exit_policy
= parse_short_policy("accept 443");
201 n
->nodelist_idx
= smartlist_len(big_fake_net_nodes
);
202 smartlist_add(big_fake_net_nodes
, n
);
205 dummy_state
= or_state_new();
206 dummy_consensus
= tor_malloc_zero(sizeof(networkstatus_t
));
207 if (reasonably_future_consensus
) {
208 /* Make the dummy consensus valid in 6 hours, and expiring in 7 hours. */
209 dummy_consensus
->valid_after
= approx_time() + 6*3600;
210 dummy_consensus
->valid_until
= approx_time() + 7*3600;
211 } else if (reasonably_past_consensus
) {
212 /* Make the dummy consensus valid from 16 hours ago, but expired 12 hours
214 dummy_consensus
->valid_after
= approx_time() - 16*3600;
215 dummy_consensus
->valid_until
= approx_time() - 12*3600;
217 /* Make the dummy consensus valid for an hour either side of now. */
218 dummy_consensus
->valid_after
= approx_time() - 3600;
219 dummy_consensus
->valid_until
= approx_time() + 3600;
222 MOCK(nodelist_get_list
, bfn_mock_nodelist_get_list
);
223 MOCK(node_get_by_id
, bfn_mock_node_get_by_id
);
225 get_or_state_replacement
);
226 MOCK(networkstatus_get_reasonably_live_consensus
,
227 bfn_mock_networkstatus_get_reasonably_live_consensus
);
228 /* Return anything but NULL (it's interpreted as test fail) */
229 return (void*)testcase
;
233 mock_randomize_time_no_randomization(time_t a
, time_t b
)
239 static or_options_t
*mocked_options
;
241 static const or_options_t
*
242 mock_get_options(void)
244 return mocked_options
;
247 #define TEST_IPV4_ADDR "123.45.67.89"
248 #define TEST_IPV6_ADDR "[1234:5678:90ab:cdef::]"
251 test_node_preferred_orport(void *arg
)
254 tor_addr_t ipv4_addr
;
255 const uint16_t ipv4_port
= 4444;
256 tor_addr_t ipv6_addr
;
257 const uint16_t ipv6_port
= 6666;
258 routerinfo_t node_ri
;
263 mocked_options
= options_new();
264 /* We don't test ClientPreferIPv6ORPort here, because it's used in
265 * nodelist_set_consensus to setup node.ipv6_preferred, which we set
267 MOCK(get_options
, mock_get_options
);
269 /* Setup IP addresses */
270 tor_addr_parse(&ipv4_addr
, TEST_IPV4_ADDR
);
271 tor_addr_parse(&ipv6_addr
, TEST_IPV6_ADDR
);
274 memset(&node_ri
, 0, sizeof(node_ri
));
275 tor_addr_copy(&node_ri
.ipv4_addr
, &ipv4_addr
);
276 node_ri
.ipv4_orport
= ipv4_port
;
277 tor_addr_copy(&node_ri
.ipv6_addr
, &ipv6_addr
);
278 node_ri
.ipv6_orport
= ipv6_port
;
281 memset(&node
, 0, sizeof(node
));
284 /* Check the preferred address is IPv4 if we're only using IPv4, regardless
285 * of whether we prefer it or not */
286 mocked_options
->ClientUseIPv4
= 1;
287 mocked_options
->ClientUseIPv6
= 0;
288 node
.ipv6_preferred
= 0;
289 node_get_pref_orport(&node
, &ap
);
290 tt_assert(tor_addr_eq(&ap
.addr
, &ipv4_addr
));
291 tt_assert(ap
.port
== ipv4_port
);
293 node
.ipv6_preferred
= 1;
294 node_get_pref_orport(&node
, &ap
);
295 tt_assert(tor_addr_eq(&ap
.addr
, &ipv4_addr
));
296 tt_assert(ap
.port
== ipv4_port
);
298 /* Check the preferred address is IPv4 if we're using IPv4 and IPv6, but
299 * don't prefer the IPv6 address */
300 mocked_options
->ClientUseIPv4
= 1;
301 mocked_options
->ClientUseIPv6
= 1;
302 node
.ipv6_preferred
= 0;
303 node_get_pref_orport(&node
, &ap
);
304 tt_assert(tor_addr_eq(&ap
.addr
, &ipv4_addr
));
305 tt_assert(ap
.port
== ipv4_port
);
307 /* Check the preferred address is IPv6 if we prefer it and
308 * ClientUseIPv6 is 1, regardless of ClientUseIPv4 */
309 mocked_options
->ClientUseIPv4
= 1;
310 mocked_options
->ClientUseIPv6
= 1;
311 node
.ipv6_preferred
= 1;
312 node_get_pref_orport(&node
, &ap
);
313 tt_assert(tor_addr_eq(&ap
.addr
, &ipv6_addr
));
314 tt_assert(ap
.port
== ipv6_port
);
316 mocked_options
->ClientUseIPv4
= 0;
317 node_get_pref_orport(&node
, &ap
);
318 tt_assert(tor_addr_eq(&ap
.addr
, &ipv6_addr
));
319 tt_assert(ap
.port
== ipv6_port
);
321 /* Check the preferred address is IPv6 if we don't prefer it, but
322 * ClientUseIPv4 is 0 */
323 mocked_options
->ClientUseIPv4
= 0;
324 mocked_options
->ClientUseIPv6
= 1;
325 node
.ipv6_preferred
= reachable_addr_prefer_ipv6_orport(mocked_options
);
326 node_get_pref_orport(&node
, &ap
);
327 tt_assert(tor_addr_eq(&ap
.addr
, &ipv6_addr
));
328 tt_assert(ap
.port
== ipv6_port
);
331 or_options_free(mocked_options
);
336 test_entry_guard_describe(void *arg
)
340 memset(&g
, 0, sizeof(g
));
341 strlcpy(g
.nickname
, "okefenokee", sizeof(g
.nickname
));
342 memcpy(g
.identity
, "theforestprimeval---", DIGEST_LEN
);
344 tt_str_op(entry_guard_describe(&g
), OP_EQ
,
345 "okefenokee ($746865666F726573747072696D6576616C2D2D2D)");
352 test_entry_guard_randomize_time(void *arg
)
354 const time_t now
= 1479153573;
355 const int delay
= 86400;
361 for (i
= 0; i
< N
; ++i
) {
362 t
= randomize_time(now
, delay
);
363 tt_int_op(t
, OP_LE
, now
);
364 tt_int_op(t
, OP_GE
, now
-delay
);
367 /* now try the corner cases */
368 for (i
= 0; i
< N
; ++i
) {
369 t
= randomize_time(100, delay
);
370 tt_int_op(t
, OP_GE
, 1);
371 tt_int_op(t
, OP_LE
, 100);
373 t
= randomize_time(0, delay
);
374 tt_int_op(t
, OP_EQ
, 1);
382 test_entry_guard_encode_for_state_minimal(void *arg
)
385 entry_guard_t
*eg
= tor_malloc_zero(sizeof(entry_guard_t
));
387 eg
->selection_name
= tor_strdup("wubwub");
388 memcpy(eg
->identity
, "plurpyflurpyslurpydo", DIGEST_LEN
);
389 eg
->sampled_on_date
= 1479081600;
390 eg
->confirmed_idx
= -1;
393 s
= entry_guard_encode_for_state(eg
, 0);
397 "rsa_id=706C75727079666C75727079736C75727079646F "
398 "sampled_on=2016-11-14T00:00:00 "
403 entry_guard_free(eg
);
408 test_entry_guard_encode_for_state_maximal(void *arg
)
411 entry_guard_t
*eg
= tor_malloc_zero(sizeof(entry_guard_t
));
413 strlcpy(eg
->nickname
, "Fred", sizeof(eg
->nickname
));
414 eg
->selection_name
= tor_strdup("default");
415 memcpy(eg
->identity
, "plurpyflurpyslurpydo", DIGEST_LEN
);
416 eg
->bridge_addr
= tor_malloc_zero(sizeof(tor_addr_port_t
));
417 tor_addr_from_ipv4h(&eg
->bridge_addr
->addr
, 0x08080404);
418 eg
->bridge_addr
->port
= 9999;
419 eg
->sampled_on_date
= 1479081600;
420 eg
->sampled_by_version
= tor_strdup("1.2.3");
421 eg
->unlisted_since_date
= 1479081645;
422 eg
->currently_listed
= 1;
423 eg
->confirmed_on_date
= 1479081690;
424 eg
->confirmed_idx
= 333;
425 eg
->sampled_idx
= 42;
426 eg
->extra_state_fields
= tor_strdup("and the green grass grew all around");
429 s
= entry_guard_encode_for_state(eg
, 0);
433 "rsa_id=706C75727079666C75727079736C75727079646F "
434 "bridge_addr=8.8.4.4:9999 "
436 "sampled_on=2016-11-14T00:00:00 "
439 "unlisted_since=2016-11-14T00:00:45 "
441 "confirmed_on=2016-11-14T00:01:30 "
443 "and the green grass grew all around");
446 entry_guard_free(eg
);
451 test_entry_guard_parse_from_state_minimal(void *arg
)
454 char *mem_op_hex_tmp
= NULL
;
455 entry_guard_t
*eg
= NULL
;
456 time_t t
= approx_time();
458 eg
= entry_guard_parse_from_state(
460 "rsa_id=596f75206d6179206e656564206120686f626279");
463 tt_str_op(eg
->selection_name
, OP_EQ
, "default_plus");
464 test_mem_op_hex(eg
->identity
, OP_EQ
,
465 "596f75206d6179206e656564206120686f626279");
466 tt_str_op(eg
->nickname
, OP_EQ
, "$596F75206D6179206E656564206120686F626279");
467 tt_ptr_op(eg
->bridge_addr
, OP_EQ
, NULL
);
468 tt_i64_op(eg
->sampled_on_date
, OP_GE
, t
);
469 tt_i64_op(eg
->sampled_on_date
, OP_LE
, t
+86400);
470 tt_i64_op(eg
->unlisted_since_date
, OP_EQ
, 0);
471 tt_ptr_op(eg
->sampled_by_version
, OP_EQ
, NULL
);
472 tt_int_op(eg
->currently_listed
, OP_EQ
, 0);
473 tt_i64_op(eg
->confirmed_on_date
, OP_EQ
, 0);
474 tt_int_op(eg
->confirmed_idx
, OP_EQ
, -1);
476 tt_int_op(eg
->last_tried_to_connect
, OP_EQ
, 0);
477 tt_int_op(eg
->is_reachable
, OP_EQ
, GUARD_REACHABLE_MAYBE
);
480 entry_guard_free(eg
);
481 tor_free(mem_op_hex_tmp
);
485 test_entry_guard_parse_from_state_maximal(void *arg
)
488 char *mem_op_hex_tmp
= NULL
;
489 entry_guard_t
*eg
= NULL
;
491 eg
= entry_guard_parse_from_state(
493 "rsa_id=706C75727079666C75727079736C75727079646F "
494 "bridge_addr=[1::3]:9999 "
496 "sampled_on=2016-11-14T00:00:00 "
498 "unlisted_since=2016-11-14T00:00:45 "
500 "confirmed_on=2016-11-14T00:01:30 "
502 "and the green grass grew all around "
503 "rsa_id=all,around");
506 test_mem_op_hex(eg
->identity
, OP_EQ
,
507 "706C75727079666C75727079736C75727079646F");
508 tt_str_op(fmt_addr(&eg
->bridge_addr
->addr
), OP_EQ
, "1::3");
509 tt_int_op(eg
->bridge_addr
->port
, OP_EQ
, 9999);
510 tt_str_op(eg
->nickname
, OP_EQ
, "Fred");
511 tt_i64_op(eg
->sampled_on_date
, OP_EQ
, 1479081600);
512 tt_i64_op(eg
->unlisted_since_date
, OP_EQ
, 1479081645);
513 tt_str_op(eg
->sampled_by_version
, OP_EQ
, "1.2.3");
514 tt_int_op(eg
->currently_listed
, OP_EQ
, 1);
515 tt_i64_op(eg
->confirmed_on_date
, OP_EQ
, 1479081690);
516 tt_int_op(eg
->confirmed_idx
, OP_EQ
, 333);
517 tt_str_op(eg
->extra_state_fields
, OP_EQ
,
518 "and the green grass grew all around rsa_id=all,around");
520 tt_int_op(eg
->last_tried_to_connect
, OP_EQ
, 0);
521 tt_int_op(eg
->is_reachable
, OP_EQ
, GUARD_REACHABLE_MAYBE
);
524 entry_guard_free(eg
);
525 tor_free(mem_op_hex_tmp
);
529 test_entry_guard_parse_from_state_failure(void *arg
)
532 entry_guard_t
*eg
= NULL
;
535 eg
= entry_guard_parse_from_state(
536 "rsa_id=596f75206d6179206e656564206120686f626270");
537 tt_ptr_op(eg
, OP_EQ
, NULL
);
540 eg
= entry_guard_parse_from_state("in=default nickname=Fred");
541 tt_ptr_op(eg
, OP_EQ
, NULL
);
543 /* Bad RSA ID: bad character. */
544 eg
= entry_guard_parse_from_state(
546 "rsa_id=596f75206d6179206e656564206120686f62627q");
547 tt_ptr_op(eg
, OP_EQ
, NULL
);
549 /* Bad RSA ID: too long.*/
550 eg
= entry_guard_parse_from_state(
552 "rsa_id=596f75206d6179206e656564206120686f6262703");
553 tt_ptr_op(eg
, OP_EQ
, NULL
);
555 /* Bad RSA ID: too short.*/
556 eg
= entry_guard_parse_from_state(
558 "rsa_id=596f75206d6179206e65656420612");
559 tt_ptr_op(eg
, OP_EQ
, NULL
);
562 entry_guard_free(eg
);
566 test_entry_guard_parse_from_state_partial_failure(void *arg
)
569 char *mem_op_hex_tmp
= NULL
;
570 entry_guard_t
*eg
= NULL
;
571 time_t t
= approx_time();
573 eg
= entry_guard_parse_from_state(
575 "rsa_id=706C75727079666C75727079736C75727079646F "
576 "bridge_addr=1.2.3.3.4:5 "
577 "nickname=FredIsANodeWithAStrangeNicknameThatIsTooLong "
578 "sampled_on=2016-11-14T00:00:99 "
579 "sampled_by=1.2.3 stuff in the middle "
580 "unlisted_since=2016-xx-14T00:00:45 "
582 "confirmed_on=2016-11-14T00:01:30zz "
584 "and the green grass grew all around "
585 "rsa_id=all,around");
588 test_mem_op_hex(eg
->identity
, OP_EQ
,
589 "706C75727079666C75727079736C75727079646F");
590 tt_str_op(eg
->nickname
, OP_EQ
, "FredIsANodeWithAStrangeNicknameThatIsTooL");
591 tt_ptr_op(eg
->bridge_addr
, OP_EQ
, NULL
);
592 tt_i64_op(eg
->sampled_on_date
, OP_EQ
, t
);
593 tt_i64_op(eg
->unlisted_since_date
, OP_EQ
, 0);
594 tt_str_op(eg
->sampled_by_version
, OP_EQ
, "1.2.3");
595 tt_int_op(eg
->currently_listed
, OP_EQ
, 0);
596 tt_i64_op(eg
->confirmed_on_date
, OP_EQ
, 0);
597 tt_int_op(eg
->confirmed_idx
, OP_EQ
, -1);
598 tt_str_op(eg
->extra_state_fields
, OP_EQ
,
599 "stuff in the middle and the green grass grew all around "
600 "rsa_id=all,around");
602 tt_int_op(eg
->last_tried_to_connect
, OP_EQ
, 0);
603 tt_int_op(eg
->is_reachable
, OP_EQ
, GUARD_REACHABLE_MAYBE
);
606 entry_guard_free(eg
);
607 tor_free(mem_op_hex_tmp
);
611 mock_entry_guard_is_listed(guard_selection_t
*gs
, const entry_guard_t
*guard
)
619 test_entry_guard_parse_from_state_full(void *arg
)
622 /* Here's a state I made while testing. The identities and locations for
623 * the bridges are redacted. */
625 "Guard in=default rsa_id=214F44BD5B638E8C817D47FF7C97397790BF0345 "
626 "nickname=TotallyNinja sampled_on=2016-11-12T19:32:49 "
628 "sampled_by=0.3.0.0-alpha-dev "
630 "Guard in=default rsa_id=052900AB0EA3ED54BAB84AE8A99E74E8693CE2B2 "
631 "nickname=5OfNovember sampled_on=2016-11-20T04:32:05 "
633 "sampled_by=0.3.0.0-alpha-dev "
634 "listed=1 confirmed_on=2016-11-22T08:13:28 confirmed_idx=0 "
635 "pb_circ_attempts=4.000000 pb_circ_successes=2.000000 "
636 "pb_successful_circuits_closed=2.000000\n"
637 "Guard in=default rsa_id=7B700C0C207EBD0002E00F499BE265519AC3C25A "
638 "nickname=dc6jgk11 sampled_on=2016-11-28T11:50:13 "
640 "sampled_by=0.3.0.0-alpha-dev "
641 "listed=1 confirmed_on=2016-11-24T08:45:30 confirmed_idx=4 "
642 "pb_circ_attempts=5.000000 pb_circ_successes=5.000000 "
643 "pb_successful_circuits_closed=5.000000\n"
644 "Guard in=wobblesome rsa_id=7B700C0C207EBD0002E00F499BE265519AC3C25A "
645 "nickname=dc6jgk11 sampled_on=2016-11-28T11:50:13 "
647 "sampled_by=0.3.0.0-alpha-dev "
649 "Guard in=default rsa_id=E9025AD60D86875D5F11548D536CC6AF60F0EF5E "
650 "nickname=maibrunn sampled_on=2016-11-25T22:36:38 "
652 "sampled_by=0.3.0.0-alpha-dev listed=1\n"
653 "Guard in=default rsa_id=DCD30B90BA3A792DA75DC54A327EF353FB84C38E "
654 "nickname=Unnamed sampled_on=2016-11-25T14:34:00 "
656 "sampled_by=0.3.0.0-alpha-dev listed=1\n"
657 "Guard in=bridges rsa_id=8FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2E "
658 "bridge_addr=24.1.1.1:443 sampled_on=2016-11-25T06:44:14 "
660 "sampled_by=0.3.0.0-alpha-dev listed=1 "
661 "confirmed_on=2016-11-29T10:36:06 confirmed_idx=0 "
662 "pb_circ_attempts=8.000000 pb_circ_successes=8.000000 "
663 "pb_successful_circuits_closed=13.000000\n"
664 "Guard in=bridges rsa_id=5800000000000000000000000000000000000000 "
665 "bridge_addr=37.218.246.143:28366 "
666 "sampled_on=2016-11-18T15:07:34 sampled_idx=1 "
667 "sampled_by=0.3.0.0-alpha-dev listed=1\n";
669 config_line_t
*lines
= NULL
;
670 or_state_t
*state
= tor_malloc_zero(sizeof(or_state_t
));
671 int r
= config_get_lines(STATE
, &lines
, 0);
673 smartlist_t
*text
= smartlist_new();
676 // So nodes aren't expired. This is Tue, 13 Dec 2016 09:37:14 GMT
677 update_approx_time(1481621834);
679 MOCK(entry_guard_is_listed
, mock_entry_guard_is_listed
);
683 get_or_state_replacement
);
685 tt_int_op(r
, OP_EQ
, 0);
688 state
->Guard
= lines
;
690 /* Try it first without setting the result. */
691 r
= entry_guards_parse_state(state
, 0, &msg
);
692 tt_int_op(r
, OP_EQ
, 0);
693 guard_selection_t
*gs_br
=
694 get_guard_selection_by_name("bridges", GS_TYPE_BRIDGE
, 0);
695 tt_ptr_op(gs_br
, OP_EQ
, NULL
);
697 r
= entry_guards_parse_state(state
, 1, &msg
);
698 tt_int_op(r
, OP_EQ
, 0);
699 gs_br
= get_guard_selection_by_name("bridges", GS_TYPE_BRIDGE
, 0);
700 guard_selection_t
*gs_df
=
701 get_guard_selection_by_name("default", GS_TYPE_NORMAL
, 0);
702 guard_selection_t
*gs_wb
=
703 get_guard_selection_by_name("wobblesome", GS_TYPE_NORMAL
, 0);
709 tt_int_op(smartlist_len(gs_df
->sampled_entry_guards
), OP_EQ
, 5);
710 tt_int_op(smartlist_len(gs_br
->sampled_entry_guards
), OP_EQ
, 2);
711 tt_int_op(smartlist_len(gs_wb
->sampled_entry_guards
), OP_EQ
, 1);
713 /* Try again; make sure it doesn't double-add the guards. */
714 r
= entry_guards_parse_state(state
, 1, &msg
);
715 tt_int_op(r
, OP_EQ
, 0);
716 gs_br
= get_guard_selection_by_name("bridges", GS_TYPE_BRIDGE
, 0);
717 gs_df
= get_guard_selection_by_name("default", GS_TYPE_NORMAL
, 0);
720 tt_int_op(smartlist_len(gs_df
->sampled_entry_guards
), OP_EQ
, 5);
721 tt_int_op(smartlist_len(gs_br
->sampled_entry_guards
), OP_EQ
, 2);
723 /* Re-encode; it should be the same... almost. */
725 /* (Make a guard nonpersistent first) */
726 entry_guard_t
*g
= smartlist_get(gs_df
->sampled_entry_guards
, 0);
727 g
->is_persistent
= 0;
729 config_free_lines(lines
);
730 lines
= state
->Guard
= NULL
; // to prevent double-free.
731 entry_guards_update_state(state
);
732 tt_assert(state
->Guard
);
733 lines
= state
->Guard
;
736 for (ln
= lines
; ln
; ln
= ln
->next
) {
737 smartlist_add_asprintf(text
, "%s %s\n",ln
->key
, ln
->value
);
739 joined
= smartlist_join_strings(text
, "", 0, NULL
);
740 tt_str_op(joined
, OP_EQ
,
741 "Guard in=default rsa_id=052900AB0EA3ED54BAB84AE8A99E74E8693CE2B2 "
742 "nickname=5OfNovember sampled_on=2016-11-20T04:32:05 "
744 "sampled_by=0.3.0.0-alpha-dev "
745 "listed=1 confirmed_on=2016-11-22T08:13:28 confirmed_idx=0 "
746 "pb_circ_attempts=4.000000 pb_circ_successes=2.000000 "
747 "pb_successful_circuits_closed=2.000000\n"
748 "Guard in=default rsa_id=7B700C0C207EBD0002E00F499BE265519AC3C25A "
749 "nickname=dc6jgk11 sampled_on=2016-11-28T11:50:13 "
751 "sampled_by=0.3.0.0-alpha-dev "
752 "listed=1 confirmed_on=2016-11-24T08:45:30 confirmed_idx=1 "
753 "pb_circ_attempts=5.000000 pb_circ_successes=5.000000 "
754 "pb_successful_circuits_closed=5.000000\n"
755 "Guard in=default rsa_id=E9025AD60D86875D5F11548D536CC6AF60F0EF5E "
756 "nickname=maibrunn sampled_on=2016-11-25T22:36:38 "
758 "sampled_by=0.3.0.0-alpha-dev listed=1\n"
759 "Guard in=default rsa_id=DCD30B90BA3A792DA75DC54A327EF353FB84C38E "
760 "nickname=Unnamed sampled_on=2016-11-25T14:34:00 "
762 "sampled_by=0.3.0.0-alpha-dev listed=1\n"
763 "Guard in=wobblesome rsa_id=7B700C0C207EBD0002E00F499BE265519AC3C25A "
764 "nickname=dc6jgk11 sampled_on=2016-11-28T11:50:13 "
766 "sampled_by=0.3.0.0-alpha-dev "
768 "Guard in=bridges rsa_id=8FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2E "
769 "bridge_addr=24.1.1.1:443 sampled_on=2016-11-25T06:44:14 "
771 "sampled_by=0.3.0.0-alpha-dev listed=1 "
772 "confirmed_on=2016-11-29T10:36:06 confirmed_idx=0 "
773 "pb_circ_attempts=8.000000 pb_circ_successes=8.000000 "
774 "pb_successful_circuits_closed=13.000000\n"
775 "Guard in=bridges rsa_id=5800000000000000000000000000000000000000 "
776 "bridge_addr=37.218.246.143:28366 "
777 "sampled_on=2016-11-18T15:07:34 sampled_idx=1 "
778 "sampled_by=0.3.0.0-alpha-dev listed=1\n");
781 config_free_lines(lines
);
784 UNMOCK(get_or_state
);
785 UNMOCK(entry_guard_is_listed
);
786 SMARTLIST_FOREACH(text
, char *, cp
, tor_free(cp
));
787 smartlist_free(text
);
792 test_entry_guard_parse_from_state_broken(void *arg
)
795 /* Here's a variation on the previous state. Every line but the first is
799 "Guard in=default rsa_id=214F44BD5B638E8C817D47FF7C97397790BF0345 "
800 "nickname=TotallyNinja sampled_on=2016-11-12T19:32:49 "
801 "sampled_by=0.3.0.0-alpha-dev "
803 /* No selection listed. */
804 "Guard rsa_id=052900AB0EA3ED54BAB84AE8A99E74E8693CE2B2 "
805 "nickname=5OfNovember sampled_on=2016-11-20T04:32:05 "
806 "sampled_by=0.3.0.0-alpha-dev "
807 "listed=1 confirmed_on=2016-11-22T08:13:28 confirmed_idx=0 "
808 "pb_circ_attempts=4.000000 pb_circ_successes=2.000000 "
809 "pb_successful_circuits_closed=2.000000\n"
810 /* Selection is "legacy"!! */
811 "Guard in=legacy rsa_id=7B700C0C207EBD0002E00F499BE265519AC3C25A "
812 "nickname=dc6jgk11 sampled_on=2016-11-28T11:50:13 "
813 "sampled_by=0.3.0.0-alpha-dev "
814 "listed=1 confirmed_on=2016-11-24T08:45:30 confirmed_idx=4 "
815 "pb_circ_attempts=5.000000 pb_circ_successes=5.000000 "
816 "pb_successful_circuits_closed=5.000000\n";
818 config_line_t
*lines
= NULL
;
819 or_state_t
*state
= tor_malloc_zero(sizeof(or_state_t
));
820 int r
= config_get_lines(STATE
, &lines
, 0);
825 get_or_state_replacement
);
827 tt_int_op(r
, OP_EQ
, 0);
830 state
->Guard
= lines
;
832 /* First, no-set case. we should get an error. */
833 r
= entry_guards_parse_state(state
, 0, &msg
);
834 tt_int_op(r
, OP_LT
, 0);
835 tt_ptr_op(msg
, OP_NE
, NULL
);
836 /* And we shouldn't have made anything. */
837 guard_selection_t
*gs_df
=
838 get_guard_selection_by_name("default", GS_TYPE_NORMAL
, 0);
839 tt_ptr_op(gs_df
, OP_EQ
, NULL
);
842 /* Now see about the set case (which shouldn't happen IRL) */
843 r
= entry_guards_parse_state(state
, 1, &msg
);
844 tt_int_op(r
, OP_LT
, 0);
845 tt_ptr_op(msg
, OP_NE
, NULL
);
846 gs_df
= get_guard_selection_by_name("default", GS_TYPE_NORMAL
, 0);
847 tt_ptr_op(gs_df
, OP_NE
, NULL
);
848 tt_int_op(smartlist_len(gs_df
->sampled_entry_guards
), OP_EQ
, 1);
851 config_free_lines(lines
);
854 UNMOCK(get_or_state
);
858 test_entry_guard_get_guard_selection_by_name(void *arg
)
861 guard_selection_t
*gs1
, *gs2
, *gs3
;
863 gs1
= get_guard_selection_by_name("unlikely", GS_TYPE_NORMAL
, 0);
864 tt_ptr_op(gs1
, OP_EQ
, NULL
);
865 gs1
= get_guard_selection_by_name("unlikely", GS_TYPE_NORMAL
, 1);
866 tt_ptr_op(gs1
, OP_NE
, NULL
);
867 gs2
= get_guard_selection_by_name("unlikely", GS_TYPE_NORMAL
, 1);
868 tt_assert(gs2
== gs1
);
869 gs2
= get_guard_selection_by_name("unlikely", GS_TYPE_NORMAL
, 0);
870 tt_assert(gs2
== gs1
);
872 gs2
= get_guard_selection_by_name("implausible", GS_TYPE_NORMAL
, 0);
873 tt_ptr_op(gs2
, OP_EQ
, NULL
);
874 gs2
= get_guard_selection_by_name("implausible", GS_TYPE_NORMAL
, 1);
875 tt_ptr_op(gs2
, OP_NE
, NULL
);
876 tt_assert(gs2
!= gs1
);
877 gs3
= get_guard_selection_by_name("implausible", GS_TYPE_NORMAL
, 0);
878 tt_assert(gs3
== gs2
);
880 gs3
= get_guard_selection_by_name("default", GS_TYPE_NORMAL
, 0);
881 tt_ptr_op(gs3
, OP_EQ
, NULL
);
882 gs3
= get_guard_selection_by_name("default", GS_TYPE_NORMAL
, 1);
883 tt_ptr_op(gs3
, OP_NE
, NULL
);
884 tt_assert(gs3
!= gs2
);
885 tt_assert(gs3
!= gs1
);
886 tt_assert(gs3
== get_guard_selection_info());
889 entry_guards_free_all();
893 test_entry_guard_choose_selection_initial(void *arg
)
895 /* Tests for picking our initial guard selection (based on having had
896 * no previous selection */
898 guard_selection_type_t type
= GS_TYPE_INFER
;
899 const char *name
= choose_guard_selection(get_options(),
900 dummy_consensus
, NULL
, &type
);
901 tt_str_op(name
, OP_EQ
, "default");
902 tt_int_op(type
, OP_EQ
, GS_TYPE_NORMAL
);
904 /* If we're using bridges, we get the bridge selection. */
905 get_options_mutable()->UseBridges
= 1;
906 name
= choose_guard_selection(get_options(),
907 dummy_consensus
, NULL
, &type
);
908 tt_str_op(name
, OP_EQ
, "bridges");
909 tt_int_op(type
, OP_EQ
, GS_TYPE_BRIDGE
);
910 get_options_mutable()->UseBridges
= 0;
912 /* If we discard >99% of our guards, though, we should be in the restricted
914 tt_assert(get_options_mutable()->EntryNodes
== NULL
);
915 get_options_mutable()->EntryNodes
= routerset_new();
916 routerset_parse(get_options_mutable()->EntryNodes
, "1.0.0.0/8", "foo");
917 name
= choose_guard_selection(get_options(),
918 dummy_consensus
, NULL
, &type
);
919 tt_str_op(name
, OP_EQ
, "restricted");
920 tt_int_op(type
, OP_EQ
, GS_TYPE_RESTRICTED
);
927 test_entry_guard_add_single_guard(void *arg
)
930 guard_selection_t
*gs
= guard_selection_new("default", GS_TYPE_NORMAL
);
932 /* 1: Add a single guard to the sample. */
933 node_t
*n1
= smartlist_get(big_fake_net_nodes
, 0);
934 time_t now
= approx_time();
935 tt_assert(n1
->is_possible_guard
== 1);
936 entry_guard_t
*g1
= entry_guard_add_to_sample(gs
, n1
);
939 /* Make sure its fields look right. */
940 tt_mem_op(n1
->identity
, OP_EQ
, g1
->identity
, DIGEST_LEN
);
941 tt_i64_op(g1
->sampled_on_date
, OP_GE
, now
- 12*86400);
942 tt_i64_op(g1
->sampled_on_date
, OP_LE
, now
);
943 tt_str_op(g1
->sampled_by_version
, OP_EQ
, VERSION
);
944 tt_uint_op(g1
->currently_listed
, OP_EQ
, 1);
945 tt_i64_op(g1
->confirmed_on_date
, OP_EQ
, 0);
946 tt_int_op(g1
->confirmed_idx
, OP_EQ
, -1);
947 tt_int_op(g1
->last_tried_to_connect
, OP_EQ
, 0);
948 tt_uint_op(g1
->is_reachable
, OP_EQ
, GUARD_REACHABLE_MAYBE
);
949 tt_i64_op(g1
->failing_since
, OP_EQ
, 0);
950 tt_uint_op(g1
->is_filtered_guard
, OP_EQ
, 1);
951 tt_uint_op(g1
->is_usable_filtered_guard
, OP_EQ
, 1);
952 tt_uint_op(g1
->is_primary
, OP_EQ
, 0);
953 tt_ptr_op(g1
->extra_state_fields
, OP_EQ
, NULL
);
955 /* Make sure it got added. */
956 tt_int_op(1, OP_EQ
, smartlist_len(gs
->sampled_entry_guards
));
957 tt_ptr_op(g1
, OP_EQ
, smartlist_get(gs
->sampled_entry_guards
, 0));
958 tt_ptr_op(g1
, OP_EQ
, get_sampled_guard_with_id(gs
, (uint8_t*)n1
->identity
));
959 const uint8_t bad_id
[20] = {0};
960 tt_ptr_op(NULL
, OP_EQ
, get_sampled_guard_with_id(gs
, bad_id
));
963 guard_selection_free(gs
);
967 test_entry_guard_node_filter(void *arg
)
970 guard_selection_t
*gs
= guard_selection_new("default", GS_TYPE_NORMAL
);
971 bridge_line_t
*bl
= NULL
;
973 /* Initialize a bunch of node objects that are all guards. */
976 entry_guard_t
*g
[NUM
];
978 for (i
=0; i
< NUM
; ++i
) {
979 n
[i
] = smartlist_get(big_fake_net_nodes
, i
*2); // even ones are guards.
980 g
[i
] = entry_guard_add_to_sample(gs
, n
[i
]);
982 // everything starts out filtered-in
983 tt_uint_op(g
[i
]->is_filtered_guard
, OP_EQ
, 1);
984 tt_uint_op(g
[i
]->is_usable_filtered_guard
, OP_EQ
, 1);
986 tt_int_op(num_reachable_filtered_guards(gs
, NULL
), OP_EQ
, NUM
);
988 /* Make sure refiltering doesn't hurt */
989 entry_guards_update_filtered_sets(gs
);
990 for (i
= 0; i
< NUM
; ++i
) {
991 tt_uint_op(g
[i
]->is_filtered_guard
, OP_EQ
, 1);
992 tt_uint_op(g
[i
]->is_usable_filtered_guard
, OP_EQ
, 1);
994 tt_int_op(num_reachable_filtered_guards(gs
, NULL
), OP_EQ
, NUM
);
996 /* Now start doing things to make the guards get filtered out, 1 by 1. */
999 g
[0]->currently_listed
= 0;
1001 /* 1: path bias says this guard is maybe eeeevil. */
1002 g
[1]->pb
.path_bias_disabled
= 1;
1004 /* 2: Unreachable address. */
1005 tor_addr_make_unspec(&n
[2]->rs
->ipv4_addr
);
1007 /* 3: ExcludeNodes */
1008 tor_addr_from_ipv4h(&n
[3]->rs
->ipv4_addr
, 0x90902020);
1009 routerset_free(get_options_mutable()->ExcludeNodes
);
1010 get_options_mutable()->ExcludeNodes
= routerset_new();
1011 routerset_parse(get_options_mutable()->ExcludeNodes
, "144.144.0.0/16", "");
1014 get_options_mutable()->UseBridges
= 1;
1015 sweep_bridge_list();
1016 bl
= tor_malloc_zero(sizeof(bridge_line_t
));
1017 tor_addr_copy(&bl
->addr
, &n
[4]->rs
->ipv4_addr
);
1018 bl
->port
= n
[4]->rs
->ipv4_orport
;
1019 memcpy(bl
->digest
, n
[4]->identity
, 20);
1020 bridge_add_from_config(bl
);
1021 bl
= NULL
; // prevent free.
1022 get_options_mutable()->UseBridges
= 0;
1024 /* 5: Unreachable. This stays in the filter, but isn't in usable-filtered */
1025 g
[5]->last_tried_to_connect
= approx_time(); // prevent retry.
1026 g
[5]->is_reachable
= GUARD_REACHABLE_NO
;
1030 /* Now refilter and inspect. */
1031 entry_guards_update_filtered_sets(gs
);
1032 for (i
= 0; i
< NUM
; ++i
) {
1033 tt_assert(g
[i
]->is_filtered_guard
== (i
== 5 || i
== 6));
1034 tt_assert(g
[i
]->is_usable_filtered_guard
== (i
== 6));
1036 tt_int_op(num_reachable_filtered_guards(gs
, NULL
), OP_EQ
, 1);
1038 /* Now make sure we have no live consensus, and no nodes. Nothing should
1039 * pass the filter any more. */
1040 tor_free(dummy_consensus
);
1041 dummy_consensus
= NULL
;
1042 SMARTLIST_FOREACH(big_fake_net_nodes
, node_t
*, node
, {
1043 memset(node
->identity
, 0xff, 20);
1045 entry_guards_update_filtered_sets(gs
);
1046 for (i
= 0; i
< NUM
; ++i
) {
1047 tt_uint_op(g
[i
]->is_filtered_guard
, OP_EQ
, 0);
1048 tt_uint_op(g
[i
]->is_usable_filtered_guard
, OP_EQ
, 0);
1050 tt_int_op(num_reachable_filtered_guards(gs
, NULL
), OP_EQ
, 0);
1053 guard_selection_free(gs
);
1059 test_entry_guard_expand_sample(void *arg
)
1062 guard_selection_t
*gs
= guard_selection_new("default", GS_TYPE_NORMAL
);
1063 digestmap_t
*node_by_id
= digestmap_new();
1065 entry_guard_t
*guard
= entry_guards_expand_sample(gs
);
1066 tt_assert(guard
); // the last guard returned.
1068 // Every sampled guard here should be filtered and reachable for now.
1069 tt_int_op(smartlist_len(gs
->sampled_entry_guards
), OP_EQ
,
1070 num_reachable_filtered_guards(gs
, NULL
));
1072 /* Make sure we got the right number. */
1073 tt_int_op(DFLT_MIN_FILTERED_SAMPLE_SIZE
, OP_EQ
,
1074 num_reachable_filtered_guards(gs
, NULL
));
1076 // Make sure everything we got was from our fake node list, and everything
1078 SMARTLIST_FOREACH_BEGIN(gs
->sampled_entry_guards
, entry_guard_t
*, g
) {
1079 const node_t
*n
= bfn_mock_node_get_by_id(g
->identity
);
1081 tt_ptr_op(NULL
, OP_EQ
, digestmap_get(node_by_id
, g
->identity
));
1082 digestmap_set(node_by_id
, g
->identity
, (void*) n
);
1083 int idx
= smartlist_pos(big_fake_net_nodes
, n
);
1084 // The even ones are the guards; make sure we got guards.
1085 tt_int_op(idx
& 1, OP_EQ
, 0);
1086 } SMARTLIST_FOREACH_END(g
);
1088 // Nothing became unusable/unfiltered, so a subsequent expand should
1090 guard
= entry_guards_expand_sample(gs
);
1091 tt_ptr_op(guard
, OP_EQ
, NULL
); // no guard was added.
1092 tt_int_op(DFLT_MIN_FILTERED_SAMPLE_SIZE
, OP_EQ
,
1093 num_reachable_filtered_guards(gs
, NULL
));
1095 // Make a few guards unreachable.
1096 guard
= smartlist_get(gs
->sampled_entry_guards
, 0);
1097 guard
->is_usable_filtered_guard
= 0;
1098 guard
= smartlist_get(gs
->sampled_entry_guards
, 1);
1099 guard
->is_usable_filtered_guard
= 0;
1100 guard
= smartlist_get(gs
->sampled_entry_guards
, 2);
1101 guard
->is_usable_filtered_guard
= 0;
1102 tt_int_op(DFLT_MIN_FILTERED_SAMPLE_SIZE
- 3, OP_EQ
,
1103 num_reachable_filtered_guards(gs
, NULL
));
1105 // This time, expanding the sample will add some more guards.
1106 guard
= entry_guards_expand_sample(gs
);
1107 tt_assert(guard
); // no guard was added.
1108 tt_int_op(DFLT_MIN_FILTERED_SAMPLE_SIZE
, OP_EQ
,
1109 num_reachable_filtered_guards(gs
, NULL
));
1110 tt_int_op(smartlist_len(gs
->sampled_entry_guards
), OP_EQ
,
1111 num_reachable_filtered_guards(gs
, NULL
)+3);
1113 // Still idempotent.
1114 guard
= entry_guards_expand_sample(gs
);
1115 tt_ptr_op(guard
, OP_EQ
, NULL
); // no guard was added.
1116 tt_int_op(DFLT_MIN_FILTERED_SAMPLE_SIZE
, OP_EQ
,
1117 num_reachable_filtered_guards(gs
, NULL
));
1119 // Now, do a nasty trick: tell the filter to exclude 31/32 of the guards.
1120 // This will cause the sample size to get reeeeally huge, while the
1121 // filtered sample size grows only slowly.
1122 routerset_free(get_options_mutable()->ExcludeNodes
);
1123 get_options_mutable()->ExcludeNodes
= routerset_new();
1124 routerset_parse(get_options_mutable()->ExcludeNodes
, "144.144.0.0/16", "");
1125 SMARTLIST_FOREACH(big_fake_net_nodes
, node_t
*, n
, {
1126 if (n_sl_idx
% 64 != 0) {
1127 tor_addr_from_ipv4h(&n
->rs
->ipv4_addr
, 0x90903030);
1130 entry_guards_update_filtered_sets(gs
);
1132 // Surely (p ~ 1-2**-60), one of our guards has been excluded.
1133 tt_int_op(num_reachable_filtered_guards(gs
, NULL
), OP_LT
,
1134 DFLT_MIN_FILTERED_SAMPLE_SIZE
);
1136 // Try to regenerate the guards.
1137 guard
= entry_guards_expand_sample(gs
);
1138 tt_assert(guard
); // no guard was added.
1140 /* this time, it's possible that we didn't add enough sampled guards. */
1141 tt_int_op(num_reachable_filtered_guards(gs
, NULL
), OP_LE
,
1142 DFLT_MIN_FILTERED_SAMPLE_SIZE
);
1143 /* but we definitely didn't exceed the sample maximum. */
1144 const int n_guards
= 271 / 2;
1145 tt_int_op(smartlist_len(gs
->sampled_entry_guards
), OP_LE
,
1146 (int)(n_guards
* .3));
1149 guard_selection_free(gs
);
1150 digestmap_free(node_by_id
, NULL
);
1154 test_entry_guard_expand_sample_small_net(void *arg
)
1157 guard_selection_t
*gs
= guard_selection_new("default", GS_TYPE_NORMAL
);
1159 /* Fun corner case: not enough guards to make up our whole sample size. */
1160 SMARTLIST_FOREACH(big_fake_net_nodes
, node_t
*, n
, {
1161 if (n_sl_idx
>= 15) {
1163 SMARTLIST_DEL_CURRENT(big_fake_net_nodes
, n
);
1165 tor_addr_make_unspec(&n
->rs
->ipv4_addr
); // make the filter reject this.
1169 entry_guard_t
*guard
= entry_guards_expand_sample(gs
);
1170 tt_assert(guard
); // the last guard returned -- some guard was added.
1171 // half the nodes are guards, so we have 8 guards left. The set
1172 // is small, so we sampled everything.
1173 tt_int_op(smartlist_len(gs
->sampled_entry_guards
), OP_EQ
, 8);
1174 tt_int_op(num_reachable_filtered_guards(gs
, NULL
), OP_EQ
, 0);
1176 guard_selection_free(gs
);
1180 test_entry_guard_update_from_consensus_status(void *arg
)
1182 /* Here we're going to have some nodes become un-guardy, and say we got a
1183 * new consensus. This should cause those nodes to get detected as
1188 time_t start
= approx_time();
1189 guard_selection_t
*gs
= guard_selection_new("default", GS_TYPE_NORMAL
);
1190 networkstatus_t
*ns_tmp
= NULL
;
1192 /* Don't randomly backdate stuff; it will make correctness harder to check.*/
1193 MOCK(randomize_time
, mock_randomize_time_no_randomization
);
1195 /* First, sample some guards. */
1196 entry_guards_expand_sample(gs
);
1197 int n_sampled_pre
= smartlist_len(gs
->sampled_entry_guards
);
1198 int n_filtered_pre
= num_reachable_filtered_guards(gs
, NULL
);
1199 tt_i64_op(n_sampled_pre
, OP_EQ
, n_filtered_pre
);
1200 tt_i64_op(n_sampled_pre
, OP_GT
, 10);
1202 /* At this point, it should be a no-op to do this: */
1203 sampled_guards_update_from_consensus(gs
);
1205 /* Now let's make some of our guards become unlisted. The easiest way to
1206 * do that would be to take away their guard flag. */
1207 for (i
= 0; i
< 5; ++i
) {
1208 entry_guard_t
*g
= smartlist_get(gs
->sampled_entry_guards
, i
);
1209 node_t
*n
= (node_t
*) bfn_mock_node_get_by_id(g
->identity
);
1211 n
->is_possible_guard
= 0;
1214 update_approx_time(start
+ 30);
1216 /* try this with no live networkstatus. Nothing should happen! */
1217 ns_tmp
= dummy_consensus
;
1218 dummy_consensus
= NULL
;
1219 sampled_guards_update_from_consensus(gs
);
1220 tt_i64_op(smartlist_len(gs
->sampled_entry_guards
), OP_EQ
, n_sampled_pre
);
1221 tt_i64_op(num_reachable_filtered_guards(gs
, NULL
), OP_EQ
, n_filtered_pre
);
1222 /* put the networkstatus back. */
1223 dummy_consensus
= ns_tmp
;
1227 /* Now those guards should become unlisted, and drop off the filter, but
1228 * stay in the sample. */
1229 update_approx_time(start
+ 60);
1230 sampled_guards_update_from_consensus(gs
);
1232 tt_i64_op(smartlist_len(gs
->sampled_entry_guards
), OP_EQ
, n_sampled_pre
);
1233 tt_i64_op(num_reachable_filtered_guards(gs
, NULL
), OP_EQ
, n_filtered_pre
-5);
1234 for (i
= 0; i
< 5; ++i
) {
1235 entry_guard_t
*g
= smartlist_get(gs
->sampled_entry_guards
, i
);
1236 tt_assert(! g
->currently_listed
);
1237 tt_i64_op(g
->unlisted_since_date
, OP_EQ
, start
+60);
1239 for (i
= 5; i
< n_sampled_pre
; ++i
) {
1240 entry_guard_t
*g
= smartlist_get(gs
->sampled_entry_guards
, i
);
1241 tt_assert(g
->currently_listed
);
1242 tt_i64_op(g
->unlisted_since_date
, OP_EQ
, 0);
1245 /* Now re-list one, and remove one completely. */
1247 entry_guard_t
*g
= smartlist_get(gs
->sampled_entry_guards
, 0);
1248 node_t
*n
= (node_t
*) bfn_mock_node_get_by_id(g
->identity
);
1250 n
->is_possible_guard
= 1;
1253 /* try removing the node, to make sure we don't crash on an absent node
1255 entry_guard_t
*g
= smartlist_get(gs
->sampled_entry_guards
, 5);
1256 node_t
*n
= (node_t
*) bfn_mock_node_get_by_id(g
->identity
);
1258 smartlist_remove(big_fake_net_nodes
, n
);
1261 update_approx_time(start
+ 300);
1262 sampled_guards_update_from_consensus(gs
);
1264 /* guards 1..5 are now unlisted; 0,6,7.. are listed. */
1265 tt_i64_op(smartlist_len(gs
->sampled_entry_guards
), OP_EQ
, n_sampled_pre
);
1266 for (i
= 1; i
< 6; ++i
) {
1267 entry_guard_t
*g
= smartlist_get(gs
->sampled_entry_guards
, i
);
1268 tt_assert(! g
->currently_listed
);
1270 tt_i64_op(g
->unlisted_since_date
, OP_EQ
, start
+300);
1272 tt_i64_op(g
->unlisted_since_date
, OP_EQ
, start
+60);
1274 for (i
= 0; i
< n_sampled_pre
; i
= (!i
) ? 6 : i
+1) { /* 0,6,7,8, ... */
1275 entry_guard_t
*g
= smartlist_get(gs
->sampled_entry_guards
, i
);
1276 tt_assert(g
->currently_listed
);
1277 tt_i64_op(g
->unlisted_since_date
, OP_EQ
, 0);
1281 tor_free(ns_tmp
); /* in case we couldn't put it back */
1282 guard_selection_free(gs
);
1283 UNMOCK(randomize_time
);
1287 test_entry_guard_update_from_consensus_repair(void *arg
)
1289 /* Here we'll make sure that our code to repair the unlisted-since
1290 * times is correct. */
1294 time_t start
= approx_time();
1295 guard_selection_t
*gs
= guard_selection_new("default", GS_TYPE_NORMAL
);
1297 /* Don't randomly backdate stuff; it will make correctness harder to check.*/
1298 MOCK(randomize_time
, mock_randomize_time_no_randomization
);
1300 /* First, sample some guards. */
1301 entry_guards_expand_sample(gs
);
1302 int n_sampled_pre
= smartlist_len(gs
->sampled_entry_guards
);
1303 int n_filtered_pre
= num_reachable_filtered_guards(gs
, NULL
);
1304 tt_i64_op(n_sampled_pre
, OP_EQ
, n_filtered_pre
);
1305 tt_i64_op(n_sampled_pre
, OP_GT
, 10);
1307 /* Now corrupt the list a bit. Call some unlisted-since-never, and some
1308 * listed-and-unlisted-since-a-time. */
1309 update_approx_time(start
+ 300);
1310 for (i
= 0; i
< 3; ++i
) {
1311 /* these will get a date. */
1312 entry_guard_t
*g
= smartlist_get(gs
->sampled_entry_guards
, i
);
1313 node_t
*n
= (node_t
*) bfn_mock_node_get_by_id(g
->identity
);
1315 n
->is_possible_guard
= 0;
1316 g
->currently_listed
= 0;
1318 for (i
= 3; i
< 6; ++i
) {
1319 /* these will become listed. */
1320 entry_guard_t
*g
= smartlist_get(gs
->sampled_entry_guards
, i
);
1321 g
->unlisted_since_date
= start
+100;
1323 setup_full_capture_of_logs(LOG_WARN
);
1324 sampled_guards_update_from_consensus(gs
);
1325 expect_log_msg_containing(
1326 "was listed, but with unlisted_since_date set");
1327 expect_log_msg_containing(
1328 "was unlisted, but with unlisted_since_date unset");
1329 teardown_capture_of_logs();
1331 tt_int_op(smartlist_len(gs
->sampled_entry_guards
), OP_EQ
, n_sampled_pre
);
1332 tt_int_op(num_reachable_filtered_guards(gs
, NULL
), OP_EQ
, n_filtered_pre
-3);
1333 for (i
= 3; i
< n_sampled_pre
; ++i
) {
1334 /* these will become listed. */
1335 entry_guard_t
*g
= smartlist_get(gs
->sampled_entry_guards
, i
);
1337 tt_assert(! g
->currently_listed
);
1338 tt_i64_op(g
->unlisted_since_date
, OP_EQ
, start
+300);
1340 tt_assert(g
->currently_listed
);
1341 tt_i64_op(g
->unlisted_since_date
, OP_EQ
, 0);
1346 teardown_capture_of_logs();
1347 guard_selection_free(gs
);
1348 UNMOCK(randomize_time
);
1352 test_entry_guard_update_from_consensus_remove(void *arg
)
1354 /* Now let's check the logic responsible for removing guards from the
1355 * sample entirely. */
1359 guard_selection_t
*gs
= guard_selection_new("default", GS_TYPE_NORMAL
);
1360 smartlist_t
*keep_ids
= smartlist_new();
1361 smartlist_t
*remove_ids
= smartlist_new();
1363 /* Don't randomly backdate stuff; it will make correctness harder to check.*/
1364 MOCK(randomize_time
, mock_randomize_time_no_randomization
);
1366 /* First, sample some guards. */
1367 entry_guards_expand_sample(gs
);
1368 int n_sampled_pre
= smartlist_len(gs
->sampled_entry_guards
);
1369 int n_filtered_pre
= num_reachable_filtered_guards(gs
, NULL
);
1370 tt_i64_op(n_sampled_pre
, OP_EQ
, n_filtered_pre
);
1371 tt_i64_op(n_sampled_pre
, OP_GT
, 10);
1373 const time_t one_day_ago
= approx_time() - 1*24*60*60;
1374 const time_t one_year_ago
= approx_time() - 365*24*60*60;
1375 const time_t two_years_ago
= approx_time() - 2*365*24*60*60;
1376 /* 0: unlisted for a day. (keep this) */
1378 entry_guard_t
*g
= smartlist_get(gs
->sampled_entry_guards
, 0);
1379 node_t
*n
= (node_t
*) bfn_mock_node_get_by_id(g
->identity
);
1381 n
->is_possible_guard
= 0;
1382 g
->currently_listed
= 0;
1383 g
->unlisted_since_date
= one_day_ago
;
1384 smartlist_add(keep_ids
, tor_memdup(g
->identity
, 20));
1386 /* 1: unlisted for a year. (remove this) */
1388 entry_guard_t
*g
= smartlist_get(gs
->sampled_entry_guards
, 1);
1389 node_t
*n
= (node_t
*) bfn_mock_node_get_by_id(g
->identity
);
1391 n
->is_possible_guard
= 0;
1392 g
->currently_listed
= 0;
1393 g
->unlisted_since_date
= one_year_ago
;
1394 smartlist_add(remove_ids
, tor_memdup(g
->identity
, 20));
1396 /* 2: added a day ago, never confirmed. (keep this) */
1398 entry_guard_t
*g
= smartlist_get(gs
->sampled_entry_guards
, 2);
1399 g
->sampled_on_date
= one_day_ago
;
1400 smartlist_add(keep_ids
, tor_memdup(g
->identity
, 20));
1402 /* 3: added a year ago, never confirmed. (remove this) */
1404 entry_guard_t
*g
= smartlist_get(gs
->sampled_entry_guards
, 3);
1405 g
->sampled_on_date
= one_year_ago
;
1406 smartlist_add(remove_ids
, tor_memdup(g
->identity
, 20));
1408 /* 4: added two year ago, confirmed yesterday, primary. (keep this.) */
1410 entry_guard_t
*g
= smartlist_get(gs
->sampled_entry_guards
, 4);
1411 g
->sampled_on_date
= one_year_ago
;
1412 g
->confirmed_on_date
= one_day_ago
;
1413 g
->confirmed_idx
= 0;
1415 smartlist_add(gs
->confirmed_entry_guards
, g
);
1416 smartlist_add(gs
->primary_entry_guards
, g
);
1417 smartlist_add(keep_ids
, tor_memdup(g
->identity
, 20));
1419 /* 5: added two years ago, confirmed a year ago, primary. (remove this) */
1421 entry_guard_t
*g
= smartlist_get(gs
->sampled_entry_guards
, 5);
1422 g
->sampled_on_date
= two_years_ago
;
1423 g
->confirmed_on_date
= one_year_ago
;
1424 g
->confirmed_idx
= 1;
1426 smartlist_add(gs
->confirmed_entry_guards
, g
);
1427 smartlist_add(gs
->primary_entry_guards
, g
);
1428 smartlist_add(remove_ids
, tor_memdup(g
->identity
, 20));
1431 sampled_guards_update_from_consensus(gs
);
1433 /* Did we remove the right ones? */
1434 SMARTLIST_FOREACH(keep_ids
, uint8_t *, id
, {
1435 tt_assert(get_sampled_guard_with_id(gs
, id
) != NULL
);
1437 SMARTLIST_FOREACH(remove_ids
, uint8_t *, id
, {
1438 tt_want(get_sampled_guard_with_id(gs
, id
) == NULL
);
1441 /* Did we remove the right number? */
1442 tt_int_op(smartlist_len(gs
->sampled_entry_guards
), OP_EQ
, n_sampled_pre
- 3);
1445 guard_selection_free(gs
);
1446 UNMOCK(randomize_time
);
1447 SMARTLIST_FOREACH(keep_ids
, char *, cp
, tor_free(cp
));
1448 SMARTLIST_FOREACH(remove_ids
, char *, cp
, tor_free(cp
));
1449 smartlist_free(keep_ids
);
1450 smartlist_free(remove_ids
);
1454 test_entry_guard_confirming_guards(void *arg
)
1457 /* Now let's check the logic responsible for manipulating the list
1458 * of confirmed guards */
1459 guard_selection_t
*gs
= guard_selection_new("default", GS_TYPE_NORMAL
);
1460 MOCK(randomize_time
, mock_randomize_time_no_randomization
);
1462 /* Create the sample. */
1463 entry_guards_expand_sample(gs
);
1465 /* Confirm a few guards. */
1466 time_t start
= approx_time();
1467 entry_guard_t
*g1
= smartlist_get(gs
->sampled_entry_guards
, 0);
1468 entry_guard_t
*g2
= smartlist_get(gs
->sampled_entry_guards
, 1);
1469 entry_guard_t
*g3
= smartlist_get(gs
->sampled_entry_guards
, 8);
1470 make_guard_confirmed(gs
, g2
);
1471 update_approx_time(start
+ 10);
1472 make_guard_confirmed(gs
, g1
);
1473 make_guard_confirmed(gs
, g3
);
1475 /* Were the correct dates and indices fed in? */
1476 tt_int_op(g1
->confirmed_idx
, OP_EQ
, 1);
1477 tt_int_op(g2
->confirmed_idx
, OP_EQ
, 0);
1478 tt_int_op(g3
->confirmed_idx
, OP_EQ
, 2);
1479 tt_i64_op(g1
->confirmed_on_date
, OP_EQ
, start
+10);
1480 tt_i64_op(g2
->confirmed_on_date
, OP_EQ
, start
);
1481 tt_i64_op(g3
->confirmed_on_date
, OP_EQ
, start
+10);
1482 tt_ptr_op(smartlist_get(gs
->confirmed_entry_guards
, 0), OP_EQ
, g1
);
1483 tt_ptr_op(smartlist_get(gs
->confirmed_entry_guards
, 1), OP_EQ
, g2
);
1484 tt_ptr_op(smartlist_get(gs
->confirmed_entry_guards
, 2), OP_EQ
, g3
);
1486 /* Now make sure we can regenerate the confirmed_entry_guards list. */
1487 smartlist_clear(gs
->confirmed_entry_guards
);
1488 g2
->confirmed_idx
= 0;
1489 g1
->confirmed_idx
= 10;
1490 g3
->confirmed_idx
= 100;
1491 entry_guards_update_confirmed(gs
);
1492 tt_int_op(g1
->confirmed_idx
, OP_EQ
, 1);
1493 tt_int_op(g2
->confirmed_idx
, OP_EQ
, 0);
1494 tt_int_op(g3
->confirmed_idx
, OP_EQ
, 2);
1495 tt_ptr_op(smartlist_get(gs
->confirmed_entry_guards
, 0), OP_EQ
, g1
);
1496 tt_ptr_op(smartlist_get(gs
->confirmed_entry_guards
, 1), OP_EQ
, g2
);
1497 tt_ptr_op(smartlist_get(gs
->confirmed_entry_guards
, 2), OP_EQ
, g3
);
1499 /* Now make sure we can regenerate the confirmed_entry_guards list if
1500 * the indices are messed up. */
1501 g1
->confirmed_idx
= g2
->confirmed_idx
= g3
->confirmed_idx
= 999;
1502 smartlist_clear(gs
->confirmed_entry_guards
);
1503 entry_guards_update_confirmed(gs
);
1504 tt_int_op(g1
->confirmed_idx
, OP_GE
, 0);
1505 tt_int_op(g2
->confirmed_idx
, OP_GE
, 0);
1506 tt_int_op(g3
->confirmed_idx
, OP_GE
, 0);
1507 tt_int_op(g1
->confirmed_idx
, OP_LE
, 2);
1508 tt_int_op(g2
->confirmed_idx
, OP_LE
, 2);
1509 tt_int_op(g3
->confirmed_idx
, OP_LE
, 2);
1510 g1
= smartlist_get(gs
->confirmed_entry_guards
, 0);
1511 g2
= smartlist_get(gs
->confirmed_entry_guards
, 1);
1512 g3
= smartlist_get(gs
->confirmed_entry_guards
, 2);
1513 tt_int_op(g1
->sampled_idx
, OP_EQ
, 0);
1514 tt_int_op(g2
->sampled_idx
, OP_EQ
, 1);
1515 tt_int_op(g3
->sampled_idx
, OP_EQ
, 8);
1516 tt_assert(g1
!= g2
);
1517 tt_assert(g1
!= g3
);
1518 tt_assert(g2
!= g3
);
1521 UNMOCK(randomize_time
);
1522 guard_selection_free(gs
);
1526 test_entry_guard_sample_reachable_filtered(void *arg
)
1529 guard_selection_t
*gs
= guard_selection_new("default", GS_TYPE_NORMAL
);
1530 entry_guards_expand_sample(gs
);
1532 /* We've got a sampled list now; let's make one non-usable-filtered; some
1533 * confirmed, some primary, some pending.
1535 int n_guards
= smartlist_len(gs
->sampled_entry_guards
);
1536 tt_int_op(n_guards
, OP_GT
, 10);
1538 g
= smartlist_get(gs
->sampled_entry_guards
, 0);
1540 g
= smartlist_get(gs
->sampled_entry_guards
, 1);
1541 make_guard_confirmed(gs
, g
);
1542 g
= smartlist_get(gs
->sampled_entry_guards
, 2);
1544 g
= smartlist_get(gs
->sampled_entry_guards
, 3);
1545 g
->pb
.path_bias_disabled
= 1;
1547 entry_guards_update_filtered_sets(gs
);
1548 gs
->primary_guards_up_to_date
= 1;
1549 tt_int_op(num_reachable_filtered_guards(gs
, NULL
), OP_EQ
, n_guards
- 1);
1550 tt_int_op(smartlist_len(gs
->sampled_entry_guards
), OP_EQ
, n_guards
);
1552 // +1 since the one we made disabled will make another one get added.
1555 /* Try a bunch of selections. */
1560 { SAMPLE_EXCLUDE_CONFIRMED
, 1 },
1561 { SAMPLE_EXCLUDE_PRIMARY
|SAMPLE_NO_UPDATE_PRIMARY
, 2 },
1562 { SAMPLE_EXCLUDE_PENDING
, 0 },
1566 for (j
= 0; tests
[j
].flag
>= 0; ++j
) {
1567 const int excluded_flags
= tests
[j
].flag
;
1568 const int excluded_idx
= tests
[j
].idx
;
1569 g
= first_reachable_filtered_entry_guard(gs
, NULL
, excluded_flags
);
1571 int pos
= smartlist_pos(gs
->sampled_entry_guards
, g
);
1572 tt_int_op(smartlist_len(gs
->sampled_entry_guards
), OP_EQ
, n_guards
);
1573 const int should_be_set
= (pos
!= excluded_idx
&&
1574 pos
!= 3); // filtered out.
1575 tt_int_op(1, OP_EQ
, should_be_set
);
1579 guard_selection_free(gs
);
1583 test_entry_guard_sample_reachable_filtered_empty(void *arg
)
1586 guard_selection_t
*gs
= guard_selection_new("default", GS_TYPE_NORMAL
);
1587 /* What if we try to sample from a set of 0? */
1588 SMARTLIST_FOREACH(big_fake_net_nodes
, node_t
*, n
,
1589 n
->is_possible_guard
= 0);
1591 entry_guard_t
*g
= first_reachable_filtered_entry_guard(gs
, NULL
, 0);
1592 tt_ptr_op(g
, OP_EQ
, NULL
);
1595 guard_selection_free(gs
);
1599 test_entry_guard_retry_unreachable(void *arg
)
1602 guard_selection_t
*gs
= guard_selection_new("default", GS_TYPE_NORMAL
);
1604 entry_guards_expand_sample(gs
);
1605 /* Let's say that we have two guards, and they're down.
1607 time_t start
= approx_time();
1608 entry_guard_t
*g1
= smartlist_get(gs
->sampled_entry_guards
, 0);
1609 entry_guard_t
*g2
= smartlist_get(gs
->sampled_entry_guards
, 1);
1610 entry_guard_t
*g3
= smartlist_get(gs
->sampled_entry_guards
, 2);
1611 g1
->is_reachable
= GUARD_REACHABLE_NO
;
1612 g2
->is_reachable
= GUARD_REACHABLE_NO
;
1614 g1
->failing_since
= g2
->failing_since
= start
;
1615 g1
->last_tried_to_connect
= g2
->last_tried_to_connect
= start
;
1617 /* Wait 5 minutes. Nothing will get retried. */
1618 update_approx_time(start
+ 5 * 60);
1619 entry_guard_consider_retry(g1
);
1620 entry_guard_consider_retry(g2
);
1621 entry_guard_consider_retry(g3
); // just to make sure this doesn't crash.
1622 tt_int_op(g1
->is_reachable
, OP_EQ
, GUARD_REACHABLE_NO
);
1623 tt_int_op(g2
->is_reachable
, OP_EQ
, GUARD_REACHABLE_NO
);
1624 tt_int_op(g3
->is_reachable
, OP_EQ
, GUARD_REACHABLE_MAYBE
);
1626 /* After 30 min, the primary one gets retried */
1627 update_approx_time(start
+ 35 * 60);
1628 entry_guard_consider_retry(g1
);
1629 entry_guard_consider_retry(g2
);
1630 tt_int_op(g1
->is_reachable
, OP_EQ
, GUARD_REACHABLE_MAYBE
);
1631 tt_int_op(g2
->is_reachable
, OP_EQ
, GUARD_REACHABLE_NO
);
1633 g1
->is_reachable
= GUARD_REACHABLE_NO
;
1634 g1
->last_tried_to_connect
= start
+ 55*60;
1636 /* After 1 hour, we'll retry the nonprimary one. */
1637 update_approx_time(start
+ 61 * 60);
1638 entry_guard_consider_retry(g1
);
1639 entry_guard_consider_retry(g2
);
1640 tt_int_op(g1
->is_reachable
, OP_EQ
, GUARD_REACHABLE_NO
);
1641 tt_int_op(g2
->is_reachable
, OP_EQ
, GUARD_REACHABLE_MAYBE
);
1643 g2
->is_reachable
= GUARD_REACHABLE_NO
;
1644 g2
->last_tried_to_connect
= start
+ 61*60;
1646 /* And then the primary one again. */
1647 update_approx_time(start
+ 66 * 60);
1648 entry_guard_consider_retry(g1
);
1649 entry_guard_consider_retry(g2
);
1650 tt_int_op(g1
->is_reachable
, OP_EQ
, GUARD_REACHABLE_MAYBE
);
1651 tt_int_op(g2
->is_reachable
, OP_EQ
, GUARD_REACHABLE_NO
);
1654 guard_selection_free(gs
);
1658 test_entry_guard_manage_primary(void *arg
)
1661 guard_selection_t
*gs
= guard_selection_new("default", GS_TYPE_NORMAL
);
1662 smartlist_t
*prev_guards
= smartlist_new();
1664 /* If no guards are confirmed, we should pick a few reachable guards and
1665 * call them all primary. But not confirmed.*/
1666 entry_guards_update_primary(gs
);
1667 int n_primary
= smartlist_len(gs
->primary_entry_guards
);
1668 tt_int_op(n_primary
, OP_GE
, 1);
1669 SMARTLIST_FOREACH(gs
->primary_entry_guards
, entry_guard_t
*, g
, {
1670 tt_assert(g
->is_primary
);
1671 tt_assert(g
->confirmed_idx
== -1);
1674 /* Calling it a second time should leave the guards unchanged. */
1675 smartlist_add_all(prev_guards
, gs
->primary_entry_guards
);
1676 entry_guards_update_primary(gs
);
1677 tt_int_op(smartlist_len(gs
->primary_entry_guards
), OP_EQ
, n_primary
);
1678 SMARTLIST_FOREACH(gs
->primary_entry_guards
, entry_guard_t
*, g
, {
1679 tt_ptr_op(g
, OP_EQ
, smartlist_get(prev_guards
, g_sl_idx
));
1683 * If we have one confirmed guard, that guards becomes the first primary
1684 * only if its sampled_idx is smaller
1687 /* find a non-primary guard... it should have a sampled_idx higher than
1688 * existing primary guards */
1689 entry_guard_t
*confirmed
= NULL
;
1690 SMARTLIST_FOREACH(gs
->sampled_entry_guards
, entry_guard_t
*, g
, {
1691 if (! g
->is_primary
) {
1696 tt_assert(confirmed
);
1697 /* make it confirmed. */
1698 make_guard_confirmed(gs
, confirmed
);
1699 /* update the list... */
1700 smartlist_clear(prev_guards
);
1701 smartlist_add_all(prev_guards
, gs
->primary_entry_guards
);
1702 entry_guards_update_primary(gs
);
1704 /* the confirmed guard should be at the end of the primary list! Hopefully,
1705 * one of the primary guards with a lower sampled_idx will confirm soon :)
1706 * Doing this won't make the client switches between primaries depending on
1707 * the order of confirming events */
1708 tt_int_op(smartlist_len(gs
->primary_entry_guards
), OP_EQ
, n_primary
);
1709 tt_ptr_op(smartlist_get(gs
->primary_entry_guards
,
1710 smartlist_len(gs
->primary_entry_guards
)-1), OP_EQ
, confirmed
);
1712 entry_guard_t
*prev_last_guard
= smartlist_get(prev_guards
, n_primary
-1);
1713 tt_assert(! prev_last_guard
->is_primary
);
1716 /* Calling it a fourth time should leave the guards unchanged. */
1717 smartlist_clear(prev_guards
);
1718 smartlist_add_all(prev_guards
, gs
->primary_entry_guards
);
1719 entry_guards_update_primary(gs
);
1720 tt_int_op(smartlist_len(gs
->primary_entry_guards
), OP_EQ
, n_primary
);
1721 SMARTLIST_FOREACH(gs
->primary_entry_guards
, entry_guard_t
*, g
, {
1722 tt_ptr_op(g
, OP_EQ
, smartlist_get(prev_guards
, g_sl_idx
));
1725 /* Do some dirinfo checks */
1727 /* Check that we have all required dirinfo for the primaries (that's done
1728 * in big_fake_network_setup()) */
1729 char *dir_info_str
=
1730 guard_selection_get_err_str_if_dir_info_missing(gs
, 0, 0, 0);
1731 tt_assert(!dir_info_str
);
1733 /* Now artificially remove the first primary's descriptor and re-check */
1734 entry_guard_t
*first_primary
;
1735 first_primary
= smartlist_get(gs
->primary_entry_guards
, 0);
1736 /* Change the first primary's identity digest so that the mocked functions
1737 * can't find its descriptor */
1738 memset(first_primary
->identity
, 9, sizeof(first_primary
->identity
));
1739 dir_info_str
=guard_selection_get_err_str_if_dir_info_missing(gs
, 1, 2, 3);
1740 tt_str_op(dir_info_str
, OP_EQ
,
1741 "We're missing descriptors for 1/2 of our primary entry guards "
1742 "(total microdescriptors: 2/3). That's ok. We will try to fetch "
1743 "missing descriptors soon.");
1744 tor_free(dir_info_str
);
1748 guard_selection_free(gs
);
1749 smartlist_free(prev_guards
);
1753 test_entry_guard_guard_preferred(void *arg
)
1756 entry_guard_t
*g1
= tor_malloc_zero(sizeof(entry_guard_t
));
1757 entry_guard_t
*g2
= tor_malloc_zero(sizeof(entry_guard_t
));
1759 g1
->confirmed_idx
= g2
->confirmed_idx
= -1;
1760 g1
->last_tried_to_connect
= approx_time();
1761 g2
->last_tried_to_connect
= approx_time();
1763 tt_int_op(0, OP_EQ
, entry_guard_has_higher_priority(g1
, g1
));
1765 /* Neither is pending; priorities equal. */
1766 tt_int_op(0, OP_EQ
, entry_guard_has_higher_priority(g2
, g1
));
1767 tt_int_op(0, OP_EQ
, entry_guard_has_higher_priority(g1
, g2
));
1769 /* If one is pending, the pending one has higher priority */
1771 tt_int_op(1, OP_EQ
, entry_guard_has_higher_priority(g1
, g2
));
1772 tt_int_op(0, OP_EQ
, entry_guard_has_higher_priority(g2
, g1
));
1774 /* If both are pending, and last_tried_to_connect is equal:
1777 tt_int_op(0, OP_EQ
, entry_guard_has_higher_priority(g2
, g1
));
1778 tt_int_op(0, OP_EQ
, entry_guard_has_higher_priority(g1
, g2
));
1780 /* One had a connection that startied earlier: it has higher priority. */
1781 g2
->last_tried_to_connect
-= 10;
1782 tt_int_op(1, OP_EQ
, entry_guard_has_higher_priority(g2
, g1
));
1783 tt_int_op(0, OP_EQ
, entry_guard_has_higher_priority(g1
, g2
));
1785 /* Now, say that g1 is confirmed. It will get higher priority. */
1786 g1
->confirmed_idx
= 5;
1787 tt_int_op(0, OP_EQ
, entry_guard_has_higher_priority(g2
, g1
));
1788 tt_int_op(1, OP_EQ
, entry_guard_has_higher_priority(g1
, g2
));
1790 /* But if g2 was confirmed first, it will get priority */
1791 g2
->confirmed_idx
= 2;
1792 tt_int_op(1, OP_EQ
, entry_guard_has_higher_priority(g2
, g1
));
1793 tt_int_op(0, OP_EQ
, entry_guard_has_higher_priority(g1
, g2
));
1801 test_entry_guard_correct_cascading_order(void *arg
)
1804 smartlist_t
*old_primary_guards
= smartlist_new();
1805 guard_selection_t
*gs
= guard_selection_new("default", GS_TYPE_NORMAL
);
1806 entry_guards_expand_sample(gs
);
1807 /** First, a test in which the primary guards need be pulled from different
1808 * lists to fill up the primary list -- this may happen, if for example, not
1809 * enough guards have confirmed yet */
1811 /** just one confirmed */
1812 g
= smartlist_get(gs
->sampled_entry_guards
, 2);
1813 make_guard_confirmed(gs
, g
);
1814 entry_guards_update_primary(gs
);
1815 g
= smartlist_get(gs
->primary_entry_guards
, 0);
1816 tt_int_op(g
->sampled_idx
, OP_EQ
, 0);
1817 g
= smartlist_get(gs
->primary_entry_guards
, 1);
1818 tt_int_op(g
->sampled_idx
, OP_EQ
, 1);
1819 g
= smartlist_get(gs
->primary_entry_guards
, 2);
1820 tt_int_op(g
->sampled_idx
, OP_EQ
, 2);
1822 /** Now the primaries get all confirmed, and the primary list should not
1824 make_guard_confirmed(gs
, smartlist_get(gs
->primary_entry_guards
, 0));
1825 make_guard_confirmed(gs
, smartlist_get(gs
->primary_entry_guards
, 1));
1826 smartlist_add_all(old_primary_guards
, gs
->primary_entry_guards
);
1827 entry_guards_update_primary(gs
);
1828 smartlist_ptrs_eq(gs
->primary_entry_guards
, old_primary_guards
);
1829 /** the confirmed guards should also have the same set of guards, in the same
1831 smartlist_ptrs_eq(gs
->confirmed_entry_guards
, gs
->primary_entry_guards
);
1832 /** Now select a guard for a circuit, and make sure it is the first primary
1834 unsigned state
= 9999;
1835 g
= select_entry_guard_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
, &state
);
1836 tt_ptr_op(g
, OP_EQ
, smartlist_get(gs
->primary_entry_guards
, 0));
1837 /** Now, let's mark this guard as unreachable and let's update the lists */
1838 g
->is_reachable
= GUARD_REACHABLE_NO
;
1839 g
->failing_since
= approx_time() - 10;
1840 g
->last_tried_to_connect
= approx_time() - 10;
1842 entry_guards_update_primary(gs
);
1843 g
= select_entry_guard_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
, &state
);
1844 /** we should have switched to the next one is sampled order */
1845 tt_int_op(g
->sampled_idx
, OP_EQ
, 1);
1847 smartlist_free(old_primary_guards
);
1848 guard_selection_free(gs
);
1852 test_entry_guard_select_for_circuit_no_confirmed(void *arg
)
1854 /* Simpler cases: no gaurds are confirmed yet. */
1856 guard_selection_t
*gs
= guard_selection_new("default", GS_TYPE_NORMAL
);
1857 entry_guard_restriction_t
*rst
= NULL
;
1859 /* simple starting configuration */
1860 entry_guards_update_primary(gs
);
1861 unsigned state
= 9999;
1863 entry_guard_t
*g
= select_entry_guard_for_circuit(gs
, GUARD_USAGE_TRAFFIC
,
1867 tt_assert(g
->is_primary
);
1868 tt_int_op(g
->confirmed_idx
, OP_EQ
, -1);
1869 tt_uint_op(g
->is_pending
, OP_EQ
, 0); // primary implies non-pending.
1870 tt_uint_op(state
, OP_EQ
, GUARD_CIRC_STATE_USABLE_ON_COMPLETION
);
1871 tt_i64_op(g
->last_tried_to_connect
, OP_EQ
, approx_time());
1873 // If we do that again, we should get the same guard.
1874 entry_guard_t
*g2
= select_entry_guard_for_circuit(gs
, GUARD_USAGE_TRAFFIC
,
1876 tt_ptr_op(g2
, OP_EQ
, g
);
1878 // if we mark that guard down, we should get a different primary guard.
1880 g
->is_reachable
= GUARD_REACHABLE_NO
;
1881 g
->failing_since
= approx_time() - 10;
1882 g
->last_tried_to_connect
= approx_time() - 10;
1884 g2
= select_entry_guard_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
, &state
);
1885 tt_ptr_op(g2
, OP_NE
, g
);
1887 tt_assert(g2
->is_primary
);
1888 tt_int_op(g2
->confirmed_idx
, OP_EQ
, -1);
1889 tt_uint_op(g2
->is_pending
, OP_EQ
, 0); // primary implies non-pending.
1890 tt_uint_op(state
, OP_EQ
, GUARD_CIRC_STATE_USABLE_ON_COMPLETION
);
1891 tt_i64_op(g2
->last_tried_to_connect
, OP_EQ
, approx_time());
1893 // If we say that the first primary guard was last tried a long time ago, we
1894 // should get an automatic retry on it.
1895 g
->failing_since
= approx_time() - 72*60*60;
1896 g
->last_tried_to_connect
= approx_time() - 72*60*60;
1898 g2
= select_entry_guard_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
, &state
);
1899 tt_ptr_op(g2
, OP_EQ
, g
);
1901 tt_uint_op(state
, OP_EQ
, GUARD_CIRC_STATE_USABLE_ON_COMPLETION
);
1902 tt_i64_op(g2
->last_tried_to_connect
, OP_EQ
, approx_time());
1903 tt_int_op(g2
->is_reachable
, OP_EQ
, GUARD_REACHABLE_MAYBE
);
1905 // And if we mark ALL the primary guards down, we should get another guard
1907 SMARTLIST_FOREACH(gs
->primary_entry_guards
, entry_guard_t
*, guard
, {
1908 guard
->is_reachable
= GUARD_REACHABLE_NO
;
1909 guard
->last_tried_to_connect
= approx_time() - 5;
1910 guard
->failing_since
= approx_time() - 30;
1913 g2
= select_entry_guard_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
, &state
);
1915 tt_assert(!g2
->is_primary
);
1916 tt_int_op(g2
->confirmed_idx
, OP_EQ
, -1);
1917 tt_uint_op(g2
->is_pending
, OP_EQ
, 1);
1918 tt_uint_op(state
, OP_EQ
, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD
);
1919 tt_i64_op(g2
->last_tried_to_connect
, OP_EQ
, approx_time());
1920 tt_int_op(g2
->is_reachable
, OP_EQ
, GUARD_REACHABLE_MAYBE
);
1922 // As a bonus, maybe we should be retrying the primary guards. Let's say so.
1923 mark_primary_guards_maybe_reachable(gs
);
1924 SMARTLIST_FOREACH(gs
->primary_entry_guards
, entry_guard_t
*, guard
, {
1925 tt_int_op(guard
->is_reachable
, OP_EQ
, GUARD_REACHABLE_MAYBE
);
1926 tt_assert(guard
->is_usable_filtered_guard
== 1);
1927 // no change to these fields.
1928 tt_i64_op(guard
->last_tried_to_connect
, OP_EQ
, approx_time() - 5);
1929 tt_i64_op(guard
->failing_since
, OP_EQ
, approx_time() - 30);
1932 /* Let's try again and we should get the first primary guard again */
1933 g
= select_entry_guard_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
, &state
);
1934 tt_ptr_op(g
, OP_EQ
, smartlist_get(gs
->primary_entry_guards
, 0));
1935 g2
= select_entry_guard_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
, &state
);
1936 tt_ptr_op(g2
, OP_EQ
, g
);
1938 /* But if we impose a restriction, we don't get the same guard */
1939 rst
= guard_create_exit_restriction((uint8_t*)g
->identity
);
1940 g2
= select_entry_guard_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, rst
, &state
);
1941 tt_ptr_op(g2
, OP_NE
, g
);
1944 guard_selection_free(gs
);
1945 entry_guard_restriction_free(rst
);
1949 test_entry_guard_select_for_circuit_confirmed(void *arg
)
1951 /* Case 2: if all the primary guards are down, and there are more confirmed
1952 guards, we use a confirmed guard. */
1955 entry_guard_restriction_t
*rst
= NULL
;
1956 guard_selection_t
*gs
= guard_selection_new("default", GS_TYPE_NORMAL
);
1957 const int N_CONFIRMED
= 10;
1959 /* slightly more complicated simple starting configuration */
1960 entry_guards_update_primary(gs
);
1961 for (i
= 0; i
< N_CONFIRMED
; ++i
) {
1962 entry_guard_t
*guard
= smartlist_get(gs
->sampled_entry_guards
, i
);
1963 make_guard_confirmed(gs
, guard
);
1965 entry_guards_update_primary(gs
); // rebuild the primary list.
1967 unsigned state
= 9999;
1969 // As above, this gives us a primary guard.
1970 entry_guard_t
*g
= select_entry_guard_for_circuit(gs
, GUARD_USAGE_TRAFFIC
,
1973 tt_assert(g
->is_primary
);
1974 tt_int_op(g
->confirmed_idx
, OP_EQ
, 0);
1975 tt_uint_op(g
->is_pending
, OP_EQ
, 0); // primary implies non-pending.
1976 tt_uint_op(state
, OP_EQ
, GUARD_CIRC_STATE_USABLE_ON_COMPLETION
);
1977 tt_i64_op(g
->last_tried_to_connect
, OP_EQ
, approx_time());
1978 tt_ptr_op(g
, OP_EQ
, smartlist_get(gs
->primary_entry_guards
, 0));
1980 // But if we mark all the primary guards down...
1981 SMARTLIST_FOREACH(gs
->primary_entry_guards
, entry_guard_t
*, guard
, {
1982 guard
->last_tried_to_connect
= approx_time();
1983 entry_guards_note_guard_failure(gs
, guard
);
1986 // ... we should get a confirmed guard.
1988 g
= select_entry_guard_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
, &state
);
1990 tt_assert(! g
->is_primary
);
1991 tt_int_op(g
->confirmed_idx
, OP_EQ
, smartlist_len(gs
->primary_entry_guards
));
1992 tt_assert(g
->is_pending
);
1993 tt_uint_op(state
, OP_EQ
, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD
);
1994 tt_i64_op(g
->last_tried_to_connect
, OP_EQ
, approx_time());
1996 // And if we try again, we should get a different confirmed guard, since
1997 // that one is pending.
1999 entry_guard_t
*g2
= select_entry_guard_for_circuit(gs
, GUARD_USAGE_TRAFFIC
,
2002 tt_assert(! g2
->is_primary
);
2003 tt_ptr_op(g2
, OP_NE
, g
);
2004 tt_int_op(g2
->confirmed_idx
, OP_EQ
,
2005 smartlist_len(gs
->primary_entry_guards
)+1);
2006 tt_assert(g2
->is_pending
);
2007 tt_uint_op(state
, OP_EQ
, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD
);
2008 tt_i64_op(g2
->last_tried_to_connect
, OP_EQ
, approx_time());
2010 // If we say that the next confirmed guard in order is excluded, and
2011 // we disable EnforceDistinctSubnets, we get the guard AFTER the
2013 get_options_mutable()->EnforceDistinctSubnets
= 0;
2014 g
= smartlist_get(gs
->confirmed_entry_guards
,
2015 smartlist_len(gs
->primary_entry_guards
)+2);
2016 rst
= guard_create_exit_restriction((uint8_t*)g
->identity
);
2017 g2
= select_entry_guard_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, rst
, &state
);
2018 tt_ptr_op(g2
, OP_NE
, NULL
);
2019 tt_ptr_op(g2
, OP_NE
, g
);
2020 tt_int_op(g2
->confirmed_idx
, OP_EQ
,
2021 smartlist_len(gs
->primary_entry_guards
)+3);
2023 // If we make every confirmed guard become pending then we start poking
2025 const int n_remaining_confirmed
=
2026 N_CONFIRMED
- 3 - smartlist_len(gs
->primary_entry_guards
);
2027 for (i
= 0; i
< n_remaining_confirmed
; ++i
) {
2028 g
= select_entry_guard_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
, &state
);
2029 tt_int_op(g
->confirmed_idx
, OP_GE
, 0);
2033 g
= select_entry_guard_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
, &state
);
2035 tt_assert(g
->is_pending
);
2036 tt_int_op(g
->confirmed_idx
, OP_EQ
, -1);
2038 // If we EnforceDistinctSubnets and apply a restriction, we get
2039 // nothing, since we put all of the nodes in the same /16.
2040 // Regression test for bug 22753/TROVE-2017-006.
2041 get_options_mutable()->EnforceDistinctSubnets
= 1;
2042 g
= smartlist_get(gs
->confirmed_entry_guards
, 0);
2043 memcpy(rst
->exclude_id
, g
->identity
, DIGEST_LEN
);
2044 g2
= select_entry_guard_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, rst
, &state
);
2045 tt_ptr_op(g2
, OP_EQ
, NULL
);
2048 guard_selection_free(gs
);
2049 entry_guard_restriction_free(rst
);
2053 test_entry_guard_select_for_circuit_highlevel_primary(void *arg
)
2055 /* Play around with selecting primary guards for circuits and markign
2056 * them up and down */
2058 guard_selection_t
*gs
= guard_selection_new("default", GS_TYPE_NORMAL
);
2060 time_t start
= approx_time();
2062 const node_t
*node
= NULL
;
2063 circuit_guard_state_t
*guard
= NULL
;
2067 * Make sure that the pick-for-circuit API basically works. We'll get
2068 * a primary guard, so it'll be usable on completion.
2070 int r
= entry_guard_pick_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
,
2073 tt_int_op(r
, OP_EQ
, 0);
2076 tt_int_op(guard
->state
, OP_EQ
, GUARD_CIRC_STATE_USABLE_ON_COMPLETION
);
2077 g
= entry_guard_handle_get(guard
->guard
);
2079 tt_mem_op(g
->identity
, OP_EQ
, node
->identity
, DIGEST_LEN
);
2080 tt_int_op(g
->is_primary
, OP_EQ
, 1);
2081 tt_i64_op(g
->last_tried_to_connect
, OP_EQ
, start
);
2082 tt_int_op(g
->confirmed_idx
, OP_EQ
, -1);
2084 /* Call that circuit successful. */
2085 update_approx_time(start
+15);
2086 u
= entry_guard_succeeded(&guard
);
2087 tt_int_op(u
, OP_EQ
, GUARD_USABLE_NOW
); /* We can use it now. */
2089 tt_int_op(guard
->state
, OP_EQ
, GUARD_CIRC_STATE_COMPLETE
);
2090 g
= entry_guard_handle_get(guard
->guard
);
2092 tt_int_op(g
->is_reachable
, OP_EQ
, GUARD_REACHABLE_YES
);
2093 tt_int_op(g
->confirmed_idx
, OP_EQ
, 0);
2095 circuit_guard_state_free(guard
);
2100 /* Try again. We'll also get a primary guard this time. (The same one,
2101 in fact.) But this time, we'll say the connection has failed. */
2102 update_approx_time(start
+35);
2103 r
= entry_guard_pick_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
,
2105 tt_int_op(r
, OP_EQ
, 0);
2108 tt_int_op(guard
->state
, OP_EQ
, GUARD_CIRC_STATE_USABLE_ON_COMPLETION
);
2109 tt_i64_op(guard
->state_set_at
, OP_EQ
, start
+35);
2110 g
= entry_guard_handle_get(guard
->guard
);
2112 tt_mem_op(g
->identity
, OP_EQ
, node
->identity
, DIGEST_LEN
);
2113 tt_int_op(g
->is_primary
, OP_EQ
, 1);
2114 tt_i64_op(g
->last_tried_to_connect
, OP_EQ
, start
+35);
2115 tt_int_op(g
->confirmed_idx
, OP_EQ
, 0); // same one.
2117 /* It's failed! What will happen to our poor guard? */
2118 update_approx_time(start
+45);
2119 entry_guard_failed(&guard
);
2121 tt_int_op(guard
->state
, OP_EQ
, GUARD_CIRC_STATE_DEAD
);
2122 tt_i64_op(guard
->state_set_at
, OP_EQ
, start
+45);
2123 g
= entry_guard_handle_get(guard
->guard
);
2125 tt_int_op(g
->is_reachable
, OP_EQ
, GUARD_REACHABLE_NO
);
2126 tt_i64_op(g
->failing_since
, OP_EQ
, start
+45);
2127 tt_int_op(g
->confirmed_idx
, OP_EQ
, 0); // still confirmed.
2129 circuit_guard_state_free(guard
);
2132 entry_guard_t
*g_prev
= g
;
2135 /* Now try a third time. Since the other one is down, we'll get a different
2136 * (still primary) guard.
2138 update_approx_time(start
+60);
2139 r
= entry_guard_pick_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
,
2141 tt_int_op(r
, OP_EQ
, 0);
2144 tt_int_op(guard
->state
, OP_EQ
, GUARD_CIRC_STATE_USABLE_ON_COMPLETION
);
2145 g
= entry_guard_handle_get(guard
->guard
);
2147 tt_ptr_op(g
, OP_NE
, g_prev
);
2148 tt_mem_op(g
->identity
, OP_EQ
, node
->identity
, DIGEST_LEN
);
2149 tt_mem_op(g
->identity
, OP_NE
, g_prev
->identity
, DIGEST_LEN
);
2150 tt_int_op(g
->is_primary
, OP_EQ
, 1);
2151 tt_i64_op(g
->last_tried_to_connect
, OP_EQ
, start
+60);
2152 tt_int_op(g
->confirmed_idx
, OP_EQ
, -1); // not confirmed now.
2154 /* Call this one up; watch it get confirmed. */
2155 update_approx_time(start
+90);
2156 u
= entry_guard_succeeded(&guard
);
2157 tt_int_op(u
, OP_EQ
, GUARD_USABLE_NOW
);
2159 tt_int_op(guard
->state
, OP_EQ
, GUARD_CIRC_STATE_COMPLETE
);
2160 g
= entry_guard_handle_get(guard
->guard
);
2162 tt_int_op(g
->is_reachable
, OP_EQ
, GUARD_REACHABLE_YES
);
2163 tt_int_op(g
->confirmed_idx
, OP_EQ
, 1);
2166 guard_selection_free(gs
);
2167 circuit_guard_state_free(guard
);
2171 test_entry_guard_select_for_circuit_highlevel_confirm_other(void *arg
)
2174 const int N_PRIMARY
= DFLT_N_PRIMARY_GUARDS
;
2176 /* At the start, we have no confirmed guards. We'll mark the primary guards
2177 * down, then confirm something else. As soon as we do, it should become
2178 * primary, and we should get it next time. */
2180 time_t start
= approx_time();
2181 guard_selection_t
*gs
= guard_selection_new("default", GS_TYPE_NORMAL
);
2182 circuit_guard_state_t
*guard
= NULL
;
2184 const node_t
*node
= NULL
;
2187 /* Declare that we're on the internet. */
2188 entry_guards_note_internet_connectivity(gs
);
2190 /* Primary guards are down! */
2191 for (i
= 0; i
< N_PRIMARY
; ++i
) {
2192 r
= entry_guard_pick_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
,
2196 tt_int_op(r
, OP_EQ
, 0);
2197 tt_int_op(guard
->state
, OP_EQ
, GUARD_CIRC_STATE_USABLE_ON_COMPLETION
);
2198 entry_guard_failed(&guard
);
2199 circuit_guard_state_free(guard
);
2204 /* Next guard should be non-primary. */
2206 r
= entry_guard_pick_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
,
2210 tt_int_op(r
, OP_EQ
, 0);
2211 entry_guard_t
*g
= entry_guard_handle_get(guard
->guard
);
2213 tt_int_op(guard
->state
, OP_EQ
, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD
);
2214 tt_int_op(g
->confirmed_idx
, OP_EQ
, -1);
2215 tt_int_op(g
->is_primary
, OP_EQ
, 0);
2216 tt_int_op(g
->is_pending
, OP_EQ
, 1);
2219 u
= entry_guard_succeeded(&guard
);
2220 /* We're on the internet (by fiat), so this guard will get called "confirmed"
2221 * and should immediately become primary.
2223 tt_int_op(guard
->state
, OP_EQ
, GUARD_CIRC_STATE_COMPLETE
);
2224 tt_assert(u
== GUARD_USABLE_NOW
);
2225 tt_int_op(g
->confirmed_idx
, OP_EQ
, 0);
2226 tt_int_op(g
->is_primary
, OP_EQ
, 1);
2227 tt_int_op(g
->is_pending
, OP_EQ
, 0);
2230 guard_selection_free(gs
);
2231 circuit_guard_state_free(guard
);
2235 test_entry_guard_select_for_circuit_highlevel_primary_retry(void *arg
)
2238 const int N_PRIMARY
= DFLT_N_PRIMARY_GUARDS
;
2240 /* At the start, we have no confirmed guards. We'll mark the primary guards
2241 * down, then confirm something else. As soon as we do, it should become
2242 * primary, and we should get it next time. */
2244 time_t start
= approx_time();
2245 guard_selection_t
*gs
= guard_selection_new("default", GS_TYPE_NORMAL
);
2246 circuit_guard_state_t
*guard
= NULL
, *guard2
= NULL
;
2248 const node_t
*node
= NULL
;
2252 /* Declare that we're on the internet. */
2253 entry_guards_note_internet_connectivity(gs
);
2255 /* Make primary guards confirmed (so they won't be superseded by a later
2256 * guard), then mark them down. */
2257 for (i
= 0; i
< N_PRIMARY
; ++i
) {
2258 r
= entry_guard_pick_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
,
2262 tt_int_op(r
, OP_EQ
, 0);
2263 tt_int_op(guard
->state
, OP_EQ
, GUARD_CIRC_STATE_USABLE_ON_COMPLETION
);
2264 g
= entry_guard_handle_get(guard
->guard
);
2265 make_guard_confirmed(gs
, g
);
2266 tt_int_op(g
->is_primary
, OP_EQ
, 1);
2267 entry_guard_failed(&guard
);
2268 circuit_guard_state_free(guard
);
2269 tt_int_op(g
->is_reachable
, OP_EQ
, GUARD_REACHABLE_NO
);
2274 /* Get another guard that we might try. */
2275 r
= entry_guard_pick_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
,
2279 tt_int_op(r
, OP_EQ
, 0);
2280 tt_int_op(guard
->state
, OP_EQ
, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD
);
2281 g
= entry_guard_handle_get(guard
->guard
);
2282 tt_int_op(g
->is_primary
, OP_EQ
, 0);
2284 tt_assert(entry_guards_all_primary_guards_are_down(gs
));
2286 /* And an hour has passed ... */
2287 update_approx_time(start
+ 3600);
2289 /* Say that guard has succeeded! */
2290 u
= entry_guard_succeeded(&guard
);
2291 tt_int_op(u
, OP_EQ
, GUARD_MAYBE_USABLE_LATER
);
2292 tt_int_op(guard
->state
, OP_EQ
, GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD
);
2293 g
= entry_guard_handle_get(guard
->guard
);
2295 /* The primary guards should have been marked up! */
2296 SMARTLIST_FOREACH(gs
->primary_entry_guards
, entry_guard_t
*, pg
, {
2297 tt_int_op(pg
->is_primary
, OP_EQ
, 1);
2298 tt_ptr_op(g
, OP_NE
, pg
);
2299 tt_int_op(pg
->is_reachable
, OP_EQ
, GUARD_REACHABLE_MAYBE
);
2302 /* Have a circuit to a primary guard succeed. */
2303 r
= entry_guard_pick_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
,
2305 tt_int_op(r
, OP_EQ
, 0);
2306 tt_int_op(guard2
->state
, OP_EQ
, GUARD_CIRC_STATE_USABLE_ON_COMPLETION
);
2307 u
= entry_guard_succeeded(&guard2
);
2308 tt_assert(u
== GUARD_USABLE_NOW
);
2309 tt_int_op(guard2
->state
, OP_EQ
, GUARD_CIRC_STATE_COMPLETE
);
2311 tt_assert(! entry_guards_all_primary_guards_are_down(gs
));
2314 guard_selection_free(gs
);
2315 circuit_guard_state_free(guard
);
2316 circuit_guard_state_free(guard2
);
2320 test_entry_guard_select_and_cancel(void *arg
)
2323 const int N_PRIMARY
= DFLT_N_PRIMARY_GUARDS
;
2325 const node_t
*node
= NULL
;
2326 circuit_guard_state_t
*guard
;
2327 guard_selection_t
*gs
= guard_selection_new("default", GS_TYPE_NORMAL
);
2330 /* Once more, we mark all the primary guards down. */
2331 entry_guards_note_internet_connectivity(gs
);
2332 for (i
= 0; i
< N_PRIMARY
; ++i
) {
2333 r
= entry_guard_pick_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
,
2335 tt_int_op(r
, OP_EQ
, 0);
2336 tt_int_op(guard
->state
, OP_EQ
, GUARD_CIRC_STATE_USABLE_ON_COMPLETION
);
2337 g
= entry_guard_handle_get(guard
->guard
);
2338 tt_int_op(g
->is_primary
, OP_EQ
, 1);
2339 tt_int_op(g
->is_pending
, OP_EQ
, 0);
2340 make_guard_confirmed(gs
, g
);
2341 entry_guard_failed(&guard
);
2342 circuit_guard_state_free(guard
);
2347 tt_assert(entry_guards_all_primary_guards_are_down(gs
));
2349 /* Now get another guard we could try... */
2350 r
= entry_guard_pick_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
,
2354 tt_int_op(r
, OP_EQ
, 0);
2355 tt_int_op(guard
->state
, OP_EQ
, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD
);
2356 g
= entry_guard_handle_get(guard
->guard
);
2357 tt_int_op(g
->is_primary
, OP_EQ
, 0);
2358 tt_int_op(g
->is_pending
, OP_EQ
, 1);
2360 /* Whoops! We should never have asked for this guard. Cancel the request! */
2361 entry_guard_cancel(&guard
);
2362 tt_ptr_op(guard
, OP_EQ
, NULL
);
2363 tt_int_op(g
->is_primary
, OP_EQ
, 0);
2364 tt_int_op(g
->is_pending
, OP_EQ
, 0);
2367 guard_selection_free(gs
);
2368 circuit_guard_state_free(guard
);
2372 test_entry_guard_drop_guards(void *arg
)
2376 const node_t
*node
= NULL
;
2377 circuit_guard_state_t
*guard
;
2378 guard_selection_t
*gs
= get_guard_selection_info();
2380 // Pick a guard, to get things set up.
2381 r
= entry_guard_pick_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
,
2383 tt_int_op(r
, OP_EQ
, 0);
2384 tt_int_op(smartlist_len(gs
->sampled_entry_guards
), OP_GE
,
2385 DFLT_MIN_FILTERED_SAMPLE_SIZE
);
2386 tt_ptr_op(gs
, OP_EQ
, get_guard_selection_info());
2388 // Drop all the guards! (This is a bad idea....)
2389 remove_all_entry_guards_for_guard_selection(gs
);
2390 gs
= get_guard_selection_info();
2391 tt_int_op(smartlist_len(gs
->sampled_entry_guards
), OP_EQ
, 0);
2392 tt_int_op(smartlist_len(gs
->primary_entry_guards
), OP_EQ
, 0);
2393 tt_int_op(smartlist_len(gs
->confirmed_entry_guards
), OP_EQ
, 0);
2396 circuit_guard_state_free(guard
);
2397 guard_selection_free(gs
);
2400 /* Unit test setup function: Create a fake network, and set everything up
2401 * for testing the upgrade-a-waiting-circuit code. */
2403 guard_selection_t
*gs
;
2405 circuit_guard_state_t
*guard1_state
;
2406 circuit_guard_state_t
*guard2_state
;
2407 entry_guard_t
*guard1
;
2408 entry_guard_t
*guard2
;
2409 origin_circuit_t
*circ1
;
2410 origin_circuit_t
*circ2
;
2411 smartlist_t
*all_origin_circuits
;
2412 } upgrade_circuits_data_t
;
2414 upgrade_circuits_setup(const struct testcase_t
*testcase
)
2416 upgrade_circuits_data_t
*data
= tor_malloc_zero(sizeof(*data
));
2417 guard_selection_t
*gs
= data
->gs
=
2418 guard_selection_new("default", GS_TYPE_NORMAL
);
2419 circuit_guard_state_t
*guard
;
2423 const int N_PRIMARY
= DFLT_N_PRIMARY_GUARDS
;
2424 const char *argument
= testcase
->setup_data
;
2425 const int make_circ1_succeed
= strstr(argument
, "c1-done") != NULL
;
2426 const int make_circ2_succeed
= strstr(argument
, "c2-done") != NULL
;
2428 big_fake_network_setup(testcase
);
2430 /* We're going to set things up in a state where a circuit will be ready to
2431 * be upgraded. Each test can make a single change (or not) that should
2432 * block the upgrade.
2435 /* First, make all the primary guards confirmed, and down. */
2436 data
->start
= approx_time();
2437 entry_guards_note_internet_connectivity(gs
);
2438 for (i
= 0; i
< N_PRIMARY
; ++i
) {
2439 entry_guard_pick_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
, &node
, &guard
);
2440 g
= entry_guard_handle_get(guard
->guard
);
2441 make_guard_confirmed(gs
, g
);
2442 entry_guard_failed(&guard
);
2443 circuit_guard_state_free(guard
);
2446 /* Grab another couple of guards */
2447 data
->all_origin_circuits
= smartlist_new();
2449 update_approx_time(data
->start
+ 27);
2450 entry_guard_pick_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
,
2451 &node
, &data
->guard1_state
);
2452 origin_circuit_t
*circ
;
2453 data
->circ1
= circ
= origin_circuit_new();
2454 circ
->base_
.purpose
= CIRCUIT_PURPOSE_C_GENERAL
;
2455 circ
->guard_state
= data
->guard1_state
;
2456 smartlist_add(data
->all_origin_circuits
, circ
);
2458 update_approx_time(data
->start
+ 30);
2459 entry_guard_pick_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
,
2460 &node
, &data
->guard2_state
);
2461 data
->circ2
= circ
= origin_circuit_new();
2462 circ
->base_
.purpose
= CIRCUIT_PURPOSE_C_GENERAL
;
2463 circ
->guard_state
= data
->guard2_state
;
2464 smartlist_add(data
->all_origin_circuits
, circ
);
2466 data
->guard1
= entry_guard_handle_get(data
->guard1_state
->guard
);
2467 data
->guard2
= entry_guard_handle_get(data
->guard2_state
->guard
);
2468 tor_assert(data
->guard1
!= data
->guard2
);
2469 tor_assert(data
->guard1_state
->state
==
2470 GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD
);
2471 tor_assert(data
->guard2_state
->state
==
2472 GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD
);
2475 update_approx_time(data
->start
+ 32);
2476 if (make_circ1_succeed
) {
2477 r
= entry_guard_succeeded(&data
->guard1_state
);
2478 tor_assert(r
== GUARD_MAYBE_USABLE_LATER
);
2479 tor_assert(data
->guard1_state
->state
==
2480 GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD
);
2482 update_approx_time(data
->start
+ 33);
2483 if (make_circ2_succeed
) {
2484 r
= entry_guard_succeeded(&data
->guard2_state
);
2485 tor_assert(r
== GUARD_MAYBE_USABLE_LATER
);
2486 tor_assert(data
->guard2_state
->state
==
2487 GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD
);
2493 upgrade_circuits_cleanup(const struct testcase_t
*testcase
, void *ptr
)
2495 upgrade_circuits_data_t
*data
= ptr
;
2496 // circuit_guard_state_free(data->guard1_state); // held in circ1
2497 // circuit_guard_state_free(data->guard2_state); // held in circ2
2498 guard_selection_free(data
->gs
);
2499 smartlist_free(data
->all_origin_circuits
);
2500 circuit_free_(TO_CIRCUIT(data
->circ1
));
2501 circuit_free_(TO_CIRCUIT(data
->circ2
));
2503 return big_fake_network_cleanup(testcase
, NULL
);
2507 test_entry_guard_upgrade_a_circuit(void *arg
)
2509 upgrade_circuits_data_t
*data
= arg
;
2511 /* This is the easy case: we have no COMPLETED circuits, all the
2512 * primary guards are down, we have two WAITING circuits: one will
2513 * get upgraded to COMPLETED! (The one that started first.)
2516 smartlist_t
*result
= smartlist_new();
2518 r
= entry_guards_upgrade_waiting_circuits(data
->gs
,
2519 data
->all_origin_circuits
,
2521 tt_int_op(r
, OP_EQ
, 1);
2522 tt_int_op(smartlist_len(result
), OP_EQ
, 1);
2523 origin_circuit_t
*oc
= smartlist_get(result
, 0);
2525 /* circ1 was started first, so we'll get told to ugrade it... */
2526 tt_ptr_op(oc
, OP_EQ
, data
->circ1
);
2528 /* And the guard state should be complete */
2529 tt_ptr_op(data
->guard1_state
, OP_NE
, NULL
);
2530 tt_int_op(data
->guard1_state
->state
, OP_EQ
, GUARD_CIRC_STATE_COMPLETE
);
2533 smartlist_free(result
);
2537 test_entry_guard_upgrade_blocked_by_live_primary_guards(void *arg
)
2539 upgrade_circuits_data_t
*data
= arg
;
2541 /* If any primary guards might be up, we can't upgrade any waiting
2544 mark_primary_guards_maybe_reachable(data
->gs
);
2546 smartlist_t
*result
= smartlist_new();
2548 setup_capture_of_logs(LOG_DEBUG
);
2549 r
= entry_guards_upgrade_waiting_circuits(data
->gs
,
2550 data
->all_origin_circuits
,
2552 tt_int_op(r
, OP_EQ
, 0);
2553 tt_int_op(smartlist_len(result
), OP_EQ
, 0);
2554 expect_log_msg_containing("not all primary guards were definitely down.");
2557 teardown_capture_of_logs();
2558 smartlist_free(result
);
2562 test_entry_guard_upgrade_blocked_by_lack_of_waiting_circuits(void *arg
)
2564 upgrade_circuits_data_t
*data
= arg
;
2566 /* If no circuits are waiting, we can't upgrade anything. (The test
2567 * setup in this case was told not to make any of the circuits "waiting".)
2569 smartlist_t
*result
= smartlist_new();
2571 setup_capture_of_logs(LOG_DEBUG
);
2572 r
= entry_guards_upgrade_waiting_circuits(data
->gs
,
2573 data
->all_origin_circuits
,
2575 tt_int_op(r
, OP_EQ
, 0);
2576 tt_int_op(smartlist_len(result
), OP_EQ
, 0);
2577 expect_log_msg_containing("Considered upgrading guard-stalled circuits, "
2578 "but didn't find any.");
2581 teardown_capture_of_logs();
2582 smartlist_free(result
);
2586 test_entry_guard_upgrade_blocked_by_better_circ_complete(void *arg
)
2588 upgrade_circuits_data_t
*data
= arg
;
2590 /* We'll run through the logic of upgrade_a_circuit below...
2591 * and then try again to make sure that circ2 isn't also upgraded.
2594 smartlist_t
*result
= smartlist_new();
2596 r
= entry_guards_upgrade_waiting_circuits(data
->gs
,
2597 data
->all_origin_circuits
,
2599 tt_int_op(r
, OP_EQ
, 1);
2600 tt_int_op(smartlist_len(result
), OP_EQ
, 1);
2601 origin_circuit_t
*oc
= smartlist_get(result
, 0);
2602 tt_ptr_op(oc
, OP_EQ
, data
->circ1
);
2603 tt_ptr_op(data
->guard1_state
, OP_NE
, NULL
);
2604 tt_int_op(data
->guard1_state
->state
, OP_EQ
, GUARD_CIRC_STATE_COMPLETE
);
2606 /* Now, try again. Make sure that circ2 isn't upgraded. */
2607 smartlist_clear(result
);
2608 setup_capture_of_logs(LOG_DEBUG
);
2609 r
= entry_guards_upgrade_waiting_circuits(data
->gs
,
2610 data
->all_origin_circuits
,
2612 tt_int_op(r
, OP_EQ
, 0);
2613 tt_int_op(smartlist_len(result
), OP_EQ
, 0);
2614 expect_log_msg_containing("At least one complete circuit had higher "
2615 "priority, so not upgrading.");
2618 teardown_capture_of_logs();
2619 smartlist_free(result
);
2623 test_entry_guard_upgrade_not_blocked_by_restricted_circ_complete(void *arg
)
2625 upgrade_circuits_data_t
*data
= arg
;
2627 /* Once more, let circ1 become complete. But this time, we'll claim
2628 * that circ2 was restricted to not use the same guard as circ1. */
2629 data
->guard2_state
->restrictions
=
2630 guard_create_exit_restriction((uint8_t*)data
->guard1
->identity
);
2632 smartlist_t
*result
= smartlist_new();
2634 r
= entry_guards_upgrade_waiting_circuits(data
->gs
,
2635 data
->all_origin_circuits
,
2637 tt_int_op(r
, OP_EQ
, 1);
2638 tt_int_op(smartlist_len(result
), OP_EQ
, 1);
2639 origin_circuit_t
*oc
= smartlist_get(result
, 0);
2640 tt_ptr_op(oc
, OP_EQ
, data
->circ1
);
2641 tt_ptr_op(data
->guard1_state
, OP_NE
, NULL
);
2642 tt_int_op(data
->guard1_state
->state
, OP_EQ
, GUARD_CIRC_STATE_COMPLETE
);
2644 /* Now, we try again. Since circ2 has a restriction that circ1 doesn't obey,
2645 * circ2 _is_ eligible for upgrade. */
2646 smartlist_clear(result
);
2647 r
= entry_guards_upgrade_waiting_circuits(data
->gs
,
2648 data
->all_origin_circuits
,
2650 tt_int_op(r
, OP_EQ
, 1);
2651 tt_int_op(smartlist_len(result
), OP_EQ
, 1);
2652 origin_circuit_t
*oc2
= smartlist_get(result
, 0);
2653 tt_ptr_op(oc2
, OP_EQ
, data
->circ2
);
2656 smartlist_free(result
);
2660 test_entry_guard_upgrade_not_blocked_by_worse_circ_complete(void *arg
)
2662 upgrade_circuits_data_t
*data
= arg
;
2663 smartlist_t
*result
= smartlist_new();
2664 /* here we manually make circ2 COMPLETE, and make sure that circ1
2665 * gets made complete anyway, since guard1 has higher priority
2667 update_approx_time(data
->start
+ 300);
2668 data
->guard2_state
->state
= GUARD_CIRC_STATE_COMPLETE
;
2669 data
->guard2_state
->state_set_at
= approx_time();
2670 update_approx_time(data
->start
+ 301);
2672 /* Now, try again. Make sure that circ1 is approved. */
2674 r
= entry_guards_upgrade_waiting_circuits(data
->gs
,
2675 data
->all_origin_circuits
,
2677 tt_int_op(r
, OP_EQ
, 1);
2678 tt_int_op(smartlist_len(result
), OP_EQ
, 1);
2679 origin_circuit_t
*oc
= smartlist_get(result
, 0);
2680 tt_ptr_op(oc
, OP_EQ
, data
->circ1
);
2683 smartlist_free(result
);
2687 test_entry_guard_upgrade_blocked_by_better_circ_pending(void *arg
)
2689 upgrade_circuits_data_t
*data
= arg
;
2691 /* circ2 is done, but circ1 is still pending. Since circ1 is better,
2692 * we won't upgrade circ2. */
2694 /* XXXX Prop271 -- this is a kludge. I'm making sure circ1 _is_ better,
2695 * by messing with the guards' confirmed_idx */
2696 make_guard_confirmed(data
->gs
, data
->guard1
);
2699 tmp
= data
->guard1
->confirmed_idx
;
2700 data
->guard1
->confirmed_idx
= data
->guard2
->confirmed_idx
;
2701 data
->guard2
->confirmed_idx
= tmp
;
2704 smartlist_t
*result
= smartlist_new();
2705 setup_capture_of_logs(LOG_DEBUG
);
2707 r
= entry_guards_upgrade_waiting_circuits(data
->gs
,
2708 data
->all_origin_circuits
,
2710 tt_int_op(r
, OP_EQ
, 0);
2711 tt_int_op(smartlist_len(result
), OP_EQ
, 0);
2712 expect_log_msg_containing("but 1 pending circuit(s) had higher guard "
2713 "priority, so not upgrading.");
2716 teardown_capture_of_logs();
2717 smartlist_free(result
);
2721 test_entry_guard_upgrade_not_blocked_by_restricted_circ_pending(void *arg
)
2723 upgrade_circuits_data_t
*data
= arg
;
2724 /* circ2 is done, but circ1 is still pending. But when there is a
2725 restriction on circ2 that circ1 can't satisfy, circ1 can't block
2728 /* XXXX Prop271 -- this is a kludge. I'm making sure circ1 _is_ better,
2729 * by messing with the guards' confirmed_idx */
2730 make_guard_confirmed(data
->gs
, data
->guard1
);
2733 tmp
= data
->guard1
->confirmed_idx
;
2734 data
->guard1
->confirmed_idx
= data
->guard2
->confirmed_idx
;
2735 data
->guard2
->confirmed_idx
= tmp
;
2738 data
->guard2_state
->restrictions
=
2739 guard_create_exit_restriction((uint8_t*)data
->guard1
->identity
);
2741 smartlist_t
*result
= smartlist_new();
2743 r
= entry_guards_upgrade_waiting_circuits(data
->gs
,
2744 data
->all_origin_circuits
,
2746 tt_int_op(r
, OP_EQ
, 1);
2747 tt_int_op(smartlist_len(result
), OP_EQ
, 1);
2748 origin_circuit_t
*oc
= smartlist_get(result
, 0);
2749 tt_ptr_op(oc
, OP_EQ
, data
->circ2
);
2752 smartlist_free(result
);
2756 test_entry_guard_upgrade_not_blocked_by_worse_circ_pending(void *arg
)
2758 upgrade_circuits_data_t
*data
= arg
;
2760 /* circ1 is done, but circ2 is still pending. Since circ1 is better,
2761 * we will upgrade it. */
2762 smartlist_t
*result
= smartlist_new();
2764 r
= entry_guards_upgrade_waiting_circuits(data
->gs
,
2765 data
->all_origin_circuits
,
2767 tt_int_op(r
, OP_EQ
, 1);
2768 tt_int_op(smartlist_len(result
), OP_EQ
, 1);
2769 origin_circuit_t
*oc
= smartlist_get(result
, 0);
2770 tt_ptr_op(oc
, OP_EQ
, data
->circ1
);
2773 smartlist_free(result
);
2777 test_entry_guard_should_expire_waiting(void *arg
)
2780 circuit_guard_state_t
*fake_state
= tor_malloc_zero(sizeof(*fake_state
));
2781 /* We'll leave "guard" unset -- it won't matter here. */
2783 /* No state? Can't expire. */
2784 tt_assert(! entry_guard_state_should_expire(NULL
));
2786 /* Let's try one that expires. */
2787 fake_state
->state
= GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD
;
2788 fake_state
->state_set_at
=
2789 approx_time() - DFLT_NONPRIMARY_GUARD_IDLE_TIMEOUT
- 1;
2791 tt_assert(entry_guard_state_should_expire(fake_state
));
2793 /* But it wouldn't expire if we changed the state. */
2794 fake_state
->state
= GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD
;
2795 tt_assert(! entry_guard_state_should_expire(fake_state
));
2797 /* And it wouldn't have expired a few seconds ago. */
2798 fake_state
->state
= GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD
;
2799 fake_state
->state_set_at
=
2800 approx_time() - DFLT_NONPRIMARY_GUARD_IDLE_TIMEOUT
+ 5;
2801 tt_assert(! entry_guard_state_should_expire(fake_state
));
2804 tor_free(fake_state
);
2807 /** Test that the number of primary guards can be controlled using torrc */
2809 test_entry_guard_number_of_primaries(void *arg
)
2813 /* Get default value */
2814 tt_int_op(get_n_primary_guards(), OP_EQ
, DFLT_N_PRIMARY_GUARDS
);
2816 /* Set number of primaries using torrc */
2817 get_options_mutable()->NumPrimaryGuards
= 42;
2818 tt_int_op(get_n_primary_guards(), OP_EQ
, 42);
2825 mock_directory_initiate_request(directory_request_t
*req
)
2827 if (req
->guard_state
) {
2828 circuit_guard_state_free(req
->guard_state
);
2832 static networkstatus_t
*mock_ns_val
= NULL
;
2833 static networkstatus_t
*
2834 mock_ns_get_by_flavor(consensus_flavor_t f
)
2840 /** Test that when we fetch microdescriptors we skip guards that have
2841 * previously failed to serve us needed microdescriptors. */
2843 test_entry_guard_outdated_dirserver_exclusion(void *arg
)
2846 response_handler_args_t
*args
= NULL
;
2847 dir_connection_t
*conn
= NULL
;
2850 /* Test prep: Make a new guard selection */
2851 guard_selection_t
*gs
= get_guard_selection_by_name("default",
2854 /* ... we want to use entry guards */
2855 or_options_t
*options
= get_options_mutable();
2856 options
->UseEntryGuards
= 1;
2857 options
->UseBridges
= 0;
2859 /* ... prepare some md digests we want to download in the future */
2860 smartlist_t
*digests
= smartlist_new();
2861 const char *prose
= "unhurried and wise, we perceive.";
2862 for (int i
= 0; i
< 20; i
++) {
2863 smartlist_add(digests
, (char*)prose
);
2866 tt_int_op(smartlist_len(digests
), OP_EQ
, 20);
2868 /* ... now mock some functions */
2869 mock_ns_val
= tor_malloc_zero(sizeof(networkstatus_t
));
2870 MOCK(networkstatus_get_latest_consensus_by_flavor
, mock_ns_get_by_flavor
);
2871 MOCK(directory_initiate_request
, mock_directory_initiate_request
);
2874 * 0. Create a proper guard set and primary guard list.
2875 * 1. Pretend to fail microdescriptor fetches from all the primary guards.
2876 * 2. Order another microdescriptor fetch and make sure that primary guards
2877 * get skipped since they failed previous fetches.
2880 { /* Setup primary guard list */
2882 entry_guards_update_primary(gs
);
2883 for (i
= 0; i
< DFLT_N_PRIMARY_GUARDS
; ++i
) {
2884 entry_guard_t
*guard
= smartlist_get(gs
->sampled_entry_guards
, i
);
2885 make_guard_confirmed(gs
, guard
);
2887 entry_guards_update_primary(gs
);
2891 /* Fail microdesc fetches with all the primary guards */
2892 args
= tor_malloc_zero(sizeof(response_handler_args_t
));
2893 args
->status_code
= 404;
2894 args
->reason
= NULL
;
2898 conn
= tor_malloc_zero(sizeof(dir_connection_t
));
2899 conn
->requested_resource
= tor_strdup("d/jlinblackorigami");
2900 conn
->base_
.purpose
= DIR_PURPOSE_FETCH_MICRODESC
;
2902 /* Pretend to fail fetches with all primary guards */
2903 SMARTLIST_FOREACH_BEGIN(gs
->primary_entry_guards
,const entry_guard_t
*,g
) {
2904 memcpy(conn
->identity_digest
, g
->identity
, DIGEST_LEN
);
2906 retval
= handle_response_fetch_microdesc(conn
, args
);
2907 tt_int_op(retval
, OP_EQ
, 0);
2908 } SMARTLIST_FOREACH_END(g
);
2912 /* Now order the final md download */
2913 setup_full_capture_of_logs(LOG_INFO
);
2914 initiate_descriptor_downloads(NULL
, DIR_PURPOSE_FETCH_MICRODESC
,
2917 /* ... and check that because we failed to fetch microdescs from all our
2918 * primaries, we didn't end up selecting a primary for fetching dir info */
2919 expect_log_msg_containing("No primary or confirmed guards available.");
2920 teardown_capture_of_logs();
2924 UNMOCK(networkstatus_get_latest_consensus_by_flavor
);
2925 UNMOCK(directory_initiate_request
);
2926 smartlist_free(digests
);
2927 tor_free(mock_ns_val
);
2930 tor_free(conn
->requested_resource
);
2935 /** Test helper to extend the <b>oc</b> circuit path <b>n</b> times and then
2936 * ensure that the circuit is now complete. */
2938 helper_extend_circuit_path_n_times(origin_circuit_t
*oc
, int n
)
2943 /* Extend path n times */
2944 for (i
= 0 ; i
< n
; i
++) {
2945 retval
= onion_extend_cpath(oc
);
2946 tt_int_op(retval
, OP_EQ
, 0);
2947 tt_int_op(circuit_get_cpath_len(oc
), OP_EQ
, i
+1);
2950 /* Now do it one last time and see that circ is complete */
2951 retval
= onion_extend_cpath(oc
);
2952 tt_int_op(retval
, OP_EQ
, 1);
2958 /** Test for basic Tor path selection. Makes sure we build 3-hop circuits. */
2960 test_entry_guard_basic_path_selection(void *arg
)
2966 /* Enable entry guards */
2967 or_options_t
*options
= get_options_mutable();
2968 options
->UseEntryGuards
= 1;
2970 /* disables /16 check since all nodes have the same addr... */
2971 options
->EnforceDistinctSubnets
= 0;
2973 /* Create our circuit */
2974 circuit_t
*circ
= dummy_origin_circuit_new(30);
2975 origin_circuit_t
*oc
= TO_ORIGIN_CIRCUIT(circ
);
2976 oc
->build_state
= tor_malloc_zero(sizeof(cpath_build_state_t
));
2978 /* First pick the exit and pin it on the build_state */
2979 retval
= onion_pick_cpath_exit(oc
, NULL
, 0);
2980 tt_int_op(retval
, OP_EQ
, 0);
2982 /* Extend path 3 times. First we pick guard, then middle, then exit. */
2983 helper_extend_circuit_path_n_times(oc
, 3);
2986 circuit_free_(circ
);
2989 /** Test helper to build an L2 and L3 vanguard list. The vanguard lists
2990 * produced should be completely disjoint. */
2992 helper_setup_vanguard_list(or_options_t
*options
)
2996 /* Add some nodes to the vanguard L2 list */
2997 options
->HSLayer2Nodes
= routerset_new();
2998 for (i
= 0; i
< 10 ; i
+= 2) {
2999 node_t
*vanguard_node
= smartlist_get(big_fake_net_nodes
, i
);
3000 tt_assert(vanguard_node
->is_possible_guard
);
3001 routerset_parse(options
->HSLayer2Nodes
, vanguard_node
->rs
->nickname
, "l2");
3003 /* also add some nodes to vanguard L3 list
3004 * (L2 list and L3 list should be disjoint for this test to work) */
3005 options
->HSLayer3Nodes
= routerset_new();
3006 for (i
= 10; i
< 20 ; 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
->HSLayer3Nodes
, vanguard_node
->rs
->nickname
, "l3");
3016 /** Test to ensure that vanguard path selection works properly. Ensures that
3017 * default vanguard circuits are 4 hops, and that path selection works
3018 * correctly given the vanguard settings. */
3020 test_entry_guard_vanguard_path_selection(void *arg
)
3026 /* Enable entry guards */
3027 or_options_t
*options
= get_options_mutable();
3028 options
->UseEntryGuards
= 1;
3030 /* XXX disables /16 check */
3031 options
->EnforceDistinctSubnets
= 0;
3033 /* Setup our vanguard list */
3034 helper_setup_vanguard_list(options
);
3036 /* Create our circuit */
3037 circuit_t
*circ
= dummy_origin_circuit_new(30);
3038 origin_circuit_t
*oc
= TO_ORIGIN_CIRCUIT(circ
);
3039 oc
->build_state
= tor_malloc_zero(sizeof(cpath_build_state_t
));
3040 oc
->build_state
->is_internal
= 1;
3042 /* Switch circuit purpose to vanguards */
3043 circ
->purpose
= CIRCUIT_PURPOSE_HS_VANGUARDS
;
3045 /* First pick the exit and pin it on the build_state */
3046 tt_int_op(oc
->build_state
->desired_path_len
, OP_EQ
, 0);
3047 retval
= onion_pick_cpath_exit(oc
, NULL
, 0);
3048 tt_int_op(retval
, OP_EQ
, 0);
3050 /* Ensure that vanguards make 4-hop circuits by default */
3051 tt_int_op(oc
->build_state
->desired_path_len
, OP_EQ
, 4);
3053 /* Extend path as many times as needed to have complete circ. */
3054 helper_extend_circuit_path_n_times(oc
, oc
->build_state
->desired_path_len
);
3056 /* Test that the cpath linked list is set correctly. */
3057 crypt_path_t
*l1_node
= oc
->cpath
;
3058 crypt_path_t
*l2_node
= l1_node
->next
;
3059 crypt_path_t
*l3_node
= l2_node
->next
;
3060 crypt_path_t
*l4_node
= l3_node
->next
;
3061 crypt_path_t
*l1_node_again
= l4_node
->next
;
3062 tt_ptr_op(l1_node
, OP_EQ
, l1_node_again
);
3064 /* Test that L2 is indeed HSLayer2Node */
3065 retval
= routerset_contains_extendinfo(options
->HSLayer2Nodes
,
3066 l2_node
->extend_info
);
3067 tt_int_op(retval
, OP_EQ
, 4);
3068 /* test that L3 node is _not_ contained in HSLayer2Node */
3069 retval
= routerset_contains_extendinfo(options
->HSLayer2Nodes
,
3070 l3_node
->extend_info
);
3071 tt_int_op(retval
, OP_LT
, 4);
3073 /* Test that L3 is indeed HSLayer3Node */
3074 retval
= routerset_contains_extendinfo(options
->HSLayer3Nodes
,
3075 l3_node
->extend_info
);
3076 tt_int_op(retval
, OP_EQ
, 4);
3077 /* test that L2 node is _not_ contained in HSLayer3Node */
3078 retval
= routerset_contains_extendinfo(options
->HSLayer3Nodes
,
3079 l2_node
->extend_info
);
3080 tt_int_op(retval
, OP_LT
, 4);
3082 /* TODO: Test that L1 can be the same as exit. To test this we need start
3083 enforcing EnforceDistinctSubnets again, which means that we need to give
3084 each test node a different address which currently breaks some tests. */
3087 circuit_free_(circ
);
3090 static const struct testcase_setup_t big_fake_network
= {
3091 big_fake_network_setup
, big_fake_network_cleanup
3094 static const struct testcase_setup_t upgrade_circuits
= {
3095 upgrade_circuits_setup
, upgrade_circuits_cleanup
3099 #define NO_PREFIX_TEST(name) \
3100 { #name, test_ ## name, 0, NULL, NULL }
3102 #define EN_TEST_BASE(name, fork, setup, arg) \
3103 { #name, test_entry_guard_ ## name, fork, setup, (void*)(arg) }
3105 #define EN_TEST(name) EN_TEST_BASE(name, 0, NULL, NULL)
3106 #define EN_TEST_FORK(name) EN_TEST_BASE(name, TT_FORK, NULL, NULL)
3108 #define BFN_TEST(name) \
3109 EN_TEST_BASE(name, TT_FORK, &big_fake_network, NULL), \
3110 { #name "_reasonably_future", test_entry_guard_ ## name, TT_FORK, \
3111 &big_fake_network, (void*)(REASONABLY_FUTURE) }, \
3112 { #name "_reasonably_past", test_entry_guard_ ## name, TT_FORK, \
3113 &big_fake_network, (void*)(REASONABLY_PAST) }
3115 #define UPGRADE_TEST(name, arg) \
3116 EN_TEST_BASE(name, TT_FORK, &upgrade_circuits, arg), \
3117 { #name "_reasonably_future", test_entry_guard_ ## name, TT_FORK, \
3118 &upgrade_circuits, (void*)(arg REASONABLY_FUTURE) }, \
3119 { #name "_reasonably_past", test_entry_guard_ ## name, TT_FORK, \
3120 &upgrade_circuits, (void*)(arg REASONABLY_PAST) }
3121 #endif /* !defined(COCCI) */
3123 struct testcase_t entrynodes_tests
[] = {
3124 NO_PREFIX_TEST(node_preferred_orport
),
3125 NO_PREFIX_TEST(entry_guard_describe
),
3127 EN_TEST(randomize_time
),
3128 EN_TEST(encode_for_state_minimal
),
3129 EN_TEST(encode_for_state_maximal
),
3130 EN_TEST(parse_from_state_minimal
),
3131 EN_TEST(parse_from_state_maximal
),
3132 EN_TEST(parse_from_state_failure
),
3133 EN_TEST(parse_from_state_partial_failure
),
3135 EN_TEST_FORK(parse_from_state_full
),
3136 EN_TEST_FORK(parse_from_state_broken
),
3137 EN_TEST_FORK(get_guard_selection_by_name
),
3138 EN_TEST_FORK(number_of_primaries
),
3140 BFN_TEST(choose_selection_initial
),
3141 BFN_TEST(add_single_guard
),
3142 BFN_TEST(node_filter
),
3143 BFN_TEST(expand_sample
),
3144 BFN_TEST(expand_sample_small_net
),
3145 BFN_TEST(update_from_consensus_status
),
3146 BFN_TEST(update_from_consensus_repair
),
3147 BFN_TEST(update_from_consensus_remove
),
3148 BFN_TEST(confirming_guards
),
3149 BFN_TEST(sample_reachable_filtered
),
3150 BFN_TEST(sample_reachable_filtered_empty
),
3151 BFN_TEST(retry_unreachable
),
3152 BFN_TEST(manage_primary
),
3153 BFN_TEST(correct_cascading_order
),
3155 EN_TEST_FORK(guard_preferred
),
3157 BFN_TEST(select_for_circuit_no_confirmed
),
3158 BFN_TEST(select_for_circuit_confirmed
),
3159 BFN_TEST(select_for_circuit_highlevel_primary
),
3160 BFN_TEST(select_for_circuit_highlevel_confirm_other
),
3161 BFN_TEST(select_for_circuit_highlevel_primary_retry
),
3162 BFN_TEST(select_and_cancel
),
3163 BFN_TEST(drop_guards
),
3164 BFN_TEST(outdated_dirserver_exclusion
),
3165 BFN_TEST(basic_path_selection
),
3166 BFN_TEST(vanguard_path_selection
),
3168 UPGRADE_TEST(upgrade_a_circuit
, "c1-done c2-done"),
3169 UPGRADE_TEST(upgrade_blocked_by_live_primary_guards
, "c1-done c2-done"),
3170 UPGRADE_TEST(upgrade_blocked_by_lack_of_waiting_circuits
, ""),
3171 UPGRADE_TEST(upgrade_blocked_by_better_circ_complete
, "c1-done c2-done"),
3172 UPGRADE_TEST(upgrade_not_blocked_by_restricted_circ_complete
,
3174 UPGRADE_TEST(upgrade_not_blocked_by_worse_circ_complete
, "c1-done c2-done"),
3175 UPGRADE_TEST(upgrade_blocked_by_better_circ_pending
, "c2-done"),
3176 UPGRADE_TEST(upgrade_not_blocked_by_restricted_circ_pending
,
3178 UPGRADE_TEST(upgrade_not_blocked_by_worse_circ_pending
, "c1-done"),
3180 EN_TEST_FORK(should_expire_waiting
),