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 CIRCUITLIST_PRIVATE
8 #include "core/or/or.h"
10 #include "test/test_helpers.h"
11 #include "app/config/config.h"
12 #include "core/or/circuitlist.h"
13 #include "core/or/circuituse.h"
14 #include "core/or/circuitbuild.h"
15 #include "feature/nodelist/nodelist.h"
17 #include "core/or/cpath_build_state_st.h"
18 #include "core/or/origin_circuit_st.h"
21 test_circuit_is_available_for_use_ret_false_when_marked_for_close(void *arg
)
25 circuit_t
*circ
= tor_malloc(sizeof(circuit_t
));
26 circ
->marked_for_close
= 1;
28 tt_int_op(0, OP_EQ
, circuit_is_available_for_use(circ
));
35 test_circuit_is_available_for_use_ret_false_when_timestamp_dirty(void *arg
)
39 circuit_t
*circ
= tor_malloc(sizeof(circuit_t
));
40 circ
->timestamp_dirty
= 1;
42 tt_int_op(0, OP_EQ
, circuit_is_available_for_use(circ
));
49 test_circuit_is_available_for_use_ret_false_for_non_general_purpose(void *arg
)
53 circuit_t
*circ
= tor_malloc(sizeof(circuit_t
));
54 circ
->purpose
= CIRCUIT_PURPOSE_REND_POINT_WAITING
;
56 tt_int_op(0, OP_EQ
, circuit_is_available_for_use(circ
));
63 test_circuit_is_available_for_use_ret_false_for_non_general_origin(void *arg
)
67 circuit_t
*circ
= tor_malloc(sizeof(circuit_t
));
68 circ
->purpose
= CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT
;
70 tt_int_op(0, OP_EQ
, circuit_is_available_for_use(circ
));
77 test_circuit_is_available_for_use_ret_false_for_non_origin_purpose(void *arg
)
81 circuit_t
*circ
= tor_malloc(sizeof(circuit_t
));
82 circ
->purpose
= CIRCUIT_PURPOSE_OR
;
84 tt_int_op(0, OP_EQ
, circuit_is_available_for_use(circ
));
91 test_circuit_is_available_for_use_ret_false_unusable_for_new_conns(void *arg
)
95 circuit_t
*circ
= dummy_origin_circuit_new(30);
96 mark_circuit_unusable_for_new_conns(TO_ORIGIN_CIRCUIT(circ
));
98 tt_int_op(0, OP_EQ
, circuit_is_available_for_use(circ
));
105 test_circuit_is_available_for_use_returns_false_for_onehop_tunnel(void *arg
)
109 circuit_t
*circ
= dummy_origin_circuit_new(30);
110 origin_circuit_t
*oc
= TO_ORIGIN_CIRCUIT(circ
);
111 oc
->build_state
= tor_malloc_zero(sizeof(cpath_build_state_t
));
112 oc
->build_state
->onehop_tunnel
= 1;
114 tt_int_op(0, OP_EQ
, circuit_is_available_for_use(circ
));
121 test_circuit_is_available_for_use_returns_true_for_clean_circuit(void *arg
)
125 circuit_t
*circ
= dummy_origin_circuit_new(30);
126 origin_circuit_t
*oc
= TO_ORIGIN_CIRCUIT(circ
);
127 oc
->build_state
= tor_malloc_zero(sizeof(cpath_build_state_t
));
128 oc
->build_state
->onehop_tunnel
= 0;
130 tt_int_op(1, OP_EQ
, circuit_is_available_for_use(circ
));
137 mock_circuit_all_predicted_ports_handled(time_t now
,
143 if (need_uptime
&& need_capacity
)
148 static consensus_path_type_t
149 mock_router_have_unknown_consensus_path(void)
151 return CONSENSUS_PATH_UNKNOWN
;
154 static consensus_path_type_t
155 mock_router_have_exit_consensus_path(void)
157 return CONSENSUS_PATH_EXIT
;
161 test_needs_exit_circuits_ret_false_for_predicted_ports_and_path(void *arg
)
165 MOCK(circuit_all_predicted_ports_handled
,
166 mock_circuit_all_predicted_ports_handled
);
167 int needs_uptime
= 1;
168 int needs_capacity
= 0;
170 time_t now
= time(NULL
);
172 needs_exit_circuits(now
, &needs_uptime
, &needs_capacity
));
175 UNMOCK(circuit_all_predicted_ports_handled
);
179 test_needs_exit_circuits_ret_false_for_non_exit_consensus_path(void *arg
)
183 MOCK(circuit_all_predicted_ports_handled
,
184 mock_circuit_all_predicted_ports_handled
);
185 int needs_uptime
= 1;
186 int needs_capacity
= 1;
187 MOCK(router_have_consensus_path
, mock_router_have_unknown_consensus_path
);
189 time_t now
= time(NULL
);
191 needs_exit_circuits(now
, &needs_uptime
, &needs_capacity
));
194 UNMOCK(circuit_all_predicted_ports_handled
);
195 UNMOCK(router_have_consensus_path
);
199 test_needs_exit_circuits_ret_true_for_predicted_ports_and_path(void *arg
)
203 MOCK(circuit_all_predicted_ports_handled
,
204 mock_circuit_all_predicted_ports_handled
);
205 int needs_uptime
= 1;
206 int needs_capacity
= 1;
207 MOCK(router_have_consensus_path
, mock_router_have_exit_consensus_path
);
209 time_t now
= time(NULL
);
211 needs_exit_circuits(now
, &needs_uptime
, &needs_capacity
));
214 UNMOCK(circuit_all_predicted_ports_handled
);
215 UNMOCK(router_have_consensus_path
);
219 test_needs_circuits_for_build_ret_false_consensus_path_unknown(void *arg
)
222 MOCK(router_have_consensus_path
, mock_router_have_unknown_consensus_path
);
223 tt_int_op(0, OP_EQ
, needs_circuits_for_build(0));
228 test_needs_circuits_for_build_ret_false_if_num_less_than_max(void *arg
)
231 MOCK(router_have_consensus_path
, mock_router_have_exit_consensus_path
);
232 tt_int_op(0, OP_EQ
, needs_circuits_for_build(13));
234 UNMOCK(router_have_consensus_path
);
238 test_needs_circuits_for_build_returns_true_when_more_are_needed(void *arg
)
241 MOCK(router_have_consensus_path
, mock_router_have_exit_consensus_path
);
242 tt_int_op(1, OP_EQ
, needs_circuits_for_build(0));
244 UNMOCK(router_have_consensus_path
);
247 struct testcase_t circuituse_tests
[] = {
249 test_circuit_is_available_for_use_ret_false_when_marked_for_close
,
253 test_circuit_is_available_for_use_ret_false_when_timestamp_dirty
,
257 test_circuit_is_available_for_use_ret_false_for_non_general_purpose
,
261 test_circuit_is_available_for_use_ret_false_for_non_general_origin
,
265 test_circuit_is_available_for_use_ret_false_for_non_origin_purpose
,
269 test_circuit_is_available_for_use_ret_false_unusable_for_new_conns
,
273 test_circuit_is_available_for_use_returns_false_for_onehop_tunnel
,
277 test_circuit_is_available_for_use_returns_true_for_clean_circuit
,
281 test_needs_exit_circuits_ret_false_for_predicted_ports_and_path
,
285 test_needs_exit_circuits_ret_true_for_predicted_ports_and_path
,
289 test_needs_exit_circuits_ret_false_for_non_exit_consensus_path
,
293 test_needs_exit_circuits_ret_true_for_predicted_ports_and_path
,
296 { "consensus_path_unknown",
297 test_needs_circuits_for_build_ret_false_consensus_path_unknown
,
301 test_needs_circuits_for_build_ret_false_if_num_less_than_max
,
305 test_needs_circuits_for_build_returns_true_when_more_are_needed
,