Remove unused function: dns_randfn_() in dns.c.
[tor.git] / src / test / test_channel.c
blobafb7db813ccf01e16c3129cfde400f6da30c7f59
1 /* Copyright (c) 2013-2019, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 #define TOR_CHANNEL_INTERNAL_
5 #define CHANNEL_PRIVATE_
6 #include "core/or/or.h"
7 #include "core/or/channel.h"
8 /* For channel_note_destroy_not_pending */
9 #define CIRCUITLIST_PRIVATE
10 #include "core/or/circuitlist.h"
11 #include "core/or/circuitmux.h"
12 #include "core/or/circuitmux_ewma.h"
13 /* For var_cell_free */
14 #include "core/or/connection_or.h"
15 #include "lib/crypt_ops/crypto_rand.h"
16 /* For packed_cell stuff */
17 #define RELAY_PRIVATE
18 #include "core/or/relay.h"
19 /* For init/free stuff */
20 #include "core/or/scheduler.h"
21 #include "feature/nodelist/networkstatus.h"
23 #include "core/or/cell_st.h"
24 #include "feature/nodelist/networkstatus_st.h"
25 #include "core/or/origin_circuit_st.h"
26 #include "feature/nodelist/routerstatus_st.h"
27 #include "core/or/var_cell_st.h"
29 /* Test suite stuff */
30 #include "test/log_test_helpers.h"
31 #include "test/test.h"
32 #include "test/fakechans.h"
34 static int test_chan_accept_cells = 0;
35 static int test_chan_fixed_cells_recved = 0;
36 static cell_t * test_chan_last_seen_fixed_cell_ptr = NULL;
37 static int test_chan_var_cells_recved = 0;
38 static var_cell_t * test_chan_last_seen_var_cell_ptr = NULL;
39 static int test_cells_written = 0;
40 static int test_doesnt_want_writes_count = 0;
41 static int test_dumpstats_calls = 0;
42 static int test_has_waiting_cells_count = 0;
43 static int test_releases_count = 0;
44 static channel_t *dump_statistics_mock_target = NULL;
45 static int dump_statistics_mock_matches = 0;
46 static int test_close_called = 0;
47 static int test_chan_should_be_canonical = 0;
48 static int test_chan_should_match_target = 0;
49 static int test_chan_listener_close_fn_called = 0;
50 static int test_chan_listener_fn_called = 0;
52 static const char *
53 chan_test_describe_transport(channel_t *ch)
55 tt_ptr_op(ch, OP_NE, NULL);
57 done:
58 return "Fake channel for unit tests";
61 /**
62 * Mock for channel_dump_statistics(); if the channel matches the
63 * target, bump a counter - otherwise ignore.
66 static void
67 chan_test_channel_dump_statistics_mock(channel_t *chan, int severity)
69 tt_ptr_op(chan, OP_NE, NULL);
71 (void)severity;
73 if (chan != NULL && chan == dump_statistics_mock_target) {
74 ++dump_statistics_mock_matches;
77 done:
78 return;
82 * Handle an incoming fixed-size cell for unit tests
85 static void
86 chan_test_cell_handler(channel_t *chan, cell_t *cell)
88 tt_assert(chan);
89 tt_assert(cell);
91 test_chan_last_seen_fixed_cell_ptr = cell;
92 ++test_chan_fixed_cells_recved;
94 done:
95 return;
99 * Fake transport-specific stats call
102 static void
103 chan_test_dumpstats(channel_t *ch, int severity)
105 tt_ptr_op(ch, OP_NE, NULL);
107 (void)severity;
109 ++test_dumpstats_calls;
111 done:
112 return;
116 * Handle an incoming variable-size cell for unit tests
119 static void
120 chan_test_var_cell_handler(channel_t *ch,
121 var_cell_t *var_cell)
123 tt_assert(ch);
124 tt_assert(var_cell);
126 test_chan_last_seen_var_cell_ptr = var_cell;
127 ++test_chan_var_cells_recved;
129 done:
130 return;
133 static void
134 chan_test_close(channel_t *ch)
136 tt_assert(ch);
138 ++test_close_called;
140 done:
141 return;
145 * Close a channel through the error path
148 static void
149 chan_test_error(channel_t *ch)
151 tt_assert(ch);
152 tt_assert(!(ch->state == CHANNEL_STATE_CLOSING ||
153 ch->state == CHANNEL_STATE_ERROR ||
154 ch->state == CHANNEL_STATE_CLOSED));
156 channel_close_for_error(ch);
158 done:
159 return;
163 * Finish closing a channel from CHANNEL_STATE_CLOSING
166 static void
167 chan_test_finish_close(channel_t *ch)
169 tt_assert(ch);
170 tt_assert(ch->state == CHANNEL_STATE_CLOSING);
172 channel_closed(ch);
174 done:
175 return;
178 static const char *
179 chan_test_get_remote_descr(channel_t *ch, int flags)
181 tt_assert(ch);
182 tt_int_op(flags & ~(GRD_FLAG_ORIGINAL | GRD_FLAG_ADDR_ONLY), OP_EQ, 0);
184 done:
185 return "Fake channel for unit tests; no real endpoint";
188 static int
189 chan_test_num_cells_writeable(channel_t *ch)
191 tt_assert(ch);
193 done:
194 return 32;
197 static int
198 chan_test_write_packed_cell(channel_t *ch,
199 packed_cell_t *packed_cell)
201 int rv = 0;
203 tt_assert(ch);
204 tt_assert(packed_cell);
206 if (test_chan_accept_cells) {
207 /* Free the cell and bump the counter */
208 ++test_cells_written;
209 rv = 1;
211 /* else return 0, we didn't accept it */
213 done:
214 return rv;
217 static int
218 chan_test_write_var_cell(channel_t *ch, var_cell_t *var_cell)
220 int rv = 0;
222 tt_assert(ch);
223 tt_assert(var_cell);
225 if (test_chan_accept_cells) {
226 /* Free the cell and bump the counter */
227 var_cell_free(var_cell);
228 ++test_cells_written;
229 rv = 1;
231 /* else return 0, we didn't accept it */
233 done:
234 return rv;
238 * Fill out c with a new fake cell for test suite use
241 void
242 make_fake_cell(cell_t *c)
244 tt_ptr_op(c, OP_NE, NULL);
246 c->circ_id = 1;
247 c->command = CELL_RELAY;
248 memset(c->payload, 0, CELL_PAYLOAD_SIZE);
250 done:
251 return;
255 * Fill out c with a new fake var_cell for test suite use
258 void
259 make_fake_var_cell(var_cell_t *c)
261 tt_ptr_op(c, OP_NE, NULL);
263 c->circ_id = 1;
264 c->command = CELL_VERSIONS;
265 c->payload_len = CELL_PAYLOAD_SIZE / 2;
266 memset(c->payload, 0, c->payload_len);
268 done:
269 return;
273 * Set up a new fake channel for the test suite
276 channel_t *
277 new_fake_channel(void)
279 channel_t *chan = tor_malloc_zero(sizeof(channel_t));
280 channel_init(chan);
282 chan->close = chan_test_close;
283 chan->num_cells_writeable = chan_test_num_cells_writeable;
284 chan->get_remote_descr = chan_test_get_remote_descr;
285 chan->write_packed_cell = chan_test_write_packed_cell;
286 chan->write_var_cell = chan_test_write_var_cell;
287 chan->state = CHANNEL_STATE_OPEN;
289 chan->cmux = circuitmux_alloc();
290 circuitmux_set_policy(chan->cmux, &ewma_policy);
292 return chan;
295 void
296 free_fake_channel(channel_t *chan)
298 if (! chan)
299 return;
301 if (chan->cmux)
302 circuitmux_free(chan->cmux);
304 tor_free(chan);
308 * Counter query for scheduler_channel_has_waiting_cells_mock()
312 get_mock_scheduler_has_waiting_cells_count(void)
314 return test_has_waiting_cells_count;
318 * Mock for scheduler_channel_has_waiting_cells()
321 void
322 scheduler_channel_has_waiting_cells_mock(channel_t *ch)
324 (void)ch;
326 /* Increment counter */
327 ++test_has_waiting_cells_count;
329 return;
332 static void
333 scheduler_channel_doesnt_want_writes_mock(channel_t *ch)
335 (void)ch;
337 /* Increment counter */
338 ++test_doesnt_want_writes_count;
340 return;
344 * Mock for scheduler_release_channel()
347 void
348 scheduler_release_channel_mock(channel_t *ch)
350 (void)ch;
352 /* Increment counter */
353 ++test_releases_count;
355 return;
358 static int
359 test_chan_is_canonical(channel_t *chan)
361 tor_assert(chan);
363 if (test_chan_should_be_canonical) {
364 return 1;
366 return 0;
369 static int
370 test_chan_matches_target(channel_t *chan, const tor_addr_t *target)
372 (void) chan;
373 (void) target;
375 if (test_chan_should_match_target) {
376 return 1;
378 return 0;
381 static void
382 test_chan_listener_close(channel_listener_t *chan)
384 (void) chan;
385 ++test_chan_listener_close_fn_called;
386 return;
389 static void
390 test_chan_listener_fn(channel_listener_t *listener, channel_t *chan)
392 (void) listener;
393 (void) chan;
395 ++test_chan_listener_fn_called;
396 return;
399 static const char *
400 test_chan_listener_describe_transport(channel_listener_t *chan)
402 (void) chan;
403 return "Fake listener channel.";
407 * Test for channel_dumpstats() and limited test for
408 * channel_dump_statistics()
411 static void
412 test_channel_dumpstats(void *arg)
414 channel_t *ch = NULL;
415 cell_t *cell = NULL;
416 packed_cell_t *p_cell = NULL;
417 int old_count;
419 (void)arg;
421 /* Mock these for duration of the test */
422 MOCK(scheduler_channel_doesnt_want_writes,
423 scheduler_channel_doesnt_want_writes_mock);
424 MOCK(scheduler_release_channel,
425 scheduler_release_channel_mock);
427 /* Set up a new fake channel */
428 ch = new_fake_channel();
429 tt_assert(ch);
431 /* Try to register it */
432 channel_register(ch);
433 tt_assert(ch->registered);
435 /* Set up mock */
436 dump_statistics_mock_target = ch;
437 dump_statistics_mock_matches = 0;
438 MOCK(channel_dump_statistics,
439 chan_test_channel_dump_statistics_mock);
441 /* Call channel_dumpstats() */
442 channel_dumpstats(LOG_DEBUG);
444 /* Assert that we hit the mock */
445 tt_int_op(dump_statistics_mock_matches, OP_EQ, 1);
447 /* Close the channel */
448 channel_mark_for_close(ch);
449 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING);
450 chan_test_finish_close(ch);
451 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSED);
453 /* Try again and hit the finished channel */
454 channel_dumpstats(LOG_DEBUG);
455 tt_int_op(dump_statistics_mock_matches, OP_EQ, 2);
457 channel_run_cleanup();
458 ch = NULL;
460 /* Now we should hit nothing */
461 channel_dumpstats(LOG_DEBUG);
462 tt_int_op(dump_statistics_mock_matches, OP_EQ, 2);
464 /* Unmock */
465 UNMOCK(channel_dump_statistics);
466 dump_statistics_mock_target = NULL;
467 dump_statistics_mock_matches = 0;
469 /* Now make another channel */
470 ch = new_fake_channel();
471 tt_assert(ch);
472 channel_register(ch);
473 tt_int_op(ch->registered, OP_EQ, 1);
474 /* Lie about its age so dumpstats gets coverage for rate calculations */
475 ch->timestamp_created = time(NULL) - 30;
476 tt_int_op(ch->timestamp_created, OP_GT, 0);
477 tt_int_op(time(NULL), OP_GT, ch->timestamp_created);
479 /* Put cells through it both ways to make the counters non-zero */
480 p_cell = packed_cell_new();
481 test_chan_accept_cells = 1;
482 old_count = test_cells_written;
483 channel_write_packed_cell(ch, p_cell);
484 tt_int_op(test_cells_written, OP_EQ, old_count + 1);
485 tt_u64_op(ch->n_bytes_xmitted, OP_GT, 0);
486 tt_u64_op(ch->n_cells_xmitted, OP_GT, 0);
488 /* Receive path */
489 channel_set_cell_handlers(ch,
490 chan_test_cell_handler,
491 chan_test_var_cell_handler);
492 tt_ptr_op(channel_get_cell_handler(ch), OP_EQ, chan_test_cell_handler);
493 tt_ptr_op(channel_get_var_cell_handler(ch), OP_EQ,
494 chan_test_var_cell_handler);
495 cell = tor_malloc_zero(sizeof(*cell));
496 old_count = test_chan_fixed_cells_recved;
497 channel_process_cell(ch, cell);
498 tt_int_op(test_chan_fixed_cells_recved, OP_EQ, old_count + 1);
499 tt_u64_op(ch->n_bytes_recved, OP_GT, 0);
500 tt_u64_op(ch->n_cells_recved, OP_GT, 0);
502 /* Test channel_dump_statistics */
503 ch->describe_transport = chan_test_describe_transport;
504 ch->dumpstats = chan_test_dumpstats;
505 test_chan_should_be_canonical = 1;
506 ch->is_canonical = test_chan_is_canonical;
507 old_count = test_dumpstats_calls;
508 channel_dump_statistics(ch, LOG_DEBUG);
509 tt_int_op(test_dumpstats_calls, OP_EQ, old_count + 1);
511 /* Close the channel */
512 channel_mark_for_close(ch);
513 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING);
514 chan_test_finish_close(ch);
515 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSED);
516 channel_run_cleanup();
517 ch = NULL;
519 done:
520 free_fake_channel(ch);
521 tor_free(cell);
523 UNMOCK(scheduler_channel_doesnt_want_writes);
524 UNMOCK(scheduler_release_channel);
526 return;
529 /* Test outbound cell. The callstack is:
530 * channel_flush_some_cells()
531 * -> channel_flush_from_first_active_circuit()
532 * -> channel_write_packed_cell()
533 * -> write_packed_cell()
534 * -> chan->write_packed_cell() fct ptr.
536 * This test goes from a cell in a circuit up to the channel write handler
537 * that should put them on the connection outbuf. */
538 static void
539 test_channel_outbound_cell(void *arg)
541 int old_count;
542 channel_t *chan = NULL;
543 packed_cell_t *p_cell = NULL, *p_cell2 = NULL;
544 origin_circuit_t *circ = NULL;
545 cell_queue_t *queue;
547 (void) arg;
549 /* Set the test time to be mocked, since this test assumes that no
550 * time will pass, ewma values will not need to be re-scaled, and so on */
551 monotime_enable_test_mocking();
552 monotime_set_mock_time_nsec(UINT64_C(1000000000) * 12345);
554 cmux_ewma_set_options(NULL,NULL);
556 /* The channel will be freed so we need to hijack this so the scheduler
557 * doesn't get confused. */
558 MOCK(scheduler_release_channel, scheduler_release_channel_mock);
560 /* Accept cells to lower layer */
561 test_chan_accept_cells = 1;
563 /* Setup a valid circuit to queue a cell. */
564 circ = origin_circuit_new();
565 tt_assert(circ);
566 /* Circuit needs an origin purpose to be considered origin. */
567 TO_CIRCUIT(circ)->purpose = CIRCUIT_PURPOSE_C_GENERAL;
568 TO_CIRCUIT(circ)->n_circ_id = 42;
569 /* This is the outbound test so use the next channel queue. */
570 queue = &TO_CIRCUIT(circ)->n_chan_cells;
571 /* Setup packed cell to queue on the circuit. */
572 p_cell = packed_cell_new();
573 tt_assert(p_cell);
574 p_cell2 = packed_cell_new();
575 tt_assert(p_cell2);
576 /* Setup a channel to put the circuit on. */
577 chan = new_fake_channel();
578 tt_assert(chan);
579 chan->state = CHANNEL_STATE_OPENING;
580 channel_change_state_open(chan);
581 /* Outbound channel. */
582 channel_mark_outgoing(chan);
583 /* Try to register it so we can clean it through the channel cleanup
584 * process. */
585 channel_register(chan);
586 tt_int_op(chan->registered, OP_EQ, 1);
587 /* Set EWMA policy so we can pick it when flushing. */
588 circuitmux_set_policy(chan->cmux, &ewma_policy);
589 tt_ptr_op(circuitmux_get_policy(chan->cmux), OP_EQ, &ewma_policy);
591 /* Register circuit to the channel circid map which will attach the circuit
592 * to the channel's cmux as well. */
593 circuit_set_n_circid_chan(TO_CIRCUIT(circ), 42, chan);
594 tt_int_op(channel_num_circuits(chan), OP_EQ, 1);
595 /* Test the cmux state. */
596 tt_ptr_op(TO_CIRCUIT(circ)->n_mux, OP_EQ, chan->cmux);
597 tt_int_op(circuitmux_is_circuit_attached(chan->cmux, TO_CIRCUIT(circ)),
598 OP_EQ, 1);
600 /* Flush the channel without any cell on it. */
601 old_count = test_cells_written;
602 ssize_t flushed = channel_flush_some_cells(chan, 1);
603 tt_i64_op(flushed, OP_EQ, 0);
604 tt_int_op(test_cells_written, OP_EQ, old_count);
605 tt_int_op(channel_more_to_flush(chan), OP_EQ, 0);
606 tt_int_op(circuitmux_num_active_circuits(chan->cmux), OP_EQ, 0);
607 tt_int_op(circuitmux_num_cells(chan->cmux), OP_EQ, 0);
608 tt_int_op(circuitmux_is_circuit_active(chan->cmux, TO_CIRCUIT(circ)),
609 OP_EQ, 0);
610 tt_u64_op(chan->n_cells_xmitted, OP_EQ, 0);
611 tt_u64_op(chan->n_bytes_xmitted, OP_EQ, 0);
613 /* Queue cell onto the next queue that is the outbound direction. Than
614 * update its cmux so the circuit can be picked when flushing cells. */
615 cell_queue_append(queue, p_cell);
616 p_cell = NULL;
617 tt_int_op(queue->n, OP_EQ, 1);
618 cell_queue_append(queue, p_cell2);
619 p_cell2 = NULL;
620 tt_int_op(queue->n, OP_EQ, 2);
622 update_circuit_on_cmux(TO_CIRCUIT(circ), CELL_DIRECTION_OUT);
623 tt_int_op(circuitmux_num_active_circuits(chan->cmux), OP_EQ, 1);
624 tt_int_op(circuitmux_num_cells(chan->cmux), OP_EQ, 2);
625 tt_int_op(circuitmux_is_circuit_active(chan->cmux, TO_CIRCUIT(circ)),
626 OP_EQ, 1);
628 /* From this point on, we have a queued cell on an active circuit attached
629 * to the channel's cmux. */
631 /* Flush the first cell. This is going to go down the call stack. */
632 old_count = test_cells_written;
633 flushed = channel_flush_some_cells(chan, 1);
634 tt_i64_op(flushed, OP_EQ, 1);
635 tt_int_op(test_cells_written, OP_EQ, old_count + 1);
636 tt_int_op(circuitmux_num_cells(chan->cmux), OP_EQ, 1);
637 tt_int_op(channel_more_to_flush(chan), OP_EQ, 1);
638 /* Circuit should remain active because there is a second cell queued. */
639 tt_int_op(circuitmux_is_circuit_active(chan->cmux, TO_CIRCUIT(circ)),
640 OP_EQ, 1);
641 /* Should still be attached. */
642 tt_int_op(circuitmux_is_circuit_attached(chan->cmux, TO_CIRCUIT(circ)),
643 OP_EQ, 1);
644 tt_u64_op(chan->n_cells_xmitted, OP_EQ, 1);
645 tt_u64_op(chan->n_bytes_xmitted, OP_EQ, get_cell_network_size(0));
647 /* Flush second cell. This is going to go down the call stack. */
648 old_count = test_cells_written;
649 flushed = channel_flush_some_cells(chan, 1);
650 tt_i64_op(flushed, OP_EQ, 1);
651 tt_int_op(test_cells_written, OP_EQ, old_count + 1);
652 tt_int_op(circuitmux_num_cells(chan->cmux), OP_EQ, 0);
653 tt_int_op(channel_more_to_flush(chan), OP_EQ, 0);
654 /* No more cells should make the circuit inactive. */
655 tt_int_op(circuitmux_is_circuit_active(chan->cmux, TO_CIRCUIT(circ)),
656 OP_EQ, 0);
657 /* Should still be attached. */
658 tt_int_op(circuitmux_is_circuit_attached(chan->cmux, TO_CIRCUIT(circ)),
659 OP_EQ, 1);
660 tt_u64_op(chan->n_cells_xmitted, OP_EQ, 2);
661 tt_u64_op(chan->n_bytes_xmitted, OP_EQ, get_cell_network_size(0) * 2);
663 done:
664 if (circ) {
665 circuit_free_(TO_CIRCUIT(circ));
667 tor_free(p_cell);
668 channel_free_all();
669 UNMOCK(scheduler_release_channel);
670 monotime_disable_test_mocking();
673 /* Test inbound cell. The callstack is:
674 * channel_process_cell()
675 * -> chan->cell_handler()
677 * This test is about checking if we can process an inbound cell down to the
678 * channel handler. */
679 static void
680 test_channel_inbound_cell(void *arg)
682 channel_t *chan = NULL;
683 cell_t *cell = NULL;
684 int old_count;
686 (void) arg;
688 /* The channel will be freed so we need to hijack this so the scheduler
689 * doesn't get confused. */
690 MOCK(scheduler_release_channel, scheduler_release_channel_mock);
692 /* Accept cells to lower layer */
693 test_chan_accept_cells = 1;
695 chan = new_fake_channel();
696 tt_assert(chan);
697 /* Start it off in OPENING */
698 chan->state = CHANNEL_STATE_OPENING;
700 /* Try to register it */
701 channel_register(chan);
702 tt_int_op(chan->registered, OP_EQ, 1);
704 /* Open it */
705 channel_change_state_open(chan);
706 tt_int_op(chan->state, OP_EQ, CHANNEL_STATE_OPEN);
707 tt_int_op(chan->has_been_open, OP_EQ, 1);
709 /* Receive a cell now. */
710 cell = tor_malloc_zero(sizeof(*cell));
711 make_fake_cell(cell);
712 old_count = test_chan_fixed_cells_recved;
713 channel_process_cell(chan, cell);
714 tt_int_op(test_chan_fixed_cells_recved, OP_EQ, old_count);
715 tt_assert(monotime_coarse_is_zero(&chan->timestamp_xfer));
716 tt_u64_op(chan->timestamp_active, OP_EQ, 0);
717 tt_u64_op(chan->timestamp_recv, OP_EQ, 0);
719 /* Setup incoming cell handlers. We don't care about var cell, the channel
720 * layers is not handling those. */
721 channel_set_cell_handlers(chan, chan_test_cell_handler, NULL);
722 tt_ptr_op(chan->cell_handler, OP_EQ, chan_test_cell_handler);
723 /* Now process the cell, we should see it. */
724 old_count = test_chan_fixed_cells_recved;
725 channel_process_cell(chan, cell);
726 tt_int_op(test_chan_fixed_cells_recved, OP_EQ, old_count + 1);
727 /* We should have a series of timestamp set. */
728 tt_assert(!monotime_coarse_is_zero(&chan->timestamp_xfer));
729 tt_u64_op(chan->timestamp_active, OP_NE, 0);
730 tt_u64_op(chan->timestamp_recv, OP_NE, 0);
731 tt_assert(monotime_coarse_is_zero(&chan->next_padding_time));
732 tt_u64_op(chan->n_cells_recved, OP_EQ, 1);
733 tt_u64_op(chan->n_bytes_recved, OP_EQ, get_cell_network_size(0));
735 /* Close it */
736 old_count = test_close_called;
737 channel_mark_for_close(chan);
738 tt_int_op(chan->state, OP_EQ, CHANNEL_STATE_CLOSING);
739 tt_int_op(chan->reason_for_closing, OP_EQ, CHANNEL_CLOSE_REQUESTED);
740 tt_int_op(test_close_called, OP_EQ, old_count + 1);
742 /* This closes the channe so it calls in the scheduler, make sure of it. */
743 old_count = test_releases_count;
744 chan_test_finish_close(chan);
745 tt_int_op(test_releases_count, OP_EQ, old_count + 1);
746 tt_int_op(chan->state, OP_EQ, CHANNEL_STATE_CLOSED);
748 /* The channel will be free, lets make sure it is not accessible. */
749 uint64_t chan_id = chan->global_identifier;
750 tt_ptr_op(channel_find_by_global_id(chan_id), OP_EQ, chan);
751 channel_run_cleanup();
752 chan = channel_find_by_global_id(chan_id);
753 tt_assert(chan == NULL);
755 done:
756 tor_free(cell);
757 UNMOCK(scheduler_release_channel);
761 * Normal channel lifecycle test:
763 * OPENING->OPEN->MAINT->OPEN->CLOSING->CLOSED
766 static void
767 test_channel_lifecycle(void *arg)
769 channel_t *ch1 = NULL, *ch2 = NULL;
770 packed_cell_t *p_cell = NULL;
771 int old_count, init_doesnt_want_writes_count;
772 int init_releases_count;
774 (void)arg;
776 /* Mock these for the whole lifecycle test */
777 MOCK(scheduler_channel_doesnt_want_writes,
778 scheduler_channel_doesnt_want_writes_mock);
779 MOCK(scheduler_release_channel,
780 scheduler_release_channel_mock);
782 /* Cache some initial counter values */
783 init_doesnt_want_writes_count = test_doesnt_want_writes_count;
784 init_releases_count = test_releases_count;
786 /* Accept cells to lower layer */
787 test_chan_accept_cells = 1;
789 ch1 = new_fake_channel();
790 tt_assert(ch1);
791 /* Start it off in OPENING */
792 ch1->state = CHANNEL_STATE_OPENING;
794 /* Try to register it */
795 channel_register(ch1);
796 tt_assert(ch1->registered);
798 /* Try to write a cell through (should queue) */
799 p_cell = packed_cell_new();
800 old_count = test_cells_written;
801 channel_write_packed_cell(ch1, p_cell);
802 tt_int_op(old_count, OP_EQ, test_cells_written);
804 /* Move it to OPEN and flush */
805 channel_change_state_open(ch1);
807 /* Get another one */
808 ch2 = new_fake_channel();
809 tt_assert(ch2);
810 ch2->state = CHANNEL_STATE_OPENING;
812 /* Register */
813 channel_register(ch2);
814 tt_assert(ch2->registered);
816 /* Check counters */
817 tt_int_op(test_doesnt_want_writes_count, OP_EQ,
818 init_doesnt_want_writes_count);
819 tt_int_op(test_releases_count, OP_EQ, init_releases_count);
821 /* Move ch1 to MAINT */
822 channel_change_state(ch1, CHANNEL_STATE_MAINT);
823 tt_int_op(test_doesnt_want_writes_count, OP_EQ,
824 init_doesnt_want_writes_count + 1);
825 tt_int_op(test_releases_count, OP_EQ, init_releases_count);
827 /* Move ch2 to OPEN */
828 channel_change_state_open(ch2);
829 tt_int_op(test_doesnt_want_writes_count, OP_EQ,
830 init_doesnt_want_writes_count + 1);
831 tt_int_op(test_releases_count, OP_EQ, init_releases_count);
833 /* Move ch1 back to OPEN */
834 channel_change_state_open(ch1);
835 tt_int_op(test_doesnt_want_writes_count, OP_EQ,
836 init_doesnt_want_writes_count + 1);
837 tt_int_op(test_releases_count, OP_EQ, init_releases_count);
839 /* Mark ch2 for close */
840 channel_mark_for_close(ch2);
841 tt_int_op(ch2->state, OP_EQ, CHANNEL_STATE_CLOSING);
842 tt_int_op(test_doesnt_want_writes_count, OP_EQ,
843 init_doesnt_want_writes_count + 1);
844 tt_int_op(test_releases_count, OP_EQ, init_releases_count + 1);
846 /* Shut down channels */
847 channel_free_all();
848 ch1 = ch2 = NULL;
849 tt_int_op(test_doesnt_want_writes_count, OP_EQ,
850 init_doesnt_want_writes_count + 1);
851 /* channel_free() calls scheduler_release_channel() */
852 tt_int_op(test_releases_count, OP_EQ, init_releases_count + 4);
854 done:
855 free_fake_channel(ch1);
856 free_fake_channel(ch2);
858 UNMOCK(scheduler_channel_doesnt_want_writes);
859 UNMOCK(scheduler_release_channel);
863 * Weird channel lifecycle test:
865 * OPENING->CLOSING->CLOSED
866 * OPENING->OPEN->CLOSING->ERROR
867 * OPENING->OPEN->MAINT->CLOSING->CLOSED
868 * OPENING->OPEN->MAINT->CLOSING->ERROR
871 static void
872 test_channel_lifecycle_2(void *arg)
874 channel_t *ch = NULL;
876 (void)arg;
878 /* Mock these for the whole lifecycle test */
879 MOCK(scheduler_channel_doesnt_want_writes,
880 scheduler_channel_doesnt_want_writes_mock);
881 MOCK(scheduler_release_channel,
882 scheduler_release_channel_mock);
884 /* Accept cells to lower layer */
885 test_chan_accept_cells = 1;
887 ch = new_fake_channel();
888 tt_assert(ch);
889 /* Start it off in OPENING */
890 ch->state = CHANNEL_STATE_OPENING;
892 /* Try to register it */
893 channel_register(ch);
894 tt_assert(ch->registered);
896 /* Try to close it */
897 channel_mark_for_close(ch);
898 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING);
900 /* Finish closing it */
901 chan_test_finish_close(ch);
902 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSED);
903 channel_run_cleanup();
904 ch = NULL;
906 /* Now try OPENING->OPEN->CLOSING->ERROR */
907 ch = new_fake_channel();
908 tt_assert(ch);
909 ch->state = CHANNEL_STATE_OPENING;
910 channel_register(ch);
911 tt_assert(ch->registered);
913 /* Finish opening it */
914 channel_change_state_open(ch);
916 /* Error exit from lower layer */
917 chan_test_error(ch);
918 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING);
919 chan_test_finish_close(ch);
920 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_ERROR);
921 channel_run_cleanup();
922 ch = NULL;
924 /* OPENING->OPEN->MAINT->CLOSING->CLOSED close from maintenance state */
925 ch = new_fake_channel();
926 tt_assert(ch);
927 ch->state = CHANNEL_STATE_OPENING;
928 channel_register(ch);
929 tt_assert(ch->registered);
931 /* Finish opening it */
932 channel_change_state_open(ch);
933 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_OPEN);
935 /* Go to maintenance state */
936 channel_change_state(ch, CHANNEL_STATE_MAINT);
937 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_MAINT);
939 /* Lower layer close */
940 channel_mark_for_close(ch);
941 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING);
943 /* Finish */
944 chan_test_finish_close(ch);
945 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSED);
946 channel_run_cleanup();
947 ch = NULL;
950 * OPENING->OPEN->MAINT->CLOSING->CLOSED lower-layer close during
951 * maintenance state
953 ch = new_fake_channel();
954 tt_assert(ch);
955 ch->state = CHANNEL_STATE_OPENING;
956 channel_register(ch);
957 tt_assert(ch->registered);
959 /* Finish opening it */
960 channel_change_state_open(ch);
961 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_OPEN);
963 /* Go to maintenance state */
964 channel_change_state(ch, CHANNEL_STATE_MAINT);
965 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_MAINT);
967 /* Lower layer close */
968 channel_close_from_lower_layer(ch);
969 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING);
971 /* Finish */
972 chan_test_finish_close(ch);
973 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSED);
974 channel_run_cleanup();
975 ch = NULL;
977 /* OPENING->OPEN->MAINT->CLOSING->ERROR */
978 ch = new_fake_channel();
979 tt_assert(ch);
980 ch->state = CHANNEL_STATE_OPENING;
981 channel_register(ch);
982 tt_assert(ch->registered);
984 /* Finish opening it */
985 channel_change_state_open(ch);
986 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_OPEN);
988 /* Go to maintenance state */
989 channel_change_state(ch, CHANNEL_STATE_MAINT);
990 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_MAINT);
992 /* Lower layer close */
993 chan_test_error(ch);
994 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING);
996 /* Finish */
997 chan_test_finish_close(ch);
998 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_ERROR);
999 channel_run_cleanup();
1000 ch = NULL;
1002 /* Shut down channels */
1003 channel_free_all();
1005 done:
1006 tor_free(ch);
1008 UNMOCK(scheduler_channel_doesnt_want_writes);
1009 UNMOCK(scheduler_release_channel);
1011 return;
1014 static void
1015 test_channel_id_map(void *arg)
1017 (void)arg;
1018 #define N_CHAN 6
1019 char rsa_id[N_CHAN][DIGEST_LEN];
1020 ed25519_public_key_t *ed_id[N_CHAN];
1021 channel_t *chan[N_CHAN];
1022 int i;
1023 ed25519_public_key_t ed_zero;
1024 memset(&ed_zero, 0, sizeof(ed_zero));
1026 tt_int_op(DIGEST_LEN, OP_EQ, sizeof(rsa_id[0])); // Do I remember C?
1028 for (i = 0; i < N_CHAN; ++i) {
1029 crypto_rand(rsa_id[i], DIGEST_LEN);
1030 ed_id[i] = tor_malloc_zero(sizeof(*ed_id[i]));
1031 crypto_rand((char*)ed_id[i]->pubkey, sizeof(ed_id[i]->pubkey));
1034 /* For channel 3, have no Ed identity. */
1035 tor_free(ed_id[3]);
1037 /* Channel 2 and 4 have same ROSA identity */
1038 memcpy(rsa_id[4], rsa_id[2], DIGEST_LEN);
1040 /* Channel 2 and 4 and 5 have same RSA identity */
1041 memcpy(rsa_id[4], rsa_id[2], DIGEST_LEN);
1042 memcpy(rsa_id[5], rsa_id[2], DIGEST_LEN);
1044 /* Channels 2 and 5 have same Ed25519 identity */
1045 memcpy(ed_id[5], ed_id[2], sizeof(*ed_id[2]));
1047 for (i = 0; i < N_CHAN; ++i) {
1048 chan[i] = new_fake_channel();
1049 channel_register(chan[i]);
1050 channel_set_identity_digest(chan[i], rsa_id[i], ed_id[i]);
1053 /* Lookup by RSA id only */
1054 tt_ptr_op(chan[0], OP_EQ,
1055 channel_find_by_remote_identity(rsa_id[0], NULL));
1056 tt_ptr_op(chan[1], OP_EQ,
1057 channel_find_by_remote_identity(rsa_id[1], NULL));
1058 tt_ptr_op(chan[3], OP_EQ,
1059 channel_find_by_remote_identity(rsa_id[3], NULL));
1060 channel_t *ch;
1061 ch = channel_find_by_remote_identity(rsa_id[2], NULL);
1062 tt_assert(ch == chan[2] || ch == chan[4] || ch == chan[5]);
1063 ch = channel_next_with_rsa_identity(ch);
1064 tt_assert(ch == chan[2] || ch == chan[4] || ch == chan[5]);
1065 ch = channel_next_with_rsa_identity(ch);
1066 tt_assert(ch == chan[2] || ch == chan[4] || ch == chan[5]);
1067 ch = channel_next_with_rsa_identity(ch);
1068 tt_ptr_op(ch, OP_EQ, NULL);
1070 /* As above, but with zero Ed25519 ID (meaning "any ID") */
1071 tt_ptr_op(chan[0], OP_EQ,
1072 channel_find_by_remote_identity(rsa_id[0], &ed_zero));
1073 tt_ptr_op(chan[1], OP_EQ,
1074 channel_find_by_remote_identity(rsa_id[1], &ed_zero));
1075 tt_ptr_op(chan[3], OP_EQ,
1076 channel_find_by_remote_identity(rsa_id[3], &ed_zero));
1077 ch = channel_find_by_remote_identity(rsa_id[2], &ed_zero);
1078 tt_assert(ch == chan[2] || ch == chan[4] || ch == chan[5]);
1079 ch = channel_next_with_rsa_identity(ch);
1080 tt_assert(ch == chan[2] || ch == chan[4] || ch == chan[5]);
1081 ch = channel_next_with_rsa_identity(ch);
1082 tt_assert(ch == chan[2] || ch == chan[4] || ch == chan[5]);
1083 ch = channel_next_with_rsa_identity(ch);
1084 tt_ptr_op(ch, OP_EQ, NULL);
1086 /* Lookup nonexistent RSA identity */
1087 tt_ptr_op(NULL, OP_EQ,
1088 channel_find_by_remote_identity("!!!!!!!!!!!!!!!!!!!!", NULL));
1090 /* Look up by full identity pair */
1091 tt_ptr_op(chan[0], OP_EQ,
1092 channel_find_by_remote_identity(rsa_id[0], ed_id[0]));
1093 tt_ptr_op(chan[1], OP_EQ,
1094 channel_find_by_remote_identity(rsa_id[1], ed_id[1]));
1095 tt_ptr_op(chan[3], OP_EQ,
1096 channel_find_by_remote_identity(rsa_id[3], ed_id[3] /*NULL*/));
1097 tt_ptr_op(chan[4], OP_EQ,
1098 channel_find_by_remote_identity(rsa_id[4], ed_id[4]));
1099 ch = channel_find_by_remote_identity(rsa_id[2], ed_id[2]);
1100 tt_assert(ch == chan[2] || ch == chan[5]);
1102 /* Look up RSA identity with wrong ed25519 identity */
1103 tt_ptr_op(NULL, OP_EQ,
1104 channel_find_by_remote_identity(rsa_id[4], ed_id[0]));
1105 tt_ptr_op(NULL, OP_EQ,
1106 channel_find_by_remote_identity(rsa_id[2], ed_id[1]));
1107 tt_ptr_op(NULL, OP_EQ,
1108 channel_find_by_remote_identity(rsa_id[3], ed_id[1]));
1110 done:
1111 for (i = 0; i < N_CHAN; ++i) {
1112 channel_clear_identity_digest(chan[i]);
1113 channel_unregister(chan[i]);
1114 free_fake_channel(chan[i]);
1115 tor_free(ed_id[i]);
1117 #undef N_CHAN
1120 static void
1121 test_channel_state(void *arg)
1123 (void) arg;
1125 /* Test state validity. */
1126 tt_int_op(channel_state_is_valid(CHANNEL_STATE_CLOSED), OP_EQ, 1);
1127 tt_int_op(channel_state_is_valid(CHANNEL_STATE_CLOSING), OP_EQ, 1);
1128 tt_int_op(channel_state_is_valid(CHANNEL_STATE_ERROR), OP_EQ, 1);
1129 tt_int_op(channel_state_is_valid(CHANNEL_STATE_OPEN), OP_EQ, 1);
1130 tt_int_op(channel_state_is_valid(CHANNEL_STATE_OPENING), OP_EQ, 1);
1131 tt_int_op(channel_state_is_valid(CHANNEL_STATE_MAINT), OP_EQ, 1);
1132 tt_int_op(channel_state_is_valid(CHANNEL_STATE_LAST), OP_EQ, 0);
1133 tt_int_op(channel_state_is_valid(INT_MAX), OP_EQ, 0);
1135 /* Test listener state validity. */
1136 tt_int_op(channel_listener_state_is_valid(CHANNEL_LISTENER_STATE_CLOSED),
1137 OP_EQ, 1);
1138 tt_int_op(channel_listener_state_is_valid(CHANNEL_LISTENER_STATE_LISTENING),
1139 OP_EQ, 1);
1140 tt_int_op(channel_listener_state_is_valid(CHANNEL_LISTENER_STATE_CLOSING),
1141 OP_EQ, 1);
1142 tt_int_op(channel_listener_state_is_valid(CHANNEL_LISTENER_STATE_ERROR),
1143 OP_EQ, 1);
1144 tt_int_op(channel_listener_state_is_valid(CHANNEL_LISTENER_STATE_LAST),
1145 OP_EQ, 0);
1146 tt_int_op(channel_listener_state_is_valid(INT_MAX), OP_EQ, 0);
1148 /* Test state transition. */
1149 tt_int_op(channel_state_can_transition(CHANNEL_STATE_CLOSED,
1150 CHANNEL_STATE_OPENING), OP_EQ, 1);
1151 tt_int_op(channel_state_can_transition(CHANNEL_STATE_CLOSED,
1152 CHANNEL_STATE_ERROR), OP_EQ, 0);
1153 tt_int_op(channel_state_can_transition(CHANNEL_STATE_CLOSING,
1154 CHANNEL_STATE_ERROR), OP_EQ, 1);
1155 tt_int_op(channel_state_can_transition(CHANNEL_STATE_CLOSING,
1156 CHANNEL_STATE_CLOSED), OP_EQ, 1);
1157 tt_int_op(channel_state_can_transition(CHANNEL_STATE_CLOSING,
1158 CHANNEL_STATE_OPEN), OP_EQ, 0);
1159 tt_int_op(channel_state_can_transition(CHANNEL_STATE_MAINT,
1160 CHANNEL_STATE_CLOSING), OP_EQ, 1);
1161 tt_int_op(channel_state_can_transition(CHANNEL_STATE_MAINT,
1162 CHANNEL_STATE_ERROR), OP_EQ, 1);
1163 tt_int_op(channel_state_can_transition(CHANNEL_STATE_MAINT,
1164 CHANNEL_STATE_OPEN), OP_EQ, 1);
1165 tt_int_op(channel_state_can_transition(CHANNEL_STATE_MAINT,
1166 CHANNEL_STATE_OPENING), OP_EQ, 0);
1167 tt_int_op(channel_state_can_transition(CHANNEL_STATE_OPENING,
1168 CHANNEL_STATE_OPEN), OP_EQ, 1);
1169 tt_int_op(channel_state_can_transition(CHANNEL_STATE_OPENING,
1170 CHANNEL_STATE_CLOSING), OP_EQ, 1);
1171 tt_int_op(channel_state_can_transition(CHANNEL_STATE_OPENING,
1172 CHANNEL_STATE_ERROR), OP_EQ, 1);
1173 tt_int_op(channel_state_can_transition(CHANNEL_STATE_OPEN,
1174 CHANNEL_STATE_ERROR), OP_EQ, 1);
1175 tt_int_op(channel_state_can_transition(CHANNEL_STATE_OPEN,
1176 CHANNEL_STATE_CLOSING), OP_EQ, 1);
1177 tt_int_op(channel_state_can_transition(CHANNEL_STATE_OPEN,
1178 CHANNEL_STATE_ERROR), OP_EQ, 1);
1179 tt_int_op(channel_state_can_transition(CHANNEL_STATE_OPEN,
1180 CHANNEL_STATE_MAINT), OP_EQ, 1);
1181 tt_int_op(channel_state_can_transition(CHANNEL_STATE_LAST,
1182 CHANNEL_STATE_MAINT), OP_EQ, 0);
1183 tt_int_op(channel_state_can_transition(CHANNEL_STATE_LAST, INT_MAX),
1184 OP_EQ, 0);
1186 /* Test listener state transition. */
1187 tt_int_op(channel_listener_state_can_transition(
1188 CHANNEL_LISTENER_STATE_CLOSED,
1189 CHANNEL_LISTENER_STATE_LISTENING),
1190 OP_EQ, 1);
1191 tt_int_op(channel_listener_state_can_transition(
1192 CHANNEL_LISTENER_STATE_CLOSED,
1193 CHANNEL_LISTENER_STATE_ERROR),
1194 OP_EQ, 0);
1196 tt_int_op(channel_listener_state_can_transition(
1197 CHANNEL_LISTENER_STATE_CLOSING,
1198 CHANNEL_LISTENER_STATE_CLOSED),
1199 OP_EQ, 1);
1201 tt_int_op(channel_listener_state_can_transition(
1202 CHANNEL_LISTENER_STATE_CLOSING,
1203 CHANNEL_LISTENER_STATE_ERROR),
1204 OP_EQ, 1);
1205 tt_int_op(channel_listener_state_can_transition(
1206 CHANNEL_LISTENER_STATE_ERROR,
1207 CHANNEL_LISTENER_STATE_CLOSING),
1208 OP_EQ, 0);
1210 tt_int_op(channel_listener_state_can_transition(
1211 CHANNEL_LISTENER_STATE_LISTENING,
1212 CHANNEL_LISTENER_STATE_CLOSING),
1213 OP_EQ, 1);
1214 tt_int_op(channel_listener_state_can_transition(
1215 CHANNEL_LISTENER_STATE_LISTENING,
1216 CHANNEL_LISTENER_STATE_ERROR),
1217 OP_EQ, 1);
1218 tt_int_op(channel_listener_state_can_transition(
1219 CHANNEL_LISTENER_STATE_LAST,
1220 INT_MAX),
1221 OP_EQ, 0);
1223 /* Test state string. */
1224 tt_str_op(channel_state_to_string(CHANNEL_STATE_CLOSING), OP_EQ,
1225 "closing");
1226 tt_str_op(channel_state_to_string(CHANNEL_STATE_ERROR), OP_EQ,
1227 "channel error");
1228 tt_str_op(channel_state_to_string(CHANNEL_STATE_CLOSED), OP_EQ,
1229 "closed");
1230 tt_str_op(channel_state_to_string(CHANNEL_STATE_OPEN), OP_EQ,
1231 "open");
1232 tt_str_op(channel_state_to_string(CHANNEL_STATE_OPENING), OP_EQ,
1233 "opening");
1234 tt_str_op(channel_state_to_string(CHANNEL_STATE_MAINT), OP_EQ,
1235 "temporarily suspended for maintenance");
1236 tt_str_op(channel_state_to_string(CHANNEL_STATE_LAST), OP_EQ,
1237 "unknown or invalid channel state");
1238 tt_str_op(channel_state_to_string(INT_MAX), OP_EQ,
1239 "unknown or invalid channel state");
1241 /* Test listener state string. */
1242 tt_str_op(channel_listener_state_to_string(CHANNEL_LISTENER_STATE_CLOSING),
1243 OP_EQ, "closing");
1244 tt_str_op(channel_listener_state_to_string(CHANNEL_LISTENER_STATE_ERROR),
1245 OP_EQ, "channel listener error");
1246 tt_str_op(channel_listener_state_to_string(CHANNEL_LISTENER_STATE_LISTENING),
1247 OP_EQ, "listening");
1248 tt_str_op(channel_listener_state_to_string(CHANNEL_LISTENER_STATE_LAST),
1249 OP_EQ, "unknown or invalid channel listener state");
1250 tt_str_op(channel_listener_state_to_string(INT_MAX),
1251 OP_EQ, "unknown or invalid channel listener state");
1253 done:
1257 static networkstatus_t *mock_ns = NULL;
1259 static networkstatus_t *
1260 mock_networkstatus_get_latest_consensus(void)
1262 return mock_ns;
1265 static void
1266 test_channel_duplicates(void *arg)
1268 channel_t *chan = NULL;
1269 routerstatus_t rs;
1271 (void) arg;
1273 setup_full_capture_of_logs(LOG_INFO);
1274 /* Try a flat call with channel nor connections. */
1275 channel_check_for_duplicates();
1276 expect_log_msg_containing(
1277 "Found 0 connections to 0 relays. Found 0 current canonical "
1278 "connections, in 0 of which we were a non-canonical peer. "
1279 "0 relays had more than 1 connection, 0 had more than 2, and "
1280 "0 had more than 4 connections.");
1282 mock_ns = tor_malloc_zero(sizeof(*mock_ns));
1283 mock_ns->routerstatus_list = smartlist_new();
1284 MOCK(networkstatus_get_latest_consensus,
1285 mock_networkstatus_get_latest_consensus);
1287 chan = new_fake_channel();
1288 tt_assert(chan);
1289 chan->is_canonical = test_chan_is_canonical;
1290 memset(chan->identity_digest, 'A', sizeof(chan->identity_digest));
1291 channel_add_to_digest_map(chan);
1292 tt_ptr_op(channel_find_by_remote_identity(chan->identity_digest, NULL),
1293 OP_EQ, chan);
1295 /* No relay has been associated with this channel. */
1296 channel_check_for_duplicates();
1297 expect_log_msg_containing(
1298 "Found 0 connections to 0 relays. Found 0 current canonical "
1299 "connections, in 0 of which we were a non-canonical peer. "
1300 "0 relays had more than 1 connection, 0 had more than 2, and "
1301 "0 had more than 4 connections.");
1303 /* Associate relay to this connection in the consensus. */
1304 memset(&rs, 0, sizeof(rs));
1305 memset(rs.identity_digest, 'A', sizeof(rs.identity_digest));
1306 smartlist_add(mock_ns->routerstatus_list, &rs);
1308 /* Non opened channel. */
1309 chan->state = CHANNEL_STATE_CLOSING;
1310 channel_check_for_duplicates();
1311 expect_log_msg_containing(
1312 "Found 0 connections to 0 relays. Found 0 current canonical "
1313 "connections, in 0 of which we were a non-canonical peer. "
1314 "0 relays had more than 1 connection, 0 had more than 2, and "
1315 "0 had more than 4 connections.");
1316 chan->state = CHANNEL_STATE_OPEN;
1318 channel_check_for_duplicates();
1319 expect_log_msg_containing(
1320 "Found 1 connections to 1 relays. Found 0 current canonical "
1321 "connections, in 0 of which we were a non-canonical peer. "
1322 "0 relays had more than 1 connection, 0 had more than 2, and "
1323 "0 had more than 4 connections.");
1325 test_chan_should_be_canonical = 1;
1326 channel_check_for_duplicates();
1327 expect_log_msg_containing(
1328 "Found 1 connections to 1 relays. Found 1 current canonical "
1329 "connections, in 1 of which we were a non-canonical peer. "
1330 "0 relays had more than 1 connection, 0 had more than 2, and "
1331 "0 had more than 4 connections.");
1332 teardown_capture_of_logs();
1334 done:
1335 free_fake_channel(chan);
1336 smartlist_clear(mock_ns->routerstatus_list);
1337 networkstatus_vote_free(mock_ns);
1338 UNMOCK(networkstatus_get_latest_consensus);
1341 static void
1342 test_channel_for_extend(void *arg)
1344 channel_t *chan1 = NULL, *chan2 = NULL;
1345 channel_t *ret_chan = NULL;
1346 char digest[DIGEST_LEN];
1347 ed25519_public_key_t ed_id;
1348 tor_addr_t addr;
1349 const char *msg;
1350 int launch;
1351 time_t now = time(NULL);
1353 (void) arg;
1355 memset(digest, 'A', sizeof(digest));
1356 memset(&ed_id, 'B', sizeof(ed_id));
1358 chan1 = new_fake_channel();
1359 tt_assert(chan1);
1360 /* Need to be registered to get added to the id map. */
1361 channel_register(chan1);
1362 tt_int_op(chan1->registered, OP_EQ, 1);
1363 /* We need those for the test. */
1364 chan1->is_canonical = test_chan_is_canonical;
1365 chan1->matches_target = test_chan_matches_target;
1366 chan1->timestamp_created = now - 9;
1368 chan2 = new_fake_channel();
1369 tt_assert(chan2);
1370 /* Need to be registered to get added to the id map. */
1371 channel_register(chan2);
1372 tt_int_op(chan2->registered, OP_EQ, 1);
1373 /* We need those for the test. */
1374 chan2->is_canonical = test_chan_is_canonical;
1375 chan2->matches_target = test_chan_matches_target;
1376 /* Make it older than chan1. */
1377 chan2->timestamp_created = chan1->timestamp_created - 1;
1379 /* Say it's all canonical. */
1380 test_chan_should_be_canonical = 1;
1382 /* Set channel identities and add it to the channel map. The last one to be
1383 * added is made the first one in the list so the lookup will always return
1384 * that one first. */
1385 channel_set_identity_digest(chan2, digest, &ed_id);
1386 channel_set_identity_digest(chan1, digest, &ed_id);
1387 tt_ptr_op(channel_find_by_remote_identity(digest, NULL), OP_EQ, chan1);
1388 tt_ptr_op(channel_find_by_remote_identity(digest, &ed_id), OP_EQ, chan1);
1390 /* The expected result is chan2 because it is older than chan1. */
1391 ret_chan = channel_get_for_extend(digest, &ed_id, &addr, &msg, &launch);
1392 tt_assert(ret_chan);
1393 tt_ptr_op(ret_chan, OP_EQ, chan2);
1394 tt_int_op(launch, OP_EQ, 0);
1395 tt_str_op(msg, OP_EQ, "Connection is fine; using it.");
1397 /* Switch that around from previous test. */
1398 chan2->timestamp_created = chan1->timestamp_created + 1;
1399 ret_chan = channel_get_for_extend(digest, &ed_id, &addr, &msg, &launch);
1400 tt_assert(ret_chan);
1401 tt_ptr_op(ret_chan, OP_EQ, chan1);
1402 tt_int_op(launch, OP_EQ, 0);
1403 tt_str_op(msg, OP_EQ, "Connection is fine; using it.");
1405 /* Same creation time, num circuits will be used and they both have 0 so the
1406 * channel 2 should be picked due to how channel_is_better() work. */
1407 chan2->timestamp_created = chan1->timestamp_created;
1408 ret_chan = channel_get_for_extend(digest, &ed_id, &addr, &msg, &launch);
1409 tt_assert(ret_chan);
1410 tt_ptr_op(ret_chan, OP_EQ, chan1);
1411 tt_int_op(launch, OP_EQ, 0);
1412 tt_str_op(msg, OP_EQ, "Connection is fine; using it.");
1414 /* For the rest of the tests, we need channel 1 to be the older. */
1415 chan2->timestamp_created = chan1->timestamp_created + 1;
1417 /* Condemned the older channel. */
1418 chan1->state = CHANNEL_STATE_CLOSING;
1419 ret_chan = channel_get_for_extend(digest, &ed_id, &addr, &msg, &launch);
1420 tt_assert(ret_chan);
1421 tt_ptr_op(ret_chan, OP_EQ, chan2);
1422 tt_int_op(launch, OP_EQ, 0);
1423 tt_str_op(msg, OP_EQ, "Connection is fine; using it.");
1424 chan1->state = CHANNEL_STATE_OPEN;
1426 /* Make the older channel a client one. */
1427 channel_mark_client(chan1);
1428 ret_chan = channel_get_for_extend(digest, &ed_id, &addr, &msg, &launch);
1429 tt_assert(ret_chan);
1430 tt_ptr_op(ret_chan, OP_EQ, chan2);
1431 tt_int_op(launch, OP_EQ, 0);
1432 tt_str_op(msg, OP_EQ, "Connection is fine; using it.");
1433 channel_clear_client(chan1);
1435 /* Non matching ed identity with valid digest. */
1436 ed25519_public_key_t dumb_ed_id;
1437 memset(&dumb_ed_id, 0, sizeof(dumb_ed_id));
1438 ret_chan = channel_get_for_extend(digest, &dumb_ed_id, &addr, &msg,
1439 &launch);
1440 tt_assert(!ret_chan);
1441 tt_str_op(msg, OP_EQ, "Not connected. Connecting.");
1442 tt_int_op(launch, OP_EQ, 1);
1444 /* Opening channel, we'll check if the target address matches. */
1445 test_chan_should_match_target = 1;
1446 chan1->state = CHANNEL_STATE_OPENING;
1447 chan2->state = CHANNEL_STATE_OPENING;
1448 ret_chan = channel_get_for_extend(digest, &ed_id, &addr, &msg, &launch);
1449 tt_assert(!ret_chan);
1450 tt_str_op(msg, OP_EQ, "Connection in progress; waiting.");
1451 tt_int_op(launch, OP_EQ, 0);
1452 chan1->state = CHANNEL_STATE_OPEN;
1453 chan2->state = CHANNEL_STATE_OPEN;
1455 /* Mark channel 1 as bad for circuits. */
1456 channel_mark_bad_for_new_circs(chan1);
1457 ret_chan = channel_get_for_extend(digest, &ed_id, &addr, &msg, &launch);
1458 tt_assert(ret_chan);
1459 tt_ptr_op(ret_chan, OP_EQ, chan2);
1460 tt_int_op(launch, OP_EQ, 0);
1461 tt_str_op(msg, OP_EQ, "Connection is fine; using it.");
1462 chan1->is_bad_for_new_circs = 0;
1464 /* Mark both channels as unusable. */
1465 channel_mark_bad_for_new_circs(chan1);
1466 channel_mark_bad_for_new_circs(chan2);
1467 ret_chan = channel_get_for_extend(digest, &ed_id, &addr, &msg, &launch);
1468 tt_assert(!ret_chan);
1469 tt_str_op(msg, OP_EQ, "Connections all too old, or too non-canonical. "
1470 " Launching a new one.");
1471 tt_int_op(launch, OP_EQ, 1);
1472 chan1->is_bad_for_new_circs = 0;
1473 chan2->is_bad_for_new_circs = 0;
1475 /* Non canonical channels. */
1476 test_chan_should_be_canonical = 0;
1477 test_chan_should_match_target = 0;
1478 ret_chan = channel_get_for_extend(digest, &ed_id, &addr, &msg, &launch);
1479 tt_assert(!ret_chan);
1480 tt_str_op(msg, OP_EQ, "Connections all too old, or too non-canonical. "
1481 " Launching a new one.");
1482 tt_int_op(launch, OP_EQ, 1);
1484 done:
1485 free_fake_channel(chan1);
1486 free_fake_channel(chan2);
1489 static void
1490 test_channel_listener(void *arg)
1492 int old_count;
1493 time_t now = time(NULL);
1494 channel_listener_t *chan = NULL;
1496 (void) arg;
1498 chan = tor_malloc_zero(sizeof(*chan));
1499 tt_assert(chan);
1500 channel_init_listener(chan);
1501 tt_u64_op(chan->global_identifier, OP_EQ, 1);
1502 tt_int_op(chan->timestamp_created, OP_GE, now);
1503 chan->close = test_chan_listener_close;
1505 /* Register it. At this point, it is not open so it will be put in the
1506 * finished list. */
1507 channel_listener_register(chan);
1508 tt_int_op(chan->registered, OP_EQ, 1);
1509 channel_listener_unregister(chan);
1511 /* Register it as listening now thus active. */
1512 chan->state = CHANNEL_LISTENER_STATE_LISTENING;
1513 channel_listener_register(chan);
1514 tt_int_op(chan->registered, OP_EQ, 1);
1516 /* Set the listener function. */
1517 channel_listener_set_listener_fn(chan, test_chan_listener_fn);
1518 tt_ptr_op(chan->listener, OP_EQ, test_chan_listener_fn);
1520 /* Put a channel in the listener incoming list and queue it.
1521 * function. By doing this, the listener() handler will be called. */
1522 channel_t *in_chan = new_fake_channel();
1523 old_count = test_chan_listener_fn_called;
1524 channel_listener_queue_incoming(chan, in_chan);
1525 free_fake_channel(in_chan);
1526 tt_int_op(test_chan_listener_fn_called, OP_EQ, old_count + 1);
1528 /* Put listener channel in CLOSING state. */
1529 old_count = test_chan_listener_close_fn_called;
1530 channel_listener_mark_for_close(chan);
1531 tt_int_op(test_chan_listener_close_fn_called, OP_EQ, old_count + 1);
1532 channel_listener_change_state(chan, CHANNEL_LISTENER_STATE_CLOSED);
1534 /* Dump stats so we at least hit the code path. */
1535 chan->describe_transport = test_chan_listener_describe_transport;
1536 /* There is a check for "now > timestamp_created" when dumping the stats so
1537 * make sure we go in. */
1538 chan->timestamp_created = now - 10;
1539 channel_listener_dump_statistics(chan, LOG_INFO);
1541 done:
1542 channel_free_all();
1545 struct testcase_t channel_tests[] = {
1546 { "inbound_cell", test_channel_inbound_cell, TT_FORK,
1547 NULL, NULL },
1548 { "outbound_cell", test_channel_outbound_cell, TT_FORK,
1549 NULL, NULL },
1550 { "id_map", test_channel_id_map, TT_FORK,
1551 NULL, NULL },
1552 { "lifecycle", test_channel_lifecycle, TT_FORK,
1553 NULL, NULL },
1554 { "lifecycle_2", test_channel_lifecycle_2, TT_FORK,
1555 NULL, NULL },
1556 { "dumpstats", test_channel_dumpstats, TT_FORK,
1557 NULL, NULL },
1558 { "state", test_channel_state, TT_FORK,
1559 NULL, NULL },
1560 { "duplicates", test_channel_duplicates, TT_FORK,
1561 NULL, NULL },
1562 { "get_channel_for_extend", test_channel_for_extend, TT_FORK,
1563 NULL, NULL },
1564 { "listener", test_channel_listener, TT_FORK,
1565 NULL, NULL },
1566 END_OF_TESTCASES