1 /* Copyright (c) 2001-2004, Roger Dingledine.
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2017, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
6 #define CIRCUITLIST_PRIVATE
10 #include "test_helpers.h"
12 #include "circuitlist.h"
13 #include "circuituse.h"
14 #include "circuitbuild.h"
18 test_circuit_is_available_for_use_ret_false_when_marked_for_close(void *arg
)
22 circuit_t
*circ
= tor_malloc(sizeof(circuit_t
));
23 circ
->marked_for_close
= 1;
25 tt_int_op(0, OP_EQ
, circuit_is_available_for_use(circ
));
32 test_circuit_is_available_for_use_ret_false_when_timestamp_dirty(void *arg
)
36 circuit_t
*circ
= tor_malloc(sizeof(circuit_t
));
37 circ
->timestamp_dirty
= 1;
39 tt_int_op(0, OP_EQ
, circuit_is_available_for_use(circ
));
46 test_circuit_is_available_for_use_ret_false_for_non_general_purpose(void *arg
)
50 circuit_t
*circ
= tor_malloc(sizeof(circuit_t
));
51 circ
->purpose
= CIRCUIT_PURPOSE_REND_POINT_WAITING
;
53 tt_int_op(0, OP_EQ
, circuit_is_available_for_use(circ
));
60 test_circuit_is_available_for_use_ret_false_for_non_general_origin(void *arg
)
64 circuit_t
*circ
= tor_malloc(sizeof(circuit_t
));
65 circ
->purpose
= CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT
;
67 tt_int_op(0, OP_EQ
, circuit_is_available_for_use(circ
));
74 test_circuit_is_available_for_use_ret_false_for_non_origin_purpose(void *arg
)
78 circuit_t
*circ
= tor_malloc(sizeof(circuit_t
));
79 circ
->purpose
= CIRCUIT_PURPOSE_OR
;
81 tt_int_op(0, OP_EQ
, circuit_is_available_for_use(circ
));
88 test_circuit_is_available_for_use_ret_false_unusable_for_new_conns(void *arg
)
92 circuit_t
*circ
= dummy_origin_circuit_new(30);
93 mark_circuit_unusable_for_new_conns(TO_ORIGIN_CIRCUIT(circ
));
95 tt_int_op(0, OP_EQ
, circuit_is_available_for_use(circ
));
102 test_circuit_is_available_for_use_returns_false_for_onehop_tunnel(void *arg
)
106 circuit_t
*circ
= dummy_origin_circuit_new(30);
107 origin_circuit_t
*oc
= TO_ORIGIN_CIRCUIT(circ
);
108 oc
->build_state
= tor_malloc_zero(sizeof(cpath_build_state_t
));
109 oc
->build_state
->onehop_tunnel
= 1;
111 tt_int_op(0, OP_EQ
, circuit_is_available_for_use(circ
));
118 test_circuit_is_available_for_use_returns_true_for_clean_circuit(void *arg
)
122 circuit_t
*circ
= dummy_origin_circuit_new(30);
123 origin_circuit_t
*oc
= TO_ORIGIN_CIRCUIT(circ
);
124 oc
->build_state
= tor_malloc_zero(sizeof(cpath_build_state_t
));
125 oc
->build_state
->onehop_tunnel
= 0;
127 tt_int_op(1, OP_EQ
, circuit_is_available_for_use(circ
));
134 mock_circuit_all_predicted_ports_handled(time_t now
,
140 if (need_uptime
&& need_capacity
)
145 static consensus_path_type_t
146 mock_router_have_unknown_consensus_path(void)
148 return CONSENSUS_PATH_UNKNOWN
;
151 static consensus_path_type_t
152 mock_router_have_exit_consensus_path(void)
154 return CONSENSUS_PATH_EXIT
;
158 test_needs_exit_circuits_ret_false_for_predicted_ports_and_path(void *arg
)
162 MOCK(circuit_all_predicted_ports_handled
,
163 mock_circuit_all_predicted_ports_handled
);
164 int needs_uptime
= 1;
165 int needs_capacity
= 0;
167 time_t now
= time(NULL
);
169 needs_exit_circuits(now
, &needs_uptime
, &needs_capacity
));
172 UNMOCK(circuit_all_predicted_ports_handled
);
176 test_needs_exit_circuits_ret_false_for_non_exit_consensus_path(void *arg
)
180 MOCK(circuit_all_predicted_ports_handled
,
181 mock_circuit_all_predicted_ports_handled
);
182 int needs_uptime
= 1;
183 int needs_capacity
= 1;
184 MOCK(router_have_consensus_path
, mock_router_have_unknown_consensus_path
);
186 time_t now
= time(NULL
);
188 needs_exit_circuits(now
, &needs_uptime
, &needs_capacity
));
191 UNMOCK(circuit_all_predicted_ports_handled
);
192 UNMOCK(router_have_consensus_path
);
196 test_needs_exit_circuits_ret_true_for_predicted_ports_and_path(void *arg
)
200 MOCK(circuit_all_predicted_ports_handled
,
201 mock_circuit_all_predicted_ports_handled
);
202 int needs_uptime
= 1;
203 int needs_capacity
= 1;
204 MOCK(router_have_consensus_path
, mock_router_have_exit_consensus_path
);
206 time_t now
= time(NULL
);
208 needs_exit_circuits(now
, &needs_uptime
, &needs_capacity
));
211 UNMOCK(circuit_all_predicted_ports_handled
);
212 UNMOCK(router_have_consensus_path
);
216 test_needs_circuits_for_build_ret_false_consensus_path_unknown(void *arg
)
219 MOCK(router_have_consensus_path
, mock_router_have_unknown_consensus_path
);
220 tt_int_op(0, OP_EQ
, needs_circuits_for_build(0));
225 test_needs_circuits_for_build_ret_false_if_num_less_than_max(void *arg
)
228 MOCK(router_have_consensus_path
, mock_router_have_exit_consensus_path
);
229 tt_int_op(0, OP_EQ
, needs_circuits_for_build(13));
231 UNMOCK(router_have_consensus_path
);
235 test_needs_circuits_for_build_returns_true_when_more_are_needed(void *arg
)
238 MOCK(router_have_consensus_path
, mock_router_have_exit_consensus_path
);
239 tt_int_op(1, OP_EQ
, needs_circuits_for_build(0));
241 UNMOCK(router_have_consensus_path
);
244 struct testcase_t circuituse_tests
[] = {
246 test_circuit_is_available_for_use_ret_false_when_marked_for_close
,
250 test_circuit_is_available_for_use_ret_false_when_timestamp_dirty
,
254 test_circuit_is_available_for_use_ret_false_for_non_general_purpose
,
258 test_circuit_is_available_for_use_ret_false_for_non_general_origin
,
262 test_circuit_is_available_for_use_ret_false_for_non_origin_purpose
,
266 test_circuit_is_available_for_use_ret_false_unusable_for_new_conns
,
270 test_circuit_is_available_for_use_returns_false_for_onehop_tunnel
,
274 test_circuit_is_available_for_use_returns_true_for_clean_circuit
,
278 test_needs_exit_circuits_ret_false_for_predicted_ports_and_path
,
282 test_needs_exit_circuits_ret_true_for_predicted_ports_and_path
,
286 test_needs_exit_circuits_ret_false_for_non_exit_consensus_path
,
290 test_needs_exit_circuits_ret_true_for_predicted_ports_and_path
,
293 { "consensus_path_unknown",
294 test_needs_circuits_for_build_ret_false_consensus_path_unknown
,
298 test_needs_circuits_for_build_ret_false_if_num_less_than_max
,
302 test_needs_circuits_for_build_returns_true_when_more_are_needed
,