Merge branch 'maint-0.2.9' into maint-0.3.3
[tor.git] / src / test / test_circuituse.c
blobdf1b43807fdf8d8b5f69f88a37ebe4656a09a64d
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
8 #include "or.h"
9 #include "test.h"
10 #include "test_helpers.h"
11 #include "config.h"
12 #include "circuitlist.h"
13 #include "circuituse.h"
14 #include "circuitbuild.h"
15 #include "nodelist.h"
17 static void
18 test_circuit_is_available_for_use_ret_false_when_marked_for_close(void *arg)
20 (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));
27 done:
28 tor_free(circ);
31 static void
32 test_circuit_is_available_for_use_ret_false_when_timestamp_dirty(void *arg)
34 (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));
41 done:
42 tor_free(circ);
45 static void
46 test_circuit_is_available_for_use_ret_false_for_non_general_purpose(void *arg)
48 (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));
55 done:
56 tor_free(circ);
59 static void
60 test_circuit_is_available_for_use_ret_false_for_non_general_origin(void *arg)
62 (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));
69 done:
70 tor_free(circ);
73 static void
74 test_circuit_is_available_for_use_ret_false_for_non_origin_purpose(void *arg)
76 (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));
83 done:
84 tor_free(circ);
87 static void
88 test_circuit_is_available_for_use_ret_false_unusable_for_new_conns(void *arg)
90 (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));
97 done:
98 circuit_free(circ);
101 static void
102 test_circuit_is_available_for_use_returns_false_for_onehop_tunnel(void *arg)
104 (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));
113 done:
114 circuit_free(circ);
117 static void
118 test_circuit_is_available_for_use_returns_true_for_clean_circuit(void *arg)
120 (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));
129 done:
130 circuit_free(circ);
133 static int
134 mock_circuit_all_predicted_ports_handled(time_t now,
135 int *need_uptime,
136 int *need_capacity)
138 (void)now;
140 if (need_uptime && need_capacity)
141 return 0;
142 return 1;
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;
157 static void
158 test_needs_exit_circuits_ret_false_for_predicted_ports_and_path(void *arg)
160 (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);
168 tt_int_op(0, OP_EQ,
169 needs_exit_circuits(now, &needs_uptime, &needs_capacity));
171 done:
172 UNMOCK(circuit_all_predicted_ports_handled);
175 static void
176 test_needs_exit_circuits_ret_false_for_non_exit_consensus_path(void *arg)
178 (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);
187 tt_int_op(0, OP_EQ,
188 needs_exit_circuits(now, &needs_uptime, &needs_capacity));
190 done:
191 UNMOCK(circuit_all_predicted_ports_handled);
192 UNMOCK(router_have_consensus_path);
195 static void
196 test_needs_exit_circuits_ret_true_for_predicted_ports_and_path(void *arg)
198 (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);
207 tt_int_op(1, OP_EQ,
208 needs_exit_circuits(now, &needs_uptime, &needs_capacity));
210 done:
211 UNMOCK(circuit_all_predicted_ports_handled);
212 UNMOCK(router_have_consensus_path);
215 static void
216 test_needs_circuits_for_build_ret_false_consensus_path_unknown(void *arg)
218 (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));
221 done: ;
224 static void
225 test_needs_circuits_for_build_ret_false_if_num_less_than_max(void *arg)
227 (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));
230 done:
231 UNMOCK(router_have_consensus_path);
234 static void
235 test_needs_circuits_for_build_returns_true_when_more_are_needed(void *arg)
237 (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));
240 done:
241 UNMOCK(router_have_consensus_path);
244 struct testcase_t circuituse_tests[] = {
245 { "marked",
246 test_circuit_is_available_for_use_ret_false_when_marked_for_close,
247 TT_FORK, NULL, NULL
249 { "timestamp",
250 test_circuit_is_available_for_use_ret_false_when_timestamp_dirty,
251 TT_FORK, NULL, NULL
253 { "non_general",
254 test_circuit_is_available_for_use_ret_false_for_non_general_purpose,
255 TT_FORK, NULL, NULL
257 { "non_general",
258 test_circuit_is_available_for_use_ret_false_for_non_general_origin,
259 TT_FORK, NULL, NULL
261 { "origin",
262 test_circuit_is_available_for_use_ret_false_for_non_origin_purpose,
263 TT_FORK, NULL, NULL
265 { "clean",
266 test_circuit_is_available_for_use_ret_false_unusable_for_new_conns,
267 TT_FORK, NULL, NULL
269 { "onehop",
270 test_circuit_is_available_for_use_returns_false_for_onehop_tunnel,
271 TT_FORK, NULL, NULL
273 { "clean_circ",
274 test_circuit_is_available_for_use_returns_true_for_clean_circuit,
275 TT_FORK, NULL, NULL
277 { "exit_f",
278 test_needs_exit_circuits_ret_false_for_predicted_ports_and_path,
279 TT_FORK, NULL, NULL
281 { "exit_t",
282 test_needs_exit_circuits_ret_true_for_predicted_ports_and_path,
283 TT_FORK, NULL, NULL
285 { "non_exit",
286 test_needs_exit_circuits_ret_false_for_non_exit_consensus_path,
287 TT_FORK, NULL, NULL
289 { "true",
290 test_needs_exit_circuits_ret_true_for_predicted_ports_and_path,
291 TT_FORK, NULL, NULL
293 { "consensus_path_unknown",
294 test_needs_circuits_for_build_ret_false_consensus_path_unknown,
295 TT_FORK, NULL, NULL
297 { "less_than_max",
298 test_needs_circuits_for_build_ret_false_if_num_less_than_max,
299 TT_FORK, NULL, NULL
301 { "more_needed",
302 test_needs_circuits_for_build_returns_true_when_more_are_needed,
303 TT_FORK, NULL, NULL
305 END_OF_TESTCASES