1 /* Copyright (c) 2014-2019, 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/nodelist/routerset.h"
30 #include "app/config/statefile.h"
32 #include "core/or/cpath_build_state_st.h"
33 #include "core/or/crypt_path_st.h"
34 #include "feature/dircommon/dir_connection_st.h"
35 #include "feature/nodelist/microdesc_st.h"
36 #include "feature/nodelist/networkstatus_st.h"
37 #include "feature/nodelist/node_st.h"
38 #include "core/or/origin_circuit_st.h"
39 #include "app/config/or_state_st.h"
40 #include "feature/nodelist/routerinfo_st.h"
41 #include "feature/nodelist/routerstatus_st.h"
43 #include "test/test_helpers.h"
44 #include "test/log_test_helpers.h"
46 #include "lib/container/bloomfilt.h"
47 #include "lib/encoding/confline.h"
50 * choose_random_entry() test with state set.
52 * parse_state() tests with more than one guards.
54 * More tests for set_from_config(): Multiple nodes, use fingerprints,
58 /** Dummy Tor state used in unittests. */
59 static or_state_t
*dummy_state
= NULL
;
61 get_or_state_replacement(void)
66 static networkstatus_t
*dummy_consensus
= NULL
;
68 static smartlist_t
*big_fake_net_nodes
= NULL
;
71 bfn_mock_nodelist_get_list(void)
73 return big_fake_net_nodes
;
76 static networkstatus_t
*
77 bfn_mock_networkstatus_get_reasonably_live_consensus(time_t now
, int flavor
)
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_reasonably_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 const char *argument
= testcase
->setup_data
;
141 int reasonably_live_consensus
= 0;
143 reasonably_live_consensus
= strstr(argument
, "reasonably-live") != NULL
;
146 big_fake_net_nodes
= smartlist_new();
147 for (i
= 0; i
< N_NODES
; ++i
) {
148 curve25519_secret_key_t curve25519_secret_key
;
150 node_t
*n
= tor_malloc_zero(sizeof(node_t
));
151 n
->md
= tor_malloc_zero(sizeof(microdesc_t
));
153 /* Generate curve25519 key for this node */
154 n
->md
->onion_curve25519_pkey
=
155 tor_malloc_zero(sizeof(curve25519_public_key_t
));
156 curve25519_secret_key_generate(&curve25519_secret_key
, 0);
157 curve25519_public_key_generate(n
->md
->onion_curve25519_pkey
,
158 &curve25519_secret_key
);
160 crypto_rand(n
->identity
, sizeof(n
->identity
));
161 n
->rs
= tor_malloc_zero(sizeof(routerstatus_t
));
163 memcpy(n
->rs
->identity_digest
, n
->identity
, DIGEST_LEN
);
165 n
->is_running
= n
->is_valid
= n
->is_fast
= n
->is_stable
= 1;
167 /* Note: all these guards have the same address, so you'll need to
168 * disable EnforceDistinctSubnets when a restriction is applied. */
169 n
->rs
->addr
= 0x04020202;
170 n
->rs
->or_port
= 1234;
171 n
->rs
->is_v2_dir
= 1;
172 n
->rs
->has_bandwidth
= 1;
173 n
->rs
->bandwidth_kb
= 30;
175 /* Make a random nickname for each node */
177 char nickname_binary
[8];
178 crypto_rand(nickname_binary
, sizeof(nickname_binary
));
179 base32_encode(n
->rs
->nickname
, sizeof(n
->rs
->nickname
),
180 nickname_binary
, sizeof(nickname_binary
));
183 /* Call half of the nodes a possible guard. */
185 n
->is_possible_guard
= 1;
186 n
->rs
->guardfraction_percentage
= 100;
187 n
->rs
->has_guardfraction
= 1;
188 n
->rs
->is_possible_guard
= 1;
191 /* Make some of these nodes a possible exit */
193 n
->md
->exit_policy
= parse_short_policy("accept 443");
196 smartlist_add(big_fake_net_nodes
, n
);
199 dummy_state
= tor_malloc_zero(sizeof(or_state_t
));
200 dummy_consensus
= tor_malloc_zero(sizeof(networkstatus_t
));
201 if (reasonably_live_consensus
) {
202 /* Make the dummy consensus valid from 4 hours ago, but expired an hour
204 dummy_consensus
->valid_after
= approx_time() - 4*3600;
205 dummy_consensus
->valid_until
= approx_time() - 3600;
207 /* Make the dummy consensus valid for an hour either side of now. */
208 dummy_consensus
->valid_after
= approx_time() - 3600;
209 dummy_consensus
->valid_until
= approx_time() + 3600;
212 MOCK(nodelist_get_list
, bfn_mock_nodelist_get_list
);
213 MOCK(node_get_by_id
, bfn_mock_node_get_by_id
);
215 get_or_state_replacement
);
216 MOCK(networkstatus_get_reasonably_live_consensus
,
217 bfn_mock_networkstatus_get_reasonably_live_consensus
);
218 /* Return anything but NULL (it's interpreted as test fail) */
219 return (void*)testcase
;
223 mock_randomize_time_no_randomization(time_t a
, time_t b
)
229 static or_options_t mocked_options
;
231 static const or_options_t
*
232 mock_get_options(void)
234 return &mocked_options
;
237 #define TEST_IPV4_ADDR "123.45.67.89"
238 #define TEST_IPV6_ADDR "[1234:5678:90ab:cdef::]"
241 test_node_preferred_orport(void *arg
)
244 tor_addr_t ipv4_addr
;
245 const uint16_t ipv4_port
= 4444;
246 tor_addr_t ipv6_addr
;
247 const uint16_t ipv6_port
= 6666;
248 routerinfo_t node_ri
;
253 memset(&mocked_options
, 0, sizeof(mocked_options
));
254 /* We don't test ClientPreferIPv6ORPort here, because it's used in
255 * nodelist_set_consensus to setup node.ipv6_preferred, which we set
257 MOCK(get_options
, mock_get_options
);
259 /* Setup IP addresses */
260 tor_addr_parse(&ipv4_addr
, TEST_IPV4_ADDR
);
261 tor_addr_parse(&ipv6_addr
, TEST_IPV6_ADDR
);
264 memset(&node_ri
, 0, sizeof(node_ri
));
265 node_ri
.addr
= tor_addr_to_ipv4h(&ipv4_addr
);
266 node_ri
.or_port
= ipv4_port
;
267 tor_addr_copy(&node_ri
.ipv6_addr
, &ipv6_addr
);
268 node_ri
.ipv6_orport
= ipv6_port
;
271 memset(&node
, 0, sizeof(node
));
274 /* Check the preferred address is IPv4 if we're only using IPv4, regardless
275 * of whether we prefer it or not */
276 mocked_options
.ClientUseIPv4
= 1;
277 mocked_options
.ClientUseIPv6
= 0;
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 node
.ipv6_preferred
= 1;
284 node_get_pref_orport(&node
, &ap
);
285 tt_assert(tor_addr_eq(&ap
.addr
, &ipv4_addr
));
286 tt_assert(ap
.port
== ipv4_port
);
288 /* Check the preferred address is IPv4 if we're using IPv4 and IPv6, but
289 * don't prefer the IPv6 address */
290 mocked_options
.ClientUseIPv4
= 1;
291 mocked_options
.ClientUseIPv6
= 1;
292 node
.ipv6_preferred
= 0;
293 node_get_pref_orport(&node
, &ap
);
294 tt_assert(tor_addr_eq(&ap
.addr
, &ipv4_addr
));
295 tt_assert(ap
.port
== ipv4_port
);
297 /* Check the preferred address is IPv6 if we prefer it and
298 * ClientUseIPv6 is 1, regardless of ClientUseIPv4 */
299 mocked_options
.ClientUseIPv4
= 1;
300 mocked_options
.ClientUseIPv6
= 1;
301 node
.ipv6_preferred
= 1;
302 node_get_pref_orport(&node
, &ap
);
303 tt_assert(tor_addr_eq(&ap
.addr
, &ipv6_addr
));
304 tt_assert(ap
.port
== ipv6_port
);
306 mocked_options
.ClientUseIPv4
= 0;
307 node_get_pref_orport(&node
, &ap
);
308 tt_assert(tor_addr_eq(&ap
.addr
, &ipv6_addr
));
309 tt_assert(ap
.port
== ipv6_port
);
311 /* Check the preferred address is IPv6 if we don't prefer it, but
312 * ClientUseIPv4 is 0 */
313 mocked_options
.ClientUseIPv4
= 0;
314 mocked_options
.ClientUseIPv6
= 1;
315 node
.ipv6_preferred
= fascist_firewall_prefer_ipv6_orport(&mocked_options
);
316 node_get_pref_orport(&node
, &ap
);
317 tt_assert(tor_addr_eq(&ap
.addr
, &ipv6_addr
));
318 tt_assert(ap
.port
== ipv6_port
);
325 test_entry_guard_describe(void *arg
)
329 memset(&g
, 0, sizeof(g
));
330 strlcpy(g
.nickname
, "okefenokee", sizeof(g
.nickname
));
331 memcpy(g
.identity
, "theforestprimeval---", DIGEST_LEN
);
333 tt_str_op(entry_guard_describe(&g
), OP_EQ
,
334 "okefenokee ($746865666F726573747072696D6576616C2D2D2D)");
341 test_entry_guard_randomize_time(void *arg
)
343 const time_t now
= 1479153573;
344 const int delay
= 86400;
350 for (i
= 0; i
< N
; ++i
) {
351 t
= randomize_time(now
, delay
);
352 tt_int_op(t
, OP_LE
, now
);
353 tt_int_op(t
, OP_GE
, now
-delay
);
356 /* now try the corner cases */
357 for (i
= 0; i
< N
; ++i
) {
358 t
= randomize_time(100, delay
);
359 tt_int_op(t
, OP_GE
, 1);
360 tt_int_op(t
, OP_LE
, 100);
362 t
= randomize_time(0, delay
);
363 tt_int_op(t
, OP_EQ
, 1);
371 test_entry_guard_encode_for_state_minimal(void *arg
)
374 entry_guard_t
*eg
= tor_malloc_zero(sizeof(entry_guard_t
));
376 eg
->selection_name
= tor_strdup("wubwub");
377 memcpy(eg
->identity
, "plurpyflurpyslurpydo", DIGEST_LEN
);
378 eg
->sampled_on_date
= 1479081600;
379 eg
->confirmed_idx
= -1;
382 s
= entry_guard_encode_for_state(eg
);
386 "rsa_id=706C75727079666C75727079736C75727079646F "
387 "sampled_on=2016-11-14T00:00:00 "
391 entry_guard_free(eg
);
396 test_entry_guard_encode_for_state_maximal(void *arg
)
399 entry_guard_t
*eg
= tor_malloc_zero(sizeof(entry_guard_t
));
401 strlcpy(eg
->nickname
, "Fred", sizeof(eg
->nickname
));
402 eg
->selection_name
= tor_strdup("default");
403 memcpy(eg
->identity
, "plurpyflurpyslurpydo", DIGEST_LEN
);
404 eg
->bridge_addr
= tor_malloc_zero(sizeof(tor_addr_port_t
));
405 tor_addr_from_ipv4h(&eg
->bridge_addr
->addr
, 0x08080404);
406 eg
->bridge_addr
->port
= 9999;
407 eg
->sampled_on_date
= 1479081600;
408 eg
->sampled_by_version
= tor_strdup("1.2.3");
409 eg
->unlisted_since_date
= 1479081645;
410 eg
->currently_listed
= 1;
411 eg
->confirmed_on_date
= 1479081690;
412 eg
->confirmed_idx
= 333;
413 eg
->extra_state_fields
= tor_strdup("and the green grass grew all around");
416 s
= entry_guard_encode_for_state(eg
);
420 "rsa_id=706C75727079666C75727079736C75727079646F "
421 "bridge_addr=8.8.4.4:9999 "
423 "sampled_on=2016-11-14T00:00:00 "
425 "unlisted_since=2016-11-14T00:00:45 "
427 "confirmed_on=2016-11-14T00:01:30 "
429 "and the green grass grew all around");
432 entry_guard_free(eg
);
437 test_entry_guard_parse_from_state_minimal(void *arg
)
440 char *mem_op_hex_tmp
= NULL
;
441 entry_guard_t
*eg
= NULL
;
442 time_t t
= approx_time();
444 eg
= entry_guard_parse_from_state(
446 "rsa_id=596f75206d6179206e656564206120686f626279");
449 tt_str_op(eg
->selection_name
, OP_EQ
, "default_plus");
450 test_mem_op_hex(eg
->identity
, OP_EQ
,
451 "596f75206d6179206e656564206120686f626279");
452 tt_str_op(eg
->nickname
, OP_EQ
, "$596F75206D6179206E656564206120686F626279");
453 tt_ptr_op(eg
->bridge_addr
, OP_EQ
, NULL
);
454 tt_i64_op(eg
->sampled_on_date
, OP_GE
, t
);
455 tt_i64_op(eg
->sampled_on_date
, OP_LE
, t
+86400);
456 tt_i64_op(eg
->unlisted_since_date
, OP_EQ
, 0);
457 tt_ptr_op(eg
->sampled_by_version
, OP_EQ
, NULL
);
458 tt_int_op(eg
->currently_listed
, OP_EQ
, 0);
459 tt_i64_op(eg
->confirmed_on_date
, OP_EQ
, 0);
460 tt_int_op(eg
->confirmed_idx
, OP_EQ
, -1);
462 tt_int_op(eg
->last_tried_to_connect
, OP_EQ
, 0);
463 tt_int_op(eg
->is_reachable
, OP_EQ
, GUARD_REACHABLE_MAYBE
);
466 entry_guard_free(eg
);
467 tor_free(mem_op_hex_tmp
);
471 test_entry_guard_parse_from_state_maximal(void *arg
)
474 char *mem_op_hex_tmp
= NULL
;
475 entry_guard_t
*eg
= NULL
;
477 eg
= entry_guard_parse_from_state(
479 "rsa_id=706C75727079666C75727079736C75727079646F "
480 "bridge_addr=[1::3]:9999 "
482 "sampled_on=2016-11-14T00:00:00 "
484 "unlisted_since=2016-11-14T00:00:45 "
486 "confirmed_on=2016-11-14T00:01:30 "
488 "and the green grass grew all around "
489 "rsa_id=all,around");
492 test_mem_op_hex(eg
->identity
, OP_EQ
,
493 "706C75727079666C75727079736C75727079646F");
494 tt_str_op(fmt_addr(&eg
->bridge_addr
->addr
), OP_EQ
, "1::3");
495 tt_int_op(eg
->bridge_addr
->port
, OP_EQ
, 9999);
496 tt_str_op(eg
->nickname
, OP_EQ
, "Fred");
497 tt_i64_op(eg
->sampled_on_date
, OP_EQ
, 1479081600);
498 tt_i64_op(eg
->unlisted_since_date
, OP_EQ
, 1479081645);
499 tt_str_op(eg
->sampled_by_version
, OP_EQ
, "1.2.3");
500 tt_int_op(eg
->currently_listed
, OP_EQ
, 1);
501 tt_i64_op(eg
->confirmed_on_date
, OP_EQ
, 1479081690);
502 tt_int_op(eg
->confirmed_idx
, OP_EQ
, 333);
503 tt_str_op(eg
->extra_state_fields
, OP_EQ
,
504 "and the green grass grew all around rsa_id=all,around");
506 tt_int_op(eg
->last_tried_to_connect
, OP_EQ
, 0);
507 tt_int_op(eg
->is_reachable
, OP_EQ
, GUARD_REACHABLE_MAYBE
);
510 entry_guard_free(eg
);
511 tor_free(mem_op_hex_tmp
);
515 test_entry_guard_parse_from_state_failure(void *arg
)
518 entry_guard_t
*eg
= NULL
;
521 eg
= entry_guard_parse_from_state(
522 "rsa_id=596f75206d6179206e656564206120686f626270");
523 tt_ptr_op(eg
, OP_EQ
, NULL
);
526 eg
= entry_guard_parse_from_state("in=default nickname=Fred");
527 tt_ptr_op(eg
, OP_EQ
, NULL
);
529 /* Bad RSA ID: bad character. */
530 eg
= entry_guard_parse_from_state(
532 "rsa_id=596f75206d6179206e656564206120686f62627q");
533 tt_ptr_op(eg
, OP_EQ
, NULL
);
535 /* Bad RSA ID: too long.*/
536 eg
= entry_guard_parse_from_state(
538 "rsa_id=596f75206d6179206e656564206120686f6262703");
539 tt_ptr_op(eg
, OP_EQ
, NULL
);
541 /* Bad RSA ID: too short.*/
542 eg
= entry_guard_parse_from_state(
544 "rsa_id=596f75206d6179206e65656420612");
545 tt_ptr_op(eg
, OP_EQ
, NULL
);
548 entry_guard_free(eg
);
552 test_entry_guard_parse_from_state_partial_failure(void *arg
)
555 char *mem_op_hex_tmp
= NULL
;
556 entry_guard_t
*eg
= NULL
;
557 time_t t
= approx_time();
559 eg
= entry_guard_parse_from_state(
561 "rsa_id=706C75727079666C75727079736C75727079646F "
562 "bridge_addr=1.2.3.3.4:5 "
563 "nickname=FredIsANodeWithAStrangeNicknameThatIsTooLong "
564 "sampled_on=2016-11-14T00:00:99 "
565 "sampled_by=1.2.3 stuff in the middle "
566 "unlisted_since=2016-xx-14T00:00:45 "
568 "confirmed_on=2016-11-14T00:01:30zz "
570 "and the green grass grew all around "
571 "rsa_id=all,around");
574 test_mem_op_hex(eg
->identity
, OP_EQ
,
575 "706C75727079666C75727079736C75727079646F");
576 tt_str_op(eg
->nickname
, OP_EQ
, "FredIsANodeWithAStrangeNicknameThatIsTooL");
577 tt_ptr_op(eg
->bridge_addr
, OP_EQ
, NULL
);
578 tt_i64_op(eg
->sampled_on_date
, OP_EQ
, t
);
579 tt_i64_op(eg
->unlisted_since_date
, OP_EQ
, 0);
580 tt_str_op(eg
->sampled_by_version
, OP_EQ
, "1.2.3");
581 tt_int_op(eg
->currently_listed
, OP_EQ
, 0);
582 tt_i64_op(eg
->confirmed_on_date
, OP_EQ
, 0);
583 tt_int_op(eg
->confirmed_idx
, OP_EQ
, -1);
584 tt_str_op(eg
->extra_state_fields
, OP_EQ
,
585 "stuff in the middle and the green grass grew all around "
586 "rsa_id=all,around");
588 tt_int_op(eg
->last_tried_to_connect
, OP_EQ
, 0);
589 tt_int_op(eg
->is_reachable
, OP_EQ
, GUARD_REACHABLE_MAYBE
);
592 entry_guard_free(eg
);
593 tor_free(mem_op_hex_tmp
);
597 mock_entry_guard_is_listed(guard_selection_t
*gs
, const entry_guard_t
*guard
)
605 test_entry_guard_parse_from_state_full(void *arg
)
608 /* Here's a state I made while testing. The identities and locations for
609 * the bridges are redacted. */
611 "Guard in=default rsa_id=214F44BD5B638E8C817D47FF7C97397790BF0345 "
612 "nickname=TotallyNinja sampled_on=2016-11-12T19:32:49 "
613 "sampled_by=0.3.0.0-alpha-dev "
615 "Guard in=default rsa_id=052900AB0EA3ED54BAB84AE8A99E74E8693CE2B2 "
616 "nickname=5OfNovember sampled_on=2016-11-20T04:32:05 "
617 "sampled_by=0.3.0.0-alpha-dev "
618 "listed=1 confirmed_on=2016-11-22T08:13:28 confirmed_idx=0 "
619 "pb_circ_attempts=4.000000 pb_circ_successes=2.000000 "
620 "pb_successful_circuits_closed=2.000000\n"
621 "Guard in=default rsa_id=7B700C0C207EBD0002E00F499BE265519AC3C25A "
622 "nickname=dc6jgk11 sampled_on=2016-11-28T11:50:13 "
623 "sampled_by=0.3.0.0-alpha-dev "
624 "listed=1 confirmed_on=2016-11-24T08:45:30 confirmed_idx=4 "
625 "pb_circ_attempts=5.000000 pb_circ_successes=5.000000 "
626 "pb_successful_circuits_closed=5.000000\n"
627 "Guard in=wobblesome rsa_id=7B700C0C207EBD0002E00F499BE265519AC3C25A "
628 "nickname=dc6jgk11 sampled_on=2016-11-28T11:50:13 "
629 "sampled_by=0.3.0.0-alpha-dev "
631 "Guard in=default rsa_id=E9025AD60D86875D5F11548D536CC6AF60F0EF5E "
632 "nickname=maibrunn sampled_on=2016-11-25T22:36:38 "
633 "sampled_by=0.3.0.0-alpha-dev listed=1\n"
634 "Guard in=default rsa_id=DCD30B90BA3A792DA75DC54A327EF353FB84C38E "
635 "nickname=Unnamed sampled_on=2016-11-25T14:34:00 "
636 "sampled_by=0.3.0.0-alpha-dev listed=1\n"
637 "Guard in=bridges rsa_id=8FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2E "
638 "bridge_addr=24.1.1.1:443 sampled_on=2016-11-25T06:44:14 "
639 "sampled_by=0.3.0.0-alpha-dev listed=1 "
640 "confirmed_on=2016-11-29T10:36:06 confirmed_idx=0 "
641 "pb_circ_attempts=8.000000 pb_circ_successes=8.000000 "
642 "pb_successful_circuits_closed=13.000000\n"
643 "Guard in=bridges rsa_id=5800000000000000000000000000000000000000 "
644 "bridge_addr=37.218.246.143:28366 "
645 "sampled_on=2016-11-18T15:07:34 sampled_by=0.3.0.0-alpha-dev listed=1\n";
647 config_line_t
*lines
= NULL
;
648 or_state_t
*state
= tor_malloc_zero(sizeof(or_state_t
));
649 int r
= config_get_lines(STATE
, &lines
, 0);
651 smartlist_t
*text
= smartlist_new();
654 // So nodes aren't expired. This is Tue, 13 Dec 2016 09:37:14 GMT
655 update_approx_time(1481621834);
657 MOCK(entry_guard_is_listed
, mock_entry_guard_is_listed
);
661 get_or_state_replacement
);
663 tt_int_op(r
, OP_EQ
, 0);
666 state
->Guard
= lines
;
668 /* Try it first without setting the result. */
669 r
= entry_guards_parse_state(state
, 0, &msg
);
670 tt_int_op(r
, OP_EQ
, 0);
671 guard_selection_t
*gs_br
=
672 get_guard_selection_by_name("bridges", GS_TYPE_BRIDGE
, 0);
673 tt_ptr_op(gs_br
, OP_EQ
, NULL
);
675 r
= entry_guards_parse_state(state
, 1, &msg
);
676 tt_int_op(r
, OP_EQ
, 0);
677 gs_br
= get_guard_selection_by_name("bridges", GS_TYPE_BRIDGE
, 0);
678 guard_selection_t
*gs_df
=
679 get_guard_selection_by_name("default", GS_TYPE_NORMAL
, 0);
680 guard_selection_t
*gs_wb
=
681 get_guard_selection_by_name("wobblesome", GS_TYPE_NORMAL
, 0);
687 tt_int_op(smartlist_len(gs_df
->sampled_entry_guards
), OP_EQ
, 5);
688 tt_int_op(smartlist_len(gs_br
->sampled_entry_guards
), OP_EQ
, 2);
689 tt_int_op(smartlist_len(gs_wb
->sampled_entry_guards
), OP_EQ
, 1);
691 /* Try again; make sure it doesn't double-add the guards. */
692 r
= entry_guards_parse_state(state
, 1, &msg
);
693 tt_int_op(r
, OP_EQ
, 0);
694 gs_br
= get_guard_selection_by_name("bridges", GS_TYPE_BRIDGE
, 0);
695 gs_df
= get_guard_selection_by_name("default", GS_TYPE_NORMAL
, 0);
698 tt_int_op(smartlist_len(gs_df
->sampled_entry_guards
), OP_EQ
, 5);
699 tt_int_op(smartlist_len(gs_br
->sampled_entry_guards
), OP_EQ
, 2);
701 /* Re-encode; it should be the same... almost. */
703 /* (Make a guard nonpersistent first) */
704 entry_guard_t
*g
= smartlist_get(gs_df
->sampled_entry_guards
, 0);
705 g
->is_persistent
= 0;
707 config_free_lines(lines
);
708 lines
= state
->Guard
= NULL
; // to prevent double-free.
709 entry_guards_update_state(state
);
710 tt_assert(state
->Guard
);
711 lines
= state
->Guard
;
714 for (ln
= lines
; ln
; ln
= ln
->next
) {
715 smartlist_add_asprintf(text
, "%s %s\n",ln
->key
, ln
->value
);
717 joined
= smartlist_join_strings(text
, "", 0, NULL
);
718 tt_str_op(joined
, OP_EQ
,
719 "Guard in=default rsa_id=052900AB0EA3ED54BAB84AE8A99E74E8693CE2B2 "
720 "nickname=5OfNovember sampled_on=2016-11-20T04:32:05 "
721 "sampled_by=0.3.0.0-alpha-dev "
722 "listed=1 confirmed_on=2016-11-22T08:13:28 confirmed_idx=0 "
723 "pb_circ_attempts=4.000000 pb_circ_successes=2.000000 "
724 "pb_successful_circuits_closed=2.000000\n"
725 "Guard in=default rsa_id=7B700C0C207EBD0002E00F499BE265519AC3C25A "
726 "nickname=dc6jgk11 sampled_on=2016-11-28T11:50:13 "
727 "sampled_by=0.3.0.0-alpha-dev "
728 "listed=1 confirmed_on=2016-11-24T08:45:30 confirmed_idx=1 "
729 "pb_circ_attempts=5.000000 pb_circ_successes=5.000000 "
730 "pb_successful_circuits_closed=5.000000\n"
731 "Guard in=default rsa_id=E9025AD60D86875D5F11548D536CC6AF60F0EF5E "
732 "nickname=maibrunn sampled_on=2016-11-25T22:36:38 "
733 "sampled_by=0.3.0.0-alpha-dev listed=1\n"
734 "Guard in=default rsa_id=DCD30B90BA3A792DA75DC54A327EF353FB84C38E "
735 "nickname=Unnamed sampled_on=2016-11-25T14:34:00 "
736 "sampled_by=0.3.0.0-alpha-dev listed=1\n"
737 "Guard in=wobblesome rsa_id=7B700C0C207EBD0002E00F499BE265519AC3C25A "
738 "nickname=dc6jgk11 sampled_on=2016-11-28T11:50:13 "
739 "sampled_by=0.3.0.0-alpha-dev "
741 "Guard in=bridges rsa_id=8FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2E "
742 "bridge_addr=24.1.1.1:443 sampled_on=2016-11-25T06:44:14 "
743 "sampled_by=0.3.0.0-alpha-dev listed=1 "
744 "confirmed_on=2016-11-29T10:36:06 confirmed_idx=0 "
745 "pb_circ_attempts=8.000000 pb_circ_successes=8.000000 "
746 "pb_successful_circuits_closed=13.000000\n"
747 "Guard in=bridges rsa_id=5800000000000000000000000000000000000000 "
748 "bridge_addr=37.218.246.143:28366 "
749 "sampled_on=2016-11-18T15:07:34 sampled_by=0.3.0.0-alpha-dev listed=1\n");
752 config_free_lines(lines
);
755 UNMOCK(get_or_state
);
756 UNMOCK(entry_guard_is_listed
);
757 SMARTLIST_FOREACH(text
, char *, cp
, tor_free(cp
));
758 smartlist_free(text
);
763 test_entry_guard_parse_from_state_broken(void *arg
)
766 /* Here's a variation on the previous state. Every line but the first is
770 "Guard in=default rsa_id=214F44BD5B638E8C817D47FF7C97397790BF0345 "
771 "nickname=TotallyNinja sampled_on=2016-11-12T19:32:49 "
772 "sampled_by=0.3.0.0-alpha-dev "
774 /* No selection listed. */
775 "Guard rsa_id=052900AB0EA3ED54BAB84AE8A99E74E8693CE2B2 "
776 "nickname=5OfNovember sampled_on=2016-11-20T04:32:05 "
777 "sampled_by=0.3.0.0-alpha-dev "
778 "listed=1 confirmed_on=2016-11-22T08:13:28 confirmed_idx=0 "
779 "pb_circ_attempts=4.000000 pb_circ_successes=2.000000 "
780 "pb_successful_circuits_closed=2.000000\n"
781 /* Selection is "legacy"!! */
782 "Guard in=legacy rsa_id=7B700C0C207EBD0002E00F499BE265519AC3C25A "
783 "nickname=dc6jgk11 sampled_on=2016-11-28T11:50:13 "
784 "sampled_by=0.3.0.0-alpha-dev "
785 "listed=1 confirmed_on=2016-11-24T08:45:30 confirmed_idx=4 "
786 "pb_circ_attempts=5.000000 pb_circ_successes=5.000000 "
787 "pb_successful_circuits_closed=5.000000\n";
789 config_line_t
*lines
= NULL
;
790 or_state_t
*state
= tor_malloc_zero(sizeof(or_state_t
));
791 int r
= config_get_lines(STATE
, &lines
, 0);
796 get_or_state_replacement
);
798 tt_int_op(r
, OP_EQ
, 0);
801 state
->Guard
= lines
;
803 /* First, no-set case. we should get an error. */
804 r
= entry_guards_parse_state(state
, 0, &msg
);
805 tt_int_op(r
, OP_LT
, 0);
806 tt_ptr_op(msg
, OP_NE
, NULL
);
807 /* And we shouldn't have made anything. */
808 guard_selection_t
*gs_df
=
809 get_guard_selection_by_name("default", GS_TYPE_NORMAL
, 0);
810 tt_ptr_op(gs_df
, OP_EQ
, NULL
);
813 /* Now see about the set case (which shouldn't happen IRL) */
814 r
= entry_guards_parse_state(state
, 1, &msg
);
815 tt_int_op(r
, OP_LT
, 0);
816 tt_ptr_op(msg
, OP_NE
, NULL
);
817 gs_df
= get_guard_selection_by_name("default", GS_TYPE_NORMAL
, 0);
818 tt_ptr_op(gs_df
, OP_NE
, NULL
);
819 tt_int_op(smartlist_len(gs_df
->sampled_entry_guards
), OP_EQ
, 1);
822 config_free_lines(lines
);
825 UNMOCK(get_or_state
);
829 test_entry_guard_get_guard_selection_by_name(void *arg
)
832 guard_selection_t
*gs1
, *gs2
, *gs3
;
834 gs1
= get_guard_selection_by_name("unlikely", GS_TYPE_NORMAL
, 0);
835 tt_ptr_op(gs1
, OP_EQ
, NULL
);
836 gs1
= get_guard_selection_by_name("unlikely", GS_TYPE_NORMAL
, 1);
837 tt_ptr_op(gs1
, OP_NE
, NULL
);
838 gs2
= get_guard_selection_by_name("unlikely", GS_TYPE_NORMAL
, 1);
839 tt_assert(gs2
== gs1
);
840 gs2
= get_guard_selection_by_name("unlikely", GS_TYPE_NORMAL
, 0);
841 tt_assert(gs2
== gs1
);
843 gs2
= get_guard_selection_by_name("implausible", GS_TYPE_NORMAL
, 0);
844 tt_ptr_op(gs2
, OP_EQ
, NULL
);
845 gs2
= get_guard_selection_by_name("implausible", GS_TYPE_NORMAL
, 1);
846 tt_ptr_op(gs2
, OP_NE
, NULL
);
847 tt_assert(gs2
!= gs1
);
848 gs3
= get_guard_selection_by_name("implausible", GS_TYPE_NORMAL
, 0);
849 tt_assert(gs3
== gs2
);
851 gs3
= get_guard_selection_by_name("default", GS_TYPE_NORMAL
, 0);
852 tt_ptr_op(gs3
, OP_EQ
, NULL
);
853 gs3
= get_guard_selection_by_name("default", GS_TYPE_NORMAL
, 1);
854 tt_ptr_op(gs3
, OP_NE
, NULL
);
855 tt_assert(gs3
!= gs2
);
856 tt_assert(gs3
!= gs1
);
857 tt_assert(gs3
== get_guard_selection_info());
860 entry_guards_free_all();
864 test_entry_guard_choose_selection_initial(void *arg
)
866 /* Tests for picking our initial guard selection (based on having had
867 * no previous selection */
869 guard_selection_type_t type
= GS_TYPE_INFER
;
870 const char *name
= choose_guard_selection(get_options(),
871 dummy_consensus
, NULL
, &type
);
872 tt_str_op(name
, OP_EQ
, "default");
873 tt_int_op(type
, OP_EQ
, GS_TYPE_NORMAL
);
875 /* If we're using bridges, we get the bridge selection. */
876 get_options_mutable()->UseBridges
= 1;
877 name
= choose_guard_selection(get_options(),
878 dummy_consensus
, NULL
, &type
);
879 tt_str_op(name
, OP_EQ
, "bridges");
880 tt_int_op(type
, OP_EQ
, GS_TYPE_BRIDGE
);
881 get_options_mutable()->UseBridges
= 0;
883 /* If we discard >99% of our guards, though, we should be in the restricted
885 tt_assert(get_options_mutable()->EntryNodes
== NULL
);
886 get_options_mutable()->EntryNodes
= routerset_new();
887 routerset_parse(get_options_mutable()->EntryNodes
, "1.0.0.0/8", "foo");
888 name
= choose_guard_selection(get_options(),
889 dummy_consensus
, NULL
, &type
);
890 tt_str_op(name
, OP_EQ
, "restricted");
891 tt_int_op(type
, OP_EQ
, GS_TYPE_RESTRICTED
);
898 test_entry_guard_add_single_guard(void *arg
)
901 guard_selection_t
*gs
= guard_selection_new("default", GS_TYPE_NORMAL
);
903 /* 1: Add a single guard to the sample. */
904 node_t
*n1
= smartlist_get(big_fake_net_nodes
, 0);
905 time_t now
= approx_time();
906 tt_assert(n1
->is_possible_guard
== 1);
907 entry_guard_t
*g1
= entry_guard_add_to_sample(gs
, n1
);
910 /* Make sure its fields look right. */
911 tt_mem_op(n1
->identity
, OP_EQ
, g1
->identity
, DIGEST_LEN
);
912 tt_i64_op(g1
->sampled_on_date
, OP_GE
, now
- 12*86400);
913 tt_i64_op(g1
->sampled_on_date
, OP_LE
, now
);
914 tt_str_op(g1
->sampled_by_version
, OP_EQ
, VERSION
);
915 tt_uint_op(g1
->currently_listed
, OP_EQ
, 1);
916 tt_i64_op(g1
->confirmed_on_date
, OP_EQ
, 0);
917 tt_int_op(g1
->confirmed_idx
, OP_EQ
, -1);
918 tt_int_op(g1
->last_tried_to_connect
, OP_EQ
, 0);
919 tt_uint_op(g1
->is_reachable
, OP_EQ
, GUARD_REACHABLE_MAYBE
);
920 tt_i64_op(g1
->failing_since
, OP_EQ
, 0);
921 tt_uint_op(g1
->is_filtered_guard
, OP_EQ
, 1);
922 tt_uint_op(g1
->is_usable_filtered_guard
, OP_EQ
, 1);
923 tt_uint_op(g1
->is_primary
, OP_EQ
, 0);
924 tt_ptr_op(g1
->extra_state_fields
, OP_EQ
, NULL
);
926 /* Make sure it got added. */
927 tt_int_op(1, OP_EQ
, smartlist_len(gs
->sampled_entry_guards
));
928 tt_ptr_op(g1
, OP_EQ
, smartlist_get(gs
->sampled_entry_guards
, 0));
929 tt_ptr_op(g1
, OP_EQ
, get_sampled_guard_with_id(gs
, (uint8_t*)n1
->identity
));
930 const uint8_t bad_id
[20] = {0};
931 tt_ptr_op(NULL
, OP_EQ
, get_sampled_guard_with_id(gs
, bad_id
));
934 guard_selection_free(gs
);
938 test_entry_guard_node_filter(void *arg
)
941 guard_selection_t
*gs
= guard_selection_new("default", GS_TYPE_NORMAL
);
942 bridge_line_t
*bl
= NULL
;
944 /* Initialize a bunch of node objects that are all guards. */
947 entry_guard_t
*g
[NUM
];
949 for (i
=0; i
< NUM
; ++i
) {
950 n
[i
] = smartlist_get(big_fake_net_nodes
, i
*2); // even ones are guards.
951 g
[i
] = entry_guard_add_to_sample(gs
, n
[i
]);
953 // everything starts out filtered-in
954 tt_uint_op(g
[i
]->is_filtered_guard
, OP_EQ
, 1);
955 tt_uint_op(g
[i
]->is_usable_filtered_guard
, OP_EQ
, 1);
957 tt_int_op(num_reachable_filtered_guards(gs
, NULL
), OP_EQ
, NUM
);
959 /* Make sure refiltering doesn't hurt */
960 entry_guards_update_filtered_sets(gs
);
961 for (i
= 0; i
< NUM
; ++i
) {
962 tt_uint_op(g
[i
]->is_filtered_guard
, OP_EQ
, 1);
963 tt_uint_op(g
[i
]->is_usable_filtered_guard
, OP_EQ
, 1);
965 tt_int_op(num_reachable_filtered_guards(gs
, NULL
), OP_EQ
, NUM
);
967 /* Now start doing things to make the guards get filtered out, 1 by 1. */
970 g
[0]->currently_listed
= 0;
972 /* 1: path bias says this guard is maybe eeeevil. */
973 g
[1]->pb
.path_bias_disabled
= 1;
975 /* 2: Unreachable address. */
978 /* 3: ExcludeNodes */
979 n
[3]->rs
->addr
= 0x90902020;
980 routerset_free(get_options_mutable()->ExcludeNodes
);
981 get_options_mutable()->ExcludeNodes
= routerset_new();
982 routerset_parse(get_options_mutable()->ExcludeNodes
, "144.144.0.0/16", "");
985 get_options_mutable()->UseBridges
= 1;
987 bl
= tor_malloc_zero(sizeof(bridge_line_t
));
988 tor_addr_from_ipv4h(&bl
->addr
, n
[4]->rs
->addr
);
989 bl
->port
= n
[4]->rs
->or_port
;
990 memcpy(bl
->digest
, n
[4]->identity
, 20);
991 bridge_add_from_config(bl
);
992 bl
= NULL
; // prevent free.
993 get_options_mutable()->UseBridges
= 0;
995 /* 5: Unreachable. This stays in the filter, but isn't in usable-filtered */
996 g
[5]->last_tried_to_connect
= approx_time(); // prevent retry.
997 g
[5]->is_reachable
= GUARD_REACHABLE_NO
;
1001 /* Now refilter and inspect. */
1002 entry_guards_update_filtered_sets(gs
);
1003 for (i
= 0; i
< NUM
; ++i
) {
1004 tt_assert(g
[i
]->is_filtered_guard
== (i
== 5 || i
== 6));
1005 tt_assert(g
[i
]->is_usable_filtered_guard
== (i
== 6));
1007 tt_int_op(num_reachable_filtered_guards(gs
, NULL
), OP_EQ
, 1);
1009 /* Now make sure we have no live consensus, and no nodes. Nothing should
1010 * pass the filter any more. */
1011 tor_free(dummy_consensus
);
1012 dummy_consensus
= NULL
;
1013 SMARTLIST_FOREACH(big_fake_net_nodes
, node_t
*, node
, {
1014 memset(node
->identity
, 0xff, 20);
1016 entry_guards_update_filtered_sets(gs
);
1017 for (i
= 0; i
< NUM
; ++i
) {
1018 tt_uint_op(g
[i
]->is_filtered_guard
, OP_EQ
, 0);
1019 tt_uint_op(g
[i
]->is_usable_filtered_guard
, OP_EQ
, 0);
1021 tt_int_op(num_reachable_filtered_guards(gs
, NULL
), OP_EQ
, 0);
1024 guard_selection_free(gs
);
1030 test_entry_guard_expand_sample(void *arg
)
1033 guard_selection_t
*gs
= guard_selection_new("default", GS_TYPE_NORMAL
);
1034 digestmap_t
*node_by_id
= digestmap_new();
1036 entry_guard_t
*guard
= entry_guards_expand_sample(gs
);
1037 tt_assert(guard
); // the last guard returned.
1039 // Every sampled guard here should be filtered and reachable for now.
1040 tt_int_op(smartlist_len(gs
->sampled_entry_guards
), OP_EQ
,
1041 num_reachable_filtered_guards(gs
, NULL
));
1043 /* Make sure we got the right number. */
1044 tt_int_op(DFLT_MIN_FILTERED_SAMPLE_SIZE
, OP_EQ
,
1045 num_reachable_filtered_guards(gs
, NULL
));
1047 // Make sure everything we got was from our fake node list, and everything
1049 SMARTLIST_FOREACH_BEGIN(gs
->sampled_entry_guards
, entry_guard_t
*, g
) {
1050 const node_t
*n
= bfn_mock_node_get_by_id(g
->identity
);
1052 tt_ptr_op(NULL
, OP_EQ
, digestmap_get(node_by_id
, g
->identity
));
1053 digestmap_set(node_by_id
, g
->identity
, (void*) n
);
1054 int idx
= smartlist_pos(big_fake_net_nodes
, n
);
1055 // The even ones are the guards; make sure we got guards.
1056 tt_int_op(idx
& 1, OP_EQ
, 0);
1057 } SMARTLIST_FOREACH_END(g
);
1059 // Nothing became unusable/unfiltered, so a subsequent expand should
1061 guard
= entry_guards_expand_sample(gs
);
1062 tt_ptr_op(guard
, OP_EQ
, NULL
); // no guard was added.
1063 tt_int_op(DFLT_MIN_FILTERED_SAMPLE_SIZE
, OP_EQ
,
1064 num_reachable_filtered_guards(gs
, NULL
));
1066 // Make a few guards unreachable.
1067 guard
= smartlist_get(gs
->sampled_entry_guards
, 0);
1068 guard
->is_usable_filtered_guard
= 0;
1069 guard
= smartlist_get(gs
->sampled_entry_guards
, 1);
1070 guard
->is_usable_filtered_guard
= 0;
1071 guard
= smartlist_get(gs
->sampled_entry_guards
, 2);
1072 guard
->is_usable_filtered_guard
= 0;
1073 tt_int_op(DFLT_MIN_FILTERED_SAMPLE_SIZE
- 3, OP_EQ
,
1074 num_reachable_filtered_guards(gs
, NULL
));
1076 // This time, expanding the sample will add some more guards.
1077 guard
= entry_guards_expand_sample(gs
);
1078 tt_assert(guard
); // no guard was added.
1079 tt_int_op(DFLT_MIN_FILTERED_SAMPLE_SIZE
, OP_EQ
,
1080 num_reachable_filtered_guards(gs
, NULL
));
1081 tt_int_op(smartlist_len(gs
->sampled_entry_guards
), OP_EQ
,
1082 num_reachable_filtered_guards(gs
, NULL
)+3);
1084 // Still idempotent.
1085 guard
= entry_guards_expand_sample(gs
);
1086 tt_ptr_op(guard
, OP_EQ
, NULL
); // no guard was added.
1087 tt_int_op(DFLT_MIN_FILTERED_SAMPLE_SIZE
, OP_EQ
,
1088 num_reachable_filtered_guards(gs
, NULL
));
1090 // Now, do a nasty trick: tell the filter to exclude 31/32 of the guards.
1091 // This will cause the sample size to get reeeeally huge, while the
1092 // filtered sample size grows only slowly.
1093 routerset_free(get_options_mutable()->ExcludeNodes
);
1094 get_options_mutable()->ExcludeNodes
= routerset_new();
1095 routerset_parse(get_options_mutable()->ExcludeNodes
, "144.144.0.0/16", "");
1096 SMARTLIST_FOREACH(big_fake_net_nodes
, node_t
*, n
, {
1097 if (n_sl_idx
% 64 != 0) {
1098 n
->rs
->addr
= 0x90903030;
1101 entry_guards_update_filtered_sets(gs
);
1103 // Surely (p ~ 1-2**-60), one of our guards has been excluded.
1104 tt_int_op(num_reachable_filtered_guards(gs
, NULL
), OP_LT
,
1105 DFLT_MIN_FILTERED_SAMPLE_SIZE
);
1107 // Try to regenerate the guards.
1108 guard
= entry_guards_expand_sample(gs
);
1109 tt_assert(guard
); // no guard was added.
1111 /* this time, it's possible that we didn't add enough sampled guards. */
1112 tt_int_op(num_reachable_filtered_guards(gs
, NULL
), OP_LE
,
1113 DFLT_MIN_FILTERED_SAMPLE_SIZE
);
1114 /* but we definitely didn't exceed the sample maximum. */
1115 const int n_guards
= 271 / 2;
1116 tt_int_op(smartlist_len(gs
->sampled_entry_guards
), OP_LE
,
1117 (int)(n_guards
* .3));
1120 guard_selection_free(gs
);
1121 digestmap_free(node_by_id
, NULL
);
1125 test_entry_guard_expand_sample_small_net(void *arg
)
1128 guard_selection_t
*gs
= guard_selection_new("default", GS_TYPE_NORMAL
);
1130 /* Fun corner case: not enough guards to make up our whole sample size. */
1131 SMARTLIST_FOREACH(big_fake_net_nodes
, node_t
*, n
, {
1132 if (n_sl_idx
>= 15) {
1134 SMARTLIST_DEL_CURRENT(big_fake_net_nodes
, n
);
1136 n
->rs
->addr
= 0; // make the filter reject this.
1140 entry_guard_t
*guard
= entry_guards_expand_sample(gs
);
1141 tt_assert(guard
); // the last guard returned -- some guard was added.
1142 // half the nodes are guards, so we have 8 guards left. The set
1143 // is small, so we sampled everything.
1144 tt_int_op(smartlist_len(gs
->sampled_entry_guards
), OP_EQ
, 8);
1145 tt_int_op(num_reachable_filtered_guards(gs
, NULL
), OP_EQ
, 0);
1147 guard_selection_free(gs
);
1151 test_entry_guard_update_from_consensus_status(void *arg
)
1153 /* Here we're going to have some nodes become un-guardy, and say we got a
1154 * new consensus. This should cause those nodes to get detected as
1159 time_t start
= approx_time();
1160 guard_selection_t
*gs
= guard_selection_new("default", GS_TYPE_NORMAL
);
1161 networkstatus_t
*ns_tmp
= NULL
;
1163 /* Don't randomly backdate stuff; it will make correctness harder to check.*/
1164 MOCK(randomize_time
, mock_randomize_time_no_randomization
);
1166 /* First, sample some guards. */
1167 entry_guards_expand_sample(gs
);
1168 int n_sampled_pre
= smartlist_len(gs
->sampled_entry_guards
);
1169 int n_filtered_pre
= num_reachable_filtered_guards(gs
, NULL
);
1170 tt_i64_op(n_sampled_pre
, OP_EQ
, n_filtered_pre
);
1171 tt_i64_op(n_sampled_pre
, OP_GT
, 10);
1173 /* At this point, it should be a no-op to do this: */
1174 sampled_guards_update_from_consensus(gs
);
1176 /* Now let's make some of our guards become unlisted. The easiest way to
1177 * do that would be to take away their guard flag. */
1178 for (i
= 0; i
< 5; ++i
) {
1179 entry_guard_t
*g
= smartlist_get(gs
->sampled_entry_guards
, i
);
1180 node_t
*n
= (node_t
*) bfn_mock_node_get_by_id(g
->identity
);
1182 n
->is_possible_guard
= 0;
1185 update_approx_time(start
+ 30);
1187 /* try this with no live networkstatus. Nothing should happen! */
1188 ns_tmp
= dummy_consensus
;
1189 dummy_consensus
= NULL
;
1190 sampled_guards_update_from_consensus(gs
);
1191 tt_i64_op(smartlist_len(gs
->sampled_entry_guards
), OP_EQ
, n_sampled_pre
);
1192 tt_i64_op(num_reachable_filtered_guards(gs
, NULL
), OP_EQ
, n_filtered_pre
);
1193 /* put the networkstatus back. */
1194 dummy_consensus
= ns_tmp
;
1198 /* Now those guards should become unlisted, and drop off the filter, but
1199 * stay in the sample. */
1200 update_approx_time(start
+ 60);
1201 sampled_guards_update_from_consensus(gs
);
1203 tt_i64_op(smartlist_len(gs
->sampled_entry_guards
), OP_EQ
, n_sampled_pre
);
1204 tt_i64_op(num_reachable_filtered_guards(gs
, NULL
), OP_EQ
, n_filtered_pre
-5);
1205 for (i
= 0; i
< 5; ++i
) {
1206 entry_guard_t
*g
= smartlist_get(gs
->sampled_entry_guards
, i
);
1207 tt_assert(! g
->currently_listed
);
1208 tt_i64_op(g
->unlisted_since_date
, OP_EQ
, start
+60);
1210 for (i
= 5; i
< n_sampled_pre
; ++i
) {
1211 entry_guard_t
*g
= smartlist_get(gs
->sampled_entry_guards
, i
);
1212 tt_assert(g
->currently_listed
);
1213 tt_i64_op(g
->unlisted_since_date
, OP_EQ
, 0);
1216 /* Now re-list one, and remove one completely. */
1218 entry_guard_t
*g
= smartlist_get(gs
->sampled_entry_guards
, 0);
1219 node_t
*n
= (node_t
*) bfn_mock_node_get_by_id(g
->identity
);
1221 n
->is_possible_guard
= 1;
1224 /* try removing the node, to make sure we don't crash on an absent node
1226 entry_guard_t
*g
= smartlist_get(gs
->sampled_entry_guards
, 5);
1227 node_t
*n
= (node_t
*) bfn_mock_node_get_by_id(g
->identity
);
1229 smartlist_remove(big_fake_net_nodes
, n
);
1232 update_approx_time(start
+ 300);
1233 sampled_guards_update_from_consensus(gs
);
1235 /* guards 1..5 are now unlisted; 0,6,7.. are listed. */
1236 tt_i64_op(smartlist_len(gs
->sampled_entry_guards
), OP_EQ
, n_sampled_pre
);
1237 for (i
= 1; i
< 6; ++i
) {
1238 entry_guard_t
*g
= smartlist_get(gs
->sampled_entry_guards
, i
);
1239 tt_assert(! g
->currently_listed
);
1241 tt_i64_op(g
->unlisted_since_date
, OP_EQ
, start
+300);
1243 tt_i64_op(g
->unlisted_since_date
, OP_EQ
, start
+60);
1245 for (i
= 0; i
< n_sampled_pre
; i
= (!i
) ? 6 : i
+1) { /* 0,6,7,8, ... */
1246 entry_guard_t
*g
= smartlist_get(gs
->sampled_entry_guards
, i
);
1247 tt_assert(g
->currently_listed
);
1248 tt_i64_op(g
->unlisted_since_date
, OP_EQ
, 0);
1252 tor_free(ns_tmp
); /* in case we couldn't put it back */
1253 guard_selection_free(gs
);
1254 UNMOCK(randomize_time
);
1258 test_entry_guard_update_from_consensus_repair(void *arg
)
1260 /* Here we'll make sure that our code to repair the unlisted-since
1261 * times is correct. */
1265 time_t start
= approx_time();
1266 guard_selection_t
*gs
= guard_selection_new("default", GS_TYPE_NORMAL
);
1268 /* Don't randomly backdate stuff; it will make correctness harder to check.*/
1269 MOCK(randomize_time
, mock_randomize_time_no_randomization
);
1271 /* First, sample some guards. */
1272 entry_guards_expand_sample(gs
);
1273 int n_sampled_pre
= smartlist_len(gs
->sampled_entry_guards
);
1274 int n_filtered_pre
= num_reachable_filtered_guards(gs
, NULL
);
1275 tt_i64_op(n_sampled_pre
, OP_EQ
, n_filtered_pre
);
1276 tt_i64_op(n_sampled_pre
, OP_GT
, 10);
1278 /* Now corrupt the list a bit. Call some unlisted-since-never, and some
1279 * listed-and-unlisted-since-a-time. */
1280 update_approx_time(start
+ 300);
1281 for (i
= 0; i
< 3; ++i
) {
1282 /* these will get a date. */
1283 entry_guard_t
*g
= smartlist_get(gs
->sampled_entry_guards
, i
);
1284 node_t
*n
= (node_t
*) bfn_mock_node_get_by_id(g
->identity
);
1286 n
->is_possible_guard
= 0;
1287 g
->currently_listed
= 0;
1289 for (i
= 3; i
< 6; ++i
) {
1290 /* these will become listed. */
1291 entry_guard_t
*g
= smartlist_get(gs
->sampled_entry_guards
, i
);
1292 g
->unlisted_since_date
= start
+100;
1294 setup_full_capture_of_logs(LOG_WARN
);
1295 sampled_guards_update_from_consensus(gs
);
1296 expect_log_msg_containing(
1297 "was listed, but with unlisted_since_date set");
1298 expect_log_msg_containing(
1299 "was unlisted, but with unlisted_since_date unset");
1300 teardown_capture_of_logs();
1302 tt_int_op(smartlist_len(gs
->sampled_entry_guards
), OP_EQ
, n_sampled_pre
);
1303 tt_int_op(num_reachable_filtered_guards(gs
, NULL
), OP_EQ
, n_filtered_pre
-3);
1304 for (i
= 3; i
< n_sampled_pre
; ++i
) {
1305 /* these will become listed. */
1306 entry_guard_t
*g
= smartlist_get(gs
->sampled_entry_guards
, i
);
1308 tt_assert(! g
->currently_listed
);
1309 tt_i64_op(g
->unlisted_since_date
, OP_EQ
, start
+300);
1311 tt_assert(g
->currently_listed
);
1312 tt_i64_op(g
->unlisted_since_date
, OP_EQ
, 0);
1317 teardown_capture_of_logs();
1318 guard_selection_free(gs
);
1319 UNMOCK(randomize_time
);
1323 test_entry_guard_update_from_consensus_remove(void *arg
)
1325 /* Now let's check the logic responsible for removing guards from the
1326 * sample entirely. */
1330 guard_selection_t
*gs
= guard_selection_new("default", GS_TYPE_NORMAL
);
1331 smartlist_t
*keep_ids
= smartlist_new();
1332 smartlist_t
*remove_ids
= smartlist_new();
1334 /* Don't randomly backdate stuff; it will make correctness harder to check.*/
1335 MOCK(randomize_time
, mock_randomize_time_no_randomization
);
1337 /* First, sample some guards. */
1338 entry_guards_expand_sample(gs
);
1339 int n_sampled_pre
= smartlist_len(gs
->sampled_entry_guards
);
1340 int n_filtered_pre
= num_reachable_filtered_guards(gs
, NULL
);
1341 tt_i64_op(n_sampled_pre
, OP_EQ
, n_filtered_pre
);
1342 tt_i64_op(n_sampled_pre
, OP_GT
, 10);
1344 const time_t one_day_ago
= approx_time() - 1*24*60*60;
1345 const time_t one_year_ago
= approx_time() - 365*24*60*60;
1346 const time_t two_years_ago
= approx_time() - 2*365*24*60*60;
1347 /* 0: unlisted for a day. (keep this) */
1349 entry_guard_t
*g
= smartlist_get(gs
->sampled_entry_guards
, 0);
1350 node_t
*n
= (node_t
*) bfn_mock_node_get_by_id(g
->identity
);
1352 n
->is_possible_guard
= 0;
1353 g
->currently_listed
= 0;
1354 g
->unlisted_since_date
= one_day_ago
;
1355 smartlist_add(keep_ids
, tor_memdup(g
->identity
, 20));
1357 /* 1: unlisted for a year. (remove this) */
1359 entry_guard_t
*g
= smartlist_get(gs
->sampled_entry_guards
, 1);
1360 node_t
*n
= (node_t
*) bfn_mock_node_get_by_id(g
->identity
);
1362 n
->is_possible_guard
= 0;
1363 g
->currently_listed
= 0;
1364 g
->unlisted_since_date
= one_year_ago
;
1365 smartlist_add(remove_ids
, tor_memdup(g
->identity
, 20));
1367 /* 2: added a day ago, never confirmed. (keep this) */
1369 entry_guard_t
*g
= smartlist_get(gs
->sampled_entry_guards
, 2);
1370 g
->sampled_on_date
= one_day_ago
;
1371 smartlist_add(keep_ids
, tor_memdup(g
->identity
, 20));
1373 /* 3: added a year ago, never confirmed. (remove this) */
1375 entry_guard_t
*g
= smartlist_get(gs
->sampled_entry_guards
, 3);
1376 g
->sampled_on_date
= one_year_ago
;
1377 smartlist_add(remove_ids
, tor_memdup(g
->identity
, 20));
1379 /* 4: added two year ago, confirmed yesterday, primary. (keep this.) */
1381 entry_guard_t
*g
= smartlist_get(gs
->sampled_entry_guards
, 4);
1382 g
->sampled_on_date
= one_year_ago
;
1383 g
->confirmed_on_date
= one_day_ago
;
1384 g
->confirmed_idx
= 0;
1386 smartlist_add(gs
->confirmed_entry_guards
, g
);
1387 smartlist_add(gs
->primary_entry_guards
, g
);
1388 smartlist_add(keep_ids
, tor_memdup(g
->identity
, 20));
1390 /* 5: added two years ago, confirmed a year ago, primary. (remove this) */
1392 entry_guard_t
*g
= smartlist_get(gs
->sampled_entry_guards
, 5);
1393 g
->sampled_on_date
= two_years_ago
;
1394 g
->confirmed_on_date
= one_year_ago
;
1395 g
->confirmed_idx
= 1;
1397 smartlist_add(gs
->confirmed_entry_guards
, g
);
1398 smartlist_add(gs
->primary_entry_guards
, g
);
1399 smartlist_add(remove_ids
, tor_memdup(g
->identity
, 20));
1402 sampled_guards_update_from_consensus(gs
);
1404 /* Did we remove the right ones? */
1405 SMARTLIST_FOREACH(keep_ids
, uint8_t *, id
, {
1406 tt_assert(get_sampled_guard_with_id(gs
, id
) != NULL
);
1408 SMARTLIST_FOREACH(remove_ids
, uint8_t *, id
, {
1409 tt_want(get_sampled_guard_with_id(gs
, id
) == NULL
);
1412 /* Did we remove the right number? */
1413 tt_int_op(smartlist_len(gs
->sampled_entry_guards
), OP_EQ
, n_sampled_pre
- 3);
1416 guard_selection_free(gs
);
1417 UNMOCK(randomize_time
);
1418 SMARTLIST_FOREACH(keep_ids
, char *, cp
, tor_free(cp
));
1419 SMARTLIST_FOREACH(remove_ids
, char *, cp
, tor_free(cp
));
1420 smartlist_free(keep_ids
);
1421 smartlist_free(remove_ids
);
1425 test_entry_guard_confirming_guards(void *arg
)
1428 /* Now let's check the logic responsible for manipulating the list
1429 * of confirmed guards */
1430 guard_selection_t
*gs
= guard_selection_new("default", GS_TYPE_NORMAL
);
1431 MOCK(randomize_time
, mock_randomize_time_no_randomization
);
1433 /* Create the sample. */
1434 entry_guards_expand_sample(gs
);
1436 /* Confirm a few guards. */
1437 time_t start
= approx_time();
1438 entry_guard_t
*g1
= smartlist_get(gs
->sampled_entry_guards
, 0);
1439 entry_guard_t
*g2
= smartlist_get(gs
->sampled_entry_guards
, 1);
1440 entry_guard_t
*g3
= smartlist_get(gs
->sampled_entry_guards
, 8);
1441 make_guard_confirmed(gs
, g2
);
1442 update_approx_time(start
+ 10);
1443 make_guard_confirmed(gs
, g1
);
1444 make_guard_confirmed(gs
, g3
);
1446 /* Were the correct dates and indices fed in? */
1447 tt_int_op(g1
->confirmed_idx
, OP_EQ
, 1);
1448 tt_int_op(g2
->confirmed_idx
, OP_EQ
, 0);
1449 tt_int_op(g3
->confirmed_idx
, OP_EQ
, 2);
1450 tt_i64_op(g1
->confirmed_on_date
, OP_EQ
, start
+10);
1451 tt_i64_op(g2
->confirmed_on_date
, OP_EQ
, start
);
1452 tt_i64_op(g3
->confirmed_on_date
, OP_EQ
, start
+10);
1453 tt_ptr_op(smartlist_get(gs
->confirmed_entry_guards
, 0), OP_EQ
, g2
);
1454 tt_ptr_op(smartlist_get(gs
->confirmed_entry_guards
, 1), OP_EQ
, g1
);
1455 tt_ptr_op(smartlist_get(gs
->confirmed_entry_guards
, 2), OP_EQ
, g3
);
1457 /* Now make sure we can regenerate the confirmed_entry_guards list. */
1458 smartlist_clear(gs
->confirmed_entry_guards
);
1459 g2
->confirmed_idx
= 0;
1460 g1
->confirmed_idx
= 10;
1461 g3
->confirmed_idx
= 100;
1462 entry_guards_update_confirmed(gs
);
1463 tt_int_op(g1
->confirmed_idx
, OP_EQ
, 1);
1464 tt_int_op(g2
->confirmed_idx
, OP_EQ
, 0);
1465 tt_int_op(g3
->confirmed_idx
, OP_EQ
, 2);
1466 tt_ptr_op(smartlist_get(gs
->confirmed_entry_guards
, 0), OP_EQ
, g2
);
1467 tt_ptr_op(smartlist_get(gs
->confirmed_entry_guards
, 1), OP_EQ
, g1
);
1468 tt_ptr_op(smartlist_get(gs
->confirmed_entry_guards
, 2), OP_EQ
, g3
);
1470 /* Now make sure we can regenerate the confirmed_entry_guards list if
1471 * the indices are messed up. */
1472 g1
->confirmed_idx
= g2
->confirmed_idx
= g3
->confirmed_idx
= 999;
1473 smartlist_clear(gs
->confirmed_entry_guards
);
1474 entry_guards_update_confirmed(gs
);
1475 tt_int_op(g1
->confirmed_idx
, OP_GE
, 0);
1476 tt_int_op(g2
->confirmed_idx
, OP_GE
, 0);
1477 tt_int_op(g3
->confirmed_idx
, OP_GE
, 0);
1478 tt_int_op(g1
->confirmed_idx
, OP_LE
, 2);
1479 tt_int_op(g2
->confirmed_idx
, OP_LE
, 2);
1480 tt_int_op(g3
->confirmed_idx
, OP_LE
, 2);
1481 g1
= smartlist_get(gs
->confirmed_entry_guards
, 0);
1482 g2
= smartlist_get(gs
->confirmed_entry_guards
, 1);
1483 g3
= smartlist_get(gs
->confirmed_entry_guards
, 2);
1484 tt_int_op(g1
->confirmed_idx
, OP_EQ
, 0);
1485 tt_int_op(g2
->confirmed_idx
, OP_EQ
, 1);
1486 tt_int_op(g3
->confirmed_idx
, OP_EQ
, 2);
1487 tt_assert(g1
!= g2
);
1488 tt_assert(g1
!= g3
);
1489 tt_assert(g2
!= g3
);
1492 UNMOCK(randomize_time
);
1493 guard_selection_free(gs
);
1497 test_entry_guard_sample_reachable_filtered(void *arg
)
1500 guard_selection_t
*gs
= guard_selection_new("default", GS_TYPE_NORMAL
);
1501 entry_guards_expand_sample(gs
);
1502 const int N
= 10000;
1503 bitarray_t
*selected
= NULL
;
1506 /* We've got a sampled list now; let's make one non-usable-filtered; some
1507 * confirmed, some primary, some pending.
1509 int n_guards
= smartlist_len(gs
->sampled_entry_guards
);
1510 tt_int_op(n_guards
, OP_GT
, 10);
1512 g
= smartlist_get(gs
->sampled_entry_guards
, 0);
1514 g
= smartlist_get(gs
->sampled_entry_guards
, 1);
1515 make_guard_confirmed(gs
, g
);
1516 g
= smartlist_get(gs
->sampled_entry_guards
, 2);
1518 g
= smartlist_get(gs
->sampled_entry_guards
, 3);
1519 g
->pb
.path_bias_disabled
= 1;
1521 entry_guards_update_filtered_sets(gs
);
1522 gs
->primary_guards_up_to_date
= 1;
1523 tt_int_op(num_reachable_filtered_guards(gs
, NULL
), OP_EQ
, n_guards
- 1);
1524 tt_int_op(smartlist_len(gs
->sampled_entry_guards
), OP_EQ
, n_guards
);
1526 // +1 since the one we made disabled will make another one get added.
1529 /* Try a bunch of selections. */
1534 { SAMPLE_EXCLUDE_CONFIRMED
, 1 },
1535 { SAMPLE_EXCLUDE_PRIMARY
|SAMPLE_NO_UPDATE_PRIMARY
, 2 },
1536 { SAMPLE_EXCLUDE_PENDING
, 0 },
1540 for (j
= 0; tests
[j
].flag
>= 0; ++j
) {
1541 selected
= bitarray_init_zero(n_guards
);
1542 const int excluded_flags
= tests
[j
].flag
;
1543 const int excluded_idx
= tests
[j
].idx
;
1544 for (i
= 0; i
< N
; ++i
) {
1545 g
= sample_reachable_filtered_entry_guards(gs
, NULL
, excluded_flags
);
1547 int pos
= smartlist_pos(gs
->sampled_entry_guards
, g
);
1548 tt_int_op(smartlist_len(gs
->sampled_entry_guards
), OP_EQ
, n_guards
);
1549 tt_int_op(pos
, OP_GE
, 0);
1550 tt_int_op(pos
, OP_LT
, n_guards
);
1551 bitarray_set(selected
, pos
);
1553 for (i
= 0; i
< n_guards
; ++i
) {
1554 const int should_be_set
= (i
!= excluded_idx
&&
1555 i
!= 3); // filtered out.
1556 tt_int_op(!!bitarray_is_set(selected
, i
), OP_EQ
, should_be_set
);
1558 bitarray_free(selected
);
1563 guard_selection_free(gs
);
1564 bitarray_free(selected
);
1568 test_entry_guard_sample_reachable_filtered_empty(void *arg
)
1571 guard_selection_t
*gs
= guard_selection_new("default", GS_TYPE_NORMAL
);
1572 /* What if we try to sample from a set of 0? */
1573 SMARTLIST_FOREACH(big_fake_net_nodes
, node_t
*, n
,
1574 n
->is_possible_guard
= 0);
1576 entry_guard_t
*g
= sample_reachable_filtered_entry_guards(gs
, NULL
, 0);
1577 tt_ptr_op(g
, OP_EQ
, NULL
);
1580 guard_selection_free(gs
);
1584 test_entry_guard_retry_unreachable(void *arg
)
1587 guard_selection_t
*gs
= guard_selection_new("default", GS_TYPE_NORMAL
);
1589 entry_guards_expand_sample(gs
);
1590 /* Let's say that we have two guards, and they're down.
1592 time_t start
= approx_time();
1593 entry_guard_t
*g1
= smartlist_get(gs
->sampled_entry_guards
, 0);
1594 entry_guard_t
*g2
= smartlist_get(gs
->sampled_entry_guards
, 1);
1595 entry_guard_t
*g3
= smartlist_get(gs
->sampled_entry_guards
, 2);
1596 g1
->is_reachable
= GUARD_REACHABLE_NO
;
1597 g2
->is_reachable
= GUARD_REACHABLE_NO
;
1599 g1
->failing_since
= g2
->failing_since
= start
;
1600 g1
->last_tried_to_connect
= g2
->last_tried_to_connect
= start
;
1602 /* Wait 5 minutes. Nothing will get retried. */
1603 update_approx_time(start
+ 5 * 60);
1604 entry_guard_consider_retry(g1
);
1605 entry_guard_consider_retry(g2
);
1606 entry_guard_consider_retry(g3
); // just to make sure this doesn't crash.
1607 tt_int_op(g1
->is_reachable
, OP_EQ
, GUARD_REACHABLE_NO
);
1608 tt_int_op(g2
->is_reachable
, OP_EQ
, GUARD_REACHABLE_NO
);
1609 tt_int_op(g3
->is_reachable
, OP_EQ
, GUARD_REACHABLE_MAYBE
);
1611 /* After 30 min, the primary one gets retried */
1612 update_approx_time(start
+ 35 * 60);
1613 entry_guard_consider_retry(g1
);
1614 entry_guard_consider_retry(g2
);
1615 tt_int_op(g1
->is_reachable
, OP_EQ
, GUARD_REACHABLE_MAYBE
);
1616 tt_int_op(g2
->is_reachable
, OP_EQ
, GUARD_REACHABLE_NO
);
1618 g1
->is_reachable
= GUARD_REACHABLE_NO
;
1619 g1
->last_tried_to_connect
= start
+ 55*60;
1621 /* After 1 hour, we'll retry the nonprimary one. */
1622 update_approx_time(start
+ 61 * 60);
1623 entry_guard_consider_retry(g1
);
1624 entry_guard_consider_retry(g2
);
1625 tt_int_op(g1
->is_reachable
, OP_EQ
, GUARD_REACHABLE_NO
);
1626 tt_int_op(g2
->is_reachable
, OP_EQ
, GUARD_REACHABLE_MAYBE
);
1628 g2
->is_reachable
= GUARD_REACHABLE_NO
;
1629 g2
->last_tried_to_connect
= start
+ 61*60;
1631 /* And then the primary one again. */
1632 update_approx_time(start
+ 66 * 60);
1633 entry_guard_consider_retry(g1
);
1634 entry_guard_consider_retry(g2
);
1635 tt_int_op(g1
->is_reachable
, OP_EQ
, GUARD_REACHABLE_MAYBE
);
1636 tt_int_op(g2
->is_reachable
, OP_EQ
, GUARD_REACHABLE_NO
);
1639 guard_selection_free(gs
);
1643 test_entry_guard_manage_primary(void *arg
)
1646 guard_selection_t
*gs
= guard_selection_new("default", GS_TYPE_NORMAL
);
1647 smartlist_t
*prev_guards
= smartlist_new();
1649 /* If no guards are confirmed, we should pick a few reachable guards and
1650 * call them all primary. But not confirmed.*/
1651 entry_guards_update_primary(gs
);
1652 int n_primary
= smartlist_len(gs
->primary_entry_guards
);
1653 tt_int_op(n_primary
, OP_GE
, 1);
1654 SMARTLIST_FOREACH(gs
->primary_entry_guards
, entry_guard_t
*, g
, {
1655 tt_assert(g
->is_primary
);
1656 tt_assert(g
->confirmed_idx
== -1);
1659 /* Calling it a second time should leave the guards unchanged. */
1660 smartlist_add_all(prev_guards
, gs
->primary_entry_guards
);
1661 entry_guards_update_primary(gs
);
1662 tt_int_op(smartlist_len(gs
->primary_entry_guards
), OP_EQ
, n_primary
);
1663 SMARTLIST_FOREACH(gs
->primary_entry_guards
, entry_guard_t
*, g
, {
1664 tt_ptr_op(g
, OP_EQ
, smartlist_get(prev_guards
, g_sl_idx
));
1667 /* If we have one confirmed guard, that guards becomes the first primary
1668 * guard, and the other primary guards get kept. */
1670 /* find a non-primary guard... */
1671 entry_guard_t
*confirmed
= NULL
;
1672 SMARTLIST_FOREACH(gs
->sampled_entry_guards
, entry_guard_t
*, g
, {
1673 if (! g
->is_primary
) {
1678 tt_assert(confirmed
);
1679 /* make it confirmed. */
1680 make_guard_confirmed(gs
, confirmed
);
1681 /* update the list... */
1682 smartlist_clear(prev_guards
);
1683 smartlist_add_all(prev_guards
, gs
->primary_entry_guards
);
1684 entry_guards_update_primary(gs
);
1686 /* and see what's primary now! */
1687 tt_int_op(smartlist_len(gs
->primary_entry_guards
), OP_EQ
, n_primary
);
1688 tt_ptr_op(smartlist_get(gs
->primary_entry_guards
, 0), OP_EQ
, confirmed
);
1689 SMARTLIST_FOREACH(gs
->primary_entry_guards
, entry_guard_t
*, g
, {
1690 tt_assert(g
->is_primary
);
1693 tt_ptr_op(g
, OP_EQ
, smartlist_get(prev_guards
, g_sl_idx
- 1));
1696 entry_guard_t
*prev_last_guard
= smartlist_get(prev_guards
, n_primary
-1);
1697 tt_assert(! prev_last_guard
->is_primary
);
1700 /* Calling it a fourth time should leave the guards unchanged. */
1701 smartlist_clear(prev_guards
);
1702 smartlist_add_all(prev_guards
, gs
->primary_entry_guards
);
1703 entry_guards_update_primary(gs
);
1704 tt_int_op(smartlist_len(gs
->primary_entry_guards
), OP_EQ
, n_primary
);
1705 SMARTLIST_FOREACH(gs
->primary_entry_guards
, entry_guard_t
*, g
, {
1706 tt_ptr_op(g
, OP_EQ
, smartlist_get(prev_guards
, g_sl_idx
));
1709 /* Do some dirinfo checks */
1711 /* Check that we have all required dirinfo for the primaries (that's done
1712 * in big_fake_network_setup()) */
1713 char *dir_info_str
=
1714 guard_selection_get_err_str_if_dir_info_missing(gs
, 0, 0, 0);
1715 tt_assert(!dir_info_str
);
1717 /* Now artificially remove the first primary's descriptor and re-check */
1718 entry_guard_t
*first_primary
;
1719 first_primary
= smartlist_get(gs
->primary_entry_guards
, 0);
1720 /* Change the first primary's identity digest so that the mocked functions
1721 * can't find its descriptor */
1722 memset(first_primary
->identity
, 9, sizeof(first_primary
->identity
));
1723 dir_info_str
=guard_selection_get_err_str_if_dir_info_missing(gs
, 1, 2, 3);
1724 tt_str_op(dir_info_str
, OP_EQ
,
1725 "We're missing descriptors for 1/2 of our primary entry guards "
1726 "(total microdescriptors: 2/3). That's ok. We will try to fetch "
1727 "missing descriptors soon.");
1728 tor_free(dir_info_str
);
1732 guard_selection_free(gs
);
1733 smartlist_free(prev_guards
);
1737 test_entry_guard_guard_preferred(void *arg
)
1740 entry_guard_t
*g1
= tor_malloc_zero(sizeof(entry_guard_t
));
1741 entry_guard_t
*g2
= tor_malloc_zero(sizeof(entry_guard_t
));
1743 g1
->confirmed_idx
= g2
->confirmed_idx
= -1;
1744 g1
->last_tried_to_connect
= approx_time();
1745 g2
->last_tried_to_connect
= approx_time();
1747 tt_int_op(0, OP_EQ
, entry_guard_has_higher_priority(g1
, g1
));
1749 /* Neither is pending; priorities equal. */
1750 tt_int_op(0, OP_EQ
, entry_guard_has_higher_priority(g2
, g1
));
1751 tt_int_op(0, OP_EQ
, entry_guard_has_higher_priority(g1
, g2
));
1753 /* If one is pending, the pending one has higher priority */
1755 tt_int_op(1, OP_EQ
, entry_guard_has_higher_priority(g1
, g2
));
1756 tt_int_op(0, OP_EQ
, entry_guard_has_higher_priority(g2
, g1
));
1758 /* If both are pending, and last_tried_to_connect is equal:
1761 tt_int_op(0, OP_EQ
, entry_guard_has_higher_priority(g2
, g1
));
1762 tt_int_op(0, OP_EQ
, entry_guard_has_higher_priority(g1
, g2
));
1764 /* One had a connection that startied earlier: it has higher priority. */
1765 g2
->last_tried_to_connect
-= 10;
1766 tt_int_op(1, OP_EQ
, entry_guard_has_higher_priority(g2
, g1
));
1767 tt_int_op(0, OP_EQ
, entry_guard_has_higher_priority(g1
, g2
));
1769 /* Now, say that g1 is confirmed. It will get higher priority. */
1770 g1
->confirmed_idx
= 5;
1771 tt_int_op(0, OP_EQ
, entry_guard_has_higher_priority(g2
, g1
));
1772 tt_int_op(1, OP_EQ
, entry_guard_has_higher_priority(g1
, g2
));
1774 /* But if g2 was confirmed first, it will get priority */
1775 g2
->confirmed_idx
= 2;
1776 tt_int_op(1, OP_EQ
, entry_guard_has_higher_priority(g2
, g1
));
1777 tt_int_op(0, OP_EQ
, entry_guard_has_higher_priority(g1
, g2
));
1785 test_entry_guard_select_for_circuit_no_confirmed(void *arg
)
1787 /* Simpler cases: no gaurds are confirmed yet. */
1789 guard_selection_t
*gs
= guard_selection_new("default", GS_TYPE_NORMAL
);
1790 entry_guard_restriction_t
*rst
= NULL
;
1792 /* simple starting configuration */
1793 entry_guards_update_primary(gs
);
1794 unsigned state
= 9999;
1796 entry_guard_t
*g
= select_entry_guard_for_circuit(gs
, GUARD_USAGE_TRAFFIC
,
1800 tt_assert(g
->is_primary
);
1801 tt_int_op(g
->confirmed_idx
, OP_EQ
, -1);
1802 tt_uint_op(g
->is_pending
, OP_EQ
, 0); // primary implies non-pending.
1803 tt_uint_op(state
, OP_EQ
, GUARD_CIRC_STATE_USABLE_ON_COMPLETION
);
1804 tt_i64_op(g
->last_tried_to_connect
, OP_EQ
, approx_time());
1806 // If we do that again, we should get the same guard.
1807 entry_guard_t
*g2
= select_entry_guard_for_circuit(gs
, GUARD_USAGE_TRAFFIC
,
1809 tt_ptr_op(g2
, OP_EQ
, g
);
1811 // if we mark that guard down, we should get a different primary guard.
1813 g
->is_reachable
= GUARD_REACHABLE_NO
;
1814 g
->failing_since
= approx_time() - 10;
1815 g
->last_tried_to_connect
= approx_time() - 10;
1817 g2
= select_entry_guard_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
, &state
);
1818 tt_ptr_op(g2
, OP_NE
, g
);
1820 tt_assert(g2
->is_primary
);
1821 tt_int_op(g2
->confirmed_idx
, OP_EQ
, -1);
1822 tt_uint_op(g2
->is_pending
, OP_EQ
, 0); // primary implies non-pending.
1823 tt_uint_op(state
, OP_EQ
, GUARD_CIRC_STATE_USABLE_ON_COMPLETION
);
1824 tt_i64_op(g2
->last_tried_to_connect
, OP_EQ
, approx_time());
1826 // If we say that the first primary guard was last tried a long time ago, we
1827 // should get an automatic retry on it.
1828 g
->failing_since
= approx_time() - 72*60*60;
1829 g
->last_tried_to_connect
= approx_time() - 72*60*60;
1831 g2
= select_entry_guard_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
, &state
);
1832 tt_ptr_op(g2
, OP_EQ
, g
);
1834 tt_uint_op(state
, OP_EQ
, GUARD_CIRC_STATE_USABLE_ON_COMPLETION
);
1835 tt_i64_op(g2
->last_tried_to_connect
, OP_EQ
, approx_time());
1836 tt_int_op(g2
->is_reachable
, OP_EQ
, GUARD_REACHABLE_MAYBE
);
1838 // And if we mark ALL the primary guards down, we should get another guard
1840 SMARTLIST_FOREACH(gs
->primary_entry_guards
, entry_guard_t
*, guard
, {
1841 guard
->is_reachable
= GUARD_REACHABLE_NO
;
1842 guard
->last_tried_to_connect
= approx_time() - 5;
1843 guard
->failing_since
= approx_time() - 30;
1846 g2
= select_entry_guard_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
, &state
);
1848 tt_assert(!g2
->is_primary
);
1849 tt_int_op(g2
->confirmed_idx
, OP_EQ
, -1);
1850 tt_uint_op(g2
->is_pending
, OP_EQ
, 1);
1851 tt_uint_op(state
, OP_EQ
, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD
);
1852 tt_i64_op(g2
->last_tried_to_connect
, OP_EQ
, approx_time());
1853 tt_int_op(g2
->is_reachable
, OP_EQ
, GUARD_REACHABLE_MAYBE
);
1855 // As a bonus, maybe we should be retrying the primary guards. Let's say so.
1856 mark_primary_guards_maybe_reachable(gs
);
1857 SMARTLIST_FOREACH(gs
->primary_entry_guards
, entry_guard_t
*, guard
, {
1858 tt_int_op(guard
->is_reachable
, OP_EQ
, GUARD_REACHABLE_MAYBE
);
1859 tt_assert(guard
->is_usable_filtered_guard
== 1);
1860 // no change to these fields.
1861 tt_i64_op(guard
->last_tried_to_connect
, OP_EQ
, approx_time() - 5);
1862 tt_i64_op(guard
->failing_since
, OP_EQ
, approx_time() - 30);
1865 /* Let's try again and we should get the first primary guard again */
1866 g
= select_entry_guard_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
, &state
);
1867 tt_ptr_op(g
, OP_EQ
, smartlist_get(gs
->primary_entry_guards
, 0));
1868 g2
= select_entry_guard_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
, &state
);
1869 tt_ptr_op(g2
, OP_EQ
, g
);
1871 /* But if we impose a restriction, we don't get the same guard */
1872 rst
= guard_create_exit_restriction((uint8_t*)g
->identity
);
1873 g2
= select_entry_guard_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, rst
, &state
);
1874 tt_ptr_op(g2
, OP_NE
, g
);
1877 guard_selection_free(gs
);
1878 entry_guard_restriction_free(rst
);
1882 test_entry_guard_select_for_circuit_confirmed(void *arg
)
1884 /* Case 2: if all the primary guards are down, and there are more confirmed
1885 guards, we use a confirmed guard. */
1888 entry_guard_restriction_t
*rst
= NULL
;
1889 guard_selection_t
*gs
= guard_selection_new("default", GS_TYPE_NORMAL
);
1890 const int N_CONFIRMED
= 10;
1892 /* slightly more complicated simple starting configuration */
1893 entry_guards_update_primary(gs
);
1894 for (i
= 0; i
< N_CONFIRMED
; ++i
) {
1895 entry_guard_t
*guard
= smartlist_get(gs
->sampled_entry_guards
, i
);
1896 make_guard_confirmed(gs
, guard
);
1898 entry_guards_update_primary(gs
); // rebuild the primary list.
1900 unsigned state
= 9999;
1902 // As above, this gives us a primary guard.
1903 entry_guard_t
*g
= select_entry_guard_for_circuit(gs
, GUARD_USAGE_TRAFFIC
,
1906 tt_assert(g
->is_primary
);
1907 tt_int_op(g
->confirmed_idx
, OP_EQ
, 0);
1908 tt_uint_op(g
->is_pending
, OP_EQ
, 0); // primary implies non-pending.
1909 tt_uint_op(state
, OP_EQ
, GUARD_CIRC_STATE_USABLE_ON_COMPLETION
);
1910 tt_i64_op(g
->last_tried_to_connect
, OP_EQ
, approx_time());
1911 tt_ptr_op(g
, OP_EQ
, smartlist_get(gs
->primary_entry_guards
, 0));
1913 // But if we mark all the primary guards down...
1914 SMARTLIST_FOREACH(gs
->primary_entry_guards
, entry_guard_t
*, guard
, {
1915 guard
->last_tried_to_connect
= approx_time();
1916 entry_guards_note_guard_failure(gs
, guard
);
1919 // ... we should get a confirmed guard.
1921 g
= select_entry_guard_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
, &state
);
1923 tt_assert(! g
->is_primary
);
1924 tt_int_op(g
->confirmed_idx
, OP_EQ
, smartlist_len(gs
->primary_entry_guards
));
1925 tt_assert(g
->is_pending
);
1926 tt_uint_op(state
, OP_EQ
, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD
);
1927 tt_i64_op(g
->last_tried_to_connect
, OP_EQ
, approx_time());
1929 // And if we try again, we should get a different confirmed guard, since
1930 // that one is pending.
1932 entry_guard_t
*g2
= select_entry_guard_for_circuit(gs
, GUARD_USAGE_TRAFFIC
,
1935 tt_assert(! g2
->is_primary
);
1936 tt_ptr_op(g2
, OP_NE
, g
);
1937 tt_int_op(g2
->confirmed_idx
, OP_EQ
,
1938 smartlist_len(gs
->primary_entry_guards
)+1);
1939 tt_assert(g2
->is_pending
);
1940 tt_uint_op(state
, OP_EQ
, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD
);
1941 tt_i64_op(g2
->last_tried_to_connect
, OP_EQ
, approx_time());
1943 // If we say that the next confirmed guard in order is excluded, and
1944 // we disable EnforceDistinctSubnets, we get the guard AFTER the
1946 get_options_mutable()->EnforceDistinctSubnets
= 0;
1947 g
= smartlist_get(gs
->confirmed_entry_guards
,
1948 smartlist_len(gs
->primary_entry_guards
)+2);
1949 rst
= guard_create_exit_restriction((uint8_t*)g
->identity
);
1950 g2
= select_entry_guard_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, rst
, &state
);
1951 tt_ptr_op(g2
, OP_NE
, NULL
);
1952 tt_ptr_op(g2
, OP_NE
, g
);
1953 tt_int_op(g2
->confirmed_idx
, OP_EQ
,
1954 smartlist_len(gs
->primary_entry_guards
)+3);
1956 // If we make every confirmed guard become pending then we start poking
1958 const int n_remaining_confirmed
=
1959 N_CONFIRMED
- 3 - smartlist_len(gs
->primary_entry_guards
);
1960 for (i
= 0; i
< n_remaining_confirmed
; ++i
) {
1961 g
= select_entry_guard_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
, &state
);
1962 tt_int_op(g
->confirmed_idx
, OP_GE
, 0);
1966 g
= select_entry_guard_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
, &state
);
1968 tt_assert(g
->is_pending
);
1969 tt_int_op(g
->confirmed_idx
, OP_EQ
, -1);
1971 // If we EnforceDistinctSubnets and apply a restriction, we get
1972 // nothing, since we put all of the nodes in the same /16.
1973 // Regression test for bug 22753/TROVE-2017-006.
1974 get_options_mutable()->EnforceDistinctSubnets
= 1;
1975 g
= smartlist_get(gs
->confirmed_entry_guards
, 0);
1976 memcpy(rst
->exclude_id
, g
->identity
, DIGEST_LEN
);
1977 g2
= select_entry_guard_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, rst
, &state
);
1978 tt_ptr_op(g2
, OP_EQ
, NULL
);
1981 guard_selection_free(gs
);
1982 entry_guard_restriction_free(rst
);
1986 test_entry_guard_select_for_circuit_highlevel_primary(void *arg
)
1988 /* Play around with selecting primary guards for circuits and markign
1989 * them up and down */
1991 guard_selection_t
*gs
= guard_selection_new("default", GS_TYPE_NORMAL
);
1993 time_t start
= approx_time();
1995 const node_t
*node
= NULL
;
1996 circuit_guard_state_t
*guard
= NULL
;
2000 * Make sure that the pick-for-circuit API basically works. We'll get
2001 * a primary guard, so it'll be usable on completion.
2003 int r
= entry_guard_pick_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
,
2006 tt_int_op(r
, OP_EQ
, 0);
2009 tt_int_op(guard
->state
, OP_EQ
, GUARD_CIRC_STATE_USABLE_ON_COMPLETION
);
2010 g
= entry_guard_handle_get(guard
->guard
);
2012 tt_mem_op(g
->identity
, OP_EQ
, node
->identity
, DIGEST_LEN
);
2013 tt_int_op(g
->is_primary
, OP_EQ
, 1);
2014 tt_i64_op(g
->last_tried_to_connect
, OP_EQ
, start
);
2015 tt_int_op(g
->confirmed_idx
, OP_EQ
, -1);
2017 /* Call that circuit successful. */
2018 update_approx_time(start
+15);
2019 u
= entry_guard_succeeded(&guard
);
2020 tt_int_op(u
, OP_EQ
, GUARD_USABLE_NOW
); /* We can use it now. */
2022 tt_int_op(guard
->state
, OP_EQ
, GUARD_CIRC_STATE_COMPLETE
);
2023 g
= entry_guard_handle_get(guard
->guard
);
2025 tt_int_op(g
->is_reachable
, OP_EQ
, GUARD_REACHABLE_YES
);
2026 tt_int_op(g
->confirmed_idx
, OP_EQ
, 0);
2028 circuit_guard_state_free(guard
);
2033 /* Try again. We'll also get a primary guard this time. (The same one,
2034 in fact.) But this time, we'll say the connection has failed. */
2035 update_approx_time(start
+35);
2036 r
= entry_guard_pick_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
,
2038 tt_int_op(r
, OP_EQ
, 0);
2041 tt_int_op(guard
->state
, OP_EQ
, GUARD_CIRC_STATE_USABLE_ON_COMPLETION
);
2042 tt_i64_op(guard
->state_set_at
, OP_EQ
, start
+35);
2043 g
= entry_guard_handle_get(guard
->guard
);
2045 tt_mem_op(g
->identity
, OP_EQ
, node
->identity
, DIGEST_LEN
);
2046 tt_int_op(g
->is_primary
, OP_EQ
, 1);
2047 tt_i64_op(g
->last_tried_to_connect
, OP_EQ
, start
+35);
2048 tt_int_op(g
->confirmed_idx
, OP_EQ
, 0); // same one.
2050 /* It's failed! What will happen to our poor guard? */
2051 update_approx_time(start
+45);
2052 entry_guard_failed(&guard
);
2054 tt_int_op(guard
->state
, OP_EQ
, GUARD_CIRC_STATE_DEAD
);
2055 tt_i64_op(guard
->state_set_at
, OP_EQ
, start
+45);
2056 g
= entry_guard_handle_get(guard
->guard
);
2058 tt_int_op(g
->is_reachable
, OP_EQ
, GUARD_REACHABLE_NO
);
2059 tt_i64_op(g
->failing_since
, OP_EQ
, start
+45);
2060 tt_int_op(g
->confirmed_idx
, OP_EQ
, 0); // still confirmed.
2062 circuit_guard_state_free(guard
);
2065 entry_guard_t
*g_prev
= g
;
2068 /* Now try a third time. Since the other one is down, we'll get a different
2069 * (still primary) guard.
2071 update_approx_time(start
+60);
2072 r
= entry_guard_pick_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
,
2074 tt_int_op(r
, OP_EQ
, 0);
2077 tt_int_op(guard
->state
, OP_EQ
, GUARD_CIRC_STATE_USABLE_ON_COMPLETION
);
2078 g
= entry_guard_handle_get(guard
->guard
);
2080 tt_ptr_op(g
, OP_NE
, g_prev
);
2081 tt_mem_op(g
->identity
, OP_EQ
, node
->identity
, DIGEST_LEN
);
2082 tt_mem_op(g
->identity
, OP_NE
, g_prev
->identity
, DIGEST_LEN
);
2083 tt_int_op(g
->is_primary
, OP_EQ
, 1);
2084 tt_i64_op(g
->last_tried_to_connect
, OP_EQ
, start
+60);
2085 tt_int_op(g
->confirmed_idx
, OP_EQ
, -1); // not confirmed now.
2087 /* Call this one up; watch it get confirmed. */
2088 update_approx_time(start
+90);
2089 u
= entry_guard_succeeded(&guard
);
2090 tt_int_op(u
, OP_EQ
, GUARD_USABLE_NOW
);
2092 tt_int_op(guard
->state
, OP_EQ
, GUARD_CIRC_STATE_COMPLETE
);
2093 g
= entry_guard_handle_get(guard
->guard
);
2095 tt_int_op(g
->is_reachable
, OP_EQ
, GUARD_REACHABLE_YES
);
2096 tt_int_op(g
->confirmed_idx
, OP_EQ
, 1);
2099 guard_selection_free(gs
);
2100 circuit_guard_state_free(guard
);
2104 test_entry_guard_select_for_circuit_highlevel_confirm_other(void *arg
)
2107 const int N_PRIMARY
= DFLT_N_PRIMARY_GUARDS
;
2109 /* At the start, we have no confirmed guards. We'll mark the primary guards
2110 * down, then confirm something else. As soon as we do, it should become
2111 * primary, and we should get it next time. */
2113 time_t start
= approx_time();
2114 guard_selection_t
*gs
= guard_selection_new("default", GS_TYPE_NORMAL
);
2115 circuit_guard_state_t
*guard
= NULL
;
2117 const node_t
*node
= NULL
;
2120 /* Declare that we're on the internet. */
2121 entry_guards_note_internet_connectivity(gs
);
2123 /* Primary guards are down! */
2124 for (i
= 0; i
< N_PRIMARY
; ++i
) {
2125 r
= entry_guard_pick_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
,
2129 tt_int_op(r
, OP_EQ
, 0);
2130 tt_int_op(guard
->state
, OP_EQ
, GUARD_CIRC_STATE_USABLE_ON_COMPLETION
);
2131 entry_guard_failed(&guard
);
2132 circuit_guard_state_free(guard
);
2137 /* Next guard should be non-primary. */
2139 r
= entry_guard_pick_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
,
2143 tt_int_op(r
, OP_EQ
, 0);
2144 entry_guard_t
*g
= entry_guard_handle_get(guard
->guard
);
2146 tt_int_op(guard
->state
, OP_EQ
, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD
);
2147 tt_int_op(g
->confirmed_idx
, OP_EQ
, -1);
2148 tt_int_op(g
->is_primary
, OP_EQ
, 0);
2149 tt_int_op(g
->is_pending
, OP_EQ
, 1);
2152 u
= entry_guard_succeeded(&guard
);
2153 /* We're on the internet (by fiat), so this guard will get called "confirmed"
2154 * and should immediately become primary.
2156 tt_int_op(guard
->state
, OP_EQ
, GUARD_CIRC_STATE_COMPLETE
);
2157 tt_assert(u
== GUARD_USABLE_NOW
);
2158 tt_int_op(g
->confirmed_idx
, OP_EQ
, 0);
2159 tt_int_op(g
->is_primary
, OP_EQ
, 1);
2160 tt_int_op(g
->is_pending
, OP_EQ
, 0);
2163 guard_selection_free(gs
);
2164 circuit_guard_state_free(guard
);
2168 test_entry_guard_select_for_circuit_highlevel_primary_retry(void *arg
)
2171 const int N_PRIMARY
= DFLT_N_PRIMARY_GUARDS
;
2173 /* At the start, we have no confirmed guards. We'll mark the primary guards
2174 * down, then confirm something else. As soon as we do, it should become
2175 * primary, and we should get it next time. */
2177 time_t start
= approx_time();
2178 guard_selection_t
*gs
= guard_selection_new("default", GS_TYPE_NORMAL
);
2179 circuit_guard_state_t
*guard
= NULL
, *guard2
= NULL
;
2181 const node_t
*node
= NULL
;
2185 /* Declare that we're on the internet. */
2186 entry_guards_note_internet_connectivity(gs
);
2188 /* Make primary guards confirmed (so they won't be superseded by a later
2189 * guard), then mark them down. */
2190 for (i
= 0; i
< N_PRIMARY
; ++i
) {
2191 r
= entry_guard_pick_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
,
2195 tt_int_op(r
, OP_EQ
, 0);
2196 tt_int_op(guard
->state
, OP_EQ
, GUARD_CIRC_STATE_USABLE_ON_COMPLETION
);
2197 g
= entry_guard_handle_get(guard
->guard
);
2198 make_guard_confirmed(gs
, g
);
2199 tt_int_op(g
->is_primary
, OP_EQ
, 1);
2200 entry_guard_failed(&guard
);
2201 circuit_guard_state_free(guard
);
2202 tt_int_op(g
->is_reachable
, OP_EQ
, GUARD_REACHABLE_NO
);
2207 /* Get another guard that we might try. */
2208 r
= entry_guard_pick_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
,
2212 tt_int_op(r
, OP_EQ
, 0);
2213 tt_int_op(guard
->state
, OP_EQ
, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD
);
2214 g
= entry_guard_handle_get(guard
->guard
);
2215 tt_int_op(g
->is_primary
, OP_EQ
, 0);
2217 tt_assert(entry_guards_all_primary_guards_are_down(gs
));
2219 /* And an hour has passed ... */
2220 update_approx_time(start
+ 3600);
2222 /* Say that guard has succeeded! */
2223 u
= entry_guard_succeeded(&guard
);
2224 tt_int_op(u
, OP_EQ
, GUARD_MAYBE_USABLE_LATER
);
2225 tt_int_op(guard
->state
, OP_EQ
, GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD
);
2226 g
= entry_guard_handle_get(guard
->guard
);
2228 /* The primary guards should have been marked up! */
2229 SMARTLIST_FOREACH(gs
->primary_entry_guards
, entry_guard_t
*, pg
, {
2230 tt_int_op(pg
->is_primary
, OP_EQ
, 1);
2231 tt_ptr_op(g
, OP_NE
, pg
);
2232 tt_int_op(pg
->is_reachable
, OP_EQ
, GUARD_REACHABLE_MAYBE
);
2235 /* Have a circuit to a primary guard succeed. */
2236 r
= entry_guard_pick_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
,
2238 tt_int_op(r
, OP_EQ
, 0);
2239 tt_int_op(guard2
->state
, OP_EQ
, GUARD_CIRC_STATE_USABLE_ON_COMPLETION
);
2240 u
= entry_guard_succeeded(&guard2
);
2241 tt_assert(u
== GUARD_USABLE_NOW
);
2242 tt_int_op(guard2
->state
, OP_EQ
, GUARD_CIRC_STATE_COMPLETE
);
2244 tt_assert(! entry_guards_all_primary_guards_are_down(gs
));
2247 guard_selection_free(gs
);
2248 circuit_guard_state_free(guard
);
2249 circuit_guard_state_free(guard2
);
2253 test_entry_guard_select_and_cancel(void *arg
)
2256 const int N_PRIMARY
= DFLT_N_PRIMARY_GUARDS
;
2258 const node_t
*node
= NULL
;
2259 circuit_guard_state_t
*guard
;
2260 guard_selection_t
*gs
= guard_selection_new("default", GS_TYPE_NORMAL
);
2263 /* Once more, we mark all the primary guards down. */
2264 entry_guards_note_internet_connectivity(gs
);
2265 for (i
= 0; i
< N_PRIMARY
; ++i
) {
2266 r
= entry_guard_pick_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
,
2268 tt_int_op(r
, OP_EQ
, 0);
2269 tt_int_op(guard
->state
, OP_EQ
, GUARD_CIRC_STATE_USABLE_ON_COMPLETION
);
2270 g
= entry_guard_handle_get(guard
->guard
);
2271 tt_int_op(g
->is_primary
, OP_EQ
, 1);
2272 tt_int_op(g
->is_pending
, OP_EQ
, 0);
2273 make_guard_confirmed(gs
, g
);
2274 entry_guard_failed(&guard
);
2275 circuit_guard_state_free(guard
);
2280 tt_assert(entry_guards_all_primary_guards_are_down(gs
));
2282 /* Now get another guard we could try... */
2283 r
= entry_guard_pick_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
,
2287 tt_int_op(r
, OP_EQ
, 0);
2288 tt_int_op(guard
->state
, OP_EQ
, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD
);
2289 g
= entry_guard_handle_get(guard
->guard
);
2290 tt_int_op(g
->is_primary
, OP_EQ
, 0);
2291 tt_int_op(g
->is_pending
, OP_EQ
, 1);
2293 /* Whoops! We should never have asked for this guard. Cancel the request! */
2294 entry_guard_cancel(&guard
);
2295 tt_ptr_op(guard
, OP_EQ
, NULL
);
2296 tt_int_op(g
->is_primary
, OP_EQ
, 0);
2297 tt_int_op(g
->is_pending
, OP_EQ
, 0);
2300 guard_selection_free(gs
);
2301 circuit_guard_state_free(guard
);
2305 test_entry_guard_drop_guards(void *arg
)
2309 const node_t
*node
= NULL
;
2310 circuit_guard_state_t
*guard
;
2311 guard_selection_t
*gs
= get_guard_selection_info();
2313 // Pick a guard, to get things set up.
2314 r
= entry_guard_pick_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
,
2316 tt_int_op(r
, OP_EQ
, 0);
2317 tt_int_op(smartlist_len(gs
->sampled_entry_guards
), OP_GE
,
2318 DFLT_MIN_FILTERED_SAMPLE_SIZE
);
2319 tt_ptr_op(gs
, OP_EQ
, get_guard_selection_info());
2321 // Drop all the guards! (This is a bad idea....)
2322 remove_all_entry_guards_for_guard_selection(gs
);
2323 gs
= get_guard_selection_info();
2324 tt_int_op(smartlist_len(gs
->sampled_entry_guards
), OP_EQ
, 0);
2325 tt_int_op(smartlist_len(gs
->primary_entry_guards
), OP_EQ
, 0);
2326 tt_int_op(smartlist_len(gs
->confirmed_entry_guards
), OP_EQ
, 0);
2329 circuit_guard_state_free(guard
);
2330 guard_selection_free(gs
);
2333 /* Unit test setup function: Create a fake network, and set everything up
2334 * for testing the upgrade-a-waiting-circuit code. */
2336 guard_selection_t
*gs
;
2338 circuit_guard_state_t
*guard1_state
;
2339 circuit_guard_state_t
*guard2_state
;
2340 entry_guard_t
*guard1
;
2341 entry_guard_t
*guard2
;
2342 origin_circuit_t
*circ1
;
2343 origin_circuit_t
*circ2
;
2344 smartlist_t
*all_origin_circuits
;
2345 } upgrade_circuits_data_t
;
2347 upgrade_circuits_setup(const struct testcase_t
*testcase
)
2349 upgrade_circuits_data_t
*data
= tor_malloc_zero(sizeof(*data
));
2350 guard_selection_t
*gs
= data
->gs
=
2351 guard_selection_new("default", GS_TYPE_NORMAL
);
2352 circuit_guard_state_t
*guard
;
2356 const int N_PRIMARY
= DFLT_N_PRIMARY_GUARDS
;
2357 const char *argument
= testcase
->setup_data
;
2358 const int make_circ1_succeed
= strstr(argument
, "c1-done") != NULL
;
2359 const int make_circ2_succeed
= strstr(argument
, "c2-done") != NULL
;
2361 big_fake_network_setup(testcase
);
2363 /* We're going to set things up in a state where a circuit will be ready to
2364 * be upgraded. Each test can make a single change (or not) that should
2365 * block the upgrade.
2368 /* First, make all the primary guards confirmed, and down. */
2369 data
->start
= approx_time();
2370 entry_guards_note_internet_connectivity(gs
);
2371 for (i
= 0; i
< N_PRIMARY
; ++i
) {
2372 entry_guard_pick_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
, &node
, &guard
);
2373 g
= entry_guard_handle_get(guard
->guard
);
2374 make_guard_confirmed(gs
, g
);
2375 entry_guard_failed(&guard
);
2376 circuit_guard_state_free(guard
);
2379 /* Grab another couple of guards */
2380 data
->all_origin_circuits
= smartlist_new();
2382 update_approx_time(data
->start
+ 27);
2383 entry_guard_pick_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
,
2384 &node
, &data
->guard1_state
);
2385 origin_circuit_t
*circ
;
2386 data
->circ1
= circ
= origin_circuit_new();
2387 circ
->base_
.purpose
= CIRCUIT_PURPOSE_C_GENERAL
;
2388 circ
->guard_state
= data
->guard1_state
;
2389 smartlist_add(data
->all_origin_circuits
, circ
);
2391 update_approx_time(data
->start
+ 30);
2392 entry_guard_pick_for_circuit(gs
, GUARD_USAGE_TRAFFIC
, NULL
,
2393 &node
, &data
->guard2_state
);
2394 data
->circ2
= circ
= origin_circuit_new();
2395 circ
->base_
.purpose
= CIRCUIT_PURPOSE_C_GENERAL
;
2396 circ
->guard_state
= data
->guard2_state
;
2397 smartlist_add(data
->all_origin_circuits
, circ
);
2399 data
->guard1
= entry_guard_handle_get(data
->guard1_state
->guard
);
2400 data
->guard2
= entry_guard_handle_get(data
->guard2_state
->guard
);
2401 tor_assert(data
->guard1
!= data
->guard2
);
2402 tor_assert(data
->guard1_state
->state
==
2403 GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD
);
2404 tor_assert(data
->guard2_state
->state
==
2405 GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD
);
2408 update_approx_time(data
->start
+ 32);
2409 if (make_circ1_succeed
) {
2410 r
= entry_guard_succeeded(&data
->guard1_state
);
2411 tor_assert(r
== GUARD_MAYBE_USABLE_LATER
);
2412 tor_assert(data
->guard1_state
->state
==
2413 GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD
);
2415 update_approx_time(data
->start
+ 33);
2416 if (make_circ2_succeed
) {
2417 r
= entry_guard_succeeded(&data
->guard2_state
);
2418 tor_assert(r
== GUARD_MAYBE_USABLE_LATER
);
2419 tor_assert(data
->guard2_state
->state
==
2420 GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD
);
2426 upgrade_circuits_cleanup(const struct testcase_t
*testcase
, void *ptr
)
2428 upgrade_circuits_data_t
*data
= ptr
;
2429 // circuit_guard_state_free(data->guard1_state); // held in circ1
2430 // circuit_guard_state_free(data->guard2_state); // held in circ2
2431 guard_selection_free(data
->gs
);
2432 smartlist_free(data
->all_origin_circuits
);
2433 circuit_free_(TO_CIRCUIT(data
->circ1
));
2434 circuit_free_(TO_CIRCUIT(data
->circ2
));
2436 return big_fake_network_cleanup(testcase
, NULL
);
2440 test_entry_guard_upgrade_a_circuit(void *arg
)
2442 upgrade_circuits_data_t
*data
= arg
;
2444 /* This is the easy case: we have no COMPLETED circuits, all the
2445 * primary guards are down, we have two WAITING circuits: one will
2446 * get upgraded to COMPLETED! (The one that started first.)
2449 smartlist_t
*result
= smartlist_new();
2451 r
= entry_guards_upgrade_waiting_circuits(data
->gs
,
2452 data
->all_origin_circuits
,
2454 tt_int_op(r
, OP_EQ
, 1);
2455 tt_int_op(smartlist_len(result
), OP_EQ
, 1);
2456 origin_circuit_t
*oc
= smartlist_get(result
, 0);
2458 /* circ1 was started first, so we'll get told to ugrade it... */
2459 tt_ptr_op(oc
, OP_EQ
, data
->circ1
);
2461 /* And the guard state should be complete */
2462 tt_ptr_op(data
->guard1_state
, OP_NE
, NULL
);
2463 tt_int_op(data
->guard1_state
->state
, OP_EQ
, GUARD_CIRC_STATE_COMPLETE
);
2466 smartlist_free(result
);
2470 test_entry_guard_upgrade_blocked_by_live_primary_guards(void *arg
)
2472 upgrade_circuits_data_t
*data
= arg
;
2474 /* If any primary guards might be up, we can't upgrade any waiting
2477 mark_primary_guards_maybe_reachable(data
->gs
);
2479 smartlist_t
*result
= smartlist_new();
2481 setup_capture_of_logs(LOG_DEBUG
);
2482 r
= entry_guards_upgrade_waiting_circuits(data
->gs
,
2483 data
->all_origin_circuits
,
2485 tt_int_op(r
, OP_EQ
, 0);
2486 tt_int_op(smartlist_len(result
), OP_EQ
, 0);
2487 expect_log_msg_containing("not all primary guards were definitely down.");
2490 teardown_capture_of_logs();
2491 smartlist_free(result
);
2495 test_entry_guard_upgrade_blocked_by_lack_of_waiting_circuits(void *arg
)
2497 upgrade_circuits_data_t
*data
= arg
;
2499 /* If no circuits are waiting, we can't upgrade anything. (The test
2500 * setup in this case was told not to make any of the circuits "waiting".)
2502 smartlist_t
*result
= smartlist_new();
2504 setup_capture_of_logs(LOG_DEBUG
);
2505 r
= entry_guards_upgrade_waiting_circuits(data
->gs
,
2506 data
->all_origin_circuits
,
2508 tt_int_op(r
, OP_EQ
, 0);
2509 tt_int_op(smartlist_len(result
), OP_EQ
, 0);
2510 expect_log_msg_containing("Considered upgrading guard-stalled circuits, "
2511 "but didn't find any.");
2514 teardown_capture_of_logs();
2515 smartlist_free(result
);
2519 test_entry_guard_upgrade_blocked_by_better_circ_complete(void *arg
)
2521 upgrade_circuits_data_t
*data
= arg
;
2523 /* We'll run through the logic of upgrade_a_circuit below...
2524 * and then try again to make sure that circ2 isn't also upgraded.
2527 smartlist_t
*result
= smartlist_new();
2529 r
= entry_guards_upgrade_waiting_circuits(data
->gs
,
2530 data
->all_origin_circuits
,
2532 tt_int_op(r
, OP_EQ
, 1);
2533 tt_int_op(smartlist_len(result
), OP_EQ
, 1);
2534 origin_circuit_t
*oc
= smartlist_get(result
, 0);
2535 tt_ptr_op(oc
, OP_EQ
, data
->circ1
);
2536 tt_ptr_op(data
->guard1_state
, OP_NE
, NULL
);
2537 tt_int_op(data
->guard1_state
->state
, OP_EQ
, GUARD_CIRC_STATE_COMPLETE
);
2539 /* Now, try again. Make sure that circ2 isn't upgraded. */
2540 smartlist_clear(result
);
2541 setup_capture_of_logs(LOG_DEBUG
);
2542 r
= entry_guards_upgrade_waiting_circuits(data
->gs
,
2543 data
->all_origin_circuits
,
2545 tt_int_op(r
, OP_EQ
, 0);
2546 tt_int_op(smartlist_len(result
), OP_EQ
, 0);
2547 expect_log_msg_containing("At least one complete circuit had higher "
2548 "priority, so not upgrading.");
2551 teardown_capture_of_logs();
2552 smartlist_free(result
);
2556 test_entry_guard_upgrade_not_blocked_by_restricted_circ_complete(void *arg
)
2558 upgrade_circuits_data_t
*data
= arg
;
2560 /* Once more, let circ1 become complete. But this time, we'll claim
2561 * that circ2 was restricted to not use the same guard as circ1. */
2562 data
->guard2_state
->restrictions
=
2563 guard_create_exit_restriction((uint8_t*)data
->guard1
->identity
);
2565 smartlist_t
*result
= smartlist_new();
2567 r
= entry_guards_upgrade_waiting_circuits(data
->gs
,
2568 data
->all_origin_circuits
,
2570 tt_int_op(r
, OP_EQ
, 1);
2571 tt_int_op(smartlist_len(result
), OP_EQ
, 1);
2572 origin_circuit_t
*oc
= smartlist_get(result
, 0);
2573 tt_ptr_op(oc
, OP_EQ
, data
->circ1
);
2574 tt_ptr_op(data
->guard1_state
, OP_NE
, NULL
);
2575 tt_int_op(data
->guard1_state
->state
, OP_EQ
, GUARD_CIRC_STATE_COMPLETE
);
2577 /* Now, we try again. Since circ2 has a restriction that circ1 doesn't obey,
2578 * circ2 _is_ eligible for upgrade. */
2579 smartlist_clear(result
);
2580 r
= entry_guards_upgrade_waiting_circuits(data
->gs
,
2581 data
->all_origin_circuits
,
2583 tt_int_op(r
, OP_EQ
, 1);
2584 tt_int_op(smartlist_len(result
), OP_EQ
, 1);
2585 origin_circuit_t
*oc2
= smartlist_get(result
, 0);
2586 tt_ptr_op(oc2
, OP_EQ
, data
->circ2
);
2589 smartlist_free(result
);
2593 test_entry_guard_upgrade_not_blocked_by_worse_circ_complete(void *arg
)
2595 upgrade_circuits_data_t
*data
= arg
;
2596 smartlist_t
*result
= smartlist_new();
2597 /* here we manually make circ2 COMPLETE, and make sure that circ1
2598 * gets made complete anyway, since guard1 has higher priority
2600 update_approx_time(data
->start
+ 300);
2601 data
->guard2_state
->state
= GUARD_CIRC_STATE_COMPLETE
;
2602 data
->guard2_state
->state_set_at
= approx_time();
2603 update_approx_time(data
->start
+ 301);
2605 /* Now, try again. Make sure that circ1 is approved. */
2607 r
= entry_guards_upgrade_waiting_circuits(data
->gs
,
2608 data
->all_origin_circuits
,
2610 tt_int_op(r
, OP_EQ
, 1);
2611 tt_int_op(smartlist_len(result
), OP_EQ
, 1);
2612 origin_circuit_t
*oc
= smartlist_get(result
, 0);
2613 tt_ptr_op(oc
, OP_EQ
, data
->circ1
);
2616 smartlist_free(result
);
2620 test_entry_guard_upgrade_blocked_by_better_circ_pending(void *arg
)
2622 upgrade_circuits_data_t
*data
= arg
;
2624 /* circ2 is done, but circ1 is still pending. Since circ1 is better,
2625 * we won't upgrade circ2. */
2627 /* XXXX Prop271 -- this is a kludge. I'm making sure circ1 _is_ better,
2628 * by messing with the guards' confirmed_idx */
2629 make_guard_confirmed(data
->gs
, data
->guard1
);
2632 tmp
= data
->guard1
->confirmed_idx
;
2633 data
->guard1
->confirmed_idx
= data
->guard2
->confirmed_idx
;
2634 data
->guard2
->confirmed_idx
= tmp
;
2637 smartlist_t
*result
= smartlist_new();
2638 setup_capture_of_logs(LOG_DEBUG
);
2640 r
= entry_guards_upgrade_waiting_circuits(data
->gs
,
2641 data
->all_origin_circuits
,
2643 tt_int_op(r
, OP_EQ
, 0);
2644 tt_int_op(smartlist_len(result
), OP_EQ
, 0);
2645 expect_log_msg_containing("but 1 pending circuit(s) had higher guard "
2646 "priority, so not upgrading.");
2649 teardown_capture_of_logs();
2650 smartlist_free(result
);
2654 test_entry_guard_upgrade_not_blocked_by_restricted_circ_pending(void *arg
)
2656 upgrade_circuits_data_t
*data
= arg
;
2657 /* circ2 is done, but circ1 is still pending. But when there is a
2658 restriction on circ2 that circ1 can't satisfy, circ1 can't block
2661 /* XXXX Prop271 -- this is a kludge. I'm making sure circ1 _is_ better,
2662 * by messing with the guards' confirmed_idx */
2663 make_guard_confirmed(data
->gs
, data
->guard1
);
2666 tmp
= data
->guard1
->confirmed_idx
;
2667 data
->guard1
->confirmed_idx
= data
->guard2
->confirmed_idx
;
2668 data
->guard2
->confirmed_idx
= tmp
;
2671 data
->guard2_state
->restrictions
=
2672 guard_create_exit_restriction((uint8_t*)data
->guard1
->identity
);
2674 smartlist_t
*result
= smartlist_new();
2676 r
= entry_guards_upgrade_waiting_circuits(data
->gs
,
2677 data
->all_origin_circuits
,
2679 tt_int_op(r
, OP_EQ
, 1);
2680 tt_int_op(smartlist_len(result
), OP_EQ
, 1);
2681 origin_circuit_t
*oc
= smartlist_get(result
, 0);
2682 tt_ptr_op(oc
, OP_EQ
, data
->circ2
);
2685 smartlist_free(result
);
2689 test_entry_guard_upgrade_not_blocked_by_worse_circ_pending(void *arg
)
2691 upgrade_circuits_data_t
*data
= arg
;
2693 /* circ1 is done, but circ2 is still pending. Since circ1 is better,
2694 * we will upgrade it. */
2695 smartlist_t
*result
= smartlist_new();
2697 r
= entry_guards_upgrade_waiting_circuits(data
->gs
,
2698 data
->all_origin_circuits
,
2700 tt_int_op(r
, OP_EQ
, 1);
2701 tt_int_op(smartlist_len(result
), OP_EQ
, 1);
2702 origin_circuit_t
*oc
= smartlist_get(result
, 0);
2703 tt_ptr_op(oc
, OP_EQ
, data
->circ1
);
2706 smartlist_free(result
);
2710 test_entry_guard_should_expire_waiting(void *arg
)
2713 circuit_guard_state_t
*fake_state
= tor_malloc_zero(sizeof(*fake_state
));
2714 /* We'll leave "guard" unset -- it won't matter here. */
2716 /* No state? Can't expire. */
2717 tt_assert(! entry_guard_state_should_expire(NULL
));
2719 /* Let's try one that expires. */
2720 fake_state
->state
= GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD
;
2721 fake_state
->state_set_at
=
2722 approx_time() - DFLT_NONPRIMARY_GUARD_IDLE_TIMEOUT
- 1;
2724 tt_assert(entry_guard_state_should_expire(fake_state
));
2726 /* But it wouldn't expire if we changed the state. */
2727 fake_state
->state
= GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD
;
2728 tt_assert(! entry_guard_state_should_expire(fake_state
));
2730 /* And it wouldn't have expired a few seconds ago. */
2731 fake_state
->state
= GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD
;
2732 fake_state
->state_set_at
=
2733 approx_time() - DFLT_NONPRIMARY_GUARD_IDLE_TIMEOUT
+ 5;
2734 tt_assert(! entry_guard_state_should_expire(fake_state
));
2737 tor_free(fake_state
);
2740 /** Test that the number of primary guards can be controlled using torrc */
2742 test_entry_guard_number_of_primaries(void *arg
)
2746 /* Get default value */
2747 tt_int_op(get_n_primary_guards(), OP_EQ
, DFLT_N_PRIMARY_GUARDS
);
2749 /* Set number of primaries using torrc */
2750 get_options_mutable()->NumPrimaryGuards
= 42;
2751 tt_int_op(get_n_primary_guards(), OP_EQ
, 42);
2758 mock_directory_initiate_request(directory_request_t
*req
)
2760 if (req
->guard_state
) {
2761 circuit_guard_state_free(req
->guard_state
);
2765 static networkstatus_t
*mock_ns_val
= NULL
;
2766 static networkstatus_t
*
2767 mock_ns_get_by_flavor(consensus_flavor_t f
)
2773 /** Test that when we fetch microdescriptors we skip guards that have
2774 * previously failed to serve us needed microdescriptors. */
2776 test_entry_guard_outdated_dirserver_exclusion(void *arg
)
2779 response_handler_args_t
*args
= NULL
;
2780 dir_connection_t
*conn
= NULL
;
2783 /* Test prep: Make a new guard selection */
2784 guard_selection_t
*gs
= get_guard_selection_by_name("default",
2787 /* ... we want to use entry guards */
2788 or_options_t
*options
= get_options_mutable();
2789 options
->UseEntryGuards
= 1;
2790 options
->UseBridges
= 0;
2792 /* ... prepare some md digests we want to download in the future */
2793 smartlist_t
*digests
= smartlist_new();
2794 const char *prose
= "unhurried and wise, we perceive.";
2795 for (int i
= 0; i
< 20; i
++) {
2796 smartlist_add(digests
, (char*)prose
);
2799 tt_int_op(smartlist_len(digests
), OP_EQ
, 20);
2801 /* ... now mock some functions */
2802 mock_ns_val
= tor_malloc_zero(sizeof(networkstatus_t
));
2803 MOCK(networkstatus_get_latest_consensus_by_flavor
, mock_ns_get_by_flavor
);
2804 MOCK(directory_initiate_request
, mock_directory_initiate_request
);
2807 * 0. Create a proper guard set and primary guard list.
2808 * 1. Pretend to fail microdescriptor fetches from all the primary guards.
2809 * 2. Order another microdescriptor fetch and make sure that primary guards
2810 * get skipped since they failed previous fetches.
2813 { /* Setup primary guard list */
2815 entry_guards_update_primary(gs
);
2816 for (i
= 0; i
< DFLT_N_PRIMARY_GUARDS
; ++i
) {
2817 entry_guard_t
*guard
= smartlist_get(gs
->sampled_entry_guards
, i
);
2818 make_guard_confirmed(gs
, guard
);
2820 entry_guards_update_primary(gs
);
2824 /* Fail microdesc fetches with all the primary guards */
2825 args
= tor_malloc_zero(sizeof(response_handler_args_t
));
2826 args
->status_code
= 404;
2827 args
->reason
= NULL
;
2831 conn
= tor_malloc_zero(sizeof(dir_connection_t
));
2832 conn
->requested_resource
= tor_strdup("d/jlinblackorigami");
2833 conn
->base_
.purpose
= DIR_PURPOSE_FETCH_MICRODESC
;
2835 /* Pretend to fail fetches with all primary guards */
2836 SMARTLIST_FOREACH_BEGIN(gs
->primary_entry_guards
,const entry_guard_t
*,g
) {
2837 memcpy(conn
->identity_digest
, g
->identity
, DIGEST_LEN
);
2839 retval
= handle_response_fetch_microdesc(conn
, args
);
2840 tt_int_op(retval
, OP_EQ
, 0);
2841 } SMARTLIST_FOREACH_END(g
);
2845 /* Now order the final md download */
2846 setup_full_capture_of_logs(LOG_INFO
);
2847 initiate_descriptor_downloads(NULL
, DIR_PURPOSE_FETCH_MICRODESC
,
2850 /* ... and check that because we failed to fetch microdescs from all our
2851 * primaries, we didn't end up selecting a primary for fetching dir info */
2852 expect_log_msg_containing("No primary or confirmed guards available.");
2853 teardown_capture_of_logs();
2857 UNMOCK(networkstatus_get_latest_consensus_by_flavor
);
2858 UNMOCK(directory_initiate_request
);
2859 smartlist_free(digests
);
2860 tor_free(mock_ns_val
);
2863 tor_free(conn
->requested_resource
);
2868 /** Test helper to extend the <b>oc</b> circuit path <b>n</b> times and then
2869 * ensure that the circuit is now complete. */
2871 helper_extend_circuit_path_n_times(origin_circuit_t
*oc
, int n
)
2876 /* Extend path n times */
2877 for (i
= 0 ; i
< n
; i
++) {
2878 retval
= onion_extend_cpath(oc
);
2879 tt_int_op(retval
, OP_EQ
, 0);
2880 tt_int_op(circuit_get_cpath_len(oc
), OP_EQ
, i
+1);
2883 /* Now do it one last time and see that circ is complete */
2884 retval
= onion_extend_cpath(oc
);
2885 tt_int_op(retval
, OP_EQ
, 1);
2891 /** Test for basic Tor path selection. Makes sure we build 3-hop circuits. */
2893 test_entry_guard_basic_path_selection(void *arg
)
2899 /* Enable entry guards */
2900 or_options_t
*options
= get_options_mutable();
2901 options
->UseEntryGuards
= 1;
2903 /* disables /16 check since all nodes have the same addr... */
2904 options
->EnforceDistinctSubnets
= 0;
2906 /* Create our circuit */
2907 circuit_t
*circ
= dummy_origin_circuit_new(30);
2908 origin_circuit_t
*oc
= TO_ORIGIN_CIRCUIT(circ
);
2909 oc
->build_state
= tor_malloc_zero(sizeof(cpath_build_state_t
));
2911 /* First pick the exit and pin it on the build_state */
2912 retval
= onion_pick_cpath_exit(oc
, NULL
, 0);
2913 tt_int_op(retval
, OP_EQ
, 0);
2915 /* Extend path 3 times. First we pick guard, then middle, then exit. */
2916 helper_extend_circuit_path_n_times(oc
, 3);
2919 circuit_free_(circ
);
2922 /** Test helper to build an L2 and L3 vanguard list. The vanguard lists
2923 * produced should be completely disjoint. */
2925 helper_setup_vanguard_list(or_options_t
*options
)
2929 /* Add some nodes to the vanguard L2 list */
2930 options
->HSLayer2Nodes
= routerset_new();
2931 for (i
= 0; i
< 10 ; i
+= 2) {
2932 node_t
*vanguard_node
= smartlist_get(big_fake_net_nodes
, i
);
2933 tt_assert(vanguard_node
->is_possible_guard
);
2934 routerset_parse(options
->HSLayer2Nodes
, vanguard_node
->rs
->nickname
, "l2");
2936 /* also add some nodes to vanguard L3 list
2937 * (L2 list and L3 list should be disjoint for this test to work) */
2938 options
->HSLayer3Nodes
= routerset_new();
2939 for (i
= 10; i
< 20 ; i
+= 2) {
2940 node_t
*vanguard_node
= smartlist_get(big_fake_net_nodes
, i
);
2941 tt_assert(vanguard_node
->is_possible_guard
);
2942 routerset_parse(options
->HSLayer3Nodes
, vanguard_node
->rs
->nickname
, "l3");
2949 /** Test to ensure that vanguard path selection works properly. Ensures that
2950 * default vanguard circuits are 4 hops, and that path selection works
2951 * correctly given the vanguard settings. */
2953 test_entry_guard_vanguard_path_selection(void *arg
)
2959 /* Enable entry guards */
2960 or_options_t
*options
= get_options_mutable();
2961 options
->UseEntryGuards
= 1;
2963 /* XXX disables /16 check */
2964 options
->EnforceDistinctSubnets
= 0;
2966 /* Setup our vanguard list */
2967 helper_setup_vanguard_list(options
);
2969 /* Create our circuit */
2970 circuit_t
*circ
= dummy_origin_circuit_new(30);
2971 origin_circuit_t
*oc
= TO_ORIGIN_CIRCUIT(circ
);
2972 oc
->build_state
= tor_malloc_zero(sizeof(cpath_build_state_t
));
2973 oc
->build_state
->is_internal
= 1;
2975 /* Switch circuit purpose to vanguards */
2976 circ
->purpose
= CIRCUIT_PURPOSE_HS_VANGUARDS
;
2978 /* First pick the exit and pin it on the build_state */
2979 tt_int_op(oc
->build_state
->desired_path_len
, OP_EQ
, 0);
2980 retval
= onion_pick_cpath_exit(oc
, NULL
, 0);
2981 tt_int_op(retval
, OP_EQ
, 0);
2983 /* Ensure that vanguards make 4-hop circuits by default */
2984 tt_int_op(oc
->build_state
->desired_path_len
, OP_EQ
, 4);
2986 /* Extend path as many times as needed to have complete circ. */
2987 helper_extend_circuit_path_n_times(oc
, oc
->build_state
->desired_path_len
);
2989 /* Test that the cpath linked list is set correctly. */
2990 crypt_path_t
*l1_node
= oc
->cpath
;
2991 crypt_path_t
*l2_node
= l1_node
->next
;
2992 crypt_path_t
*l3_node
= l2_node
->next
;
2993 crypt_path_t
*l4_node
= l3_node
->next
;
2994 crypt_path_t
*l1_node_again
= l4_node
->next
;
2995 tt_ptr_op(l1_node
, OP_EQ
, l1_node_again
);
2997 /* Test that L2 is indeed HSLayer2Node */
2998 retval
= routerset_contains_extendinfo(options
->HSLayer2Nodes
,
2999 l2_node
->extend_info
);
3000 tt_int_op(retval
, OP_EQ
, 4);
3001 /* test that L3 node is _not_ contained in HSLayer2Node */
3002 retval
= routerset_contains_extendinfo(options
->HSLayer2Nodes
,
3003 l3_node
->extend_info
);
3004 tt_int_op(retval
, OP_LT
, 4);
3006 /* Test that L3 is indeed HSLayer3Node */
3007 retval
= routerset_contains_extendinfo(options
->HSLayer3Nodes
,
3008 l3_node
->extend_info
);
3009 tt_int_op(retval
, OP_EQ
, 4);
3010 /* test that L2 node is _not_ contained in HSLayer3Node */
3011 retval
= routerset_contains_extendinfo(options
->HSLayer3Nodes
,
3012 l2_node
->extend_info
);
3013 tt_int_op(retval
, OP_LT
, 4);
3015 /* TODO: Test that L1 can be the same as exit. To test this we need start
3016 enforcing EnforceDistinctSubnets again, which means that we need to give
3017 each test node a different address which currently breaks some tests. */
3020 circuit_free_(circ
);
3023 static const struct testcase_setup_t big_fake_network
= {
3024 big_fake_network_setup
, big_fake_network_cleanup
3027 static const struct testcase_setup_t upgrade_circuits
= {
3028 upgrade_circuits_setup
, upgrade_circuits_cleanup
3031 #define NO_PREFIX_TEST(name) \
3032 { #name, test_ ## name, 0, NULL, NULL }
3034 #define EN_TEST_BASE(name, fork, setup, arg) \
3035 { #name, test_entry_guard_ ## name, fork, setup, (void*)(arg) }
3037 #define EN_TEST(name) EN_TEST_BASE(name, 0, NULL, NULL)
3038 #define EN_TEST_FORK(name) EN_TEST_BASE(name, TT_FORK, NULL, NULL)
3040 #define BFN_TEST(name) \
3041 EN_TEST_BASE(name, TT_FORK, &big_fake_network, NULL), \
3042 { #name "_reasonably_live", test_entry_guard_ ## name, TT_FORK, \
3043 &big_fake_network, (void*)("reasonably-live") }
3045 #define UPGRADE_TEST(name, arg) \
3046 EN_TEST_BASE(name, TT_FORK, &upgrade_circuits, arg), \
3047 { #name "_reasonably_live", test_entry_guard_ ## name, TT_FORK, \
3048 &upgrade_circuits, (void*)(arg " reasonably-live") }
3050 struct testcase_t entrynodes_tests
[] = {
3051 NO_PREFIX_TEST(node_preferred_orport
),
3052 NO_PREFIX_TEST(entry_guard_describe
),
3054 EN_TEST(randomize_time
),
3055 EN_TEST(encode_for_state_minimal
),
3056 EN_TEST(encode_for_state_maximal
),
3057 EN_TEST(parse_from_state_minimal
),
3058 EN_TEST(parse_from_state_maximal
),
3059 EN_TEST(parse_from_state_failure
),
3060 EN_TEST(parse_from_state_partial_failure
),
3062 EN_TEST_FORK(parse_from_state_full
),
3063 EN_TEST_FORK(parse_from_state_broken
),
3064 EN_TEST_FORK(get_guard_selection_by_name
),
3065 EN_TEST_FORK(number_of_primaries
),
3067 BFN_TEST(choose_selection_initial
),
3068 BFN_TEST(add_single_guard
),
3069 BFN_TEST(node_filter
),
3070 BFN_TEST(expand_sample
),
3071 BFN_TEST(expand_sample_small_net
),
3072 BFN_TEST(update_from_consensus_status
),
3073 BFN_TEST(update_from_consensus_repair
),
3074 BFN_TEST(update_from_consensus_remove
),
3075 BFN_TEST(confirming_guards
),
3076 BFN_TEST(sample_reachable_filtered
),
3077 BFN_TEST(sample_reachable_filtered_empty
),
3078 BFN_TEST(retry_unreachable
),
3079 BFN_TEST(manage_primary
),
3081 EN_TEST_FORK(guard_preferred
),
3083 BFN_TEST(select_for_circuit_no_confirmed
),
3084 BFN_TEST(select_for_circuit_confirmed
),
3085 BFN_TEST(select_for_circuit_highlevel_primary
),
3086 BFN_TEST(select_for_circuit_highlevel_confirm_other
),
3087 BFN_TEST(select_for_circuit_highlevel_primary_retry
),
3088 BFN_TEST(select_and_cancel
),
3089 BFN_TEST(drop_guards
),
3090 BFN_TEST(outdated_dirserver_exclusion
),
3091 BFN_TEST(basic_path_selection
),
3092 BFN_TEST(vanguard_path_selection
),
3094 UPGRADE_TEST(upgrade_a_circuit
, "c1-done c2-done"),
3095 UPGRADE_TEST(upgrade_blocked_by_live_primary_guards
, "c1-done c2-done"),
3096 UPGRADE_TEST(upgrade_blocked_by_lack_of_waiting_circuits
, ""),
3097 UPGRADE_TEST(upgrade_blocked_by_better_circ_complete
, "c1-done c2-done"),
3098 UPGRADE_TEST(upgrade_not_blocked_by_restricted_circ_complete
,
3100 UPGRADE_TEST(upgrade_not_blocked_by_worse_circ_complete
, "c1-done c2-done"),
3101 UPGRADE_TEST(upgrade_blocked_by_better_circ_pending
, "c2-done"),
3102 UPGRADE_TEST(upgrade_not_blocked_by_restricted_circ_pending
,
3104 UPGRADE_TEST(upgrade_not_blocked_by_worse_circ_pending
, "c1-done"),
3106 EN_TEST_FORK(should_expire_waiting
),