Line data Source code
1 : /* Copyright (c) 2016-2021, The Tor Project, Inc. */
2 : /* See LICENSE for licensing information */
3 :
4 : #define CHANNEL_OBJECT_PRIVATE
5 : #define MAINLOOP_PRIVATE
6 : #define NETWORKSTATUS_PRIVATE
7 : #define TOR_TIMERS_PRIVATE
8 : #include "core/or/or.h"
9 : #include "test/test.h"
10 : #include "lib/testsupport/testsupport.h"
11 : #include "core/mainloop/connection.h"
12 : #include "core/or/connection_or.h"
13 : #include "core/or/channel.h"
14 : #include "core/or/channeltls.h"
15 : #include "core/or/channelpadding.h"
16 : #include "lib/evloop/compat_libevent.h"
17 : #include "app/config/config.h"
18 : #include "lib/time/compat_time.h"
19 : #include "core/mainloop/mainloop.h"
20 : #include "feature/nodelist/networkstatus.h"
21 : #include "test/log_test_helpers.h"
22 : #include "lib/tls/tortls.h"
23 : #include "lib/evloop/timers.h"
24 : #include "lib/buf/buffers.h"
25 :
26 : #include "core/or/cell_st.h"
27 : #include "feature/nodelist/networkstatus_st.h"
28 : #include "core/or/or_connection_st.h"
29 : #include "feature/nodelist/routerstatus_st.h"
30 :
31 : int channelpadding_get_netflow_inactive_timeout_ms(channel_t *chan);
32 : int64_t channelpadding_compute_time_until_pad_for_netflow(channel_t *chan);
33 : int channelpadding_send_disable_command(channel_t*);
34 : int channelpadding_find_timerslot(channel_t *chan);
35 :
36 : void test_channelpadding_timers(void *arg);
37 : void test_channelpadding_consensus(void *arg);
38 : void test_channelpadding_negotiation(void *arg);
39 : void test_channelpadding_decide_to_pad_channel(void *arg);
40 : void test_channelpadding_killonehop(void *arg);
41 :
42 : void dummy_nop_timer(void);
43 :
44 : #define NSEC_PER_MSEC (1000*1000)
45 :
46 : /* Thing to cast to fake tor_tls_t * to appease assert_connection_ok() */
47 : static int fake_tortls = 0; /* Bleh... */
48 :
49 : static int dont_stop_libevent = 0;
50 :
51 : // From test_channel.c
52 : channel_t * new_fake_channel(void);
53 : void free_fake_channel(channel_t*);
54 :
55 : static int
56 431 : mock_channel_has_queued_writes(channel_t *chan)
57 : {
58 431 : (void)chan;
59 431 : return 0;
60 : }
61 :
62 : static int tried_to_write_cell = 0;
63 :
64 : static channel_t *relay1_relay2;
65 : static channel_t *relay2_relay1;
66 : static channel_t *relay3_client;
67 : static channel_t *client_relay3;
68 :
69 : static int
70 1 : mock_channel_write_cell_relay2(channel_t *chan, cell_t *cell)
71 : {
72 1 : (void)chan;
73 1 : tried_to_write_cell++;
74 1 : channel_tls_handle_cell(cell, ((channel_tls_t*)relay1_relay2)->conn);
75 1 : tor_libevent_exit_loop_after_callback(tor_libevent_get_base());
76 1 : return 0;
77 : }
78 :
79 : static int
80 0 : mock_channel_write_cell_relay1(channel_t *chan, cell_t *cell)
81 : {
82 0 : (void)chan;
83 0 : tried_to_write_cell++;
84 0 : channel_tls_handle_cell(cell, ((channel_tls_t*)relay2_relay1)->conn);
85 0 : tor_libevent_exit_loop_after_callback(tor_libevent_get_base());
86 0 : return 0;
87 : }
88 :
89 : static int
90 2 : mock_channel_write_cell_relay3(channel_t *chan, cell_t *cell)
91 : {
92 2 : (void)chan;
93 2 : tried_to_write_cell++;
94 2 : channel_tls_handle_cell(cell, ((channel_tls_t*)client_relay3)->conn);
95 2 : tor_libevent_exit_loop_after_callback(tor_libevent_get_base());
96 2 : return 0;
97 : }
98 :
99 : static int
100 7 : mock_channel_write_cell_client(channel_t *chan, cell_t *cell)
101 : {
102 7 : (void)chan;
103 7 : tried_to_write_cell++;
104 7 : channel_tls_handle_cell(cell, ((channel_tls_t*)relay3_client)->conn);
105 7 : tor_libevent_exit_loop_after_callback(tor_libevent_get_base());
106 7 : return 0;
107 : }
108 :
109 : static int
110 209 : mock_channel_write_cell(channel_t *chan, cell_t *cell)
111 : {
112 209 : tried_to_write_cell++;
113 209 : channel_tls_handle_cell(cell, ((channel_tls_t*)chan)->conn);
114 209 : if (!dont_stop_libevent)
115 209 : tor_libevent_exit_loop_after_callback(tor_libevent_get_base());
116 209 : return 0;
117 : }
118 :
119 : static void
120 110 : setup_fake_connection_for_channel(channel_tls_t *chan)
121 : {
122 110 : or_connection_t *conn = (or_connection_t*)connection_new(CONN_TYPE_OR,
123 : AF_INET);
124 :
125 110 : conn->base_.conn_array_index = smartlist_len(connection_array);
126 110 : smartlist_add(connection_array, conn);
127 :
128 110 : conn->chan = chan;
129 110 : chan->conn = conn;
130 :
131 110 : conn->base_.magic = OR_CONNECTION_MAGIC;
132 110 : conn->base_.state = OR_CONN_STATE_OPEN;
133 110 : conn->base_.type = CONN_TYPE_OR;
134 110 : conn->base_.socket_family = AF_INET;
135 110 : conn->base_.address = tor_strdup("<fake>");
136 :
137 110 : conn->base_.port = 4242;
138 :
139 110 : conn->tls = (tor_tls_t *)((void *)(&fake_tortls));
140 :
141 110 : conn->link_proto = MIN_LINK_PROTO_FOR_CHANNEL_PADDING;
142 :
143 110 : connection_or_set_canonical(conn, 1);
144 110 : }
145 :
146 : static channel_tls_t *
147 110 : new_fake_channeltls(uint8_t id)
148 : {
149 110 : channel_tls_t *chan = tor_realloc(new_fake_channel(), sizeof(channel_tls_t));
150 110 : chan->base_.magic = TLS_CHAN_MAGIC;
151 110 : setup_fake_connection_for_channel(chan);
152 110 : chan->base_.channel_usage = CHANNEL_USED_FOR_FULL_CIRCS;
153 110 : chan->base_.has_queued_writes = mock_channel_has_queued_writes;
154 110 : chan->base_.write_cell = mock_channel_write_cell;
155 110 : chan->base_.padding_enabled = 1;
156 :
157 110 : chan->base_.identity_digest[0] = id;
158 110 : channel_register(&chan->base_);
159 :
160 110 : return chan;
161 : }
162 :
163 : static void
164 110 : free_fake_channeltls(channel_tls_t *chan)
165 : {
166 110 : channel_unregister(&chan->base_);
167 :
168 110 : tor_free(((channel_tls_t*)chan)->conn->base_.address);
169 110 : buf_free(((channel_tls_t*)chan)->conn->base_.inbuf);
170 110 : buf_free(((channel_tls_t*)chan)->conn->base_.outbuf);
171 110 : tor_free(((channel_tls_t*)chan)->conn);
172 :
173 110 : timer_free(chan->base_.padding_timer);
174 110 : channel_handle_free(chan->base_.timer_handle);
175 110 : channel_handles_clear(&chan->base_);
176 :
177 110 : free_fake_channel(&chan->base_);
178 :
179 110 : return;
180 : }
181 :
182 : static void
183 3 : setup_mock_consensus(void)
184 : {
185 6 : current_md_consensus = current_ns_consensus
186 3 : = tor_malloc_zero(sizeof(networkstatus_t));
187 3 : current_md_consensus->net_params = smartlist_new();
188 3 : current_md_consensus->routerstatus_list = smartlist_new();
189 3 : channelpadding_new_consensus_params(current_md_consensus);
190 3 : }
191 :
192 : static void
193 3 : free_mock_consensus(void)
194 : {
195 10 : SMARTLIST_FOREACH(current_md_consensus->routerstatus_list, void *, r,
196 : tor_free(r));
197 3 : smartlist_free(current_md_consensus->routerstatus_list);
198 3 : smartlist_free(current_ns_consensus->net_params);
199 3 : tor_free(current_ns_consensus);
200 3 : }
201 :
202 : static void
203 2 : setup_mock_network(void)
204 : {
205 2 : routerstatus_t *relay;
206 2 : if (!connection_array)
207 0 : connection_array = smartlist_new();
208 :
209 2 : relay1_relay2 = (channel_t*)new_fake_channeltls(2);
210 2 : relay1_relay2->write_cell = mock_channel_write_cell_relay1;
211 2 : channel_timestamp_active(relay1_relay2);
212 2 : relay = tor_malloc_zero(sizeof(routerstatus_t));
213 2 : relay->identity_digest[0] = 1;
214 2 : smartlist_add(current_md_consensus->routerstatus_list, relay);
215 :
216 2 : relay2_relay1 = (channel_t*)new_fake_channeltls(1);
217 2 : relay2_relay1->write_cell = mock_channel_write_cell_relay2;
218 2 : channel_timestamp_active(relay2_relay1);
219 2 : relay = tor_malloc_zero(sizeof(routerstatus_t));
220 2 : relay->identity_digest[0] = 2;
221 2 : smartlist_add(current_md_consensus->routerstatus_list, relay);
222 :
223 2 : relay3_client = (channel_t*)new_fake_channeltls(0);
224 2 : relay3_client->write_cell = mock_channel_write_cell_relay3;
225 2 : relay3_client->is_client = 1;
226 2 : channel_timestamp_active(relay3_client);
227 2 : relay = tor_malloc_zero(sizeof(routerstatus_t));
228 2 : relay->identity_digest[0] = 3;
229 2 : smartlist_add(current_md_consensus->routerstatus_list, relay);
230 :
231 2 : client_relay3 = (channel_t*)new_fake_channeltls(3);
232 2 : client_relay3->write_cell = mock_channel_write_cell_client;
233 2 : channel_timestamp_active(client_relay3);
234 :
235 2 : channel_do_open_actions(relay1_relay2);
236 2 : channel_do_open_actions(relay2_relay1);
237 2 : channel_do_open_actions(relay3_client);
238 2 : channel_do_open_actions(client_relay3);
239 2 : }
240 :
241 : static void
242 2 : free_mock_network(void)
243 : {
244 2 : free_fake_channeltls((channel_tls_t*)relay1_relay2);
245 2 : free_fake_channeltls((channel_tls_t*)relay2_relay1);
246 2 : free_fake_channeltls((channel_tls_t*)relay3_client);
247 2 : free_fake_channeltls((channel_tls_t*)client_relay3);
248 :
249 2 : smartlist_free(connection_array);
250 2 : }
251 :
252 : static void
253 0 : dummy_timer_cb(tor_timer_t *t, void *arg, const monotime_t *now_mono)
254 : {
255 0 : (void)t; (void)arg; (void)now_mono;
256 0 : tor_libevent_exit_loop_after_callback(tor_libevent_get_base());
257 0 : return;
258 : }
259 :
260 : // This hack adds a dummy timer so that the libevent base loop
261 : // actually returns when we don't expect any timers to fire. Otherwise,
262 : // the global_timer_event gets scheduled an hour from now, and the
263 : // base loop never returns.
264 : void
265 0 : dummy_nop_timer(void)
266 : {
267 0 : tor_timer_t *dummy_timer = timer_new(dummy_timer_cb, NULL);
268 0 : struct timeval timeout;
269 0 : timeout.tv_sec = 1;
270 0 : timeout.tv_usec = 0;
271 :
272 0 : timer_schedule(dummy_timer, &timeout);
273 :
274 0 : tor_libevent_run_event_loop(tor_libevent_get_base(), 0);
275 :
276 0 : timer_free(dummy_timer);
277 0 : }
278 :
279 : #define CHANNELPADDING_MAX_TIMERS 25
280 : #define CHANNELS_TO_TEST (CHANNELPADDING_MAX_TIMERS*4)
281 : /**
282 : * Tests to ensure that we handle more than the max number of pending
283 : * timers properly.
284 : */
285 : void
286 1 : test_channelpadding_timers(void *arg)
287 : {
288 1 : channelpadding_decision_t decision;
289 1 : channel_t *chans[CHANNELS_TO_TEST];
290 1 : (void)arg;
291 :
292 1 : if (!connection_array)
293 0 : connection_array = smartlist_new();
294 :
295 1 : monotime_init();
296 1 : monotime_enable_test_mocking();
297 1 : uint64_t nsec_mock = 1;
298 1 : monotime_set_mock_time_nsec(nsec_mock);
299 1 : monotime_coarse_set_mock_time_nsec(nsec_mock);
300 :
301 1 : timers_initialize();
302 1 : channelpadding_new_consensus_params(NULL);
303 :
304 101 : for (int i = 0; i < CHANNELS_TO_TEST; i++) {
305 100 : chans[i] = (channel_t*)new_fake_channeltls(0);
306 100 : channel_timestamp_active(chans[i]);
307 : }
308 :
309 3 : for (int j = 0; j < 2; j++) {
310 2 : tried_to_write_cell = 0;
311 2 : int i = 0;
312 :
313 2 : monotime_coarse_t now;
314 2 : monotime_coarse_get(&now);
315 :
316 : /* This loop fills our timerslot array with timers of increasing time
317 : * until they fire */
318 54 : for (; i < CHANNELPADDING_MAX_TIMERS; i++) {
319 50 : monotime_coarse_add_msec(&chans[i]->next_padding_time,
320 50 : &now, 10 + i*4);
321 50 : decision = channelpadding_decide_to_pad_channel(chans[i]);
322 50 : tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
323 50 : tt_assert(chans[i]->pending_padding_callback);
324 50 : tt_int_op(tried_to_write_cell, OP_EQ, 0);
325 : }
326 :
327 : /* This loop should add timers to the first position in the timerslot
328 : * array, since its timeout is before all other timers. */
329 18 : for (; i < CHANNELS_TO_TEST/3; i++) {
330 16 : monotime_coarse_add_msec(&chans[i]->next_padding_time,
331 : &now, 1);
332 16 : decision = channelpadding_decide_to_pad_channel(chans[i]);
333 16 : tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
334 16 : tt_assert(chans[i]->pending_padding_callback);
335 16 : tt_int_op(tried_to_write_cell, OP_EQ, 0);
336 : }
337 :
338 : /* This loop should add timers to our existing lists in a weak
339 : * pseudorandom pattern. It ensures that the lists can grow with multiple
340 : * timers in them. */
341 36 : for (; i < CHANNELS_TO_TEST/2; i++) {
342 34 : monotime_coarse_add_msec(&chans[i]->next_padding_time,
343 34 : &now, 10 + i*3 % CHANNELPADDING_MAX_TIMERS);
344 34 : decision = channelpadding_decide_to_pad_channel(chans[i]);
345 34 : tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
346 34 : tt_assert(chans[i]->pending_padding_callback);
347 34 : tt_int_op(tried_to_write_cell, OP_EQ, 0);
348 : }
349 :
350 : /* This loop should add timers to the last position in the timerslot
351 : * array, since its timeout is after all other timers. */
352 102 : for (; i < CHANNELS_TO_TEST; i++) {
353 100 : monotime_coarse_add_msec(&chans[i]->next_padding_time,
354 100 : &now, 500 + i % CHANNELPADDING_MAX_TIMERS);
355 100 : decision = channelpadding_decide_to_pad_channel(chans[i]);
356 100 : tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
357 100 : tt_assert(chans[i]->pending_padding_callback);
358 100 : tt_int_op(tried_to_write_cell, OP_EQ, 0);
359 : }
360 :
361 : // Wait for the timers and then kill the event loop.
362 2 : nsec_mock += 1001 * NSEC_PER_MSEC;
363 2 : monotime_coarse_set_mock_time_nsec(nsec_mock);
364 2 : monotime_set_mock_time_nsec(nsec_mock);
365 2 : timers_run_pending();
366 :
367 2 : tt_int_op(tried_to_write_cell, OP_EQ, CHANNELS_TO_TEST);
368 :
369 : // Test that we have no pending callbacks and all empty slots now
370 202 : for (i = 0; i < CHANNELS_TO_TEST; i++) {
371 200 : tt_assert(!chans[i]->pending_padding_callback);
372 : }
373 : }
374 :
375 1 : done:
376 101 : for (int i = 0; i < CHANNELS_TO_TEST; i++) {
377 100 : free_fake_channeltls((channel_tls_t*)chans[i]);
378 : }
379 1 : smartlist_free(connection_array);
380 :
381 1 : timers_shutdown();
382 1 : monotime_disable_test_mocking();
383 1 : channel_free_all();
384 :
385 1 : return;
386 : }
387 :
388 : void
389 1 : test_channelpadding_killonehop(void *arg)
390 : {
391 1 : channelpadding_decision_t decision;
392 1 : int64_t new_time;
393 1 : (void)arg;
394 :
395 1 : routerstatus_t *relay = tor_malloc_zero(sizeof(routerstatus_t));
396 1 : monotime_init();
397 1 : monotime_enable_test_mocking();
398 1 : monotime_set_mock_time_nsec(1);
399 1 : monotime_coarse_set_mock_time_nsec(1);
400 1 : new_time = 1;
401 :
402 1 : timers_initialize();
403 1 : setup_mock_consensus();
404 1 : setup_mock_network();
405 :
406 : /* Do we disable padding if rsos is enabled, and the consensus says don't
407 : * pad? */
408 :
409 1 : monotime_coarse_t now;
410 1 : monotime_coarse_get(&now);
411 :
412 : // First, test that padding works if either is enabled
413 1 : smartlist_clear(current_md_consensus->net_params);
414 1 : channelpadding_new_consensus_params(current_md_consensus);
415 :
416 1 : relay3_client->padding_enabled = 1;
417 1 : client_relay3->padding_enabled = 1;
418 :
419 1 : tried_to_write_cell = 0;
420 1 : get_options_mutable()->ORPort_set = 0;
421 1 : get_options_mutable()->HiddenServiceSingleHopMode = 1;
422 1 : get_options_mutable()->HiddenServiceNonAnonymousMode = 1;
423 :
424 1 : monotime_coarse_add_msec(&client_relay3->next_padding_time, &now, 100);
425 1 : decision = channelpadding_decide_to_pad_channel(client_relay3);
426 1 : tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
427 1 : tt_assert(client_relay3->pending_padding_callback);
428 1 : tt_int_op(tried_to_write_cell, OP_EQ, 0);
429 :
430 1 : decision = channelpadding_decide_to_pad_channel(client_relay3);
431 1 : tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_ALREADY_SCHEDULED);
432 :
433 : // Wait for the timer
434 1 : new_time += 101 * NSEC_PER_MSEC;
435 1 : monotime_coarse_set_mock_time_nsec(new_time);
436 1 : monotime_set_mock_time_nsec(new_time);
437 1 : monotime_coarse_get(&now);
438 1 : timers_run_pending();
439 1 : tt_int_op(tried_to_write_cell, OP_EQ, 1);
440 1 : tt_assert(!client_relay3->pending_padding_callback);
441 :
442 : // Then test disabling each via consensus param
443 1 : smartlist_add(current_md_consensus->net_params,
444 : (void*)"nf_pad_single_onion=0");
445 1 : channelpadding_new_consensus_params(current_md_consensus);
446 :
447 : // Before the client tries to pad, the relay will still pad:
448 1 : tried_to_write_cell = 0;
449 1 : monotime_coarse_add_msec(&relay3_client->next_padding_time, &now, 100);
450 1 : get_options_mutable()->ORPort_set = 1;
451 1 : get_options_mutable()->HiddenServiceSingleHopMode = 0;
452 1 : get_options_mutable()->HiddenServiceNonAnonymousMode = 0;
453 1 : decision = channelpadding_decide_to_pad_channel(relay3_client);
454 1 : tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
455 1 : tt_assert(relay3_client->pending_padding_callback);
456 :
457 : // Wait for the timer
458 1 : new_time += 101 * NSEC_PER_MSEC;
459 1 : monotime_coarse_set_mock_time_nsec(new_time);
460 1 : monotime_set_mock_time_nsec(new_time);
461 1 : monotime_coarse_get(&now);
462 1 : timers_run_pending();
463 1 : tt_int_op(tried_to_write_cell, OP_EQ, 1);
464 1 : tt_assert(!client_relay3->pending_padding_callback);
465 :
466 : // Test client side (it should stop immediately)
467 1 : get_options_mutable()->HiddenServiceSingleHopMode = 1;
468 1 : get_options_mutable()->HiddenServiceNonAnonymousMode = 1;
469 : /* For the relay to receive the negotiate: */
470 1 : get_options_mutable()->ORPort_set = 1;
471 1 : decision = channelpadding_decide_to_pad_channel(client_relay3);
472 1 : tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
473 1 : tt_assert(!client_relay3->pending_padding_callback);
474 :
475 : // Test relay side (it should have gotten the negotiation to disable)
476 1 : get_options_mutable()->ORPort_set = 1;
477 1 : get_options_mutable()->HiddenServiceSingleHopMode = 0;
478 1 : get_options_mutable()->HiddenServiceNonAnonymousMode = 0;
479 1 : tt_int_op(channelpadding_decide_to_pad_channel(relay3_client), OP_EQ,
480 : CHANNELPADDING_WONTPAD);
481 1 : tt_assert(!relay3_client->padding_enabled);
482 :
483 1 : done:
484 1 : free_mock_consensus();
485 1 : free_mock_network();
486 1 : tor_free(relay);
487 :
488 1 : timers_shutdown();
489 1 : monotime_disable_test_mocking();
490 1 : channel_free_all();
491 1 : }
492 :
493 : void
494 1 : test_channelpadding_consensus(void *arg)
495 : {
496 1 : channelpadding_decision_t decision;
497 1 : or_options_t *options = get_options_mutable();
498 1 : int64_t val;
499 1 : int64_t new_time;
500 1 : (void)arg;
501 :
502 : /*
503 : * Params tested:
504 : * nf_pad_before_usage
505 : * nf_pad_relays
506 : * nf_ito_low
507 : * nf_ito_high
508 : *
509 : * Plan:
510 : * 1. Padding can be completely disabled via consensus
511 : * 2. Negotiation can't re-enable consensus-disabled padding
512 : * 3. Negotiation can't increase padding from relays beyond
513 : * consensus defaults
514 : * 4. Relay-to-relay padding can be enabled/disabled in consensus
515 : * 5. Can enable/disable padding before actually using a connection
516 : * 6. Can we control circ and TLS conn lifetime from the consensus?
517 : */
518 1 : channel_t *chan;
519 1 : routerstatus_t *relay = tor_malloc_zero(sizeof(routerstatus_t));
520 1 : monotime_enable_test_mocking();
521 1 : monotime_set_mock_time_nsec(1);
522 1 : monotime_coarse_set_mock_time_nsec(1);
523 1 : new_time = 1;
524 1 : monotime_coarse_t now;
525 1 : monotime_coarse_get(&now);
526 1 : timers_initialize();
527 :
528 1 : if (!connection_array)
529 0 : connection_array = smartlist_new();
530 1 : chan = (channel_t*)new_fake_channeltls(0);
531 1 : channel_timestamp_active(chan);
532 :
533 1 : setup_mock_consensus();
534 :
535 1 : get_options_mutable()->ORPort_set = 1;
536 :
537 : /* Test 1: Padding can be completely disabled via consensus */
538 1 : tried_to_write_cell = 0;
539 1 : monotime_coarse_add_msec(&chan->next_padding_time, &now, 100);
540 1 : decision = channelpadding_decide_to_pad_channel(chan);
541 1 : tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
542 1 : tt_assert(chan->pending_padding_callback);
543 1 : tt_int_op(tried_to_write_cell, OP_EQ, 0);
544 :
545 1 : decision = channelpadding_decide_to_pad_channel(chan);
546 1 : tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_ALREADY_SCHEDULED);
547 :
548 : // Wait for the timer
549 1 : new_time += 101*NSEC_PER_MSEC;
550 1 : monotime_coarse_set_mock_time_nsec(new_time);
551 1 : monotime_set_mock_time_nsec(new_time);
552 1 : monotime_coarse_get(&now);
553 1 : timers_run_pending();
554 1 : tt_int_op(tried_to_write_cell, OP_EQ, 1);
555 1 : tt_assert(!chan->pending_padding_callback);
556 :
557 1 : smartlist_add(current_md_consensus->net_params,
558 : (void*)"nf_ito_low=0");
559 1 : smartlist_add(current_md_consensus->net_params,
560 : (void*)"nf_ito_high=0");
561 1 : get_options_mutable()->ConnectionPadding = 1;
562 1 : channelpadding_new_consensus_params(current_md_consensus);
563 :
564 1 : decision = channelpadding_decide_to_pad_channel(chan);
565 1 : tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
566 1 : tt_assert(!chan->pending_padding_callback);
567 1 : val = channelpadding_get_netflow_inactive_timeout_ms(chan);
568 1 : tt_i64_op(val, OP_EQ, 0);
569 1 : val = channelpadding_compute_time_until_pad_for_netflow(chan);
570 1 : tt_i64_op(val, OP_EQ, -2);
571 :
572 : /* Test 2: Negotiation can't re-enable consensus-disabled padding */
573 1 : channelpadding_send_enable_command(chan, 100, 200);
574 1 : tried_to_write_cell = 0;
575 1 : decision = channelpadding_decide_to_pad_channel(chan);
576 1 : tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
577 1 : tt_assert(!chan->pending_padding_callback);
578 1 : val = channelpadding_get_netflow_inactive_timeout_ms(chan);
579 1 : tt_i64_op(val, OP_EQ, 0);
580 1 : val = channelpadding_compute_time_until_pad_for_netflow(chan);
581 1 : tt_i64_op(val, OP_EQ, -2);
582 1 : tt_assert(monotime_coarse_is_zero(&chan->next_padding_time));
583 :
584 1 : smartlist_clear(current_md_consensus->net_params);
585 :
586 : /* Test 3: Negotiation can't increase padding from relays beyond consensus
587 : * values */
588 1 : smartlist_add(current_md_consensus->net_params,
589 : (void*)"nf_ito_low=100");
590 1 : smartlist_add(current_md_consensus->net_params,
591 : (void*)"nf_ito_high=200");
592 1 : channelpadding_new_consensus_params(current_md_consensus);
593 :
594 1 : tried_to_write_cell = 0;
595 1 : monotime_coarse_add_msec(&chan->next_padding_time, &now, 100);
596 1 : decision = channelpadding_decide_to_pad_channel(chan);
597 1 : tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
598 1 : tt_assert(chan->pending_padding_callback);
599 1 : tt_int_op(tried_to_write_cell, OP_EQ, 0);
600 1 : val = channelpadding_get_netflow_inactive_timeout_ms(chan);
601 1 : tt_i64_op(val, OP_GE, 100);
602 1 : tt_i64_op(val, OP_LE, 200);
603 1 : val = channelpadding_compute_time_until_pad_for_netflow(chan);
604 1 : tt_i64_op(val, OP_LE, 200);
605 :
606 : // Wait for the timer
607 1 : new_time += 201*NSEC_PER_MSEC;
608 1 : monotime_set_mock_time_nsec(new_time);
609 1 : monotime_coarse_set_mock_time_nsec(new_time);
610 1 : monotime_coarse_get(&now);
611 1 : timers_run_pending();
612 1 : tt_int_op(tried_to_write_cell, OP_EQ, 1);
613 1 : tt_assert(!chan->pending_padding_callback);
614 :
615 1 : smartlist_clear(current_md_consensus->net_params);
616 1 : smartlist_add(current_md_consensus->net_params,
617 : (void*)"nf_ito_low=1500");
618 1 : smartlist_add(current_md_consensus->net_params,
619 : (void*)"nf_ito_high=4500");
620 1 : channelpadding_new_consensus_params(current_md_consensus);
621 :
622 1 : channelpadding_send_enable_command(chan, 100, 200);
623 1 : tried_to_write_cell = 0;
624 1 : decision = channelpadding_decide_to_pad_channel(chan);
625 1 : tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
626 1 : tt_assert(!chan->pending_padding_callback);
627 1 : val = channelpadding_get_netflow_inactive_timeout_ms(chan);
628 1 : tt_i64_op(val, OP_GE, 1500);
629 1 : tt_i64_op(val, OP_LE, 4500);
630 1 : val = channelpadding_compute_time_until_pad_for_netflow(chan);
631 1 : tt_i64_op(val, OP_LE, 4500);
632 :
633 : /* Test 4: Relay-to-relay padding can be enabled/disabled in consensus */
634 : /* Make this channel a relay's channel */
635 1 : memcpy(relay->identity_digest,
636 1 : ((channel_tls_t *)chan)->conn->identity_digest, DIGEST_LEN);
637 1 : smartlist_add(current_md_consensus->routerstatus_list, relay);
638 1 : relay = NULL; /* Prevent double-free */
639 :
640 1 : tried_to_write_cell = 0;
641 1 : decision = channelpadding_decide_to_pad_channel(chan);
642 1 : tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
643 1 : tt_assert(!chan->pending_padding_callback);
644 :
645 1 : smartlist_add(current_md_consensus->net_params,
646 : (void*)"nf_pad_relays=1");
647 1 : channelpadding_new_consensus_params(current_md_consensus);
648 :
649 1 : decision = channelpadding_decide_to_pad_channel(chan);
650 1 : tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
651 1 : tt_assert(!chan->pending_padding_callback);
652 1 : val = channelpadding_get_netflow_inactive_timeout_ms(chan);
653 1 : tt_i64_op(val, OP_GE, 1500);
654 1 : tt_i64_op(val, OP_LE, 4500);
655 1 : val = channelpadding_compute_time_until_pad_for_netflow(chan);
656 1 : tt_i64_op(val, OP_LE, 4500);
657 :
658 : /* Test 5: If we disable padding before channel usage, does that work? */
659 1 : smartlist_add(current_md_consensus->net_params,
660 : (void*)"nf_pad_before_usage=0");
661 1 : channelpadding_new_consensus_params(current_md_consensus);
662 1 : tried_to_write_cell = 0;
663 1 : decision = channelpadding_decide_to_pad_channel(chan);
664 1 : tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
665 1 : tt_assert(!chan->pending_padding_callback);
666 :
667 : /* Test 6: Can we control circ and TLS conn lifetime from the consensus? */
668 1 : val = channelpadding_get_channel_idle_timeout(NULL, 0);
669 1 : tt_i64_op(val, OP_GE, 180);
670 1 : tt_i64_op(val, OP_LE, 180+90);
671 1 : val = channelpadding_get_channel_idle_timeout(chan, 0);
672 1 : tt_i64_op(val, OP_GE, 180);
673 1 : tt_i64_op(val, OP_LE, 180+90);
674 1 : options->ReducedConnectionPadding = 1;
675 1 : val = channelpadding_get_channel_idle_timeout(chan, 0);
676 1 : tt_i64_op(val, OP_GE, 180/2);
677 1 : tt_i64_op(val, OP_LE, (180+90)/2);
678 :
679 1 : options->ReducedConnectionPadding = 0;
680 1 : options->ORPort_set = 1;
681 1 : smartlist_add(current_md_consensus->net_params,
682 : (void*)"nf_conntimeout_relays=600");
683 1 : channelpadding_new_consensus_params(current_md_consensus);
684 1 : val = channelpadding_get_channel_idle_timeout(chan, 1);
685 1 : tt_i64_op(val, OP_GE, 450);
686 1 : tt_i64_op(val, OP_LE, 750);
687 :
688 1 : val = channelpadding_get_circuits_available_timeout();
689 1 : tt_i64_op(val, OP_GE, 30*60);
690 1 : tt_i64_op(val, OP_LE, 30*60*2);
691 :
692 1 : options->ReducedConnectionPadding = 1;
693 1 : smartlist_add(current_md_consensus->net_params,
694 : (void*)"nf_conntimeout_clients=600");
695 1 : channelpadding_new_consensus_params(current_md_consensus);
696 1 : val = channelpadding_get_circuits_available_timeout();
697 1 : tt_i64_op(val, OP_GE, 600/2);
698 1 : tt_i64_op(val, OP_LE, 600*2/2);
699 :
700 1 : options->ReducedConnectionPadding = 0;
701 1 : options->CircuitsAvailableTimeout = 24*60*60;
702 1 : val = channelpadding_get_circuits_available_timeout();
703 1 : tt_i64_op(val, OP_GE, 24*60*60);
704 1 : tt_i64_op(val, OP_LE, 24*60*60*2);
705 :
706 1 : done:
707 1 : tor_free(relay);
708 :
709 1 : free_mock_consensus();
710 1 : free_fake_channeltls((channel_tls_t*)chan);
711 1 : smartlist_free(connection_array);
712 :
713 1 : timers_shutdown();
714 1 : monotime_disable_test_mocking();
715 1 : channel_free_all();
716 :
717 1 : return;
718 : }
719 :
720 : void
721 1 : test_channelpadding_negotiation(void *arg)
722 : {
723 1 : channelpadding_negotiate_t disable;
724 1 : cell_t cell;
725 1 : channelpadding_decision_t decision;
726 1 : int val;
727 1 : (void)arg;
728 :
729 : /* Plan:
730 : * 1. Clients reject negotiation, relays accept it.
731 : * * Bridges accept negotiation from their clients,
732 : * but not from relays.
733 : * 2. Torrc options can override client-side negotiation
734 : * 3. Test a version issue in channelpadidng cell
735 : * 4. Test channelpadding_reduced_padding
736 : */
737 1 : monotime_init();
738 1 : monotime_enable_test_mocking();
739 1 : monotime_set_mock_time_nsec(1);
740 1 : monotime_coarse_set_mock_time_nsec(1);
741 1 : timers_initialize();
742 1 : setup_mock_consensus();
743 1 : setup_mock_network();
744 :
745 : /* Test case #1: Do the right things ignore negotiation? */
746 : /* relay-to-client case: */
747 1 : channelpadding_send_disable_command(relay3_client);
748 1 : tt_assert(client_relay3->padding_enabled);
749 :
750 : /* client-to-relay case: */
751 1 : get_options_mutable()->ORPort_set = 1;
752 1 : channelpadding_disable_padding_on_channel(client_relay3);
753 1 : tt_int_op(channelpadding_decide_to_pad_channel(relay3_client), OP_EQ,
754 : CHANNELPADDING_WONTPAD);
755 1 : tt_assert(!relay3_client->padding_enabled);
756 1 : relay3_client->padding_enabled = 1;
757 1 : client_relay3->padding_enabled = 1;
758 :
759 : /* Bridge case from relay */
760 1 : get_options_mutable()->BridgeRelay = 1;
761 1 : channelpadding_disable_padding_on_channel(relay2_relay1);
762 1 : tt_assert(relay1_relay2->padding_enabled);
763 :
764 : /* Bridge case from client */
765 1 : channelpadding_disable_padding_on_channel(client_relay3);
766 1 : tt_assert(!relay3_client->padding_enabled);
767 1 : tt_int_op(channelpadding_decide_to_pad_channel(relay3_client), OP_EQ,
768 : CHANNELPADDING_WONTPAD);
769 1 : relay3_client->padding_enabled = 1;
770 1 : client_relay3->padding_enabled = 1;
771 1 : get_options_mutable()->BridgeRelay = 0;
772 1 : get_options_mutable()->ORPort_set = 0;
773 :
774 : /* Test case #2: Torrc options */
775 : /* ConnectionPadding auto; Relay doesn't support us */
776 1 : ((channel_tls_t*)relay3_client)->conn->link_proto = 4;
777 1 : relay3_client->padding_enabled = 0;
778 1 : tried_to_write_cell = 0;
779 1 : decision = channelpadding_decide_to_pad_channel(relay3_client);
780 1 : tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
781 1 : tt_assert(!relay3_client->pending_padding_callback);
782 1 : tt_int_op(tried_to_write_cell, OP_EQ, 0);
783 1 : ((channel_tls_t*)relay3_client)->conn->link_proto = 5;
784 1 : relay3_client->padding_enabled = 1;
785 :
786 : /* ConnectionPadding 1; Relay doesn't support us */
787 1 : get_options_mutable()->ConnectionPadding = 1;
788 1 : tried_to_write_cell = 0;
789 1 : decision = channelpadding_decide_to_pad_channel(client_relay3);
790 1 : tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
791 1 : tt_assert(!client_relay3->pending_padding_callback);
792 1 : tt_int_op(tried_to_write_cell, OP_EQ, 0);
793 1 : get_options_mutable()->ConnectionPadding = 0;
794 :
795 : /* Test case #3: Test a version issue in channelpadding cell */
796 1 : get_options_mutable()->ORPort_set = 1;
797 1 : client_relay3->padding_enabled = 1;
798 1 : relay3_client->padding_enabled = 1;
799 1 : memset(&cell, 0, sizeof(cell_t));
800 1 : memset(&disable, 0, sizeof(channelpadding_negotiate_t));
801 1 : cell.command = CELL_PADDING_NEGOTIATE;
802 :
803 1 : channelpadding_negotiate_set_command(&disable, CHANNELPADDING_COMMAND_STOP);
804 1 : disable.version = 1;
805 1 : channelpadding_negotiate_encode(cell.payload, CELL_PAYLOAD_SIZE, &disable);
806 1 : client_relay3->write_cell(client_relay3, &cell);
807 1 : tt_assert(relay3_client->padding_enabled);
808 1 : tt_int_op(channelpadding_update_padding_for_channel(client_relay3, &disable),
809 : OP_EQ, -1);
810 1 : tt_assert(client_relay3->padding_enabled);
811 :
812 1 : disable.version = 0;
813 1 : channelpadding_negotiate_encode(cell.payload, CELL_PAYLOAD_SIZE, &disable);
814 1 : client_relay3->write_cell(client_relay3, &cell);
815 1 : tt_assert(!relay3_client->padding_enabled);
816 :
817 : /* Test case 4: Reducing padding actually reduces it */
818 1 : relay3_client->padding_enabled = 1;
819 1 : client_relay3->padding_enabled = 1;
820 :
821 1 : decision = channelpadding_decide_to_pad_channel(relay3_client);
822 1 : tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
823 :
824 1 : channelpadding_reduce_padding_on_channel(client_relay3);
825 :
826 1 : tried_to_write_cell = 0;
827 1 : decision = channelpadding_decide_to_pad_channel(relay3_client);
828 1 : tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
829 :
830 1 : get_options_mutable()->ORPort_set = 0;
831 1 : decision = channelpadding_decide_to_pad_channel(client_relay3);
832 1 : tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
833 :
834 1 : tt_assert(!client_relay3->pending_padding_callback);
835 1 : val = channelpadding_get_netflow_inactive_timeout_ms(client_relay3);
836 1 : tt_int_op(val, OP_GE, 9000);
837 1 : tt_int_op(val, OP_LE, 14000);
838 1 : int64_t val64 =
839 1 : channelpadding_compute_time_until_pad_for_netflow(client_relay3);
840 1 : tt_i64_op(val64, OP_LE, 14000);
841 :
842 1 : done:
843 1 : free_mock_network();
844 1 : free_mock_consensus();
845 :
846 1 : timers_shutdown();
847 1 : monotime_disable_test_mocking();
848 1 : channel_free_all();
849 :
850 1 : return;
851 : }
852 :
853 : void
854 1 : test_channelpadding_decide_to_pad_channel(void *arg)
855 : {
856 1 : channelpadding_decision_t decision;
857 : /**
858 : * Test case plan:
859 : *
860 : * 1. Channel that has "sent a packet" before the timeout.
861 : * + We should decide to pad later
862 : * 2. Channel that has not "sent a packet" before the timeout:
863 : * 2a. Not within 1.1s of the timeout.
864 : * + We should decide to pad later
865 : * 2b. Within 1.1s of the timemout.
866 : * + We should schedule padding
867 : * + We should get feedback that we wrote a cell
868 : * 2c. Within 0.1s of the timeout.
869 : * + We should schedule padding
870 : * + We should get feedback that we wrote a cell
871 : * 2d. Channel that asks to pad while timeout is scheduled
872 : * + We should schedule padding
873 : * + We should get feedback that we wrote a cell
874 : * 2e. 0s of the timeout
875 : * + We should send padding immediately
876 : * + We should get feedback that we wrote a cell
877 : * 2f. <0s of the timeout
878 : * + We should send padding immediately
879 : * + We should get feedback that we wrote a cell
880 : * 3. Channel that sends a packet while timeout is scheduled
881 : * + We should not get feedback that we wrote a cell
882 : * 4. Channel that closes while timeout is scheduled
883 : * + We should not get feedback that we wrote a cell
884 : * 5. Make sure the channel still would work if repaired
885 : * + We should be able to schedule padding and resend
886 : * 6. Channel is not used for full circuits
887 : * 7. Channel that disappears while timeout is scheduled
888 : * + We should not send padding
889 : */
890 1 : channel_t *chan;
891 1 : int64_t new_time;
892 1 : if (!connection_array)
893 0 : connection_array = smartlist_new();
894 1 : (void)arg;
895 :
896 1 : monotime_init();
897 1 : monotime_enable_test_mocking();
898 1 : monotime_set_mock_time_nsec(1);
899 1 : monotime_coarse_set_mock_time_nsec(1);
900 1 : new_time = 1;
901 1 : monotime_coarse_t now;
902 1 : monotime_coarse_get(&now);
903 1 : timers_initialize();
904 1 : setup_full_capture_of_logs(LOG_WARN);
905 1 : channelpadding_new_consensus_params(NULL);
906 :
907 1 : chan = (channel_t*)new_fake_channeltls(0);
908 1 : channel_timestamp_active(chan);
909 :
910 : /* Test case #1: Channel that has "sent a packet" before the timeout. */
911 1 : tried_to_write_cell = 0;
912 1 : decision = channelpadding_decide_to_pad_channel(chan);
913 1 : tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
914 1 : tt_assert(!chan->pending_padding_callback);
915 1 : tt_int_op(tried_to_write_cell, OP_EQ, 0);
916 :
917 : /* Test case #2a: > 1.1s until timeout */
918 1 : tried_to_write_cell = 0;
919 1 : monotime_coarse_add_msec(&chan->next_padding_time, &now, 1200);
920 1 : decision = channelpadding_decide_to_pad_channel(chan);
921 1 : tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
922 1 : tt_assert(!chan->pending_padding_callback);
923 1 : tt_int_op(tried_to_write_cell, OP_EQ, 0);
924 :
925 : /* Test case #2b: >= 1.0s until timeout */
926 1 : tried_to_write_cell = 0;
927 1 : monotime_coarse_add_msec(&chan->next_padding_time, &now, 1000);
928 1 : decision = channelpadding_decide_to_pad_channel(chan);
929 1 : tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
930 1 : tt_assert(chan->pending_padding_callback);
931 1 : tt_int_op(tried_to_write_cell, OP_EQ, 0);
932 :
933 : // Set up a timer for the <0 case below.
934 1 : monotime_coarse_t now_minus_100s;
935 1 : monotime_coarse_add_msec(&now_minus_100s, &now, 900);
936 : // Wait for the timer from case #2b
937 1 : new_time += 1000*NSEC_PER_MSEC;
938 1 : monotime_set_mock_time_nsec(new_time);
939 1 : monotime_coarse_set_mock_time_nsec(new_time);
940 1 : monotime_coarse_get(&now);
941 1 : timers_run_pending();
942 1 : tt_int_op(tried_to_write_cell, OP_EQ, 1);
943 1 : tt_assert(!chan->pending_padding_callback);
944 :
945 : /* Test case #2c: > 0.1s until timeout */
946 1 : tried_to_write_cell = 0;
947 1 : monotime_coarse_add_msec(&chan->next_padding_time, &now, 100);
948 1 : decision = channelpadding_decide_to_pad_channel(chan);
949 1 : tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
950 1 : tt_assert(chan->pending_padding_callback);
951 1 : tt_int_op(tried_to_write_cell, OP_EQ, 0);
952 :
953 : /* Test case #2d: Channel that asks to pad while timeout is scheduled */
954 1 : decision = channelpadding_decide_to_pad_channel(chan);
955 1 : tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_ALREADY_SCHEDULED);
956 :
957 : // Wait for the timer
958 1 : new_time += 101*NSEC_PER_MSEC;
959 1 : monotime_coarse_set_mock_time_nsec(new_time);
960 1 : monotime_set_mock_time_nsec(new_time);
961 1 : monotime_coarse_get(&now);
962 1 : timers_run_pending();
963 1 : tt_int_op(tried_to_write_cell, OP_EQ, 1);
964 1 : tt_assert(!chan->pending_padding_callback);
965 :
966 : /* Test case #2e: 0s until timeout */
967 1 : tried_to_write_cell = 0;
968 1 : monotime_coarse_add_msec(&chan->next_padding_time, &now, 0);
969 1 : decision = channelpadding_decide_to_pad_channel(chan);
970 1 : tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SENT);
971 1 : tt_int_op(tried_to_write_cell, OP_EQ, 1);
972 1 : tt_assert(!chan->pending_padding_callback);
973 :
974 : /* Test case #2f: <0s until timeout */
975 1 : tried_to_write_cell = 0;
976 1 : monotime_coarse_add_msec(&chan->next_padding_time, &now_minus_100s, 0);
977 1 : decision = channelpadding_decide_to_pad_channel(chan);
978 1 : tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SENT);
979 1 : tt_int_op(tried_to_write_cell, OP_EQ, 1);
980 1 : tt_assert(!chan->pending_padding_callback);
981 :
982 : /* Test case #3: Channel that sends a packet while timeout is scheduled */
983 1 : tried_to_write_cell = 0;
984 1 : monotime_coarse_add_msec(&chan->next_padding_time, &now, 100);
985 1 : decision = channelpadding_decide_to_pad_channel(chan);
986 1 : tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
987 1 : tt_int_op(tried_to_write_cell, OP_EQ, 0);
988 1 : tt_assert(chan->pending_padding_callback);
989 :
990 : // Pretend the channel sent a packet
991 1 : channel_timestamp_active(chan);
992 :
993 : // We don't expect any timer callbacks here. Make a dummy one to be sure.
994 : // Wait for the timer
995 1 : new_time += 101*NSEC_PER_MSEC;
996 1 : monotime_coarse_set_mock_time_nsec(new_time);
997 1 : monotime_set_mock_time_nsec(new_time);
998 1 : monotime_coarse_get(&now);
999 1 : timers_run_pending();
1000 :
1001 1 : tt_int_op(tried_to_write_cell, OP_EQ, 0);
1002 1 : tt_assert(!chan->pending_padding_callback);
1003 :
1004 : /* Test case #4: Channel that closes while a timeout is scheduled */
1005 1 : tried_to_write_cell = 0;
1006 1 : monotime_coarse_add_msec(&chan->next_padding_time, &now, 100);
1007 1 : decision = channelpadding_decide_to_pad_channel(chan);
1008 1 : tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
1009 1 : tt_int_op(tried_to_write_cell, OP_EQ, 0);
1010 1 : tt_assert(chan->pending_padding_callback);
1011 :
1012 : // Pretend the channel is temporarily down
1013 1 : chan->state = CHANNEL_STATE_MAINT;
1014 :
1015 : // We don't expect any timer callbacks here. Make a dummy one to be sure.
1016 1 : new_time += 101*NSEC_PER_MSEC;
1017 1 : monotime_coarse_set_mock_time_nsec(new_time);
1018 1 : monotime_set_mock_time_nsec(new_time);
1019 1 : monotime_coarse_get(&now);
1020 1 : timers_run_pending();
1021 :
1022 1 : tt_int_op(tried_to_write_cell, OP_EQ, 0);
1023 1 : tt_assert(!chan->pending_padding_callback);
1024 1 : chan->state = CHANNEL_STATE_OPEN;
1025 :
1026 : /* Test case #5: Make sure previous test case didn't break everything */
1027 1 : tried_to_write_cell = 0;
1028 1 : monotime_coarse_add_msec(&chan->next_padding_time, &now, 100);
1029 1 : decision = channelpadding_decide_to_pad_channel(chan);
1030 1 : tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
1031 1 : tt_assert(chan->pending_padding_callback);
1032 1 : tt_int_op(tried_to_write_cell, OP_EQ, 0);
1033 :
1034 : // Wait for the timer
1035 1 : new_time += 101*NSEC_PER_MSEC;
1036 1 : monotime_coarse_set_mock_time_nsec(new_time);
1037 1 : monotime_set_mock_time_nsec(new_time);
1038 1 : monotime_coarse_get(&now);
1039 1 : timers_run_pending();
1040 :
1041 1 : tt_int_op(tried_to_write_cell, OP_EQ, 1);
1042 1 : tt_assert(!chan->pending_padding_callback);
1043 :
1044 : /* Test case #6. Channel is not used for full circuits */
1045 1 : chan->channel_usage = CHANNEL_USED_NOT_USED_FOR_FULL_CIRCS;
1046 1 : decision = channelpadding_decide_to_pad_channel(chan);
1047 1 : tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
1048 1 : tt_assert(!chan->pending_padding_callback);
1049 1 : chan->channel_usage = CHANNEL_USED_FOR_FULL_CIRCS;
1050 :
1051 : /* Test case #7. Channel is closed while timeout is scheduled.
1052 : *
1053 : * NOTE: This test deliberately breaks the channel callback mechanism.
1054 : * It must be last.
1055 : */
1056 1 : tried_to_write_cell = 0;
1057 1 : monotime_coarse_add_msec(&chan->next_padding_time, &now, 100);
1058 1 : decision = channelpadding_decide_to_pad_channel(chan);
1059 1 : tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
1060 1 : tt_int_op(tried_to_write_cell, OP_EQ, 0);
1061 1 : tt_assert(chan->pending_padding_callback);
1062 :
1063 : // Close the connection while the timer is scheduled
1064 1 : free_fake_channeltls((channel_tls_t*)chan);
1065 :
1066 : // We don't expect any timer callbacks here. Make a dummy one to be sure.
1067 1 : new_time = 101*NSEC_PER_MSEC;
1068 1 : monotime_coarse_set_mock_time_nsec(new_time);
1069 1 : monotime_set_mock_time_nsec(new_time);
1070 1 : monotime_coarse_get(&now);
1071 1 : timers_run_pending();
1072 :
1073 1 : tt_int_op(tried_to_write_cell, OP_EQ, 0);
1074 :
1075 1 : done:
1076 1 : smartlist_free(connection_array);
1077 :
1078 1 : teardown_capture_of_logs();
1079 1 : monotime_disable_test_mocking();
1080 1 : timers_shutdown();
1081 1 : channel_free_all();
1082 :
1083 1 : return;
1084 : }
1085 :
1086 : #define TEST_CHANNELPADDING(name, flags) \
1087 : { #name, test_##name, (flags), NULL, NULL }
1088 :
1089 : struct testcase_t channelpadding_tests[] = {
1090 : //TEST_CHANNELPADDING(channelpadding_decide_to_pad_channel, 0),
1091 : TEST_CHANNELPADDING(channelpadding_decide_to_pad_channel, TT_FORK),
1092 : TEST_CHANNELPADDING(channelpadding_negotiation, TT_FORK),
1093 : TEST_CHANNELPADDING(channelpadding_consensus, TT_FORK),
1094 : TEST_CHANNELPADDING(channelpadding_killonehop, TT_FORK),
1095 : TEST_CHANNELPADDING(channelpadding_timers, TT_FORK),
1096 : END_OF_TESTCASES
1097 : };
|