1 /* Copyright (c) 2014-2018, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
6 #define CIRCUITLIST_PRIVATE
7 #define CIRCUITBUILD_PRIVATE
8 #define STATEFILE_PRIVATE
9 #define ENTRYNODES_PRIVATE
10 #define ROUTERLIST_PRIVATE
11 #define DIRCLIENT_PRIVATE
13 #include "core/or/or.h"
14 #include "test/test.h"
16 #include "feature/client/bridges.h"
17 #include "core/or/circuitlist.h"
18 #include "core/or/circuitbuild.h"
19 #include "app/config/config.h"
20 #include "app/config/confparse.h"
21 #include "lib/crypt_ops/crypto_rand.h"
22 #include "feature/dircommon/directory.h"
23 #include "feature/dirclient/dirclient.h"
24 #include "feature/client/entrynodes.h"
25 #include "feature/nodelist/nodelist.h"
26 #include "feature/nodelist/networkstatus.h"
27 #include "core/or/policies.h"
28 #include "feature/nodelist/routerlist.h"
29 #include "feature/dirparse/routerparse.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
;
72 bfn_mock_nodelist_get_list(void)
74 return big_fake_net_nodes
;
77 static networkstatus_t
*
78 bfn_mock_networkstatus_get_live_consensus(time_t now
)
81 return dummy_consensus
;
85 bfn_mock_node_get_by_id(const char *id
)
87 SMARTLIST_FOREACH(big_fake_net_nodes
, node_t
*, n
,
88 if (fast_memeq(n
->identity
, id
, 20))
94 /* Helper function to free a test node. */
96 test_node_free(node_t
*n
)
99 tor_free(n
->md
->onion_curve25519_pkey
);
100 short_policy_free(n
->md
->exit_policy
);
105 /* Unittest cleanup function: Cleanup the fake network. */
107 big_fake_network_cleanup(const struct testcase_t
*testcase
, void *ptr
)
112 if (big_fake_net_nodes
) {
113 SMARTLIST_FOREACH(big_fake_net_nodes
, node_t
*, n
, {
116 smartlist_free(big_fake_net_nodes
);
119 UNMOCK(nodelist_get_list
);
120 UNMOCK(node_get_by_id
);
121 UNMOCK(get_or_state
);
122 UNMOCK(networkstatus_get_live_consensus
);
123 or_state_free(dummy_state
);
125 tor_free(dummy_consensus
);
130 /* Unittest setup function: Setup a fake network. */
132 big_fake_network_setup(const struct testcase_t
*testcase
)
136 /* These are minimal node_t objects that only contain the aspects of node_t
137 * that we need for entrynodes.c. */
138 const int N_NODES
= 271;
140 big_fake_net_nodes
= smartlist_new();
141 for (i
= 0; i
< N_NODES
; ++i
) {
142 curve25519_secret_key_t curve25519_secret_key
;
144 node_t
*n
= tor_malloc_zero(sizeof(node_t
));
145 n
->md
= tor_malloc_zero(sizeof(microdesc_t
));
147 /* Generate curve25519 key for this node */
148 n
->md
->onion_curve25519_pkey
=
149 tor_malloc_zero(sizeof(curve25519_public_key_t
));
150 curve25519_secret_key_generate(&curve25519_secret_key
, 0);
151 curve25519_public_key_generate(n
->md
->onion_curve25519_pkey
,
152 &curve25519_secret_key
);
154 crypto_rand(n
->identity
, sizeof(n
->identity
));
155 n
->rs
= tor_malloc_zero(sizeof(routerstatus_t
));
157 memcpy(n
->rs
->identity_digest
, n
->identity
, DIGEST_LEN
);
159 n
->is_running
= n
->is_valid
= n
->is_fast
= n
->is_stable
= 1;
161 /* Note: all these guards have the same address, so you'll need to
162 * disable EnforceDistinctSubnets when a restriction is applied. */
163 n
->rs
->addr
= 0x04020202;
164 n
->rs
->or_port
= 1234;
165 n
->rs
->is_v2_dir
= 1;
166 n
->rs
->has_bandwidth
= 1;
167 n
->rs
->bandwidth_kb
= 30;
169 /* Make a random nickname for each node */
171 char nickname_binary
[8];
172 crypto_rand(nickname_binary
, sizeof(nickname_binary
));
173 base32_encode(n
->rs
->nickname
, sizeof(n
->rs
->nickname
),
174 nickname_binary
, sizeof(nickname_binary
));
177 /* Call half of the nodes a possible guard. */
179 n
->is_possible_guard
= 1;
180 n
->rs
->guardfraction_percentage
= 100;
181 n
->rs
->has_guardfraction
= 1;
182 n
->rs
->is_possible_guard
= 1;
185 /* Make some of these nodes a possible exit */
187 n
->md
->exit_policy
= parse_short_policy("accept 443");
190 smartlist_add(big_fake_net_nodes
, n
);
193 dummy_state
= tor_malloc_zero(sizeof(or_state_t
));
194 dummy_consensus
= tor_malloc_zero(sizeof(networkstatus_t
));
195 dummy_consensus
->valid_after
= approx_time() - 3600;
196 dummy_consensus
->valid_until
= approx_time() + 3600;
198 MOCK(nodelist_get_list
, bfn_mock_nodelist_get_list
);
199 MOCK(node_get_by_id
, bfn_mock_node_get_by_id
);
201 get_or_state_replacement
);
202 MOCK(networkstatus_get_live_consensus
,
203 bfn_mock_networkstatus_get_live_consensus
);
204 /* Return anything but NULL (it's interpreted as test fail) */
205 return (void*)testcase
;
209 mock_randomize_time_no_randomization(time_t a
, time_t b
)
215 static or_options_t mocked_options
;
217 static const or_options_t
*
218 mock_get_options(void)
220 return &mocked_options
;
223 #define TEST_IPV4_ADDR "123.45.67.89"
224 #define TEST_IPV6_ADDR "[1234:5678:90ab:cdef::]"
227 test_node_preferred_orport(void *arg
)
230 tor_addr_t ipv4_addr
;
231 const uint16_t ipv4_port
= 4444;
232 tor_addr_t ipv6_addr
;
233 const uint16_t ipv6_port
= 6666;
234 routerinfo_t node_ri
;
239 memset(&mocked_options
, 0, sizeof(mocked_options
));
240 /* We don't test ClientPreferIPv6ORPort here, because it's used in
241 * nodelist_set_consensus to setup node.ipv6_preferred, which we set
243 MOCK(get_options
, mock_get_options
);
245 /* Setup IP addresses */
246 tor_addr_parse(&ipv4_addr
, TEST_IPV4_ADDR
);
247 tor_addr_parse(&ipv6_addr
, TEST_IPV6_ADDR
);
250 memset(&node_ri
, 0, sizeof(node_ri
));
251 node_ri
.addr
= tor_addr_to_ipv4h(&ipv4_addr
);
252 node_ri
.or_port
= ipv4_port
;
253 tor_addr_copy(&node_ri
.ipv6_addr
, &ipv6_addr
);
254 node_ri
.ipv6_orport
= ipv6_port
;
257 memset(&node
, 0, sizeof(node
));
260 /* Check the preferred address is IPv4 if we're only using IPv4, regardless
261 * of whether we prefer it or not */
262 mocked_options
.ClientUseIPv4
= 1;
263 mocked_options
.ClientUseIPv6
= 0;
264 node
.ipv6_preferred
= 0;
265 node_get_pref_orport(&node
, &ap
);
266 tt_assert(tor_addr_eq(&ap
.addr
, &ipv4_addr
));
267 tt_assert(ap
.port
== ipv4_port
);
269 node
.ipv6_preferred
= 1;
270 node_get_pref_orport(&node
, &ap
);
271 tt_assert(tor_addr_eq(&ap
.addr
, &ipv4_addr
));
272 tt_assert(ap
.port
== ipv4_port
);
274 /* Check the preferred address is IPv4 if we're using IPv4 and IPv6, but
275 * don't prefer the IPv6 address */
276 mocked_options
.ClientUseIPv4
= 1;
277 mocked_options
.ClientUseIPv6
= 1;
278 node
.ipv6_preferred
= 0;
279 node_get_pref_orport(&node
, &ap
);
280 tt_assert(tor_addr_eq(&ap
.addr
, &ipv4_addr
));
281 tt_assert(ap
.port
== ipv4_port
);
283 /* Check the preferred address is IPv6 if we prefer it and
284 * ClientUseIPv6 is 1, regardless of ClientUseIPv4 */
285 mocked_options
.ClientUseIPv4
= 1;
286 mocked_options
.ClientUseIPv6
= 1;
287 node
.ipv6_preferred
= 1;
288 node_get_pref_orport(&node
, &ap
);
289 tt_assert(tor_addr_eq(&ap
.addr
, &ipv6_addr
));
290 tt_assert(ap
.port
== ipv6_port
);
292 mocked_options
.ClientUseIPv4
= 0;
293 node_get_pref_orport(&node
, &ap
);
294 tt_assert(tor_addr_eq(&ap
.addr
, &ipv6_addr
));
295 tt_assert(ap
.port
== ipv6_port
);
297 /* Check the preferred address is IPv6 if we don't prefer it, but
298 * ClientUseIPv4 is 0 */
299 mocked_options
.ClientUseIPv4
= 0;
300 mocked_options
.ClientUseIPv6
= 1;
301 node
.ipv6_preferred
= fascist_firewall_prefer_ipv6_orport(&mocked_options
);
302 node_get_pref_orport(&node
, &ap
);
303 tt_assert(tor_addr_eq(&ap
.addr
, &ipv6_addr
));
304 tt_assert(ap
.port
== ipv6_port
);
311 test_entry_guard_describe(void *arg
)
315 memset(&g
, 0, sizeof(g
));
316 strlcpy(g
.nickname
, "okefenokee", sizeof(g
.nickname
));
317 memcpy(g
.identity
, "theforestprimeval---", DIGEST_LEN
);
319 tt_str_op(entry_guard_describe(&g
), OP_EQ
,
320 "okefenokee ($746865666F726573747072696D6576616C2D2D2D)");
327 test_entry_guard_randomize_time(void *arg
)
329 const time_t now
= 1479153573;
330 const int delay
= 86400;
336 for (i
= 0; i
< N
; ++i
) {
337 t
= randomize_time(now
, delay
);
338 tt_int_op(t
, OP_LE
, now
);
339 tt_int_op(t
, OP_GE
, now
-delay
);
342 /* now try the corner cases */
343 for (i
= 0; i
< N
; ++i
) {
344 t
= randomize_time(100, delay
);
345 tt_int_op(t
, OP_GE
, 1);
346 tt_int_op(t
, OP_LE
, 100);
348 t
= randomize_time(0, delay
);
349 tt_int_op(t
, OP_EQ
, 1);
357 test_entry_guard_encode_for_state_minimal(void *arg
)
360 entry_guard_t
*eg
= tor_malloc_zero(sizeof(entry_guard_t
));
362 eg
->selection_name
= tor_strdup("wubwub");
363 memcpy(eg
->identity
, "plurpyflurpyslurpydo", DIGEST_LEN
);
364 eg
->sampled_on_date
= 1479081600;
365 eg
->confirmed_idx
= -1;
368 s
= entry_guard_encode_for_state(eg
);
372 "rsa_id=706C75727079666C75727079736C75727079646F "
373 "sampled_on=2016-11-14T00:00:00 "
377 entry_guard_free(eg
);
382 test_entry_guard_encode_for_state_maximal(void *arg
)
385 entry_guard_t
*eg
= tor_malloc_zero(sizeof(entry_guard_t
));
387 strlcpy(eg
->nickname
, "Fred", sizeof(eg
->nickname
));
388 eg
->selection_name
= tor_strdup("default");
389 memcpy(eg
->identity
, "plurpyflurpyslurpydo", DIGEST_LEN
);
390 eg
->bridge_addr
= tor_malloc_zero(sizeof(tor_addr_port_t
));
391 tor_addr_from_ipv4h(&eg
->bridge_addr
->addr
, 0x08080404);
392 eg
->bridge_addr
->port
= 9999;
393 eg
->sampled_on_date
= 1479081600;
394 eg
->sampled_by_version
= tor_strdup("1.2.3");
395 eg
->unlisted_since_date
= 1479081645;
396 eg
->currently_listed
= 1;
397 eg
->confirmed_on_date
= 1479081690;
398 eg
->confirmed_idx
= 333;
399 eg
->extra_state_fields
= tor_strdup("and the green grass grew all around");
402 s
= entry_guard_encode_for_state(eg
);
406 "rsa_id=706C75727079666C75727079736C75727079646F "
407 "bridge_addr=8.8.4.4:9999 "
409 "sampled_on=2016-11-14T00:00:00 "
411 "unlisted_since=2016-11-14T00:00:45 "
413 "confirmed_on=2016-11-14T00:01:30 "
415 "and the green grass grew all around");
418 entry_guard_free(eg
);
423 test_entry_guard_parse_from_state_minimal(void *arg
)
426 char *mem_op_hex_tmp
= NULL
;
427 entry_guard_t
*eg
= NULL
;
428 time_t t
= approx_time();
430 eg
= entry_guard_parse_from_state(
432 "rsa_id=596f75206d6179206e656564206120686f626279");
435 tt_str_op(eg
->selection_name
, OP_EQ
, "default_plus");
436 test_mem_op_hex(eg
->identity
, OP_EQ
,
437 "596f75206d6179206e656564206120686f626279");
438 tt_str_op(eg
->nickname
, OP_EQ
, "$596F75206D6179206E656564206120686F626279");
439 tt_ptr_op(eg
->bridge_addr
, OP_EQ
, NULL
);
440 tt_i64_op(eg
->sampled_on_date
, OP_GE
, t
);
441 tt_i64_op(eg
->sampled_on_date
, OP_LE
, t
+86400);
442 tt_i64_op(eg
->unlisted_since_date
, OP_EQ
, 0);
443 tt_ptr_op(eg
->sampled_by_version
, OP_EQ
, NULL
);
444 tt_int_op(eg
->currently_listed
, OP_EQ
, 0);
445 tt_i64_op(eg
->confirmed_on_date
, OP_EQ
, 0);
446 tt_int_op(eg
->confirmed_idx
, OP_EQ
, -1);
448 tt_int_op(eg
->last_tried_to_connect
, OP_EQ
, 0);
449 tt_int_op(eg
->is_reachable
, OP_EQ
, GUARD_REACHABLE_MAYBE
);
452 entry_guard_free(eg
);
453 tor_free(mem_op_hex_tmp
);
457 test_entry_guard_parse_from_state_maximal(void *arg
)
460 char *mem_op_hex_tmp
= NULL
;
461 entry_guard_t
*eg
= NULL
;
463 eg
= entry_guard_parse_from_state(
465 "rsa_id=706C75727079666C75727079736C75727079646F "
466 "bridge_addr=[1::3]:9999 "
468 "sampled_on=2016-11-14T00:00:00 "
470 "unlisted_since=2016-11-14T00:00:45 "
472 "confirmed_on=2016-11-14T00:01:30 "
474 "and the green grass grew all around "
475 "rsa_id=all,around");
478 test_mem_op_hex(eg
->identity
, OP_EQ
,
479 "706C75727079666C75727079736C75727079646F");
480 tt_str_op(fmt_addr(&eg
->bridge_addr
->addr
), OP_EQ
, "1::3");
481 tt_int_op(eg
->bridge_addr
->port
, OP_EQ
, 9999);
482 tt_str_op(eg
->nickname
, OP_EQ
, "Fred");
483 tt_i64_op(eg
->sampled_on_date
, OP_EQ
, 1479081600);
484 tt_i64_op(eg
->unlisted_since_date
, OP_EQ
, 1479081645);
485 tt_str_op(eg
->sampled_by_version
, OP_EQ
, "1.2.3");
486 tt_int_op(eg
->currently_listed
, OP_EQ
, 1);
487 tt_i64_op(eg
->confirmed_on_date
, OP_EQ
, 1479081690);
488 tt_int_op(eg
->confirmed_idx
, OP_EQ
, 333);
489 tt_str_op(eg
->extra_state_fields
, OP_EQ
,
490 "and the green grass grew all around rsa_id=all,around");
492 tt_int_op(eg
->last_tried_to_connect
, OP_EQ
, 0);
493 tt_int_op(eg
->is_reachable
, OP_EQ
, GUARD_REACHABLE_MAYBE
);
496 entry_guard_free(eg
);
497 tor_free(mem_op_hex_tmp
);
501 test_entry_guard_parse_from_state_failure(void *arg
)
504 entry_guard_t
*eg
= NULL
;
507 eg
= entry_guard_parse_from_state(
508 "rsa_id=596f75206d6179206e656564206120686f626270");
509 tt_ptr_op(eg
, OP_EQ
, NULL
);
512 eg
= entry_guard_parse_from_state("in=default nickname=Fred");
513 tt_ptr_op(eg
, OP_EQ
, NULL
);
515 /* Bad RSA ID: bad character. */
516 eg
= entry_guard_parse_from_state(
518 "rsa_id=596f75206d6179206e656564206120686f62627q");
519 tt_ptr_op(eg
, OP_EQ
, NULL
);
521 /* Bad RSA ID: too long.*/
522 eg
= entry_guard_parse_from_state(
524 "rsa_id=596f75206d6179206e656564206120686f6262703");
525 tt_ptr_op(eg
, OP_EQ
, NULL
);
527 /* Bad RSA ID: too short.*/
528 eg
= entry_guard_parse_from_state(
530 "rsa_id=596f75206d6179206e65656420612");
531 tt_ptr_op(eg
, OP_EQ
, NULL
);
534 entry_guard_free(eg
);
538 test_entry_guard_parse_from_state_partial_failure(void *arg
)
541 char *mem_op_hex_tmp
= NULL
;
542 entry_guard_t
*eg
= NULL
;
543 time_t t
= approx_time();
545 eg
= entry_guard_parse_from_state(
547 "rsa_id=706C75727079666C75727079736C75727079646F "
548 "bridge_addr=1.2.3.3.4:5 "
549 "nickname=FredIsANodeWithAStrangeNicknameThatIsTooLong "
550 "sampled_on=2016-11-14T00:00:99 "
551 "sampled_by=1.2.3 stuff in the middle "
552 "unlisted_since=2016-xx-14T00:00:45 "
554 "confirmed_on=2016-11-14T00:01:30zz "
556 "and the green grass grew all around "
557 "rsa_id=all,around");
560 test_mem_op_hex(eg
->identity
, OP_EQ
,
561 "706C75727079666C75727079736C75727079646F");
562 tt_str_op(eg
->nickname
, OP_EQ
, "FredIsANodeWithAStrangeNicknameThatIsTooL");
563 tt_ptr_op(eg
->bridge_addr
, OP_EQ
, NULL
);
564 tt_i64_op(eg
->sampled_on_date
, OP_EQ
, t
);
565 tt_i64_op(eg
->unlisted_since_date
, OP_EQ
, 0);
566 tt_str_op(eg
->sampled_by_version
, OP_EQ
, "1.2.3");
567 tt_int_op(eg
->currently_listed
, OP_EQ
, 0);
568 tt_i64_op(eg
->confirmed_on_date
, OP_EQ
, 0);
569 tt_int_op(eg
->confirmed_idx
, OP_EQ
, -1);
570 tt_str_op(eg
->extra_state_fields
, OP_EQ
,
571 "stuff in the middle and the green grass grew all around "
572 "rsa_id=all,around");
574 tt_int_op(eg
->last_tried_to_connect
, OP_EQ
, 0);
575 tt_int_op(eg
->is_reachable
, OP_EQ
, GUARD_REACHABLE_MAYBE
);
578 entry_guard_free(eg
);
579 tor_free(mem_op_hex_tmp
);
583 mock_entry_guard_is_listed(guard_selection_t
*gs
, const entry_guard_t
*guard
)
591 test_entry_guard_parse_from_state_full(void *arg
)
594 /* Here's a state I made while testing. The identities and locations for
595 * the bridges are redacted. */
597 "Guard in=default rsa_id=214F44BD5B638E8C817D47FF7C97397790BF0345 "
598 "nickname=TotallyNinja sampled_on=2016-11-12T19:32:49 "
599 "sampled_by=0.3.0.0-alpha-dev "
601 "Guard in=default rsa_id=052900AB0EA3ED54BAB84AE8A99E74E8693CE2B2 "
602 "nickname=5OfNovember sampled_on=2016-11-20T04:32:05 "
603 "sampled_by=0.3.0.0-alpha-dev "
604 "listed=1 confirmed_on=2016-11-22T08:13:28 confirmed_idx=0 "
605 "pb_circ_attempts=4.000000 pb_circ_successes=2.000000 "
606 "pb_successful_circuits_closed=2.000000\n"
607 "Guard in=default rsa_id=7B700C0C207EBD0002E00F499BE265519AC3C25A "
608 "nickname=dc6jgk11 sampled_on=2016-11-28T11:50:13 "
609 "sampled_by=0.3.0.0-alpha-dev "
610 "listed=1 confirmed_on=2016-11-24T08:45:30 confirmed_idx=4 "
611 "pb_circ_attempts=5.000000 pb_circ_successes=5.000000 "
612 "pb_successful_circuits_closed=5.000000\n"
613 "Guard in=wobblesome rsa_id=7B700C0C207EBD0002E00F499BE265519AC3C25A "
614 "nickname=dc6jgk11 sampled_on=2016-11-28T11:50:13 "
615 "sampled_by=0.3.0.0-alpha-dev "
617 "Guard in=default rsa_id=E9025AD60D86875D5F11548D536CC6AF60F0EF5E "
618 "nickname=maibrunn sampled_on=2016-11-25T22:36:38 "
619 "sampled_by=0.3.0.0-alpha-dev listed=1\n"
620 "Guard in=default rsa_id=DCD30B90BA3A792DA75DC54A327EF353FB84C38E "
621 "nickname=Unnamed sampled_on=2016-11-25T14:34:00 "
622 "sampled_by=0.3.0.0-alpha-dev listed=1\n"
623 "Guard in=bridges rsa_id=8FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2E "
624 "bridge_addr=24.1.1.1:443 sampled_on=2016-11-25T06:44:14 "
625 "sampled_by=0.3.0.0-alpha-dev listed=1 "
626 "confirmed_on=2016-11-29T10:36:06 confirmed_idx=0 "
627 "pb_circ_attempts=8.000000 pb_circ_successes=8.000000 "
628 "pb_successful_circuits_closed=13.000000\n"
629 "Guard in=bridges rsa_id=5800000000000000000000000000000000000000 "
630 "bridge_addr=37.218.246.143:28366 "
631 "sampled_on=2016-11-18T15:07:34 sampled_by=0.3.0.0-alpha-dev listed=1\n";
633 config_line_t
*lines
= NULL
;
634 or_state_t
*state
= tor_malloc_zero(sizeof(or_state_t
));
635 int r
= config_get_lines(STATE
, &lines
, 0);
637 smartlist_t
*text
= smartlist_new();
640 // So nodes aren't expired. This is Tue, 13 Dec 2016 09:37:14 GMT
641 update_approx_time(1481621834);
643 MOCK(entry_guard_is_listed
, mock_entry_guard_is_listed
);
647 get_or_state_replacement
);
649 tt_int_op(r
, OP_EQ
, 0);
652 state
->Guard
= lines
;
654 /* Try it first without setting the result. */
655 r
= entry_guards_parse_state(state
, 0, &msg
);
656 tt_int_op(r
, OP_EQ
, 0);
657 guard_selection_t
*gs_br
=
658 get_guard_selection_by_name("bridges", GS_TYPE_BRIDGE
, 0);
659 tt_ptr_op(gs_br
, OP_EQ
, NULL
);
661 r
= entry_guards_parse_state(state
, 1, &msg
);
662 tt_int_op(r
, OP_EQ
, 0);
663 gs_br
= get_guard_selection_by_name("bridges", GS_TYPE_BRIDGE
, 0);
664 guard_selection_t
*gs_df
=
665 get_guard_selection_by_name("default", GS_TYPE_NORMAL
, 0);
666 guard_selection_t
*gs_wb
=
667 get_guard_selection_by_name("wobblesome", GS_TYPE_NORMAL
, 0);
673 tt_int_op(smartlist_len(gs_df
->sampled_entry_guards
), OP_EQ
, 5);
674 tt_int_op(smartlist_len(gs_br
->sampled_entry_guards
), OP_EQ
, 2);
675 tt_int_op(smartlist_len(gs_wb
->sampled_entry_guards
), OP_EQ
, 1);
677 /* Try again; make sure it doesn't double-add the guards. */
678 r
= entry_guards_parse_state(state
, 1, &msg
);
679 tt_int_op(r
, OP_EQ
, 0);
680 gs_br
= get_guard_selection_by_name("bridges", GS_TYPE_BRIDGE
, 0);
681 gs_df
= get_guard_selection_by_name("default", GS_TYPE_NORMAL
, 0);
684 tt_int_op(smartlist_len(gs_df
->sampled_entry_guards
), OP_EQ
, 5);
685 tt_int_op(smartlist_len(gs_br
->sampled_entry_guards
), OP_EQ
, 2);
687 /* Re-encode; it should be the same... almost. */
689 /* (Make a guard nonpersistent first) */
690 entry_guard_t
*g
= smartlist_get(gs_df
->sampled_entry_guards
, 0);
691 g
->is_persistent
= 0;
693 config_free_lines(lines
);
694 lines
= state
->Guard
= NULL
; // to prevent double-free.
695 entry_guards_update_state(state
);
696 tt_assert(state
->Guard
);
697 lines
= state
->Guard
;
700 for (ln
= lines
; ln
; ln
= ln
->next
) {
701 smartlist_add_asprintf(text
, "%s %s\n",ln
->key
, ln
->value
);
703 joined
= smartlist_join_strings(text
, "", 0, NULL
);
704 tt_str_op(joined
, OP_EQ
,
705 "Guard in=default rsa_id=052900AB0EA3ED54BAB84AE8A99E74E8693CE2B2 "
706 "nickname=5OfNovember sampled_on=2016-11-20T04:32:05 "
707 "sampled_by=0.3.0.0-alpha-dev "
708 "listed=1 confirmed_on=2016-11-22T08:13:28 confirmed_idx=0 "
709 "pb_circ_attempts=4.000000 pb_circ_successes=2.000000 "
710 "pb_successful_circuits_closed=2.000000\n"
711 "Guard in=default rsa_id=7B700C0C207EBD0002E00F499BE265519AC3C25A "
712 "nickname=dc6jgk11 sampled_on=2016-11-28T11:50:13 "
713 "sampled_by=0.3.0.0-alpha-dev "
714 "listed=1 confirmed_on=2016-11-24T08:45:30 confirmed_idx=1 "
715 "pb_circ_attempts=5.000000 pb_circ_successes=5.000000 "
716 "pb_successful_circuits_closed=5.000000\n"
717 "Guard in=default rsa_id=E9025AD60D86875D5F11548D536CC6AF60F0EF5E "
718 "nickname=maibrunn sampled_on=2016-11-25T22:36:38 "
719 "sampled_by=0.3.0.0-alpha-dev listed=1\n"
720 "Guard in=default rsa_id=DCD30B90BA3A792DA75DC54A327EF353FB84C38E "
721 "nickname=Unnamed sampled_on=2016-11-25T14:34:00 "
722 "sampled_by=0.3.0.0-alpha-dev listed=1\n"
723 "Guard in=wobblesome rsa_id=7B700C0C207EBD0002E00F499BE265519AC3C25A "
724 "nickname=dc6jgk11 sampled_on=2016-11-28T11:50:13 "
725 "sampled_by=0.3.0.0-alpha-dev "
727 "Guard in=bridges rsa_id=8FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2E "
728 "bridge_addr=24.1.1.1:443 sampled_on=2016-11-25T06:44:14 "
729 "sampled_by=0.3.0.0-alpha-dev listed=1 "
730 "confirmed_on=2016-11-29T10:36:06 confirmed_idx=0 "
731 "pb_circ_attempts=8.000000 pb_circ_successes=8.000000 "
732 "pb_successful_circuits_closed=13.000000\n"
733 "Guard in=bridges rsa_id=5800000000000000000000000000000000000000 "
734 "bridge_addr=37.218.246.143:28366 "
735 "sampled_on=2016-11-18T15:07:34 sampled_by=0.3.0.0-alpha-dev listed=1\n");
738 config_free_lines(lines
);
741 UNMOCK(get_or_state
);
742 UNMOCK(entry_guard_is_listed
);
743 SMARTLIST_FOREACH(text
, char *, cp
, tor_free(cp
));
744 smartlist_free(text
);
749 test_entry_guard_parse_from_state_broken(void *arg
)
752 /* Here's a variation on the previous state. Every line but the first is
756 "Guard in=default rsa_id=214F44BD5B638E8C817D47FF7C97397790BF0345 "
757 "nickname=TotallyNinja sampled_on=2016-11-12T19:32:49 "
758 "sampled_by=0.3.0.0-alpha-dev "
760 /* No selection listed. */
761 "Guard rsa_id=052900AB0EA3ED54BAB84AE8A99E74E8693CE2B2 "
762 "nickname=5OfNovember sampled_on=2016-11-20T04:32:05 "
763 "sampled_by=0.3.0.0-alpha-dev "
764 "listed=1 confirmed_on=2016-11-22T08:13:28 confirmed_idx=0 "
765 "pb_circ_attempts=4.000000 pb_circ_successes=2.000000 "
766 "pb_successful_circuits_closed=2.000000\n"
767 /* Selection is "legacy"!! */
768 "Guard in=legacy rsa_id=7B700C0C207EBD0002E00F499BE265519AC3C25A "
769 "nickname=dc6jgk11 sampled_on=2016-11-28T11:50:13 "
770 "sampled_by=0.3.0.0-alpha-dev "
771 "listed=1 confirmed_on=2016-11-24T08:45:30 confirmed_idx=4 "
772 "pb_circ_attempts=5.000000 pb_circ_successes=5.000000 "
773 "pb_successful_circuits_closed=5.000000\n";
775 config_line_t
*lines
= NULL
;
776 or_state_t
*state
= tor_malloc_zero(sizeof(or_state_t
));
777 int r
= config_get_lines(STATE
, &lines
, 0);
782 get_or_state_replacement
);
784 tt_int_op(r
, OP_EQ
, 0);
787 state
->Guard
= lines
;
789 /* First, no-set case. we should get an error. */
790 r
= entry_guards_parse_state(state
, 0, &msg
);
791 tt_int_op(r
, OP_LT
, 0);
792 tt_ptr_op(msg
, OP_NE
, NULL
);
793 /* And we shouldn't have made anything. */
794 guard_selection_t
*gs_df
=
795 get_guard_selection_by_name("default", GS_TYPE_NORMAL
, 0);
796 tt_ptr_op(gs_df
, OP_EQ
, NULL
);
799 /* Now see about the set case (which shouldn't happen IRL) */
800 r
= entry_guards_parse_state(state
, 1, &msg
);
801 tt_int_op(r
, OP_LT
, 0);
802 tt_ptr_op(msg
, OP_NE
, NULL
);
803 gs_df
= get_guard_selection_by_name("default", GS_TYPE_NORMAL
, 0);
804 tt_ptr_op(gs_df
, OP_NE
, NULL
);
805 tt_int_op(smartlist_len(gs_df
->sampled_entry_guards
), OP_EQ
, 1);
808 config_free_lines(lines
);
811 UNMOCK(get_or_state
);
815 test_entry_guard_get_guard_selection_by_name(void *arg
)
818 guard_selection_t
*gs1
, *gs2
, *gs3
;
820 gs1
= get_guard_selection_by_name("unlikely", GS_TYPE_NORMAL
, 0);
821 tt_ptr_op(gs1
, OP_EQ
, NULL
);
822 gs1
= get_guard_selection_by_name("unlikely", GS_TYPE_NORMAL
, 1);
823 tt_ptr_op(gs1
, OP_NE
, NULL
);
824 gs2
= get_guard_selection_by_name("unlikely", GS_TYPE_NORMAL
, 1);
825 tt_assert(gs2
== gs1
);
826 gs2
= get_guard_selection_by_name("unlikely", GS_TYPE_NORMAL
, 0);
827 tt_assert(gs2
== gs1
);
829 gs2
= get_guard_selection_by_name("implausible", GS_TYPE_NORMAL
, 0);
830 tt_ptr_op(gs2
, OP_EQ
, NULL
);
831 gs2
= get_guard_selection_by_name("implausible", GS_TYPE_NORMAL
, 1);
832 tt_ptr_op(gs2
, OP_NE
, NULL
);
833 tt_assert(gs2
!= gs1
);
834 gs3
= get_guard_selection_by_name("implausible", GS_TYPE_NORMAL
, 0);
835 tt_assert(gs3
== gs2
);
837 gs3
= get_guard_selection_by_name("default", GS_TYPE_NORMAL
, 0);
838 tt_ptr_op(gs3
, OP_EQ
, NULL
);
839 gs3
= get_guard_selection_by_name("default", GS_TYPE_NORMAL
, 1);
840 tt_ptr_op(gs3
, OP_NE
, NULL
);
841 tt_assert(gs3
!= gs2
);
842 tt_assert(gs3
!= gs1
);
843 tt_assert(gs3
== get_guard_selection_info());
846 entry_guards_free_all();
850 test_entry_guard_choose_selection_initial(void *arg
)
852 /* Tests for picking our initial guard selection (based on having had
853 * no previous selection */
855 guard_selection_type_t type
= GS_TYPE_INFER
;
856 const char *name
= choose_guard_selection(get_options(),
857 dummy_consensus
, NULL
, &type
);
858 tt_str_op(name
, OP_EQ
, "default");
859 tt_int_op(type
, OP_EQ
, GS_TYPE_NORMAL
);
861 /* If we're using bridges, we get the bridge selection. */
862 get_options_mutable()->UseBridges
= 1;
863 name
= choose_guard_selection(get_options(),
864 dummy_consensus
, NULL
, &type
);
865 tt_str_op(name
, OP_EQ
, "bridges");
866 tt_int_op(type
, OP_EQ
, GS_TYPE_BRIDGE
);
867 get_options_mutable()->UseBridges
= 0;
869 /* If we discard >99% of our guards, though, we should be in the restricted
871 tt_assert(get_options_mutable()->EntryNodes
== NULL
);
872 get_options_mutable()->EntryNodes
= routerset_new();
873 routerset_parse(get_options_mutable()->EntryNodes
, "1.0.0.0/8", "foo");
874 name
= choose_guard_selection(get_options(),
875 dummy_consensus
, NULL
, &type
);
876 tt_str_op(name
, OP_EQ
, "restricted");
877 tt_int_op(type
, OP_EQ
, GS_TYPE_RESTRICTED
);
884 test_entry_guard_add_single_guard(void *arg
)
887 guard_selection_t
*gs
= guard_selection_new("default", GS_TYPE_NORMAL
);
889 /* 1: Add a single guard to the sample. */
890 node_t
*n1
= smartlist_get(big_fake_net_nodes
, 0);
891 time_t now
= approx_time();
892 tt_assert(n1
->is_possible_guard
== 1);
893 entry_guard_t
*g1
= entry_guard_add_to_sample(gs
, n1
);
896 /* Make sure its fields look right. */
897 tt_mem_op(n1
->identity
, OP_EQ
, g1
->identity
, DIGEST_LEN
);
898 tt_i64_op(g1
->sampled_on_date
, OP_GE
, now
- 12*86400);
899 tt_i64_op(g1
->sampled_on_date
, OP_LE
, now
);
900 tt_str_op(g1
->sampled_by_version
, OP_EQ
, VERSION
);
901 tt_uint_op(g1
->currently_listed
, OP_EQ
, 1);
902 tt_i64_op(g1
->confirmed_on_date
, OP_EQ
, 0);
903 tt_int_op(g1
->confirmed_idx
, OP_EQ
, -1);
904 tt_int_op(g1
->last_tried_to_connect
, OP_EQ
, 0);
905 tt_uint_op(g1
->is_reachable
, OP_EQ
, GUARD_REACHABLE_MAYBE
);
906 tt_i64_op(g1
->failing_since
, OP_EQ
, 0);
907 tt_uint_op(g1
->is_filtered_guard
, OP_EQ
, 1);
908 tt_uint_op(g1
->is_usable_filtered_guard
, OP_EQ
, 1);
909 tt_uint_op(g1
->is_primary
, OP_EQ
, 0);
910 tt_ptr_op(g1
->extra_state_fields
, OP_EQ
, NULL
);
912 /* Make sure it got added. */
913 tt_int_op(1, OP_EQ
, smartlist_len(gs
->sampled_entry_guards
));
914 tt_ptr_op(g1
, OP_EQ
, smartlist_get(gs
->sampled_entry_guards
, 0));
915 tt_ptr_op(g1
, OP_EQ
, get_sampled_guard_with_id(gs
, (uint8_t*)n1
->identity
));
916 const uint8_t bad_id
[20] = {0};
917 tt_ptr_op(NULL
, OP_EQ
, get_sampled_guard_with_id(gs
, bad_id
));
920 guard_selection_free(gs
);
924 test_entry_guard_node_filter(void *arg
)
927 guard_selection_t
*gs
= guard_selection_new("default", GS_TYPE_NORMAL
);
928 bridge_line_t
*bl
= NULL
;
930 /* Initialize a bunch of node objects that are all guards. */
933 entry_guard_t
*g
[NUM
];
935 for (i
=0; i
< NUM
; ++i
) {
936 n
[i
] = smartlist_get(big_fake_net_nodes
, i
*2); // even ones are guards.
937 g
[i
] = entry_guard_add_to_sample(gs
, n
[i
]);
939 // everything starts out filtered-in
940 tt_uint_op(g
[i
]->is_filtered_guard
, OP_EQ
, 1);
941 tt_uint_op(g
[i
]->is_usable_filtered_guard
, OP_EQ
, 1);
943 tt_int_op(num_reachable_filtered_guards(gs
, NULL
), OP_EQ
, NUM
);
945 /* Make sure refiltering doesn't hurt */
946 entry_guards_update_filtered_sets(gs
);
947 for (i
= 0; i
< NUM
; ++i
) {
948 tt_uint_op(g
[i
]->is_filtered_guard
, OP_EQ
, 1);
949 tt_uint_op(g
[i
]->is_usable_filtered_guard
, OP_EQ
, 1);
951 tt_int_op(num_reachable_filtered_guards(gs
, NULL
), OP_EQ
, NUM
);
953 /* Now start doing things to make the guards get filtered out, 1 by 1. */
956 g
[0]->currently_listed
= 0;
958 /* 1: path bias says this guard is maybe eeeevil. */
959 g
[1]->pb
.path_bias_disabled
= 1;
961 /* 2: Unreachable address. */
964 /* 3: ExcludeNodes */
965 n
[3]->rs
->addr
= 0x90902020;
966 routerset_free(get_options_mutable()->ExcludeNodes
);
967 get_options_mutable()->ExcludeNodes
= routerset_new();
968 routerset_parse(get_options_mutable()->ExcludeNodes
, "144.144.0.0/16", "");
971 get_options_mutable()->UseBridges
= 1;
973 bl
= tor_malloc_zero(sizeof(bridge_line_t
));
974 tor_addr_from_ipv4h(&bl
->addr
, n
[4]->rs
->addr
);
975 bl
->port
= n
[4]->rs
->or_port
;
976 memcpy(bl
->digest
, n
[4]->identity
, 20);
977 bridge_add_from_config(bl
);
978 bl
= NULL
; // prevent free.
979 get_options_mutable()->UseBridges
= 0;
981 /* 5: Unreachable. This stays in the filter, but isn't in usable-filtered */
982 g
[5]->last_tried_to_connect
= approx_time(); // prevent retry.
983 g
[5]->is_reachable
= GUARD_REACHABLE_NO
;
987 /* Now refilter and inspect. */
988 entry_guards_update_filtered_sets(gs
);
989 for (i
= 0; i
< NUM
; ++i
) {
990 tt_assert(g
[i
]->is_filtered_guard
== (i
== 5 || i
== 6));
991 tt_assert(g
[i
]->is_usable_filtered_guard
== (i
== 6));
993 tt_int_op(num_reachable_filtered_guards(gs
, NULL
), OP_EQ
, 1);
995 /* Now make sure we have no live consensus, and no nodes. Nothing should
996 * pass the filter any more. */
997 tor_free(dummy_consensus
);
998 dummy_consensus
= NULL
;
999 SMARTLIST_FOREACH(big_fake_net_nodes
, node_t
*, node
, {
1000 memset(node
->identity
, 0xff, 20);
1002 entry_guards_update_filtered_sets(gs
);
1003 for (i
= 0; i
< NUM
; ++i
) {
1004 tt_uint_op(g
[i
]->is_filtered_guard
, OP_EQ
, 0);
1005 tt_uint_op(g
[i
]->is_usable_filtered_guard
, OP_EQ
, 0);
1007 tt_int_op(num_reachable_filtered_guards(gs
, NULL
), OP_EQ
, 0);
1010 guard_selection_free(gs
);
1016 test_entry_guard_expand_sample(void *arg
)
1019 guard_selection_t
*gs
= guard_selection_new("default", GS_TYPE_NORMAL
);
1020 digestmap_t
*node_by_id
= digestmap_new();
1022 entry_guard_t
*guard
= entry_guards_expand_sample(gs
);
1023 tt_assert(guard
); // the last guard returned.
1025 // Every sampled guard here should be filtered and reachable for now.
1026 tt_int_op(smartlist_len(gs
->sampled_entry_guards
), OP_EQ
,
1027 num_reachable_filtered_guards(gs
, NULL
));
1029 /* Make sure we got the right number. */
1030 tt_int_op(DFLT_MIN_FILTERED_SAMPLE_SIZE
, OP_EQ
,
1031 num_reachable_filtered_guards(gs
, NULL
));
1033 // Make sure everything we got was from our fake node list, and everything
1035 SMARTLIST_FOREACH_BEGIN(gs
->sampled_entry_guards
, entry_guard_t
*, g
) {
1036 const node_t
*n
= bfn_mock_node_get_by_id(g
->identity
);
1038 tt_ptr_op(NULL
, OP_EQ
, digestmap_get(node_by_id
, g
->identity
));
1039 digestmap_set(node_by_id
, g
->identity
, (void*) n
);
1040 int idx
= smartlist_pos(big_fake_net_nodes
, n
);
1041 // The even ones are the guards; make sure we got guards.
1042 tt_int_op(idx
& 1, OP_EQ
, 0);
1043 } SMARTLIST_FOREACH_END(g
);
1045 // Nothing became unusable/unfiltered, so a subsequent expand should
1047 guard
= entry_guards_expand_sample(gs
);
1048 tt_ptr_op(guard
, OP_EQ
, NULL
); // no guard was added.
1049 tt_int_op(DFLT_MIN_FILTERED_SAMPLE_SIZE
, OP_EQ
,
1050 num_reachable_filtered_guards(gs
, NULL
));
1052 // Make a few guards unreachable.
1053 guard
= smartlist_get(gs
->sampled_entry_guards
, 0);
1054 guard
->is_usable_filtered_guard
= 0;
1055 guard
= smartlist_get(gs
->sampled_entry_guards
, 1);
1056 guard
->is_usable_filtered_guard
= 0;
1057 guard
= smartlist_get(gs
->sampled_entry_guards
, 2);
1058 guard
->is_usable_filtered_guard
= 0;
1059 tt_int_op(DFLT_MIN_FILTERED_SAMPLE_SIZE
- 3, OP_EQ
,
1060 num_reachable_filtered_guards(gs
, NULL
));
1062 // This time, expanding the sample will add some more guards.
1063 guard
= entry_guards_expand_sample(gs
);
1064 tt_assert(guard
); // no guard was added.
1065 tt_int_op(DFLT_MIN_FILTERED_SAMPLE_SIZE
, OP_EQ
,
1066 num_reachable_filtered_guards(gs
, NULL
));
1067 tt_int_op(smartlist_len(gs
->sampled_entry_guards
), OP_EQ
,
1068 num_reachable_filtered_guards(gs
, NULL
)+3);
1070 // Still idempotent.
1071 guard
= entry_guards_expand_sample(gs
);
1072 tt_ptr_op(guard
, OP_EQ
, NULL
); // no guard was added.
1073 tt_int_op(DFLT_MIN_FILTERED_SAMPLE_SIZE
, OP_EQ
,
1074 num_reachable_filtered_guards(gs
, NULL
));
1076 // Now, do a nasty trick: tell the filter to exclude 31/32 of the guards.
1077 // This will cause the sample size to get reeeeally huge, while the
1078 // filtered sample size grows only slowly.
1079 routerset_free(get_options_mutable()->ExcludeNodes
);
1080 get_options_mutable()->ExcludeNodes
= routerset_new();
1081 routerset_parse(get_options_mutable()->ExcludeNodes
, "144.144.0.0/16", "");
1082 SMARTLIST_FOREACH(big_fake_net_nodes
, node_t
*, n
, {
1083 if (n_sl_idx
% 64 != 0) {
1084 n
->rs
->addr
= 0x90903030;
1087 entry_guards_update_filtered_sets(gs
);
1089 // Surely (p ~ 1-2**-60), one of our guards has been excluded.
1090 tt_int_op(num_reachable_filtered_guards(gs
, NULL
), OP_LT
,
1091 DFLT_MIN_FILTERED_SAMPLE_SIZE
);
1093 // Try to regenerate the guards.
1094 guard
= entry_guards_expand_sample(gs
);
1095 tt_assert(guard
); // no guard was added.
1097 /* this time, it's possible that we didn't add enough sampled guards. */
1098 tt_int_op(num_reachable_filtered_guards(gs
, NULL
), OP_LE
,
1099 DFLT_MIN_FILTERED_SAMPLE_SIZE
);
1100 /* but we definitely didn't exceed the sample maximum. */
1101 const int n_guards
= 271 / 2;
1102 tt_int_op(smartlist_len(gs
->sampled_entry_guards
), OP_LE
,
1103 (int)(n_guards
* .3));
1106 guard_selection_free(gs
);
1107 digestmap_free(node_by_id
, NULL
);
1111 test_entry_guard_expand_sample_small_net(void *arg
)
1114 guard_selection_t
*gs
= guard_selection_new("default", GS_TYPE_NORMAL
);
1116 /* Fun corner case: not enough guards to make up our whole sample size. */
1117 SMARTLIST_FOREACH(big_fake_net_nodes
, node_t
*, n
, {
1118 if (n_sl_idx
>= 15) {
1120 SMARTLIST_DEL_CURRENT(big_fake_net_nodes
, n
);
1122 n
->rs
->addr
= 0; // make the filter reject this.
1126 entry_guard_t
*guard
= entry_guards_expand_sample(gs
);
1127 tt_assert(guard
); // the last guard returned -- some guard was added.
1128 // half the nodes are guards, so we have 8 guards left. The set
1129 // is small, so we sampled everything.
1130 tt_int_op(smartlist_len(gs
->sampled_entry_guards
), OP_EQ
, 8);
1131 tt_int_op(num_reachable_filtered_guards(gs
, NULL
), OP_EQ
, 0);
1133 guard_selection_free(gs
);
1137 test_entry_guard_update_from_consensus_status(void *arg
)
1139 /* Here we're going to have some nodes become un-guardy, and say we got a
1140 * new consensus. This should cause those nodes to get detected as
1145 time_t start
= approx_time();
1146 guard_selection_t
*gs
= guard_selection_new("default", GS_TYPE_NORMAL
);
1147 networkstatus_t
*ns_tmp
= NULL
;
1149 /* Don't randomly backdate stuff; it will make correctness harder to check.*/
1150 MOCK(randomize_time
, mock_randomize_time_no_randomization
);
1152 /* First, sample some guards. */
1153 entry_guards_expand_sample(gs
);
1154 int n_sampled_pre
= smartlist_len(gs
->sampled_entry_guards
);
1155 int n_filtered_pre
= num_reachable_filtered_guards(gs
, NULL
);
1156 tt_i64_op(n_sampled_pre
, OP_EQ
, n_filtered_pre
);
1157 tt_i64_op(n_sampled_pre
, OP_GT
, 10);
1159 /* At this point, it should be a no-op to do this: */
1160 sampled_guards_update_from_consensus(gs
);
1162 /* Now let's make some of our guards become unlisted. The easiest way to
1163 * do that would be to take away their guard flag. */
1164 for (i
= 0; i
< 5; ++i
) {
1165 entry_guard_t
*g
= smartlist_get(gs
->sampled_entry_guards
, i
);
1166 node_t
*n
= (node_t
*) bfn_mock_node_get_by_id(g
->identity
);
1168 n
->is_possible_guard
= 0;
1171 update_approx_time(start
+ 30);
1173 /* try this with no live networkstatus. Nothing should happen! */
1174 ns_tmp
= dummy_consensus
;
1175 dummy_consensus
= NULL
;
1176 sampled_guards_update_from_consensus(gs
);
1177 tt_i64_op(smartlist_len(gs
->sampled_entry_guards
), OP_EQ
, n_sampled_pre
);
1178 tt_i64_op(num_reachable_filtered_guards(gs
, NULL
), OP_EQ
, n_filtered_pre
);
1179 /* put the networkstatus back. */
1180 dummy_consensus
= ns_tmp
;
1184 /* Now those guards should become unlisted, and drop off the filter, but
1185 * stay in the sample. */
1186 update_approx_time(start
+ 60);
1187 sampled_guards_update_from_consensus(gs
);
1189 tt_i64_op(smartlist_len(gs
->sampled_entry_guards
), OP_EQ
, n_sampled_pre
);
1190 tt_i64_op(num_reachable_filtered_guards(gs
, NULL
), OP_EQ
, n_filtered_pre
-5);
1191 for (i
= 0; i
< 5; ++i
) {
1192 entry_guard_t
*g
= smartlist_get(gs
->sampled_entry_guards
, i
);
1193 tt_assert(! g
->currently_listed
);
1194 tt_i64_op(g
->unlisted_since_date
, OP_EQ
, start
+60);
1196 for (i
= 5; i
< n_sampled_pre
; ++i
) {
1197 entry_guard_t
*g
= smartlist_get(gs
->sampled_entry_guards
, i
);
1198 tt_assert(g
->currently_listed
);
1199 tt_i64_op(g
->unlisted_since_date
, OP_EQ
, 0);
1202 /* Now re-list one, and remove one completely. */
1204 entry_guard_t
*g
= smartlist_get(gs
->sampled_entry_guards
, 0);
1205 node_t
*n
= (node_t
*) bfn_mock_node_get_by_id(g
->identity
);
1207 n
->is_possible_guard
= 1;
1210 /* try removing the node, to make sure we don't crash on an absent node
1212 entry_guard_t
*g
= smartlist_get(gs
->sampled_entry_guards
, 5);
1213 node_t
*n
= (node_t
*) bfn_mock_node_get_by_id(g
->identity
);
1215 smartlist_remove(big_fake_net_nodes
, n
);
1218 update_approx_time(start
+ 300);
1219 sampled_guards_update_from_consensus(gs
);
1221 /* guards 1..5 are now unlisted; 0,6,7.. are listed. */
1222 tt_i64_op(smartlist_len(gs
->sampled_entry_guards
), OP_EQ
, n_sampled_pre
);
1223 for (i
= 1; i
< 6; ++i
) {
1224 entry_guard_t
*g
= smartlist_get(gs
->sampled_entry_guards
, i
);
1225 tt_assert(! g
->currently_listed
);
1227 tt_i64_op(g
->unlisted_since_date
, OP_EQ
, start
+300);
1229 tt_i64_op(g
->unlisted_since_date
, OP_EQ
, start
+60);
1231 for (i
= 0; i
< n_sampled_pre
; i
= (!i
) ? 6 : i
+1) { /* 0,6,7,8, ... */
1232 entry_guard_t
*g
= smartlist_get(gs
->sampled_entry_guards
, i
);
1233 tt_assert(g
->currently_listed
);
1234 tt_i64_op(g
->unlisted_since_date
, OP_EQ
, 0);
1238 tor_free(ns_tmp
); /* in case we couldn't put it back */
1239 guard_selection_free(gs
);
1240 UNMOCK(randomize_time
);
1244 test_entry_guard_update_from_consensus_repair(void *arg
)
1246 /* Here we'll make sure that our code to repair the unlisted-since
1247 * times is correct. */
1251 time_t start
= approx_time();
1252 guard_selection_t
*gs
= guard_selection_new("default", GS_TYPE_NORMAL
);
1254 /* Don't randomly backdate stuff; it will make correctness harder to check.*/
1255 MOCK(randomize_time
, mock_randomize_time_no_randomization
);
1257 /* First, sample some guards. */
1258 entry_guards_expand_sample(gs
);
1259 int n_sampled_pre
= smartlist_len(gs
->sampled_entry_guards
);
1260 int n_filtered_pre
= num_reachable_filtered_guards(gs
, NULL
);
1261 tt_i64_op(n_sampled_pre
, OP_EQ
, n_filtered_pre
);
1262 tt_i64_op(n_sampled_pre
, OP_GT
, 10);
1264 /* Now corrupt the list a bit. Call some unlisted-since-never, and some
1265 * listed-and-unlisted-since-a-time. */
1266 update_approx_time(start
+ 300);
1267 for (i
= 0; i
< 3; ++i
) {
1268 /* these will get a date. */
1269 entry_guard_t
*g
= smartlist_get(gs
->sampled_entry_guards
, i
);
1270 node_t
*n
= (node_t
*) bfn_mock_node_get_by_id(g
->identity
);
1272 n
->is_possible_guard
= 0;
1273 g
->currently_listed
= 0;
1275 for (i
= 3; i
< 6; ++i
) {
1276 /* these will become listed. */
1277 entry_guard_t
*g
= smartlist_get(gs
->sampled_entry_guards
, i
);
1278 g
->unlisted_since_date
= start
+100;
1280 setup_full_capture_of_logs(LOG_WARN
);
1281 sampled_guards_update_from_consensus(gs
);
1282 expect_log_msg_containing(
1283 "was listed, but with unlisted_since_date set");
1284 expect_log_msg_containing(
1285 "was unlisted, but with unlisted_since_date unset");
1286 teardown_capture_of_logs();
1288 tt_int_op(smartlist_len(gs
->sampled_entry_guards
), OP_EQ
, n_sampled_pre
);
1289 tt_int_op(num_reachable_filtered_guards(gs
, NULL
), OP_EQ
, n_filtered_pre
-3);
1290 for (i
= 3; i
< n_sampled_pre
; ++i
) {
1291 /* these will become listed. */
1292 entry_guard_t
*g
= smartlist_get(gs
->sampled_entry_guards
, i
);
1294 tt_assert(! g
->currently_listed
);
1295 tt_i64_op(g
->unlisted_since_date
, OP_EQ
, start
+300);
1297 tt_assert(g
->currently_listed
);
1298 tt_i64_op(g
->unlisted_since_date
, OP_EQ
, 0);
1303 teardown_capture_of_logs();
1304 guard_selection_free(gs
);
1305 UNMOCK(randomize_time
);
1309 test_entry_guard_update_from_consensus_remove(void *arg
)
1311 /* Now let's check the logic responsible for removing guards from the
1312 * sample entirely. */
1316 guard_selection_t
*gs
= guard_selection_new("default", GS_TYPE_NORMAL
);
1317 smartlist_t
*keep_ids
= smartlist_new();
1318 smartlist_t
*remove_ids
= smartlist_new();
1320 /* Don't randomly backdate stuff; it will make correctness harder to check.*/
1321 MOCK(randomize_time
, mock_randomize_time_no_randomization
);
1323 /* First, sample some guards. */
1324 entry_guards_expand_sample(gs
);
1325 int n_sampled_pre
= smartlist_len(gs
->sampled_entry_guards
);
1326 int n_filtered_pre
= num_reachable_filtered_guards(gs
, NULL
);
1327 tt_i64_op(n_sampled_pre
, OP_EQ
, n_filtered_pre
);
1328 tt_i64_op(n_sampled_pre
, OP_GT
, 10);
1330 const time_t one_day_ago
= approx_time() - 1*24*60*60;
1331 const time_t one_year_ago
= approx_time() - 365*24*60*60;
1332 const time_t two_years_ago
= approx_time() - 2*365*24*60*60;
1333 /* 0: unlisted for a day. (keep this) */
1335 entry_guard_t
*g
= smartlist_get(gs
->sampled_entry_guards
, 0);
1336 node_t
*n
= (node_t
*) bfn_mock_node_get_by_id(g
->identity
);
1338 n
->is_possible_guard
= 0;
1339 g
->currently_listed
= 0;
1340 g
->unlisted_since_date
= one_day_ago
;
1341 smartlist_add(keep_ids
, tor_memdup(g
->identity
, 20));
1343 /* 1: unlisted for a year. (remove this) */
1345 entry_guard_t
*g
= smartlist_get(gs
->sampled_entry_guards
, 1);
1346 node_t
*n
= (node_t
*) bfn_mock_node_get_by_id(g
->identity
);
1348 n
->is_possible_guard
= 0;
1349 g
->currently_listed
= 0;
1350 g
->unlisted_since_date
= one_year_ago
;
1351 smartlist_add(remove_ids
, tor_memdup(g
->identity
, 20));
1353 /* 2: added a day ago, never confirmed. (keep this) */
1355 entry_guard_t
*g
= smartlist_get(gs
->sampled_entry_guards
, 2);
1356 g
->sampled_on_date
= one_day_ago
;
1357 smartlist_add(keep_ids
, tor_memdup(g
->identity
, 20));
1359 /* 3: added a year ago, never confirmed. (remove this) */
1361 entry_guard_t
*g
= smartlist_get(gs
->sampled_entry_guards
, 3);
1362 g
->sampled_on_date
= one_year_ago
;
1363 smartlist_add(remove_ids
, tor_memdup(g
->identity
, 20));
1365 /* 4: added two year ago, confirmed yesterday, primary. (keep this.) */
1367 entry_guard_t
*g
= smartlist_get(gs
->sampled_entry_guards
, 4);
1368 g
->sampled_on_date
= one_year_ago
;
1369 g
->confirmed_on_date
= one_day_ago
;
1370 g
->confirmed_idx
= 0;
1372 smartlist_add(gs
->confirmed_entry_guards
, g
);
1373 smartlist_add(gs
->primary_entry_guards
, g
);
1374 smartlist_add(keep_ids
, tor_memdup(g
->identity
, 20));
1376 /* 5: added two years ago, confirmed a year ago, primary. (remove this) */
1378 entry_guard_t
*g
= smartlist_get(gs
->sampled_entry_guards
, 5);
1379 g
->sampled_on_date
= two_years_ago
;
1380 g
->confirmed_on_date
= one_year_ago
;
1381 g
->confirmed_idx
= 1;
1383 smartlist_add(gs
->confirmed_entry_guards
, g
);
1384 smartlist_add(gs
->primary_entry_guards
, g
);
1385 smartlist_add(remove_ids
, tor_memdup(g
->identity
, 20));
1388 sampled_guards_update_from_consensus(gs
);
1390 /* Did we remove the right ones? */
1391 SMARTLIST_FOREACH(keep_ids
, uint8_t *, id
, {
1392 tt_assert(get_sampled_guard_with_id(gs
, id
) != NULL
);
1394 SMARTLIST_FOREACH(remove_ids
, uint8_t *, id
, {
1395 tt_want(get_sampled_guard_with_id(gs
, id
) == NULL
);
1398 /* Did we remove the right number? */
1399 tt_int_op(smartlist_len(gs
->sampled_entry_guards
), OP_EQ
, n_sampled_pre
- 3);
1402 guard_selection_free(gs
);
1403 UNMOCK(randomize_time
);
1404 SMARTLIST_FOREACH(keep_ids
, char *, cp
, tor_free(cp
));
1405 SMARTLIST_FOREACH(remove_ids
, char *, cp
, tor_free(cp
));
1406 smartlist_free(keep_ids
);
1407 smartlist_free(remove_ids
);
1411 test_entry_guard_confirming_guards(void *arg
)
1414 /* Now let's check the logic responsible for manipulating the list
1415 * of confirmed guards */
1416 guard_selection_t
*gs
= guard_selection_new("default", GS_TYPE_NORMAL
);
1417 MOCK(randomize_time
, mock_randomize_time_no_randomization
);
1419 /* Create the sample. */
1420 entry_guards_expand_sample(gs
);
1422 /* Confirm a few guards. */
1423 time_t start
= approx_time();
1424 entry_guard_t
*g1
= smartlist_get(gs
->sampled_entry_guards
, 0);
1425 entry_guard_t
*g2
= smartlist_get(gs
->sampled_entry_guards
, 1);
1426 entry_guard_t
*g3
= smartlist_get(gs
->sampled_entry_guards
, 8);
1427 make_guard_confirmed(gs
, g2
);
1428 update_approx_time(start
+ 10);
1429 make_guard_confirmed(gs
, g1
);
1430 make_guard_confirmed(gs
, g3
);
1432 /* Were the correct dates and indices fed in? */
1433 tt_int_op(g1
->confirmed_idx
, OP_EQ
, 1);
1434 tt_int_op(g2
->confirmed_idx
, OP_EQ
, 0);
1435 tt_int_op(g3
->confirmed_idx
, OP_EQ
, 2);
1436 tt_i64_op(g1
->confirmed_on_date
, OP_EQ
, start
+10);
1437 tt_i64_op(g2
->confirmed_on_date
, OP_EQ
, start
);
1438 tt_i64_op(g3
->confirmed_on_date
, OP_EQ
, start
+10);
1439 tt_ptr_op(smartlist_get(gs
->confirmed_entry_guards
, 0), OP_EQ
, g2
);
1440 tt_ptr_op(smartlist_get(gs
->confirmed_entry_guards
, 1), OP_EQ
, g1
);
1441 tt_ptr_op(smartlist_get(gs
->confirmed_entry_guards
, 2), OP_EQ
, g3
);
1443 /* Now make sure we can regenerate the confirmed_entry_guards list. */
1444 smartlist_clear(gs
->confirmed_entry_guards
);
1445 g2
->confirmed_idx
= 0;
1446 g1
->confirmed_idx
= 10;
1447 g3
->confirmed_idx
= 100;
1448 entry_guards_update_confirmed(gs
);
1449 tt_int_op(g1
->confirmed_idx
, OP_EQ
, 1);
1450 tt_int_op(g2
->confirmed_idx
, OP_EQ
, 0);
1451 tt_int_op(g3
->confirmed_idx
, OP_EQ
, 2);
1452 tt_ptr_op(smartlist_get(gs
->confirmed_entry_guards
, 0), OP_EQ
, g2
);
1453 tt_ptr_op(smartlist_get(gs
->confirmed_entry_guards
, 1), OP_EQ
, g1
);
1454 tt_ptr_op(smartlist_get(gs
->confirmed_entry_guards
, 2), OP_EQ
, g3
);
1456 /* Now make sure we can regenerate the confirmed_entry_guards list if
1457 * the indices are messed up. */
1458 g1
->confirmed_idx
= g2
->confirmed_idx
= g3
->confirmed_idx
= 999;
1459 smartlist_clear(gs
->confirmed_entry_guards
);
1460 entry_guards_update_confirmed(gs
);
1461 tt_int_op(g1
->confirmed_idx
, OP_GE
, 0);
1462 tt_int_op(g2
->confirmed_idx
, OP_GE
, 0);
1463 tt_int_op(g3
->confirmed_idx
, OP_GE
, 0);
1464 tt_int_op(g1
->confirmed_idx
, OP_LE
, 2);
1465 tt_int_op(g2
->confirmed_idx
, OP_LE
, 2);
1466 tt_int_op(g3
->confirmed_idx
, OP_LE
, 2);
1467 g1
= smartlist_get(gs
->confirmed_entry_guards
, 0);
1468 g2
= smartlist_get(gs
->confirmed_entry_guards
, 1);
1469 g3
= smartlist_get(gs
->confirmed_entry_guards
, 2);
1470 tt_int_op(g1
->confirmed_idx
, OP_EQ
, 0);
1471 tt_int_op(g2
->confirmed_idx
, OP_EQ
, 1);
1472 tt_int_op(g3
->confirmed_idx
, OP_EQ
, 2);
1473 tt_assert(g1
!= g2
);
1474 tt_assert(g1
!= g3
);
1475 tt_assert(g2
!= g3
);
1478 UNMOCK(randomize_time
);
1479 guard_selection_free(gs
);
1483 test_entry_guard_sample_reachable_filtered(void *arg
)
1486 guard_selection_t
*gs
= guard_selection_new("default", GS_TYPE_NORMAL
);
1487 entry_guards_expand_sample(gs
);
1488 const int N
= 10000;
1489 bitarray_t
*selected
= NULL
;
1492 /* We've got a sampled list now; let's make one non-usable-filtered; some
1493 * confirmed, some primary, some pending.
1495 int n_guards
= smartlist_len(gs
->sampled_entry_guards
);
1496 tt_int_op(n_guards
, OP_GT
, 10);
1498 g
= smartlist_get(gs
->sampled_entry_guards
, 0);
1500 g
= smartlist_get(gs
->sampled_entry_guards
, 1);
1501 make_guard_confirmed(gs
, g
);
1502 g
= smartlist_get(gs
->sampled_entry_guards
, 2);
1504 g
= smartlist_get(gs
->sampled_entry_guards
, 3);
1505 g
->pb
.path_bias_disabled
= 1;
1507 entry_guards_update_filtered_sets(gs
);
1508 gs
->primary_guards_up_to_date
= 1;
1509 tt_int_op(num_reachable_filtered_guards(gs
, NULL
), OP_EQ
, n_guards
- 1);
1510 tt_int_op(smartlist_len(gs
->sampled_entry_guards
), OP_EQ
, n_guards
);
1512 // +1 since the one we made disabled will make another one get added.
1515 /* Try a bunch of selections. */
1520 { SAMPLE_EXCLUDE_CONFIRMED
, 1 },
1521 { SAMPLE_EXCLUDE_PRIMARY
|SAMPLE_NO_UPDATE_PRIMARY
, 2 },
1522 { SAMPLE_EXCLUDE_PENDING
, 0 },
1526 for (j
= 0; tests
[j
].flag
>= 0; ++j
) {
1527 selected
= bitarray_init_zero(n_guards
);
1528 const int excluded_flags
= tests
[j
].flag
;
1529 const int excluded_idx
= tests
[j
].idx
;
1530 for (i
= 0; i
< N
; ++i
) {
1531 g
= sample_reachable_filtered_entry_guards(gs
, NULL
, excluded_flags
);
1533 int pos
= smartlist_pos(gs
->sampled_entry_guards
, g
);
1534 tt_int_op(smartlist_len(gs
->sampled_entry_guards
), OP_EQ
, n_guards
);
1535 tt_int_op(pos
, OP_GE
, 0);
1536 tt_int_op(pos
, OP_LT
, n_guards
);
1537 bitarray_set(selected
, pos
);
1539 for (i
= 0; i
< n_guards
; ++i
) {
1540 const int should_be_set
= (i
!= excluded_idx
&&
1541 i
!= 3); // filtered out.
1542 tt_int_op(!!bitarray_is_set(selected
, i
), OP_EQ
, should_be_set
);
1544 bitarray_free(selected
);
1549 guard_selection_free(gs
);
1550 bitarray_free(selected
);
1554 test_entry_guard_sample_reachable_filtered_empty(void *arg
)
1557 guard_selection_t
*gs
= guard_selection_new("default", GS_TYPE_NORMAL
);
1558 /* What if we try to sample from a set of 0? */
1559 SMARTLIST_FOREACH(big_fake_net_nodes
, node_t
*, n
,
1560 n
->is_possible_guard
= 0);
1562 entry_guard_t
*g
= sample_reachable_filtered_entry_guards(gs
, NULL
, 0);
1563 tt_ptr_op(g
, OP_EQ
, NULL
);
1566 guard_selection_free(gs
);
1570 test_entry_guard_retry_unreachable(void *arg
)
1573 guard_selection_t
*gs
= guard_selection_new("default", GS_TYPE_NORMAL
);
1575 entry_guards_expand_sample(gs
);
1576 /* Let's say that we have two guards, and they're down.
1578 time_t start
= approx_time();
1579 entry_guard_t
*g1
= smartlist_get(gs
->sampled_entry_guards
, 0);
1580 entry_guard_t
*g2
= smartlist_get(gs
->sampled_entry_guards
, 1);
1581 entry_guard_t
*g3
= smartlist_get(gs
->sampled_entry_guards
, 2);
1582 g1
->is_reachable
= GUARD_REACHABLE_NO
;
1583 g2
->is_reachable
= GUARD_REACHABLE_NO
;
1585 g1
->failing_since
= g2
->failing_since
= start
;
1586 g1
->last_tried_to_connect
= g2
->last_tried_to_connect
= start
;
1588 /* Wait 5 minutes. Nothing will get retried. */
1589 update_approx_time(start
+ 5 * 60);
1590 entry_guard_consider_retry(g1
);
1591 entry_guard_consider_retry(g2
);
1592 entry_guard_consider_retry(g3
); // just to make sure this doesn't crash.
1593 tt_int_op(g1
->is_reachable
, OP_EQ
, GUARD_REACHABLE_NO
);
1594 tt_int_op(g2
->is_reachable
, OP_EQ
, GUARD_REACHABLE_NO
);
1595 tt_int_op(g3
->is_reachable
, OP_EQ
, GUARD_REACHABLE_MAYBE
);
1597 /* After 30 min, the primary one gets retried */
1598 update_approx_time(start
+ 35 * 60);
1599 entry_guard_consider_retry(g1
);
1600 entry_guard_consider_retry(g2
);
1601 tt_int_op(g1
->is_reachable
, OP_EQ
, GUARD_REACHABLE_MAYBE
);
1602 tt_int_op(g2
->is_reachable
, OP_EQ
, GUARD_REACHABLE_NO
);
1604 g1
->is_reachable
= GUARD_REACHABLE_NO
;
1605 g1
->last_tried_to_connect
= start
+ 55*60;
1607 /* After 1 hour, we'll retry the nonprimary one. */
1608 update_approx_time(start
+ 61 * 60);
1609 entry_guard_consider_retry(g1
);
1610 entry_guard_consider_retry(g2
);
1611 tt_int_op(g1
->is_reachable
, OP_EQ
, GUARD_REACHABLE_NO
);
1612 tt_int_op(g2
->is_reachable
, OP_EQ
, GUARD_REACHABLE_MAYBE
);
1614 g2
->is_reachable
= GUARD_REACHABLE_NO
;
1615 g2
->last_tried_to_connect
= start
+ 61*60;
1617 /* And then the primary one again. */
1618 update_approx_time(start
+ 66 * 60);
1619 entry_guard_consider_retry(g1
);
1620 entry_guard_consider_retry(g2
);
1621 tt_int_op(g1
->is_reachable
, OP_EQ
, GUARD_REACHABLE_MAYBE
);
1622 tt_int_op(g2
->is_reachable
, OP_EQ
, GUARD_REACHABLE_NO
);
1625 guard_selection_free(gs
);
1629 test_entry_guard_manage_primary(void *arg
)
1632 guard_selection_t
*gs
= guard_selection_new("default", GS_TYPE_NORMAL
);
1633 smartlist_t
*prev_guards
= smartlist_new();
1635 /* If no guards are confirmed, we should pick a few reachable guards and
1636 * call them all primary. But not confirmed.*/
1637 entry_guards_update_primary(gs
);
1638 int n_primary
= smartlist_len(gs
->primary_entry_guards
);
1639 tt_int_op(n_primary
, OP_GE
, 1);
1640 SMARTLIST_FOREACH(gs
->primary_entry_guards
, entry_guard_t
*, g
, {
1641 tt_assert(g
->is_primary
);
1642 tt_assert(g
->confirmed_idx
== -1);
1645 /* Calling it a second time should leave the guards unchanged. */
1646 smartlist_add_all(prev_guards
, gs
->primary_entry_guards
);
1647 entry_guards_update_primary(gs
);
1648 tt_int_op(smartlist_len(gs
->primary_entry_guards
), OP_EQ
, n_primary
);
1649 SMARTLIST_FOREACH(gs
->primary_entry_guards
, entry_guard_t
*, g
, {
1650 tt_ptr_op(g
, OP_EQ
, smartlist_get(prev_guards
, g_sl_idx
));
1653 /* If we have one confirmed guard, that guards becomes the first primary
1654 * guard, and the other primary guards get kept. */
1656 /* find a non-primary guard... */
1657 entry_guard_t
*confirmed
= NULL
;
1658 SMARTLIST_FOREACH(gs
->sampled_entry_guards
, entry_guard_t
*, g
, {
1659 if (! g
->is_primary
) {
1664 tt_assert(confirmed
);
1665 /* make it confirmed. */
1666 make_guard_confirmed(gs
, confirmed
);
1667 /* update the list... */
1668 smartlist_clear(prev_guards
);
1669 smartlist_add_all(prev_guards
, gs
->primary_entry_guards
);
1670 entry_guards_update_primary(gs
);
1672 /* and see what's primary now! */
1673 tt_int_op(smartlist_len(gs
->primary_entry_guards
), OP_EQ
, n_primary
);
1674 tt_ptr_op(smartlist_get(gs
->primary_entry_guards
, 0), OP_EQ
, confirmed
);
1675 SMARTLIST_FOREACH(gs
->primary_entry_guards
, entry_guard_t
*, g
, {
1676 tt_assert(g
->is_primary
);
1679 tt_ptr_op(g
, OP_EQ
, smartlist_get(prev_guards
, g_sl_idx
- 1));
1682 entry_guard_t
*prev_last_guard
= smartlist_get(prev_guards
, n_primary
-1);
1683 tt_assert(! prev_last_guard
->is_primary
);
1686 /* Calling it a fourth time should leave the guards unchanged. */
1687 smartlist_clear(prev_guards
);
1688 smartlist_add_all(prev_guards
, gs
->primary_entry_guards
);
1689 entry_guards_update_primary(gs
);
1690 tt_int_op(smartlist_len(gs
->primary_entry_guards
), OP_EQ
, n_primary
);
1691 SMARTLIST_FOREACH(gs
->primary_entry_guards
, entry_guard_t
*, g
, {
1692 tt_ptr_op(g
, OP_EQ
, smartlist_get(prev_guards
, g_sl_idx
));
1695 /* Do some dirinfo checks */
1697 /* Check that we have all required dirinfo for the primaries (that's done
1698 * in big_fake_network_setup()) */
1699 char *dir_info_str
=
1700 guard_selection_get_err_str_if_dir_info_missing(gs
, 0, 0, 0);
1701 tt_assert(!dir_info_str
);
1703 /* Now artificially remove the first primary's descriptor and re-check */
1704 entry_guard_t
*first_primary
;
1705 first_primary
= smartlist_get(gs
->primary_entry_guards
, 0);
1706 /* Change the first primary's identity digest so that the mocked functions
1707 * can't find its descriptor */
1708 memset(first_primary
->identity
, 9, sizeof(first_primary
->identity
));
1709 dir_info_str
=guard_selection_get_err_str_if_dir_info_missing(gs
, 1, 2, 3);
1710 tt_str_op(dir_info_str
, OP_EQ
,
1711 "We're missing descriptors for 1/2 of our primary entry guards "
1712 "(total microdescriptors: 2/3).");
1713 tor_free(dir_info_str
);
1717 guard_selection_free(gs
);
1718 smartlist_free(prev_guards
);
1722 test_entry_guard_guard_preferred(void *arg
)
1725 entry_guard_t
*g1
= tor_malloc_zero(sizeof(entry_guard_t
));
1726 entry_guard_t
*g2
= tor_malloc_zero(sizeof(entry_guard_t
));
1728 g1
->confirmed_idx
= g2
->confirmed_idx
= -1;
1729 g1
->last_tried_to_connect
= approx_time();
1730 g2
->last_tried_to_connect
= approx_time();
1732 tt_int_op(0, OP_EQ
, entry_guard_has_higher_priority(g1
, g1
));
1734 /* Neither is pending; priorities equal. */
1735 tt_int_op(0, OP_EQ
, entry_guard_has_higher_priority(g2
, g1
));
1736 tt_int_op(0, OP_EQ
, entry_guard_has_higher_priority(g1
, g2
));
1738 /* If one is pending, the pending one has higher priority */
1740 tt_int_op(1, OP_EQ
, entry_guard_has_higher_priority(g1
, g2
));
1741 tt_int_op(0, OP_EQ
, entry_guard_has_higher_priority(g2
, g1
));
1743 /* If both are pending, and last_tried_to_connect is equal:
1746 tt_int_op(0, OP_EQ
, entry_guard_has_higher_priority(g2
, g1
));
1747 tt_int_op(0, OP_EQ
, entry_guard_has_higher_priority(g1
, g2
));
1749 /* One had a connection that startied earlier: it has higher priority. */
1750 g2
->last_tried_to_connect
-= 10;
1751 tt_int_op(1, OP_EQ
, entry_guard_has_higher_priority(g2
, g1
));
1752 tt_int_op(0, OP_EQ
, entry_guard_has_higher_priority(g1
, g2
));
1754 /* Now, say that g1 is confirmed. It will get higher priority. */
1755 g1
->confirmed_idx
= 5;
1756 tt_int_op(0, OP_EQ
, entry_guard_has_higher_priority(g2
, g1
));
1757 tt_int_op(1, OP_EQ
, entry_guard_has_higher_priority(g1
, g2
));
1759 /* But if g2 was confirmed first, it will get priority */
1760 g2
->confirmed_idx
= 2;
1761 tt_int_op(1, OP_EQ
, entry_guard_has_higher_priority(g2
, g1
));
1762 tt_int_op(0, OP_EQ
, entry_guard_has_higher_priority(g1
, g2
));
1770 test_entry_guard_select_for_circuit_no_confirmed(void *arg
)
1772 /* Simpler cases: no gaurds are confirmed yet. */
1774 guard_selection_t
*gs
= guard_selection_new("default", GS_TYPE_NORMAL
);
1775 entry_guard_restriction_t
*rst
= NULL
;
1777 /* simple starting configuration */
1778 entry_guards_update_primary(gs
);
1779 unsigned state
= 9999;
1781 entry_guard_t
*g
= select_entry_guard_for_circuit(gs
, GUARD_USAGE_TRAFFIC
,
1785 tt_assert(g
->is_primary
);
1786 tt_int_op(g
->confirmed_idx
, OP_EQ
, -1);
1787 tt_uint_op(g
->is_pending
, OP_EQ
, 0); // primary implies non-pending.
1788 tt_uint_op(state
, OP_EQ
, GUARD_CIRC_STATE_USABLE_ON_COMPLETION
);
1789 tt_i64_op(g
->last_tried_to_connect
, OP_EQ
, approx_time());
1791 // If we do that again, we should get the same guard.
1792 entry_guard_t
*g2
= select_entry_guard_for_circuit(gs
, GUARD_USAGE_TRAFFIC
,
1794 tt_ptr_op(g2
, OP_EQ
, g
);
1796 // if we mark that guard down, we should get a different primary guard.
1798 g
->is_reachable
= GUARD_REACHABLE_NO
;
1799 g
->failing_since
= approx_time() - 10;
1800 g
->last_tried_to_connect
= approx_time() - 10;
1802 g2
= select_entry_guard_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
, &state
);
1803 tt_ptr_op(g2
, OP_NE
, g
);
1805 tt_assert(g2
->is_primary
);
1806 tt_int_op(g2
->confirmed_idx
, OP_EQ
, -1);
1807 tt_uint_op(g2
->is_pending
, OP_EQ
, 0); // primary implies non-pending.
1808 tt_uint_op(state
, OP_EQ
, GUARD_CIRC_STATE_USABLE_ON_COMPLETION
);
1809 tt_i64_op(g2
->last_tried_to_connect
, OP_EQ
, approx_time());
1811 // If we say that the first primary guard was last tried a long time ago, we
1812 // should get an automatic retry on it.
1813 g
->failing_since
= approx_time() - 72*60*60;
1814 g
->last_tried_to_connect
= approx_time() - 72*60*60;
1816 g2
= select_entry_guard_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
, &state
);
1817 tt_ptr_op(g2
, OP_EQ
, g
);
1819 tt_uint_op(state
, OP_EQ
, GUARD_CIRC_STATE_USABLE_ON_COMPLETION
);
1820 tt_i64_op(g2
->last_tried_to_connect
, OP_EQ
, approx_time());
1821 tt_int_op(g2
->is_reachable
, OP_EQ
, GUARD_REACHABLE_MAYBE
);
1823 // And if we mark ALL the primary guards down, we should get another guard
1825 SMARTLIST_FOREACH(gs
->primary_entry_guards
, entry_guard_t
*, guard
, {
1826 guard
->is_reachable
= GUARD_REACHABLE_NO
;
1827 guard
->last_tried_to_connect
= approx_time() - 5;
1828 guard
->failing_since
= approx_time() - 30;
1831 g2
= select_entry_guard_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
, &state
);
1833 tt_assert(!g2
->is_primary
);
1834 tt_int_op(g2
->confirmed_idx
, OP_EQ
, -1);
1835 tt_uint_op(g2
->is_pending
, OP_EQ
, 1);
1836 tt_uint_op(state
, OP_EQ
, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD
);
1837 tt_i64_op(g2
->last_tried_to_connect
, OP_EQ
, approx_time());
1838 tt_int_op(g2
->is_reachable
, OP_EQ
, GUARD_REACHABLE_MAYBE
);
1840 // As a bonus, maybe we should be retrying the primary guards. Let's say so.
1841 mark_primary_guards_maybe_reachable(gs
);
1842 SMARTLIST_FOREACH(gs
->primary_entry_guards
, entry_guard_t
*, guard
, {
1843 tt_int_op(guard
->is_reachable
, OP_EQ
, GUARD_REACHABLE_MAYBE
);
1844 tt_assert(guard
->is_usable_filtered_guard
== 1);
1845 // no change to these fields.
1846 tt_i64_op(guard
->last_tried_to_connect
, OP_EQ
, approx_time() - 5);
1847 tt_i64_op(guard
->failing_since
, OP_EQ
, approx_time() - 30);
1850 /* Let's try again and we should get the first primary guard again */
1851 g
= select_entry_guard_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
, &state
);
1852 tt_ptr_op(g
, OP_EQ
, smartlist_get(gs
->primary_entry_guards
, 0));
1853 g2
= select_entry_guard_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
, &state
);
1854 tt_ptr_op(g2
, OP_EQ
, g
);
1856 /* But if we impose a restriction, we don't get the same guard */
1857 rst
= guard_create_exit_restriction((uint8_t*)g
->identity
);
1858 g2
= select_entry_guard_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, rst
, &state
);
1859 tt_ptr_op(g2
, OP_NE
, g
);
1862 guard_selection_free(gs
);
1863 entry_guard_restriction_free(rst
);
1867 test_entry_guard_select_for_circuit_confirmed(void *arg
)
1869 /* Case 2: if all the primary guards are down, and there are more confirmed
1870 guards, we use a confirmed guard. */
1873 entry_guard_restriction_t
*rst
= NULL
;
1874 guard_selection_t
*gs
= guard_selection_new("default", GS_TYPE_NORMAL
);
1875 const int N_CONFIRMED
= 10;
1877 /* slightly more complicated simple starting configuration */
1878 entry_guards_update_primary(gs
);
1879 for (i
= 0; i
< N_CONFIRMED
; ++i
) {
1880 entry_guard_t
*guard
= smartlist_get(gs
->sampled_entry_guards
, i
);
1881 make_guard_confirmed(gs
, guard
);
1883 entry_guards_update_primary(gs
); // rebuild the primary list.
1885 unsigned state
= 9999;
1887 // As above, this gives us a primary guard.
1888 entry_guard_t
*g
= select_entry_guard_for_circuit(gs
, GUARD_USAGE_TRAFFIC
,
1891 tt_assert(g
->is_primary
);
1892 tt_int_op(g
->confirmed_idx
, OP_EQ
, 0);
1893 tt_uint_op(g
->is_pending
, OP_EQ
, 0); // primary implies non-pending.
1894 tt_uint_op(state
, OP_EQ
, GUARD_CIRC_STATE_USABLE_ON_COMPLETION
);
1895 tt_i64_op(g
->last_tried_to_connect
, OP_EQ
, approx_time());
1896 tt_ptr_op(g
, OP_EQ
, smartlist_get(gs
->primary_entry_guards
, 0));
1898 // But if we mark all the primary guards down...
1899 SMARTLIST_FOREACH(gs
->primary_entry_guards
, entry_guard_t
*, guard
, {
1900 guard
->last_tried_to_connect
= approx_time();
1901 entry_guards_note_guard_failure(gs
, guard
);
1904 // ... we should get a confirmed guard.
1906 g
= select_entry_guard_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
, &state
);
1908 tt_assert(! g
->is_primary
);
1909 tt_int_op(g
->confirmed_idx
, OP_EQ
, smartlist_len(gs
->primary_entry_guards
));
1910 tt_assert(g
->is_pending
);
1911 tt_uint_op(state
, OP_EQ
, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD
);
1912 tt_i64_op(g
->last_tried_to_connect
, OP_EQ
, approx_time());
1914 // And if we try again, we should get a different confirmed guard, since
1915 // that one is pending.
1917 entry_guard_t
*g2
= select_entry_guard_for_circuit(gs
, GUARD_USAGE_TRAFFIC
,
1920 tt_assert(! g2
->is_primary
);
1921 tt_ptr_op(g2
, OP_NE
, g
);
1922 tt_int_op(g2
->confirmed_idx
, OP_EQ
,
1923 smartlist_len(gs
->primary_entry_guards
)+1);
1924 tt_assert(g2
->is_pending
);
1925 tt_uint_op(state
, OP_EQ
, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD
);
1926 tt_i64_op(g2
->last_tried_to_connect
, OP_EQ
, approx_time());
1928 // If we say that the next confirmed guard in order is excluded, and
1929 // we disable EnforceDistinctSubnets, we get the guard AFTER the
1931 get_options_mutable()->EnforceDistinctSubnets
= 0;
1932 g
= smartlist_get(gs
->confirmed_entry_guards
,
1933 smartlist_len(gs
->primary_entry_guards
)+2);
1934 rst
= guard_create_exit_restriction((uint8_t*)g
->identity
);
1935 g2
= select_entry_guard_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, rst
, &state
);
1936 tt_ptr_op(g2
, OP_NE
, NULL
);
1937 tt_ptr_op(g2
, OP_NE
, g
);
1938 tt_int_op(g2
->confirmed_idx
, OP_EQ
,
1939 smartlist_len(gs
->primary_entry_guards
)+3);
1941 // If we make every confirmed guard become pending then we start poking
1943 const int n_remaining_confirmed
=
1944 N_CONFIRMED
- 3 - smartlist_len(gs
->primary_entry_guards
);
1945 for (i
= 0; i
< n_remaining_confirmed
; ++i
) {
1946 g
= select_entry_guard_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
, &state
);
1947 tt_int_op(g
->confirmed_idx
, OP_GE
, 0);
1951 g
= select_entry_guard_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
, &state
);
1953 tt_assert(g
->is_pending
);
1954 tt_int_op(g
->confirmed_idx
, OP_EQ
, -1);
1956 // If we EnforceDistinctSubnets and apply a restriction, we get
1957 // nothing, since we put all of the nodes in the same /16.
1958 // Regression test for bug 22753/TROVE-2017-006.
1959 get_options_mutable()->EnforceDistinctSubnets
= 1;
1960 g
= smartlist_get(gs
->confirmed_entry_guards
, 0);
1961 memcpy(rst
->exclude_id
, g
->identity
, DIGEST_LEN
);
1962 g2
= select_entry_guard_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, rst
, &state
);
1963 tt_ptr_op(g2
, OP_EQ
, NULL
);
1966 guard_selection_free(gs
);
1967 entry_guard_restriction_free(rst
);
1971 test_entry_guard_select_for_circuit_highlevel_primary(void *arg
)
1973 /* Play around with selecting primary guards for circuits and markign
1974 * them up and down */
1976 guard_selection_t
*gs
= guard_selection_new("default", GS_TYPE_NORMAL
);
1978 time_t start
= approx_time();
1980 const node_t
*node
= NULL
;
1981 circuit_guard_state_t
*guard
= NULL
;
1985 * Make sure that the pick-for-circuit API basically works. We'll get
1986 * a primary guard, so it'll be usable on completion.
1988 int r
= entry_guard_pick_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
,
1991 tt_int_op(r
, OP_EQ
, 0);
1994 tt_int_op(guard
->state
, OP_EQ
, GUARD_CIRC_STATE_USABLE_ON_COMPLETION
);
1995 g
= entry_guard_handle_get(guard
->guard
);
1997 tt_mem_op(g
->identity
, OP_EQ
, node
->identity
, DIGEST_LEN
);
1998 tt_int_op(g
->is_primary
, OP_EQ
, 1);
1999 tt_i64_op(g
->last_tried_to_connect
, OP_EQ
, start
);
2000 tt_int_op(g
->confirmed_idx
, OP_EQ
, -1);
2002 /* Call that circuit successful. */
2003 update_approx_time(start
+15);
2004 u
= entry_guard_succeeded(&guard
);
2005 tt_int_op(u
, OP_EQ
, GUARD_USABLE_NOW
); /* We can use it now. */
2007 tt_int_op(guard
->state
, OP_EQ
, GUARD_CIRC_STATE_COMPLETE
);
2008 g
= entry_guard_handle_get(guard
->guard
);
2010 tt_int_op(g
->is_reachable
, OP_EQ
, GUARD_REACHABLE_YES
);
2011 tt_int_op(g
->confirmed_idx
, OP_EQ
, 0);
2013 circuit_guard_state_free(guard
);
2018 /* Try again. We'll also get a primary guard this time. (The same one,
2019 in fact.) But this time, we'll say the connection has failed. */
2020 update_approx_time(start
+35);
2021 r
= entry_guard_pick_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
,
2023 tt_int_op(r
, OP_EQ
, 0);
2026 tt_int_op(guard
->state
, OP_EQ
, GUARD_CIRC_STATE_USABLE_ON_COMPLETION
);
2027 tt_i64_op(guard
->state_set_at
, OP_EQ
, start
+35);
2028 g
= entry_guard_handle_get(guard
->guard
);
2030 tt_mem_op(g
->identity
, OP_EQ
, node
->identity
, DIGEST_LEN
);
2031 tt_int_op(g
->is_primary
, OP_EQ
, 1);
2032 tt_i64_op(g
->last_tried_to_connect
, OP_EQ
, start
+35);
2033 tt_int_op(g
->confirmed_idx
, OP_EQ
, 0); // same one.
2035 /* It's failed! What will happen to our poor guard? */
2036 update_approx_time(start
+45);
2037 entry_guard_failed(&guard
);
2039 tt_int_op(guard
->state
, OP_EQ
, GUARD_CIRC_STATE_DEAD
);
2040 tt_i64_op(guard
->state_set_at
, OP_EQ
, start
+45);
2041 g
= entry_guard_handle_get(guard
->guard
);
2043 tt_int_op(g
->is_reachable
, OP_EQ
, GUARD_REACHABLE_NO
);
2044 tt_i64_op(g
->failing_since
, OP_EQ
, start
+45);
2045 tt_int_op(g
->confirmed_idx
, OP_EQ
, 0); // still confirmed.
2047 circuit_guard_state_free(guard
);
2050 entry_guard_t
*g_prev
= g
;
2053 /* Now try a third time. Since the other one is down, we'll get a different
2054 * (still primary) guard.
2056 update_approx_time(start
+60);
2057 r
= entry_guard_pick_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
,
2059 tt_int_op(r
, OP_EQ
, 0);
2062 tt_int_op(guard
->state
, OP_EQ
, GUARD_CIRC_STATE_USABLE_ON_COMPLETION
);
2063 g
= entry_guard_handle_get(guard
->guard
);
2065 tt_ptr_op(g
, OP_NE
, g_prev
);
2066 tt_mem_op(g
->identity
, OP_EQ
, node
->identity
, DIGEST_LEN
);
2067 tt_mem_op(g
->identity
, OP_NE
, g_prev
->identity
, DIGEST_LEN
);
2068 tt_int_op(g
->is_primary
, OP_EQ
, 1);
2069 tt_i64_op(g
->last_tried_to_connect
, OP_EQ
, start
+60);
2070 tt_int_op(g
->confirmed_idx
, OP_EQ
, -1); // not confirmed now.
2072 /* Call this one up; watch it get confirmed. */
2073 update_approx_time(start
+90);
2074 u
= entry_guard_succeeded(&guard
);
2075 tt_int_op(u
, OP_EQ
, GUARD_USABLE_NOW
);
2077 tt_int_op(guard
->state
, OP_EQ
, GUARD_CIRC_STATE_COMPLETE
);
2078 g
= entry_guard_handle_get(guard
->guard
);
2080 tt_int_op(g
->is_reachable
, OP_EQ
, GUARD_REACHABLE_YES
);
2081 tt_int_op(g
->confirmed_idx
, OP_EQ
, 1);
2084 guard_selection_free(gs
);
2085 circuit_guard_state_free(guard
);
2089 test_entry_guard_select_for_circuit_highlevel_confirm_other(void *arg
)
2092 const int N_PRIMARY
= DFLT_N_PRIMARY_GUARDS
;
2094 /* At the start, we have no confirmed guards. We'll mark the primary guards
2095 * down, then confirm something else. As soon as we do, it should become
2096 * primary, and we should get it next time. */
2098 time_t start
= approx_time();
2099 guard_selection_t
*gs
= guard_selection_new("default", GS_TYPE_NORMAL
);
2100 circuit_guard_state_t
*guard
= NULL
;
2102 const node_t
*node
= NULL
;
2105 /* Declare that we're on the internet. */
2106 entry_guards_note_internet_connectivity(gs
);
2108 /* Primary guards are down! */
2109 for (i
= 0; i
< N_PRIMARY
; ++i
) {
2110 r
= entry_guard_pick_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
,
2114 tt_int_op(r
, OP_EQ
, 0);
2115 tt_int_op(guard
->state
, OP_EQ
, GUARD_CIRC_STATE_USABLE_ON_COMPLETION
);
2116 entry_guard_failed(&guard
);
2117 circuit_guard_state_free(guard
);
2122 /* Next guard should be non-primary. */
2124 r
= entry_guard_pick_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
,
2128 tt_int_op(r
, OP_EQ
, 0);
2129 entry_guard_t
*g
= entry_guard_handle_get(guard
->guard
);
2131 tt_int_op(guard
->state
, OP_EQ
, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD
);
2132 tt_int_op(g
->confirmed_idx
, OP_EQ
, -1);
2133 tt_int_op(g
->is_primary
, OP_EQ
, 0);
2134 tt_int_op(g
->is_pending
, OP_EQ
, 1);
2137 u
= entry_guard_succeeded(&guard
);
2138 /* We're on the internet (by fiat), so this guard will get called "confirmed"
2139 * and should immediately become primary.
2141 tt_int_op(guard
->state
, OP_EQ
, GUARD_CIRC_STATE_COMPLETE
);
2142 tt_assert(u
== GUARD_USABLE_NOW
);
2143 tt_int_op(g
->confirmed_idx
, OP_EQ
, 0);
2144 tt_int_op(g
->is_primary
, OP_EQ
, 1);
2145 tt_int_op(g
->is_pending
, OP_EQ
, 0);
2148 guard_selection_free(gs
);
2149 circuit_guard_state_free(guard
);
2153 test_entry_guard_select_for_circuit_highlevel_primary_retry(void *arg
)
2156 const int N_PRIMARY
= DFLT_N_PRIMARY_GUARDS
;
2158 /* At the start, we have no confirmed guards. We'll mark the primary guards
2159 * down, then confirm something else. As soon as we do, it should become
2160 * primary, and we should get it next time. */
2162 time_t start
= approx_time();
2163 guard_selection_t
*gs
= guard_selection_new("default", GS_TYPE_NORMAL
);
2164 circuit_guard_state_t
*guard
= NULL
, *guard2
= NULL
;
2166 const node_t
*node
= NULL
;
2170 /* Declare that we're on the internet. */
2171 entry_guards_note_internet_connectivity(gs
);
2173 /* Make primary guards confirmed (so they won't be superseded by a later
2174 * guard), then mark them down. */
2175 for (i
= 0; i
< N_PRIMARY
; ++i
) {
2176 r
= entry_guard_pick_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
,
2180 tt_int_op(r
, OP_EQ
, 0);
2181 tt_int_op(guard
->state
, OP_EQ
, GUARD_CIRC_STATE_USABLE_ON_COMPLETION
);
2182 g
= entry_guard_handle_get(guard
->guard
);
2183 make_guard_confirmed(gs
, g
);
2184 tt_int_op(g
->is_primary
, OP_EQ
, 1);
2185 entry_guard_failed(&guard
);
2186 circuit_guard_state_free(guard
);
2187 tt_int_op(g
->is_reachable
, OP_EQ
, GUARD_REACHABLE_NO
);
2192 /* Get another guard that we might try. */
2193 r
= entry_guard_pick_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
,
2197 tt_int_op(r
, OP_EQ
, 0);
2198 tt_int_op(guard
->state
, OP_EQ
, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD
);
2199 g
= entry_guard_handle_get(guard
->guard
);
2200 tt_int_op(g
->is_primary
, OP_EQ
, 0);
2202 tt_assert(entry_guards_all_primary_guards_are_down(gs
));
2204 /* And an hour has passed ... */
2205 update_approx_time(start
+ 3600);
2207 /* Say that guard has succeeded! */
2208 u
= entry_guard_succeeded(&guard
);
2209 tt_int_op(u
, OP_EQ
, GUARD_MAYBE_USABLE_LATER
);
2210 tt_int_op(guard
->state
, OP_EQ
, GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD
);
2211 g
= entry_guard_handle_get(guard
->guard
);
2213 /* The primary guards should have been marked up! */
2214 SMARTLIST_FOREACH(gs
->primary_entry_guards
, entry_guard_t
*, pg
, {
2215 tt_int_op(pg
->is_primary
, OP_EQ
, 1);
2216 tt_ptr_op(g
, OP_NE
, pg
);
2217 tt_int_op(pg
->is_reachable
, OP_EQ
, GUARD_REACHABLE_MAYBE
);
2220 /* Have a circuit to a primary guard succeed. */
2221 r
= entry_guard_pick_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
,
2223 tt_int_op(r
, OP_EQ
, 0);
2224 tt_int_op(guard2
->state
, OP_EQ
, GUARD_CIRC_STATE_USABLE_ON_COMPLETION
);
2225 u
= entry_guard_succeeded(&guard2
);
2226 tt_assert(u
== GUARD_USABLE_NOW
);
2227 tt_int_op(guard2
->state
, OP_EQ
, GUARD_CIRC_STATE_COMPLETE
);
2229 tt_assert(! entry_guards_all_primary_guards_are_down(gs
));
2232 guard_selection_free(gs
);
2233 circuit_guard_state_free(guard
);
2234 circuit_guard_state_free(guard2
);
2238 test_entry_guard_select_and_cancel(void *arg
)
2241 const int N_PRIMARY
= DFLT_N_PRIMARY_GUARDS
;
2243 const node_t
*node
= NULL
;
2244 circuit_guard_state_t
*guard
;
2245 guard_selection_t
*gs
= guard_selection_new("default", GS_TYPE_NORMAL
);
2248 /* Once more, we mark all the primary guards down. */
2249 entry_guards_note_internet_connectivity(gs
);
2250 for (i
= 0; i
< N_PRIMARY
; ++i
) {
2251 r
= entry_guard_pick_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
,
2253 tt_int_op(r
, OP_EQ
, 0);
2254 tt_int_op(guard
->state
, OP_EQ
, GUARD_CIRC_STATE_USABLE_ON_COMPLETION
);
2255 g
= entry_guard_handle_get(guard
->guard
);
2256 tt_int_op(g
->is_primary
, OP_EQ
, 1);
2257 tt_int_op(g
->is_pending
, OP_EQ
, 0);
2258 make_guard_confirmed(gs
, g
);
2259 entry_guard_failed(&guard
);
2260 circuit_guard_state_free(guard
);
2265 tt_assert(entry_guards_all_primary_guards_are_down(gs
));
2267 /* Now get another guard we could try... */
2268 r
= entry_guard_pick_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
,
2272 tt_int_op(r
, OP_EQ
, 0);
2273 tt_int_op(guard
->state
, OP_EQ
, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD
);
2274 g
= entry_guard_handle_get(guard
->guard
);
2275 tt_int_op(g
->is_primary
, OP_EQ
, 0);
2276 tt_int_op(g
->is_pending
, OP_EQ
, 1);
2278 /* Whoops! We should never have asked for this guard. Cancel the request! */
2279 entry_guard_cancel(&guard
);
2280 tt_ptr_op(guard
, OP_EQ
, NULL
);
2281 tt_int_op(g
->is_primary
, OP_EQ
, 0);
2282 tt_int_op(g
->is_pending
, OP_EQ
, 0);
2285 guard_selection_free(gs
);
2286 circuit_guard_state_free(guard
);
2290 test_entry_guard_drop_guards(void *arg
)
2294 const node_t
*node
= NULL
;
2295 circuit_guard_state_t
*guard
;
2296 guard_selection_t
*gs
= get_guard_selection_info();
2298 // Pick a guard, to get things set up.
2299 r
= entry_guard_pick_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
,
2301 tt_int_op(r
, OP_EQ
, 0);
2302 tt_int_op(smartlist_len(gs
->sampled_entry_guards
), OP_GE
,
2303 DFLT_MIN_FILTERED_SAMPLE_SIZE
);
2304 tt_ptr_op(gs
, OP_EQ
, get_guard_selection_info());
2306 // Drop all the guards! (This is a bad idea....)
2307 remove_all_entry_guards_for_guard_selection(gs
);
2308 gs
= get_guard_selection_info();
2309 tt_int_op(smartlist_len(gs
->sampled_entry_guards
), OP_EQ
, 0);
2310 tt_int_op(smartlist_len(gs
->primary_entry_guards
), OP_EQ
, 0);
2311 tt_int_op(smartlist_len(gs
->confirmed_entry_guards
), OP_EQ
, 0);
2314 circuit_guard_state_free(guard
);
2315 guard_selection_free(gs
);
2318 /* Unit test setup function: Create a fake network, and set everything up
2319 * for testing the upgrade-a-waiting-circuit code. */
2321 guard_selection_t
*gs
;
2323 circuit_guard_state_t
*guard1_state
;
2324 circuit_guard_state_t
*guard2_state
;
2325 entry_guard_t
*guard1
;
2326 entry_guard_t
*guard2
;
2327 origin_circuit_t
*circ1
;
2328 origin_circuit_t
*circ2
;
2329 smartlist_t
*all_origin_circuits
;
2330 } upgrade_circuits_data_t
;
2332 upgrade_circuits_setup(const struct testcase_t
*testcase
)
2334 upgrade_circuits_data_t
*data
= tor_malloc_zero(sizeof(*data
));
2335 guard_selection_t
*gs
= data
->gs
=
2336 guard_selection_new("default", GS_TYPE_NORMAL
);
2337 circuit_guard_state_t
*guard
;
2341 const int N_PRIMARY
= DFLT_N_PRIMARY_GUARDS
;
2342 const char *argument
= testcase
->setup_data
;
2343 const int make_circ1_succeed
= strstr(argument
, "c1-done") != NULL
;
2344 const int make_circ2_succeed
= strstr(argument
, "c2-done") != NULL
;
2346 big_fake_network_setup(testcase
);
2348 /* We're going to set things up in a state where a circuit will be ready to
2349 * be upgraded. Each test can make a single change (or not) that should
2350 * block the upgrade.
2353 /* First, make all the primary guards confirmed, and down. */
2354 data
->start
= approx_time();
2355 entry_guards_note_internet_connectivity(gs
);
2356 for (i
= 0; i
< N_PRIMARY
; ++i
) {
2357 entry_guard_pick_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
, &node
, &guard
);
2358 g
= entry_guard_handle_get(guard
->guard
);
2359 make_guard_confirmed(gs
, g
);
2360 entry_guard_failed(&guard
);
2361 circuit_guard_state_free(guard
);
2364 /* Grab another couple of guards */
2365 data
->all_origin_circuits
= smartlist_new();
2367 update_approx_time(data
->start
+ 27);
2368 entry_guard_pick_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
,
2369 &node
, &data
->guard1_state
);
2370 origin_circuit_t
*circ
;
2371 data
->circ1
= circ
= origin_circuit_new();
2372 circ
->base_
.purpose
= CIRCUIT_PURPOSE_C_GENERAL
;
2373 circ
->guard_state
= data
->guard1_state
;
2374 smartlist_add(data
->all_origin_circuits
, circ
);
2376 update_approx_time(data
->start
+ 30);
2377 entry_guard_pick_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
,
2378 &node
, &data
->guard2_state
);
2379 data
->circ2
= circ
= origin_circuit_new();
2380 circ
->base_
.purpose
= CIRCUIT_PURPOSE_C_GENERAL
;
2381 circ
->guard_state
= data
->guard2_state
;
2382 smartlist_add(data
->all_origin_circuits
, circ
);
2384 data
->guard1
= entry_guard_handle_get(data
->guard1_state
->guard
);
2385 data
->guard2
= entry_guard_handle_get(data
->guard2_state
->guard
);
2386 tor_assert(data
->guard1
!= data
->guard2
);
2387 tor_assert(data
->guard1_state
->state
==
2388 GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD
);
2389 tor_assert(data
->guard2_state
->state
==
2390 GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD
);
2393 update_approx_time(data
->start
+ 32);
2394 if (make_circ1_succeed
) {
2395 r
= entry_guard_succeeded(&data
->guard1_state
);
2396 tor_assert(r
== GUARD_MAYBE_USABLE_LATER
);
2397 tor_assert(data
->guard1_state
->state
==
2398 GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD
);
2400 update_approx_time(data
->start
+ 33);
2401 if (make_circ2_succeed
) {
2402 r
= entry_guard_succeeded(&data
->guard2_state
);
2403 tor_assert(r
== GUARD_MAYBE_USABLE_LATER
);
2404 tor_assert(data
->guard2_state
->state
==
2405 GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD
);
2411 upgrade_circuits_cleanup(const struct testcase_t
*testcase
, void *ptr
)
2413 upgrade_circuits_data_t
*data
= ptr
;
2414 // circuit_guard_state_free(data->guard1_state); // held in circ1
2415 // circuit_guard_state_free(data->guard2_state); // held in circ2
2416 guard_selection_free(data
->gs
);
2417 smartlist_free(data
->all_origin_circuits
);
2418 circuit_free_(TO_CIRCUIT(data
->circ1
));
2419 circuit_free_(TO_CIRCUIT(data
->circ2
));
2421 return big_fake_network_cleanup(testcase
, NULL
);
2425 test_entry_guard_upgrade_a_circuit(void *arg
)
2427 upgrade_circuits_data_t
*data
= arg
;
2429 /* This is the easy case: we have no COMPLETED circuits, all the
2430 * primary guards are down, we have two WAITING circuits: one will
2431 * get upgraded to COMPLETED! (The one that started first.)
2434 smartlist_t
*result
= smartlist_new();
2436 r
= entry_guards_upgrade_waiting_circuits(data
->gs
,
2437 data
->all_origin_circuits
,
2439 tt_int_op(r
, OP_EQ
, 1);
2440 tt_int_op(smartlist_len(result
), OP_EQ
, 1);
2441 origin_circuit_t
*oc
= smartlist_get(result
, 0);
2443 /* circ1 was started first, so we'll get told to ugrade it... */
2444 tt_ptr_op(oc
, OP_EQ
, data
->circ1
);
2446 /* And the guard state should be complete */
2447 tt_ptr_op(data
->guard1_state
, OP_NE
, NULL
);
2448 tt_int_op(data
->guard1_state
->state
, OP_EQ
, GUARD_CIRC_STATE_COMPLETE
);
2451 smartlist_free(result
);
2455 test_entry_guard_upgrade_blocked_by_live_primary_guards(void *arg
)
2457 upgrade_circuits_data_t
*data
= arg
;
2459 /* If any primary guards might be up, we can't upgrade any waiting
2462 mark_primary_guards_maybe_reachable(data
->gs
);
2464 smartlist_t
*result
= smartlist_new();
2466 setup_capture_of_logs(LOG_DEBUG
);
2467 r
= entry_guards_upgrade_waiting_circuits(data
->gs
,
2468 data
->all_origin_circuits
,
2470 tt_int_op(r
, OP_EQ
, 0);
2471 tt_int_op(smartlist_len(result
), OP_EQ
, 0);
2472 expect_log_msg_containing("not all primary guards were definitely down.");
2475 teardown_capture_of_logs();
2476 smartlist_free(result
);
2480 test_entry_guard_upgrade_blocked_by_lack_of_waiting_circuits(void *arg
)
2482 upgrade_circuits_data_t
*data
= arg
;
2484 /* If no circuits are waiting, we can't upgrade anything. (The test
2485 * setup in this case was told not to make any of the circuits "waiting".)
2487 smartlist_t
*result
= smartlist_new();
2489 setup_capture_of_logs(LOG_DEBUG
);
2490 r
= entry_guards_upgrade_waiting_circuits(data
->gs
,
2491 data
->all_origin_circuits
,
2493 tt_int_op(r
, OP_EQ
, 0);
2494 tt_int_op(smartlist_len(result
), OP_EQ
, 0);
2495 expect_log_msg_containing("Considered upgrading guard-stalled circuits, "
2496 "but didn't find any.");
2499 teardown_capture_of_logs();
2500 smartlist_free(result
);
2504 test_entry_guard_upgrade_blocked_by_better_circ_complete(void *arg
)
2506 upgrade_circuits_data_t
*data
= arg
;
2508 /* We'll run through the logic of upgrade_a_circuit below...
2509 * and then try again to make sure that circ2 isn't also upgraded.
2512 smartlist_t
*result
= smartlist_new();
2514 r
= entry_guards_upgrade_waiting_circuits(data
->gs
,
2515 data
->all_origin_circuits
,
2517 tt_int_op(r
, OP_EQ
, 1);
2518 tt_int_op(smartlist_len(result
), OP_EQ
, 1);
2519 origin_circuit_t
*oc
= smartlist_get(result
, 0);
2520 tt_ptr_op(oc
, OP_EQ
, data
->circ1
);
2521 tt_ptr_op(data
->guard1_state
, OP_NE
, NULL
);
2522 tt_int_op(data
->guard1_state
->state
, OP_EQ
, GUARD_CIRC_STATE_COMPLETE
);
2524 /* Now, try again. Make sure that circ2 isn't upgraded. */
2525 smartlist_clear(result
);
2526 setup_capture_of_logs(LOG_DEBUG
);
2527 r
= entry_guards_upgrade_waiting_circuits(data
->gs
,
2528 data
->all_origin_circuits
,
2530 tt_int_op(r
, OP_EQ
, 0);
2531 tt_int_op(smartlist_len(result
), OP_EQ
, 0);
2532 expect_log_msg_containing("At least one complete circuit had higher "
2533 "priority, so not upgrading.");
2536 teardown_capture_of_logs();
2537 smartlist_free(result
);
2541 test_entry_guard_upgrade_not_blocked_by_restricted_circ_complete(void *arg
)
2543 upgrade_circuits_data_t
*data
= arg
;
2545 /* Once more, let circ1 become complete. But this time, we'll claim
2546 * that circ2 was restricted to not use the same guard as circ1. */
2547 data
->guard2_state
->restrictions
=
2548 guard_create_exit_restriction((uint8_t*)data
->guard1
->identity
);
2550 smartlist_t
*result
= smartlist_new();
2552 r
= entry_guards_upgrade_waiting_circuits(data
->gs
,
2553 data
->all_origin_circuits
,
2555 tt_int_op(r
, OP_EQ
, 1);
2556 tt_int_op(smartlist_len(result
), OP_EQ
, 1);
2557 origin_circuit_t
*oc
= smartlist_get(result
, 0);
2558 tt_ptr_op(oc
, OP_EQ
, data
->circ1
);
2559 tt_ptr_op(data
->guard1_state
, OP_NE
, NULL
);
2560 tt_int_op(data
->guard1_state
->state
, OP_EQ
, GUARD_CIRC_STATE_COMPLETE
);
2562 /* Now, we try again. Since circ2 has a restriction that circ1 doesn't obey,
2563 * circ2 _is_ eligible for upgrade. */
2564 smartlist_clear(result
);
2565 r
= entry_guards_upgrade_waiting_circuits(data
->gs
,
2566 data
->all_origin_circuits
,
2568 tt_int_op(r
, OP_EQ
, 1);
2569 tt_int_op(smartlist_len(result
), OP_EQ
, 1);
2570 origin_circuit_t
*oc2
= smartlist_get(result
, 0);
2571 tt_ptr_op(oc2
, OP_EQ
, data
->circ2
);
2574 smartlist_free(result
);
2578 test_entry_guard_upgrade_not_blocked_by_worse_circ_complete(void *arg
)
2580 upgrade_circuits_data_t
*data
= arg
;
2581 smartlist_t
*result
= smartlist_new();
2582 /* here we manually make circ2 COMPLETE, and make sure that circ1
2583 * gets made complete anyway, since guard1 has higher priority
2585 update_approx_time(data
->start
+ 300);
2586 data
->guard2_state
->state
= GUARD_CIRC_STATE_COMPLETE
;
2587 data
->guard2_state
->state_set_at
= approx_time();
2588 update_approx_time(data
->start
+ 301);
2590 /* Now, try again. Make sure that circ1 is approved. */
2592 r
= entry_guards_upgrade_waiting_circuits(data
->gs
,
2593 data
->all_origin_circuits
,
2595 tt_int_op(r
, OP_EQ
, 1);
2596 tt_int_op(smartlist_len(result
), OP_EQ
, 1);
2597 origin_circuit_t
*oc
= smartlist_get(result
, 0);
2598 tt_ptr_op(oc
, OP_EQ
, data
->circ1
);
2601 smartlist_free(result
);
2605 test_entry_guard_upgrade_blocked_by_better_circ_pending(void *arg
)
2607 upgrade_circuits_data_t
*data
= arg
;
2609 /* circ2 is done, but circ1 is still pending. Since circ1 is better,
2610 * we won't upgrade circ2. */
2612 /* XXXX Prop271 -- this is a kludge. I'm making sure circ1 _is_ better,
2613 * by messing with the guards' confirmed_idx */
2614 make_guard_confirmed(data
->gs
, data
->guard1
);
2617 tmp
= data
->guard1
->confirmed_idx
;
2618 data
->guard1
->confirmed_idx
= data
->guard2
->confirmed_idx
;
2619 data
->guard2
->confirmed_idx
= tmp
;
2622 smartlist_t
*result
= smartlist_new();
2623 setup_capture_of_logs(LOG_DEBUG
);
2625 r
= entry_guards_upgrade_waiting_circuits(data
->gs
,
2626 data
->all_origin_circuits
,
2628 tt_int_op(r
, OP_EQ
, 0);
2629 tt_int_op(smartlist_len(result
), OP_EQ
, 0);
2630 expect_log_msg_containing("but 1 pending circuit(s) had higher guard "
2631 "priority, so not upgrading.");
2634 teardown_capture_of_logs();
2635 smartlist_free(result
);
2639 test_entry_guard_upgrade_not_blocked_by_restricted_circ_pending(void *arg
)
2641 upgrade_circuits_data_t
*data
= arg
;
2642 /* circ2 is done, but circ1 is still pending. But when there is a
2643 restriction on circ2 that circ1 can't satisfy, circ1 can't block
2646 /* XXXX Prop271 -- this is a kludge. I'm making sure circ1 _is_ better,
2647 * by messing with the guards' confirmed_idx */
2648 make_guard_confirmed(data
->gs
, data
->guard1
);
2651 tmp
= data
->guard1
->confirmed_idx
;
2652 data
->guard1
->confirmed_idx
= data
->guard2
->confirmed_idx
;
2653 data
->guard2
->confirmed_idx
= tmp
;
2656 data
->guard2_state
->restrictions
=
2657 guard_create_exit_restriction((uint8_t*)data
->guard1
->identity
);
2659 smartlist_t
*result
= smartlist_new();
2661 r
= entry_guards_upgrade_waiting_circuits(data
->gs
,
2662 data
->all_origin_circuits
,
2664 tt_int_op(r
, OP_EQ
, 1);
2665 tt_int_op(smartlist_len(result
), OP_EQ
, 1);
2666 origin_circuit_t
*oc
= smartlist_get(result
, 0);
2667 tt_ptr_op(oc
, OP_EQ
, data
->circ2
);
2670 smartlist_free(result
);
2674 test_entry_guard_upgrade_not_blocked_by_worse_circ_pending(void *arg
)
2676 upgrade_circuits_data_t
*data
= arg
;
2678 /* circ1 is done, but circ2 is still pending. Since circ1 is better,
2679 * we will upgrade it. */
2680 smartlist_t
*result
= smartlist_new();
2682 r
= entry_guards_upgrade_waiting_circuits(data
->gs
,
2683 data
->all_origin_circuits
,
2685 tt_int_op(r
, OP_EQ
, 1);
2686 tt_int_op(smartlist_len(result
), OP_EQ
, 1);
2687 origin_circuit_t
*oc
= smartlist_get(result
, 0);
2688 tt_ptr_op(oc
, OP_EQ
, data
->circ1
);
2691 smartlist_free(result
);
2695 test_enty_guard_should_expire_waiting(void *arg
)
2698 circuit_guard_state_t
*fake_state
= tor_malloc_zero(sizeof(*fake_state
));
2699 /* We'll leave "guard" unset -- it won't matter here. */
2701 /* No state? Can't expire. */
2702 tt_assert(! entry_guard_state_should_expire(NULL
));
2704 /* Let's try one that expires. */
2705 fake_state
->state
= GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD
;
2706 fake_state
->state_set_at
=
2707 approx_time() - DFLT_NONPRIMARY_GUARD_IDLE_TIMEOUT
- 1;
2709 tt_assert(entry_guard_state_should_expire(fake_state
));
2711 /* But it wouldn't expire if we changed the state. */
2712 fake_state
->state
= GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD
;
2713 tt_assert(! entry_guard_state_should_expire(fake_state
));
2715 /* And it wouldn't have expired a few seconds ago. */
2716 fake_state
->state
= GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD
;
2717 fake_state
->state_set_at
=
2718 approx_time() - DFLT_NONPRIMARY_GUARD_IDLE_TIMEOUT
+ 5;
2719 tt_assert(! entry_guard_state_should_expire(fake_state
));
2722 tor_free(fake_state
);
2725 /** Test that the number of primary guards can be controlled using torrc */
2727 test_entry_guard_number_of_primaries(void *arg
)
2731 /* Get default value */
2732 tt_int_op(get_n_primary_guards(), OP_EQ
, DFLT_N_PRIMARY_GUARDS
);
2734 /* Set number of primaries using torrc */
2735 get_options_mutable()->NumPrimaryGuards
= 42;
2736 tt_int_op(get_n_primary_guards(), OP_EQ
, 42);
2743 mock_directory_initiate_request(directory_request_t
*req
)
2745 if (req
->guard_state
) {
2746 circuit_guard_state_free(req
->guard_state
);
2750 static networkstatus_t
*mock_ns_val
= NULL
;
2751 static networkstatus_t
*
2752 mock_ns_get_by_flavor(consensus_flavor_t f
)
2758 /** Test that when we fetch microdescriptors we skip guards that have
2759 * previously failed to serve us needed microdescriptors. */
2761 test_entry_guard_outdated_dirserver_exclusion(void *arg
)
2764 response_handler_args_t
*args
= NULL
;
2765 dir_connection_t
*conn
= NULL
;
2768 /* Test prep: Make a new guard selection */
2769 guard_selection_t
*gs
= get_guard_selection_by_name("default",
2772 /* ... we want to use entry guards */
2773 or_options_t
*options
= get_options_mutable();
2774 options
->UseEntryGuards
= 1;
2775 options
->UseBridges
= 0;
2777 /* ... prepare some md digests we want to download in the future */
2778 smartlist_t
*digests
= smartlist_new();
2779 const char *prose
= "unhurried and wise, we perceive.";
2780 for (int i
= 0; i
< 20; i
++) {
2781 smartlist_add(digests
, (char*)prose
);
2784 tt_int_op(smartlist_len(digests
), OP_EQ
, 20);
2786 /* ... now mock some functions */
2787 mock_ns_val
= tor_malloc_zero(sizeof(networkstatus_t
));
2788 MOCK(networkstatus_get_latest_consensus_by_flavor
, mock_ns_get_by_flavor
);
2789 MOCK(directory_initiate_request
, mock_directory_initiate_request
);
2792 * 0. Create a proper guard set and primary guard list.
2793 * 1. Pretend to fail microdescriptor fetches from all the primary guards.
2794 * 2. Order another microdescriptor fetch and make sure that primary guards
2795 * get skipped since they failed previous fetches.
2798 { /* Setup primary guard list */
2800 entry_guards_update_primary(gs
);
2801 for (i
= 0; i
< DFLT_N_PRIMARY_GUARDS
; ++i
) {
2802 entry_guard_t
*guard
= smartlist_get(gs
->sampled_entry_guards
, i
);
2803 make_guard_confirmed(gs
, guard
);
2805 entry_guards_update_primary(gs
);
2809 /* Fail microdesc fetches with all the primary guards */
2810 args
= tor_malloc_zero(sizeof(response_handler_args_t
));
2811 args
->status_code
= 404;
2812 args
->reason
= NULL
;
2816 conn
= tor_malloc_zero(sizeof(dir_connection_t
));
2817 conn
->requested_resource
= tor_strdup("d/jlinblackorigami");
2818 conn
->base_
.purpose
= DIR_PURPOSE_FETCH_MICRODESC
;
2820 /* Pretend to fail fetches with all primary guards */
2821 SMARTLIST_FOREACH_BEGIN(gs
->primary_entry_guards
,const entry_guard_t
*,g
) {
2822 memcpy(conn
->identity_digest
, g
->identity
, DIGEST_LEN
);
2824 retval
= handle_response_fetch_microdesc(conn
, args
);
2825 tt_int_op(retval
, OP_EQ
, 0);
2826 } SMARTLIST_FOREACH_END(g
);
2830 /* Now order the final md download */
2831 setup_full_capture_of_logs(LOG_INFO
);
2832 initiate_descriptor_downloads(NULL
, DIR_PURPOSE_FETCH_MICRODESC
,
2835 /* ... and check that because we failed to fetch microdescs from all our
2836 * primaries, we didnt end up selecting a primary for fetching dir info */
2837 expect_log_msg_containing("No primary or confirmed guards available.");
2838 teardown_capture_of_logs();
2842 smartlist_free(digests
);
2845 tor_free(conn
->requested_resource
);
2850 /** Test helper to extend the <b>oc</b> circuit path <b>n</b> times and then
2851 * ensure that the circuit is now complete. */
2853 helper_extend_circuit_path_n_times(origin_circuit_t
*oc
, int n
)
2858 /* Extend path n times */
2859 for (i
= 0 ; i
< n
; i
++) {
2860 retval
= onion_extend_cpath(oc
);
2861 tt_int_op(retval
, OP_EQ
, 0);
2862 tt_int_op(circuit_get_cpath_len(oc
), OP_EQ
, i
+1);
2865 /* Now do it one last time and see that circ is complete */
2866 retval
= onion_extend_cpath(oc
);
2867 tt_int_op(retval
, OP_EQ
, 1);
2873 /** Test for basic Tor path selection. Makes sure we build 3-hop circuits. */
2875 test_entry_guard_basic_path_selection(void *arg
)
2881 /* Enable entry guards */
2882 or_options_t
*options
= get_options_mutable();
2883 options
->UseEntryGuards
= 1;
2885 /* disables /16 check since all nodes have the same addr... */
2886 options
->EnforceDistinctSubnets
= 0;
2888 /* Create our circuit */
2889 circuit_t
*circ
= dummy_origin_circuit_new(30);
2890 origin_circuit_t
*oc
= TO_ORIGIN_CIRCUIT(circ
);
2891 oc
->build_state
= tor_malloc_zero(sizeof(cpath_build_state_t
));
2893 /* First pick the exit and pin it on the build_state */
2894 retval
= onion_pick_cpath_exit(oc
, NULL
, 0);
2895 tt_int_op(retval
, OP_EQ
, 0);
2897 /* Extend path 3 times. First we pick guard, then middle, then exit. */
2898 helper_extend_circuit_path_n_times(oc
, 3);
2901 circuit_free_(circ
);
2904 /** Test helper to build an L2 and L3 vanguard list. The vanguard lists
2905 * produced should be completely disjoint. */
2907 helper_setup_vanguard_list(or_options_t
*options
)
2911 /* Add some nodes to the vanguard L2 list */
2912 options
->HSLayer2Nodes
= routerset_new();
2913 for (i
= 0; i
< 10 ; i
+= 2) {
2914 node_t
*vanguard_node
= smartlist_get(big_fake_net_nodes
, i
);
2915 tt_assert(vanguard_node
->is_possible_guard
);
2916 routerset_parse(options
->HSLayer2Nodes
, vanguard_node
->rs
->nickname
, "l2");
2918 /* also add some nodes to vanguard L3 list
2919 * (L2 list and L3 list should be disjoint for this test to work) */
2920 options
->HSLayer3Nodes
= routerset_new();
2921 for (i
= 10; i
< 20 ; i
+= 2) {
2922 node_t
*vanguard_node
= smartlist_get(big_fake_net_nodes
, i
);
2923 tt_assert(vanguard_node
->is_possible_guard
);
2924 routerset_parse(options
->HSLayer3Nodes
, vanguard_node
->rs
->nickname
, "l3");
2931 /** Test to ensure that vanguard path selection works properly. Ensures that
2932 * default vanguard circuits are 4 hops, and that path selection works
2933 * correctly given the vanguard settings. */
2935 test_entry_guard_vanguard_path_selection(void *arg
)
2941 /* Enable entry guards */
2942 or_options_t
*options
= get_options_mutable();
2943 options
->UseEntryGuards
= 1;
2945 /* XXX disables /16 check */
2946 options
->EnforceDistinctSubnets
= 0;
2948 /* Setup our vanguard list */
2949 helper_setup_vanguard_list(options
);
2951 /* Create our circuit */
2952 circuit_t
*circ
= dummy_origin_circuit_new(30);
2953 origin_circuit_t
*oc
= TO_ORIGIN_CIRCUIT(circ
);
2954 oc
->build_state
= tor_malloc_zero(sizeof(cpath_build_state_t
));
2955 oc
->build_state
->is_internal
= 1;
2957 /* Switch circuit purpose to vanguards */
2958 circ
->purpose
= CIRCUIT_PURPOSE_HS_VANGUARDS
;
2960 /* First pick the exit and pin it on the build_state */
2961 tt_int_op(oc
->build_state
->desired_path_len
, OP_EQ
, 0);
2962 retval
= onion_pick_cpath_exit(oc
, NULL
, 0);
2963 tt_int_op(retval
, OP_EQ
, 0);
2965 /* Ensure that vanguards make 4-hop circuits by default */
2966 tt_int_op(oc
->build_state
->desired_path_len
, OP_EQ
, 4);
2968 /* Extend path as many times as needed to have complete circ. */
2969 helper_extend_circuit_path_n_times(oc
, oc
->build_state
->desired_path_len
);
2971 /* Test that the cpath linked list is set correctly. */
2972 crypt_path_t
*l1_node
= oc
->cpath
;
2973 crypt_path_t
*l2_node
= l1_node
->next
;
2974 crypt_path_t
*l3_node
= l2_node
->next
;
2975 crypt_path_t
*l4_node
= l3_node
->next
;
2976 crypt_path_t
*l1_node_again
= l4_node
->next
;
2977 tt_ptr_op(l1_node
, OP_EQ
, l1_node_again
);
2979 /* Test that L2 is indeed HSLayer2Node */
2980 retval
= routerset_contains_extendinfo(options
->HSLayer2Nodes
,
2981 l2_node
->extend_info
);
2982 tt_int_op(retval
, OP_EQ
, 4);
2983 /* test that L3 node is _not_ contained in HSLayer2Node */
2984 retval
= routerset_contains_extendinfo(options
->HSLayer2Nodes
,
2985 l3_node
->extend_info
);
2986 tt_int_op(retval
, OP_LT
, 4);
2988 /* Test that L3 is indeed HSLayer3Node */
2989 retval
= routerset_contains_extendinfo(options
->HSLayer3Nodes
,
2990 l3_node
->extend_info
);
2991 tt_int_op(retval
, OP_EQ
, 4);
2992 /* test that L2 node is _not_ contained in HSLayer3Node */
2993 retval
= routerset_contains_extendinfo(options
->HSLayer3Nodes
,
2994 l2_node
->extend_info
);
2995 tt_int_op(retval
, OP_LT
, 4);
2997 /* TODO: Test that L1 can be the same as exit. To test this we need start
2998 enforcing EnforceDistinctSubnets again, which means that we need to give
2999 each test node a different address which currently breaks some tests. */
3002 circuit_free_(circ
);
3005 static const struct testcase_setup_t big_fake_network
= {
3006 big_fake_network_setup
, big_fake_network_cleanup
3009 static const struct testcase_setup_t upgrade_circuits
= {
3010 upgrade_circuits_setup
, upgrade_circuits_cleanup
3013 #define BFN_TEST(name) \
3014 { #name, test_entry_guard_ ## name, TT_FORK, &big_fake_network, NULL }
3016 #define UPGRADE_TEST(name, arg) \
3017 { #name, test_entry_guard_ ## name, TT_FORK, &upgrade_circuits, \
3020 struct testcase_t entrynodes_tests
[] = {
3021 { "node_preferred_orport",
3022 test_node_preferred_orport
,
3024 { "entry_guard_describe", test_entry_guard_describe
, 0, NULL
, NULL
},
3025 { "randomize_time", test_entry_guard_randomize_time
, 0, NULL
, NULL
},
3026 { "encode_for_state_minimal",
3027 test_entry_guard_encode_for_state_minimal
, 0, NULL
, NULL
},
3028 { "encode_for_state_maximal",
3029 test_entry_guard_encode_for_state_maximal
, 0, NULL
, NULL
},
3030 { "parse_from_state_minimal",
3031 test_entry_guard_parse_from_state_minimal
, 0, NULL
, NULL
},
3032 { "parse_from_state_maximal",
3033 test_entry_guard_parse_from_state_maximal
, 0, NULL
, NULL
},
3034 { "parse_from_state_failure",
3035 test_entry_guard_parse_from_state_failure
, 0, NULL
, NULL
},
3036 { "parse_from_state_partial_failure",
3037 test_entry_guard_parse_from_state_partial_failure
, 0, NULL
, NULL
},
3038 { "parse_from_state_full",
3039 test_entry_guard_parse_from_state_full
, TT_FORK
, NULL
, NULL
},
3040 { "parse_from_state_broken",
3041 test_entry_guard_parse_from_state_broken
, TT_FORK
, NULL
, NULL
},
3042 { "get_guard_selection_by_name",
3043 test_entry_guard_get_guard_selection_by_name
, TT_FORK
, NULL
, NULL
},
3044 { "number_of_primaries",
3045 test_entry_guard_number_of_primaries
, TT_FORK
, NULL
, NULL
},
3046 BFN_TEST(choose_selection_initial
),
3047 BFN_TEST(add_single_guard
),
3048 BFN_TEST(node_filter
),
3049 BFN_TEST(expand_sample
),
3050 BFN_TEST(expand_sample_small_net
),
3051 BFN_TEST(update_from_consensus_status
),
3052 BFN_TEST(update_from_consensus_repair
),
3053 BFN_TEST(update_from_consensus_remove
),
3054 BFN_TEST(confirming_guards
),
3055 BFN_TEST(sample_reachable_filtered
),
3056 BFN_TEST(sample_reachable_filtered_empty
),
3057 BFN_TEST(retry_unreachable
),
3058 BFN_TEST(manage_primary
),
3059 { "guard_preferred", test_entry_guard_guard_preferred
, TT_FORK
, NULL
, NULL
},
3060 BFN_TEST(select_for_circuit_no_confirmed
),
3061 BFN_TEST(select_for_circuit_confirmed
),
3062 BFN_TEST(select_for_circuit_highlevel_primary
),
3063 BFN_TEST(select_for_circuit_highlevel_confirm_other
),
3064 BFN_TEST(select_for_circuit_highlevel_primary_retry
),
3065 BFN_TEST(select_and_cancel
),
3066 BFN_TEST(drop_guards
),
3067 BFN_TEST(outdated_dirserver_exclusion
),
3068 BFN_TEST(basic_path_selection
),
3069 BFN_TEST(vanguard_path_selection
),
3071 UPGRADE_TEST(upgrade_a_circuit
, "c1-done c2-done"),
3072 UPGRADE_TEST(upgrade_blocked_by_live_primary_guards
, "c1-done c2-done"),
3073 UPGRADE_TEST(upgrade_blocked_by_lack_of_waiting_circuits
, ""),
3074 UPGRADE_TEST(upgrade_blocked_by_better_circ_complete
, "c1-done c2-done"),
3075 UPGRADE_TEST(upgrade_not_blocked_by_restricted_circ_complete
,
3077 UPGRADE_TEST(upgrade_not_blocked_by_worse_circ_complete
, "c1-done c2-done"),
3078 UPGRADE_TEST(upgrade_blocked_by_better_circ_pending
, "c2-done"),
3079 UPGRADE_TEST(upgrade_not_blocked_by_restricted_circ_pending
,
3081 UPGRADE_TEST(upgrade_not_blocked_by_worse_circ_pending
, "c1-done"),
3082 { "should_expire_waiting", test_enty_guard_should_expire_waiting
, TT_FORK
,