Merge branch 'maint-0.4.8'
[tor.git] / src / test / test_circuituse.c
blobb6e945c36e500bb4c156b08038ffdaa48b2adf4e
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"
9 #include "test/test.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"
20 static void
21 test_circuit_is_available_for_use_ret_false_when_marked_for_close(void *arg)
23 (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));
30 done:
31 tor_free(circ);
34 static void
35 test_circuit_is_available_for_use_ret_false_when_timestamp_dirty(void *arg)
37 (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));
44 done:
45 tor_free(circ);
48 static void
49 test_circuit_is_available_for_use_ret_false_for_non_general_purpose(void *arg)
51 (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));
58 done:
59 tor_free(circ);
62 static void
63 test_circuit_is_available_for_use_ret_false_for_non_general_origin(void *arg)
65 (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));
72 done:
73 tor_free(circ);
76 static void
77 test_circuit_is_available_for_use_ret_false_for_non_origin_purpose(void *arg)
79 (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));
86 done:
87 tor_free(circ);
90 static void
91 test_circuit_is_available_for_use_ret_false_unusable_for_new_conns(void *arg)
93 (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));
100 done:
101 circuit_free(circ);
104 static void
105 test_circuit_is_available_for_use_returns_false_for_onehop_tunnel(void *arg)
107 (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));
116 done:
117 circuit_free(circ);
120 static void
121 test_circuit_is_available_for_use_returns_true_for_clean_circuit(void *arg)
123 (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));
132 done:
133 circuit_free(circ);
136 static int
137 mock_circuit_all_predicted_ports_handled(time_t now,
138 int *need_uptime,
139 int *need_capacity)
141 (void)now;
143 if (need_uptime && need_capacity)
144 return 0;
145 return 1;
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;
160 static void
161 test_needs_exit_circuits_ret_false_for_predicted_ports_and_path(void *arg)
163 (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);
171 tt_int_op(0, OP_EQ,
172 needs_exit_circuits(now, &needs_uptime, &needs_capacity));
174 done:
175 UNMOCK(circuit_all_predicted_ports_handled);
178 static void
179 test_needs_exit_circuits_ret_false_for_non_exit_consensus_path(void *arg)
181 (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);
190 tt_int_op(0, OP_EQ,
191 needs_exit_circuits(now, &needs_uptime, &needs_capacity));
193 done:
194 UNMOCK(circuit_all_predicted_ports_handled);
195 UNMOCK(router_have_consensus_path);
198 static void
199 test_needs_exit_circuits_ret_true_for_predicted_ports_and_path(void *arg)
201 (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);
210 tt_int_op(1, OP_EQ,
211 needs_exit_circuits(now, &needs_uptime, &needs_capacity));
213 done:
214 UNMOCK(circuit_all_predicted_ports_handled);
215 UNMOCK(router_have_consensus_path);
218 static void
219 test_needs_circuits_for_build_ret_false_consensus_path_unknown(void *arg)
221 (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));
224 done: ;
227 static void
228 test_needs_circuits_for_build_ret_false_if_num_less_than_max(void *arg)
230 (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));
233 done:
234 UNMOCK(router_have_consensus_path);
237 static void
238 test_needs_circuits_for_build_returns_true_when_more_are_needed(void *arg)
240 (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));
243 done:
244 UNMOCK(router_have_consensus_path);
247 struct testcase_t circuituse_tests[] = {
248 { "marked",
249 test_circuit_is_available_for_use_ret_false_when_marked_for_close,
250 TT_FORK, NULL, NULL
252 { "timestamp",
253 test_circuit_is_available_for_use_ret_false_when_timestamp_dirty,
254 TT_FORK, NULL, NULL
256 { "non_general",
257 test_circuit_is_available_for_use_ret_false_for_non_general_purpose,
258 TT_FORK, NULL, NULL
260 { "non_general",
261 test_circuit_is_available_for_use_ret_false_for_non_general_origin,
262 TT_FORK, NULL, NULL
264 { "origin",
265 test_circuit_is_available_for_use_ret_false_for_non_origin_purpose,
266 TT_FORK, NULL, NULL
268 { "clean",
269 test_circuit_is_available_for_use_ret_false_unusable_for_new_conns,
270 TT_FORK, NULL, NULL
272 { "onehop",
273 test_circuit_is_available_for_use_returns_false_for_onehop_tunnel,
274 TT_FORK, NULL, NULL
276 { "clean_circ",
277 test_circuit_is_available_for_use_returns_true_for_clean_circuit,
278 TT_FORK, NULL, NULL
280 { "exit_f",
281 test_needs_exit_circuits_ret_false_for_predicted_ports_and_path,
282 TT_FORK, NULL, NULL
284 { "exit_t",
285 test_needs_exit_circuits_ret_true_for_predicted_ports_and_path,
286 TT_FORK, NULL, NULL
288 { "non_exit",
289 test_needs_exit_circuits_ret_false_for_non_exit_consensus_path,
290 TT_FORK, NULL, NULL
292 { "true",
293 test_needs_exit_circuits_ret_true_for_predicted_ports_and_path,
294 TT_FORK, NULL, NULL
296 { "consensus_path_unknown",
297 test_needs_circuits_for_build_ret_false_consensus_path_unknown,
298 TT_FORK, NULL, NULL
300 { "less_than_max",
301 test_needs_circuits_for_build_ret_false_if_num_less_than_max,
302 TT_FORK, NULL, NULL
304 { "more_needed",
305 test_needs_circuits_for_build_returns_true_when_more_are_needed,
306 TT_FORK, NULL, NULL
308 END_OF_TESTCASES