LCOV - code coverage report
Current view: top level - test - test_channel.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 786 806 97.5 %
Date: 2021-11-24 03:28:48 Functions: 35 37 94.6 %

          Line data    Source code
       1             : /* Copyright (c) 2013-2021, The Tor Project, Inc. */
       2             : /* See LICENSE for licensing information */
       3             : 
       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 */
      17             : #define RELAY_PRIVATE
      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"
      26             : 
      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"
      34             : 
      35             : /* Test suite stuff */
      36             : #include "test/log_test_helpers.h"
      37             : #include "test/test.h"
      38             : #include "test/fakechans.h"
      39             : 
      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;
      55             : 
      56             : static const char *
      57           1 : chan_test_describe_transport(channel_t *ch)
      58             : {
      59           1 :   tt_ptr_op(ch, OP_NE, NULL);
      60             : 
      61           1 :  done:
      62           1 :   return "Fake channel for unit tests";
      63             : }
      64             : 
      65             : /**
      66             :  * Mock for channel_dump_statistics(); if the channel matches the
      67             :  * target, bump a counter - otherwise ignore.
      68             :  */
      69             : 
      70             : static void
      71           2 : chan_test_channel_dump_statistics_mock(channel_t *chan, int severity)
      72             : {
      73           2 :   tt_ptr_op(chan, OP_NE, NULL);
      74             : 
      75           2 :   (void)severity;
      76             : 
      77           2 :   if (chan != NULL && chan == dump_statistics_mock_target) {
      78           2 :     ++dump_statistics_mock_matches;
      79             :   }
      80             : 
      81           0 :  done:
      82           2 :   return;
      83             : }
      84             : 
      85             : /*
      86             :  * Handle an incoming fixed-size cell for unit tests
      87             :  */
      88             : 
      89             : static void
      90           2 : chan_test_cell_handler(channel_t *chan, cell_t *cell)
      91             : {
      92           2 :   tt_assert(chan);
      93           2 :   tt_assert(cell);
      94             : 
      95           2 :   test_chan_last_seen_fixed_cell_ptr = cell;
      96           2 :   ++test_chan_fixed_cells_recved;
      97             : 
      98           2 :  done:
      99           2 :   return;
     100             : }
     101             : 
     102             : /*
     103             :  * Fake transport-specific stats call
     104             :  */
     105             : 
     106             : static void
     107           1 : chan_test_dumpstats(channel_t *ch, int severity)
     108             : {
     109           1 :   tt_ptr_op(ch, OP_NE, NULL);
     110             : 
     111           1 :   (void)severity;
     112             : 
     113           1 :   ++test_dumpstats_calls;
     114             : 
     115           1 :  done:
     116           1 :   return;
     117             : }
     118             : 
     119             : static void
     120          20 : chan_test_close(channel_t *ch)
     121             : {
     122          20 :   tt_assert(ch);
     123             : 
     124          20 :   ++test_close_called;
     125             : 
     126          20 :  done:
     127          20 :   return;
     128             : }
     129             : 
     130             : /*
     131             :  * Close a channel through the error path
     132             :  */
     133             : 
     134             : static void
     135           2 : chan_test_error(channel_t *ch)
     136             : {
     137           2 :   tt_assert(ch);
     138           2 :   tt_assert(!(ch->state == CHANNEL_STATE_CLOSING ||
     139             :                 ch->state == CHANNEL_STATE_ERROR ||
     140             :                 ch->state == CHANNEL_STATE_CLOSED));
     141             : 
     142           2 :   channel_close_for_error(ch);
     143             : 
     144           2 :  done:
     145           2 :   return;
     146             : }
     147             : 
     148             : /*
     149             :  * Finish closing a channel from CHANNEL_STATE_CLOSING
     150             :  */
     151             : 
     152             : static void
     153           8 : chan_test_finish_close(channel_t *ch)
     154             : {
     155           8 :   tt_assert(ch);
     156           8 :   tt_assert(ch->state == CHANNEL_STATE_CLOSING);
     157             : 
     158           8 :   channel_closed(ch);
     159             : 
     160           8 :  done:
     161           8 :   return;
     162             : }
     163             : 
     164             : static const char *
     165         243 : chan_test_describe_peer(const channel_t *ch)
     166             : {
     167         243 :   tt_assert(ch);
     168             : 
     169         243 :  done:
     170         243 :   return "Fake channel for unit tests; no real endpoint";
     171             : }
     172             : 
     173             : static int
     174           3 : chan_test_get_remote_addr(const channel_t *ch, tor_addr_t *out)
     175             : {
     176           3 :   (void)ch;
     177           3 :   tor_addr_from_ipv4h(out, 0x7f000001);
     178           3 :   return 1;
     179             : }
     180             : 
     181             : static int
     182           4 : chan_test_num_cells_writeable(channel_t *ch)
     183             : {
     184           4 :   tt_assert(ch);
     185             : 
     186           4 :  done:
     187           4 :   return 32;
     188             : }
     189             : 
     190             : static int
     191           3 : chan_test_write_packed_cell(channel_t *ch,
     192             :                             packed_cell_t *packed_cell)
     193             : {
     194           3 :   int rv = 0;
     195             : 
     196           3 :   tt_assert(ch);
     197           3 :   tt_assert(packed_cell);
     198             : 
     199           3 :   if (test_chan_accept_cells) {
     200             :     /* Free the cell and bump the counter */
     201           3 :     ++test_cells_written;
     202           3 :     rv = 1;
     203             :   }
     204             :   /* else return 0, we didn't accept it */
     205             : 
     206           0 :  done:
     207           3 :   return rv;
     208             : }
     209             : 
     210             : static int
     211           0 : chan_test_write_var_cell(channel_t *ch, var_cell_t *var_cell)
     212             : {
     213           0 :   int rv = 0;
     214             : 
     215           0 :   tt_assert(ch);
     216           0 :   tt_assert(var_cell);
     217             : 
     218           0 :   if (test_chan_accept_cells) {
     219             :     /* Free the cell and bump the counter */
     220           0 :     var_cell_free(var_cell);
     221           0 :     ++test_cells_written;
     222           0 :     rv = 1;
     223             :   }
     224             :   /* else return 0, we didn't accept it */
     225             : 
     226           0 :  done:
     227           0 :   return rv;
     228             : }
     229             : 
     230             : /**
     231             :  * Fill out c with a new fake cell for test suite use
     232             :  */
     233             : 
     234             : void
     235           3 : make_fake_cell(cell_t *c)
     236             : {
     237           3 :   tt_ptr_op(c, OP_NE, NULL);
     238             : 
     239           3 :   c->circ_id = 1;
     240           3 :   c->command = CELL_RELAY;
     241           3 :   memset(c->payload, 0, CELL_PAYLOAD_SIZE);
     242             : 
     243           3 :  done:
     244           3 :   return;
     245             : }
     246             : 
     247             : /**
     248             :  * Fill out c with a new fake var_cell for test suite use
     249             :  */
     250             : 
     251             : void
     252           0 : make_fake_var_cell(var_cell_t *c)
     253             : {
     254           0 :   tt_ptr_op(c, OP_NE, NULL);
     255             : 
     256           0 :   c->circ_id = 1;
     257           0 :   c->command = CELL_VERSIONS;
     258           0 :   c->payload_len = CELL_PAYLOAD_SIZE / 2;
     259           0 :   memset(c->payload, 0, c->payload_len);
     260             : 
     261           0 :  done:
     262           0 :   return;
     263             : }
     264             : 
     265             : /**
     266             :  * Set up a new fake channel for the test suite
     267             :  */
     268             : 
     269             : channel_t *
     270         159 : new_fake_channel(void)
     271             : {
     272         159 :   channel_t *chan = tor_malloc_zero(sizeof(channel_t));
     273         159 :   channel_init(chan);
     274             : 
     275         159 :   chan->close = chan_test_close;
     276         159 :   chan->num_cells_writeable = chan_test_num_cells_writeable;
     277         159 :   chan->describe_peer = chan_test_describe_peer;
     278         159 :   chan->get_remote_addr = chan_test_get_remote_addr;
     279         159 :   chan->write_packed_cell = chan_test_write_packed_cell;
     280         159 :   chan->write_var_cell = chan_test_write_var_cell;
     281         159 :   chan->state = CHANNEL_STATE_OPEN;
     282             : 
     283         159 :   chan->cmux = circuitmux_alloc();
     284         159 :   circuitmux_set_policy(chan->cmux, &ewma_policy);
     285             : 
     286         159 :   return chan;
     287             : }
     288             : 
     289             : void
     290         141 : free_fake_channel(channel_t *chan)
     291             : {
     292         141 :   if (! chan)
     293             :     return;
     294             : 
     295         138 :   if (chan->cmux)
     296         138 :     circuitmux_free(chan->cmux);
     297             : 
     298         138 :   tor_free(chan);
     299             : }
     300             : 
     301             : /**
     302             :  * Counter query for scheduler_channel_has_waiting_cells_mock()
     303             :  */
     304             : 
     305             : int
     306           8 : get_mock_scheduler_has_waiting_cells_count(void)
     307             : {
     308           8 :   return test_has_waiting_cells_count;
     309             : }
     310             : 
     311             : /**
     312             :  * Mock for scheduler_channel_has_waiting_cells()
     313             :  */
     314             : 
     315             : void
     316           4 : scheduler_channel_has_waiting_cells_mock(channel_t *ch)
     317             : {
     318           4 :   (void)ch;
     319             : 
     320             :   /* Increment counter */
     321           4 :   ++test_has_waiting_cells_count;
     322             : 
     323           4 :   return;
     324             : }
     325             : 
     326             : static void
     327           4 : scheduler_channel_doesnt_want_writes_mock(channel_t *ch)
     328             : {
     329           4 :   (void)ch;
     330             : 
     331             :   /* Increment counter */
     332           4 :   ++test_doesnt_want_writes_count;
     333             : 
     334           4 :   return;
     335             : }
     336             : 
     337             : /**
     338             :  * Mock for scheduler_release_channel()
     339             :  */
     340             : 
     341             : void
     342          40 : scheduler_release_channel_mock(channel_t *ch)
     343             : {
     344          40 :   (void)ch;
     345             : 
     346             :   /* Increment counter */
     347          40 :   ++test_releases_count;
     348             : 
     349          40 :   return;
     350             : }
     351             : 
     352             : static int
     353          22 : test_chan_is_canonical(channel_t *chan)
     354             : {
     355          22 :   tor_assert(chan);
     356             : 
     357          22 :   if (test_chan_should_be_canonical) {
     358          18 :     return 1;
     359             :   }
     360             :   return 0;
     361             : }
     362             : 
     363             : static int
     364          26 : test_chan_matches_target(channel_t *chan, const tor_addr_t *target)
     365             : {
     366          26 :   (void) chan;
     367          26 :   (void) target;
     368             : 
     369          26 :   if (test_chan_should_match_target) {
     370           6 :     return 1;
     371             :   }
     372             :   return 0;
     373             : }
     374             : 
     375             : static void
     376           1 : test_chan_listener_close(channel_listener_t *chan)
     377             : {
     378           1 :   (void) chan;
     379           1 :   ++test_chan_listener_close_fn_called;
     380           1 :   return;
     381             : }
     382             : 
     383             : static void
     384           1 : test_chan_listener_fn(channel_listener_t *listener, channel_t *chan)
     385             : {
     386           1 :   (void) listener;
     387           1 :   (void) chan;
     388             : 
     389           1 :   ++test_chan_listener_fn_called;
     390           1 :   return;
     391             : }
     392             : 
     393             : static const char *
     394           1 : test_chan_listener_describe_transport(channel_listener_t *chan)
     395             : {
     396           1 :   (void) chan;
     397           1 :   return "Fake listener channel.";
     398             : }
     399             : 
     400             : /**
     401             :  * Test for channel_dumpstats() and limited test for
     402             :  * channel_dump_statistics()
     403             :  */
     404             : 
     405             : static void
     406           1 : test_channel_dumpstats(void *arg)
     407             : {
     408           1 :   channel_t *ch = NULL;
     409           1 :   cell_t *cell = NULL;
     410           1 :   packed_cell_t *p_cell = NULL;
     411           1 :   int old_count;
     412             : 
     413           1 :   (void)arg;
     414             : 
     415             :   /* Mock these for duration of the test */
     416           1 :   MOCK(scheduler_channel_doesnt_want_writes,
     417             :        scheduler_channel_doesnt_want_writes_mock);
     418           1 :   MOCK(scheduler_release_channel,
     419             :        scheduler_release_channel_mock);
     420             : 
     421             :   /* Set up a new fake channel */
     422           1 :   ch = new_fake_channel();
     423           1 :   tt_assert(ch);
     424             : 
     425             :   /* Try to register it */
     426           1 :   channel_register(ch);
     427           1 :   tt_assert(ch->registered);
     428             : 
     429             :   /* Set up mock */
     430           1 :   dump_statistics_mock_target = ch;
     431           1 :   dump_statistics_mock_matches = 0;
     432           1 :   MOCK(channel_dump_statistics,
     433             :        chan_test_channel_dump_statistics_mock);
     434             : 
     435             :   /* Call channel_dumpstats() */
     436           1 :   channel_dumpstats(LOG_DEBUG);
     437             : 
     438             :   /* Assert that we hit the mock */
     439           1 :   tt_int_op(dump_statistics_mock_matches, OP_EQ, 1);
     440             : 
     441             :   /* Close the channel */
     442           1 :   channel_mark_for_close(ch);
     443           1 :   tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING);
     444           1 :   chan_test_finish_close(ch);
     445           1 :   tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSED);
     446             : 
     447             :   /* Try again and hit the finished channel */
     448           1 :   channel_dumpstats(LOG_DEBUG);
     449           1 :   tt_int_op(dump_statistics_mock_matches, OP_EQ, 2);
     450             : 
     451           1 :   channel_run_cleanup();
     452           1 :   ch = NULL;
     453             : 
     454             :   /* Now we should hit nothing */
     455           1 :   channel_dumpstats(LOG_DEBUG);
     456           1 :   tt_int_op(dump_statistics_mock_matches, OP_EQ, 2);
     457             : 
     458             :   /* Unmock */
     459           1 :   UNMOCK(channel_dump_statistics);
     460           1 :   dump_statistics_mock_target = NULL;
     461           1 :   dump_statistics_mock_matches = 0;
     462             : 
     463             :   /* Now make another channel */
     464           1 :   ch = new_fake_channel();
     465           1 :   tt_assert(ch);
     466           1 :   channel_register(ch);
     467           1 :   tt_int_op(ch->registered, OP_EQ, 1);
     468             :   /* Lie about its age so dumpstats gets coverage for rate calculations */
     469           1 :   ch->timestamp_created = time(NULL) - 30;
     470           1 :   tt_int_op(ch->timestamp_created, OP_GT, 0);
     471           1 :   tt_int_op(time(NULL), OP_GT, ch->timestamp_created);
     472             : 
     473             :   /* Put cells through it both ways to make the counters non-zero */
     474           1 :   p_cell = packed_cell_new();
     475           1 :   test_chan_accept_cells = 1;
     476           1 :   old_count = test_cells_written;
     477           1 :   channel_write_packed_cell(ch, p_cell);
     478           1 :   tt_int_op(test_cells_written, OP_EQ, old_count + 1);
     479           1 :   tt_u64_op(ch->n_bytes_xmitted, OP_GT, 0);
     480           1 :   tt_u64_op(ch->n_cells_xmitted, OP_GT, 0);
     481             : 
     482             :   /* Receive path */
     483           1 :   channel_set_cell_handlers(ch,
     484             :                             chan_test_cell_handler);
     485           1 :   tt_ptr_op(channel_get_cell_handler(ch), OP_EQ, chan_test_cell_handler);
     486           1 :   cell = tor_malloc_zero(sizeof(*cell));
     487           1 :   old_count = test_chan_fixed_cells_recved;
     488           1 :   channel_process_cell(ch, cell);
     489           1 :   tt_int_op(test_chan_fixed_cells_recved, OP_EQ, old_count + 1);
     490           1 :   tt_u64_op(ch->n_bytes_recved, OP_GT, 0);
     491           1 :   tt_u64_op(ch->n_cells_recved, OP_GT, 0);
     492             : 
     493             :   /* Test channel_dump_statistics */
     494           1 :   ch->describe_transport = chan_test_describe_transport;
     495           1 :   ch->dumpstats = chan_test_dumpstats;
     496           1 :   test_chan_should_be_canonical = 1;
     497           1 :   ch->is_canonical = test_chan_is_canonical;
     498           1 :   old_count = test_dumpstats_calls;
     499           1 :   channel_dump_statistics(ch, LOG_DEBUG);
     500           1 :   tt_int_op(test_dumpstats_calls, OP_EQ, old_count + 1);
     501             : 
     502             :   /* Close the channel */
     503           1 :   channel_mark_for_close(ch);
     504           1 :   tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING);
     505           1 :   chan_test_finish_close(ch);
     506           1 :   tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSED);
     507           1 :   channel_run_cleanup();
     508           1 :   ch = NULL;
     509             : 
     510           1 :  done:
     511           1 :   free_fake_channel(ch);
     512           1 :   tor_free(cell);
     513             : 
     514           1 :   UNMOCK(scheduler_channel_doesnt_want_writes);
     515           1 :   UNMOCK(scheduler_release_channel);
     516             : 
     517           1 :   return;
     518             : }
     519             : 
     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.
     526             :  *
     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. */
     529             : static void
     530           1 : test_channel_outbound_cell(void *arg)
     531             : {
     532           1 :   int old_count;
     533           1 :   channel_t *chan = NULL;
     534           1 :   packed_cell_t *p_cell = NULL, *p_cell2 = NULL;
     535           1 :   origin_circuit_t *circ = NULL;
     536           1 :   cell_queue_t *queue;
     537             : 
     538           1 :   (void) arg;
     539             : 
     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           1 :   monotime_enable_test_mocking();
     543           1 :   monotime_set_mock_time_nsec(UINT64_C(1000000000) * 12345);
     544             : 
     545           1 :   cmux_ewma_set_options(NULL,NULL);
     546             : 
     547             :   /* The channel will be freed so we need to hijack this so the scheduler
     548             :    * doesn't get confused. */
     549           1 :   MOCK(scheduler_release_channel, scheduler_release_channel_mock);
     550             : 
     551             :   /* Accept cells to lower layer */
     552           1 :   test_chan_accept_cells = 1;
     553             : 
     554             :   /* Setup a valid circuit to queue a cell. */
     555           1 :   circ = origin_circuit_new();
     556           1 :   tt_assert(circ);
     557             :   /* Circuit needs an origin purpose to be considered origin. */
     558           1 :   TO_CIRCUIT(circ)->purpose = CIRCUIT_PURPOSE_C_GENERAL;
     559           1 :   TO_CIRCUIT(circ)->n_circ_id = 42;
     560             :   /* This is the outbound test so use the next channel queue. */
     561           1 :   queue = &TO_CIRCUIT(circ)->n_chan_cells;
     562             :   /* Setup packed cell to queue on the circuit. */
     563           1 :   p_cell = packed_cell_new();
     564           1 :   tt_assert(p_cell);
     565           1 :   p_cell2 = packed_cell_new();
     566           1 :   tt_assert(p_cell2);
     567             :   /* Setup a channel to put the circuit on. */
     568           1 :   chan = new_fake_channel();
     569           1 :   tt_assert(chan);
     570           1 :   chan->state = CHANNEL_STATE_OPENING;
     571           1 :   channel_change_state_open(chan);
     572             :   /* Outbound channel. */
     573           1 :   channel_mark_outgoing(chan);
     574             :   /* Try to register it so we can clean it through the channel cleanup
     575             :    * process. */
     576           1 :   channel_register(chan);
     577           1 :   tt_int_op(chan->registered, OP_EQ, 1);
     578             :   /* Set EWMA policy so we can pick it when flushing. */
     579           1 :   circuitmux_set_policy(chan->cmux, &ewma_policy);
     580           1 :   tt_ptr_op(circuitmux_get_policy(chan->cmux), OP_EQ, &ewma_policy);
     581             : 
     582             :   /* Register circuit to the channel circid map which will attach the circuit
     583             :    * to the channel's cmux as well. */
     584           1 :   circuit_set_n_circid_chan(TO_CIRCUIT(circ), 42, chan);
     585           1 :   tt_int_op(channel_num_circuits(chan), OP_EQ, 1);
     586             :   /* Test the cmux state. */
     587           1 :   tt_int_op(circuitmux_is_circuit_attached(chan->cmux, TO_CIRCUIT(circ)),
     588             :             OP_EQ, 1);
     589             : 
     590             :   /* Flush the channel without any cell on it. */
     591           1 :   old_count = test_cells_written;
     592           1 :   ssize_t flushed = channel_flush_some_cells(chan, 1);
     593           1 :   tt_i64_op(flushed, OP_EQ, 0);
     594           1 :   tt_int_op(test_cells_written, OP_EQ, old_count);
     595           1 :   tt_int_op(channel_more_to_flush(chan), OP_EQ, 0);
     596           1 :   tt_int_op(circuitmux_num_active_circuits(chan->cmux), OP_EQ, 0);
     597           1 :   tt_int_op(circuitmux_num_cells(chan->cmux), OP_EQ, 0);
     598           1 :   tt_int_op(circuitmux_is_circuit_active(chan->cmux, TO_CIRCUIT(circ)),
     599             :             OP_EQ, 0);
     600           1 :   tt_u64_op(chan->n_cells_xmitted, OP_EQ, 0);
     601           1 :   tt_u64_op(chan->n_bytes_xmitted, OP_EQ, 0);
     602             : 
     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           1 :   cell_queue_append(queue, p_cell);
     606           1 :   p_cell = NULL;
     607           1 :   tt_int_op(queue->n, OP_EQ, 1);
     608           1 :   cell_queue_append(queue, p_cell2);
     609           1 :   p_cell2 = NULL;
     610           1 :   tt_int_op(queue->n, OP_EQ, 2);
     611             : 
     612           1 :   update_circuit_on_cmux(TO_CIRCUIT(circ), CELL_DIRECTION_OUT);
     613           1 :   tt_int_op(circuitmux_num_active_circuits(chan->cmux), OP_EQ, 1);
     614           1 :   tt_int_op(circuitmux_num_cells(chan->cmux), OP_EQ, 2);
     615           1 :   tt_int_op(circuitmux_is_circuit_active(chan->cmux, TO_CIRCUIT(circ)),
     616             :             OP_EQ, 1);
     617             : 
     618             :   /* From this point on, we have a queued cell on an active circuit attached
     619             :    * to the channel's cmux. */
     620             : 
     621             :   /* Flush the first cell. This is going to go down the call stack. */
     622           1 :   old_count = test_cells_written;
     623           1 :   flushed = channel_flush_some_cells(chan, 1);
     624           1 :   tt_i64_op(flushed, OP_EQ, 1);
     625           1 :   tt_int_op(test_cells_written, OP_EQ, old_count + 1);
     626           1 :   tt_int_op(circuitmux_num_cells(chan->cmux), OP_EQ, 1);
     627           1 :   tt_int_op(channel_more_to_flush(chan), OP_EQ, 1);
     628             :   /* Circuit should remain active because there is a second cell queued. */
     629           1 :   tt_int_op(circuitmux_is_circuit_active(chan->cmux, TO_CIRCUIT(circ)),
     630             :             OP_EQ, 1);
     631             :   /* Should still be attached. */
     632           1 :   tt_int_op(circuitmux_is_circuit_attached(chan->cmux, TO_CIRCUIT(circ)),
     633             :             OP_EQ, 1);
     634           1 :   tt_u64_op(chan->n_cells_xmitted, OP_EQ, 1);
     635           1 :   tt_u64_op(chan->n_bytes_xmitted, OP_EQ, get_cell_network_size(0));
     636             : 
     637             :   /* Flush second cell. This is going to go down the call stack. */
     638           1 :   old_count = test_cells_written;
     639           1 :   flushed = channel_flush_some_cells(chan, 1);
     640           1 :   tt_i64_op(flushed, OP_EQ, 1);
     641           1 :   tt_int_op(test_cells_written, OP_EQ, old_count + 1);
     642           1 :   tt_int_op(circuitmux_num_cells(chan->cmux), OP_EQ, 0);
     643           1 :   tt_int_op(channel_more_to_flush(chan), OP_EQ, 0);
     644             :   /* No more cells should make the circuit inactive. */
     645           1 :   tt_int_op(circuitmux_is_circuit_active(chan->cmux, TO_CIRCUIT(circ)),
     646             :             OP_EQ, 0);
     647             :   /* Should still be attached. */
     648           1 :   tt_int_op(circuitmux_is_circuit_attached(chan->cmux, TO_CIRCUIT(circ)),
     649             :             OP_EQ, 1);
     650           1 :   tt_u64_op(chan->n_cells_xmitted, OP_EQ, 2);
     651           1 :   tt_u64_op(chan->n_bytes_xmitted, OP_EQ, get_cell_network_size(0) * 2);
     652             : 
     653           1 :  done:
     654           1 :   if (circ) {
     655           1 :     circuit_free_(TO_CIRCUIT(circ));
     656             :   }
     657           1 :   tor_free(p_cell);
     658           1 :   channel_free_all();
     659           1 :   UNMOCK(scheduler_release_channel);
     660           1 :   monotime_disable_test_mocking();
     661           1 : }
     662             : 
     663             : /* Test inbound cell. The callstack is:
     664             :  *  channel_process_cell()
     665             :  *    -> chan->cell_handler()
     666             :  *
     667             :  * This test is about checking if we can process an inbound cell down to the
     668             :  * channel handler. */
     669             : static void
     670           1 : test_channel_inbound_cell(void *arg)
     671             : {
     672           1 :   channel_t *chan = NULL;
     673           1 :   cell_t *cell = NULL;
     674           1 :   int old_count;
     675             : 
     676           1 :   (void) arg;
     677             : 
     678             :   /* The channel will be freed so we need to hijack this so the scheduler
     679             :    * doesn't get confused. */
     680           1 :   MOCK(scheduler_release_channel, scheduler_release_channel_mock);
     681             : 
     682             :   /* Accept cells to lower layer */
     683           1 :   test_chan_accept_cells = 1;
     684             : 
     685           1 :   chan = new_fake_channel();
     686           1 :   tt_assert(chan);
     687             :   /* Start it off in OPENING */
     688           1 :   chan->state = CHANNEL_STATE_OPENING;
     689             : 
     690             :   /* Try to register it */
     691           1 :   channel_register(chan);
     692           1 :   tt_int_op(chan->registered, OP_EQ, 1);
     693             : 
     694             :   /* Open it */
     695           1 :   channel_change_state_open(chan);
     696           1 :   tt_int_op(chan->state, OP_EQ, CHANNEL_STATE_OPEN);
     697           1 :   tt_int_op(chan->has_been_open, OP_EQ, 1);
     698             : 
     699             :   /* Receive a cell now. */
     700           1 :   cell = tor_malloc_zero(sizeof(*cell));
     701           1 :   make_fake_cell(cell);
     702           1 :   old_count = test_chan_fixed_cells_recved;
     703           1 :   channel_process_cell(chan, cell);
     704           1 :   tt_int_op(test_chan_fixed_cells_recved, OP_EQ, old_count);
     705           1 :   tt_assert(monotime_coarse_is_zero(&chan->timestamp_xfer));
     706           1 :   tt_u64_op(chan->timestamp_active, OP_EQ, 0);
     707           1 :   tt_u64_op(chan->timestamp_recv, OP_EQ, 0);
     708             : 
     709             :   /* Setup incoming cell handlers. We don't care about var cell, the channel
     710             :    * layers is not handling those. */
     711           1 :   channel_set_cell_handlers(chan, chan_test_cell_handler);
     712           1 :   tt_ptr_op(chan->cell_handler, OP_EQ, chan_test_cell_handler);
     713             :   /* Now process the cell, we should see it. */
     714           1 :   old_count = test_chan_fixed_cells_recved;
     715           1 :   channel_process_cell(chan, cell);
     716           1 :   tt_int_op(test_chan_fixed_cells_recved, OP_EQ, old_count + 1);
     717             :   /* We should have a series of timestamp set. */
     718           1 :   tt_assert(!monotime_coarse_is_zero(&chan->timestamp_xfer));
     719           1 :   tt_u64_op(chan->timestamp_active, OP_NE, 0);
     720           1 :   tt_u64_op(chan->timestamp_recv, OP_NE, 0);
     721           1 :   tt_assert(monotime_coarse_is_zero(&chan->next_padding_time));
     722           1 :   tt_u64_op(chan->n_cells_recved, OP_EQ, 1);
     723           1 :   tt_u64_op(chan->n_bytes_recved, OP_EQ, get_cell_network_size(0));
     724             : 
     725             :   /* Close it */
     726           1 :   old_count = test_close_called;
     727           1 :   channel_mark_for_close(chan);
     728           1 :   tt_int_op(chan->state, OP_EQ, CHANNEL_STATE_CLOSING);
     729           1 :   tt_int_op(chan->reason_for_closing, OP_EQ, CHANNEL_CLOSE_REQUESTED);
     730           1 :   tt_int_op(test_close_called, OP_EQ, old_count + 1);
     731             : 
     732             :   /* This closes the channel so it calls in the scheduler, make sure of it. */
     733           1 :   old_count = test_releases_count;
     734           1 :   chan_test_finish_close(chan);
     735           1 :   tt_int_op(test_releases_count, OP_EQ, old_count + 1);
     736           1 :   tt_int_op(chan->state, OP_EQ, CHANNEL_STATE_CLOSED);
     737             : 
     738             :   /* The channel will be free, lets make sure it is not accessible. */
     739           1 :   uint64_t chan_id = chan->global_identifier;
     740           1 :   tt_ptr_op(channel_find_by_global_id(chan_id), OP_EQ, chan);
     741           1 :   channel_run_cleanup();
     742           1 :   chan = channel_find_by_global_id(chan_id);
     743           1 :   tt_assert(chan == NULL);
     744             : 
     745           1 :  done:
     746           1 :   tor_free(cell);
     747           1 :   UNMOCK(scheduler_release_channel);
     748           1 : }
     749             : 
     750             : /**
     751             :  * Normal channel lifecycle test:
     752             :  *
     753             :  * OPENING->OPEN->MAINT->OPEN->CLOSING->CLOSED
     754             :  */
     755             : 
     756             : static void
     757           1 : test_channel_lifecycle(void *arg)
     758             : {
     759           1 :   channel_t *ch1 = NULL, *ch2 = NULL;
     760           1 :   packed_cell_t *p_cell = NULL;
     761           1 :   int old_count, init_doesnt_want_writes_count;
     762           1 :   int init_releases_count;
     763             : 
     764           1 :   (void)arg;
     765             : 
     766             :   /* Mock these for the whole lifecycle test */
     767           1 :   MOCK(scheduler_channel_doesnt_want_writes,
     768             :        scheduler_channel_doesnt_want_writes_mock);
     769           1 :   MOCK(scheduler_release_channel,
     770             :        scheduler_release_channel_mock);
     771             : 
     772             :   /* Cache some initial counter values */
     773           1 :   init_doesnt_want_writes_count = test_doesnt_want_writes_count;
     774           1 :   init_releases_count = test_releases_count;
     775             : 
     776             :   /* Accept cells to lower layer */
     777           1 :   test_chan_accept_cells = 1;
     778             : 
     779           1 :   ch1 = new_fake_channel();
     780           1 :   tt_assert(ch1);
     781             :   /* Start it off in OPENING */
     782           1 :   ch1->state = CHANNEL_STATE_OPENING;
     783             : 
     784             :   /* Try to register it */
     785           1 :   channel_register(ch1);
     786           1 :   tt_assert(ch1->registered);
     787             : 
     788             :   /* Try to write a cell through (should queue) */
     789           1 :   p_cell = packed_cell_new();
     790           1 :   old_count = test_cells_written;
     791           1 :   channel_write_packed_cell(ch1, p_cell);
     792           1 :   tt_int_op(old_count, OP_EQ, test_cells_written);
     793             : 
     794             :   /* Move it to OPEN and flush */
     795           1 :   channel_change_state_open(ch1);
     796             : 
     797             : /* Get another one */
     798           1 :   ch2 = new_fake_channel();
     799           1 :   tt_assert(ch2);
     800           1 :   ch2->state = CHANNEL_STATE_OPENING;
     801             : 
     802             :   /* Register */
     803           1 :   channel_register(ch2);
     804           1 :   tt_assert(ch2->registered);
     805             : 
     806             :   /* Check counters */
     807           1 :   tt_int_op(test_doesnt_want_writes_count, OP_EQ,
     808             :             init_doesnt_want_writes_count);
     809           1 :   tt_int_op(test_releases_count, OP_EQ, init_releases_count);
     810             : 
     811             :   /* Move ch1 to MAINT */
     812           1 :   channel_change_state(ch1, CHANNEL_STATE_MAINT);
     813           1 :   tt_int_op(test_doesnt_want_writes_count, OP_EQ,
     814             :             init_doesnt_want_writes_count + 1);
     815           1 :   tt_int_op(test_releases_count, OP_EQ, init_releases_count);
     816             : 
     817             :   /* Move ch2 to OPEN */
     818           1 :   channel_change_state_open(ch2);
     819           1 :   tt_int_op(test_doesnt_want_writes_count, OP_EQ,
     820             :             init_doesnt_want_writes_count + 1);
     821           1 :   tt_int_op(test_releases_count, OP_EQ, init_releases_count);
     822             : 
     823             :   /* Move ch1 back to OPEN */
     824           1 :   channel_change_state_open(ch1);
     825           1 :   tt_int_op(test_doesnt_want_writes_count, OP_EQ,
     826             :             init_doesnt_want_writes_count + 1);
     827           1 :   tt_int_op(test_releases_count, OP_EQ, init_releases_count);
     828             : 
     829             :   /* Mark ch2 for close */
     830           1 :   channel_mark_for_close(ch2);
     831           1 :   tt_int_op(ch2->state, OP_EQ, CHANNEL_STATE_CLOSING);
     832           1 :   tt_int_op(test_doesnt_want_writes_count, OP_EQ,
     833             :             init_doesnt_want_writes_count + 1);
     834           1 :   tt_int_op(test_releases_count, OP_EQ, init_releases_count + 1);
     835             : 
     836             :   /* Shut down channels */
     837           1 :   channel_free_all();
     838           1 :   ch1 = ch2 = NULL;
     839           1 :   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           1 :   tt_int_op(test_releases_count, OP_EQ, init_releases_count + 4);
     843             : 
     844           1 :  done:
     845           1 :   free_fake_channel(ch1);
     846           1 :   free_fake_channel(ch2);
     847             : 
     848           1 :   UNMOCK(scheduler_channel_doesnt_want_writes);
     849           1 :   UNMOCK(scheduler_release_channel);
     850           1 : }
     851             : 
     852             : /**
     853             :  * Weird channel lifecycle test:
     854             :  *
     855             :  * OPENING->CLOSING->CLOSED
     856             :  * OPENING->OPEN->CLOSING->ERROR
     857             :  * OPENING->OPEN->MAINT->CLOSING->CLOSED
     858             :  * OPENING->OPEN->MAINT->CLOSING->ERROR
     859             :  */
     860             : 
     861             : static void
     862           1 : test_channel_lifecycle_2(void *arg)
     863             : {
     864           1 :   channel_t *ch = NULL;
     865             : 
     866           1 :   (void)arg;
     867             : 
     868             :   /* Mock these for the whole lifecycle test */
     869           1 :   MOCK(scheduler_channel_doesnt_want_writes,
     870             :        scheduler_channel_doesnt_want_writes_mock);
     871           1 :   MOCK(scheduler_release_channel,
     872             :        scheduler_release_channel_mock);
     873             : 
     874             :   /* Accept cells to lower layer */
     875           1 :   test_chan_accept_cells = 1;
     876             : 
     877           1 :   ch = new_fake_channel();
     878           1 :   tt_assert(ch);
     879             :   /* Start it off in OPENING */
     880           1 :   ch->state = CHANNEL_STATE_OPENING;
     881             : 
     882             :   /* Try to register it */
     883           1 :   channel_register(ch);
     884           1 :   tt_assert(ch->registered);
     885             : 
     886             :   /* Try to close it */
     887           1 :   channel_mark_for_close(ch);
     888           1 :   tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING);
     889             : 
     890             :   /* Finish closing it */
     891           1 :   chan_test_finish_close(ch);
     892           1 :   tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSED);
     893           1 :   channel_run_cleanup();
     894           1 :   ch = NULL;
     895             : 
     896             :   /* Now try OPENING->OPEN->CLOSING->ERROR */
     897           1 :   ch = new_fake_channel();
     898           1 :   tt_assert(ch);
     899           1 :   ch->state = CHANNEL_STATE_OPENING;
     900           1 :   channel_register(ch);
     901           1 :   tt_assert(ch->registered);
     902             : 
     903             :   /* Finish opening it */
     904           1 :   channel_change_state_open(ch);
     905             : 
     906             :   /* Error exit from lower layer */
     907           1 :   chan_test_error(ch);
     908           1 :   tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING);
     909           1 :   chan_test_finish_close(ch);
     910           1 :   tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_ERROR);
     911           1 :   channel_run_cleanup();
     912           1 :   ch = NULL;
     913             : 
     914             :   /* OPENING->OPEN->MAINT->CLOSING->CLOSED close from maintenance state */
     915           1 :   ch = new_fake_channel();
     916           1 :   tt_assert(ch);
     917           1 :   ch->state = CHANNEL_STATE_OPENING;
     918           1 :   channel_register(ch);
     919           1 :   tt_assert(ch->registered);
     920             : 
     921             :   /* Finish opening it */
     922           1 :   channel_change_state_open(ch);
     923           1 :   tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_OPEN);
     924             : 
     925             :   /* Go to maintenance state */
     926           1 :   channel_change_state(ch, CHANNEL_STATE_MAINT);
     927           1 :   tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_MAINT);
     928             : 
     929             :   /* Lower layer close */
     930           1 :   channel_mark_for_close(ch);
     931           1 :   tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING);
     932             : 
     933             :   /* Finish */
     934           1 :   chan_test_finish_close(ch);
     935           1 :   tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSED);
     936           1 :   channel_run_cleanup();
     937           1 :   ch = NULL;
     938             : 
     939             :   /*
     940             :    * OPENING->OPEN->MAINT->CLOSING->CLOSED lower-layer close during
     941             :    * maintenance state
     942             :    */
     943           1 :   ch = new_fake_channel();
     944           1 :   tt_assert(ch);
     945           1 :   ch->state = CHANNEL_STATE_OPENING;
     946           1 :   channel_register(ch);
     947           1 :   tt_assert(ch->registered);
     948             : 
     949             :   /* Finish opening it */
     950           1 :   channel_change_state_open(ch);
     951           1 :   tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_OPEN);
     952             : 
     953             :   /* Go to maintenance state */
     954           1 :   channel_change_state(ch, CHANNEL_STATE_MAINT);
     955           1 :   tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_MAINT);
     956             : 
     957             :   /* Lower layer close */
     958           1 :   channel_close_from_lower_layer(ch);
     959           1 :   tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING);
     960             : 
     961             :   /* Finish */
     962           1 :   chan_test_finish_close(ch);
     963           1 :   tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSED);
     964           1 :   channel_run_cleanup();
     965           1 :   ch = NULL;
     966             : 
     967             :   /* OPENING->OPEN->MAINT->CLOSING->ERROR */
     968           1 :   ch = new_fake_channel();
     969           1 :   tt_assert(ch);
     970           1 :   ch->state = CHANNEL_STATE_OPENING;
     971           1 :   channel_register(ch);
     972           1 :   tt_assert(ch->registered);
     973             : 
     974             :   /* Finish opening it */
     975           1 :   channel_change_state_open(ch);
     976           1 :   tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_OPEN);
     977             : 
     978             :   /* Go to maintenance state */
     979           1 :   channel_change_state(ch, CHANNEL_STATE_MAINT);
     980           1 :   tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_MAINT);
     981             : 
     982             :   /* Lower layer close */
     983           1 :   chan_test_error(ch);
     984           1 :   tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING);
     985             : 
     986             :   /* Finish */
     987           1 :   chan_test_finish_close(ch);
     988           1 :   tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_ERROR);
     989           1 :   channel_run_cleanup();
     990           1 :   ch = NULL;
     991             : 
     992             :   /* Shut down channels */
     993           1 :   channel_free_all();
     994             : 
     995           1 :  done:
     996           1 :   tor_free(ch);
     997             : 
     998           1 :   UNMOCK(scheduler_channel_doesnt_want_writes);
     999           1 :   UNMOCK(scheduler_release_channel);
    1000             : 
    1001           1 :   return;
    1002             : }
    1003             : 
    1004             : static void
    1005           1 : test_channel_id_map(void *arg)
    1006             : {
    1007           1 :   (void)arg;
    1008             : #define N_CHAN 6
    1009           1 :   char rsa_id[N_CHAN][DIGEST_LEN];
    1010           1 :   ed25519_public_key_t *ed_id[N_CHAN];
    1011           1 :   channel_t *chan[N_CHAN];
    1012           1 :   int i;
    1013           1 :   ed25519_public_key_t ed_zero;
    1014           1 :   memset(&ed_zero, 0, sizeof(ed_zero));
    1015             : 
    1016           1 :   tt_int_op(DIGEST_LEN, OP_EQ, sizeof(rsa_id[0])); // Do I remember C?
    1017             : 
    1018           7 :   for (i = 0; i < N_CHAN; ++i) {
    1019           6 :     crypto_rand(rsa_id[i], DIGEST_LEN);
    1020           6 :     ed_id[i] = tor_malloc_zero(sizeof(*ed_id[i]));
    1021           6 :     crypto_rand((char*)ed_id[i]->pubkey, sizeof(ed_id[i]->pubkey));
    1022             :   }
    1023             : 
    1024             :   /* For channel 3, have no Ed identity. */
    1025           1 :   tor_free(ed_id[3]);
    1026             : 
    1027             :   /* Channel 2 and 4 have same ROSA identity */
    1028           1 :   memcpy(rsa_id[4], rsa_id[2], DIGEST_LEN);
    1029             : 
    1030             :   /* Channel 2 and 4 and 5 have same RSA identity */
    1031           1 :   memcpy(rsa_id[4], rsa_id[2], DIGEST_LEN);
    1032           1 :   memcpy(rsa_id[5], rsa_id[2], DIGEST_LEN);
    1033             : 
    1034             :   /* Channels 2 and 5 have same Ed25519 identity */
    1035           1 :   memcpy(ed_id[5], ed_id[2], sizeof(*ed_id[2]));
    1036             : 
    1037           7 :   for (i = 0; i < N_CHAN; ++i) {
    1038           6 :     chan[i] = new_fake_channel();
    1039           6 :     channel_register(chan[i]);
    1040           6 :     channel_set_identity_digest(chan[i], rsa_id[i], ed_id[i]);
    1041             :   }
    1042             : 
    1043             :   /* Lookup by RSA id only */
    1044           1 :   tt_ptr_op(chan[0], OP_EQ,
    1045             :             channel_find_by_remote_identity(rsa_id[0], NULL));
    1046           1 :   tt_ptr_op(chan[1], OP_EQ,
    1047             :             channel_find_by_remote_identity(rsa_id[1], NULL));
    1048           1 :   tt_ptr_op(chan[3], OP_EQ,
    1049             :             channel_find_by_remote_identity(rsa_id[3], NULL));
    1050           1 :   channel_t *ch;
    1051           1 :   ch = channel_find_by_remote_identity(rsa_id[2], NULL);
    1052           1 :   tt_assert(ch == chan[2] || ch == chan[4] || ch == chan[5]);
    1053           1 :   ch = channel_next_with_rsa_identity(ch);
    1054           1 :   tt_assert(ch == chan[2] || ch == chan[4] || ch == chan[5]);
    1055           1 :   ch = channel_next_with_rsa_identity(ch);
    1056           1 :   tt_assert(ch == chan[2] || ch == chan[4] || ch == chan[5]);
    1057           1 :   ch = channel_next_with_rsa_identity(ch);
    1058           1 :   tt_ptr_op(ch, OP_EQ, NULL);
    1059             : 
    1060             :   /* As above, but with zero Ed25519 ID (meaning "any ID") */
    1061           1 :   tt_ptr_op(chan[0], OP_EQ,
    1062             :             channel_find_by_remote_identity(rsa_id[0], &ed_zero));
    1063           1 :   tt_ptr_op(chan[1], OP_EQ,
    1064             :             channel_find_by_remote_identity(rsa_id[1], &ed_zero));
    1065           1 :   tt_ptr_op(chan[3], OP_EQ,
    1066             :             channel_find_by_remote_identity(rsa_id[3], &ed_zero));
    1067           1 :   ch = channel_find_by_remote_identity(rsa_id[2], &ed_zero);
    1068           1 :   tt_assert(ch == chan[2] || ch == chan[4] || ch == chan[5]);
    1069           1 :   ch = channel_next_with_rsa_identity(ch);
    1070           1 :   tt_assert(ch == chan[2] || ch == chan[4] || ch == chan[5]);
    1071           1 :   ch = channel_next_with_rsa_identity(ch);
    1072           1 :   tt_assert(ch == chan[2] || ch == chan[4] || ch == chan[5]);
    1073           1 :   ch = channel_next_with_rsa_identity(ch);
    1074           1 :   tt_ptr_op(ch, OP_EQ, NULL);
    1075             : 
    1076             :   /* Lookup nonexistent RSA identity */
    1077           1 :   tt_ptr_op(NULL, OP_EQ,
    1078             :             channel_find_by_remote_identity("!!!!!!!!!!!!!!!!!!!!", NULL));
    1079             : 
    1080             :   /* Look up by full identity pair */
    1081           1 :   tt_ptr_op(chan[0], OP_EQ,
    1082             :             channel_find_by_remote_identity(rsa_id[0], ed_id[0]));
    1083           1 :   tt_ptr_op(chan[1], OP_EQ,
    1084             :             channel_find_by_remote_identity(rsa_id[1], ed_id[1]));
    1085           1 :   tt_ptr_op(chan[3], OP_EQ,
    1086             :             channel_find_by_remote_identity(rsa_id[3], ed_id[3] /*NULL*/));
    1087           1 :   tt_ptr_op(chan[4], OP_EQ,
    1088             :             channel_find_by_remote_identity(rsa_id[4], ed_id[4]));
    1089           1 :   ch = channel_find_by_remote_identity(rsa_id[2], ed_id[2]);
    1090           1 :   tt_assert(ch == chan[2] || ch == chan[5]);
    1091             : 
    1092             :   /* Look up RSA identity with wrong ed25519 identity */
    1093           1 :   tt_ptr_op(NULL, OP_EQ,
    1094             :             channel_find_by_remote_identity(rsa_id[4], ed_id[0]));
    1095           1 :   tt_ptr_op(NULL, OP_EQ,
    1096             :             channel_find_by_remote_identity(rsa_id[2], ed_id[1]));
    1097           1 :   tt_ptr_op(NULL, OP_EQ,
    1098             :             channel_find_by_remote_identity(rsa_id[3], ed_id[1]));
    1099             : 
    1100           1 :  done:
    1101           7 :   for (i = 0; i < N_CHAN; ++i) {
    1102           6 :     channel_clear_identity_digest(chan[i]);
    1103           6 :     channel_unregister(chan[i]);
    1104           6 :     free_fake_channel(chan[i]);
    1105           6 :     tor_free(ed_id[i]);
    1106             :   }
    1107             : #undef N_CHAN
    1108           1 : }
    1109             : 
    1110             : static void
    1111           1 : test_channel_state(void *arg)
    1112             : {
    1113           1 :   (void) arg;
    1114             : 
    1115             :   /* Test state validity. */
    1116           1 :   tt_int_op(channel_state_is_valid(CHANNEL_STATE_CLOSED), OP_EQ, 1);
    1117           1 :   tt_int_op(channel_state_is_valid(CHANNEL_STATE_CLOSING), OP_EQ, 1);
    1118           1 :   tt_int_op(channel_state_is_valid(CHANNEL_STATE_ERROR), OP_EQ, 1);
    1119           1 :   tt_int_op(channel_state_is_valid(CHANNEL_STATE_OPEN), OP_EQ, 1);
    1120           1 :   tt_int_op(channel_state_is_valid(CHANNEL_STATE_OPENING), OP_EQ, 1);
    1121           1 :   tt_int_op(channel_state_is_valid(CHANNEL_STATE_MAINT), OP_EQ, 1);
    1122           1 :   tt_int_op(channel_state_is_valid(CHANNEL_STATE_LAST), OP_EQ, 0);
    1123           1 :   tt_int_op(channel_state_is_valid(INT_MAX), OP_EQ, 0);
    1124             : 
    1125             :   /* Test listener state validity. */
    1126           1 :   tt_int_op(channel_listener_state_is_valid(CHANNEL_LISTENER_STATE_CLOSED),
    1127             :             OP_EQ, 1);
    1128           1 :   tt_int_op(channel_listener_state_is_valid(CHANNEL_LISTENER_STATE_LISTENING),
    1129             :             OP_EQ, 1);
    1130           1 :   tt_int_op(channel_listener_state_is_valid(CHANNEL_LISTENER_STATE_CLOSING),
    1131             :             OP_EQ, 1);
    1132           1 :   tt_int_op(channel_listener_state_is_valid(CHANNEL_LISTENER_STATE_ERROR),
    1133             :             OP_EQ, 1);
    1134           1 :   tt_int_op(channel_listener_state_is_valid(CHANNEL_LISTENER_STATE_LAST),
    1135             :             OP_EQ, 0);
    1136           1 :   tt_int_op(channel_listener_state_is_valid(INT_MAX), OP_EQ, 0);
    1137             : 
    1138             :   /* Test state transition. */
    1139           1 :   tt_int_op(channel_state_can_transition(CHANNEL_STATE_CLOSED,
    1140             :                                          CHANNEL_STATE_OPENING), OP_EQ, 1);
    1141           1 :   tt_int_op(channel_state_can_transition(CHANNEL_STATE_CLOSED,
    1142             :                                          CHANNEL_STATE_ERROR), OP_EQ, 0);
    1143           1 :   tt_int_op(channel_state_can_transition(CHANNEL_STATE_CLOSING,
    1144             :                                          CHANNEL_STATE_ERROR), OP_EQ, 1);
    1145           1 :   tt_int_op(channel_state_can_transition(CHANNEL_STATE_CLOSING,
    1146             :                                          CHANNEL_STATE_CLOSED), OP_EQ, 1);
    1147           1 :   tt_int_op(channel_state_can_transition(CHANNEL_STATE_CLOSING,
    1148             :                                          CHANNEL_STATE_OPEN), OP_EQ, 0);
    1149           1 :   tt_int_op(channel_state_can_transition(CHANNEL_STATE_MAINT,
    1150             :                                          CHANNEL_STATE_CLOSING), OP_EQ, 1);
    1151           1 :   tt_int_op(channel_state_can_transition(CHANNEL_STATE_MAINT,
    1152             :                                          CHANNEL_STATE_ERROR), OP_EQ, 1);
    1153           1 :   tt_int_op(channel_state_can_transition(CHANNEL_STATE_MAINT,
    1154             :                                          CHANNEL_STATE_OPEN), OP_EQ, 1);
    1155           1 :   tt_int_op(channel_state_can_transition(CHANNEL_STATE_MAINT,
    1156             :                                          CHANNEL_STATE_OPENING), OP_EQ, 0);
    1157           1 :   tt_int_op(channel_state_can_transition(CHANNEL_STATE_OPENING,
    1158             :                                          CHANNEL_STATE_OPEN), OP_EQ, 1);
    1159           1 :   tt_int_op(channel_state_can_transition(CHANNEL_STATE_OPENING,
    1160             :                                          CHANNEL_STATE_CLOSING), OP_EQ, 1);
    1161           1 :   tt_int_op(channel_state_can_transition(CHANNEL_STATE_OPENING,
    1162             :                                          CHANNEL_STATE_ERROR), OP_EQ, 1);
    1163           1 :   tt_int_op(channel_state_can_transition(CHANNEL_STATE_OPEN,
    1164             :                                          CHANNEL_STATE_ERROR), OP_EQ, 1);
    1165           1 :   tt_int_op(channel_state_can_transition(CHANNEL_STATE_OPEN,
    1166             :                                          CHANNEL_STATE_CLOSING), OP_EQ, 1);
    1167           1 :   tt_int_op(channel_state_can_transition(CHANNEL_STATE_OPEN,
    1168             :                                          CHANNEL_STATE_ERROR), OP_EQ, 1);
    1169           1 :   tt_int_op(channel_state_can_transition(CHANNEL_STATE_OPEN,
    1170             :                                          CHANNEL_STATE_MAINT), OP_EQ, 1);
    1171           1 :   tt_int_op(channel_state_can_transition(CHANNEL_STATE_LAST,
    1172             :                                          CHANNEL_STATE_MAINT), OP_EQ, 0);
    1173           1 :   tt_int_op(channel_state_can_transition(CHANNEL_STATE_LAST, INT_MAX),
    1174             :             OP_EQ, 0);
    1175             : 
    1176             :   /* Test listener state transition. */
    1177           1 :   tt_int_op(channel_listener_state_can_transition(
    1178             :                                        CHANNEL_LISTENER_STATE_CLOSED,
    1179             :                                        CHANNEL_LISTENER_STATE_LISTENING),
    1180             :             OP_EQ, 1);
    1181           1 :   tt_int_op(channel_listener_state_can_transition(
    1182             :                                        CHANNEL_LISTENER_STATE_CLOSED,
    1183             :                                        CHANNEL_LISTENER_STATE_ERROR),
    1184             :             OP_EQ, 0);
    1185             : 
    1186           1 :   tt_int_op(channel_listener_state_can_transition(
    1187             :                                        CHANNEL_LISTENER_STATE_CLOSING,
    1188             :                                        CHANNEL_LISTENER_STATE_CLOSED),
    1189             :             OP_EQ, 1);
    1190             : 
    1191           1 :   tt_int_op(channel_listener_state_can_transition(
    1192             :                                        CHANNEL_LISTENER_STATE_CLOSING,
    1193             :                                        CHANNEL_LISTENER_STATE_ERROR),
    1194             :             OP_EQ, 1);
    1195           1 :   tt_int_op(channel_listener_state_can_transition(
    1196             :                                        CHANNEL_LISTENER_STATE_ERROR,
    1197             :                                        CHANNEL_LISTENER_STATE_CLOSING),
    1198             :             OP_EQ, 0);
    1199             : 
    1200           1 :   tt_int_op(channel_listener_state_can_transition(
    1201             :                                        CHANNEL_LISTENER_STATE_LISTENING,
    1202             :                                        CHANNEL_LISTENER_STATE_CLOSING),
    1203             :             OP_EQ, 1);
    1204           1 :   tt_int_op(channel_listener_state_can_transition(
    1205             :                                        CHANNEL_LISTENER_STATE_LISTENING,
    1206             :                                        CHANNEL_LISTENER_STATE_ERROR),
    1207             :             OP_EQ, 1);
    1208           1 :   tt_int_op(channel_listener_state_can_transition(
    1209             :                                        CHANNEL_LISTENER_STATE_LAST,
    1210             :                                        INT_MAX),
    1211             :             OP_EQ, 0);
    1212             : 
    1213             :   /* Test state string. */
    1214           1 :   tt_str_op(channel_state_to_string(CHANNEL_STATE_CLOSING), OP_EQ,
    1215             :             "closing");
    1216           1 :   tt_str_op(channel_state_to_string(CHANNEL_STATE_ERROR), OP_EQ,
    1217             :             "channel error");
    1218           1 :   tt_str_op(channel_state_to_string(CHANNEL_STATE_CLOSED), OP_EQ,
    1219             :             "closed");
    1220           1 :   tt_str_op(channel_state_to_string(CHANNEL_STATE_OPEN), OP_EQ,
    1221             :             "open");
    1222           1 :   tt_str_op(channel_state_to_string(CHANNEL_STATE_OPENING), OP_EQ,
    1223             :             "opening");
    1224           1 :   tt_str_op(channel_state_to_string(CHANNEL_STATE_MAINT), OP_EQ,
    1225             :             "temporarily suspended for maintenance");
    1226           1 :   tt_str_op(channel_state_to_string(CHANNEL_STATE_LAST), OP_EQ,
    1227             :             "unknown or invalid channel state");
    1228           1 :   tt_str_op(channel_state_to_string(INT_MAX), OP_EQ,
    1229             :             "unknown or invalid channel state");
    1230             : 
    1231             :   /* Test listener state string. */
    1232           1 :   tt_str_op(channel_listener_state_to_string(CHANNEL_LISTENER_STATE_CLOSING),
    1233             :             OP_EQ, "closing");
    1234           1 :   tt_str_op(channel_listener_state_to_string(CHANNEL_LISTENER_STATE_ERROR),
    1235             :             OP_EQ, "channel listener error");
    1236           1 :   tt_str_op(channel_listener_state_to_string(CHANNEL_LISTENER_STATE_LISTENING),
    1237             :             OP_EQ, "listening");
    1238           1 :   tt_str_op(channel_listener_state_to_string(CHANNEL_LISTENER_STATE_LAST),
    1239             :             OP_EQ, "unknown or invalid channel listener state");
    1240           1 :   tt_str_op(channel_listener_state_to_string(INT_MAX),
    1241             :             OP_EQ, "unknown or invalid channel listener state");
    1242             : 
    1243           1 :  done:
    1244           1 :   ;
    1245           1 : }
    1246             : 
    1247             : static networkstatus_t *mock_ns = NULL;
    1248             : 
    1249             : static networkstatus_t *
    1250           4 : mock_networkstatus_get_latest_consensus(void)
    1251             : {
    1252           4 :   return mock_ns;
    1253             : }
    1254             : 
    1255             : static void
    1256           1 : test_channel_duplicates(void *arg)
    1257             : {
    1258           1 :   channel_t *chan = NULL;
    1259           1 :   routerstatus_t rs;
    1260             : 
    1261           1 :   (void) arg;
    1262             : 
    1263           1 :   setup_full_capture_of_logs(LOG_INFO);
    1264             :   /* Try a flat call with channel nor connections. */
    1265           1 :   channel_check_for_duplicates();
    1266           1 :   expect_log_msg_containing(
    1267             :     "Found 0 connections to 0 relays. Found 0 current canonical "
    1268             :     "connections, in 0 of which we were a non-canonical peer. "
    1269             :     "0 relays had more than 1 connection, 0 had more than 2, and "
    1270           1 :     "0 had more than 4 connections.");
    1271             : 
    1272           1 :   mock_ns = tor_malloc_zero(sizeof(*mock_ns));
    1273           1 :   mock_ns->routerstatus_list = smartlist_new();
    1274           1 :   MOCK(networkstatus_get_latest_consensus,
    1275             :        mock_networkstatus_get_latest_consensus);
    1276             : 
    1277           1 :   chan = new_fake_channel();
    1278           1 :   tt_assert(chan);
    1279           1 :   chan->is_canonical = test_chan_is_canonical;
    1280           1 :   memset(chan->identity_digest, 'A', sizeof(chan->identity_digest));
    1281           1 :   channel_add_to_digest_map(chan);
    1282           1 :   tt_ptr_op(channel_find_by_remote_identity(chan->identity_digest, NULL),
    1283             :             OP_EQ, chan);
    1284             : 
    1285             :   /* No relay has been associated with this channel. */
    1286           1 :   channel_check_for_duplicates();
    1287           1 :   expect_log_msg_containing(
    1288             :     "Found 0 connections to 0 relays. Found 0 current canonical "
    1289             :     "connections, in 0 of which we were a non-canonical peer. "
    1290             :     "0 relays had more than 1 connection, 0 had more than 2, and "
    1291           1 :     "0 had more than 4 connections.");
    1292             : 
    1293             :   /* Associate relay to this connection in the consensus. */
    1294           1 :   memset(&rs, 0, sizeof(rs));
    1295           1 :   memset(rs.identity_digest, 'A', sizeof(rs.identity_digest));
    1296           1 :   smartlist_add(mock_ns->routerstatus_list, &rs);
    1297             : 
    1298             :   /* Non opened channel. */
    1299           1 :   chan->state = CHANNEL_STATE_CLOSING;
    1300           1 :   channel_check_for_duplicates();
    1301           1 :   expect_log_msg_containing(
    1302             :     "Found 0 connections to 0 relays. Found 0 current canonical "
    1303             :     "connections, in 0 of which we were a non-canonical peer. "
    1304             :     "0 relays had more than 1 connection, 0 had more than 2, and "
    1305           1 :     "0 had more than 4 connections.");
    1306           1 :   chan->state = CHANNEL_STATE_OPEN;
    1307             : 
    1308           1 :   channel_check_for_duplicates();
    1309           1 :   expect_log_msg_containing(
    1310             :     "Found 1 connections to 1 relays. Found 0 current canonical "
    1311             :     "connections, in 0 of which we were a non-canonical peer. "
    1312             :     "0 relays had more than 1 connection, 0 had more than 2, and "
    1313           1 :     "0 had more than 4 connections.");
    1314             : 
    1315           1 :   test_chan_should_be_canonical = 1;
    1316           1 :   channel_check_for_duplicates();
    1317           1 :   expect_log_msg_containing(
    1318             :     "Found 1 connections to 1 relays. Found 1 current canonical "
    1319             :     "connections, in 1 of which we were a non-canonical peer. "
    1320             :     "0 relays had more than 1 connection, 0 had more than 2, and "
    1321           1 :     "0 had more than 4 connections.");
    1322           1 :   teardown_capture_of_logs();
    1323             : 
    1324           1 :  done:
    1325           1 :   free_fake_channel(chan);
    1326           1 :   smartlist_clear(mock_ns->routerstatus_list);
    1327           1 :   networkstatus_vote_free(mock_ns);
    1328           1 :   UNMOCK(networkstatus_get_latest_consensus);
    1329           1 : }
    1330             : 
    1331             : static void
    1332           1 : test_channel_for_extend(void *arg)
    1333             : {
    1334           1 :   channel_t *chan1 = NULL, *chan2 = NULL;
    1335           1 :   channel_t *ret_chan = NULL;
    1336           1 :   char digest[DIGEST_LEN];
    1337           1 :   ed25519_public_key_t ed_id;
    1338           1 :   tor_addr_t ipv4_addr, ipv6_addr;
    1339           1 :   const char *msg;
    1340           1 :   int launch;
    1341           1 :   time_t now = time(NULL);
    1342             : 
    1343           1 :   (void) arg;
    1344             : 
    1345           1 :   memset(digest, 'A', sizeof(digest));
    1346           1 :   memset(&ed_id, 'B', sizeof(ed_id));
    1347             : 
    1348           1 :   tor_addr_make_null(&ipv4_addr, AF_INET);
    1349           1 :   tor_addr_make_null(&ipv6_addr, AF_INET6);
    1350             : 
    1351           1 :   chan1 = new_fake_channel();
    1352           1 :   tt_assert(chan1);
    1353             :   /* Need to be registered to get added to the id map. */
    1354           1 :   channel_register(chan1);
    1355           1 :   tt_int_op(chan1->registered, OP_EQ, 1);
    1356             :   /* We need those for the test. */
    1357           1 :   chan1->is_canonical = test_chan_is_canonical;
    1358           1 :   chan1->matches_target = test_chan_matches_target;
    1359           1 :   chan1->timestamp_created = now - 9;
    1360             : 
    1361           1 :   chan2 = new_fake_channel();
    1362           1 :   tt_assert(chan2);
    1363             :   /* Need to be registered to get added to the id map. */
    1364           1 :   channel_register(chan2);
    1365           1 :   tt_int_op(chan2->registered, OP_EQ, 1);
    1366             :   /* We need those for the test. */
    1367           1 :   chan2->is_canonical = test_chan_is_canonical;
    1368           1 :   chan2->matches_target = test_chan_matches_target;
    1369             :   /* Make it older than chan1. */
    1370           1 :   chan2->timestamp_created = chan1->timestamp_created - 1;
    1371             : 
    1372             :   /* Say it's all canonical. */
    1373           1 :   test_chan_should_be_canonical = 1;
    1374             : 
    1375             :   /* Set channel identities and add it to the channel map. The last one to be
    1376             :    * added is made the first one in the list so the lookup will always return
    1377             :    * that one first. */
    1378           1 :   channel_set_identity_digest(chan2, digest, &ed_id);
    1379           1 :   channel_set_identity_digest(chan1, digest, &ed_id);
    1380           1 :   tt_ptr_op(channel_find_by_remote_identity(digest, NULL), OP_EQ, chan1);
    1381           1 :   tt_ptr_op(channel_find_by_remote_identity(digest, &ed_id), OP_EQ, chan1);
    1382             : 
    1383             :   /* The expected result is chan2 because it is older than chan1. */
    1384           1 :   ret_chan = channel_get_for_extend(digest, &ed_id, &ipv4_addr, &ipv6_addr,
    1385             :                                     false, &msg, &launch);
    1386           1 :   tt_assert(ret_chan);
    1387           1 :   tt_ptr_op(ret_chan, OP_EQ, chan2);
    1388           1 :   tt_int_op(launch, OP_EQ, 0);
    1389           1 :   tt_str_op(msg, OP_EQ, "Connection is fine; using it.");
    1390             : 
    1391             :   /* Switch that around from previous test. */
    1392           1 :   chan2->timestamp_created = chan1->timestamp_created + 1;
    1393           1 :   ret_chan = channel_get_for_extend(digest, &ed_id, &ipv4_addr, &ipv6_addr,
    1394             :                                     false, &msg, &launch);
    1395           1 :   tt_assert(ret_chan);
    1396           1 :   tt_ptr_op(ret_chan, OP_EQ, chan1);
    1397           1 :   tt_int_op(launch, OP_EQ, 0);
    1398           1 :   tt_str_op(msg, OP_EQ, "Connection is fine; using it.");
    1399             : 
    1400             :   /* Same creation time, num circuits will be used and they both have 0 so the
    1401             :    * channel 2 should be picked due to how channel_is_better() works. */
    1402           1 :   chan2->timestamp_created = chan1->timestamp_created;
    1403           1 :   ret_chan = channel_get_for_extend(digest, &ed_id, &ipv4_addr, &ipv6_addr,
    1404             :                                     false, &msg, &launch);
    1405           1 :   tt_assert(ret_chan);
    1406           1 :   tt_ptr_op(ret_chan, OP_EQ, chan1);
    1407           1 :   tt_int_op(launch, OP_EQ, 0);
    1408           1 :   tt_str_op(msg, OP_EQ, "Connection is fine; using it.");
    1409             : 
    1410             :   /* For the rest of the tests, we need channel 1 to be the older. */
    1411           1 :   chan2->timestamp_created = chan1->timestamp_created + 1;
    1412             : 
    1413             :   /* Condemned the older channel. */
    1414           1 :   chan1->state = CHANNEL_STATE_CLOSING;
    1415           1 :   ret_chan = channel_get_for_extend(digest, &ed_id, &ipv4_addr, &ipv6_addr,
    1416             :                                     false, &msg, &launch);
    1417           1 :   tt_assert(ret_chan);
    1418           1 :   tt_ptr_op(ret_chan, OP_EQ, chan2);
    1419           1 :   tt_int_op(launch, OP_EQ, 0);
    1420           1 :   tt_str_op(msg, OP_EQ, "Connection is fine; using it.");
    1421           1 :   chan1->state = CHANNEL_STATE_OPEN;
    1422             : 
    1423             :   /* Make the older channel a client one. */
    1424           1 :   channel_mark_client(chan1);
    1425           1 :   ret_chan = channel_get_for_extend(digest, &ed_id, &ipv4_addr, &ipv6_addr,
    1426             :                                     false, &msg, &launch);
    1427           1 :   tt_assert(ret_chan);
    1428           1 :   tt_ptr_op(ret_chan, OP_EQ, chan2);
    1429           1 :   tt_int_op(launch, OP_EQ, 0);
    1430           1 :   tt_str_op(msg, OP_EQ, "Connection is fine; using it.");
    1431           1 :   channel_clear_client(chan1);
    1432             : 
    1433             :   /* Non matching ed identity with valid digest. */
    1434           1 :   ed25519_public_key_t dumb_ed_id;
    1435           1 :   memset(&dumb_ed_id, 0, sizeof(dumb_ed_id));
    1436           1 :   ret_chan = channel_get_for_extend(digest, &dumb_ed_id,
    1437             :                                     &ipv4_addr, &ipv6_addr,
    1438             :                                     false, &msg, &launch);
    1439           1 :   tt_assert(!ret_chan);
    1440           1 :   tt_str_op(msg, OP_EQ, "Not connected. Connecting.");
    1441           1 :   tt_int_op(launch, OP_EQ, 1);
    1442             : 
    1443             :   /* Opening channel, we'll check if the target address matches. */
    1444           1 :   test_chan_should_match_target = 1;
    1445           1 :   chan1->state = CHANNEL_STATE_OPENING;
    1446           1 :   chan2->state = CHANNEL_STATE_OPENING;
    1447           1 :   ret_chan = channel_get_for_extend(digest, &ed_id, &ipv4_addr, &ipv6_addr,
    1448             :                                     false, &msg, &launch);
    1449           1 :   tt_assert(!ret_chan);
    1450           1 :   tt_str_op(msg, OP_EQ, "Connection in progress; waiting.");
    1451           1 :   tt_int_op(launch, OP_EQ, 0);
    1452           1 :   chan1->state = CHANNEL_STATE_OPEN;
    1453           1 :   chan2->state = CHANNEL_STATE_OPEN;
    1454             : 
    1455             :   /* Mark channel 1 as bad for circuits. */
    1456           1 :   channel_mark_bad_for_new_circs(chan1);
    1457           1 :   ret_chan = channel_get_for_extend(digest, &ed_id, &ipv4_addr, &ipv6_addr,
    1458             :                                     false, &msg, &launch);
    1459           1 :   tt_assert(ret_chan);
    1460           1 :   tt_ptr_op(ret_chan, OP_EQ, chan2);
    1461           1 :   tt_int_op(launch, OP_EQ, 0);
    1462           1 :   tt_str_op(msg, OP_EQ, "Connection is fine; using it.");
    1463           1 :   chan1->is_bad_for_new_circs = 0;
    1464             : 
    1465             :   /* Mark both channels as unusable. */
    1466           1 :   channel_mark_bad_for_new_circs(chan1);
    1467           1 :   channel_mark_bad_for_new_circs(chan2);
    1468           1 :   ret_chan = channel_get_for_extend(digest, &ed_id, &ipv4_addr, &ipv6_addr,
    1469             :                                     false, &msg, &launch);
    1470           1 :   tt_assert(!ret_chan);
    1471           1 :   tt_str_op(msg, OP_EQ, "Connections all too old, or too non-canonical. "
    1472             :                         " Launching a new one.");
    1473           1 :   tt_int_op(launch, OP_EQ, 1);
    1474           1 :   chan1->is_bad_for_new_circs = 0;
    1475           1 :   chan2->is_bad_for_new_circs = 0;
    1476             : 
    1477             :   /* Non canonical channels. */
    1478           1 :   test_chan_should_be_canonical = 0;
    1479           1 :   test_chan_should_match_target = 0;
    1480           1 :   ret_chan = channel_get_for_extend(digest, &ed_id, &ipv4_addr, &ipv6_addr,
    1481             :                                     false, &msg, &launch);
    1482           1 :   tt_assert(!ret_chan);
    1483           1 :   tt_str_op(msg, OP_EQ, "Connections all too old, or too non-canonical. "
    1484             :                         " Launching a new one.");
    1485           1 :   tt_int_op(launch, OP_EQ, 1);
    1486             : 
    1487           1 :  done:
    1488           1 :   free_fake_channel(chan1);
    1489           1 :   free_fake_channel(chan2);
    1490           1 : }
    1491             : 
    1492             : static void
    1493           1 : test_channel_listener(void *arg)
    1494             : {
    1495           1 :   int old_count;
    1496           1 :   time_t now = time(NULL);
    1497           1 :   channel_listener_t *chan = NULL;
    1498             : 
    1499           1 :   (void) arg;
    1500             : 
    1501           1 :   chan = tor_malloc_zero(sizeof(*chan));
    1502           1 :   tt_assert(chan);
    1503           1 :   channel_init_listener(chan);
    1504           1 :   tt_u64_op(chan->global_identifier, OP_EQ, 1);
    1505           1 :   tt_int_op(chan->timestamp_created, OP_GE, now);
    1506           1 :   chan->close = test_chan_listener_close;
    1507             : 
    1508             :   /* Register it. At this point, it is not open so it will be put in the
    1509             :    * finished list. */
    1510           1 :   channel_listener_register(chan);
    1511           1 :   tt_int_op(chan->registered, OP_EQ, 1);
    1512           1 :   channel_listener_unregister(chan);
    1513             : 
    1514             :   /* Register it as listening now thus active. */
    1515           1 :   chan->state = CHANNEL_LISTENER_STATE_LISTENING;
    1516           1 :   channel_listener_register(chan);
    1517           1 :   tt_int_op(chan->registered, OP_EQ, 1);
    1518             : 
    1519             :   /* Set the listener function. */
    1520           1 :   channel_listener_set_listener_fn(chan, test_chan_listener_fn);
    1521           1 :   tt_ptr_op(chan->listener, OP_EQ, test_chan_listener_fn);
    1522             : 
    1523             :   /* Put a channel in the listener incoming list and queue it.
    1524             :    * function. By doing this, the listener() handler will be called. */
    1525           1 :   channel_t *in_chan = new_fake_channel();
    1526           1 :   old_count = test_chan_listener_fn_called;
    1527           1 :   channel_listener_queue_incoming(chan, in_chan);
    1528           1 :   free_fake_channel(in_chan);
    1529           1 :   tt_int_op(test_chan_listener_fn_called, OP_EQ, old_count + 1);
    1530             : 
    1531             :   /* Put listener channel in CLOSING state. */
    1532           1 :   old_count = test_chan_listener_close_fn_called;
    1533           1 :   channel_listener_mark_for_close(chan);
    1534           1 :   tt_int_op(test_chan_listener_close_fn_called, OP_EQ, old_count + 1);
    1535           1 :   channel_listener_change_state(chan, CHANNEL_LISTENER_STATE_CLOSED);
    1536             : 
    1537             :   /* Dump stats so we at least hit the code path. */
    1538           1 :   chan->describe_transport = test_chan_listener_describe_transport;
    1539             :   /* There is a check for "now > timestamp_created" when dumping the stats so
    1540             :    * make sure we go in. */
    1541           1 :   chan->timestamp_created = now - 10;
    1542           1 :   channel_listener_dump_statistics(chan, LOG_INFO);
    1543             : 
    1544           1 :  done:
    1545           1 :   if (chan) {
    1546           1 :     channel_listener_unregister(chan);
    1547           1 :     tor_free(chan);
    1548             :   }
    1549           1 :   channel_free_all();
    1550           1 : }
    1551             : 
    1552             : #define TEST_SETUP_MATCHES_ADDR(orcon, addr, src, rv) STMT_BEGIN \
    1553             :     rv = tor_inet_pton(addr.family, src, &addr.addr); \
    1554             :     tt_int_op(rv, OP_EQ, 1); \
    1555             :     orcon->base_.addr = addr; \
    1556             :   STMT_END;
    1557             : 
    1558             : #define TEST_MATCHES_ADDR(chan, addr4, addr6, rv, exp) STMT_BEGIN       \
    1559             :     rv = channel_matches_target_addr_for_extend(chan, addr4, addr6);    \
    1560             :     tt_int_op(rv, OP_EQ, exp); \
    1561             :   STMT_END;
    1562             : 
    1563             : static void
    1564           1 : test_channel_matches_target_addr_for_extend(void *arg)
    1565             : {
    1566           1 :   (void) arg;
    1567             : 
    1568           1 :   channel_tls_t *tlschan = tor_malloc_zero(sizeof(*tlschan));
    1569           1 :   or_connection_t *orcon = tor_malloc_zero(sizeof(*orcon));
    1570           1 :   channel_t *chan = &(tlschan->base_);
    1571           1 :   tor_addr_t addr;
    1572           1 :   int rv;
    1573             : 
    1574           1 :   tlschan->conn = orcon;
    1575           1 :   channel_tls_common_init(tlschan);
    1576             : 
    1577             :   /* Test for IPv4 addresses. */
    1578           1 :   addr.family = AF_INET;
    1579           1 :   TEST_SETUP_MATCHES_ADDR(orcon, addr, "1.2.3.4", rv);
    1580           1 :   TEST_MATCHES_ADDR(chan, &addr, NULL, rv, 1);
    1581             : 
    1582           1 :   tor_inet_pton(addr.family, "2.5.3.4", &addr.addr);
    1583           1 :   TEST_MATCHES_ADDR(chan, &addr, NULL, rv, 0);
    1584             : 
    1585             :   /* Test for IPv6 addresses. */
    1586           1 :   addr.family = AF_INET6;
    1587           1 :   TEST_SETUP_MATCHES_ADDR(orcon, addr, "3:4:7:1:9:8:09:10", rv);
    1588           1 :   TEST_MATCHES_ADDR(chan, NULL, &addr, rv, 1);
    1589             : 
    1590           1 :   tor_inet_pton(addr.family, "::", &addr.addr);
    1591           1 :   TEST_MATCHES_ADDR(chan, NULL, &addr, rv, 0);
    1592             : 
    1593           1 :  done:
    1594           1 :   circuitmux_clear_policy(chan->cmux);
    1595           1 :   circuitmux_free(chan->cmux);
    1596           1 :   tor_free(orcon);
    1597           1 :   tor_free(tlschan);
    1598           1 : }
    1599             : 
    1600             : struct testcase_t channel_tests[] = {
    1601             :   { "inbound_cell", test_channel_inbound_cell, TT_FORK,
    1602             :     NULL, NULL },
    1603             :   { "outbound_cell", test_channel_outbound_cell, TT_FORK,
    1604             :     NULL, NULL },
    1605             :   { "id_map", test_channel_id_map, TT_FORK,
    1606             :     NULL, NULL },
    1607             :   { "lifecycle", test_channel_lifecycle, TT_FORK,
    1608             :     NULL, NULL },
    1609             :   { "lifecycle_2", test_channel_lifecycle_2, TT_FORK,
    1610             :     NULL, NULL },
    1611             :   { "dumpstats", test_channel_dumpstats, TT_FORK,
    1612             :     NULL, NULL },
    1613             :   { "state", test_channel_state, TT_FORK,
    1614             :     NULL, NULL },
    1615             :   { "duplicates", test_channel_duplicates, TT_FORK,
    1616             :     NULL, NULL },
    1617             :   { "get_channel_for_extend", test_channel_for_extend, TT_FORK,
    1618             :     NULL, NULL },
    1619             :   { "listener", test_channel_listener, TT_FORK,
    1620             :     NULL, NULL },
    1621             :   { "matches_target", test_channel_matches_target_addr_for_extend, TT_FORK,
    1622             :     NULL, NULL },
    1623             :   END_OF_TESTCASES
    1624             : };

Generated by: LCOV version 1.14