cmux: Make EWMA policy mandatory
[tor.git] / src / test / test_channel.c
blob39275976876c3539bcccb02b2d86aea6f11464e5
1 /* Copyright (c) 2013-2017, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 #define TOR_CHANNEL_INTERNAL_
5 #define CHANNEL_PRIVATE_
6 #include "or.h"
7 #include "channel.h"
8 /* For channel_note_destroy_not_pending */
9 #define CIRCUITLIST_PRIVATE
10 #include "circuitlist.h"
11 #include "circuitmux.h"
12 #include "circuitmux_ewma.h"
13 /* For var_cell_free */
14 #include "connection_or.h"
15 /* For packed_cell stuff */
16 #define RELAY_PRIVATE
17 #include "relay.h"
18 /* For init/free stuff */
19 #include "scheduler.h"
20 #include "networkstatus.h"
22 /* Test suite stuff */
23 #include "log_test_helpers.h"
24 #include "test.h"
25 #include "fakechans.h"
27 static int test_chan_accept_cells = 0;
28 static int test_chan_fixed_cells_recved = 0;
29 static cell_t * test_chan_last_seen_fixed_cell_ptr = NULL;
30 static int test_chan_var_cells_recved = 0;
31 static var_cell_t * test_chan_last_seen_var_cell_ptr = NULL;
32 static int test_cells_written = 0;
33 static int test_doesnt_want_writes_count = 0;
34 static int test_dumpstats_calls = 0;
35 static int test_has_waiting_cells_count = 0;
36 static int test_releases_count = 0;
37 static channel_t *dump_statistics_mock_target = NULL;
38 static int dump_statistics_mock_matches = 0;
39 static int test_close_called = 0;
40 static int test_chan_should_be_canonical = 0;
41 static int test_chan_should_match_target = 0;
42 static int test_chan_canonical_should_be_reliable = 0;
43 static int test_chan_listener_close_fn_called = 0;
44 static int test_chan_listener_fn_called = 0;
46 static const char *
47 chan_test_describe_transport(channel_t *ch)
49 tt_ptr_op(ch, OP_NE, NULL);
51 done:
52 return "Fake channel for unit tests";
55 /**
56 * Mock for channel_dump_statistics(); if the channel matches the
57 * target, bump a counter - otherwise ignore.
60 static void
61 chan_test_channel_dump_statistics_mock(channel_t *chan, int severity)
63 tt_ptr_op(chan, OP_NE, NULL);
65 (void)severity;
67 if (chan != NULL && chan == dump_statistics_mock_target) {
68 ++dump_statistics_mock_matches;
71 done:
72 return;
76 * Handle an incoming fixed-size cell for unit tests
79 static void
80 chan_test_cell_handler(channel_t *chan, cell_t *cell)
82 tt_assert(chan);
83 tt_assert(cell);
85 test_chan_last_seen_fixed_cell_ptr = cell;
86 ++test_chan_fixed_cells_recved;
88 done:
89 return;
93 * Fake transport-specific stats call
96 static void
97 chan_test_dumpstats(channel_t *ch, int severity)
99 tt_ptr_op(ch, OP_NE, NULL);
101 (void)severity;
103 ++test_dumpstats_calls;
105 done:
106 return;
110 * Handle an incoming variable-size cell for unit tests
113 static void
114 chan_test_var_cell_handler(channel_t *ch,
115 var_cell_t *var_cell)
117 tt_assert(ch);
118 tt_assert(var_cell);
120 test_chan_last_seen_var_cell_ptr = var_cell;
121 ++test_chan_var_cells_recved;
123 done:
124 return;
127 static void
128 chan_test_close(channel_t *ch)
130 tt_assert(ch);
132 ++test_close_called;
134 done:
135 return;
139 * Close a channel through the error path
142 static void
143 chan_test_error(channel_t *ch)
145 tt_assert(ch);
146 tt_assert(!(ch->state == CHANNEL_STATE_CLOSING ||
147 ch->state == CHANNEL_STATE_ERROR ||
148 ch->state == CHANNEL_STATE_CLOSED));
150 channel_close_for_error(ch);
152 done:
153 return;
157 * Finish closing a channel from CHANNEL_STATE_CLOSING
160 static void
161 chan_test_finish_close(channel_t *ch)
163 tt_assert(ch);
164 tt_assert(ch->state == CHANNEL_STATE_CLOSING);
166 channel_closed(ch);
168 done:
169 return;
172 static const char *
173 chan_test_get_remote_descr(channel_t *ch, int flags)
175 tt_assert(ch);
176 tt_int_op(flags & ~(GRD_FLAG_ORIGINAL | GRD_FLAG_ADDR_ONLY), OP_EQ, 0);
178 done:
179 return "Fake channel for unit tests; no real endpoint";
182 static int
183 chan_test_num_cells_writeable(channel_t *ch)
185 tt_assert(ch);
187 done:
188 return 32;
191 static int
192 chan_test_write_packed_cell(channel_t *ch,
193 packed_cell_t *packed_cell)
195 int rv = 0;
197 tt_assert(ch);
198 tt_assert(packed_cell);
200 if (test_chan_accept_cells) {
201 /* Free the cell and bump the counter */
202 ++test_cells_written;
203 rv = 1;
205 /* else return 0, we didn't accept it */
207 done:
208 return rv;
211 static int
212 chan_test_write_var_cell(channel_t *ch, var_cell_t *var_cell)
214 int rv = 0;
216 tt_assert(ch);
217 tt_assert(var_cell);
219 if (test_chan_accept_cells) {
220 /* Free the cell and bump the counter */
221 var_cell_free(var_cell);
222 ++test_cells_written;
223 rv = 1;
225 /* else return 0, we didn't accept it */
227 done:
228 return rv;
232 * Fill out c with a new fake cell for test suite use
235 void
236 make_fake_cell(cell_t *c)
238 tt_ptr_op(c, OP_NE, NULL);
240 c->circ_id = 1;
241 c->command = CELL_RELAY;
242 memset(c->payload, 0, CELL_PAYLOAD_SIZE);
244 done:
245 return;
249 * Fill out c with a new fake var_cell for test suite use
252 void
253 make_fake_var_cell(var_cell_t *c)
255 tt_ptr_op(c, OP_NE, NULL);
257 c->circ_id = 1;
258 c->command = CELL_VERSIONS;
259 c->payload_len = CELL_PAYLOAD_SIZE / 2;
260 memset(c->payload, 0, c->payload_len);
262 done:
263 return;
267 * Set up a new fake channel for the test suite
270 channel_t *
271 new_fake_channel(void)
273 channel_t *chan = tor_malloc_zero(sizeof(channel_t));
274 channel_init(chan);
276 chan->close = chan_test_close;
277 chan->num_cells_writeable = chan_test_num_cells_writeable;
278 chan->get_remote_descr = chan_test_get_remote_descr;
279 chan->write_packed_cell = chan_test_write_packed_cell;
280 chan->write_var_cell = chan_test_write_var_cell;
281 chan->state = CHANNEL_STATE_OPEN;
283 chan->cmux = circuitmux_alloc();
285 return chan;
288 void
289 free_fake_channel(channel_t *chan)
291 if (! chan)
292 return;
294 if (chan->cmux)
295 circuitmux_free(chan->cmux);
297 tor_free(chan);
301 * Counter query for scheduler_channel_has_waiting_cells_mock()
305 get_mock_scheduler_has_waiting_cells_count(void)
307 return test_has_waiting_cells_count;
311 * Mock for scheduler_channel_has_waiting_cells()
314 void
315 scheduler_channel_has_waiting_cells_mock(channel_t *ch)
317 (void)ch;
319 /* Increment counter */
320 ++test_has_waiting_cells_count;
322 return;
325 static void
326 scheduler_channel_doesnt_want_writes_mock(channel_t *ch)
328 (void)ch;
330 /* Increment counter */
331 ++test_doesnt_want_writes_count;
333 return;
337 * Mock for scheduler_release_channel()
340 void
341 scheduler_release_channel_mock(channel_t *ch)
343 (void)ch;
345 /* Increment counter */
346 ++test_releases_count;
348 return;
351 static int
352 test_chan_is_canonical(channel_t *chan, int req)
354 tor_assert(chan);
356 if (req && test_chan_canonical_should_be_reliable) {
357 return 1;
360 if (test_chan_should_be_canonical) {
361 return 1;
363 return 0;
366 static int
367 test_chan_matches_target(channel_t *chan, const tor_addr_t *target)
369 (void) chan;
370 (void) target;
372 if (test_chan_should_match_target) {
373 return 1;
375 return 0;
378 static void
379 test_chan_listener_close(channel_listener_t *chan)
381 (void) chan;
382 ++test_chan_listener_close_fn_called;
383 return;
386 static void
387 test_chan_listener_fn(channel_listener_t *listener, channel_t *chan)
389 (void) listener;
390 (void) chan;
392 ++test_chan_listener_fn_called;
393 return;
396 static const char *
397 test_chan_listener_describe_transport(channel_listener_t *chan)
399 (void) chan;
400 return "Fake listener channel.";
404 * Test for channel_dumpstats() and limited test for
405 * channel_dump_statistics()
408 static void
409 test_channel_dumpstats(void *arg)
411 channel_t *ch = NULL;
412 cell_t *cell = NULL;
413 packed_cell_t *p_cell = NULL;
414 int old_count;
416 (void)arg;
418 /* Mock these for duration of the test */
419 MOCK(scheduler_channel_doesnt_want_writes,
420 scheduler_channel_doesnt_want_writes_mock);
421 MOCK(scheduler_release_channel,
422 scheduler_release_channel_mock);
424 /* Set up a new fake channel */
425 ch = new_fake_channel();
426 tt_assert(ch);
428 /* Try to register it */
429 channel_register(ch);
430 tt_assert(ch->registered);
432 /* Set up mock */
433 dump_statistics_mock_target = ch;
434 dump_statistics_mock_matches = 0;
435 MOCK(channel_dump_statistics,
436 chan_test_channel_dump_statistics_mock);
438 /* Call channel_dumpstats() */
439 channel_dumpstats(LOG_DEBUG);
441 /* Assert that we hit the mock */
442 tt_int_op(dump_statistics_mock_matches, OP_EQ, 1);
444 /* Close the channel */
445 channel_mark_for_close(ch);
446 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING);
447 chan_test_finish_close(ch);
448 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSED);
450 /* Try again and hit the finished channel */
451 channel_dumpstats(LOG_DEBUG);
452 tt_int_op(dump_statistics_mock_matches, OP_EQ, 2);
454 channel_run_cleanup();
455 ch = NULL;
457 /* Now we should hit nothing */
458 channel_dumpstats(LOG_DEBUG);
459 tt_int_op(dump_statistics_mock_matches, OP_EQ, 2);
461 /* Unmock */
462 UNMOCK(channel_dump_statistics);
463 dump_statistics_mock_target = NULL;
464 dump_statistics_mock_matches = 0;
466 /* Now make another channel */
467 ch = new_fake_channel();
468 tt_assert(ch);
469 channel_register(ch);
470 tt_int_op(ch->registered, OP_EQ, 1);
471 /* Lie about its age so dumpstats gets coverage for rate calculations */
472 ch->timestamp_created = time(NULL) - 30;
473 tt_int_op(ch->timestamp_created, OP_GT, 0);
474 tt_int_op(time(NULL), OP_GT, ch->timestamp_created);
476 /* Put cells through it both ways to make the counters non-zero */
477 p_cell = packed_cell_new();
478 test_chan_accept_cells = 1;
479 old_count = test_cells_written;
480 channel_write_packed_cell(ch, p_cell);
481 tt_int_op(test_cells_written, OP_EQ, old_count + 1);
482 tt_u64_op(ch->n_bytes_xmitted, OP_GT, 0);
483 tt_u64_op(ch->n_cells_xmitted, OP_GT, 0);
485 /* Receive path */
486 channel_set_cell_handlers(ch,
487 chan_test_cell_handler,
488 chan_test_var_cell_handler);
489 tt_ptr_op(channel_get_cell_handler(ch), OP_EQ, chan_test_cell_handler);
490 tt_ptr_op(channel_get_var_cell_handler(ch), OP_EQ,
491 chan_test_var_cell_handler);
492 cell = tor_malloc_zero(sizeof(*cell));
493 old_count = test_chan_fixed_cells_recved;
494 channel_process_cell(ch, cell);
495 tt_int_op(test_chan_fixed_cells_recved, OP_EQ, old_count + 1);
496 tt_u64_op(ch->n_bytes_recved, OP_GT, 0);
497 tt_u64_op(ch->n_cells_recved, OP_GT, 0);
499 /* Test channel_dump_statistics */
500 ch->describe_transport = chan_test_describe_transport;
501 ch->dumpstats = chan_test_dumpstats;
502 test_chan_should_be_canonical = 1;
503 ch->is_canonical = test_chan_is_canonical;
504 old_count = test_dumpstats_calls;
505 channel_dump_statistics(ch, LOG_DEBUG);
506 tt_int_op(test_dumpstats_calls, OP_EQ, old_count + 1);
508 /* Close the channel */
509 channel_mark_for_close(ch);
510 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING);
511 chan_test_finish_close(ch);
512 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSED);
513 channel_run_cleanup();
514 ch = NULL;
516 done:
517 free_fake_channel(ch);
518 tor_free(cell);
520 UNMOCK(scheduler_channel_doesnt_want_writes);
521 UNMOCK(scheduler_release_channel);
523 return;
526 /* Test outbound cell. The callstack is:
527 * channel_flush_some_cells()
528 * -> channel_flush_from_first_active_circuit()
529 * -> channel_write_packed_cell()
530 * -> write_packed_cell()
531 * -> chan->write_packed_cell() fct ptr.
533 * This test goes from a cell in a circuit up to the channel write handler
534 * that should put them on the connection outbuf. */
535 static void
536 test_channel_outbound_cell(void *arg)
538 int old_count;
539 channel_t *chan = NULL;
540 packed_cell_t *p_cell = NULL, *p_cell2 = NULL;
541 origin_circuit_t *circ = NULL;
542 cell_queue_t *queue;
544 (void) arg;
546 /* The channel will be freed so we need to hijack this so the scheduler
547 * doesn't get confused. */
548 MOCK(scheduler_release_channel, scheduler_release_channel_mock);
550 /* Accept cells to lower layer */
551 test_chan_accept_cells = 1;
553 /* Setup a valid circuit to queue a cell. */
554 circ = origin_circuit_new();
555 tt_assert(circ);
556 /* Circuit needs an origin purpose to be considered origin. */
557 TO_CIRCUIT(circ)->purpose = CIRCUIT_PURPOSE_C_GENERAL;
558 TO_CIRCUIT(circ)->n_circ_id = 42;
559 /* This is the outbound test so use the next channel queue. */
560 queue = &TO_CIRCUIT(circ)->n_chan_cells;
561 /* Setup packed cell to queue on the circuit. */
562 p_cell = packed_cell_new();
563 tt_assert(p_cell);
564 p_cell2 = packed_cell_new();
565 tt_assert(p_cell2);
566 /* Setup a channel to put the circuit on. */
567 chan = new_fake_channel();
568 tt_assert(chan);
569 chan->state = CHANNEL_STATE_OPENING;
570 channel_change_state_open(chan);
571 /* Outbound channel. */
572 channel_mark_outgoing(chan);
573 /* Try to register it so we can clean it through the channel cleanup
574 * process. */
575 channel_register(chan);
576 tt_int_op(chan->registered, OP_EQ, 1);
577 /* Set EWMA policy so we can pick it when flushing. */
578 circuitmux_set_policy(chan->cmux, &ewma_policy);
579 tt_ptr_op(circuitmux_get_policy(chan->cmux), OP_EQ, &ewma_policy);
581 /* Register circuit to the channel circid map which will attach the circuit
582 * to the channel's cmux as well. */
583 circuit_set_n_circid_chan(TO_CIRCUIT(circ), 42, chan);
584 tt_int_op(channel_num_circuits(chan), OP_EQ, 1);
585 tt_assert(!TO_CIRCUIT(circ)->next_active_on_n_chan);
586 tt_assert(!TO_CIRCUIT(circ)->prev_active_on_n_chan);
587 /* Test the cmux state. */
588 tt_ptr_op(TO_CIRCUIT(circ)->n_mux, OP_EQ, chan->cmux);
589 tt_int_op(circuitmux_is_circuit_attached(chan->cmux, TO_CIRCUIT(circ)),
590 OP_EQ, 1);
592 /* Flush the channel without any cell on it. */
593 old_count = test_cells_written;
594 ssize_t flushed = channel_flush_some_cells(chan, 1);
595 tt_i64_op(flushed, OP_EQ, 0);
596 tt_int_op(test_cells_written, OP_EQ, old_count);
597 tt_int_op(channel_more_to_flush(chan), OP_EQ, 0);
598 tt_int_op(circuitmux_num_active_circuits(chan->cmux), OP_EQ, 0);
599 tt_int_op(circuitmux_num_cells(chan->cmux), OP_EQ, 0);
600 tt_int_op(circuitmux_is_circuit_active(chan->cmux, TO_CIRCUIT(circ)),
601 OP_EQ, 0);
602 tt_u64_op(chan->n_cells_xmitted, OP_EQ, 0);
603 tt_u64_op(chan->n_bytes_xmitted, OP_EQ, 0);
605 /* Queue cell onto the next queue that is the outbound direction. Than
606 * update its cmux so the circuit can be picked when flushing cells. */
607 cell_queue_append(queue, p_cell);
608 p_cell = NULL;
609 tt_int_op(queue->n, OP_EQ, 1);
610 cell_queue_append(queue, p_cell2);
611 p_cell2 = NULL;
612 tt_int_op(queue->n, OP_EQ, 2);
614 update_circuit_on_cmux(TO_CIRCUIT(circ), CELL_DIRECTION_OUT);
615 tt_int_op(circuitmux_num_active_circuits(chan->cmux), OP_EQ, 1);
616 tt_int_op(circuitmux_num_cells(chan->cmux), OP_EQ, 2);
617 tt_int_op(circuitmux_is_circuit_active(chan->cmux, TO_CIRCUIT(circ)),
618 OP_EQ, 1);
620 /* From this point on, we have a queued cell on an active circuit attached
621 * to the channel's cmux. */
623 /* Flush the first cell. This is going to go down the call stack. */
624 old_count = test_cells_written;
625 flushed = channel_flush_some_cells(chan, 1);
626 tt_i64_op(flushed, OP_EQ, 1);
627 tt_int_op(test_cells_written, OP_EQ, old_count + 1);
628 tt_int_op(circuitmux_num_cells(chan->cmux), OP_EQ, 1);
629 tt_int_op(channel_more_to_flush(chan), OP_EQ, 1);
630 /* Circuit should remain active because there is a second cell queued. */
631 tt_int_op(circuitmux_is_circuit_active(chan->cmux, TO_CIRCUIT(circ)),
632 OP_EQ, 1);
633 /* Should still be attached. */
634 tt_int_op(circuitmux_is_circuit_attached(chan->cmux, TO_CIRCUIT(circ)),
635 OP_EQ, 1);
636 tt_u64_op(chan->n_cells_xmitted, OP_EQ, 1);
637 tt_u64_op(chan->n_bytes_xmitted, OP_EQ, get_cell_network_size(0));
639 /* Flush second cell. This is going to go down the call stack. */
640 old_count = test_cells_written;
641 flushed = channel_flush_some_cells(chan, 1);
642 tt_i64_op(flushed, OP_EQ, 1);
643 tt_int_op(test_cells_written, OP_EQ, old_count + 1);
644 tt_int_op(circuitmux_num_cells(chan->cmux), OP_EQ, 0);
645 tt_int_op(channel_more_to_flush(chan), OP_EQ, 0);
646 /* No more cells should make the circuit inactive. */
647 tt_int_op(circuitmux_is_circuit_active(chan->cmux, TO_CIRCUIT(circ)),
648 OP_EQ, 0);
649 /* Should still be attached. */
650 tt_int_op(circuitmux_is_circuit_attached(chan->cmux, TO_CIRCUIT(circ)),
651 OP_EQ, 1);
652 tt_u64_op(chan->n_cells_xmitted, OP_EQ, 2);
653 tt_u64_op(chan->n_bytes_xmitted, OP_EQ, get_cell_network_size(0) * 2);
655 done:
656 if (circ) {
657 circuit_free_(TO_CIRCUIT(circ));
659 tor_free(p_cell);
660 channel_free_all();
661 UNMOCK(scheduler_release_channel);
664 /* Test inbound cell. The callstack is:
665 * channel_process_cell()
666 * -> chan->cell_handler()
668 * This test is about checking if we can process an inbound cell down to the
669 * channel handler. */
670 static void
671 test_channel_inbound_cell(void *arg)
673 channel_t *chan = NULL;
674 cell_t *cell = NULL;
675 int old_count;
677 (void) arg;
679 /* The channel will be freed so we need to hijack this so the scheduler
680 * doesn't get confused. */
681 MOCK(scheduler_release_channel, scheduler_release_channel_mock);
683 /* Accept cells to lower layer */
684 test_chan_accept_cells = 1;
686 chan = new_fake_channel();
687 tt_assert(chan);
688 /* Start it off in OPENING */
689 chan->state = CHANNEL_STATE_OPENING;
691 /* Try to register it */
692 channel_register(chan);
693 tt_int_op(chan->registered, OP_EQ, 1);
695 /* Open it */
696 channel_change_state_open(chan);
697 tt_int_op(chan->state, OP_EQ, CHANNEL_STATE_OPEN);
698 tt_int_op(chan->has_been_open, OP_EQ, 1);
700 /* Receive a cell now. */
701 cell = tor_malloc_zero(sizeof(*cell));
702 make_fake_cell(cell);
703 old_count = test_chan_fixed_cells_recved;
704 channel_process_cell(chan, cell);
705 tt_int_op(test_chan_fixed_cells_recved, OP_EQ, old_count);
706 tt_assert(monotime_coarse_is_zero(&chan->timestamp_xfer));
707 tt_u64_op(chan->timestamp_active, OP_EQ, 0);
708 tt_u64_op(chan->timestamp_recv, OP_EQ, 0);
710 /* Setup incoming cell handlers. We don't care about var cell, the channel
711 * layers is not handling those. */
712 channel_set_cell_handlers(chan, chan_test_cell_handler, NULL);
713 tt_ptr_op(chan->cell_handler, OP_EQ, chan_test_cell_handler);
714 /* Now process the cell, we should see it. */
715 old_count = test_chan_fixed_cells_recved;
716 channel_process_cell(chan, cell);
717 tt_int_op(test_chan_fixed_cells_recved, OP_EQ, old_count + 1);
718 /* We should have a series of timestamp set. */
719 tt_assert(!monotime_coarse_is_zero(&chan->timestamp_xfer));
720 tt_u64_op(chan->timestamp_active, OP_NE, 0);
721 tt_u64_op(chan->timestamp_recv, OP_NE, 0);
722 tt_assert(monotime_coarse_is_zero(&chan->next_padding_time));
723 tt_u64_op(chan->n_cells_recved, OP_EQ, 1);
724 tt_u64_op(chan->n_bytes_recved, OP_EQ, get_cell_network_size(0));
726 /* Close it */
727 old_count = test_close_called;
728 channel_mark_for_close(chan);
729 tt_int_op(chan->state, OP_EQ, CHANNEL_STATE_CLOSING);
730 tt_int_op(chan->reason_for_closing, OP_EQ, CHANNEL_CLOSE_REQUESTED);
731 tt_int_op(test_close_called, OP_EQ, old_count + 1);
733 /* This closes the channe so it calls in the scheduler, make sure of it. */
734 old_count = test_releases_count;
735 chan_test_finish_close(chan);
736 tt_int_op(test_releases_count, OP_EQ, old_count + 1);
737 tt_int_op(chan->state, OP_EQ, CHANNEL_STATE_CLOSED);
739 /* The channel will be free, lets make sure it is not accessible. */
740 uint64_t chan_id = chan->global_identifier;
741 tt_ptr_op(channel_find_by_global_id(chan_id), OP_EQ, chan);
742 channel_run_cleanup();
743 chan = channel_find_by_global_id(chan_id);
744 tt_assert(chan == NULL);
746 done:
747 tor_free(cell);
748 UNMOCK(scheduler_release_channel);
752 * Normal channel lifecycle test:
754 * OPENING->OPEN->MAINT->OPEN->CLOSING->CLOSED
757 static void
758 test_channel_lifecycle(void *arg)
760 channel_t *ch1 = NULL, *ch2 = NULL;
761 packed_cell_t *p_cell = NULL;
762 int old_count, init_doesnt_want_writes_count;
763 int init_releases_count;
765 (void)arg;
767 /* Mock these for the whole lifecycle test */
768 MOCK(scheduler_channel_doesnt_want_writes,
769 scheduler_channel_doesnt_want_writes_mock);
770 MOCK(scheduler_release_channel,
771 scheduler_release_channel_mock);
773 /* Cache some initial counter values */
774 init_doesnt_want_writes_count = test_doesnt_want_writes_count;
775 init_releases_count = test_releases_count;
777 /* Accept cells to lower layer */
778 test_chan_accept_cells = 1;
780 ch1 = new_fake_channel();
781 tt_assert(ch1);
782 /* Start it off in OPENING */
783 ch1->state = CHANNEL_STATE_OPENING;
785 /* Try to register it */
786 channel_register(ch1);
787 tt_assert(ch1->registered);
789 /* Try to write a cell through (should queue) */
790 p_cell = packed_cell_new();
791 old_count = test_cells_written;
792 channel_write_packed_cell(ch1, p_cell);
793 tt_int_op(old_count, OP_EQ, test_cells_written);
795 /* Move it to OPEN and flush */
796 channel_change_state_open(ch1);
798 /* Get another one */
799 ch2 = new_fake_channel();
800 tt_assert(ch2);
801 ch2->state = CHANNEL_STATE_OPENING;
803 /* Register */
804 channel_register(ch2);
805 tt_assert(ch2->registered);
807 /* Check counters */
808 tt_int_op(test_doesnt_want_writes_count, OP_EQ,
809 init_doesnt_want_writes_count);
810 tt_int_op(test_releases_count, OP_EQ, init_releases_count);
812 /* Move ch1 to MAINT */
813 channel_change_state(ch1, CHANNEL_STATE_MAINT);
814 tt_int_op(test_doesnt_want_writes_count, OP_EQ,
815 init_doesnt_want_writes_count + 1);
816 tt_int_op(test_releases_count, OP_EQ, init_releases_count);
818 /* Move ch2 to OPEN */
819 channel_change_state_open(ch2);
820 tt_int_op(test_doesnt_want_writes_count, OP_EQ,
821 init_doesnt_want_writes_count + 1);
822 tt_int_op(test_releases_count, OP_EQ, init_releases_count);
824 /* Move ch1 back to OPEN */
825 channel_change_state_open(ch1);
826 tt_int_op(test_doesnt_want_writes_count, OP_EQ,
827 init_doesnt_want_writes_count + 1);
828 tt_int_op(test_releases_count, OP_EQ, init_releases_count);
830 /* Mark ch2 for close */
831 channel_mark_for_close(ch2);
832 tt_int_op(ch2->state, OP_EQ, CHANNEL_STATE_CLOSING);
833 tt_int_op(test_doesnt_want_writes_count, OP_EQ,
834 init_doesnt_want_writes_count + 1);
835 tt_int_op(test_releases_count, OP_EQ, init_releases_count + 1);
837 /* Shut down channels */
838 channel_free_all();
839 ch1 = ch2 = NULL;
840 tt_int_op(test_doesnt_want_writes_count, OP_EQ,
841 init_doesnt_want_writes_count + 1);
842 /* channel_free() calls scheduler_release_channel() */
843 tt_int_op(test_releases_count, OP_EQ, init_releases_count + 4);
845 done:
846 free_fake_channel(ch1);
847 free_fake_channel(ch2);
849 UNMOCK(scheduler_channel_doesnt_want_writes);
850 UNMOCK(scheduler_release_channel);
854 * Weird channel lifecycle test:
856 * OPENING->CLOSING->CLOSED
857 * OPENING->OPEN->CLOSING->ERROR
858 * OPENING->OPEN->MAINT->CLOSING->CLOSED
859 * OPENING->OPEN->MAINT->CLOSING->ERROR
862 static void
863 test_channel_lifecycle_2(void *arg)
865 channel_t *ch = NULL;
867 (void)arg;
869 /* Mock these for the whole lifecycle test */
870 MOCK(scheduler_channel_doesnt_want_writes,
871 scheduler_channel_doesnt_want_writes_mock);
872 MOCK(scheduler_release_channel,
873 scheduler_release_channel_mock);
875 /* Accept cells to lower layer */
876 test_chan_accept_cells = 1;
878 ch = new_fake_channel();
879 tt_assert(ch);
880 /* Start it off in OPENING */
881 ch->state = CHANNEL_STATE_OPENING;
883 /* Try to register it */
884 channel_register(ch);
885 tt_assert(ch->registered);
887 /* Try to close it */
888 channel_mark_for_close(ch);
889 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING);
891 /* Finish closing it */
892 chan_test_finish_close(ch);
893 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSED);
894 channel_run_cleanup();
895 ch = NULL;
897 /* Now try OPENING->OPEN->CLOSING->ERROR */
898 ch = new_fake_channel();
899 tt_assert(ch);
900 ch->state = CHANNEL_STATE_OPENING;
901 channel_register(ch);
902 tt_assert(ch->registered);
904 /* Finish opening it */
905 channel_change_state_open(ch);
907 /* Error exit from lower layer */
908 chan_test_error(ch);
909 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING);
910 chan_test_finish_close(ch);
911 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_ERROR);
912 channel_run_cleanup();
913 ch = NULL;
915 /* OPENING->OPEN->MAINT->CLOSING->CLOSED close from maintenance state */
916 ch = new_fake_channel();
917 tt_assert(ch);
918 ch->state = CHANNEL_STATE_OPENING;
919 channel_register(ch);
920 tt_assert(ch->registered);
922 /* Finish opening it */
923 channel_change_state_open(ch);
924 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_OPEN);
926 /* Go to maintenance state */
927 channel_change_state(ch, CHANNEL_STATE_MAINT);
928 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_MAINT);
930 /* Lower layer close */
931 channel_mark_for_close(ch);
932 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING);
934 /* Finish */
935 chan_test_finish_close(ch);
936 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSED);
937 channel_run_cleanup();
938 ch = NULL;
941 * OPENING->OPEN->MAINT->CLOSING->CLOSED lower-layer close during
942 * maintenance state
944 ch = new_fake_channel();
945 tt_assert(ch);
946 ch->state = CHANNEL_STATE_OPENING;
947 channel_register(ch);
948 tt_assert(ch->registered);
950 /* Finish opening it */
951 channel_change_state_open(ch);
952 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_OPEN);
954 /* Go to maintenance state */
955 channel_change_state(ch, CHANNEL_STATE_MAINT);
956 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_MAINT);
958 /* Lower layer close */
959 channel_close_from_lower_layer(ch);
960 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING);
962 /* Finish */
963 chan_test_finish_close(ch);
964 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSED);
965 channel_run_cleanup();
966 ch = NULL;
968 /* OPENING->OPEN->MAINT->CLOSING->ERROR */
969 ch = new_fake_channel();
970 tt_assert(ch);
971 ch->state = CHANNEL_STATE_OPENING;
972 channel_register(ch);
973 tt_assert(ch->registered);
975 /* Finish opening it */
976 channel_change_state_open(ch);
977 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_OPEN);
979 /* Go to maintenance state */
980 channel_change_state(ch, CHANNEL_STATE_MAINT);
981 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_MAINT);
983 /* Lower layer close */
984 chan_test_error(ch);
985 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING);
987 /* Finish */
988 chan_test_finish_close(ch);
989 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_ERROR);
990 channel_run_cleanup();
991 ch = NULL;
993 /* Shut down channels */
994 channel_free_all();
996 done:
997 tor_free(ch);
999 UNMOCK(scheduler_channel_doesnt_want_writes);
1000 UNMOCK(scheduler_release_channel);
1002 return;
1005 static void
1006 test_channel_id_map(void *arg)
1008 (void)arg;
1009 #define N_CHAN 6
1010 char rsa_id[N_CHAN][DIGEST_LEN];
1011 ed25519_public_key_t *ed_id[N_CHAN];
1012 channel_t *chan[N_CHAN];
1013 int i;
1014 ed25519_public_key_t ed_zero;
1015 memset(&ed_zero, 0, sizeof(ed_zero));
1017 tt_int_op(DIGEST_LEN, OP_EQ, sizeof(rsa_id[0])); // Do I remember C?
1019 for (i = 0; i < N_CHAN; ++i) {
1020 crypto_rand(rsa_id[i], DIGEST_LEN);
1021 ed_id[i] = tor_malloc_zero(sizeof(*ed_id[i]));
1022 crypto_rand((char*)ed_id[i]->pubkey, sizeof(ed_id[i]->pubkey));
1025 /* For channel 3, have no Ed identity. */
1026 tor_free(ed_id[3]);
1028 /* Channel 2 and 4 have same ROSA identity */
1029 memcpy(rsa_id[4], rsa_id[2], DIGEST_LEN);
1031 /* Channel 2 and 4 and 5 have same RSA identity */
1032 memcpy(rsa_id[4], rsa_id[2], DIGEST_LEN);
1033 memcpy(rsa_id[5], rsa_id[2], DIGEST_LEN);
1035 /* Channels 2 and 5 have same Ed25519 identity */
1036 memcpy(ed_id[5], ed_id[2], sizeof(*ed_id[2]));
1038 for (i = 0; i < N_CHAN; ++i) {
1039 chan[i] = new_fake_channel();
1040 channel_register(chan[i]);
1041 channel_set_identity_digest(chan[i], rsa_id[i], ed_id[i]);
1044 /* Lookup by RSA id only */
1045 tt_ptr_op(chan[0], OP_EQ,
1046 channel_find_by_remote_identity(rsa_id[0], NULL));
1047 tt_ptr_op(chan[1], OP_EQ,
1048 channel_find_by_remote_identity(rsa_id[1], NULL));
1049 tt_ptr_op(chan[3], OP_EQ,
1050 channel_find_by_remote_identity(rsa_id[3], NULL));
1051 channel_t *ch;
1052 ch = channel_find_by_remote_identity(rsa_id[2], NULL);
1053 tt_assert(ch == chan[2] || ch == chan[4] || ch == chan[5]);
1054 ch = channel_next_with_rsa_identity(ch);
1055 tt_assert(ch == chan[2] || ch == chan[4] || ch == chan[5]);
1056 ch = channel_next_with_rsa_identity(ch);
1057 tt_assert(ch == chan[2] || ch == chan[4] || ch == chan[5]);
1058 ch = channel_next_with_rsa_identity(ch);
1059 tt_ptr_op(ch, OP_EQ, NULL);
1061 /* As above, but with zero Ed25519 ID (meaning "any ID") */
1062 tt_ptr_op(chan[0], OP_EQ,
1063 channel_find_by_remote_identity(rsa_id[0], &ed_zero));
1064 tt_ptr_op(chan[1], OP_EQ,
1065 channel_find_by_remote_identity(rsa_id[1], &ed_zero));
1066 tt_ptr_op(chan[3], OP_EQ,
1067 channel_find_by_remote_identity(rsa_id[3], &ed_zero));
1068 ch = channel_find_by_remote_identity(rsa_id[2], &ed_zero);
1069 tt_assert(ch == chan[2] || ch == chan[4] || ch == chan[5]);
1070 ch = channel_next_with_rsa_identity(ch);
1071 tt_assert(ch == chan[2] || ch == chan[4] || ch == chan[5]);
1072 ch = channel_next_with_rsa_identity(ch);
1073 tt_assert(ch == chan[2] || ch == chan[4] || ch == chan[5]);
1074 ch = channel_next_with_rsa_identity(ch);
1075 tt_ptr_op(ch, OP_EQ, NULL);
1077 /* Lookup nonexistent RSA identity */
1078 tt_ptr_op(NULL, OP_EQ,
1079 channel_find_by_remote_identity("!!!!!!!!!!!!!!!!!!!!", NULL));
1081 /* Look up by full identity pair */
1082 tt_ptr_op(chan[0], OP_EQ,
1083 channel_find_by_remote_identity(rsa_id[0], ed_id[0]));
1084 tt_ptr_op(chan[1], OP_EQ,
1085 channel_find_by_remote_identity(rsa_id[1], ed_id[1]));
1086 tt_ptr_op(chan[3], OP_EQ,
1087 channel_find_by_remote_identity(rsa_id[3], ed_id[3] /*NULL*/));
1088 tt_ptr_op(chan[4], OP_EQ,
1089 channel_find_by_remote_identity(rsa_id[4], ed_id[4]));
1090 ch = channel_find_by_remote_identity(rsa_id[2], ed_id[2]);
1091 tt_assert(ch == chan[2] || ch == chan[5]);
1093 /* Look up RSA identity with wrong ed25519 identity */
1094 tt_ptr_op(NULL, OP_EQ,
1095 channel_find_by_remote_identity(rsa_id[4], ed_id[0]));
1096 tt_ptr_op(NULL, OP_EQ,
1097 channel_find_by_remote_identity(rsa_id[2], ed_id[1]));
1098 tt_ptr_op(NULL, OP_EQ,
1099 channel_find_by_remote_identity(rsa_id[3], ed_id[1]));
1101 done:
1102 for (i = 0; i < N_CHAN; ++i) {
1103 channel_clear_identity_digest(chan[i]);
1104 channel_unregister(chan[i]);
1105 free_fake_channel(chan[i]);
1106 tor_free(ed_id[i]);
1108 #undef N_CHAN
1111 static void
1112 test_channel_state(void *arg)
1114 (void) arg;
1116 /* Test state validity. */
1117 tt_int_op(channel_state_is_valid(CHANNEL_STATE_CLOSED), OP_EQ, 1);
1118 tt_int_op(channel_state_is_valid(CHANNEL_STATE_CLOSING), OP_EQ, 1);
1119 tt_int_op(channel_state_is_valid(CHANNEL_STATE_ERROR), OP_EQ, 1);
1120 tt_int_op(channel_state_is_valid(CHANNEL_STATE_OPEN), OP_EQ, 1);
1121 tt_int_op(channel_state_is_valid(CHANNEL_STATE_OPENING), OP_EQ, 1);
1122 tt_int_op(channel_state_is_valid(CHANNEL_STATE_MAINT), OP_EQ, 1);
1123 tt_int_op(channel_state_is_valid(CHANNEL_STATE_LAST), OP_EQ, 0);
1124 tt_int_op(channel_state_is_valid(INT_MAX), OP_EQ, 0);
1126 /* Test listener state validity. */
1127 tt_int_op(channel_listener_state_is_valid(CHANNEL_LISTENER_STATE_CLOSED),
1128 OP_EQ, 1);
1129 tt_int_op(channel_listener_state_is_valid(CHANNEL_LISTENER_STATE_LISTENING),
1130 OP_EQ, 1);
1131 tt_int_op(channel_listener_state_is_valid(CHANNEL_LISTENER_STATE_CLOSING),
1132 OP_EQ, 1);
1133 tt_int_op(channel_listener_state_is_valid(CHANNEL_LISTENER_STATE_ERROR),
1134 OP_EQ, 1);
1135 tt_int_op(channel_listener_state_is_valid(CHANNEL_LISTENER_STATE_LAST),
1136 OP_EQ, 0);
1137 tt_int_op(channel_listener_state_is_valid(INT_MAX), OP_EQ, 0);
1139 /* Test state transition. */
1140 tt_int_op(channel_state_can_transition(CHANNEL_STATE_CLOSED,
1141 CHANNEL_STATE_OPENING), OP_EQ, 1);
1142 tt_int_op(channel_state_can_transition(CHANNEL_STATE_CLOSED,
1143 CHANNEL_STATE_ERROR), OP_EQ, 0);
1144 tt_int_op(channel_state_can_transition(CHANNEL_STATE_CLOSING,
1145 CHANNEL_STATE_ERROR), OP_EQ, 1);
1146 tt_int_op(channel_state_can_transition(CHANNEL_STATE_CLOSING,
1147 CHANNEL_STATE_CLOSED), OP_EQ, 1);
1148 tt_int_op(channel_state_can_transition(CHANNEL_STATE_CLOSING,
1149 CHANNEL_STATE_OPEN), OP_EQ, 0);
1150 tt_int_op(channel_state_can_transition(CHANNEL_STATE_MAINT,
1151 CHANNEL_STATE_CLOSING), OP_EQ, 1);
1152 tt_int_op(channel_state_can_transition(CHANNEL_STATE_MAINT,
1153 CHANNEL_STATE_ERROR), OP_EQ, 1);
1154 tt_int_op(channel_state_can_transition(CHANNEL_STATE_MAINT,
1155 CHANNEL_STATE_OPEN), OP_EQ, 1);
1156 tt_int_op(channel_state_can_transition(CHANNEL_STATE_MAINT,
1157 CHANNEL_STATE_OPENING), OP_EQ, 0);
1158 tt_int_op(channel_state_can_transition(CHANNEL_STATE_OPENING,
1159 CHANNEL_STATE_OPEN), OP_EQ, 1);
1160 tt_int_op(channel_state_can_transition(CHANNEL_STATE_OPENING,
1161 CHANNEL_STATE_CLOSING), OP_EQ, 1);
1162 tt_int_op(channel_state_can_transition(CHANNEL_STATE_OPENING,
1163 CHANNEL_STATE_ERROR), OP_EQ, 1);
1164 tt_int_op(channel_state_can_transition(CHANNEL_STATE_OPEN,
1165 CHANNEL_STATE_ERROR), OP_EQ, 1);
1166 tt_int_op(channel_state_can_transition(CHANNEL_STATE_OPEN,
1167 CHANNEL_STATE_CLOSING), OP_EQ, 1);
1168 tt_int_op(channel_state_can_transition(CHANNEL_STATE_OPEN,
1169 CHANNEL_STATE_ERROR), OP_EQ, 1);
1170 tt_int_op(channel_state_can_transition(CHANNEL_STATE_OPEN,
1171 CHANNEL_STATE_MAINT), OP_EQ, 1);
1172 tt_int_op(channel_state_can_transition(CHANNEL_STATE_LAST,
1173 CHANNEL_STATE_MAINT), OP_EQ, 0);
1174 tt_int_op(channel_state_can_transition(CHANNEL_STATE_LAST, INT_MAX),
1175 OP_EQ, 0);
1177 /* Test listener state transition. */
1178 tt_int_op(channel_listener_state_can_transition(
1179 CHANNEL_LISTENER_STATE_CLOSED,
1180 CHANNEL_LISTENER_STATE_LISTENING),
1181 OP_EQ, 1);
1182 tt_int_op(channel_listener_state_can_transition(
1183 CHANNEL_LISTENER_STATE_CLOSED,
1184 CHANNEL_LISTENER_STATE_ERROR),
1185 OP_EQ, 0);
1187 tt_int_op(channel_listener_state_can_transition(
1188 CHANNEL_LISTENER_STATE_CLOSING,
1189 CHANNEL_LISTENER_STATE_CLOSED),
1190 OP_EQ, 1);
1192 tt_int_op(channel_listener_state_can_transition(
1193 CHANNEL_LISTENER_STATE_CLOSING,
1194 CHANNEL_LISTENER_STATE_ERROR),
1195 OP_EQ, 1);
1196 tt_int_op(channel_listener_state_can_transition(
1197 CHANNEL_LISTENER_STATE_ERROR,
1198 CHANNEL_LISTENER_STATE_CLOSING),
1199 OP_EQ, 0);
1201 tt_int_op(channel_listener_state_can_transition(
1202 CHANNEL_LISTENER_STATE_LISTENING,
1203 CHANNEL_LISTENER_STATE_CLOSING),
1204 OP_EQ, 1);
1205 tt_int_op(channel_listener_state_can_transition(
1206 CHANNEL_LISTENER_STATE_LISTENING,
1207 CHANNEL_LISTENER_STATE_ERROR),
1208 OP_EQ, 1);
1209 tt_int_op(channel_listener_state_can_transition(
1210 CHANNEL_LISTENER_STATE_LAST,
1211 INT_MAX),
1212 OP_EQ, 0);
1214 /* Test state string. */
1215 tt_str_op(channel_state_to_string(CHANNEL_STATE_CLOSING), OP_EQ,
1216 "closing");
1217 tt_str_op(channel_state_to_string(CHANNEL_STATE_ERROR), OP_EQ,
1218 "channel error");
1219 tt_str_op(channel_state_to_string(CHANNEL_STATE_CLOSED), OP_EQ,
1220 "closed");
1221 tt_str_op(channel_state_to_string(CHANNEL_STATE_OPEN), OP_EQ,
1222 "open");
1223 tt_str_op(channel_state_to_string(CHANNEL_STATE_OPENING), OP_EQ,
1224 "opening");
1225 tt_str_op(channel_state_to_string(CHANNEL_STATE_MAINT), OP_EQ,
1226 "temporarily suspended for maintenance");
1227 tt_str_op(channel_state_to_string(CHANNEL_STATE_LAST), OP_EQ,
1228 "unknown or invalid channel state");
1229 tt_str_op(channel_state_to_string(INT_MAX), OP_EQ,
1230 "unknown or invalid channel state");
1232 /* Test listener state string. */
1233 tt_str_op(channel_listener_state_to_string(CHANNEL_LISTENER_STATE_CLOSING),
1234 OP_EQ, "closing");
1235 tt_str_op(channel_listener_state_to_string(CHANNEL_LISTENER_STATE_ERROR),
1236 OP_EQ, "channel listener error");
1237 tt_str_op(channel_listener_state_to_string(CHANNEL_LISTENER_STATE_LISTENING),
1238 OP_EQ, "listening");
1239 tt_str_op(channel_listener_state_to_string(CHANNEL_LISTENER_STATE_LAST),
1240 OP_EQ, "unknown or invalid channel listener state");
1241 tt_str_op(channel_listener_state_to_string(INT_MAX),
1242 OP_EQ, "unknown or invalid channel listener state");
1244 done:
1248 static networkstatus_t *mock_ns = NULL;
1250 static networkstatus_t *
1251 mock_networkstatus_get_latest_consensus(void)
1253 return mock_ns;
1256 static void
1257 test_channel_duplicates(void *arg)
1259 channel_t *chan = NULL;
1260 routerstatus_t rs;
1262 (void) arg;
1264 setup_full_capture_of_logs(LOG_INFO);
1265 /* Try a flat call with channel nor connections. */
1266 channel_check_for_duplicates();
1267 expect_log_msg_containing(
1268 "Found 0 connections to 0 relays. Found 0 current canonical "
1269 "connections, in 0 of which we were a non-canonical peer. "
1270 "0 relays had more than 1 connection, 0 had more than 2, and "
1271 "0 had more than 4 connections.");
1273 mock_ns = tor_malloc_zero(sizeof(*mock_ns));
1274 mock_ns->routerstatus_list = smartlist_new();
1275 MOCK(networkstatus_get_latest_consensus,
1276 mock_networkstatus_get_latest_consensus);
1278 chan = new_fake_channel();
1279 tt_assert(chan);
1280 chan->is_canonical = test_chan_is_canonical;
1281 memset(chan->identity_digest, 'A', sizeof(chan->identity_digest));
1282 channel_add_to_digest_map(chan);
1283 tt_ptr_op(channel_find_by_remote_identity(chan->identity_digest, NULL),
1284 OP_EQ, chan);
1286 /* No relay has been associated with this channel. */
1287 channel_check_for_duplicates();
1288 expect_log_msg_containing(
1289 "Found 0 connections to 0 relays. Found 0 current canonical "
1290 "connections, in 0 of which we were a non-canonical peer. "
1291 "0 relays had more than 1 connection, 0 had more than 2, and "
1292 "0 had more than 4 connections.");
1294 /* Associate relay to this connection in the consensus. */
1295 memset(&rs, 0, sizeof(rs));
1296 memset(rs.identity_digest, 'A', sizeof(rs.identity_digest));
1297 smartlist_add(mock_ns->routerstatus_list, &rs);
1299 /* Non opened channel. */
1300 chan->state = CHANNEL_STATE_CLOSING;
1301 channel_check_for_duplicates();
1302 expect_log_msg_containing(
1303 "Found 0 connections to 0 relays. Found 0 current canonical "
1304 "connections, in 0 of which we were a non-canonical peer. "
1305 "0 relays had more than 1 connection, 0 had more than 2, and "
1306 "0 had more than 4 connections.");
1307 chan->state = CHANNEL_STATE_OPEN;
1309 channel_check_for_duplicates();
1310 expect_log_msg_containing(
1311 "Found 1 connections to 1 relays. Found 0 current canonical "
1312 "connections, in 0 of which we were a non-canonical peer. "
1313 "0 relays had more than 1 connection, 0 had more than 2, and "
1314 "0 had more than 4 connections.");
1316 test_chan_should_be_canonical = 1;
1317 channel_check_for_duplicates();
1318 expect_log_msg_containing(
1319 "Found 1 connections to 1 relays. Found 1 current canonical "
1320 "connections, in 1 of which we were a non-canonical peer. "
1321 "0 relays had more than 1 connection, 0 had more than 2, and "
1322 "0 had more than 4 connections.");
1323 teardown_capture_of_logs();
1325 done:
1326 free_fake_channel(chan);
1327 smartlist_clear(mock_ns->routerstatus_list);
1328 networkstatus_vote_free(mock_ns);
1329 UNMOCK(networkstatus_get_latest_consensus);
1332 static void
1333 test_channel_for_extend(void *arg)
1335 channel_t *chan1 = NULL, *chan2 = NULL;
1336 channel_t *ret_chan = NULL;
1337 char digest[DIGEST_LEN];
1338 ed25519_public_key_t ed_id;
1339 tor_addr_t addr;
1340 const char *msg;
1341 int launch;
1342 time_t now = time(NULL);
1344 (void) arg;
1346 memset(digest, 'A', sizeof(digest));
1347 memset(&ed_id, 'B', sizeof(ed_id));
1349 chan1 = new_fake_channel();
1350 tt_assert(chan1);
1351 /* Need to be registered to get added to the id map. */
1352 channel_register(chan1);
1353 tt_int_op(chan1->registered, OP_EQ, 1);
1354 /* We need those for the test. */
1355 chan1->is_canonical = test_chan_is_canonical;
1356 chan1->matches_target = test_chan_matches_target;
1357 chan1->timestamp_created = now - 9;
1359 chan2 = new_fake_channel();
1360 tt_assert(chan2);
1361 /* Need to be registered to get added to the id map. */
1362 channel_register(chan2);
1363 tt_int_op(chan2->registered, OP_EQ, 1);
1364 /* We need those for the test. */
1365 chan2->is_canonical = test_chan_is_canonical;
1366 chan2->matches_target = test_chan_matches_target;
1367 /* Make it older than chan1. */
1368 chan2->timestamp_created = chan1->timestamp_created - 1;
1370 /* Set channel identities and add it to the channel map. The last one to be
1371 * added is made the first one in the list so the lookup will always return
1372 * that one first. */
1373 channel_set_identity_digest(chan2, digest, &ed_id);
1374 channel_set_identity_digest(chan1, digest, &ed_id);
1375 tt_ptr_op(channel_find_by_remote_identity(digest, NULL), OP_EQ, chan1);
1376 tt_ptr_op(channel_find_by_remote_identity(digest, &ed_id), OP_EQ, chan1);
1378 /* The expected result is chan2 because it is older than chan1. */
1379 ret_chan = channel_get_for_extend(digest, &ed_id, &addr, &msg, &launch);
1380 tt_assert(ret_chan);
1381 tt_ptr_op(ret_chan, OP_EQ, chan2);
1382 tt_int_op(launch, OP_EQ, 0);
1383 tt_str_op(msg, OP_EQ, "Connection is fine; using it.");
1385 /* Switch that around from previous test. */
1386 chan2->timestamp_created = chan1->timestamp_created + 1;
1387 ret_chan = channel_get_for_extend(digest, &ed_id, &addr, &msg, &launch);
1388 tt_assert(ret_chan);
1389 tt_ptr_op(ret_chan, OP_EQ, chan1);
1390 tt_int_op(launch, OP_EQ, 0);
1391 tt_str_op(msg, OP_EQ, "Connection is fine; using it.");
1393 /* Same creation time, num circuits will be used and they both have 0 so the
1394 * channel 2 should be picked due to how channel_is_better() work. */
1395 chan2->timestamp_created = chan1->timestamp_created;
1396 ret_chan = channel_get_for_extend(digest, &ed_id, &addr, &msg, &launch);
1397 tt_assert(ret_chan);
1398 tt_ptr_op(ret_chan, OP_EQ, chan1);
1399 tt_int_op(launch, OP_EQ, 0);
1400 tt_str_op(msg, OP_EQ, "Connection is fine; using it.");
1402 /* For the rest of the tests, we need channel 1 to be the older. */
1403 chan2->timestamp_created = chan1->timestamp_created + 1;
1405 /* Condemned the older channel. */
1406 chan1->state = CHANNEL_STATE_CLOSING;
1407 ret_chan = channel_get_for_extend(digest, &ed_id, &addr, &msg, &launch);
1408 tt_assert(ret_chan);
1409 tt_ptr_op(ret_chan, OP_EQ, chan2);
1410 tt_int_op(launch, OP_EQ, 0);
1411 tt_str_op(msg, OP_EQ, "Connection is fine; using it.");
1412 chan1->state = CHANNEL_STATE_OPEN;
1414 /* Make the older channel a client one. */
1415 channel_mark_client(chan1);
1416 ret_chan = channel_get_for_extend(digest, &ed_id, &addr, &msg, &launch);
1417 tt_assert(ret_chan);
1418 tt_ptr_op(ret_chan, OP_EQ, chan2);
1419 tt_int_op(launch, OP_EQ, 0);
1420 tt_str_op(msg, OP_EQ, "Connection is fine; using it.");
1421 channel_clear_client(chan1);
1423 /* Non matching ed identity with valid digest. */
1424 ed25519_public_key_t dumb_ed_id;
1425 memset(&dumb_ed_id, 0, sizeof(dumb_ed_id));
1426 ret_chan = channel_get_for_extend(digest, &dumb_ed_id, &addr, &msg,
1427 &launch);
1428 tt_assert(!ret_chan);
1429 tt_str_op(msg, OP_EQ, "Not connected. Connecting.");
1430 tt_int_op(launch, OP_EQ, 1);
1432 /* Opening channel, we'll check if the target address matches. */
1433 test_chan_should_match_target = 1;
1434 chan1->state = CHANNEL_STATE_OPENING;
1435 chan2->state = CHANNEL_STATE_OPENING;
1436 ret_chan = channel_get_for_extend(digest, &ed_id, &addr, &msg, &launch);
1437 tt_assert(!ret_chan);
1438 tt_str_op(msg, OP_EQ, "Connection in progress; waiting.");
1439 tt_int_op(launch, OP_EQ, 0);
1440 chan1->state = CHANNEL_STATE_OPEN;
1441 chan2->state = CHANNEL_STATE_OPEN;
1443 /* Mark channel 1 as bad for circuits. */
1444 channel_mark_bad_for_new_circs(chan1);
1445 ret_chan = channel_get_for_extend(digest, &ed_id, &addr, &msg, &launch);
1446 tt_assert(ret_chan);
1447 tt_ptr_op(ret_chan, OP_EQ, chan2);
1448 tt_int_op(launch, OP_EQ, 0);
1449 tt_str_op(msg, OP_EQ, "Connection is fine; using it.");
1450 chan1->is_bad_for_new_circs = 0;
1452 /* Mark both channels as unusable. */
1453 channel_mark_bad_for_new_circs(chan1);
1454 channel_mark_bad_for_new_circs(chan2);
1455 ret_chan = channel_get_for_extend(digest, &ed_id, &addr, &msg, &launch);
1456 tt_assert(!ret_chan);
1457 tt_str_op(msg, OP_EQ, "Connections all too old, or too non-canonical. "
1458 " Launching a new one.");
1459 tt_int_op(launch, OP_EQ, 1);
1460 chan1->is_bad_for_new_circs = 0;
1461 chan2->is_bad_for_new_circs = 0;
1463 /* Non canonical channels. */
1464 test_chan_should_match_target = 0;
1465 test_chan_canonical_should_be_reliable = 1;
1466 ret_chan = channel_get_for_extend(digest, &ed_id, &addr, &msg, &launch);
1467 tt_assert(!ret_chan);
1468 tt_str_op(msg, OP_EQ, "Connections all too old, or too non-canonical. "
1469 " Launching a new one.");
1470 tt_int_op(launch, OP_EQ, 1);
1472 done:
1473 free_fake_channel(chan1);
1474 free_fake_channel(chan2);
1477 static void
1478 test_channel_listener(void *arg)
1480 int old_count;
1481 time_t now = time(NULL);
1482 channel_listener_t *chan = NULL;
1484 (void) arg;
1486 chan = tor_malloc_zero(sizeof(*chan));
1487 tt_assert(chan);
1488 channel_init_listener(chan);
1489 tt_u64_op(chan->global_identifier, OP_EQ, 1);
1490 tt_int_op(chan->timestamp_created, OP_GE, now);
1491 chan->close = test_chan_listener_close;
1493 /* Register it. At this point, it is not open so it will be put in the
1494 * finished list. */
1495 channel_listener_register(chan);
1496 tt_int_op(chan->registered, OP_EQ, 1);
1497 channel_listener_unregister(chan);
1499 /* Register it as listening now thus active. */
1500 chan->state = CHANNEL_LISTENER_STATE_LISTENING;
1501 channel_listener_register(chan);
1502 tt_int_op(chan->registered, OP_EQ, 1);
1504 /* Set the listener function. */
1505 channel_listener_set_listener_fn(chan, test_chan_listener_fn);
1506 tt_ptr_op(chan->listener, OP_EQ, test_chan_listener_fn);
1508 /* Put a channel in the listener incoming list and queue it.
1509 * function. By doing this, the listener() handler will be called. */
1510 channel_t *in_chan = new_fake_channel();
1511 old_count = test_chan_listener_fn_called;
1512 channel_listener_queue_incoming(chan, in_chan);
1513 free_fake_channel(in_chan);
1514 tt_int_op(test_chan_listener_fn_called, OP_EQ, old_count + 1);
1516 /* Put listener channel in CLOSING state. */
1517 old_count = test_chan_listener_close_fn_called;
1518 channel_listener_mark_for_close(chan);
1519 tt_int_op(test_chan_listener_close_fn_called, OP_EQ, old_count + 1);
1520 channel_listener_change_state(chan, CHANNEL_LISTENER_STATE_CLOSED);
1522 /* Dump stats so we at least hit the code path. */
1523 chan->describe_transport = test_chan_listener_describe_transport;
1524 /* There is a check for "now > timestamp_created" when dumping the stats so
1525 * make sure we go in. */
1526 chan->timestamp_created = now - 10;
1527 channel_listener_dump_statistics(chan, LOG_INFO);
1529 done:
1530 channel_free_all();
1533 struct testcase_t channel_tests[] = {
1534 { "inbound_cell", test_channel_inbound_cell, TT_FORK,
1535 NULL, NULL },
1536 { "outbound_cell", test_channel_outbound_cell, TT_FORK,
1537 NULL, NULL },
1538 { "id_map", test_channel_id_map, TT_FORK,
1539 NULL, NULL },
1540 { "lifecycle", test_channel_lifecycle, TT_FORK,
1541 NULL, NULL },
1542 { "lifecycle_2", test_channel_lifecycle_2, TT_FORK,
1543 NULL, NULL },
1544 { "dumpstats", test_channel_dumpstats, TT_FORK,
1545 NULL, NULL },
1546 { "state", test_channel_state, TT_FORK,
1547 NULL, NULL },
1548 { "duplicates", test_channel_duplicates, TT_FORK,
1549 NULL, NULL },
1550 { "get_channel_for_extend", test_channel_for_extend, TT_FORK,
1551 NULL, NULL },
1552 { "listener", test_channel_listener, TT_FORK,
1553 NULL, NULL },
1554 END_OF_TESTCASES