2017-03-28 23:34:54 +02:00
|
|
|
/* Copyright (c) 2001-2004, Roger Dingledine.
|
|
|
|
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
|
2020-01-09 00:39:17 +01:00
|
|
|
* Copyright (c) 2007-2020, The Tor Project, Inc. */
|
2017-03-28 23:34:54 +02:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
|
|
|
#define CIRCUITBUILD_PRIVATE
|
2019-06-18 19:32:45 +02:00
|
|
|
#define CIRCUITLIST_PRIVATE
|
|
|
|
#define ENTRYNODES_PRIVATE
|
2017-03-28 23:34:54 +02:00
|
|
|
|
2018-07-05 22:34:59 +02:00
|
|
|
#include "core/or/or.h"
|
2018-06-20 15:35:05 +02:00
|
|
|
#include "test/test.h"
|
|
|
|
#include "test/test_helpers.h"
|
|
|
|
#include "test/log_test_helpers.h"
|
2018-07-05 22:34:59 +02:00
|
|
|
#include "app/config/config.h"
|
|
|
|
#include "core/or/circuitbuild.h"
|
|
|
|
#include "core/or/circuitlist.h"
|
2018-06-20 15:35:05 +02:00
|
|
|
|
2019-06-18 19:32:45 +02:00
|
|
|
#include "core/or/cpath_build_state_st.h"
|
2018-07-05 22:34:59 +02:00
|
|
|
#include "core/or/extend_info_st.h"
|
2019-06-18 19:32:45 +02:00
|
|
|
#include "core/or/origin_circuit_st.h"
|
|
|
|
|
|
|
|
#include "feature/client/entrynodes.h"
|
2018-06-15 21:37:05 +02:00
|
|
|
|
2017-03-28 23:34:54 +02:00
|
|
|
/* Dummy nodes smartlist for testing */
|
|
|
|
static smartlist_t dummy_nodes;
|
|
|
|
/* Dummy exit extend_info for testing */
|
|
|
|
static extend_info_t dummy_ei;
|
|
|
|
|
|
|
|
static int
|
2019-04-26 16:36:49 +02:00
|
|
|
mock_count_acceptable_nodes(const smartlist_t *nodes, int direct)
|
2017-03-28 23:34:54 +02:00
|
|
|
{
|
|
|
|
(void)nodes;
|
|
|
|
|
2018-10-18 03:43:59 +02:00
|
|
|
return direct ? 1 : DEFAULT_ROUTE_LEN + 1;
|
2017-03-28 23:34:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Test route lengths when the caller of new_route_len() doesn't
|
|
|
|
* specify exit_ei. */
|
|
|
|
static void
|
|
|
|
test_new_route_len_noexit(void *arg)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
(void)arg;
|
|
|
|
MOCK(count_acceptable_nodes, mock_count_acceptable_nodes);
|
|
|
|
|
|
|
|
r = new_route_len(CIRCUIT_PURPOSE_C_GENERAL, NULL, &dummy_nodes);
|
|
|
|
tt_int_op(DEFAULT_ROUTE_LEN, OP_EQ, r);
|
|
|
|
|
|
|
|
r = new_route_len(CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT, NULL, &dummy_nodes);
|
|
|
|
tt_int_op(DEFAULT_ROUTE_LEN, OP_EQ, r);
|
|
|
|
|
|
|
|
r = new_route_len(CIRCUIT_PURPOSE_S_CONNECT_REND, NULL, &dummy_nodes);
|
|
|
|
tt_int_op(DEFAULT_ROUTE_LEN, OP_EQ, r);
|
|
|
|
|
|
|
|
done:
|
|
|
|
UNMOCK(count_acceptable_nodes);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Test route lengths where someone else chose the "exit" node, which
|
|
|
|
* require an extra hop for safety. */
|
|
|
|
static void
|
|
|
|
test_new_route_len_unsafe_exit(void *arg)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
(void)arg;
|
|
|
|
MOCK(count_acceptable_nodes, mock_count_acceptable_nodes);
|
|
|
|
|
|
|
|
/* connecting to hidden service directory */
|
|
|
|
r = new_route_len(CIRCUIT_PURPOSE_C_GENERAL, &dummy_ei, &dummy_nodes);
|
|
|
|
tt_int_op(DEFAULT_ROUTE_LEN + 1, OP_EQ, r);
|
|
|
|
|
|
|
|
/* client connecting to introduction point */
|
|
|
|
r = new_route_len(CIRCUIT_PURPOSE_C_INTRODUCING, &dummy_ei, &dummy_nodes);
|
|
|
|
tt_int_op(DEFAULT_ROUTE_LEN + 1, OP_EQ, r);
|
|
|
|
|
|
|
|
/* hidden service connecting to rendezvous point */
|
|
|
|
r = new_route_len(CIRCUIT_PURPOSE_S_CONNECT_REND, &dummy_ei, &dummy_nodes);
|
|
|
|
tt_int_op(DEFAULT_ROUTE_LEN + 1, OP_EQ, r);
|
|
|
|
|
|
|
|
done:
|
|
|
|
UNMOCK(count_acceptable_nodes);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Test route lengths where we chose the "exit" node, which don't
|
|
|
|
* require an extra hop for safety. */
|
|
|
|
static void
|
|
|
|
test_new_route_len_safe_exit(void *arg)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
(void)arg;
|
|
|
|
MOCK(count_acceptable_nodes, mock_count_acceptable_nodes);
|
|
|
|
|
|
|
|
/* hidden service connecting to introduction point */
|
|
|
|
r = new_route_len(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO, &dummy_ei,
|
|
|
|
&dummy_nodes);
|
|
|
|
tt_int_op(DEFAULT_ROUTE_LEN, OP_EQ, r);
|
|
|
|
|
|
|
|
/* router testing its own reachability */
|
|
|
|
r = new_route_len(CIRCUIT_PURPOSE_TESTING, &dummy_ei, &dummy_nodes);
|
|
|
|
tt_int_op(DEFAULT_ROUTE_LEN, OP_EQ, r);
|
|
|
|
|
|
|
|
done:
|
|
|
|
UNMOCK(count_acceptable_nodes);
|
|
|
|
}
|
|
|
|
|
2017-03-28 23:35:25 +02:00
|
|
|
/* Make sure a non-fatal assertion fails when new_route_len() gets an
|
|
|
|
* unexpected circuit purpose. */
|
|
|
|
static void
|
|
|
|
test_new_route_len_unhandled_exit(void *arg)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
(void)arg;
|
2020-03-06 16:55:21 +01:00
|
|
|
#ifdef ALL_BUGS_ARE_FATAL
|
2020-03-19 08:11:13 +01:00
|
|
|
/* Coverity (and maybe clang analyser) complain that the code following
|
|
|
|
* tt_skip() is unconditionally unreachable. */
|
|
|
|
#if !defined(__COVERITY__) && !defined(__clang_analyzer__)
|
2020-03-06 16:55:21 +01:00
|
|
|
tt_skip();
|
2020-03-19 08:11:13 +01:00
|
|
|
#endif
|
2020-03-06 16:55:21 +01:00
|
|
|
#endif
|
|
|
|
|
2017-03-28 23:35:25 +02:00
|
|
|
MOCK(count_acceptable_nodes, mock_count_acceptable_nodes);
|
|
|
|
|
|
|
|
tor_capture_bugs_(1);
|
2017-04-03 21:00:33 +02:00
|
|
|
setup_full_capture_of_logs(LOG_WARN);
|
2017-03-28 23:35:25 +02:00
|
|
|
r = new_route_len(CIRCUIT_PURPOSE_CONTROLLER, &dummy_ei, &dummy_nodes);
|
|
|
|
tt_int_op(DEFAULT_ROUTE_LEN + 1, OP_EQ, r);
|
|
|
|
tt_int_op(smartlist_len(tor_get_captured_bug_log_()), OP_EQ, 1);
|
|
|
|
tt_str_op(smartlist_get(tor_get_captured_bug_log_(), 0), OP_EQ,
|
|
|
|
"!(exit_ei && !known_purpose)");
|
2017-04-03 21:00:33 +02:00
|
|
|
expect_single_log_msg_containing("Unhandled purpose");
|
|
|
|
expect_single_log_msg_containing("with a chosen exit; assuming routelen");
|
|
|
|
teardown_capture_of_logs();
|
2017-03-28 23:35:25 +02:00
|
|
|
tor_end_capture_bugs_();
|
|
|
|
|
|
|
|
done:
|
|
|
|
UNMOCK(count_acceptable_nodes);
|
|
|
|
}
|
|
|
|
|
2019-06-18 19:32:45 +02:00
|
|
|
static void
|
|
|
|
test_upgrade_from_guard_wait(void *arg)
|
|
|
|
{
|
|
|
|
circuit_t *circ = NULL;
|
|
|
|
origin_circuit_t *orig_circ = NULL;
|
|
|
|
entry_guard_t *guard = NULL;
|
|
|
|
smartlist_t *list = NULL;
|
|
|
|
|
|
|
|
(void) arg;
|
|
|
|
|
|
|
|
circ = dummy_origin_circuit_new(0);
|
|
|
|
orig_circ = TO_ORIGIN_CIRCUIT(circ);
|
|
|
|
tt_assert(orig_circ);
|
|
|
|
|
|
|
|
orig_circ->build_state = tor_malloc_zero(sizeof(cpath_build_state_t));
|
|
|
|
|
|
|
|
circuit_set_state(circ, CIRCUIT_STATE_GUARD_WAIT);
|
|
|
|
|
|
|
|
/* Put it in guard wait state. */
|
|
|
|
guard = tor_malloc_zero(sizeof(*guard));
|
|
|
|
guard->in_selection = get_guard_selection_info();
|
|
|
|
|
|
|
|
orig_circ->guard_state =
|
|
|
|
circuit_guard_state_new(guard, GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
/* Mark the circuit for close. */
|
|
|
|
circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
|
|
|
|
tt_int_op(circ->marked_for_close, OP_NE, 0);
|
|
|
|
|
|
|
|
/* We shouldn't pick the mark for close circuit. */
|
|
|
|
list = circuit_find_circuits_to_upgrade_from_guard_wait();
|
|
|
|
tt_assert(!list);
|
|
|
|
|
|
|
|
done:
|
2019-08-19 22:21:55 +02:00
|
|
|
smartlist_free(list);
|
2019-06-18 19:32:45 +02:00
|
|
|
circuit_free(circ);
|
|
|
|
entry_guard_free_(guard);
|
|
|
|
}
|
|
|
|
|
2017-03-28 23:34:54 +02:00
|
|
|
struct testcase_t circuitbuild_tests[] = {
|
|
|
|
{ "noexit", test_new_route_len_noexit, 0, NULL, NULL },
|
|
|
|
{ "safe_exit", test_new_route_len_safe_exit, 0, NULL, NULL },
|
|
|
|
{ "unsafe_exit", test_new_route_len_unsafe_exit, 0, NULL, NULL },
|
2017-03-28 23:35:25 +02:00
|
|
|
{ "unhandled_exit", test_new_route_len_unhandled_exit, 0, NULL, NULL },
|
2019-06-18 19:32:45 +02:00
|
|
|
{ "upgrade_from_guard_wait", test_upgrade_from_guard_wait, TT_FORK,
|
|
|
|
&helper_pubsub_setup, NULL },
|
2017-03-28 23:34:54 +02:00
|
|
|
END_OF_TESTCASES
|
|
|
|
};
|