1 /* Copyright (c) 2013-2021, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 #define CHANNEL_OBJECT_PRIVATE
5 #define CHANNEL_FILE_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 */
18 #include "core/or/relay.h"
19 /* For channel_tls_t object and private functions. */
20 #define CHANNEL_OBJECT_PRIVATE
21 #define CHANNELTLS_PRIVATE
22 #include "core/or/channeltls.h"
23 /* For init/free stuff */
24 #include "core/or/scheduler.h"
25 #include "feature/nodelist/networkstatus.h"
27 #include "core/or/cell_st.h"
28 #include "feature/nodelist/networkstatus_st.h"
29 #include "core/or/origin_circuit_st.h"
30 #include "feature/nodelist/routerstatus_st.h"
31 #include "core/or/var_cell_st.h"
32 #include "core/or/or_connection_st.h"
33 #include "lib/net/inaddr.h"
35 /* Test suite stuff */
36 #include "test/log_test_helpers.h"
37 #include "test/test.h"
38 #include "test/fakechans.h"
40 static int test_chan_accept_cells
= 0;
41 static int test_chan_fixed_cells_recved
= 0;
42 static cell_t
* test_chan_last_seen_fixed_cell_ptr
= NULL
;
43 static int test_cells_written
= 0;
44 static int test_doesnt_want_writes_count
= 0;
45 static int test_dumpstats_calls
= 0;
46 static int test_has_waiting_cells_count
= 0;
47 static int test_releases_count
= 0;
48 static channel_t
*dump_statistics_mock_target
= NULL
;
49 static int dump_statistics_mock_matches
= 0;
50 static int test_close_called
= 0;
51 static int test_chan_should_be_canonical
= 0;
52 static int test_chan_should_match_target
= 0;
53 static int test_chan_listener_close_fn_called
= 0;
54 static int test_chan_listener_fn_called
= 0;
57 chan_test_describe_transport(channel_t
*ch
)
59 tt_ptr_op(ch
, OP_NE
, NULL
);
62 return "Fake channel for unit tests";
66 * Mock for channel_dump_statistics(); if the channel matches the
67 * target, bump a counter - otherwise ignore.
71 chan_test_channel_dump_statistics_mock(channel_t
*chan
, int severity
)
73 tt_ptr_op(chan
, OP_NE
, NULL
);
77 if (chan
!= NULL
&& chan
== dump_statistics_mock_target
) {
78 ++dump_statistics_mock_matches
;
86 * Handle an incoming fixed-size cell for unit tests
90 chan_test_cell_handler(channel_t
*chan
, cell_t
*cell
)
95 test_chan_last_seen_fixed_cell_ptr
= cell
;
96 ++test_chan_fixed_cells_recved
;
103 * Fake transport-specific stats call
107 chan_test_dumpstats(channel_t
*ch
, int severity
)
109 tt_ptr_op(ch
, OP_NE
, NULL
);
113 ++test_dumpstats_calls
;
120 chan_test_close(channel_t
*ch
)
131 * Close a channel through the error path
135 chan_test_error(channel_t
*ch
)
138 tt_assert(!(ch
->state
== CHANNEL_STATE_CLOSING
||
139 ch
->state
== CHANNEL_STATE_ERROR
||
140 ch
->state
== CHANNEL_STATE_CLOSED
));
142 channel_close_for_error(ch
);
149 * Finish closing a channel from CHANNEL_STATE_CLOSING
153 chan_test_finish_close(channel_t
*ch
)
156 tt_assert(ch
->state
== CHANNEL_STATE_CLOSING
);
165 chan_test_describe_peer(const channel_t
*ch
)
170 return "Fake channel for unit tests; no real endpoint";
174 chan_test_get_remote_addr(const channel_t
*ch
, tor_addr_t
*out
)
177 tor_addr_from_ipv4h(out
, 0x7f000001);
182 chan_test_num_cells_writeable(channel_t
*ch
)
191 chan_test_write_packed_cell(channel_t
*ch
,
192 packed_cell_t
*packed_cell
)
197 tt_assert(packed_cell
);
199 if (test_chan_accept_cells
) {
200 /* Free the cell and bump the counter */
201 ++test_cells_written
;
204 /* else return 0, we didn't accept it */
211 chan_test_write_var_cell(channel_t
*ch
, var_cell_t
*var_cell
)
218 if (test_chan_accept_cells
) {
219 /* Free the cell and bump the counter */
220 var_cell_free(var_cell
);
221 ++test_cells_written
;
224 /* else return 0, we didn't accept it */
231 * Fill out c with a new fake cell for test suite use
235 make_fake_cell(cell_t
*c
)
237 tt_ptr_op(c
, OP_NE
, NULL
);
240 c
->command
= CELL_RELAY
;
241 memset(c
->payload
, 0, CELL_PAYLOAD_SIZE
);
248 * Fill out c with a new fake var_cell for test suite use
252 make_fake_var_cell(var_cell_t
*c
)
254 tt_ptr_op(c
, OP_NE
, NULL
);
257 c
->command
= CELL_VERSIONS
;
258 c
->payload_len
= CELL_PAYLOAD_SIZE
/ 2;
259 memset(c
->payload
, 0, c
->payload_len
);
266 * Set up a new fake channel for the test suite
270 new_fake_channel(void)
272 channel_t
*chan
= tor_malloc_zero(sizeof(channel_t
));
275 chan
->close
= chan_test_close
;
276 chan
->num_cells_writeable
= chan_test_num_cells_writeable
;
277 chan
->describe_peer
= chan_test_describe_peer
;
278 chan
->get_remote_addr
= chan_test_get_remote_addr
;
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();
284 circuitmux_set_policy(chan
->cmux
, &ewma_policy
);
290 free_fake_channel(channel_t
*chan
)
296 circuitmux_free(chan
->cmux
);
302 * Counter query for scheduler_channel_has_waiting_cells_mock()
306 get_mock_scheduler_has_waiting_cells_count(void)
308 return test_has_waiting_cells_count
;
312 * Mock for scheduler_channel_has_waiting_cells()
316 scheduler_channel_has_waiting_cells_mock(channel_t
*ch
)
320 /* Increment counter */
321 ++test_has_waiting_cells_count
;
327 scheduler_channel_doesnt_want_writes_mock(channel_t
*ch
)
331 /* Increment counter */
332 ++test_doesnt_want_writes_count
;
338 * Mock for scheduler_release_channel()
342 scheduler_release_channel_mock(channel_t
*ch
)
346 /* Increment counter */
347 ++test_releases_count
;
353 test_chan_is_canonical(channel_t
*chan
)
357 if (test_chan_should_be_canonical
) {
364 test_chan_matches_target(channel_t
*chan
, const tor_addr_t
*target
)
369 if (test_chan_should_match_target
) {
376 test_chan_listener_close(channel_listener_t
*chan
)
379 ++test_chan_listener_close_fn_called
;
384 test_chan_listener_fn(channel_listener_t
*listener
, channel_t
*chan
)
389 ++test_chan_listener_fn_called
;
394 test_chan_listener_describe_transport(channel_listener_t
*chan
)
397 return "Fake listener channel.";
401 * Test for channel_dumpstats() and limited test for
402 * channel_dump_statistics()
406 test_channel_dumpstats(void *arg
)
408 channel_t
*ch
= NULL
;
410 packed_cell_t
*p_cell
= NULL
;
415 /* Mock these for duration of the test */
416 MOCK(scheduler_channel_doesnt_want_writes
,
417 scheduler_channel_doesnt_want_writes_mock
);
418 MOCK(scheduler_release_channel
,
419 scheduler_release_channel_mock
);
421 /* Set up a new fake channel */
422 ch
= new_fake_channel();
425 /* Try to register it */
426 channel_register(ch
);
427 tt_assert(ch
->registered
);
430 dump_statistics_mock_target
= ch
;
431 dump_statistics_mock_matches
= 0;
432 MOCK(channel_dump_statistics
,
433 chan_test_channel_dump_statistics_mock
);
435 /* Call channel_dumpstats() */
436 channel_dumpstats(LOG_DEBUG
);
438 /* Assert that we hit the mock */
439 tt_int_op(dump_statistics_mock_matches
, OP_EQ
, 1);
441 /* Close the channel */
442 channel_mark_for_close(ch
);
443 tt_int_op(ch
->state
, OP_EQ
, CHANNEL_STATE_CLOSING
);
444 chan_test_finish_close(ch
);
445 tt_int_op(ch
->state
, OP_EQ
, CHANNEL_STATE_CLOSED
);
447 /* Try again and hit the finished channel */
448 channel_dumpstats(LOG_DEBUG
);
449 tt_int_op(dump_statistics_mock_matches
, OP_EQ
, 2);
451 channel_run_cleanup();
454 /* Now we should hit nothing */
455 channel_dumpstats(LOG_DEBUG
);
456 tt_int_op(dump_statistics_mock_matches
, OP_EQ
, 2);
459 UNMOCK(channel_dump_statistics
);
460 dump_statistics_mock_target
= NULL
;
461 dump_statistics_mock_matches
= 0;
463 /* Now make another channel */
464 ch
= new_fake_channel();
466 channel_register(ch
);
467 tt_int_op(ch
->registered
, OP_EQ
, 1);
468 /* Lie about its age so dumpstats gets coverage for rate calculations */
469 ch
->timestamp_created
= time(NULL
) - 30;
470 tt_int_op(ch
->timestamp_created
, OP_GT
, 0);
471 tt_int_op(time(NULL
), OP_GT
, ch
->timestamp_created
);
473 /* Put cells through it both ways to make the counters non-zero */
474 p_cell
= packed_cell_new();
475 test_chan_accept_cells
= 1;
476 old_count
= test_cells_written
;
477 channel_write_packed_cell(ch
, p_cell
);
478 tt_int_op(test_cells_written
, OP_EQ
, old_count
+ 1);
479 tt_u64_op(ch
->n_bytes_xmitted
, OP_GT
, 0);
480 tt_u64_op(ch
->n_cells_xmitted
, OP_GT
, 0);
483 channel_set_cell_handlers(ch
,
484 chan_test_cell_handler
);
485 tt_ptr_op(channel_get_cell_handler(ch
), OP_EQ
, chan_test_cell_handler
);
486 cell
= tor_malloc_zero(sizeof(*cell
));
487 old_count
= test_chan_fixed_cells_recved
;
488 channel_process_cell(ch
, cell
);
489 tt_int_op(test_chan_fixed_cells_recved
, OP_EQ
, old_count
+ 1);
490 tt_u64_op(ch
->n_bytes_recved
, OP_GT
, 0);
491 tt_u64_op(ch
->n_cells_recved
, OP_GT
, 0);
493 /* Test channel_dump_statistics */
494 ch
->describe_transport
= chan_test_describe_transport
;
495 ch
->dumpstats
= chan_test_dumpstats
;
496 test_chan_should_be_canonical
= 1;
497 ch
->is_canonical
= test_chan_is_canonical
;
498 old_count
= test_dumpstats_calls
;
499 channel_dump_statistics(ch
, LOG_DEBUG
);
500 tt_int_op(test_dumpstats_calls
, OP_EQ
, old_count
+ 1);
502 /* Close the channel */
503 channel_mark_for_close(ch
);
504 tt_int_op(ch
->state
, OP_EQ
, CHANNEL_STATE_CLOSING
);
505 chan_test_finish_close(ch
);
506 tt_int_op(ch
->state
, OP_EQ
, CHANNEL_STATE_CLOSED
);
507 channel_run_cleanup();
511 free_fake_channel(ch
);
514 UNMOCK(scheduler_channel_doesnt_want_writes
);
515 UNMOCK(scheduler_release_channel
);
520 /* Test outbound cell. The callstack is:
521 * channel_flush_some_cells()
522 * -> channel_flush_from_first_active_circuit()
523 * -> channel_write_packed_cell()
524 * -> write_packed_cell()
525 * -> chan->write_packed_cell() fct ptr.
527 * This test goes from a cell in a circuit up to the channel write handler
528 * that should put them on the connection outbuf. */
530 test_channel_outbound_cell(void *arg
)
533 channel_t
*chan
= NULL
;
534 packed_cell_t
*p_cell
= NULL
, *p_cell2
= NULL
;
535 origin_circuit_t
*circ
= NULL
;
540 /* Set the test time to be mocked, since this test assumes that no
541 * time will pass, ewma values will not need to be re-scaled, and so on */
542 monotime_enable_test_mocking();
543 monotime_set_mock_time_nsec(UINT64_C(1000000000) * 12345);
545 cmux_ewma_set_options(NULL
,NULL
);
547 /* The channel will be freed so we need to hijack this so the scheduler
548 * doesn't get confused. */
549 MOCK(scheduler_release_channel
, scheduler_release_channel_mock
);
551 /* Accept cells to lower layer */
552 test_chan_accept_cells
= 1;
554 /* Setup a valid circuit to queue a cell. */
555 circ
= origin_circuit_new();
557 /* Circuit needs an origin purpose to be considered origin. */
558 TO_CIRCUIT(circ
)->purpose
= CIRCUIT_PURPOSE_C_GENERAL
;
559 TO_CIRCUIT(circ
)->n_circ_id
= 42;
560 /* This is the outbound test so use the next channel queue. */
561 queue
= &TO_CIRCUIT(circ
)->n_chan_cells
;
562 /* Setup packed cell to queue on the circuit. */
563 p_cell
= packed_cell_new();
565 p_cell2
= packed_cell_new();
567 /* Setup a channel to put the circuit on. */
568 chan
= new_fake_channel();
570 chan
->state
= CHANNEL_STATE_OPENING
;
571 channel_change_state_open(chan
);
572 /* Outbound channel. */
573 channel_mark_outgoing(chan
);
574 /* Try to register it so we can clean it through the channel cleanup
576 channel_register(chan
);
577 tt_int_op(chan
->registered
, OP_EQ
, 1);
578 /* Set EWMA policy so we can pick it when flushing. */
579 circuitmux_set_policy(chan
->cmux
, &ewma_policy
);
580 tt_ptr_op(circuitmux_get_policy(chan
->cmux
), OP_EQ
, &ewma_policy
);
582 /* Register circuit to the channel circid map which will attach the circuit
583 * to the channel's cmux as well. */
584 circuit_set_n_circid_chan(TO_CIRCUIT(circ
), 42, chan
);
585 tt_int_op(channel_num_circuits(chan
), OP_EQ
, 1);
586 /* Test the cmux state. */
587 tt_int_op(circuitmux_is_circuit_attached(chan
->cmux
, TO_CIRCUIT(circ
)),
590 /* Flush the channel without any cell on it. */
591 old_count
= test_cells_written
;
592 ssize_t flushed
= channel_flush_some_cells(chan
, 1);
593 tt_i64_op(flushed
, OP_EQ
, 0);
594 tt_int_op(test_cells_written
, OP_EQ
, old_count
);
595 tt_int_op(channel_more_to_flush(chan
), OP_EQ
, 0);
596 tt_int_op(circuitmux_num_active_circuits(chan
->cmux
), OP_EQ
, 0);
597 tt_int_op(circuitmux_num_cells(chan
->cmux
), OP_EQ
, 0);
598 tt_int_op(circuitmux_is_circuit_active(chan
->cmux
, TO_CIRCUIT(circ
)),
600 tt_u64_op(chan
->n_cells_xmitted
, OP_EQ
, 0);
601 tt_u64_op(chan
->n_bytes_xmitted
, OP_EQ
, 0);
603 /* Queue cell onto the next queue that is the outbound direction. Than
604 * update its cmux so the circuit can be picked when flushing cells. */
605 cell_queue_append(queue
, p_cell
);
607 tt_int_op(queue
->n
, OP_EQ
, 1);
608 cell_queue_append(queue
, p_cell2
);
610 tt_int_op(queue
->n
, OP_EQ
, 2);
612 update_circuit_on_cmux(TO_CIRCUIT(circ
), CELL_DIRECTION_OUT
);
613 tt_int_op(circuitmux_num_active_circuits(chan
->cmux
), OP_EQ
, 1);
614 tt_int_op(circuitmux_num_cells(chan
->cmux
), OP_EQ
, 2);
615 tt_int_op(circuitmux_is_circuit_active(chan
->cmux
, TO_CIRCUIT(circ
)),
618 /* From this point on, we have a queued cell on an active circuit attached
619 * to the channel's cmux. */
621 /* Flush the first cell. This is going to go down the call stack. */
622 old_count
= test_cells_written
;
623 flushed
= channel_flush_some_cells(chan
, 1);
624 tt_i64_op(flushed
, OP_EQ
, 1);
625 tt_int_op(test_cells_written
, OP_EQ
, old_count
+ 1);
626 tt_int_op(circuitmux_num_cells(chan
->cmux
), OP_EQ
, 1);
627 tt_int_op(channel_more_to_flush(chan
), OP_EQ
, 1);
628 /* Circuit should remain active because there is a second cell queued. */
629 tt_int_op(circuitmux_is_circuit_active(chan
->cmux
, TO_CIRCUIT(circ
)),
631 /* Should still be attached. */
632 tt_int_op(circuitmux_is_circuit_attached(chan
->cmux
, TO_CIRCUIT(circ
)),
634 tt_u64_op(chan
->n_cells_xmitted
, OP_EQ
, 1);
635 tt_u64_op(chan
->n_bytes_xmitted
, OP_EQ
, get_cell_network_size(0));
637 /* Flush second cell. This is going to go down the call stack. */
638 old_count
= test_cells_written
;
639 flushed
= channel_flush_some_cells(chan
, 1);
640 tt_i64_op(flushed
, OP_EQ
, 1);
641 tt_int_op(test_cells_written
, OP_EQ
, old_count
+ 1);
642 tt_int_op(circuitmux_num_cells(chan
->cmux
), OP_EQ
, 0);
643 tt_int_op(channel_more_to_flush(chan
), OP_EQ
, 0);
644 /* No more cells should make the circuit inactive. */
645 tt_int_op(circuitmux_is_circuit_active(chan
->cmux
, TO_CIRCUIT(circ
)),
647 /* Should still be attached. */
648 tt_int_op(circuitmux_is_circuit_attached(chan
->cmux
, TO_CIRCUIT(circ
)),
650 tt_u64_op(chan
->n_cells_xmitted
, OP_EQ
, 2);
651 tt_u64_op(chan
->n_bytes_xmitted
, OP_EQ
, get_cell_network_size(0) * 2);
655 circuit_free_(TO_CIRCUIT(circ
));
659 UNMOCK(scheduler_release_channel
);
660 monotime_disable_test_mocking();
663 /* Test inbound cell. The callstack is:
664 * channel_process_cell()
665 * -> chan->cell_handler()
667 * This test is about checking if we can process an inbound cell down to the
668 * channel handler. */
670 test_channel_inbound_cell(void *arg
)
672 channel_t
*chan
= NULL
;
678 /* The channel will be freed so we need to hijack this so the scheduler
679 * doesn't get confused. */
680 MOCK(scheduler_release_channel
, scheduler_release_channel_mock
);
682 /* Accept cells to lower layer */
683 test_chan_accept_cells
= 1;
685 chan
= new_fake_channel();
687 /* Start it off in OPENING */
688 chan
->state
= CHANNEL_STATE_OPENING
;
690 /* Try to register it */
691 channel_register(chan
);
692 tt_int_op(chan
->registered
, OP_EQ
, 1);
695 channel_change_state_open(chan
);
696 tt_int_op(chan
->state
, OP_EQ
, CHANNEL_STATE_OPEN
);
697 tt_int_op(chan
->has_been_open
, OP_EQ
, 1);
699 /* Receive a cell now. */
700 cell
= tor_malloc_zero(sizeof(*cell
));
701 make_fake_cell(cell
);
702 old_count
= test_chan_fixed_cells_recved
;
703 channel_process_cell(chan
, cell
);
704 tt_int_op(test_chan_fixed_cells_recved
, OP_EQ
, old_count
);
705 tt_assert(monotime_coarse_is_zero(&chan
->timestamp_xfer
));
706 tt_u64_op(chan
->timestamp_active
, OP_EQ
, 0);
707 tt_u64_op(chan
->timestamp_recv
, OP_EQ
, 0);
709 /* Setup incoming cell handlers. We don't care about var cell, the channel
710 * layers is not handling those. */
711 channel_set_cell_handlers(chan
, chan_test_cell_handler
);
712 tt_ptr_op(chan
->cell_handler
, OP_EQ
, chan_test_cell_handler
);
713 /* Now process the cell, we should see it. */
714 old_count
= test_chan_fixed_cells_recved
;
715 channel_process_cell(chan
, cell
);
716 tt_int_op(test_chan_fixed_cells_recved
, OP_EQ
, old_count
+ 1);
717 /* We should have a series of timestamp set. */
718 tt_assert(!monotime_coarse_is_zero(&chan
->timestamp_xfer
));
719 tt_u64_op(chan
->timestamp_active
, OP_NE
, 0);
720 tt_u64_op(chan
->timestamp_recv
, OP_NE
, 0);
721 tt_assert(monotime_coarse_is_zero(&chan
->next_padding_time
));
722 tt_u64_op(chan
->n_cells_recved
, OP_EQ
, 1);
723 tt_u64_op(chan
->n_bytes_recved
, OP_EQ
, get_cell_network_size(0));
726 old_count
= test_close_called
;
727 channel_mark_for_close(chan
);
728 tt_int_op(chan
->state
, OP_EQ
, CHANNEL_STATE_CLOSING
);
729 tt_int_op(chan
->reason_for_closing
, OP_EQ
, CHANNEL_CLOSE_REQUESTED
);
730 tt_int_op(test_close_called
, OP_EQ
, old_count
+ 1);
732 /* This closes the channel so it calls in the scheduler, make sure of it. */
733 old_count
= test_releases_count
;
734 chan_test_finish_close(chan
);
735 tt_int_op(test_releases_count
, OP_EQ
, old_count
+ 1);
736 tt_int_op(chan
->state
, OP_EQ
, CHANNEL_STATE_CLOSED
);
738 /* The channel will be free, lets make sure it is not accessible. */
739 uint64_t chan_id
= chan
->global_identifier
;
740 tt_ptr_op(channel_find_by_global_id(chan_id
), OP_EQ
, chan
);
741 channel_run_cleanup();
742 chan
= channel_find_by_global_id(chan_id
);
743 tt_assert(chan
== NULL
);
747 UNMOCK(scheduler_release_channel
);
751 * Normal channel lifecycle test:
753 * OPENING->OPEN->MAINT->OPEN->CLOSING->CLOSED
757 test_channel_lifecycle(void *arg
)
759 channel_t
*ch1
= NULL
, *ch2
= NULL
;
760 packed_cell_t
*p_cell
= NULL
;
761 int old_count
, init_doesnt_want_writes_count
;
762 int init_releases_count
;
766 /* Mock these for the whole lifecycle test */
767 MOCK(scheduler_channel_doesnt_want_writes
,
768 scheduler_channel_doesnt_want_writes_mock
);
769 MOCK(scheduler_release_channel
,
770 scheduler_release_channel_mock
);
772 /* Cache some initial counter values */
773 init_doesnt_want_writes_count
= test_doesnt_want_writes_count
;
774 init_releases_count
= test_releases_count
;
776 /* Accept cells to lower layer */
777 test_chan_accept_cells
= 1;
779 ch1
= new_fake_channel();
781 /* Start it off in OPENING */
782 ch1
->state
= CHANNEL_STATE_OPENING
;
784 /* Try to register it */
785 channel_register(ch1
);
786 tt_assert(ch1
->registered
);
788 /* Try to write a cell through (should queue) */
789 p_cell
= packed_cell_new();
790 old_count
= test_cells_written
;
791 channel_write_packed_cell(ch1
, p_cell
);
792 tt_int_op(old_count
, OP_EQ
, test_cells_written
);
794 /* Move it to OPEN and flush */
795 channel_change_state_open(ch1
);
797 /* Get another one */
798 ch2
= new_fake_channel();
800 ch2
->state
= CHANNEL_STATE_OPENING
;
803 channel_register(ch2
);
804 tt_assert(ch2
->registered
);
807 tt_int_op(test_doesnt_want_writes_count
, OP_EQ
,
808 init_doesnt_want_writes_count
);
809 tt_int_op(test_releases_count
, OP_EQ
, init_releases_count
);
811 /* Move ch1 to MAINT */
812 channel_change_state(ch1
, CHANNEL_STATE_MAINT
);
813 tt_int_op(test_doesnt_want_writes_count
, OP_EQ
,
814 init_doesnt_want_writes_count
+ 1);
815 tt_int_op(test_releases_count
, OP_EQ
, init_releases_count
);
817 /* Move ch2 to OPEN */
818 channel_change_state_open(ch2
);
819 tt_int_op(test_doesnt_want_writes_count
, OP_EQ
,
820 init_doesnt_want_writes_count
+ 1);
821 tt_int_op(test_releases_count
, OP_EQ
, init_releases_count
);
823 /* Move ch1 back to OPEN */
824 channel_change_state_open(ch1
);
825 tt_int_op(test_doesnt_want_writes_count
, OP_EQ
,
826 init_doesnt_want_writes_count
+ 1);
827 tt_int_op(test_releases_count
, OP_EQ
, init_releases_count
);
829 /* Mark ch2 for close */
830 channel_mark_for_close(ch2
);
831 tt_int_op(ch2
->state
, OP_EQ
, CHANNEL_STATE_CLOSING
);
832 tt_int_op(test_doesnt_want_writes_count
, OP_EQ
,
833 init_doesnt_want_writes_count
+ 1);
834 tt_int_op(test_releases_count
, OP_EQ
, init_releases_count
+ 1);
836 /* Shut down channels */
839 tt_int_op(test_doesnt_want_writes_count
, OP_EQ
,
840 init_doesnt_want_writes_count
+ 1);
841 /* channel_free() calls scheduler_release_channel() */
842 tt_int_op(test_releases_count
, OP_EQ
, init_releases_count
+ 4);
845 free_fake_channel(ch1
);
846 free_fake_channel(ch2
);
848 UNMOCK(scheduler_channel_doesnt_want_writes
);
849 UNMOCK(scheduler_release_channel
);
853 * Weird channel lifecycle test:
855 * OPENING->CLOSING->CLOSED
856 * OPENING->OPEN->CLOSING->ERROR
857 * OPENING->OPEN->MAINT->CLOSING->CLOSED
858 * OPENING->OPEN->MAINT->CLOSING->ERROR
862 test_channel_lifecycle_2(void *arg
)
864 channel_t
*ch
= NULL
;
868 /* Mock these for the whole lifecycle test */
869 MOCK(scheduler_channel_doesnt_want_writes
,
870 scheduler_channel_doesnt_want_writes_mock
);
871 MOCK(scheduler_release_channel
,
872 scheduler_release_channel_mock
);
874 /* Accept cells to lower layer */
875 test_chan_accept_cells
= 1;
877 ch
= new_fake_channel();
879 /* Start it off in OPENING */
880 ch
->state
= CHANNEL_STATE_OPENING
;
882 /* Try to register it */
883 channel_register(ch
);
884 tt_assert(ch
->registered
);
886 /* Try to close it */
887 channel_mark_for_close(ch
);
888 tt_int_op(ch
->state
, OP_EQ
, CHANNEL_STATE_CLOSING
);
890 /* Finish closing it */
891 chan_test_finish_close(ch
);
892 tt_int_op(ch
->state
, OP_EQ
, CHANNEL_STATE_CLOSED
);
893 channel_run_cleanup();
896 /* Now try OPENING->OPEN->CLOSING->ERROR */
897 ch
= new_fake_channel();
899 ch
->state
= CHANNEL_STATE_OPENING
;
900 channel_register(ch
);
901 tt_assert(ch
->registered
);
903 /* Finish opening it */
904 channel_change_state_open(ch
);
906 /* Error exit from lower layer */
908 tt_int_op(ch
->state
, OP_EQ
, CHANNEL_STATE_CLOSING
);
909 chan_test_finish_close(ch
);
910 tt_int_op(ch
->state
, OP_EQ
, CHANNEL_STATE_ERROR
);
911 channel_run_cleanup();
914 /* OPENING->OPEN->MAINT->CLOSING->CLOSED close from maintenance state */
915 ch
= new_fake_channel();
917 ch
->state
= CHANNEL_STATE_OPENING
;
918 channel_register(ch
);
919 tt_assert(ch
->registered
);
921 /* Finish opening it */
922 channel_change_state_open(ch
);
923 tt_int_op(ch
->state
, OP_EQ
, CHANNEL_STATE_OPEN
);
925 /* Go to maintenance state */
926 channel_change_state(ch
, CHANNEL_STATE_MAINT
);
927 tt_int_op(ch
->state
, OP_EQ
, CHANNEL_STATE_MAINT
);
929 /* Lower layer close */
930 channel_mark_for_close(ch
);
931 tt_int_op(ch
->state
, OP_EQ
, CHANNEL_STATE_CLOSING
);
934 chan_test_finish_close(ch
);
935 tt_int_op(ch
->state
, OP_EQ
, CHANNEL_STATE_CLOSED
);
936 channel_run_cleanup();
940 * OPENING->OPEN->MAINT->CLOSING->CLOSED lower-layer close during
943 ch
= new_fake_channel();
945 ch
->state
= CHANNEL_STATE_OPENING
;
946 channel_register(ch
);
947 tt_assert(ch
->registered
);
949 /* Finish opening it */
950 channel_change_state_open(ch
);
951 tt_int_op(ch
->state
, OP_EQ
, CHANNEL_STATE_OPEN
);
953 /* Go to maintenance state */
954 channel_change_state(ch
, CHANNEL_STATE_MAINT
);
955 tt_int_op(ch
->state
, OP_EQ
, CHANNEL_STATE_MAINT
);
957 /* Lower layer close */
958 channel_close_from_lower_layer(ch
);
959 tt_int_op(ch
->state
, OP_EQ
, CHANNEL_STATE_CLOSING
);
962 chan_test_finish_close(ch
);
963 tt_int_op(ch
->state
, OP_EQ
, CHANNEL_STATE_CLOSED
);
964 channel_run_cleanup();
967 /* OPENING->OPEN->MAINT->CLOSING->ERROR */
968 ch
= new_fake_channel();
970 ch
->state
= CHANNEL_STATE_OPENING
;
971 channel_register(ch
);
972 tt_assert(ch
->registered
);
974 /* Finish opening it */
975 channel_change_state_open(ch
);
976 tt_int_op(ch
->state
, OP_EQ
, CHANNEL_STATE_OPEN
);
978 /* Go to maintenance state */
979 channel_change_state(ch
, CHANNEL_STATE_MAINT
);
980 tt_int_op(ch
->state
, OP_EQ
, CHANNEL_STATE_MAINT
);
982 /* Lower layer close */
984 tt_int_op(ch
->state
, OP_EQ
, CHANNEL_STATE_CLOSING
);
987 chan_test_finish_close(ch
);
988 tt_int_op(ch
->state
, OP_EQ
, CHANNEL_STATE_ERROR
);
989 channel_run_cleanup();
992 /* Shut down channels */
998 UNMOCK(scheduler_channel_doesnt_want_writes
);
999 UNMOCK(scheduler_release_channel
);
1005 test_channel_id_map(void *arg
)
1009 char rsa_id
[N_CHAN
][DIGEST_LEN
];
1010 ed25519_public_key_t
*ed_id
[N_CHAN
];
1011 channel_t
*chan
[N_CHAN
];
1013 ed25519_public_key_t ed_zero
;
1014 memset(&ed_zero
, 0, sizeof(ed_zero
));
1016 tt_int_op(DIGEST_LEN
, OP_EQ
, sizeof(rsa_id
[0])); // Do I remember C?
1018 for (i
= 0; i
< N_CHAN
; ++i
) {
1019 crypto_rand(rsa_id
[i
], DIGEST_LEN
);
1020 ed_id
[i
] = tor_malloc_zero(sizeof(*ed_id
[i
]));
1021 crypto_rand((char*)ed_id
[i
]->pubkey
, sizeof(ed_id
[i
]->pubkey
));
1024 /* For channel 3, have no Ed identity. */
1027 /* Channel 2 and 4 have same ROSA identity */
1028 memcpy(rsa_id
[4], rsa_id
[2], DIGEST_LEN
);
1030 /* Channel 2 and 4 and 5 have same RSA identity */
1031 memcpy(rsa_id
[4], rsa_id
[2], DIGEST_LEN
);
1032 memcpy(rsa_id
[5], rsa_id
[2], DIGEST_LEN
);
1034 /* Channels 2 and 5 have same Ed25519 identity */
1035 memcpy(ed_id
[5], ed_id
[2], sizeof(*ed_id
[2]));
1037 for (i
= 0; i
< N_CHAN
; ++i
) {
1038 chan
[i
] = new_fake_channel();
1039 channel_register(chan
[i
]);
1040 channel_set_identity_digest(chan
[i
], rsa_id
[i
], ed_id
[i
]);
1043 /* Lookup by RSA id only */
1044 tt_ptr_op(chan
[0], OP_EQ
,
1045 channel_find_by_remote_identity(rsa_id
[0], NULL
));
1046 tt_ptr_op(chan
[1], OP_EQ
,
1047 channel_find_by_remote_identity(rsa_id
[1], NULL
));
1048 tt_ptr_op(chan
[3], OP_EQ
,
1049 channel_find_by_remote_identity(rsa_id
[3], NULL
));
1051 ch
= channel_find_by_remote_identity(rsa_id
[2], NULL
);
1052 tt_assert(ch
== chan
[2] || ch
== chan
[4] || ch
== chan
[5]);
1053 ch
= channel_next_with_rsa_identity(ch
);
1054 tt_assert(ch
== chan
[2] || ch
== chan
[4] || ch
== chan
[5]);
1055 ch
= channel_next_with_rsa_identity(ch
);
1056 tt_assert(ch
== chan
[2] || ch
== chan
[4] || ch
== chan
[5]);
1057 ch
= channel_next_with_rsa_identity(ch
);
1058 tt_ptr_op(ch
, OP_EQ
, NULL
);
1060 /* As above, but with zero Ed25519 ID (meaning "any ID") */
1061 tt_ptr_op(chan
[0], OP_EQ
,
1062 channel_find_by_remote_identity(rsa_id
[0], &ed_zero
));
1063 tt_ptr_op(chan
[1], OP_EQ
,
1064 channel_find_by_remote_identity(rsa_id
[1], &ed_zero
));
1065 tt_ptr_op(chan
[3], OP_EQ
,
1066 channel_find_by_remote_identity(rsa_id
[3], &ed_zero
));
1067 ch
= channel_find_by_remote_identity(rsa_id
[2], &ed_zero
);
1068 tt_assert(ch
== chan
[2] || ch
== chan
[4] || ch
== chan
[5]);
1069 ch
= channel_next_with_rsa_identity(ch
);
1070 tt_assert(ch
== chan
[2] || ch
== chan
[4] || ch
== chan
[5]);
1071 ch
= channel_next_with_rsa_identity(ch
);
1072 tt_assert(ch
== chan
[2] || ch
== chan
[4] || ch
== chan
[5]);
1073 ch
= channel_next_with_rsa_identity(ch
);
1074 tt_ptr_op(ch
, OP_EQ
, NULL
);
1076 /* Lookup nonexistent RSA identity */
1077 tt_ptr_op(NULL
, OP_EQ
,
1078 channel_find_by_remote_identity("!!!!!!!!!!!!!!!!!!!!", NULL
));
1080 /* Look up by full identity pair */
1081 tt_ptr_op(chan
[0], OP_EQ
,
1082 channel_find_by_remote_identity(rsa_id
[0], ed_id
[0]));
1083 tt_ptr_op(chan
[1], OP_EQ
,
1084 channel_find_by_remote_identity(rsa_id
[1], ed_id
[1]));
1085 tt_ptr_op(chan
[3], OP_EQ
,
1086 channel_find_by_remote_identity(rsa_id
[3], ed_id
[3] /*NULL*/));
1087 tt_ptr_op(chan
[4], OP_EQ
,
1088 channel_find_by_remote_identity(rsa_id
[4], ed_id
[4]));
1089 ch
= channel_find_by_remote_identity(rsa_id
[2], ed_id
[2]);
1090 tt_assert(ch
== chan
[2] || ch
== chan
[5]);
1092 /* Look up RSA identity with wrong ed25519 identity */
1093 tt_ptr_op(NULL
, OP_EQ
,
1094 channel_find_by_remote_identity(rsa_id
[4], ed_id
[0]));
1095 tt_ptr_op(NULL
, OP_EQ
,
1096 channel_find_by_remote_identity(rsa_id
[2], ed_id
[1]));
1097 tt_ptr_op(NULL
, OP_EQ
,
1098 channel_find_by_remote_identity(rsa_id
[3], ed_id
[1]));
1101 for (i
= 0; i
< N_CHAN
; ++i
) {
1102 channel_clear_identity_digest(chan
[i
]);
1103 channel_unregister(chan
[i
]);
1104 free_fake_channel(chan
[i
]);
1111 test_channel_state(void *arg
)
1115 /* Test state validity. */
1116 tt_int_op(channel_state_is_valid(CHANNEL_STATE_CLOSED
), OP_EQ
, 1);
1117 tt_int_op(channel_state_is_valid(CHANNEL_STATE_CLOSING
), OP_EQ
, 1);
1118 tt_int_op(channel_state_is_valid(CHANNEL_STATE_ERROR
), OP_EQ
, 1);
1119 tt_int_op(channel_state_is_valid(CHANNEL_STATE_OPEN
), OP_EQ
, 1);
1120 tt_int_op(channel_state_is_valid(CHANNEL_STATE_OPENING
), OP_EQ
, 1);
1121 tt_int_op(channel_state_is_valid(CHANNEL_STATE_MAINT
), OP_EQ
, 1);
1122 tt_int_op(channel_state_is_valid(CHANNEL_STATE_LAST
), OP_EQ
, 0);
1123 tt_int_op(channel_state_is_valid(INT_MAX
), OP_EQ
, 0);
1125 /* Test listener state validity. */
1126 tt_int_op(channel_listener_state_is_valid(CHANNEL_LISTENER_STATE_CLOSED
),
1128 tt_int_op(channel_listener_state_is_valid(CHANNEL_LISTENER_STATE_LISTENING
),
1130 tt_int_op(channel_listener_state_is_valid(CHANNEL_LISTENER_STATE_CLOSING
),
1132 tt_int_op(channel_listener_state_is_valid(CHANNEL_LISTENER_STATE_ERROR
),
1134 tt_int_op(channel_listener_state_is_valid(CHANNEL_LISTENER_STATE_LAST
),
1136 tt_int_op(channel_listener_state_is_valid(INT_MAX
), OP_EQ
, 0);
1138 /* Test state transition. */
1139 tt_int_op(channel_state_can_transition(CHANNEL_STATE_CLOSED
,
1140 CHANNEL_STATE_OPENING
), OP_EQ
, 1);
1141 tt_int_op(channel_state_can_transition(CHANNEL_STATE_CLOSED
,
1142 CHANNEL_STATE_ERROR
), OP_EQ
, 0);
1143 tt_int_op(channel_state_can_transition(CHANNEL_STATE_CLOSING
,
1144 CHANNEL_STATE_ERROR
), OP_EQ
, 1);
1145 tt_int_op(channel_state_can_transition(CHANNEL_STATE_CLOSING
,
1146 CHANNEL_STATE_CLOSED
), OP_EQ
, 1);
1147 tt_int_op(channel_state_can_transition(CHANNEL_STATE_CLOSING
,
1148 CHANNEL_STATE_OPEN
), OP_EQ
, 0);
1149 tt_int_op(channel_state_can_transition(CHANNEL_STATE_MAINT
,
1150 CHANNEL_STATE_CLOSING
), OP_EQ
, 1);
1151 tt_int_op(channel_state_can_transition(CHANNEL_STATE_MAINT
,
1152 CHANNEL_STATE_ERROR
), OP_EQ
, 1);
1153 tt_int_op(channel_state_can_transition(CHANNEL_STATE_MAINT
,
1154 CHANNEL_STATE_OPEN
), OP_EQ
, 1);
1155 tt_int_op(channel_state_can_transition(CHANNEL_STATE_MAINT
,
1156 CHANNEL_STATE_OPENING
), OP_EQ
, 0);
1157 tt_int_op(channel_state_can_transition(CHANNEL_STATE_OPENING
,
1158 CHANNEL_STATE_OPEN
), OP_EQ
, 1);
1159 tt_int_op(channel_state_can_transition(CHANNEL_STATE_OPENING
,
1160 CHANNEL_STATE_CLOSING
), OP_EQ
, 1);
1161 tt_int_op(channel_state_can_transition(CHANNEL_STATE_OPENING
,
1162 CHANNEL_STATE_ERROR
), OP_EQ
, 1);
1163 tt_int_op(channel_state_can_transition(CHANNEL_STATE_OPEN
,
1164 CHANNEL_STATE_ERROR
), OP_EQ
, 1);
1165 tt_int_op(channel_state_can_transition(CHANNEL_STATE_OPEN
,
1166 CHANNEL_STATE_CLOSING
), OP_EQ
, 1);
1167 tt_int_op(channel_state_can_transition(CHANNEL_STATE_OPEN
,
1168 CHANNEL_STATE_ERROR
), OP_EQ
, 1);
1169 tt_int_op(channel_state_can_transition(CHANNEL_STATE_OPEN
,
1170 CHANNEL_STATE_MAINT
), OP_EQ
, 1);
1171 tt_int_op(channel_state_can_transition(CHANNEL_STATE_LAST
,
1172 CHANNEL_STATE_MAINT
), OP_EQ
, 0);
1173 tt_int_op(channel_state_can_transition(CHANNEL_STATE_LAST
, INT_MAX
),
1176 /* Test listener state transition. */
1177 tt_int_op(channel_listener_state_can_transition(
1178 CHANNEL_LISTENER_STATE_CLOSED
,
1179 CHANNEL_LISTENER_STATE_LISTENING
),
1181 tt_int_op(channel_listener_state_can_transition(
1182 CHANNEL_LISTENER_STATE_CLOSED
,
1183 CHANNEL_LISTENER_STATE_ERROR
),
1186 tt_int_op(channel_listener_state_can_transition(
1187 CHANNEL_LISTENER_STATE_CLOSING
,
1188 CHANNEL_LISTENER_STATE_CLOSED
),
1191 tt_int_op(channel_listener_state_can_transition(
1192 CHANNEL_LISTENER_STATE_CLOSING
,
1193 CHANNEL_LISTENER_STATE_ERROR
),
1195 tt_int_op(channel_listener_state_can_transition(
1196 CHANNEL_LISTENER_STATE_ERROR
,
1197 CHANNEL_LISTENER_STATE_CLOSING
),
1200 tt_int_op(channel_listener_state_can_transition(
1201 CHANNEL_LISTENER_STATE_LISTENING
,
1202 CHANNEL_LISTENER_STATE_CLOSING
),
1204 tt_int_op(channel_listener_state_can_transition(
1205 CHANNEL_LISTENER_STATE_LISTENING
,
1206 CHANNEL_LISTENER_STATE_ERROR
),
1208 tt_int_op(channel_listener_state_can_transition(
1209 CHANNEL_LISTENER_STATE_LAST
,
1213 /* Test state string. */
1214 tt_str_op(channel_state_to_string(CHANNEL_STATE_CLOSING
), OP_EQ
,
1216 tt_str_op(channel_state_to_string(CHANNEL_STATE_ERROR
), OP_EQ
,
1218 tt_str_op(channel_state_to_string(CHANNEL_STATE_CLOSED
), OP_EQ
,
1220 tt_str_op(channel_state_to_string(CHANNEL_STATE_OPEN
), OP_EQ
,
1222 tt_str_op(channel_state_to_string(CHANNEL_STATE_OPENING
), OP_EQ
,
1224 tt_str_op(channel_state_to_string(CHANNEL_STATE_MAINT
), OP_EQ
,
1225 "temporarily suspended for maintenance");
1226 tt_str_op(channel_state_to_string(CHANNEL_STATE_LAST
), OP_EQ
,
1227 "unknown or invalid channel state");
1228 tt_str_op(channel_state_to_string(INT_MAX
), OP_EQ
,
1229 "unknown or invalid channel state");
1231 /* Test listener state string. */
1232 tt_str_op(channel_listener_state_to_string(CHANNEL_LISTENER_STATE_CLOSING
),
1234 tt_str_op(channel_listener_state_to_string(CHANNEL_LISTENER_STATE_ERROR
),
1235 OP_EQ
, "channel listener error");
1236 tt_str_op(channel_listener_state_to_string(CHANNEL_LISTENER_STATE_LISTENING
),
1237 OP_EQ
, "listening");
1238 tt_str_op(channel_listener_state_to_string(CHANNEL_LISTENER_STATE_LAST
),
1239 OP_EQ
, "unknown or invalid channel listener state");
1240 tt_str_op(channel_listener_state_to_string(INT_MAX
),
1241 OP_EQ
, "unknown or invalid channel listener state");
1247 static networkstatus_t
*mock_ns
= NULL
;
1249 static networkstatus_t
*
1250 mock_networkstatus_get_latest_consensus(void)
1256 test_channel_duplicates(void *arg
)
1258 channel_t
*chan
= NULL
;
1263 setup_full_capture_of_logs(LOG_INFO
);
1264 /* Try a flat call with channel nor connections. */
1265 channel_check_for_duplicates();
1266 expect_log_msg_containing(
1267 "Found 0 connections to authorities, 0 connections to 0 relays. "
1268 "Found 0 current canonical connections, "
1269 "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();
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
),
1286 /* No relay has been associated with this channel. */
1287 channel_check_for_duplicates();
1288 expect_log_msg_containing(
1289 "Found 0 connections to authorities, 0 connections to 0 relays. "
1290 "Found 0 current canonical connections, "
1291 "in 0 of which we were a non-canonical peer. "
1292 "0 relays had more than 1 connection, 0 had more than 2, and "
1293 "0 had more than 4 connections.");
1295 /* Associate relay to this connection in the consensus. */
1296 memset(&rs
, 0, sizeof(rs
));
1297 memset(rs
.identity_digest
, 'A', sizeof(rs
.identity_digest
));
1298 smartlist_add(mock_ns
->routerstatus_list
, &rs
);
1300 /* Non opened channel. */
1301 chan
->state
= CHANNEL_STATE_CLOSING
;
1302 channel_check_for_duplicates();
1303 expect_log_msg_containing(
1304 "Found 0 connections to authorities, 0 connections to 0 relays. "
1305 "Found 0 current canonical connections, "
1306 "in 0 of which we were a non-canonical peer. "
1307 "0 relays had more than 1 connection, 0 had more than 2, and "
1308 "0 had more than 4 connections.");
1309 chan
->state
= CHANNEL_STATE_OPEN
;
1311 channel_check_for_duplicates();
1312 expect_log_msg_containing(
1313 "Found 0 connections to authorities, 1 connections to 1 relays. "
1314 "Found 0 current canonical connections, "
1315 "in 0 of which we were a non-canonical peer. "
1316 "0 relays had more than 1 connection, 0 had more than 2, and "
1317 "0 had more than 4 connections.");
1319 test_chan_should_be_canonical
= 1;
1320 channel_check_for_duplicates();
1321 expect_log_msg_containing(
1322 "Found 0 connections to authorities, 1 connections to 1 relays. "
1323 "Found 1 current canonical connections, "
1324 "in 1 of which we were a non-canonical peer. "
1325 "0 relays had more than 1 connection, 0 had more than 2, and "
1326 "0 had more than 4 connections.");
1327 teardown_capture_of_logs();
1330 free_fake_channel(chan
);
1331 smartlist_clear(mock_ns
->routerstatus_list
);
1332 networkstatus_vote_free(mock_ns
);
1333 UNMOCK(networkstatus_get_latest_consensus
);
1337 test_channel_for_extend(void *arg
)
1339 channel_t
*chan1
= NULL
, *chan2
= NULL
;
1340 channel_t
*ret_chan
= NULL
;
1341 char digest
[DIGEST_LEN
];
1342 ed25519_public_key_t ed_id
;
1343 tor_addr_t ipv4_addr
, ipv6_addr
;
1346 time_t now
= time(NULL
);
1350 memset(digest
, 'A', sizeof(digest
));
1351 memset(&ed_id
, 'B', sizeof(ed_id
));
1353 tor_addr_make_null(&ipv4_addr
, AF_INET
);
1354 tor_addr_make_null(&ipv6_addr
, AF_INET6
);
1356 chan1
= new_fake_channel();
1358 /* Need to be registered to get added to the id map. */
1359 channel_register(chan1
);
1360 tt_int_op(chan1
->registered
, OP_EQ
, 1);
1361 /* We need those for the test. */
1362 chan1
->is_canonical
= test_chan_is_canonical
;
1363 chan1
->matches_target
= test_chan_matches_target
;
1364 chan1
->timestamp_created
= now
- 9;
1366 chan2
= new_fake_channel();
1368 /* Need to be registered to get added to the id map. */
1369 channel_register(chan2
);
1370 tt_int_op(chan2
->registered
, OP_EQ
, 1);
1371 /* We need those for the test. */
1372 chan2
->is_canonical
= test_chan_is_canonical
;
1373 chan2
->matches_target
= test_chan_matches_target
;
1374 /* Make it older than chan1. */
1375 chan2
->timestamp_created
= chan1
->timestamp_created
- 1;
1377 /* Say it's all canonical. */
1378 test_chan_should_be_canonical
= 1;
1380 /* Set channel identities and add it to the channel map. The last one to be
1381 * added is made the first one in the list so the lookup will always return
1382 * that one first. */
1383 channel_set_identity_digest(chan2
, digest
, &ed_id
);
1384 channel_set_identity_digest(chan1
, digest
, &ed_id
);
1385 tt_ptr_op(channel_find_by_remote_identity(digest
, NULL
), OP_EQ
, chan1
);
1386 tt_ptr_op(channel_find_by_remote_identity(digest
, &ed_id
), OP_EQ
, chan1
);
1388 /* The expected result is chan2 because it is older than chan1. */
1389 ret_chan
= channel_get_for_extend(digest
, &ed_id
, &ipv4_addr
, &ipv6_addr
,
1390 false, &msg
, &launch
);
1391 tt_assert(ret_chan
);
1392 tt_ptr_op(ret_chan
, OP_EQ
, chan2
);
1393 tt_int_op(launch
, OP_EQ
, 0);
1394 tt_str_op(msg
, OP_EQ
, "Connection is fine; using it.");
1396 /* Switch that around from previous test. */
1397 chan2
->timestamp_created
= chan1
->timestamp_created
+ 1;
1398 ret_chan
= channel_get_for_extend(digest
, &ed_id
, &ipv4_addr
, &ipv6_addr
,
1399 false, &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() works. */
1407 chan2
->timestamp_created
= chan1
->timestamp_created
;
1408 ret_chan
= channel_get_for_extend(digest
, &ed_id
, &ipv4_addr
, &ipv6_addr
,
1409 false, &msg
, &launch
);
1410 tt_assert(ret_chan
);
1411 tt_ptr_op(ret_chan
, OP_EQ
, chan1
);
1412 tt_int_op(launch
, OP_EQ
, 0);
1413 tt_str_op(msg
, OP_EQ
, "Connection is fine; using it.");
1415 /* For the rest of the tests, we need channel 1 to be the older. */
1416 chan2
->timestamp_created
= chan1
->timestamp_created
+ 1;
1418 /* Condemned the older channel. */
1419 chan1
->state
= CHANNEL_STATE_CLOSING
;
1420 ret_chan
= channel_get_for_extend(digest
, &ed_id
, &ipv4_addr
, &ipv6_addr
,
1421 false, &msg
, &launch
);
1422 tt_assert(ret_chan
);
1423 tt_ptr_op(ret_chan
, OP_EQ
, chan2
);
1424 tt_int_op(launch
, OP_EQ
, 0);
1425 tt_str_op(msg
, OP_EQ
, "Connection is fine; using it.");
1426 chan1
->state
= CHANNEL_STATE_OPEN
;
1428 /* Make the older channel a client one. */
1429 channel_mark_client(chan1
);
1430 ret_chan
= channel_get_for_extend(digest
, &ed_id
, &ipv4_addr
, &ipv6_addr
,
1431 false, &msg
, &launch
);
1432 tt_assert(ret_chan
);
1433 tt_ptr_op(ret_chan
, OP_EQ
, chan2
);
1434 tt_int_op(launch
, OP_EQ
, 0);
1435 tt_str_op(msg
, OP_EQ
, "Connection is fine; using it.");
1436 channel_clear_client(chan1
);
1438 /* Non matching ed identity with valid digest. */
1439 ed25519_public_key_t dumb_ed_id
;
1440 memset(&dumb_ed_id
, 0, sizeof(dumb_ed_id
));
1441 ret_chan
= channel_get_for_extend(digest
, &dumb_ed_id
,
1442 &ipv4_addr
, &ipv6_addr
,
1443 false, &msg
, &launch
);
1444 tt_assert(!ret_chan
);
1445 tt_str_op(msg
, OP_EQ
, "Not connected. Connecting.");
1446 tt_int_op(launch
, OP_EQ
, 1);
1448 /* Opening channel, we'll check if the target address matches. */
1449 test_chan_should_match_target
= 1;
1450 chan1
->state
= CHANNEL_STATE_OPENING
;
1451 chan2
->state
= CHANNEL_STATE_OPENING
;
1452 ret_chan
= channel_get_for_extend(digest
, &ed_id
, &ipv4_addr
, &ipv6_addr
,
1453 false, &msg
, &launch
);
1454 tt_assert(!ret_chan
);
1455 tt_str_op(msg
, OP_EQ
, "Connection in progress; waiting.");
1456 tt_int_op(launch
, OP_EQ
, 0);
1457 chan1
->state
= CHANNEL_STATE_OPEN
;
1458 chan2
->state
= CHANNEL_STATE_OPEN
;
1460 /* Mark channel 1 as bad for circuits. */
1461 channel_mark_bad_for_new_circs(chan1
);
1462 ret_chan
= channel_get_for_extend(digest
, &ed_id
, &ipv4_addr
, &ipv6_addr
,
1463 false, &msg
, &launch
);
1464 tt_assert(ret_chan
);
1465 tt_ptr_op(ret_chan
, OP_EQ
, chan2
);
1466 tt_int_op(launch
, OP_EQ
, 0);
1467 tt_str_op(msg
, OP_EQ
, "Connection is fine; using it.");
1468 chan1
->is_bad_for_new_circs
= 0;
1470 /* Mark both channels as unusable. */
1471 channel_mark_bad_for_new_circs(chan1
);
1472 channel_mark_bad_for_new_circs(chan2
);
1473 ret_chan
= channel_get_for_extend(digest
, &ed_id
, &ipv4_addr
, &ipv6_addr
,
1474 false, &msg
, &launch
);
1475 tt_assert(!ret_chan
);
1476 tt_str_op(msg
, OP_EQ
, "Connections all too old, or too non-canonical. "
1477 " Launching a new one.");
1478 tt_int_op(launch
, OP_EQ
, 1);
1479 chan1
->is_bad_for_new_circs
= 0;
1480 chan2
->is_bad_for_new_circs
= 0;
1482 /* Non canonical channels. */
1483 test_chan_should_be_canonical
= 0;
1484 test_chan_should_match_target
= 0;
1485 ret_chan
= channel_get_for_extend(digest
, &ed_id
, &ipv4_addr
, &ipv6_addr
,
1486 false, &msg
, &launch
);
1487 tt_assert(!ret_chan
);
1488 tt_str_op(msg
, OP_EQ
, "Connections all too old, or too non-canonical. "
1489 " Launching a new one.");
1490 tt_int_op(launch
, OP_EQ
, 1);
1493 free_fake_channel(chan1
);
1494 free_fake_channel(chan2
);
1498 test_channel_listener(void *arg
)
1501 time_t now
= time(NULL
);
1502 channel_listener_t
*chan
= NULL
;
1506 chan
= tor_malloc_zero(sizeof(*chan
));
1508 channel_init_listener(chan
);
1509 tt_u64_op(chan
->global_identifier
, OP_EQ
, 1);
1510 tt_int_op(chan
->timestamp_created
, OP_GE
, now
);
1511 chan
->close
= test_chan_listener_close
;
1513 /* Register it. At this point, it is not open so it will be put in the
1515 channel_listener_register(chan
);
1516 tt_int_op(chan
->registered
, OP_EQ
, 1);
1517 channel_listener_unregister(chan
);
1519 /* Register it as listening now thus active. */
1520 chan
->state
= CHANNEL_LISTENER_STATE_LISTENING
;
1521 channel_listener_register(chan
);
1522 tt_int_op(chan
->registered
, OP_EQ
, 1);
1524 /* Set the listener function. */
1525 channel_listener_set_listener_fn(chan
, test_chan_listener_fn
);
1526 tt_ptr_op(chan
->listener
, OP_EQ
, test_chan_listener_fn
);
1528 /* Put a channel in the listener incoming list and queue it.
1529 * function. By doing this, the listener() handler will be called. */
1530 channel_t
*in_chan
= new_fake_channel();
1531 old_count
= test_chan_listener_fn_called
;
1532 channel_listener_queue_incoming(chan
, in_chan
);
1533 free_fake_channel(in_chan
);
1534 tt_int_op(test_chan_listener_fn_called
, OP_EQ
, old_count
+ 1);
1536 /* Put listener channel in CLOSING state. */
1537 old_count
= test_chan_listener_close_fn_called
;
1538 channel_listener_mark_for_close(chan
);
1539 tt_int_op(test_chan_listener_close_fn_called
, OP_EQ
, old_count
+ 1);
1540 channel_listener_change_state(chan
, CHANNEL_LISTENER_STATE_CLOSED
);
1542 /* Dump stats so we at least hit the code path. */
1543 chan
->describe_transport
= test_chan_listener_describe_transport
;
1544 /* There is a check for "now > timestamp_created" when dumping the stats so
1545 * make sure we go in. */
1546 chan
->timestamp_created
= now
- 10;
1547 channel_listener_dump_statistics(chan
, LOG_INFO
);
1551 channel_listener_unregister(chan
);
1557 #define TEST_SETUP_MATCHES_ADDR(orcon, addr, src, rv) STMT_BEGIN \
1558 rv = tor_inet_pton(addr.family, src, &addr.addr); \
1559 tt_int_op(rv, OP_EQ, 1); \
1560 orcon->base_.addr = addr; \
1563 #define TEST_MATCHES_ADDR(chan, addr4, addr6, rv, exp) STMT_BEGIN \
1564 rv = channel_matches_target_addr_for_extend(chan, addr4, addr6); \
1565 tt_int_op(rv, OP_EQ, exp); \
1569 test_channel_matches_target_addr_for_extend(void *arg
)
1573 channel_tls_t
*tlschan
= tor_malloc_zero(sizeof(*tlschan
));
1574 or_connection_t
*orcon
= tor_malloc_zero(sizeof(*orcon
));
1575 channel_t
*chan
= &(tlschan
->base_
);
1579 tlschan
->conn
= orcon
;
1580 channel_tls_common_init(tlschan
);
1582 /* Test for IPv4 addresses. */
1583 addr
.family
= AF_INET
;
1584 TEST_SETUP_MATCHES_ADDR(orcon
, addr
, "1.2.3.4", rv
);
1585 TEST_MATCHES_ADDR(chan
, &addr
, NULL
, rv
, 1);
1587 tor_inet_pton(addr
.family
, "2.5.3.4", &addr
.addr
);
1588 TEST_MATCHES_ADDR(chan
, &addr
, NULL
, rv
, 0);
1590 /* Test for IPv6 addresses. */
1591 addr
.family
= AF_INET6
;
1592 TEST_SETUP_MATCHES_ADDR(orcon
, addr
, "3:4:7:1:9:8:09:10", rv
);
1593 TEST_MATCHES_ADDR(chan
, NULL
, &addr
, rv
, 1);
1595 tor_inet_pton(addr
.family
, "::", &addr
.addr
);
1596 TEST_MATCHES_ADDR(chan
, NULL
, &addr
, rv
, 0);
1599 circuitmux_clear_policy(chan
->cmux
);
1600 circuitmux_free(chan
->cmux
);
1605 struct testcase_t channel_tests
[] = {
1606 { "inbound_cell", test_channel_inbound_cell
, TT_FORK
,
1608 { "outbound_cell", test_channel_outbound_cell
, TT_FORK
,
1610 { "id_map", test_channel_id_map
, TT_FORK
,
1612 { "lifecycle", test_channel_lifecycle
, TT_FORK
,
1614 { "lifecycle_2", test_channel_lifecycle_2
, TT_FORK
,
1616 { "dumpstats", test_channel_dumpstats
, TT_FORK
,
1618 { "state", test_channel_state
, TT_FORK
,
1620 { "duplicates", test_channel_duplicates
, TT_FORK
,
1622 { "get_channel_for_extend", test_channel_for_extend
, TT_FORK
,
1624 { "listener", test_channel_listener
, TT_FORK
,
1626 { "matches_target", test_channel_matches_target_addr_for_extend
, TT_FORK
,