2016-10-14 14:00:35 +02:00
|
|
|
/* Copyright (c) 2001-2004, Roger Dingledine.
|
|
|
|
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
|
2017-03-15 21:13:17 +01:00
|
|
|
* Copyright (c) 2007-2017, The Tor Project, Inc. */
|
2016-10-14 14:00:35 +02:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
2016-12-09 16:20:14 +01:00
|
|
|
#define CIRCUITLIST_PRIVATE
|
|
|
|
|
2016-10-14 14:00:35 +02:00
|
|
|
#include "or.h"
|
|
|
|
#include "test.h"
|
|
|
|
#include "test_helpers.h"
|
|
|
|
#include "config.h"
|
|
|
|
#include "circuitlist.h"
|
|
|
|
#include "circuituse.h"
|
|
|
|
#include "circuitbuild.h"
|
|
|
|
#include "nodelist.h"
|
|
|
|
|
2018-06-15 18:07:20 +02:00
|
|
|
#include "cpath_build_state_st.h"
|
2018-06-15 17:34:33 +02:00
|
|
|
#include "origin_circuit_st.h"
|
|
|
|
|
2016-10-14 14:00:35 +02:00
|
|
|
static void
|
|
|
|
test_circuit_is_available_for_use_ret_false_when_marked_for_close(void *arg)
|
|
|
|
{
|
|
|
|
(void)arg;
|
|
|
|
|
|
|
|
circuit_t *circ = tor_malloc(sizeof(circuit_t));
|
|
|
|
circ->marked_for_close = 1;
|
|
|
|
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_int_op(0, OP_EQ, circuit_is_available_for_use(circ));
|
2016-10-14 14:00:35 +02:00
|
|
|
|
|
|
|
done:
|
|
|
|
tor_free(circ);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_circuit_is_available_for_use_ret_false_when_timestamp_dirty(void *arg)
|
|
|
|
{
|
|
|
|
(void)arg;
|
|
|
|
|
|
|
|
circuit_t *circ = tor_malloc(sizeof(circuit_t));
|
|
|
|
circ->timestamp_dirty = 1;
|
|
|
|
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_int_op(0, OP_EQ, circuit_is_available_for_use(circ));
|
2016-10-14 14:00:35 +02:00
|
|
|
|
|
|
|
done:
|
|
|
|
tor_free(circ);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_circuit_is_available_for_use_ret_false_for_non_general_purpose(void *arg)
|
|
|
|
{
|
|
|
|
(void)arg;
|
|
|
|
|
|
|
|
circuit_t *circ = tor_malloc(sizeof(circuit_t));
|
2016-11-02 15:45:02 +01:00
|
|
|
circ->purpose = CIRCUIT_PURPOSE_REND_POINT_WAITING;
|
|
|
|
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_int_op(0, OP_EQ, circuit_is_available_for_use(circ));
|
2016-11-02 15:45:02 +01:00
|
|
|
|
|
|
|
done:
|
|
|
|
tor_free(circ);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_circuit_is_available_for_use_ret_false_for_non_general_origin(void *arg)
|
|
|
|
{
|
|
|
|
(void)arg;
|
|
|
|
|
|
|
|
circuit_t *circ = tor_malloc(sizeof(circuit_t));
|
|
|
|
circ->purpose = CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT;
|
2016-10-14 14:00:35 +02:00
|
|
|
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_int_op(0, OP_EQ, circuit_is_available_for_use(circ));
|
2016-10-14 14:00:35 +02:00
|
|
|
|
|
|
|
done:
|
|
|
|
tor_free(circ);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_circuit_is_available_for_use_ret_false_for_non_origin_purpose(void *arg)
|
|
|
|
{
|
|
|
|
(void)arg;
|
|
|
|
|
|
|
|
circuit_t *circ = tor_malloc(sizeof(circuit_t));
|
|
|
|
circ->purpose = CIRCUIT_PURPOSE_OR;
|
|
|
|
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_int_op(0, OP_EQ, circuit_is_available_for_use(circ));
|
2016-10-14 14:00:35 +02:00
|
|
|
|
|
|
|
done:
|
|
|
|
tor_free(circ);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_circuit_is_available_for_use_ret_false_unusable_for_new_conns(void *arg)
|
|
|
|
{
|
|
|
|
(void)arg;
|
|
|
|
|
|
|
|
circuit_t *circ = dummy_origin_circuit_new(30);
|
|
|
|
mark_circuit_unusable_for_new_conns(TO_ORIGIN_CIRCUIT(circ));
|
|
|
|
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_int_op(0, OP_EQ, circuit_is_available_for_use(circ));
|
2016-10-14 14:00:35 +02:00
|
|
|
|
|
|
|
done:
|
2016-12-09 16:20:14 +01:00
|
|
|
circuit_free(circ);
|
2016-10-14 14:00:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_circuit_is_available_for_use_returns_false_for_onehop_tunnel(void *arg)
|
|
|
|
{
|
|
|
|
(void)arg;
|
|
|
|
|
|
|
|
circuit_t *circ = dummy_origin_circuit_new(30);
|
|
|
|
origin_circuit_t *oc = TO_ORIGIN_CIRCUIT(circ);
|
2016-12-09 16:20:14 +01:00
|
|
|
oc->build_state = tor_malloc_zero(sizeof(cpath_build_state_t));
|
2016-10-14 14:00:35 +02:00
|
|
|
oc->build_state->onehop_tunnel = 1;
|
|
|
|
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_int_op(0, OP_EQ, circuit_is_available_for_use(circ));
|
2016-10-14 14:00:35 +02:00
|
|
|
|
|
|
|
done:
|
2016-12-09 16:20:14 +01:00
|
|
|
circuit_free(circ);
|
2016-10-14 14:00:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_circuit_is_available_for_use_returns_true_for_clean_circuit(void *arg)
|
|
|
|
{
|
|
|
|
(void)arg;
|
|
|
|
|
|
|
|
circuit_t *circ = dummy_origin_circuit_new(30);
|
|
|
|
origin_circuit_t *oc = TO_ORIGIN_CIRCUIT(circ);
|
2016-12-09 16:20:14 +01:00
|
|
|
oc->build_state = tor_malloc_zero(sizeof(cpath_build_state_t));
|
2016-10-14 14:00:35 +02:00
|
|
|
oc->build_state->onehop_tunnel = 0;
|
|
|
|
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_int_op(1, OP_EQ, circuit_is_available_for_use(circ));
|
2016-10-14 14:00:35 +02:00
|
|
|
|
|
|
|
done:
|
2016-12-09 16:20:14 +01:00
|
|
|
circuit_free(circ);
|
2016-10-14 14:00:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
mock_circuit_all_predicted_ports_handled(time_t now,
|
|
|
|
int *need_uptime,
|
|
|
|
int *need_capacity)
|
|
|
|
{
|
|
|
|
(void)now;
|
|
|
|
|
|
|
|
if (need_uptime && need_capacity)
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static consensus_path_type_t
|
|
|
|
mock_router_have_unknown_consensus_path(void)
|
|
|
|
{
|
|
|
|
return CONSENSUS_PATH_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
static consensus_path_type_t
|
|
|
|
mock_router_have_exit_consensus_path(void)
|
|
|
|
{
|
|
|
|
return CONSENSUS_PATH_EXIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_needs_exit_circuits_ret_false_for_predicted_ports_and_path(void *arg)
|
|
|
|
{
|
|
|
|
(void)arg;
|
|
|
|
|
|
|
|
MOCK(circuit_all_predicted_ports_handled,
|
|
|
|
mock_circuit_all_predicted_ports_handled);
|
|
|
|
int needs_uptime = 1;
|
|
|
|
int needs_capacity = 0;
|
|
|
|
|
|
|
|
time_t now = time(NULL);
|
2017-06-05 16:38:20 +02:00
|
|
|
tt_int_op(0, OP_EQ,
|
|
|
|
needs_exit_circuits(now, &needs_uptime, &needs_capacity));
|
2016-10-14 14:00:35 +02:00
|
|
|
|
|
|
|
done:
|
|
|
|
UNMOCK(circuit_all_predicted_ports_handled);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_needs_exit_circuits_ret_false_for_non_exit_consensus_path(void *arg)
|
|
|
|
{
|
|
|
|
(void)arg;
|
|
|
|
|
|
|
|
MOCK(circuit_all_predicted_ports_handled,
|
|
|
|
mock_circuit_all_predicted_ports_handled);
|
|
|
|
int needs_uptime = 1;
|
|
|
|
int needs_capacity = 1;
|
|
|
|
MOCK(router_have_consensus_path, mock_router_have_unknown_consensus_path);
|
|
|
|
|
|
|
|
time_t now = time(NULL);
|
2017-06-05 16:38:20 +02:00
|
|
|
tt_int_op(0, OP_EQ,
|
|
|
|
needs_exit_circuits(now, &needs_uptime, &needs_capacity));
|
2016-10-14 14:00:35 +02:00
|
|
|
|
|
|
|
done:
|
|
|
|
UNMOCK(circuit_all_predicted_ports_handled);
|
|
|
|
UNMOCK(router_have_consensus_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_needs_exit_circuits_ret_true_for_predicted_ports_and_path(void *arg)
|
|
|
|
{
|
|
|
|
(void)arg;
|
|
|
|
|
|
|
|
MOCK(circuit_all_predicted_ports_handled,
|
|
|
|
mock_circuit_all_predicted_ports_handled);
|
|
|
|
int needs_uptime = 1;
|
|
|
|
int needs_capacity = 1;
|
|
|
|
MOCK(router_have_consensus_path, mock_router_have_exit_consensus_path);
|
|
|
|
|
|
|
|
time_t now = time(NULL);
|
2017-06-05 16:38:20 +02:00
|
|
|
tt_int_op(1, OP_EQ,
|
|
|
|
needs_exit_circuits(now, &needs_uptime, &needs_capacity));
|
2016-10-14 14:00:35 +02:00
|
|
|
|
|
|
|
done:
|
|
|
|
UNMOCK(circuit_all_predicted_ports_handled);
|
|
|
|
UNMOCK(router_have_consensus_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_needs_circuits_for_build_ret_false_consensus_path_unknown(void *arg)
|
|
|
|
{
|
|
|
|
(void)arg;
|
|
|
|
MOCK(router_have_consensus_path, mock_router_have_unknown_consensus_path);
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_int_op(0, OP_EQ, needs_circuits_for_build(0));
|
2016-10-14 14:00:35 +02:00
|
|
|
done: ;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_needs_circuits_for_build_ret_false_if_num_less_than_max(void *arg)
|
|
|
|
{
|
|
|
|
(void)arg;
|
|
|
|
MOCK(router_have_consensus_path, mock_router_have_exit_consensus_path);
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_int_op(0, OP_EQ, needs_circuits_for_build(13));
|
2016-10-14 14:00:35 +02:00
|
|
|
done:
|
|
|
|
UNMOCK(router_have_consensus_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_needs_circuits_for_build_returns_true_when_more_are_needed(void *arg)
|
|
|
|
{
|
|
|
|
(void)arg;
|
|
|
|
MOCK(router_have_consensus_path, mock_router_have_exit_consensus_path);
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_int_op(1, OP_EQ, needs_circuits_for_build(0));
|
2016-10-14 14:00:35 +02:00
|
|
|
done:
|
|
|
|
UNMOCK(router_have_consensus_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct testcase_t circuituse_tests[] = {
|
|
|
|
{ "marked",
|
|
|
|
test_circuit_is_available_for_use_ret_false_when_marked_for_close,
|
|
|
|
TT_FORK, NULL, NULL
|
|
|
|
},
|
|
|
|
{ "timestamp",
|
|
|
|
test_circuit_is_available_for_use_ret_false_when_timestamp_dirty,
|
|
|
|
TT_FORK, NULL, NULL
|
|
|
|
},
|
|
|
|
{ "non_general",
|
|
|
|
test_circuit_is_available_for_use_ret_false_for_non_general_purpose,
|
|
|
|
TT_FORK, NULL, NULL
|
|
|
|
},
|
2016-11-02 15:45:02 +01:00
|
|
|
{ "non_general",
|
|
|
|
test_circuit_is_available_for_use_ret_false_for_non_general_origin,
|
|
|
|
TT_FORK, NULL, NULL
|
|
|
|
},
|
2016-10-14 14:00:35 +02:00
|
|
|
{ "origin",
|
|
|
|
test_circuit_is_available_for_use_ret_false_for_non_origin_purpose,
|
|
|
|
TT_FORK, NULL, NULL
|
|
|
|
},
|
|
|
|
{ "clean",
|
|
|
|
test_circuit_is_available_for_use_ret_false_unusable_for_new_conns,
|
|
|
|
TT_FORK, NULL, NULL
|
|
|
|
},
|
|
|
|
{ "onehop",
|
|
|
|
test_circuit_is_available_for_use_returns_false_for_onehop_tunnel,
|
|
|
|
TT_FORK, NULL, NULL
|
|
|
|
},
|
|
|
|
{ "clean_circ",
|
|
|
|
test_circuit_is_available_for_use_returns_true_for_clean_circuit,
|
|
|
|
TT_FORK, NULL, NULL
|
|
|
|
},
|
|
|
|
{ "exit_f",
|
|
|
|
test_needs_exit_circuits_ret_false_for_predicted_ports_and_path,
|
|
|
|
TT_FORK, NULL, NULL
|
|
|
|
},
|
|
|
|
{ "exit_t",
|
|
|
|
test_needs_exit_circuits_ret_true_for_predicted_ports_and_path,
|
|
|
|
TT_FORK, NULL, NULL
|
|
|
|
},
|
|
|
|
{ "non_exit",
|
|
|
|
test_needs_exit_circuits_ret_false_for_non_exit_consensus_path,
|
|
|
|
TT_FORK, NULL, NULL
|
|
|
|
},
|
|
|
|
{ "true",
|
|
|
|
test_needs_exit_circuits_ret_true_for_predicted_ports_and_path,
|
|
|
|
TT_FORK, NULL, NULL
|
|
|
|
},
|
|
|
|
{ "consensus_path_unknown",
|
|
|
|
test_needs_circuits_for_build_ret_false_consensus_path_unknown,
|
|
|
|
TT_FORK, NULL, NULL
|
|
|
|
},
|
|
|
|
{ "less_than_max",
|
|
|
|
test_needs_circuits_for_build_ret_false_if_num_less_than_max,
|
|
|
|
TT_FORK, NULL, NULL
|
|
|
|
},
|
|
|
|
{ "more_needed",
|
|
|
|
test_needs_circuits_for_build_returns_true_when_more_are_needed,
|
|
|
|
TT_FORK, NULL, NULL
|
|
|
|
},
|
|
|
|
END_OF_TESTCASES
|
|
|
|
};
|
|
|
|
|