1 /* Copyright (c) 2001-2004, Roger Dingledine.
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2021, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
6 #define CIRCUITBUILD_PRIVATE
7 #define CIRCUITLIST_PRIVATE
8 #define ENTRYNODES_PRIVATE
10 #include "core/or/or.h"
12 #include "test/test.h"
13 #include "test/test_helpers.h"
14 #include "test/log_test_helpers.h"
16 #define CONFIG_PRIVATE
17 #include "app/config/config.h"
19 #include "core/or/channel.h"
20 #include "core/or/circuitbuild.h"
21 #include "core/or/circuitlist.h"
22 #include "core/or/circuituse.h"
23 #include "core/or/onion.h"
25 #include "core/or/cell_st.h"
26 #include "core/or/cpath_build_state_st.h"
27 #include "core/or/extend_info_st.h"
28 #include "core/or/origin_circuit_st.h"
29 #include "core/or/or_circuit_st.h"
31 #include "feature/client/entrynodes.h"
32 #include "feature/nodelist/nodelist.h"
33 #include "feature/nodelist/node_select.h"
34 #include "feature/relay/circuitbuild_relay.h"
35 #include "feature/relay/router.h"
36 #include "feature/relay/routermode.h"
38 #include "feature/nodelist/node_st.h"
39 #include "feature/nodelist/routerinfo_st.h"
41 /* Dummy nodes smartlist for testing */
42 static smartlist_t dummy_nodes
;
43 /* Dummy exit extend_info for testing */
44 static extend_info_t dummy_ei
;
47 mock_count_acceptable_nodes(const smartlist_t
*nodes
, int direct
)
51 return direct
? 1 : DEFAULT_ROUTE_LEN
+ 1;
54 /* Test route lengths when the caller of new_route_len() doesn't
57 test_new_route_len_noexit(void *arg
)
62 MOCK(count_acceptable_nodes
, mock_count_acceptable_nodes
);
64 r
= new_route_len(CIRCUIT_PURPOSE_C_GENERAL
, NULL
, &dummy_nodes
);
65 tt_int_op(DEFAULT_ROUTE_LEN
, OP_EQ
, r
);
67 r
= new_route_len(CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT
, NULL
, &dummy_nodes
);
68 tt_int_op(DEFAULT_ROUTE_LEN
, OP_EQ
, r
);
70 r
= new_route_len(CIRCUIT_PURPOSE_S_CONNECT_REND
, NULL
, &dummy_nodes
);
71 tt_int_op(DEFAULT_ROUTE_LEN
, OP_EQ
, r
);
74 UNMOCK(count_acceptable_nodes
);
77 /* Test route lengths where someone else chose the "exit" node, which
78 * require an extra hop for safety. */
80 test_new_route_len_unsafe_exit(void *arg
)
85 MOCK(count_acceptable_nodes
, mock_count_acceptable_nodes
);
87 /* connecting to hidden service directory */
88 r
= new_route_len(CIRCUIT_PURPOSE_C_GENERAL
, &dummy_ei
, &dummy_nodes
);
89 tt_int_op(DEFAULT_ROUTE_LEN
+ 1, OP_EQ
, r
);
91 /* client connecting to introduction point */
92 r
= new_route_len(CIRCUIT_PURPOSE_C_INTRODUCING
, &dummy_ei
, &dummy_nodes
);
93 tt_int_op(DEFAULT_ROUTE_LEN
+ 1, OP_EQ
, r
);
95 /* hidden service connecting to rendezvous point */
96 r
= new_route_len(CIRCUIT_PURPOSE_S_CONNECT_REND
, &dummy_ei
, &dummy_nodes
);
97 tt_int_op(DEFAULT_ROUTE_LEN
+ 1, OP_EQ
, r
);
100 UNMOCK(count_acceptable_nodes
);
103 /* Test route lengths where we chose the "exit" node, which don't
104 * require an extra hop for safety. */
106 test_new_route_len_safe_exit(void *arg
)
111 MOCK(count_acceptable_nodes
, mock_count_acceptable_nodes
);
113 /* hidden service connecting to introduction point */
114 r
= new_route_len(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO
, &dummy_ei
,
116 tt_int_op(DEFAULT_ROUTE_LEN
+1, OP_EQ
, r
);
118 /* router testing its own reachability */
119 r
= new_route_len(CIRCUIT_PURPOSE_TESTING
, &dummy_ei
, &dummy_nodes
);
120 tt_int_op(DEFAULT_ROUTE_LEN
, OP_EQ
, r
);
123 UNMOCK(count_acceptable_nodes
);
126 /* Make sure a non-fatal assertion fails when new_route_len() gets an
127 * unexpected circuit purpose. */
129 test_new_route_len_unhandled_exit(void *arg
)
134 #ifdef ALL_BUGS_ARE_FATAL
135 /* Coverity (and maybe clang analyser) complain that the code following
136 * tt_skip() is unconditionally unreachable. */
137 #if !defined(__COVERITY__) && !defined(__clang_analyzer__)
140 #endif /* defined(ALL_BUGS_ARE_FATAL) */
142 MOCK(count_acceptable_nodes
, mock_count_acceptable_nodes
);
144 tor_capture_bugs_(1);
145 setup_full_capture_of_logs(LOG_WARN
);
146 r
= new_route_len(CIRCUIT_PURPOSE_CONTROLLER
, &dummy_ei
, &dummy_nodes
);
147 tt_int_op(DEFAULT_ROUTE_LEN
+ 1, OP_EQ
, r
);
148 tt_int_op(smartlist_len(tor_get_captured_bug_log_()), OP_EQ
, 1);
149 tt_str_op(smartlist_get(tor_get_captured_bug_log_(), 0), OP_EQ
,
150 "!(exit_ei && !known_purpose)");
151 expect_single_log_msg_containing("Unhandled purpose");
152 expect_single_log_msg_containing("with a chosen exit; assuming routelen");
155 teardown_capture_of_logs();
156 tor_end_capture_bugs_();
157 UNMOCK(count_acceptable_nodes
);
161 test_upgrade_from_guard_wait(void *arg
)
163 circuit_t
*circ
= NULL
;
164 origin_circuit_t
*orig_circ
= NULL
;
165 entry_guard_t
*guard
= NULL
;
166 smartlist_t
*list
= NULL
;
170 circ
= dummy_origin_circuit_new(0);
171 orig_circ
= TO_ORIGIN_CIRCUIT(circ
);
172 tt_assert(orig_circ
);
174 orig_circ
->build_state
= tor_malloc_zero(sizeof(cpath_build_state_t
));
176 circuit_set_state(circ
, CIRCUIT_STATE_GUARD_WAIT
);
178 /* Put it in guard wait state. */
179 guard
= tor_malloc_zero(sizeof(*guard
));
180 guard
->in_selection
= get_guard_selection_info();
182 orig_circ
->guard_state
=
183 circuit_guard_state_new(guard
, GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD
,
186 /* Mark the circuit for close. */
187 circuit_mark_for_close(circ
, END_CIRC_REASON_TORPROTOCOL
);
188 tt_int_op(circ
->marked_for_close
, OP_NE
, 0);
190 /* We shouldn't pick the mark for close circuit. */
191 list
= circuit_find_circuits_to_upgrade_from_guard_wait();
195 smartlist_free(list
);
197 entry_guard_free_(guard
);
200 static int server
= 0;
202 mock_server_mode(const or_options_t
*options
)
208 /* Test the different cases in circuit_extend_state_valid_helper(). */
210 test_circuit_extend_state_valid(void *arg
)
213 circuit_t
*circ
= tor_malloc_zero(sizeof(circuit_t
));
216 MOCK(server_mode
, mock_server_mode
);
218 setup_full_capture_of_logs(LOG_INFO
);
220 /* Clients can't extend */
222 tt_int_op(circuit_extend_state_valid_helper(NULL
), OP_EQ
, -1);
223 expect_log_msg("Got an extend cell, but running as a client. Closing.\n");
224 mock_clean_saved_logs();
226 #ifndef ALL_BUGS_ARE_FATAL
227 /* Circuit must be non-NULL */
228 tor_capture_bugs_(1);
230 tt_int_op(circuit_extend_state_valid_helper(NULL
), OP_EQ
, -1);
231 tt_int_op(smartlist_len(tor_get_captured_bug_log_()), OP_EQ
, 1);
232 tt_str_op(smartlist_get(tor_get_captured_bug_log_(), 0), OP_EQ
,
233 "!(ASSERT_PREDICT_UNLIKELY_(!circ))");
234 tor_end_capture_bugs_();
235 mock_clean_saved_logs();
236 #endif /* !defined(ALL_BUGS_ARE_FATAL) */
238 /* n_chan and n_hop are NULL, this should succeed */
240 tt_int_op(circuit_extend_state_valid_helper(circ
), OP_EQ
, 0);
241 mock_clean_saved_logs();
243 /* But clients still can't extend */
245 tt_int_op(circuit_extend_state_valid_helper(circ
), OP_EQ
, -1);
246 expect_log_msg("Got an extend cell, but running as a client. Closing.\n");
247 mock_clean_saved_logs();
249 /* n_chan must be NULL */
250 circ
->n_chan
= tor_malloc_zero(sizeof(channel_t
));
252 tt_int_op(circuit_extend_state_valid_helper(circ
), OP_EQ
, -1);
253 expect_log_msg("n_chan already set. Bug/attack. Closing.\n");
254 mock_clean_saved_logs();
255 tor_free(circ
->n_chan
);
257 /* n_hop must be NULL */
258 circ
->n_hop
= tor_malloc_zero(sizeof(extend_info_t
));
260 tt_int_op(circuit_extend_state_valid_helper(circ
), OP_EQ
, -1);
261 expect_log_msg("conn to next hop already launched. Bug/attack. Closing.\n");
262 mock_clean_saved_logs();
263 tor_free(circ
->n_hop
);
266 tor_end_capture_bugs_();
267 teardown_capture_of_logs();
272 tor_free(circ
->n_chan
);
273 tor_free(circ
->n_hop
);
277 static node_t
*mocked_node
= NULL
;
278 static const node_t
*
279 mock_node_get_by_id(const char *identity_digest
)
281 (void)identity_digest
;
285 static bool mocked_supports_ed25519_link_authentication
= 0;
287 mock_node_supports_ed25519_link_authentication(const node_t
*node
,
288 bool compatible_with_us
)
291 (void)compatible_with_us
;
292 return mocked_supports_ed25519_link_authentication
;
295 static ed25519_public_key_t
* mocked_ed25519_id
= NULL
;
296 static const ed25519_public_key_t
*
297 mock_node_get_ed25519_id(const node_t
*node
)
300 return mocked_ed25519_id
;
303 /* Test the different cases in circuit_extend_add_ed25519_helper(). */
305 test_circuit_extend_add_ed25519(void *arg
)
308 extend_cell_t
*ec
= tor_malloc_zero(sizeof(extend_cell_t
));
309 extend_cell_t
*old_ec
= tor_malloc_zero(sizeof(extend_cell_t
));
310 extend_cell_t
*zero_ec
= tor_malloc_zero(sizeof(extend_cell_t
));
312 node_t
*fake_node
= tor_malloc_zero(sizeof(node_t
));
313 ed25519_public_key_t
*fake_ed25519_id
= NULL
;
314 fake_ed25519_id
= tor_malloc_zero(sizeof(ed25519_public_key_t
));
316 MOCK(node_get_by_id
, mock_node_get_by_id
);
317 MOCK(node_supports_ed25519_link_authentication
,
318 mock_node_supports_ed25519_link_authentication
);
319 MOCK(node_get_ed25519_id
, mock_node_get_ed25519_id
);
321 setup_full_capture_of_logs(LOG_INFO
);
323 #ifndef ALL_BUGS_ARE_FATAL
324 /* The extend cell must be non-NULL */
325 tor_capture_bugs_(1);
326 tt_int_op(circuit_extend_add_ed25519_helper(NULL
), OP_EQ
, -1);
327 tt_int_op(smartlist_len(tor_get_captured_bug_log_()), OP_EQ
, 1);
328 tt_str_op(smartlist_get(tor_get_captured_bug_log_(), 0), OP_EQ
,
329 "!(ASSERT_PREDICT_UNLIKELY_(!ec))");
330 tor_end_capture_bugs_();
331 mock_clean_saved_logs();
332 #endif /* !defined(ALL_BUGS_ARE_FATAL) */
334 /* The node id must be non-zero */
335 memcpy(old_ec
, ec
, sizeof(extend_cell_t
));
336 tt_int_op(circuit_extend_add_ed25519_helper(ec
), OP_EQ
, -1);
338 "Client asked me to extend without specifying an id_digest.\n");
339 /* And nothing should have changed */
340 tt_mem_op(ec
, OP_EQ
, old_ec
, sizeof(extend_cell_t
));
341 mock_clean_saved_logs();
343 /* Fill in fake node_id, and try again */
344 memset(ec
->node_id
, 0xAA, sizeof(ec
->node_id
));
345 memcpy(old_ec
, ec
, sizeof(extend_cell_t
));
346 tt_int_op(circuit_extend_add_ed25519_helper(ec
), OP_EQ
, 0);
347 /* There's no node with that id, so the ed pubkey should still be zeroed */
348 tt_mem_op(&ec
->ed_pubkey
, OP_EQ
, &zero_ec
->ed_pubkey
, sizeof(ec
->ed_pubkey
));
349 /* In fact, nothing should have changed */
350 tt_mem_op(ec
, OP_EQ
, old_ec
, sizeof(extend_cell_t
));
351 mock_clean_saved_logs();
353 /* Provide 2 out of 3 of node, supports link auth, and ed_id.
354 * The ed_id should remain zeroed. */
356 /* Provide node and supports link auth */
357 memset(ec
->node_id
, 0xAA, sizeof(ec
->node_id
));
358 memcpy(old_ec
, ec
, sizeof(extend_cell_t
));
359 /* Set up the fake variables */
360 mocked_node
= fake_node
;
361 mocked_supports_ed25519_link_authentication
= 1;
363 tt_int_op(circuit_extend_add_ed25519_helper(ec
), OP_EQ
, 0);
364 /* The ed pubkey should still be zeroed */
365 tt_mem_op(&ec
->ed_pubkey
, OP_EQ
, &zero_ec
->ed_pubkey
, sizeof(ec
->ed_pubkey
));
366 /* In fact, nothing should have changed */
367 tt_mem_op(ec
, OP_EQ
, old_ec
, sizeof(extend_cell_t
));
369 mock_clean_saved_logs();
371 mocked_supports_ed25519_link_authentication
= 0;
372 mocked_ed25519_id
= NULL
;
373 memset(fake_ed25519_id
, 0x00, sizeof(ed25519_public_key_t
));
375 /* Provide supports link auth and ed id */
376 memset(ec
->node_id
, 0xAA, sizeof(ec
->node_id
));
377 memcpy(old_ec
, ec
, sizeof(extend_cell_t
));
378 /* Set up the fake variables */
379 mocked_supports_ed25519_link_authentication
= 1;
380 memset(fake_ed25519_id
, 0xEE, sizeof(ed25519_public_key_t
));
381 mocked_ed25519_id
= fake_ed25519_id
;
383 tt_int_op(circuit_extend_add_ed25519_helper(ec
), OP_EQ
, 0);
384 /* The ed pubkey should still be zeroed */
385 tt_mem_op(&ec
->ed_pubkey
, OP_EQ
, &zero_ec
->ed_pubkey
, sizeof(ec
->ed_pubkey
));
386 /* In fact, nothing should have changed */
387 tt_mem_op(ec
, OP_EQ
, old_ec
, sizeof(extend_cell_t
));
389 mock_clean_saved_logs();
391 mocked_supports_ed25519_link_authentication
= 0;
392 mocked_ed25519_id
= NULL
;
393 memset(fake_ed25519_id
, 0x00, sizeof(ed25519_public_key_t
));
395 /* Provide node and ed id */
396 memset(ec
->node_id
, 0xAA, sizeof(ec
->node_id
));
397 memcpy(old_ec
, ec
, sizeof(extend_cell_t
));
398 /* Set up the fake variables */
399 mocked_node
= fake_node
;
400 memset(fake_ed25519_id
, 0xEE, sizeof(ed25519_public_key_t
));
401 mocked_ed25519_id
= fake_ed25519_id
;
403 tt_int_op(circuit_extend_add_ed25519_helper(ec
), OP_EQ
, 0);
404 /* The ed pubkey should still be zeroed */
405 tt_mem_op(&ec
->ed_pubkey
, OP_EQ
, &zero_ec
->ed_pubkey
, sizeof(ec
->ed_pubkey
));
406 /* In fact, nothing should have changed */
407 tt_mem_op(ec
, OP_EQ
, old_ec
, sizeof(extend_cell_t
));
409 mock_clean_saved_logs();
411 mocked_supports_ed25519_link_authentication
= 0;
412 mocked_ed25519_id
= NULL
;
413 memset(fake_ed25519_id
, 0x00, sizeof(ed25519_public_key_t
));
415 /* Now do the real lookup */
416 memset(ec
->node_id
, 0xAA, sizeof(ec
->node_id
));
417 memcpy(old_ec
, ec
, sizeof(extend_cell_t
));
418 /* Set up the fake variables */
419 mocked_node
= fake_node
;
420 mocked_supports_ed25519_link_authentication
= 1;
421 memset(fake_ed25519_id
, 0xEE, sizeof(ed25519_public_key_t
));
422 mocked_ed25519_id
= fake_ed25519_id
;
424 tt_int_op(circuit_extend_add_ed25519_helper(ec
), OP_EQ
, 0);
425 /* The ed pubkey should match */
426 tt_mem_op(&ec
->ed_pubkey
, OP_EQ
, fake_ed25519_id
, sizeof(ec
->ed_pubkey
));
427 /* Nothing else should have changed */
428 memcpy(&ec
->ed_pubkey
, &old_ec
->ed_pubkey
, sizeof(ec
->ed_pubkey
));
429 tt_mem_op(ec
, OP_EQ
, old_ec
, sizeof(extend_cell_t
));
431 mock_clean_saved_logs();
433 mocked_supports_ed25519_link_authentication
= 0;
434 mocked_ed25519_id
= NULL
;
435 memset(fake_ed25519_id
, 0x00, sizeof(ed25519_public_key_t
));
437 /* Now do the real lookup, but with a zeroed ed id */
438 memset(ec
->node_id
, 0xAA, sizeof(ec
->node_id
));
439 memcpy(old_ec
, ec
, sizeof(extend_cell_t
));
440 /* Set up the fake variables */
441 mocked_node
= fake_node
;
442 mocked_supports_ed25519_link_authentication
= 1;
443 memset(fake_ed25519_id
, 0x00, sizeof(ed25519_public_key_t
));
444 mocked_ed25519_id
= fake_ed25519_id
;
446 tt_int_op(circuit_extend_add_ed25519_helper(ec
), OP_EQ
, 0);
447 /* The ed pubkey should match */
448 tt_mem_op(&ec
->ed_pubkey
, OP_EQ
, fake_ed25519_id
, sizeof(ec
->ed_pubkey
));
449 /* Nothing else should have changed */
450 memcpy(&ec
->ed_pubkey
, &old_ec
->ed_pubkey
, sizeof(ec
->ed_pubkey
));
451 tt_mem_op(ec
, OP_EQ
, old_ec
, sizeof(extend_cell_t
));
453 mock_clean_saved_logs();
455 mocked_supports_ed25519_link_authentication
= 0;
456 mocked_ed25519_id
= NULL
;
457 memset(fake_ed25519_id
, 0x00, sizeof(ed25519_public_key_t
));
460 UNMOCK(node_get_by_id
);
461 UNMOCK(node_supports_ed25519_link_authentication
);
462 UNMOCK(node_get_ed25519_id
);
464 tor_end_capture_bugs_();
465 teardown_capture_of_logs();
471 tor_free(fake_ed25519_id
);
475 static or_options_t
*mocked_options
= NULL
;
476 static const or_options_t
*
477 mock_get_options(void)
479 return mocked_options
;
482 #define PUBLIC_IPV4 "1.2.3.4"
483 #define INTERNAL_IPV4 "0.0.0.1"
485 #define PUBLIC_IPV6 "1234::cdef"
486 #define INTERNAL_IPV6 "::1"
488 #define VALID_PORT 0x1234
490 /* Test the different cases in circuit_extend_lspec_valid_helper(). */
492 test_circuit_extend_lspec_valid(void *arg
)
495 extend_cell_t
*ec
= tor_malloc_zero(sizeof(extend_cell_t
));
496 channel_t
*p_chan
= tor_malloc_zero(sizeof(channel_t
));
497 or_circuit_t
*or_circ
= tor_malloc_zero(sizeof(or_circuit_t
));
498 circuit_t
*circ
= TO_CIRCUIT(or_circ
);
500 or_options_t
*fake_options
= options_new();
501 MOCK(get_options
, mock_get_options
);
502 mocked_options
= fake_options
;
504 setup_full_capture_of_logs(LOG_INFO
);
506 #ifndef ALL_BUGS_ARE_FATAL
507 /* Extend cell must be non-NULL */
508 tor_capture_bugs_(1);
509 tt_int_op(circuit_extend_lspec_valid_helper(NULL
, circ
), OP_EQ
, -1);
510 tt_int_op(smartlist_len(tor_get_captured_bug_log_()), OP_EQ
, 1);
511 tt_str_op(smartlist_get(tor_get_captured_bug_log_(), 0), OP_EQ
,
512 "!(ASSERT_PREDICT_UNLIKELY_(!ec))");
513 tor_end_capture_bugs_();
514 mock_clean_saved_logs();
516 /* Circuit must be non-NULL */
517 tor_capture_bugs_(1);
518 tt_int_op(circuit_extend_lspec_valid_helper(ec
, NULL
), OP_EQ
, -1);
519 tt_int_op(smartlist_len(tor_get_captured_bug_log_()), OP_EQ
, 1);
520 tt_str_op(smartlist_get(tor_get_captured_bug_log_(), 0), OP_EQ
,
521 "!(ASSERT_PREDICT_UNLIKELY_(!circ))");
522 tor_end_capture_bugs_();
523 mock_clean_saved_logs();
525 /* Extend cell and circuit must be non-NULL */
526 tor_capture_bugs_(1);
527 tt_int_op(circuit_extend_lspec_valid_helper(NULL
, NULL
), OP_EQ
, -1);
528 /* Since we're using IF_BUG_ONCE(), we might not log any bugs */
529 tt_int_op(smartlist_len(tor_get_captured_bug_log_()), OP_GE
, 0);
530 tt_int_op(smartlist_len(tor_get_captured_bug_log_()), OP_LE
, 2);
531 tor_end_capture_bugs_();
532 mock_clean_saved_logs();
533 #endif /* !defined(ALL_BUGS_ARE_FATAL) */
535 /* IPv4 and IPv6 addr and port are all zero, this should fail */
536 tt_int_op(circuit_extend_lspec_valid_helper(ec
, circ
), OP_EQ
, -1);
537 expect_log_msg("Client asked me to extend to a zero destination port "
538 "or unspecified address '[scrubbed]'.\n");
539 mock_clean_saved_logs();
541 /* Now ask for the actual address in the logs */
542 fake_options
->SafeLogging_
= SAFELOG_SCRUB_NONE
;
544 /* IPv4 port is 0, IPv6 addr and port are both zero, this should fail */
545 tor_addr_parse(&ec
->orport_ipv4
.addr
, PUBLIC_IPV4
);
546 tt_int_op(circuit_extend_lspec_valid_helper(ec
, circ
), OP_EQ
, -1);
547 expect_log_msg("Client asked me to extend to a zero destination port "
548 "or IPv4 address '1.2.3.4:0'.\n");
549 mock_clean_saved_logs();
550 tor_addr_port_make_null_ap(&ec
->orport_ipv4
, AF_INET
);
551 tor_addr_port_make_null_ap(&ec
->orport_ipv6
, AF_INET6
);
553 /* IPv4 addr is 0, IPv6 addr and port are both zero, this should fail */
554 ec
->orport_ipv4
.port
= VALID_PORT
;
555 tt_int_op(circuit_extend_lspec_valid_helper(ec
, circ
), OP_EQ
, -1);
556 expect_log_msg("Client asked me to extend to a zero destination port "
557 "or IPv4 address '0.0.0.0:4660'.\n");
558 mock_clean_saved_logs();
559 ec
->orport_ipv4
.port
= 0;
560 tor_addr_port_make_null_ap(&ec
->orport_ipv4
, AF_INET
);
561 tor_addr_port_make_null_ap(&ec
->orport_ipv6
, AF_INET6
);
563 /* IPv4 addr is internal, and port is valid.
564 * (IPv6 addr and port are both zero.)
565 * Result depends on ExtendAllowPrivateAddresses. */
566 tor_addr_parse(&ec
->orport_ipv4
.addr
, INTERNAL_IPV4
);
567 ec
->orport_ipv4
.port
= VALID_PORT
;
569 fake_options
->ExtendAllowPrivateAddresses
= 0;
570 tt_int_op(circuit_extend_lspec_valid_helper(ec
, circ
), OP_EQ
, -1);
571 expect_log_msg("Client asked me to extend "
572 "to a private IPv4 address '0.0.0.1'.\n");
573 mock_clean_saved_logs();
574 fake_options
->ExtendAllowPrivateAddresses
= 0;
575 tor_addr_port_make_null_ap(&ec
->orport_ipv4
, AF_INET
);
576 tor_addr_port_make_null_ap(&ec
->orport_ipv6
, AF_INET6
);
578 /* Now do the same tests, but for IPv6 */
580 /* IPv6 port is 0, IPv4 addr and port are both zero, this should fail */
581 tor_addr_parse(&ec
->orport_ipv6
.addr
, PUBLIC_IPV6
);
582 tt_int_op(circuit_extend_lspec_valid_helper(ec
, circ
), OP_EQ
, -1);
583 expect_log_msg("Client asked me to extend to a zero destination port "
584 "or IPv6 address '[1234::cdef]:0'.\n");
585 mock_clean_saved_logs();
586 tor_addr_port_make_null_ap(&ec
->orport_ipv4
, AF_INET
);
587 tor_addr_port_make_null_ap(&ec
->orport_ipv6
, AF_INET6
);
589 /* IPv6 addr is 0, IPv4 addr and port are both zero, this should fail */
590 ec
->orport_ipv6
.port
= VALID_PORT
;
591 tt_int_op(circuit_extend_lspec_valid_helper(ec
, circ
), OP_EQ
, -1);
592 expect_log_msg("Client asked me to extend to a zero destination port "
593 "or IPv6 address '[::]:4660'.\n");
594 mock_clean_saved_logs();
595 ec
->orport_ipv4
.port
= 0;
596 tor_addr_port_make_null_ap(&ec
->orport_ipv4
, AF_INET
);
597 tor_addr_port_make_null_ap(&ec
->orport_ipv6
, AF_INET6
);
599 /* IPv6 addr is internal, and port is valid.
600 * (IPv4 addr and port are both zero.)
601 * Result depends on ExtendAllowPrivateAddresses. */
602 tor_addr_parse(&ec
->orport_ipv6
.addr
, INTERNAL_IPV6
);
603 ec
->orport_ipv6
.port
= VALID_PORT
;
605 fake_options
->ExtendAllowPrivateAddresses
= 0;
606 tt_int_op(circuit_extend_lspec_valid_helper(ec
, circ
), OP_EQ
, -1);
607 expect_log_msg("Client asked me to extend "
608 "to a private IPv6 address '[::1]'.\n");
609 mock_clean_saved_logs();
610 fake_options
->ExtendAllowPrivateAddresses
= 0;
611 tor_addr_port_make_null_ap(&ec
->orport_ipv4
, AF_INET
);
612 tor_addr_port_make_null_ap(&ec
->orport_ipv6
, AF_INET6
);
614 /* Both addresses are internal.
615 * Result depends on ExtendAllowPrivateAddresses. */
616 tor_addr_parse(&ec
->orport_ipv4
.addr
, INTERNAL_IPV4
);
617 ec
->orport_ipv4
.port
= VALID_PORT
;
618 tor_addr_parse(&ec
->orport_ipv6
.addr
, INTERNAL_IPV6
);
619 ec
->orport_ipv6
.port
= VALID_PORT
;
621 fake_options
->ExtendAllowPrivateAddresses
= 0;
622 tt_int_op(circuit_extend_lspec_valid_helper(ec
, circ
), OP_EQ
, -1);
623 expect_log_msg("Client asked me to extend "
624 "to a private IPv4 address '0.0.0.1'.\n");
625 expect_log_msg("Client asked me to extend "
626 "to a private IPv6 address '[::1]'.\n");
627 mock_clean_saved_logs();
628 fake_options
->ExtendAllowPrivateAddresses
= 0;
629 tor_addr_port_make_null_ap(&ec
->orport_ipv4
, AF_INET
);
630 tor_addr_port_make_null_ap(&ec
->orport_ipv6
, AF_INET6
);
632 #ifndef ALL_BUGS_ARE_FATAL
633 /* If we pass the private address check, but don't have the right
634 * OR circuit magic number, we trigger another bug */
635 tor_addr_parse(&ec
->orport_ipv4
.addr
, INTERNAL_IPV4
);
636 ec
->orport_ipv4
.port
= VALID_PORT
;
637 tor_addr_parse(&ec
->orport_ipv6
.addr
, INTERNAL_IPV6
);
638 ec
->orport_ipv6
.port
= VALID_PORT
;
639 fake_options
->ExtendAllowPrivateAddresses
= 1;
641 tor_capture_bugs_(1);
642 tt_int_op(circuit_extend_lspec_valid_helper(ec
, circ
), OP_EQ
, -1);
643 tt_int_op(smartlist_len(tor_get_captured_bug_log_()), OP_EQ
, 1);
644 tt_str_op(smartlist_get(tor_get_captured_bug_log_(), 0), OP_EQ
,
645 "!(ASSERT_PREDICT_UNLIKELY_(circ->magic != 0x98ABC04Fu))");
646 tor_end_capture_bugs_();
647 mock_clean_saved_logs();
648 fake_options
->ExtendAllowPrivateAddresses
= 0;
649 tor_addr_port_make_null_ap(&ec
->orport_ipv4
, AF_INET
);
650 tor_addr_port_make_null_ap(&ec
->orport_ipv6
, AF_INET6
);
652 /* Fail again, but this time only set an IPv4 address. */
653 tor_addr_parse(&ec
->orport_ipv4
.addr
, INTERNAL_IPV4
);
654 ec
->orport_ipv4
.port
= VALID_PORT
;
655 fake_options
->ExtendAllowPrivateAddresses
= 1;
656 tor_capture_bugs_(1);
657 tt_int_op(circuit_extend_lspec_valid_helper(ec
, circ
), OP_EQ
, -1);
658 /* Since we're using IF_BUG_ONCE(), expect 0-1 bug logs */
659 tt_int_op(smartlist_len(tor_get_captured_bug_log_()), OP_GE
, 0);
660 tt_int_op(smartlist_len(tor_get_captured_bug_log_()), OP_LE
, 1);
661 tor_end_capture_bugs_();
662 mock_clean_saved_logs();
663 fake_options
->ExtendAllowPrivateAddresses
= 0;
664 #endif /* !defined(ALL_BUGS_ARE_FATAL) */
666 /* Now set the right magic */
667 or_circ
->base_
.magic
= OR_CIRCUIT_MAGIC
;
669 #ifndef ALL_BUGS_ARE_FATAL
670 /* If we pass the OR circuit magic check, but don't have p_chan,
671 * we trigger another bug */
672 fake_options
->ExtendAllowPrivateAddresses
= 1;
673 tor_capture_bugs_(1);
674 tt_int_op(circuit_extend_lspec_valid_helper(ec
, circ
), OP_EQ
, -1);
675 tt_int_op(smartlist_len(tor_get_captured_bug_log_()), OP_EQ
, 1);
676 tt_str_op(smartlist_get(tor_get_captured_bug_log_(), 0), OP_EQ
,
677 "!(ASSERT_PREDICT_UNLIKELY_(!p_chan))");
678 tor_end_capture_bugs_();
679 mock_clean_saved_logs();
680 fake_options
->ExtendAllowPrivateAddresses
= 0;
682 /* We can also pass the OR circuit magic check with a public address */
683 tor_addr_parse(&ec
->orport_ipv4
.addr
, PUBLIC_IPV4
);
684 fake_options
->ExtendAllowPrivateAddresses
= 0;
685 tor_capture_bugs_(1);
686 tt_int_op(circuit_extend_lspec_valid_helper(ec
, circ
), OP_EQ
, -1);
687 /* Since we're using IF_BUG_ONCE(), expect 0-1 bug logs */
688 tt_int_op(smartlist_len(tor_get_captured_bug_log_()), OP_GE
, 0);
689 tt_int_op(smartlist_len(tor_get_captured_bug_log_()), OP_LE
, 1);
690 tor_end_capture_bugs_();
691 mock_clean_saved_logs();
692 fake_options
->ExtendAllowPrivateAddresses
= 0;
694 tor_addr_make_null(&ec
->orport_ipv4
.addr
, AF_INET
);
695 ec
->orport_ipv4
.port
= 0x0000;
696 #endif /* !defined(ALL_BUGS_ARE_FATAL) */
698 /* Now let's fake a p_chan and the addresses */
699 tor_addr_parse(&ec
->orport_ipv4
.addr
, PUBLIC_IPV4
);
700 ec
->orport_ipv4
.port
= VALID_PORT
;
701 or_circ
->p_chan
= p_chan
;
703 /* This is a trivial failure: node_id and p_chan->identity_digest are both
705 tt_int_op(circuit_extend_lspec_valid_helper(ec
, circ
), OP_EQ
, -1);
706 expect_log_msg("Client asked me to extend back to the previous hop.\n");
707 mock_clean_saved_logs();
709 /* Let's check with non-zero identities as well */
710 memset(ec
->node_id
, 0xAA, sizeof(ec
->node_id
));
711 memset(p_chan
->identity_digest
, 0xAA, sizeof(p_chan
->identity_digest
));
713 tt_int_op(circuit_extend_lspec_valid_helper(ec
, circ
), OP_EQ
, -1);
714 expect_log_msg("Client asked me to extend back to the previous hop.\n");
715 mock_clean_saved_logs();
717 memset(ec
->node_id
, 0, sizeof(ec
->node_id
));
718 memset(p_chan
->identity_digest
, 0, sizeof(p_chan
->identity_digest
));
720 /* Let's pass the node_id test */
721 memset(ec
->node_id
, 0xAA, sizeof(ec
->node_id
));
722 memset(p_chan
->identity_digest
, 0xBB, sizeof(p_chan
->identity_digest
));
724 /* ed_pubkey is zero, and that's allowed, so we should succeed */
725 tt_int_op(circuit_extend_lspec_valid_helper(ec
, circ
), OP_EQ
, 0);
726 mock_clean_saved_logs();
728 /* Now let's check that we warn, but succeed, when only one address is
730 tor_addr_parse(&ec
->orport_ipv4
.addr
, INTERNAL_IPV4
);
731 ec
->orport_ipv4
.port
= VALID_PORT
;
732 tor_addr_parse(&ec
->orport_ipv6
.addr
, PUBLIC_IPV6
);
733 ec
->orport_ipv6
.port
= VALID_PORT
;
734 fake_options
->ExtendAllowPrivateAddresses
= 0;
736 tt_int_op(circuit_extend_lspec_valid_helper(ec
, circ
), OP_EQ
, 0);
737 expect_log_msg("Client asked me to extend "
738 "to a private IPv4 address '0.0.0.1'.\n");
739 mock_clean_saved_logs();
740 tor_addr_port_make_null_ap(&ec
->orport_ipv4
, AF_INET
);
741 tor_addr_port_make_null_ap(&ec
->orport_ipv6
, AF_INET6
);
743 /* Now with private IPv6 */
744 tor_addr_parse(&ec
->orport_ipv4
.addr
, PUBLIC_IPV4
);
745 ec
->orport_ipv4
.port
= VALID_PORT
;
746 tor_addr_parse(&ec
->orport_ipv6
.addr
, INTERNAL_IPV6
);
747 ec
->orport_ipv6
.port
= VALID_PORT
;
748 fake_options
->ExtendAllowPrivateAddresses
= 0;
750 tt_int_op(circuit_extend_lspec_valid_helper(ec
, circ
), OP_EQ
, 0);
751 expect_log_msg("Client asked me to extend "
752 "to a private IPv6 address '[::1]'.\n");
753 mock_clean_saved_logs();
754 tor_addr_port_make_null_ap(&ec
->orport_ipv4
, AF_INET
);
755 tor_addr_port_make_null_ap(&ec
->orport_ipv6
, AF_INET6
);
757 /* Now reset to public IPv4 and IPv6 */
758 tor_addr_parse(&ec
->orport_ipv4
.addr
, PUBLIC_IPV4
);
759 ec
->orport_ipv4
.port
= VALID_PORT
;
760 tor_addr_parse(&ec
->orport_ipv6
.addr
, PUBLIC_IPV6
);
761 ec
->orport_ipv6
.port
= VALID_PORT
;
763 /* Fail on matching non-zero identities */
764 memset(&ec
->ed_pubkey
, 0xEE, sizeof(ec
->ed_pubkey
));
765 memset(&p_chan
->ed25519_identity
, 0xEE, sizeof(p_chan
->ed25519_identity
));
767 tt_int_op(circuit_extend_lspec_valid_helper(ec
, circ
), OP_EQ
, -1);
768 expect_log_msg("Client asked me to extend back to the previous hop "
769 "(by Ed25519 ID).\n");
770 mock_clean_saved_logs();
772 memset(&ec
->ed_pubkey
, 0, sizeof(ec
->ed_pubkey
));
773 memset(&p_chan
->ed25519_identity
, 0, sizeof(p_chan
->ed25519_identity
));
775 /* Succeed on different, non-zero identities */
776 memset(&ec
->ed_pubkey
, 0xDD, sizeof(ec
->ed_pubkey
));
777 memset(&p_chan
->ed25519_identity
, 0xEE, sizeof(p_chan
->ed25519_identity
));
779 tt_int_op(circuit_extend_lspec_valid_helper(ec
, circ
), OP_EQ
, 0);
780 mock_clean_saved_logs();
782 memset(&ec
->ed_pubkey
, 0, sizeof(ec
->ed_pubkey
));
783 memset(&p_chan
->ed25519_identity
, 0, sizeof(p_chan
->ed25519_identity
));
785 /* Succeed if the client knows the identity, but we don't */
786 memset(&ec
->ed_pubkey
, 0xDD, sizeof(ec
->ed_pubkey
));
787 memset(&p_chan
->ed25519_identity
, 0x00, sizeof(p_chan
->ed25519_identity
));
789 tt_int_op(circuit_extend_lspec_valid_helper(ec
, circ
), OP_EQ
, 0);
790 mock_clean_saved_logs();
792 memset(&ec
->ed_pubkey
, 0, sizeof(ec
->ed_pubkey
));
793 memset(&p_chan
->ed25519_identity
, 0, sizeof(p_chan
->ed25519_identity
));
795 /* Succeed if we know the identity, but the client doesn't */
796 memset(&ec
->ed_pubkey
, 0x00, sizeof(ec
->ed_pubkey
));
797 memset(&p_chan
->ed25519_identity
, 0xEE, sizeof(p_chan
->ed25519_identity
));
799 tt_int_op(circuit_extend_lspec_valid_helper(ec
, circ
), OP_EQ
, 0);
800 mock_clean_saved_logs();
802 memset(&ec
->ed_pubkey
, 0, sizeof(ec
->ed_pubkey
));
803 memset(&p_chan
->ed25519_identity
, 0, sizeof(p_chan
->ed25519_identity
));
805 /* Cleanup the node ids */
806 memset(ec
->node_id
, 0, sizeof(ec
->node_id
));
807 memset(p_chan
->identity_digest
, 0, sizeof(p_chan
->identity_digest
));
809 /* Cleanup the p_chan and the addresses */
810 tor_addr_make_null(&ec
->orport_ipv4
.addr
, AF_UNSPEC
);
811 ec
->orport_ipv4
.port
= 0;
812 or_circ
->p_chan
= NULL
;
815 tor_end_capture_bugs_();
816 teardown_capture_of_logs();
819 or_options_free(fake_options
);
820 mocked_options
= NULL
;
827 #define NODE_SET_IPV4(node, ipv4_addr_str, ipv4_port) { \
828 tor_addr_parse(&node->ri->ipv4_addr, ipv4_addr_str); \
829 node->ri->ipv4_orport = ipv4_port; \
832 #define NODE_CLEAR_IPV4(node) { \
833 tor_addr_make_unspec(&node->ri->ipv4_addr); \
834 node->ri->ipv4_orport = 0; \
837 #define NODE_SET_IPV6(node, ipv6_addr_str, ipv6_port) { \
838 tor_addr_parse(&node->ri->ipv6_addr, ipv6_addr_str); \
839 node->ri->ipv6_orport = ipv6_port; \
842 /* Test the different cases in circuit_extend_add_ed25519_helper(). */
844 test_circuit_extend_add_ip(void *arg
)
848 extend_cell_t
*ec
= tor_malloc_zero(sizeof(extend_cell_t
));
849 extend_cell_t
*old_ec
= tor_malloc_zero(sizeof(extend_cell_t
));
851 node_t
*fake_node
= tor_malloc_zero(sizeof(node_t
));
852 routerinfo_t
*ri
= tor_malloc_zero(sizeof(routerinfo_t
));
854 MOCK(node_get_by_id
, mock_node_get_by_id
);
856 /* Set up the fake variables for the IPv4 test */
858 mocked_node
= fake_node
;
859 memset(ec
->node_id
, 0xAA, sizeof(ec
->node_id
));
860 memcpy(old_ec
, ec
, sizeof(extend_cell_t
));
861 NODE_SET_IPV4(fake_node
, PUBLIC_IPV4
, VALID_PORT
);
863 /* Do the IPv4 test */
864 tt_int_op(circuit_extend_add_ipv4_helper(ec
), OP_EQ
, 0);
865 tor_addr_copy(&ipv4_tmp
, &fake_node
->ri
->ipv4_addr
);
866 /* The IPv4 should match */
867 tt_int_op(tor_addr_compare(&ec
->orport_ipv4
.addr
, &ipv4_tmp
, CMP_SEMANTIC
),
869 tt_int_op(ec
->orport_ipv4
.port
, OP_EQ
, VALID_PORT
);
871 /* Set up the fake variables for the IPv6 test */
872 memcpy(ec
, old_ec
, sizeof(extend_cell_t
));
873 NODE_CLEAR_IPV4(fake_node
);
874 NODE_SET_IPV6(fake_node
, PUBLIC_IPV6
, VALID_PORT
);
876 /* Do the IPv6 test */
877 tt_int_op(circuit_extend_add_ipv6_helper(ec
), OP_EQ
, 0);
878 /* The IPv6 should match */
879 tt_int_op(tor_addr_compare(&ec
->orport_ipv6
.addr
, &fake_node
->ri
->ipv6_addr
,
880 CMP_SEMANTIC
), OP_EQ
, 0);
881 tt_int_op(ec
->orport_ipv6
.port
, OP_EQ
, VALID_PORT
);
887 UNMOCK(node_get_by_id
);
896 static bool can_extend_over_ipv6_result
= false;
897 static int mock_router_can_extend_over_ipv6_calls
= 0;
899 mock_router_can_extend_over_ipv6(const or_options_t
*options
)
902 mock_router_can_extend_over_ipv6_calls
++;
903 return can_extend_over_ipv6_result
;
906 /* Test the different cases in circuit_choose_ip_ap_for_extend(). */
908 test_circuit_choose_ip_ap_for_extend(void *arg
)
911 tor_addr_port_t ipv4_ap
;
912 tor_addr_port_t ipv6_ap
;
914 /* Set up valid addresses */
915 tor_addr_parse(&ipv4_ap
.addr
, PUBLIC_IPV4
);
916 ipv4_ap
.port
= VALID_PORT
;
917 tor_addr_parse(&ipv6_ap
.addr
, PUBLIC_IPV6
);
918 ipv6_ap
.port
= VALID_PORT
;
920 or_options_t
*fake_options
= options_new();
921 MOCK(get_options
, mock_get_options
);
922 mocked_options
= fake_options
;
924 MOCK(router_can_extend_over_ipv6
,
925 mock_router_can_extend_over_ipv6
);
926 can_extend_over_ipv6_result
= true;
927 mock_router_can_extend_over_ipv6_calls
= 0;
929 /* No valid addresses */
930 can_extend_over_ipv6_result
= true;
931 mock_router_can_extend_over_ipv6_calls
= 0;
932 tt_ptr_op(circuit_choose_ip_ap_for_extend(NULL
, NULL
), OP_EQ
, NULL
);
933 tt_int_op(mock_router_can_extend_over_ipv6_calls
, OP_EQ
, 1);
935 can_extend_over_ipv6_result
= false;
936 mock_router_can_extend_over_ipv6_calls
= 0;
937 tt_ptr_op(circuit_choose_ip_ap_for_extend(NULL
, NULL
), OP_EQ
, NULL
);
938 tt_int_op(mock_router_can_extend_over_ipv6_calls
, OP_EQ
, 1);
940 /* One valid address: IPv4 */
941 can_extend_over_ipv6_result
= true;
942 mock_router_can_extend_over_ipv6_calls
= 0;
943 tt_ptr_op(circuit_choose_ip_ap_for_extend(&ipv4_ap
, NULL
), OP_EQ
, &ipv4_ap
);
944 tt_int_op(mock_router_can_extend_over_ipv6_calls
, OP_EQ
, 1);
946 can_extend_over_ipv6_result
= false;
947 mock_router_can_extend_over_ipv6_calls
= 0;
948 tt_ptr_op(circuit_choose_ip_ap_for_extend(&ipv4_ap
, NULL
), OP_EQ
, &ipv4_ap
);
949 tt_int_op(mock_router_can_extend_over_ipv6_calls
, OP_EQ
, 1);
951 /* One valid address: IPv6 */
952 can_extend_over_ipv6_result
= true;
953 mock_router_can_extend_over_ipv6_calls
= 0;
954 tt_ptr_op(circuit_choose_ip_ap_for_extend(NULL
, &ipv6_ap
), OP_EQ
, &ipv6_ap
);
955 tt_int_op(mock_router_can_extend_over_ipv6_calls
, OP_EQ
, 1);
957 can_extend_over_ipv6_result
= false;
958 mock_router_can_extend_over_ipv6_calls
= 0;
959 tt_ptr_op(circuit_choose_ip_ap_for_extend(NULL
, &ipv6_ap
), OP_EQ
, NULL
);
960 tt_int_op(mock_router_can_extend_over_ipv6_calls
, OP_EQ
, 1);
962 /* Two valid addresses */
963 const tor_addr_port_t
*chosen_addr
= NULL
;
965 can_extend_over_ipv6_result
= true;
966 mock_router_can_extend_over_ipv6_calls
= 0;
967 chosen_addr
= circuit_choose_ip_ap_for_extend(&ipv4_ap
, &ipv6_ap
);
968 tt_assert(chosen_addr
== &ipv4_ap
|| chosen_addr
== &ipv6_ap
);
969 tt_int_op(mock_router_can_extend_over_ipv6_calls
, OP_EQ
, 1);
971 can_extend_over_ipv6_result
= false;
972 mock_router_can_extend_over_ipv6_calls
= 0;
973 tt_ptr_op(circuit_choose_ip_ap_for_extend(&ipv4_ap
, &ipv6_ap
),
975 tt_int_op(mock_router_can_extend_over_ipv6_calls
, OP_EQ
, 1);
979 or_options_free(fake_options
);
980 mocked_options
= NULL
;
982 UNMOCK(router_can_extend_over_ipv6
);
984 tor_free(fake_options
);
987 static int mock_circuit_close_calls
= 0;
989 mock_circuit_mark_for_close_(circuit_t
*circ
, int reason
,
990 int line
, const char *cfile
)
996 mock_circuit_close_calls
++;
999 static int mock_channel_connect_calls
= 0;
1000 static channel_t
*mock_channel_connect_nchan
= NULL
;
1002 mock_channel_connect_for_circuit(const extend_info_t
*ei
)
1005 mock_channel_connect_calls
++;
1006 return mock_channel_connect_nchan
;
1009 /* Test the different cases in circuit_open_connection_for_extend().
1010 * Chooses different IP addresses depending on the first character in arg:
1013 * - d: IPv4 and IPv6 (dual-stack)
1016 test_circuit_open_connection_for_extend(void *arg
)
1018 const char ip_version
= ((const char *)arg
)[0];
1019 const bool use_ipv4
= (ip_version
== '4' || ip_version
== 'd');
1020 const bool use_ipv6
= (ip_version
== '6' || ip_version
== 'd');
1021 tor_assert(use_ipv4
|| use_ipv6
);
1023 extend_cell_t
*ec
= tor_malloc_zero(sizeof(extend_cell_t
));
1024 circuit_t
*circ
= tor_malloc_zero(sizeof(circuit_t
));
1025 channel_t
*fake_n_chan
= tor_malloc_zero(sizeof(channel_t
));
1027 or_options_t
*fake_options
= options_new();
1028 MOCK(get_options
, mock_get_options
);
1029 mocked_options
= fake_options
;
1031 MOCK(circuit_mark_for_close_
, mock_circuit_mark_for_close_
);
1032 mock_circuit_close_calls
= 0;
1033 MOCK(channel_connect_for_circuit
, mock_channel_connect_for_circuit
);
1034 mock_channel_connect_calls
= 0;
1035 mock_channel_connect_nchan
= NULL
;
1037 MOCK(router_can_extend_over_ipv6
,
1038 mock_router_can_extend_over_ipv6
);
1039 can_extend_over_ipv6_result
= true;
1041 setup_full_capture_of_logs(LOG_INFO
);
1043 #ifndef ALL_BUGS_ARE_FATAL
1044 /* Circuit must be non-NULL */
1045 mock_circuit_close_calls
= 0;
1046 mock_channel_connect_calls
= 0;
1047 tor_capture_bugs_(1);
1048 circuit_open_connection_for_extend(ec
, NULL
, 0);
1049 /* We can't close a NULL circuit */
1050 tt_int_op(mock_circuit_close_calls
, OP_EQ
, 0);
1051 tt_int_op(mock_channel_connect_calls
, OP_EQ
, 0);
1052 tt_int_op(smartlist_len(tor_get_captured_bug_log_()), OP_EQ
, 1);
1053 tt_str_op(smartlist_get(tor_get_captured_bug_log_(), 0), OP_EQ
,
1054 "!(ASSERT_PREDICT_UNLIKELY_(!circ))");
1055 tor_end_capture_bugs_();
1056 mock_clean_saved_logs();
1058 /* Extend cell must be non-NULL */
1059 mock_circuit_close_calls
= 0;
1060 mock_channel_connect_calls
= 0;
1061 tor_capture_bugs_(1);
1062 circuit_open_connection_for_extend(NULL
, circ
, 0);
1063 tt_int_op(mock_circuit_close_calls
, OP_EQ
, 1);
1064 tt_int_op(mock_channel_connect_calls
, OP_EQ
, 0);
1065 tt_int_op(smartlist_len(tor_get_captured_bug_log_()), OP_EQ
, 1);
1066 tt_str_op(smartlist_get(tor_get_captured_bug_log_(), 0), OP_EQ
,
1067 "!(ASSERT_PREDICT_UNLIKELY_(!ec))");
1068 tor_end_capture_bugs_();
1069 mock_clean_saved_logs();
1071 /* Extend cell and circuit must be non-NULL */
1072 mock_circuit_close_calls
= 0;
1073 mock_channel_connect_calls
= 0;
1074 tor_capture_bugs_(1);
1075 circuit_open_connection_for_extend(NULL
, NULL
, 0);
1076 /* We can't close a NULL circuit */
1077 tt_int_op(mock_circuit_close_calls
, OP_EQ
, 0);
1078 tt_int_op(mock_channel_connect_calls
, OP_EQ
, 0);
1079 /* Since we're using IF_BUG_ONCE(), we might not log any bugs */
1080 tt_int_op(smartlist_len(tor_get_captured_bug_log_()), OP_GE
, 0);
1081 tt_int_op(smartlist_len(tor_get_captured_bug_log_()), OP_LE
, 2);
1082 tor_end_capture_bugs_();
1083 mock_clean_saved_logs();
1085 /* Fail, because neither address is valid */
1086 mock_circuit_close_calls
= 0;
1087 mock_channel_connect_calls
= 0;
1088 tor_capture_bugs_(1);
1089 circuit_open_connection_for_extend(ec
, circ
, 0);
1090 /* Close the circuit, don't connect */
1091 tt_int_op(mock_circuit_close_calls
, OP_EQ
, 1);
1092 tt_int_op(mock_channel_connect_calls
, OP_EQ
, 0);
1094 tt_ptr_op(circ
->n_hop
, OP_EQ
, NULL
);
1095 tt_ptr_op(circ
->n_chan_create_cell
, OP_EQ
, NULL
);
1096 tt_int_op(circ
->state
, OP_EQ
, 0);
1098 tor_end_capture_bugs_();
1099 mock_clean_saved_logs();
1100 #endif /* !defined(ALL_BUGS_ARE_FATAL) */
1102 /* Set up valid addresses */
1104 tor_addr_parse(&ec
->orport_ipv4
.addr
, PUBLIC_IPV4
);
1105 ec
->orport_ipv4
.port
= VALID_PORT
;
1108 tor_addr_parse(&ec
->orport_ipv6
.addr
, PUBLIC_IPV6
);
1109 ec
->orport_ipv6
.port
= VALID_PORT
;
1112 /* Succeed, but don't try to open a connection */
1113 mock_circuit_close_calls
= 0;
1114 mock_channel_connect_calls
= 0;
1115 circuit_open_connection_for_extend(ec
, circ
, 0);
1116 /* If we haven't closed the circuit, that's success */
1117 tt_int_op(mock_circuit_close_calls
, OP_EQ
, 0);
1118 tt_int_op(mock_channel_connect_calls
, OP_EQ
, 0);
1120 tt_ptr_op(circ
->n_hop
, OP_NE
, NULL
);
1121 tt_ptr_op(circ
->n_chan_create_cell
, OP_NE
, NULL
);
1122 tt_int_op(circ
->state
, OP_EQ
, CIRCUIT_STATE_CHAN_WAIT
);
1124 mock_clean_saved_logs();
1125 tor_free(circ
->n_hop
);
1126 tor_free(circ
->n_chan_create_cell
);
1129 /* Try to open a connection, but fail with a NULL n_chan */
1130 mock_circuit_close_calls
= 0;
1131 mock_channel_connect_calls
= 0;
1132 circuit_open_connection_for_extend(ec
, circ
, 1);
1133 /* Try to connect, but fail, and close the circuit */
1134 tt_int_op(mock_circuit_close_calls
, OP_EQ
, 1);
1135 tt_int_op(mock_channel_connect_calls
, OP_EQ
, 1);
1136 expect_log_msg("Launching n_chan failed. Closing circuit.\n");
1138 tt_ptr_op(circ
->n_hop
, OP_NE
, NULL
);
1139 tt_ptr_op(circ
->n_chan_create_cell
, OP_NE
, NULL
);
1140 tt_int_op(circ
->state
, OP_EQ
, CIRCUIT_STATE_CHAN_WAIT
);
1142 mock_clean_saved_logs();
1143 tor_free(circ
->n_hop
);
1144 tor_free(circ
->n_chan_create_cell
);
1147 /* Try to open a connection, and succeed, because n_chan is not NULL */
1148 mock_channel_connect_nchan
= fake_n_chan
;
1149 mock_circuit_close_calls
= 0;
1150 mock_channel_connect_calls
= 0;
1151 circuit_open_connection_for_extend(ec
, circ
, 1);
1152 /* Connection attempt succeeded, leaving the circuit open */
1153 tt_int_op(mock_circuit_close_calls
, OP_EQ
, 0);
1154 tt_int_op(mock_channel_connect_calls
, OP_EQ
, 1);
1156 tt_ptr_op(circ
->n_hop
, OP_NE
, NULL
);
1157 tt_ptr_op(circ
->n_chan_create_cell
, OP_NE
, NULL
);
1158 tt_int_op(circ
->state
, OP_EQ
, CIRCUIT_STATE_CHAN_WAIT
);
1160 mock_clean_saved_logs();
1161 tor_free(circ
->n_hop
);
1162 tor_free(circ
->n_chan_create_cell
);
1164 mock_channel_connect_nchan
= NULL
;
1167 tor_end_capture_bugs_();
1168 teardown_capture_of_logs();
1170 UNMOCK(circuit_mark_for_close_
);
1171 mock_circuit_close_calls
= 0;
1172 UNMOCK(channel_connect_for_circuit
);
1173 mock_channel_connect_calls
= 0;
1175 UNMOCK(get_options
);
1176 or_options_free(fake_options
);
1177 mocked_options
= NULL
;
1179 UNMOCK(router_can_extend_over_ipv6
);
1182 tor_free(circ
->n_hop
);
1183 tor_free(circ
->n_chan_create_cell
);
1185 tor_free(fake_n_chan
);
1188 /* Guaranteed to be initialised to zero. */
1189 static extend_cell_t mock_extend_cell_parse_cell_out
;
1190 static int mock_extend_cell_parse_result
= 0;
1191 static int mock_extend_cell_parse_calls
= 0;
1194 mock_extend_cell_parse(extend_cell_t
*cell_out
,
1195 const uint8_t command
,
1196 const uint8_t *payload_in
,
1203 mock_extend_cell_parse_calls
++;
1204 memcpy(cell_out
, &mock_extend_cell_parse_cell_out
,
1205 sizeof(extend_cell_t
));
1206 return mock_extend_cell_parse_result
;
1209 static int mock_channel_get_for_extend_calls
= 0;
1210 static int mock_channel_get_for_extend_launch_out
= 0;
1211 static channel_t
*mock_channel_get_for_extend_nchan
= NULL
;
1213 mock_channel_get_for_extend(const char *rsa_id_digest
,
1214 const ed25519_public_key_t
*ed_id
,
1215 const tor_addr_t
*target_ipv4_addr
,
1216 const tor_addr_t
*target_ipv6_addr
,
1217 bool for_origin_circ
,
1218 const char **msg_out
,
1221 (void)rsa_id_digest
;
1223 (void)target_ipv4_addr
;
1224 (void)target_ipv6_addr
;
1225 (void)for_origin_circ
;
1227 /* channel_get_for_extend() requires non-NULL arguments */
1228 tt_ptr_op(msg_out
, OP_NE
, NULL
);
1229 tt_ptr_op(launch_out
, OP_NE
, NULL
);
1231 mock_channel_get_for_extend_calls
++;
1233 *launch_out
= mock_channel_get_for_extend_launch_out
;
1234 return mock_channel_get_for_extend_nchan
;
1241 mock_channel_get_canonical_remote_descr(channel_t
*chan
)
1244 return "mock_channel_get_canonical_remote_descr()";
1247 /* Should mock_circuit_deliver_create_cell() expect a direct connection? */
1248 static bool mock_circuit_deliver_create_cell_expect_direct
= false;
1249 static int mock_circuit_deliver_create_cell_calls
= 0;
1250 static int mock_circuit_deliver_create_cell_result
= 0;
1252 mock_circuit_deliver_create_cell(circuit_t
*circ
,
1253 const struct create_cell_t
*create_cell
,
1258 /* circuit_deliver_create_cell() requires non-NULL arguments,
1259 * but we only check circ and circ->n_chan here. */
1260 tt_ptr_op(circ
, OP_NE
, NULL
);
1261 /* We expect n_chan for relayed cells. But should we also expect it for
1262 * direct connections? */
1263 if (!mock_circuit_deliver_create_cell_expect_direct
)
1264 tt_ptr_op(circ
->n_chan
, OP_NE
, NULL
);
1266 /* We should only ever get relayed cells from extends */
1267 tt_int_op(relayed
, OP_EQ
, !mock_circuit_deliver_create_cell_expect_direct
);
1269 mock_circuit_deliver_create_cell_calls
++;
1270 return mock_circuit_deliver_create_cell_result
;
1276 /* Test the different cases in circuit_extend(). */
1278 test_circuit_extend(void *arg
)
1281 cell_t
*cell
= tor_malloc_zero(sizeof(cell_t
));
1282 channel_t
*p_chan
= tor_malloc_zero(sizeof(channel_t
));
1283 or_circuit_t
*or_circ
= tor_malloc_zero(sizeof(or_circuit_t
));
1284 circuit_t
*circ
= TO_CIRCUIT(or_circ
);
1285 channel_t
*fake_n_chan
= tor_malloc_zero(sizeof(channel_t
));
1288 MOCK(server_mode
, mock_server_mode
);
1290 /* Mock a debug function, but otherwise ignore it */
1291 MOCK(channel_describe_peer
,
1292 mock_channel_get_canonical_remote_descr
);
1294 setup_full_capture_of_logs(LOG_INFO
);
1296 #ifndef ALL_BUGS_ARE_FATAL
1297 /* Circuit must be non-NULL */
1298 tor_capture_bugs_(1);
1299 tt_int_op(circuit_extend(cell
, NULL
), OP_EQ
, -1);
1300 tt_int_op(smartlist_len(tor_get_captured_bug_log_()), OP_EQ
, 1);
1301 tt_str_op(smartlist_get(tor_get_captured_bug_log_(), 0), OP_EQ
,
1302 "!(ASSERT_PREDICT_UNLIKELY_(!circ))");
1303 tor_end_capture_bugs_();
1304 mock_clean_saved_logs();
1306 /* Cell must be non-NULL */
1307 tor_capture_bugs_(1);
1308 tt_int_op(circuit_extend(NULL
, circ
), OP_EQ
, -1);
1309 tt_int_op(smartlist_len(tor_get_captured_bug_log_()), OP_EQ
, 1);
1310 tt_str_op(smartlist_get(tor_get_captured_bug_log_(), 0), OP_EQ
,
1311 "!(ASSERT_PREDICT_UNLIKELY_(!cell))");
1312 tor_end_capture_bugs_();
1313 mock_clean_saved_logs();
1315 /* Extend cell and circuit must be non-NULL */
1316 tor_capture_bugs_(1);
1317 tt_int_op(circuit_extend(NULL
, NULL
), OP_EQ
, -1);
1318 /* Since we're using IF_BUG_ONCE(), we might not log any bugs */
1319 tt_int_op(smartlist_len(tor_get_captured_bug_log_()), OP_GE
, 0);
1320 tt_int_op(smartlist_len(tor_get_captured_bug_log_()), OP_LE
, 2);
1321 tor_end_capture_bugs_();
1322 mock_clean_saved_logs();
1323 #endif /* !defined(ALL_BUGS_ARE_FATAL) */
1325 /* Clients can't extend */
1327 tt_int_op(circuit_extend(cell
, circ
), OP_EQ
, -1);
1328 expect_log_msg("Got an extend cell, but running as a client. Closing.\n");
1329 mock_clean_saved_logs();
1331 /* But servers can. Unpack the cell, but fail parsing. */
1333 tt_int_op(circuit_extend(cell
, circ
), OP_EQ
, -1);
1334 expect_log_msg("Can't parse extend cell. Closing circuit.\n");
1335 mock_clean_saved_logs();
1337 /* Now mock parsing */
1338 MOCK(extend_cell_parse
, mock_extend_cell_parse
);
1340 /* And make parsing succeed, but fail on adding ed25519 */
1341 memset(&mock_extend_cell_parse_cell_out
, 0,
1342 sizeof(mock_extend_cell_parse_cell_out
));
1343 mock_extend_cell_parse_result
= 0;
1344 mock_extend_cell_parse_calls
= 0;
1346 tt_int_op(circuit_extend(cell
, circ
), OP_EQ
, -1);
1347 tt_int_op(mock_extend_cell_parse_calls
, OP_EQ
, 1);
1349 "Client asked me to extend without specifying an id_digest.\n");
1350 mock_clean_saved_logs();
1351 mock_extend_cell_parse_calls
= 0;
1353 /* Now add a node_id. Fail the lspec check because IPv4 and port are zero. */
1354 memset(&mock_extend_cell_parse_cell_out
.node_id
, 0xAA,
1355 sizeof(mock_extend_cell_parse_cell_out
.node_id
));
1357 tt_int_op(circuit_extend(cell
, circ
), OP_EQ
, -1);
1358 tt_int_op(mock_extend_cell_parse_calls
, OP_EQ
, 1);
1359 expect_log_msg("Client asked me to extend to a zero destination port "
1360 "or unspecified address '[scrubbed]'.\n");
1361 mock_clean_saved_logs();
1362 mock_extend_cell_parse_calls
= 0;
1364 /* Now add a valid IPv4 and port. Fail the OR circuit magic check. */
1365 tor_addr_parse(&mock_extend_cell_parse_cell_out
.orport_ipv4
.addr
,
1367 mock_extend_cell_parse_cell_out
.orport_ipv4
.port
= VALID_PORT
;
1369 #ifndef ALL_BUGS_ARE_FATAL
1370 tor_capture_bugs_(1);
1371 tt_int_op(circuit_extend(cell
, circ
), OP_EQ
, -1);
1372 tt_int_op(mock_extend_cell_parse_calls
, OP_EQ
, 1);
1373 tt_int_op(smartlist_len(tor_get_captured_bug_log_()), OP_EQ
, 1);
1374 tt_str_op(smartlist_get(tor_get_captured_bug_log_(), 0), OP_EQ
,
1375 "!(ASSERT_PREDICT_UNLIKELY_(circ->magic != 0x98ABC04Fu))");
1376 tor_end_capture_bugs_();
1377 mock_clean_saved_logs();
1378 mock_extend_cell_parse_calls
= 0;
1379 #endif /* !defined(ALL_BUGS_ARE_FATAL) */
1381 /* Now add the right magic and a p_chan. */
1382 or_circ
->base_
.magic
= OR_CIRCUIT_MAGIC
;
1383 or_circ
->p_chan
= p_chan
;
1385 /* Mock channel_get_for_extend(), so it doesn't crash. */
1386 mock_channel_get_for_extend_calls
= 0;
1387 MOCK(channel_get_for_extend
, mock_channel_get_for_extend
);
1389 /* Test circuit not established, but don't launch another one */
1390 mock_channel_get_for_extend_launch_out
= 0;
1391 mock_channel_get_for_extend_nchan
= NULL
;
1392 tt_int_op(circuit_extend(cell
, circ
), OP_EQ
, 0);
1393 tt_int_op(mock_extend_cell_parse_calls
, OP_EQ
, 1);
1394 tt_int_op(mock_channel_get_for_extend_calls
, OP_EQ
, 1);
1397 mock_clean_saved_logs();
1398 mock_extend_cell_parse_calls
= 0;
1399 mock_channel_get_for_extend_calls
= 0;
1400 /* circ and or_circ are the same object */
1401 tor_free(circ
->n_hop
);
1402 tor_free(circ
->n_chan_create_cell
);
1404 /* Mock channel_connect_for_circuit(), so we don't crash */
1405 mock_channel_connect_calls
= 0;
1406 MOCK(channel_connect_for_circuit
, mock_channel_connect_for_circuit
);
1408 /* Test circuit not established, and successful launch of a channel */
1409 mock_channel_get_for_extend_launch_out
= 1;
1410 mock_channel_get_for_extend_nchan
= NULL
;
1411 mock_channel_connect_nchan
= fake_n_chan
;
1412 tt_int_op(circuit_extend(cell
, circ
), OP_EQ
, 0);
1413 tt_int_op(mock_extend_cell_parse_calls
, OP_EQ
, 1);
1414 tt_int_op(mock_channel_get_for_extend_calls
, OP_EQ
, 1);
1415 tt_int_op(mock_channel_connect_calls
, OP_EQ
, 1);
1418 mock_clean_saved_logs();
1419 mock_extend_cell_parse_calls
= 0;
1420 mock_channel_get_for_extend_calls
= 0;
1421 mock_channel_connect_calls
= 0;
1422 /* circ and or_circ are the same object */
1423 tor_free(circ
->n_hop
);
1424 tor_free(circ
->n_chan_create_cell
);
1426 /* Mock circuit_deliver_create_cell(), so it doesn't crash */
1427 mock_circuit_deliver_create_cell_calls
= 0;
1428 mock_circuit_deliver_create_cell_expect_direct
= false;
1429 MOCK(circuit_deliver_create_cell
, mock_circuit_deliver_create_cell
);
1431 /* Test circuit established, re-using channel, successful delivery */
1432 mock_channel_get_for_extend_launch_out
= 0;
1433 mock_channel_get_for_extend_nchan
= fake_n_chan
;
1434 mock_channel_connect_nchan
= NULL
;
1435 mock_circuit_deliver_create_cell_result
= 0;
1436 tt_int_op(circuit_extend(cell
, circ
), OP_EQ
, 0);
1437 tt_int_op(mock_extend_cell_parse_calls
, OP_EQ
, 1);
1438 tt_int_op(mock_channel_get_for_extend_calls
, OP_EQ
, 1);
1439 tt_int_op(mock_channel_connect_calls
, OP_EQ
, 0);
1440 tt_int_op(mock_circuit_deliver_create_cell_calls
, OP_EQ
, 1);
1441 tt_ptr_op(circ
->n_chan
, OP_EQ
, fake_n_chan
);
1444 circ
->n_chan
= NULL
;
1445 mock_clean_saved_logs();
1446 mock_extend_cell_parse_calls
= 0;
1447 mock_channel_get_for_extend_calls
= 0;
1448 mock_channel_connect_calls
= 0;
1449 mock_circuit_deliver_create_cell_calls
= 0;
1450 /* circ and or_circ are the same object */
1451 tor_free(circ
->n_hop
);
1452 tor_free(circ
->n_chan_create_cell
);
1454 /* Test circuit established, re-using channel, failed delivery */
1455 mock_channel_get_for_extend_launch_out
= 0;
1456 mock_channel_get_for_extend_nchan
= fake_n_chan
;
1457 mock_channel_connect_nchan
= NULL
;
1458 mock_circuit_deliver_create_cell_result
= -1;
1459 tt_int_op(circuit_extend(cell
, circ
), OP_EQ
, -1);
1460 tt_int_op(mock_extend_cell_parse_calls
, OP_EQ
, 1);
1461 tt_int_op(mock_channel_get_for_extend_calls
, OP_EQ
, 1);
1462 tt_int_op(mock_channel_connect_calls
, OP_EQ
, 0);
1463 tt_int_op(mock_circuit_deliver_create_cell_calls
, OP_EQ
, 1);
1464 tt_ptr_op(circ
->n_chan
, OP_EQ
, fake_n_chan
);
1467 circ
->n_chan
= NULL
;
1468 mock_clean_saved_logs();
1469 mock_extend_cell_parse_calls
= 0;
1470 mock_channel_get_for_extend_calls
= 0;
1471 mock_channel_connect_calls
= 0;
1472 mock_circuit_deliver_create_cell_calls
= 0;
1473 /* circ and or_circ are the same object */
1474 tor_free(circ
->n_hop
);
1475 tor_free(circ
->n_chan_create_cell
);
1478 tor_end_capture_bugs_();
1479 teardown_capture_of_logs();
1481 UNMOCK(server_mode
);
1484 UNMOCK(channel_describe_peer
);
1486 UNMOCK(extend_cell_parse
);
1487 memset(&mock_extend_cell_parse_cell_out
, 0,
1488 sizeof(mock_extend_cell_parse_cell_out
));
1489 mock_extend_cell_parse_result
= 0;
1490 mock_extend_cell_parse_calls
= 0;
1492 UNMOCK(channel_get_for_extend
);
1493 mock_channel_get_for_extend_calls
= 0;
1494 mock_channel_get_for_extend_launch_out
= 0;
1495 mock_channel_get_for_extend_nchan
= NULL
;
1497 UNMOCK(channel_connect_for_circuit
);
1498 mock_channel_connect_calls
= 0;
1499 mock_channel_connect_nchan
= NULL
;
1501 UNMOCK(circuit_deliver_create_cell
);
1502 mock_circuit_deliver_create_cell_calls
= 0;
1503 mock_circuit_deliver_create_cell_result
= 0;
1506 /* circ and or_circ are the same object */
1507 tor_free(circ
->n_hop
);
1508 tor_free(circ
->n_chan_create_cell
);
1511 tor_free(fake_n_chan
);
1514 /* Test the different cases in onionskin_answer(). */
1516 test_onionskin_answer(void *arg
)
1519 created_cell_t
*created_cell
= tor_malloc_zero(sizeof(created_cell_t
));
1520 or_circuit_t
*or_circ
= tor_malloc_zero(sizeof(or_circuit_t
));
1521 char keys
[CPATH_KEY_MATERIAL_LEN
] = {0};
1522 uint8_t rend_circ_nonce
[DIGEST_LEN
] = {0};
1524 setup_full_capture_of_logs(LOG_INFO
);
1526 #ifndef ALL_BUGS_ARE_FATAL
1527 /* Circuit must be non-NULL */
1528 tor_capture_bugs_(1);
1529 tt_int_op(onionskin_answer(NULL
, created_cell
,
1530 keys
, CPATH_KEY_MATERIAL_LEN
,
1531 rend_circ_nonce
), OP_EQ
, -1);
1532 tt_int_op(smartlist_len(tor_get_captured_bug_log_()), OP_EQ
, 1);
1533 tt_str_op(smartlist_get(tor_get_captured_bug_log_(), 0), OP_EQ
,
1534 "!(ASSERT_PREDICT_UNLIKELY_(!circ))");
1535 tor_end_capture_bugs_();
1536 mock_clean_saved_logs();
1538 /* Created cell must be non-NULL */
1539 tor_capture_bugs_(1);
1540 tt_int_op(onionskin_answer(or_circ
, NULL
,
1541 keys
, CPATH_KEY_MATERIAL_LEN
,
1542 rend_circ_nonce
), OP_EQ
, -1);
1543 tt_int_op(smartlist_len(tor_get_captured_bug_log_()), OP_EQ
, 1);
1544 tt_str_op(smartlist_get(tor_get_captured_bug_log_(), 0), OP_EQ
,
1545 "!(ASSERT_PREDICT_UNLIKELY_(!created_cell))");
1546 tor_end_capture_bugs_();
1547 mock_clean_saved_logs();
1549 /* Keys must be non-NULL */
1550 tor_capture_bugs_(1);
1551 tt_int_op(onionskin_answer(or_circ
, created_cell
,
1552 NULL
, CPATH_KEY_MATERIAL_LEN
,
1553 rend_circ_nonce
), OP_EQ
, -1);
1554 tt_int_op(smartlist_len(tor_get_captured_bug_log_()), OP_EQ
, 1);
1555 tt_str_op(smartlist_get(tor_get_captured_bug_log_(), 0), OP_EQ
,
1556 "!(ASSERT_PREDICT_UNLIKELY_(!keys))");
1557 tor_end_capture_bugs_();
1558 mock_clean_saved_logs();
1560 /* The rend circuit nonce must be non-NULL */
1561 tor_capture_bugs_(1);
1562 tt_int_op(onionskin_answer(or_circ
, created_cell
,
1563 keys
, CPATH_KEY_MATERIAL_LEN
,
1565 tt_int_op(smartlist_len(tor_get_captured_bug_log_()), OP_EQ
, 1);
1566 tt_str_op(smartlist_get(tor_get_captured_bug_log_(), 0), OP_EQ
,
1567 "!(ASSERT_PREDICT_UNLIKELY_(!rend_circ_nonce))");
1568 tor_end_capture_bugs_();
1569 mock_clean_saved_logs();
1570 #endif /* !defined(ALL_BUGS_ARE_FATAL) */
1572 /* Also, the keys length must be CPATH_KEY_MATERIAL_LEN, but we can't catch
1573 * asserts in unit tests. */
1575 /* Fail when formatting the created cell */
1576 tt_int_op(onionskin_answer(or_circ
, created_cell
,
1577 keys
, CPATH_KEY_MATERIAL_LEN
,
1578 rend_circ_nonce
), OP_EQ
, -1);
1579 expect_log_msg("couldn't format created cell (type=0, len=0).\n");
1580 mock_clean_saved_logs();
1582 /* TODO: test the rest of onionskin_answer(), see #33860 */
1583 /* TODO: mock created_cell_format for the next test */
1586 tor_end_capture_bugs_();
1587 teardown_capture_of_logs();
1589 tor_free(created_cell
);
1593 /* Test the different cases in origin_circuit_init(). */
1595 test_origin_circuit_init(void *arg
)
1598 origin_circuit_t
*origin_circ
= NULL
;
1600 /* Init with 0 purpose and 0 flags */
1601 origin_circ
= origin_circuit_init(0, 0);
1602 tt_int_op(origin_circ
->base_
.purpose
, OP_EQ
, 0);
1603 tt_int_op(origin_circ
->base_
.state
, OP_EQ
, CIRCUIT_STATE_CHAN_WAIT
);
1604 tt_ptr_op(origin_circ
->build_state
, OP_NE
, NULL
);
1605 tt_int_op(origin_circ
->build_state
->is_internal
, OP_EQ
, 0);
1606 tt_int_op(origin_circ
->build_state
->is_ipv6_selftest
, OP_EQ
, 0);
1607 tt_int_op(origin_circ
->build_state
->need_capacity
, OP_EQ
, 0);
1608 tt_int_op(origin_circ
->build_state
->need_uptime
, OP_EQ
, 0);
1609 tt_int_op(origin_circ
->build_state
->onehop_tunnel
, OP_EQ
, 0);
1610 /* The circuits are automatically freed by the circuitlist. */
1612 /* Init with a purpose */
1613 origin_circ
= origin_circuit_init(CIRCUIT_PURPOSE_C_GENERAL
, 0);
1614 tt_int_op(origin_circ
->base_
.purpose
, OP_EQ
, CIRCUIT_PURPOSE_C_GENERAL
);
1616 /* Init with each flag */
1617 origin_circ
= origin_circuit_init(0, CIRCLAUNCH_IS_INTERNAL
);
1618 tt_ptr_op(origin_circ
->build_state
, OP_NE
, NULL
);
1619 tt_int_op(origin_circ
->build_state
->is_internal
, OP_EQ
, 1);
1620 tt_int_op(origin_circ
->build_state
->is_ipv6_selftest
, OP_EQ
, 0);
1621 tt_int_op(origin_circ
->build_state
->need_capacity
, OP_EQ
, 0);
1622 tt_int_op(origin_circ
->build_state
->need_uptime
, OP_EQ
, 0);
1623 tt_int_op(origin_circ
->build_state
->onehop_tunnel
, OP_EQ
, 0);
1625 origin_circ
= origin_circuit_init(0, CIRCLAUNCH_IS_IPV6_SELFTEST
);
1626 tt_ptr_op(origin_circ
->build_state
, OP_NE
, NULL
);
1627 tt_int_op(origin_circ
->build_state
->is_internal
, OP_EQ
, 0);
1628 tt_int_op(origin_circ
->build_state
->is_ipv6_selftest
, OP_EQ
, 1);
1629 tt_int_op(origin_circ
->build_state
->need_capacity
, OP_EQ
, 0);
1630 tt_int_op(origin_circ
->build_state
->need_uptime
, OP_EQ
, 0);
1631 tt_int_op(origin_circ
->build_state
->onehop_tunnel
, OP_EQ
, 0);
1633 origin_circ
= origin_circuit_init(0, CIRCLAUNCH_NEED_CAPACITY
);
1634 tt_ptr_op(origin_circ
->build_state
, OP_NE
, NULL
);
1635 tt_int_op(origin_circ
->build_state
->is_internal
, OP_EQ
, 0);
1636 tt_int_op(origin_circ
->build_state
->is_ipv6_selftest
, OP_EQ
, 0);
1637 tt_int_op(origin_circ
->build_state
->need_capacity
, OP_EQ
, 1);
1638 tt_int_op(origin_circ
->build_state
->need_uptime
, OP_EQ
, 0);
1639 tt_int_op(origin_circ
->build_state
->onehop_tunnel
, OP_EQ
, 0);
1641 origin_circ
= origin_circuit_init(0, CIRCLAUNCH_NEED_UPTIME
);
1642 tt_ptr_op(origin_circ
->build_state
, OP_NE
, NULL
);
1643 tt_int_op(origin_circ
->build_state
->is_internal
, OP_EQ
, 0);
1644 tt_int_op(origin_circ
->build_state
->is_ipv6_selftest
, OP_EQ
, 0);
1645 tt_int_op(origin_circ
->build_state
->need_capacity
, OP_EQ
, 0);
1646 tt_int_op(origin_circ
->build_state
->need_uptime
, OP_EQ
, 1);
1647 tt_int_op(origin_circ
->build_state
->onehop_tunnel
, OP_EQ
, 0);
1649 origin_circ
= origin_circuit_init(0, CIRCLAUNCH_ONEHOP_TUNNEL
);
1650 tt_ptr_op(origin_circ
->build_state
, OP_NE
, NULL
);
1651 tt_int_op(origin_circ
->build_state
->is_internal
, OP_EQ
, 0);
1652 tt_int_op(origin_circ
->build_state
->is_ipv6_selftest
, OP_EQ
, 0);
1653 tt_int_op(origin_circ
->build_state
->need_capacity
, OP_EQ
, 0);
1654 tt_int_op(origin_circ
->build_state
->need_uptime
, OP_EQ
, 0);
1655 tt_int_op(origin_circ
->build_state
->onehop_tunnel
, OP_EQ
, 1);
1658 /* The circuits are automatically freed by the circuitlist. */
1662 /* Test the different cases in circuit_send_next_onion_skin(). */
1664 test_circuit_send_next_onion_skin(void *arg
)
1667 origin_circuit_t
*origin_circ
= NULL
;
1668 struct timeval circ_start_time
;
1669 memset(&circ_start_time
, 0, sizeof(circ_start_time
));
1671 extend_info_t fakehop
;
1672 memset(&fakehop
, 0, sizeof(fakehop
));
1673 extend_info_t
*single_fakehop
= &fakehop
;
1674 extend_info_t
*multi_fakehop
[DEFAULT_ROUTE_LEN
] = {&fakehop
,
1678 extend_info_t ipv6_hop
;
1679 memset(&ipv6_hop
, 0, sizeof(ipv6_hop
));
1680 tor_addr_parse(&ipv6_hop
.orports
[0].addr
, "1::2");
1681 extend_info_t
*multi_ipv6_hop
[DEFAULT_ROUTE_LEN
] = {&ipv6_hop
,
1685 extend_info_t ipv4_hop
;
1686 memset(&ipv4_hop
, 0, sizeof(ipv4_hop
));
1687 tor_addr_from_ipv4h(&ipv4_hop
.orports
[0].addr
, 0x20304050);
1688 extend_info_t
*multi_ipv4_hop
[DEFAULT_ROUTE_LEN
] = {&ipv4_hop
,
1692 mock_circuit_deliver_create_cell_expect_direct
= false;
1693 MOCK(circuit_deliver_create_cell
, mock_circuit_deliver_create_cell
);
1695 MOCK(server_mode
, mock_server_mode
);
1697 /* Try a direct connection, and succeed on a client */
1699 origin_circ
= new_test_origin_circuit(false,
1703 tt_ptr_op(origin_circ
, OP_NE
, NULL
);
1704 /* Skip some of the multi-hop checks */
1705 origin_circ
->build_state
->onehop_tunnel
= 1;
1706 /* This is a direct connection */
1707 mock_circuit_deliver_create_cell_expect_direct
= true;
1708 tt_int_op(circuit_send_next_onion_skin(origin_circ
), OP_EQ
, 0);
1709 /* The circuits are automatically freed by the circuitlist. */
1711 /* Try a direct connection, and succeed on a server */
1713 origin_circ
= new_test_origin_circuit(false,
1717 tt_ptr_op(origin_circ
, OP_NE
, NULL
);
1718 origin_circ
->build_state
->onehop_tunnel
= 1;
1719 mock_circuit_deliver_create_cell_expect_direct
= true;
1720 tt_int_op(circuit_send_next_onion_skin(origin_circ
), OP_EQ
, 0);
1722 /* Start capturing bugs */
1723 setup_full_capture_of_logs(LOG_WARN
);
1724 tor_capture_bugs_(1);
1726 /* Try an extend, but fail the client valid address family check */
1728 origin_circ
= new_test_origin_circuit(true,
1730 ARRAY_LENGTH(multi_fakehop
),
1732 tt_ptr_op(origin_circ
, OP_NE
, NULL
);
1734 origin_circ
->base_
.state
= 0;
1735 /* This is an indirect connection */
1736 mock_circuit_deliver_create_cell_expect_direct
= false;
1737 /* Fail because the address family is invalid */
1738 tt_int_op(circuit_send_next_onion_skin(origin_circ
), OP_EQ
,
1739 -END_CIRC_REASON_INTERNAL
);
1740 expect_log_msg("No supported address family found in extend_info.\n");
1741 mock_clean_saved_logs();
1743 /* Try an extend, but fail the server valid address check */
1745 origin_circ
= new_test_origin_circuit(true,
1747 ARRAY_LENGTH(multi_fakehop
),
1749 tt_ptr_op(origin_circ
, OP_NE
, NULL
);
1750 origin_circ
->base_
.state
= 0;
1751 mock_circuit_deliver_create_cell_expect_direct
= false;
1752 tt_int_op(circuit_send_next_onion_skin(origin_circ
), OP_EQ
,
1753 -END_CIRC_REASON_INTERNAL
);
1754 expect_log_msg("No supported address family found in extend_info.\n");
1755 mock_clean_saved_logs();
1757 /* Try an extend, but fail in the client code, with an IPv6 address */
1759 origin_circ
= new_test_origin_circuit(true,
1761 ARRAY_LENGTH(multi_ipv6_hop
),
1763 tt_ptr_op(origin_circ
, OP_NE
, NULL
);
1764 origin_circ
->base_
.state
= 0;
1765 mock_circuit_deliver_create_cell_expect_direct
= false;
1766 tt_int_op(circuit_send_next_onion_skin(origin_circ
), OP_EQ
,
1767 -END_CIRC_REASON_INTERNAL
);
1768 expect_log_msg("No supported address family found in extend_info.\n");
1769 mock_clean_saved_logs();
1771 /* Stop capturing bugs, but keep capturing logs */
1772 tor_end_capture_bugs_();
1774 /* Try an extend, pass the client IPv4 check, but fail later */
1776 origin_circ
= new_test_origin_circuit(true,
1778 ARRAY_LENGTH(multi_ipv4_hop
),
1780 tt_ptr_op(origin_circ
, OP_NE
, NULL
);
1781 origin_circ
->base_
.state
= 0;
1782 mock_circuit_deliver_create_cell_expect_direct
= false;
1783 /* Fail because the circuit data is invalid */
1784 tt_int_op(circuit_send_next_onion_skin(origin_circ
), OP_EQ
,
1785 -END_CIRC_REASON_INTERNAL
);
1786 expect_log_msg("onion_skin_create failed.\n");
1787 mock_clean_saved_logs();
1789 /* Try an extend, pass the server IPv4 check, but fail later */
1791 origin_circ
= new_test_origin_circuit(true,
1793 ARRAY_LENGTH(multi_ipv4_hop
),
1795 tt_ptr_op(origin_circ
, OP_NE
, NULL
);
1796 origin_circ
->base_
.state
= 0;
1797 mock_circuit_deliver_create_cell_expect_direct
= false;
1798 tt_int_op(circuit_send_next_onion_skin(origin_circ
), OP_EQ
,
1799 -END_CIRC_REASON_INTERNAL
);
1800 expect_log_msg("onion_skin_create failed.\n");
1801 mock_clean_saved_logs();
1803 /* Try an extend, pass the server IPv6 check, but fail later */
1805 origin_circ
= new_test_origin_circuit(true,
1807 ARRAY_LENGTH(multi_ipv6_hop
),
1809 tt_ptr_op(origin_circ
, OP_NE
, NULL
);
1810 origin_circ
->base_
.state
= 0;
1811 mock_circuit_deliver_create_cell_expect_direct
= false;
1812 tt_int_op(circuit_send_next_onion_skin(origin_circ
), OP_EQ
,
1813 -END_CIRC_REASON_INTERNAL
);
1814 expect_log_msg("onion_skin_create failed.\n");
1815 mock_clean_saved_logs();
1817 /* Things we're not testing right now:
1818 * - the addresses in the extend cell inside
1819 * circuit_send_intermediate_onion_skin() matches the address in the
1820 * supplied extend_info.
1821 * - valid circuit data.
1822 * - actually extending the circuit to each hop. */
1825 tor_end_capture_bugs_();
1826 mock_clean_saved_logs();
1827 teardown_capture_of_logs();
1829 UNMOCK(circuit_deliver_create_cell
);
1830 UNMOCK(server_mode
);
1833 /* The circuits are automatically freed by the circuitlist. */
1836 /* Test the different cases in cpath_build_state_to_crn_flags(). */
1838 test_cpath_build_state_to_crn_flags(void *arg
)
1842 cpath_build_state_t state
;
1843 memset(&state
, 0, sizeof(state
));
1845 tt_int_op(cpath_build_state_to_crn_flags(&state
), OP_EQ
,
1848 memset(&state
, 0, sizeof(state
));
1849 state
.need_uptime
= 1;
1850 tt_int_op(cpath_build_state_to_crn_flags(&state
), OP_EQ
,
1853 memset(&state
, 0, sizeof(state
));
1854 state
.need_capacity
= 1;
1855 tt_int_op(cpath_build_state_to_crn_flags(&state
), OP_EQ
,
1858 memset(&state
, 0, sizeof(state
));
1859 state
.need_capacity
= 1;
1860 state
.need_uptime
= 1;
1861 tt_int_op(cpath_build_state_to_crn_flags(&state
), OP_EQ
,
1862 CRN_NEED_CAPACITY
| CRN_NEED_UPTIME
);
1864 /* Check that no other flags are handled */
1865 memset(&state
, 0xff, sizeof(state
));
1866 tt_int_op(cpath_build_state_to_crn_flags(&state
), OP_EQ
,
1867 CRN_NEED_CAPACITY
| CRN_NEED_UPTIME
);
1873 /* Test the different cases in cpath_build_state_to_crn_ipv6_extend_flag(). */
1875 test_cpath_build_state_to_crn_ipv6_extend_flag(void *arg
)
1879 cpath_build_state_t state
;
1881 memset(&state
, 0, sizeof(state
));
1882 state
.desired_path_len
= DEFAULT_ROUTE_LEN
;
1883 tt_int_op(cpath_build_state_to_crn_ipv6_extend_flag(&state
, 0), OP_EQ
,
1886 /* Pass the state flag check, but not the length check */
1887 memset(&state
, 0, sizeof(state
));
1888 state
.desired_path_len
= DEFAULT_ROUTE_LEN
;
1889 state
.is_ipv6_selftest
= 1;
1890 tt_int_op(cpath_build_state_to_crn_ipv6_extend_flag(&state
, 0), OP_EQ
,
1893 /* Pass the length check, but not the state flag check */
1894 memset(&state
, 0, sizeof(state
));
1895 state
.desired_path_len
= DEFAULT_ROUTE_LEN
;
1897 cpath_build_state_to_crn_ipv6_extend_flag(&state
,
1898 DEFAULT_ROUTE_LEN
- 2),
1901 /* Pass both checks */
1902 memset(&state
, 0, sizeof(state
));
1903 state
.desired_path_len
= DEFAULT_ROUTE_LEN
;
1904 state
.is_ipv6_selftest
= 1;
1906 cpath_build_state_to_crn_ipv6_extend_flag(&state
,
1907 DEFAULT_ROUTE_LEN
- 2),
1908 OP_EQ
, CRN_INITIATE_IPV6_EXTEND
);
1910 /* Check that no other flags are handled */
1911 memset(&state
, 0xff, sizeof(state
));
1912 state
.desired_path_len
= INT_MAX
;
1913 tt_int_op(cpath_build_state_to_crn_ipv6_extend_flag(&state
, INT_MAX
), OP_EQ
,
1916 #ifndef ALL_BUGS_ARE_FATAL
1917 /* Start capturing bugs */
1918 setup_full_capture_of_logs(LOG_INFO
);
1919 tor_capture_bugs_(1);
1921 /* Now test the single hop circuit case */
1922 #define SINGLE_HOP_ROUTE_LEN 1
1923 memset(&state
, 0, sizeof(state
));
1924 state
.desired_path_len
= SINGLE_HOP_ROUTE_LEN
;
1925 state
.is_ipv6_selftest
= 1;
1927 cpath_build_state_to_crn_ipv6_extend_flag(&state
,
1928 SINGLE_HOP_ROUTE_LEN
- 2),
1930 tt_int_op(smartlist_len(tor_get_captured_bug_log_()), OP_EQ
, 1);
1931 tt_str_op(smartlist_get(tor_get_captured_bug_log_(), 0), OP_EQ
,
1932 "!(ASSERT_PREDICT_UNLIKELY_(state->desired_path_len < 2))");
1933 mock_clean_saved_logs();
1934 #endif /* !defined(ALL_BUGS_ARE_FATAL) */
1937 tor_end_capture_bugs_();
1938 mock_clean_saved_logs();
1939 teardown_capture_of_logs();
1942 #define TEST(name, flags, setup, cleanup) \
1943 { #name, test_ ## name, flags, setup, cleanup }
1945 #define TEST_NEW_ROUTE_LEN(name, flags) \
1946 { #name, test_new_route_len_ ## name, flags, NULL, NULL }
1948 #define TEST_CIRCUIT(name, flags) \
1949 { #name, test_circuit_ ## name, flags, NULL, NULL }
1951 #define TEST_CPATH(name, flags) \
1952 { #name, test_cpath_ ## name, flags, NULL, NULL }
1955 #define TEST_CIRCUIT_PASSTHROUGH(name, flags, arg) \
1956 { #name "/" arg, test_circuit_ ## name, flags, \
1957 &passthrough_setup, (void *)(arg) }
1960 struct testcase_t circuitbuild_tests
[] = {
1961 TEST_NEW_ROUTE_LEN(noexit
, 0),
1962 TEST_NEW_ROUTE_LEN(safe_exit
, 0),
1963 TEST_NEW_ROUTE_LEN(unsafe_exit
, 0),
1964 TEST_NEW_ROUTE_LEN(unhandled_exit
, 0),
1966 TEST(upgrade_from_guard_wait
, TT_FORK
, &helper_pubsub_setup
, NULL
),
1968 TEST_CIRCUIT(extend_state_valid
, TT_FORK
),
1969 TEST_CIRCUIT(extend_add_ed25519
, TT_FORK
),
1970 TEST_CIRCUIT(extend_lspec_valid
, TT_FORK
),
1971 TEST_CIRCUIT(extend_add_ip
, TT_FORK
),
1972 TEST_CIRCUIT(choose_ip_ap_for_extend
, 0),
1974 TEST_CIRCUIT_PASSTHROUGH(open_connection_for_extend
, TT_FORK
, "4"),
1975 TEST_CIRCUIT_PASSTHROUGH(open_connection_for_extend
, TT_FORK
, "6"),
1976 TEST_CIRCUIT_PASSTHROUGH(open_connection_for_extend
, TT_FORK
, "dual-stack"),
1978 TEST_CIRCUIT(extend
, TT_FORK
),
1980 TEST(onionskin_answer
, TT_FORK
, NULL
, NULL
),
1982 TEST(origin_circuit_init
, TT_FORK
, NULL
, NULL
),
1983 TEST_CIRCUIT(send_next_onion_skin
, TT_FORK
),
1984 TEST_CPATH(build_state_to_crn_flags
, 0),
1985 TEST_CPATH(build_state_to_crn_ipv6_extend_flag
, TT_FORK
),