2017-03-15 21:13:17 +01:00
|
|
|
/* Copyright (c) 2013-2017, The Tor Project, Inc. */
|
2013-12-13 15:34:46 +01:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
|
|
|
#define TOR_CHANNEL_INTERNAL_
|
2014-01-22 05:51:50 +01:00
|
|
|
#define CHANNEL_PRIVATE_
|
2013-12-13 15:34:46 +01:00
|
|
|
#include "or.h"
|
|
|
|
#include "channel.h"
|
|
|
|
/* For channel_note_destroy_not_pending */
|
2017-11-22 17:17:21 +01:00
|
|
|
#define CIRCUITLIST_PRIVATE
|
2013-12-13 15:34:46 +01:00
|
|
|
#include "circuitlist.h"
|
2014-01-22 07:20:59 +01:00
|
|
|
#include "circuitmux.h"
|
2017-11-22 17:17:21 +01:00
|
|
|
#include "circuitmux_ewma.h"
|
2013-12-13 15:34:46 +01:00
|
|
|
/* For var_cell_free */
|
|
|
|
#include "connection_or.h"
|
|
|
|
/* For packed_cell stuff */
|
|
|
|
#define RELAY_PRIVATE
|
|
|
|
#include "relay.h"
|
|
|
|
/* For init/free stuff */
|
|
|
|
#include "scheduler.h"
|
2014-01-21 09:46:57 +01:00
|
|
|
|
|
|
|
/* Test suite stuff */
|
2013-12-13 15:34:46 +01:00
|
|
|
#include "test.h"
|
2014-01-21 09:46:57 +01:00
|
|
|
#include "fakechans.h"
|
2013-12-13 15:34:46 +01:00
|
|
|
|
|
|
|
static int test_chan_accept_cells = 0;
|
2014-01-22 09:06:06 +01:00
|
|
|
static int test_chan_fixed_cells_recved = 0;
|
2016-03-15 04:28:04 +01:00
|
|
|
static cell_t * test_chan_last_seen_fixed_cell_ptr = NULL;
|
2014-01-22 09:06:06 +01:00
|
|
|
static int test_chan_var_cells_recved = 0;
|
2016-03-15 04:28:04 +01:00
|
|
|
static var_cell_t * test_chan_last_seen_var_cell_ptr = NULL;
|
2013-12-13 15:34:46 +01:00
|
|
|
static int test_cells_written = 0;
|
2013-12-19 00:50:32 +01:00
|
|
|
static int test_doesnt_want_writes_count = 0;
|
2014-01-23 01:23:54 +01:00
|
|
|
static int test_dumpstats_calls = 0;
|
2014-01-21 11:50:33 +01:00
|
|
|
static int test_has_waiting_cells_count = 0;
|
2016-05-30 19:57:32 +02:00
|
|
|
static double test_overhead_estimate = 1.0;
|
2013-12-19 00:50:32 +01:00
|
|
|
static int test_releases_count = 0;
|
2014-01-22 13:10:15 +01:00
|
|
|
static channel_t *dump_statistics_mock_target = NULL;
|
|
|
|
static int dump_statistics_mock_matches = 0;
|
2017-11-21 22:06:46 +01:00
|
|
|
static int test_close_called = 0;
|
2013-12-13 15:34:46 +01:00
|
|
|
|
2014-01-22 13:10:15 +01:00
|
|
|
static void chan_test_channel_dump_statistics_mock(
|
|
|
|
channel_t *chan, int severity);
|
2014-01-23 01:23:54 +01:00
|
|
|
static const char * chan_test_describe_transport(channel_t *ch);
|
|
|
|
static void chan_test_dumpstats(channel_t *ch, int severity);
|
2014-01-22 09:06:06 +01:00
|
|
|
static void chan_test_var_cell_handler(channel_t *ch,
|
|
|
|
var_cell_t *var_cell);
|
2014-01-22 07:20:59 +01:00
|
|
|
static void chan_test_error(channel_t *ch);
|
|
|
|
static void chan_test_finish_close(channel_t *ch);
|
2014-01-23 01:23:54 +01:00
|
|
|
static const char * chan_test_get_remote_descr(channel_t *ch, int flags);
|
|
|
|
static int chan_test_is_canonical(channel_t *ch, int req);
|
2013-12-13 15:34:46 +01:00
|
|
|
static size_t chan_test_num_bytes_queued(channel_t *ch);
|
2013-12-18 22:42:02 +01:00
|
|
|
static int chan_test_num_cells_writeable(channel_t *ch);
|
2013-12-13 15:34:46 +01:00
|
|
|
static int chan_test_write_cell(channel_t *ch, cell_t *cell);
|
|
|
|
static int chan_test_write_packed_cell(channel_t *ch,
|
|
|
|
packed_cell_t *packed_cell);
|
|
|
|
static int chan_test_write_var_cell(channel_t *ch, var_cell_t *var_cell);
|
2013-12-19 00:50:32 +01:00
|
|
|
static void scheduler_channel_doesnt_want_writes_mock(channel_t *ch);
|
2013-12-19 00:04:00 +01:00
|
|
|
|
2014-01-22 13:10:15 +01:00
|
|
|
static void test_channel_dumpstats(void *arg);
|
2013-12-19 00:50:32 +01:00
|
|
|
static void test_channel_lifecycle(void *arg);
|
2013-12-13 15:34:46 +01:00
|
|
|
|
2014-01-23 01:23:54 +01:00
|
|
|
static const char *
|
|
|
|
chan_test_describe_transport(channel_t *ch)
|
|
|
|
{
|
2017-08-24 21:55:27 +02:00
|
|
|
tt_ptr_op(ch, OP_NE, NULL);
|
2014-01-23 01:23:54 +01:00
|
|
|
|
|
|
|
done:
|
|
|
|
return "Fake channel for unit tests";
|
|
|
|
}
|
|
|
|
|
2014-01-22 13:10:15 +01:00
|
|
|
/**
|
|
|
|
* Mock for channel_dump_statistics(); if the channel matches the
|
|
|
|
* target, bump a counter - otherwise ignore.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
chan_test_channel_dump_statistics_mock(channel_t *chan, int severity)
|
|
|
|
{
|
2017-08-24 21:55:27 +02:00
|
|
|
tt_ptr_op(chan, OP_NE, NULL);
|
2014-01-22 13:10:15 +01:00
|
|
|
|
|
|
|
(void)severity;
|
|
|
|
|
|
|
|
if (chan != NULL && chan == dump_statistics_mock_target) {
|
|
|
|
++dump_statistics_mock_matches;
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-22 09:06:06 +01:00
|
|
|
/*
|
|
|
|
* Handle an incoming fixed-size cell for unit tests
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
2017-11-21 22:06:46 +01:00
|
|
|
chan_test_cell_handler(channel_t *chan, cell_t *cell)
|
2014-01-22 09:06:06 +01:00
|
|
|
{
|
2017-11-21 22:06:46 +01:00
|
|
|
tt_assert(chan);
|
2014-09-23 16:11:24 +02:00
|
|
|
tt_assert(cell);
|
2014-01-22 09:06:06 +01:00
|
|
|
|
2016-03-15 04:28:04 +01:00
|
|
|
test_chan_last_seen_fixed_cell_ptr = cell;
|
2014-01-22 09:06:06 +01:00
|
|
|
++test_chan_fixed_cells_recved;
|
|
|
|
|
|
|
|
done:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-23 01:23:54 +01:00
|
|
|
/*
|
|
|
|
* Fake transport-specific stats call
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
chan_test_dumpstats(channel_t *ch, int severity)
|
|
|
|
{
|
2017-08-24 21:55:27 +02:00
|
|
|
tt_ptr_op(ch, OP_NE, NULL);
|
2014-01-23 01:23:54 +01:00
|
|
|
|
|
|
|
(void)severity;
|
|
|
|
|
|
|
|
++test_dumpstats_calls;
|
|
|
|
|
|
|
|
done:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-22 09:06:06 +01:00
|
|
|
/*
|
|
|
|
* Handle an incoming variable-size cell for unit tests
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
chan_test_var_cell_handler(channel_t *ch,
|
|
|
|
var_cell_t *var_cell)
|
|
|
|
{
|
2014-09-23 16:11:24 +02:00
|
|
|
tt_assert(ch);
|
|
|
|
tt_assert(var_cell);
|
2014-01-22 09:06:06 +01:00
|
|
|
|
2016-03-15 04:28:04 +01:00
|
|
|
test_chan_last_seen_var_cell_ptr = var_cell;
|
2014-01-22 09:06:06 +01:00
|
|
|
++test_chan_var_cells_recved;
|
|
|
|
|
|
|
|
done:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-13 15:34:46 +01:00
|
|
|
static void
|
|
|
|
chan_test_close(channel_t *ch)
|
|
|
|
{
|
2014-09-23 16:11:24 +02:00
|
|
|
tt_assert(ch);
|
2013-12-13 15:34:46 +01:00
|
|
|
|
2017-11-21 22:06:46 +01:00
|
|
|
++test_close_called;
|
|
|
|
|
2013-12-13 15:34:46 +01:00
|
|
|
done:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-22 07:20:59 +01:00
|
|
|
/*
|
|
|
|
* Close a channel through the error path
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
chan_test_error(channel_t *ch)
|
|
|
|
{
|
2014-09-23 16:11:24 +02:00
|
|
|
tt_assert(ch);
|
|
|
|
tt_assert(!(ch->state == CHANNEL_STATE_CLOSING ||
|
2014-01-22 07:20:59 +01:00
|
|
|
ch->state == CHANNEL_STATE_ERROR ||
|
|
|
|
ch->state == CHANNEL_STATE_CLOSED));
|
|
|
|
|
|
|
|
channel_close_for_error(ch);
|
|
|
|
|
|
|
|
done:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Finish closing a channel from CHANNEL_STATE_CLOSING
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
chan_test_finish_close(channel_t *ch)
|
|
|
|
{
|
2014-09-23 16:11:24 +02:00
|
|
|
tt_assert(ch);
|
|
|
|
tt_assert(ch->state == CHANNEL_STATE_CLOSING);
|
2014-01-22 07:20:59 +01:00
|
|
|
|
|
|
|
channel_closed(ch);
|
|
|
|
|
|
|
|
done:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-23 01:23:54 +01:00
|
|
|
static const char *
|
|
|
|
chan_test_get_remote_descr(channel_t *ch, int flags)
|
|
|
|
{
|
2014-09-23 16:11:24 +02:00
|
|
|
tt_assert(ch);
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_int_op(flags & ~(GRD_FLAG_ORIGINAL | GRD_FLAG_ADDR_ONLY), OP_EQ, 0);
|
2014-01-23 01:23:54 +01:00
|
|
|
|
|
|
|
done:
|
|
|
|
return "Fake channel for unit tests; no real endpoint";
|
|
|
|
}
|
|
|
|
|
2013-12-18 22:42:02 +01:00
|
|
|
static double
|
|
|
|
chan_test_get_overhead_estimate(channel_t *ch)
|
|
|
|
{
|
2014-09-23 16:11:24 +02:00
|
|
|
tt_assert(ch);
|
2013-12-18 22:42:02 +01:00
|
|
|
|
|
|
|
done:
|
|
|
|
return test_overhead_estimate;
|
|
|
|
}
|
|
|
|
|
2014-01-23 01:23:54 +01:00
|
|
|
static int
|
|
|
|
chan_test_is_canonical(channel_t *ch, int req)
|
|
|
|
{
|
2017-08-24 21:55:27 +02:00
|
|
|
tt_ptr_op(ch, OP_NE, NULL);
|
2014-09-23 16:11:24 +02:00
|
|
|
tt_assert(req == 0 || req == 1);
|
2014-01-23 01:23:54 +01:00
|
|
|
|
|
|
|
done:
|
|
|
|
/* Fake channels are always canonical */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-12-13 15:34:46 +01:00
|
|
|
static size_t
|
|
|
|
chan_test_num_bytes_queued(channel_t *ch)
|
|
|
|
{
|
2014-09-23 16:11:24 +02:00
|
|
|
tt_assert(ch);
|
2013-12-13 15:34:46 +01:00
|
|
|
|
|
|
|
done:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-12-18 22:42:02 +01:00
|
|
|
static int
|
|
|
|
chan_test_num_cells_writeable(channel_t *ch)
|
|
|
|
{
|
2014-09-23 16:11:24 +02:00
|
|
|
tt_assert(ch);
|
2013-12-18 22:42:02 +01:00
|
|
|
|
|
|
|
done:
|
|
|
|
return 32;
|
|
|
|
}
|
|
|
|
|
2013-12-13 15:34:46 +01:00
|
|
|
static int
|
|
|
|
chan_test_write_cell(channel_t *ch, cell_t *cell)
|
|
|
|
{
|
|
|
|
int rv = 0;
|
|
|
|
|
2014-09-23 16:11:24 +02:00
|
|
|
tt_assert(ch);
|
|
|
|
tt_assert(cell);
|
2013-12-13 15:34:46 +01:00
|
|
|
|
|
|
|
if (test_chan_accept_cells) {
|
|
|
|
/* Free the cell and bump the counter */
|
|
|
|
tor_free(cell);
|
|
|
|
++test_cells_written;
|
|
|
|
rv = 1;
|
|
|
|
}
|
|
|
|
/* else return 0, we didn't accept it */
|
|
|
|
|
|
|
|
done:
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
chan_test_write_packed_cell(channel_t *ch,
|
|
|
|
packed_cell_t *packed_cell)
|
|
|
|
{
|
|
|
|
int rv = 0;
|
|
|
|
|
2014-09-23 16:11:24 +02:00
|
|
|
tt_assert(ch);
|
|
|
|
tt_assert(packed_cell);
|
2013-12-13 15:34:46 +01:00
|
|
|
|
|
|
|
if (test_chan_accept_cells) {
|
|
|
|
/* Free the cell and bump the counter */
|
|
|
|
packed_cell_free(packed_cell);
|
|
|
|
++test_cells_written;
|
|
|
|
rv = 1;
|
|
|
|
}
|
|
|
|
/* else return 0, we didn't accept it */
|
|
|
|
|
|
|
|
done:
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
chan_test_write_var_cell(channel_t *ch, var_cell_t *var_cell)
|
|
|
|
{
|
|
|
|
int rv = 0;
|
|
|
|
|
2014-09-23 16:11:24 +02:00
|
|
|
tt_assert(ch);
|
|
|
|
tt_assert(var_cell);
|
2013-12-13 15:34:46 +01:00
|
|
|
|
|
|
|
if (test_chan_accept_cells) {
|
|
|
|
/* Free the cell and bump the counter */
|
|
|
|
var_cell_free(var_cell);
|
|
|
|
++test_cells_written;
|
|
|
|
rv = 1;
|
|
|
|
}
|
|
|
|
/* else return 0, we didn't accept it */
|
|
|
|
|
|
|
|
done:
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-01-21 09:46:57 +01:00
|
|
|
/**
|
|
|
|
* Fill out c with a new fake cell for test suite use
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2013-12-13 15:34:46 +01:00
|
|
|
make_fake_cell(cell_t *c)
|
|
|
|
{
|
2017-08-24 21:55:27 +02:00
|
|
|
tt_ptr_op(c, OP_NE, NULL);
|
2013-12-13 15:34:46 +01:00
|
|
|
|
|
|
|
c->circ_id = 1;
|
|
|
|
c->command = CELL_RELAY;
|
|
|
|
memset(c->payload, 0, CELL_PAYLOAD_SIZE);
|
|
|
|
|
|
|
|
done:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-21 09:46:57 +01:00
|
|
|
/**
|
|
|
|
* Fill out c with a new fake var_cell for test suite use
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2013-12-13 15:34:46 +01:00
|
|
|
make_fake_var_cell(var_cell_t *c)
|
|
|
|
{
|
2017-08-24 21:55:27 +02:00
|
|
|
tt_ptr_op(c, OP_NE, NULL);
|
2013-12-13 15:34:46 +01:00
|
|
|
|
|
|
|
c->circ_id = 1;
|
|
|
|
c->command = CELL_VERSIONS;
|
|
|
|
c->payload_len = CELL_PAYLOAD_SIZE / 2;
|
|
|
|
memset(c->payload, 0, c->payload_len);
|
|
|
|
|
|
|
|
done:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-21 09:46:57 +01:00
|
|
|
/**
|
|
|
|
* Set up a new fake channel for the test suite
|
|
|
|
*/
|
|
|
|
|
|
|
|
channel_t *
|
2013-12-13 15:34:46 +01:00
|
|
|
new_fake_channel(void)
|
|
|
|
{
|
|
|
|
channel_t *chan = tor_malloc_zero(sizeof(channel_t));
|
|
|
|
channel_init(chan);
|
|
|
|
|
|
|
|
chan->close = chan_test_close;
|
2013-12-18 22:42:02 +01:00
|
|
|
chan->get_overhead_estimate = chan_test_get_overhead_estimate;
|
2015-05-26 16:24:21 +02:00
|
|
|
chan->get_remote_descr = chan_test_get_remote_descr;
|
2013-12-13 15:34:46 +01:00
|
|
|
chan->num_bytes_queued = chan_test_num_bytes_queued;
|
2013-12-18 22:42:02 +01:00
|
|
|
chan->num_cells_writeable = chan_test_num_cells_writeable;
|
2013-12-13 15:34:46 +01:00
|
|
|
chan->write_cell = chan_test_write_cell;
|
|
|
|
chan->write_packed_cell = chan_test_write_packed_cell;
|
|
|
|
chan->write_var_cell = chan_test_write_var_cell;
|
|
|
|
chan->state = CHANNEL_STATE_OPEN;
|
|
|
|
|
2017-11-21 22:06:46 +01:00
|
|
|
chan->cmux = circuitmux_alloc();
|
|
|
|
|
2013-12-13 15:34:46 +01:00
|
|
|
return chan;
|
|
|
|
}
|
|
|
|
|
2014-12-22 18:27:26 +01:00
|
|
|
void
|
|
|
|
free_fake_channel(channel_t *chan)
|
|
|
|
{
|
|
|
|
if (! chan)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (chan->cmux)
|
|
|
|
circuitmux_free(chan->cmux);
|
|
|
|
|
|
|
|
tor_free(chan);
|
|
|
|
}
|
|
|
|
|
2014-01-21 11:50:33 +01:00
|
|
|
/**
|
|
|
|
* Counter query for scheduler_channel_has_waiting_cells_mock()
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
get_mock_scheduler_has_waiting_cells_count(void)
|
|
|
|
{
|
|
|
|
return test_has_waiting_cells_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mock for scheduler_channel_has_waiting_cells()
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
|
|
|
scheduler_channel_has_waiting_cells_mock(channel_t *ch)
|
|
|
|
{
|
|
|
|
(void)ch;
|
|
|
|
|
|
|
|
/* Increment counter */
|
|
|
|
++test_has_waiting_cells_count;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-19 00:50:32 +01:00
|
|
|
static void
|
|
|
|
scheduler_channel_doesnt_want_writes_mock(channel_t *ch)
|
|
|
|
{
|
|
|
|
(void)ch;
|
|
|
|
|
|
|
|
/* Increment counter */
|
|
|
|
++test_doesnt_want_writes_count;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-21 11:50:33 +01:00
|
|
|
/**
|
|
|
|
* Counter query for scheduler_release_channel_mock()
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
get_mock_scheduler_release_channel_count(void)
|
|
|
|
{
|
|
|
|
return test_releases_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mock for scheduler_release_channel()
|
|
|
|
*/
|
|
|
|
|
2014-01-21 09:58:27 +01:00
|
|
|
void
|
2013-12-13 15:34:46 +01:00
|
|
|
scheduler_release_channel_mock(channel_t *ch)
|
|
|
|
{
|
|
|
|
(void)ch;
|
|
|
|
|
|
|
|
/* Increment counter */
|
|
|
|
++test_releases_count;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-22 13:10:15 +01:00
|
|
|
/**
|
2014-01-23 01:23:54 +01:00
|
|
|
* Test for channel_dumpstats() and limited test for
|
|
|
|
* channel_dump_statistics()
|
2014-01-22 13:10:15 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_channel_dumpstats(void *arg)
|
|
|
|
{
|
|
|
|
channel_t *ch = NULL;
|
2017-11-22 17:28:30 +01:00
|
|
|
cell_t *cell = NULL;
|
2017-11-21 18:44:46 +01:00
|
|
|
packed_cell_t *p_cell = NULL;
|
2014-01-23 01:23:54 +01:00
|
|
|
int old_count;
|
2014-01-22 13:10:15 +01:00
|
|
|
|
|
|
|
(void)arg;
|
|
|
|
|
|
|
|
/* Mock these for duration of the test */
|
|
|
|
MOCK(scheduler_channel_doesnt_want_writes,
|
|
|
|
scheduler_channel_doesnt_want_writes_mock);
|
|
|
|
MOCK(scheduler_release_channel,
|
|
|
|
scheduler_release_channel_mock);
|
|
|
|
|
|
|
|
/* Set up a new fake channel */
|
|
|
|
ch = new_fake_channel();
|
2014-09-23 16:11:24 +02:00
|
|
|
tt_assert(ch);
|
2014-01-22 13:10:15 +01:00
|
|
|
|
|
|
|
/* Try to register it */
|
|
|
|
channel_register(ch);
|
2014-09-23 16:11:24 +02:00
|
|
|
tt_assert(ch->registered);
|
2014-01-22 13:10:15 +01:00
|
|
|
|
|
|
|
/* Set up mock */
|
|
|
|
dump_statistics_mock_target = ch;
|
|
|
|
dump_statistics_mock_matches = 0;
|
|
|
|
MOCK(channel_dump_statistics,
|
|
|
|
chan_test_channel_dump_statistics_mock);
|
|
|
|
|
|
|
|
/* Call channel_dumpstats() */
|
|
|
|
channel_dumpstats(LOG_DEBUG);
|
|
|
|
|
|
|
|
/* Assert that we hit the mock */
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_int_op(dump_statistics_mock_matches, OP_EQ, 1);
|
2014-01-22 13:10:15 +01:00
|
|
|
|
|
|
|
/* Close the channel */
|
|
|
|
channel_mark_for_close(ch);
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING);
|
2014-01-22 13:10:15 +01:00
|
|
|
chan_test_finish_close(ch);
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSED);
|
2014-01-22 13:10:15 +01:00
|
|
|
|
|
|
|
/* Try again and hit the finished channel */
|
|
|
|
channel_dumpstats(LOG_DEBUG);
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_int_op(dump_statistics_mock_matches, OP_EQ, 2);
|
2014-01-22 13:10:15 +01:00
|
|
|
|
|
|
|
channel_run_cleanup();
|
|
|
|
ch = NULL;
|
|
|
|
|
|
|
|
/* Now we should hit nothing */
|
|
|
|
channel_dumpstats(LOG_DEBUG);
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_int_op(dump_statistics_mock_matches, OP_EQ, 2);
|
2014-01-22 13:10:15 +01:00
|
|
|
|
|
|
|
/* Unmock */
|
|
|
|
UNMOCK(channel_dump_statistics);
|
|
|
|
dump_statistics_mock_target = NULL;
|
|
|
|
dump_statistics_mock_matches = 0;
|
|
|
|
|
2014-01-23 01:23:54 +01:00
|
|
|
/* Now make another channel */
|
|
|
|
ch = new_fake_channel();
|
2014-09-23 16:11:24 +02:00
|
|
|
tt_assert(ch);
|
2014-01-23 01:23:54 +01:00
|
|
|
channel_register(ch);
|
2017-11-22 17:28:30 +01:00
|
|
|
tt_int_op(ch->registered, OP_EQ, 1);
|
2014-01-23 01:23:54 +01:00
|
|
|
/* Lie about its age so dumpstats gets coverage for rate calculations */
|
|
|
|
ch->timestamp_created = time(NULL) - 30;
|
2017-11-22 17:28:30 +01:00
|
|
|
tt_int_op(ch->timestamp_created, OP_GT, 0);
|
|
|
|
tt_int_op(time(NULL), OP_GT, ch->timestamp_created);
|
2014-01-23 01:23:54 +01:00
|
|
|
|
|
|
|
/* Put cells through it both ways to make the counters non-zero */
|
2017-11-21 18:44:46 +01:00
|
|
|
p_cell = packed_cell_new();
|
2014-01-23 01:23:54 +01:00
|
|
|
test_chan_accept_cells = 1;
|
|
|
|
old_count = test_cells_written;
|
2017-11-21 18:44:46 +01:00
|
|
|
channel_write_packed_cell(ch, p_cell);
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_int_op(test_cells_written, OP_EQ, old_count + 1);
|
2017-11-22 17:28:30 +01:00
|
|
|
tt_u64_op(ch->n_bytes_xmitted, OP_GT, 0);
|
|
|
|
tt_u64_op(ch->n_cells_xmitted, OP_GT, 0);
|
2014-01-23 01:23:54 +01:00
|
|
|
|
|
|
|
/* Receive path */
|
|
|
|
channel_set_cell_handlers(ch,
|
|
|
|
chan_test_cell_handler,
|
|
|
|
chan_test_var_cell_handler);
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_ptr_op(channel_get_cell_handler(ch), OP_EQ, chan_test_cell_handler);
|
2017-06-05 16:38:20 +02:00
|
|
|
tt_ptr_op(channel_get_var_cell_handler(ch), OP_EQ,
|
|
|
|
chan_test_var_cell_handler);
|
2017-11-22 17:28:30 +01:00
|
|
|
cell = tor_malloc_zero(sizeof(*cell));
|
2014-01-23 01:23:54 +01:00
|
|
|
old_count = test_chan_fixed_cells_recved;
|
2017-11-22 17:28:30 +01:00
|
|
|
channel_process_cell(ch, cell);
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_int_op(test_chan_fixed_cells_recved, OP_EQ, old_count + 1);
|
2017-11-22 17:28:30 +01:00
|
|
|
tt_u64_op(ch->n_bytes_recved, OP_GT, 0);
|
|
|
|
tt_u64_op(ch->n_cells_recved, OP_GT, 0);
|
2014-01-23 01:23:54 +01:00
|
|
|
|
|
|
|
/* Test channel_dump_statistics */
|
|
|
|
ch->describe_transport = chan_test_describe_transport;
|
|
|
|
ch->dumpstats = chan_test_dumpstats;
|
|
|
|
ch->is_canonical = chan_test_is_canonical;
|
|
|
|
old_count = test_dumpstats_calls;
|
|
|
|
channel_dump_statistics(ch, LOG_DEBUG);
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_int_op(test_dumpstats_calls, OP_EQ, old_count + 1);
|
2014-01-23 01:23:54 +01:00
|
|
|
|
|
|
|
/* Close the channel */
|
|
|
|
channel_mark_for_close(ch);
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING);
|
2014-01-23 01:23:54 +01:00
|
|
|
chan_test_finish_close(ch);
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSED);
|
2014-01-23 01:23:54 +01:00
|
|
|
channel_run_cleanup();
|
|
|
|
ch = NULL;
|
|
|
|
|
2014-01-22 13:10:15 +01:00
|
|
|
done:
|
2014-12-22 18:27:26 +01:00
|
|
|
free_fake_channel(ch);
|
2017-11-22 17:28:30 +01:00
|
|
|
tor_free(cell);
|
2014-01-22 13:10:15 +01:00
|
|
|
|
|
|
|
UNMOCK(scheduler_channel_doesnt_want_writes);
|
|
|
|
UNMOCK(scheduler_release_channel);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-22 17:17:21 +01:00
|
|
|
/* Test outbound cell. The callstack is:
|
|
|
|
* channel_flush_some_cells()
|
|
|
|
* -> channel_flush_from_first_active_circuit()
|
|
|
|
* -> channel_write_packed_cell()
|
|
|
|
* -> write_packed_cell()
|
|
|
|
* -> chan->write_packed_cell() fct ptr.
|
|
|
|
*
|
|
|
|
* This test goes from a cell in a circuit up to the channel write handler
|
|
|
|
* that should put them on the connection outbuf. */
|
|
|
|
static void
|
|
|
|
test_channel_outbound_cell(void *arg)
|
|
|
|
{
|
|
|
|
int old_count;
|
|
|
|
channel_t *chan = NULL;
|
|
|
|
packed_cell_t *p_cell = NULL, *p_cell2 = NULL;
|
|
|
|
origin_circuit_t *circ = NULL;
|
|
|
|
cell_queue_t *queue;
|
|
|
|
|
|
|
|
(void) arg;
|
|
|
|
|
|
|
|
/* The channel will be freed so we need to hijack this so the scheduler
|
|
|
|
* doesn't get confused. */
|
|
|
|
MOCK(scheduler_release_channel, scheduler_release_channel_mock);
|
|
|
|
|
|
|
|
/* Accept cells to lower layer */
|
|
|
|
test_chan_accept_cells = 1;
|
|
|
|
/* Use default overhead factor */
|
|
|
|
test_overhead_estimate = 1.0;
|
|
|
|
|
|
|
|
/* Setup a valid circuit to queue a cell. */
|
|
|
|
circ = origin_circuit_new();
|
|
|
|
tt_assert(circ);
|
|
|
|
/* Circuit needs an origin purpose to be considered origin. */
|
|
|
|
TO_CIRCUIT(circ)->purpose = CIRCUIT_PURPOSE_C_GENERAL;
|
|
|
|
TO_CIRCUIT(circ)->n_circ_id = 42;
|
|
|
|
/* This is the outbound test so use the next channel queue. */
|
|
|
|
queue = &TO_CIRCUIT(circ)->n_chan_cells;
|
|
|
|
/* Setup packed cell to queue on the circuit. */
|
|
|
|
p_cell = packed_cell_new();
|
|
|
|
tt_assert(p_cell);
|
|
|
|
p_cell2 = packed_cell_new();
|
|
|
|
tt_assert(p_cell2);
|
|
|
|
/* Setup a channel to put the circuit on. */
|
|
|
|
chan = new_fake_channel();
|
|
|
|
tt_assert(chan);
|
|
|
|
chan->state = CHANNEL_STATE_OPENING;
|
|
|
|
channel_change_state_open(chan);
|
|
|
|
/* Outbound channel. */
|
|
|
|
channel_mark_outgoing(chan);
|
|
|
|
/* Try to register it so we can clean it through the channel cleanup
|
|
|
|
* process. */
|
|
|
|
channel_register(chan);
|
|
|
|
tt_int_op(chan->registered, OP_EQ, 1);
|
|
|
|
/* Set EWMA policy so we can pick it when flushing. */
|
|
|
|
channel_set_cmux_policy_everywhere(&ewma_policy);
|
|
|
|
tt_ptr_op(circuitmux_get_policy(chan->cmux), OP_EQ, &ewma_policy);
|
|
|
|
|
|
|
|
/* Register circuit to the channel circid map which will attach the circuit
|
|
|
|
* to the channel's cmux as well. */
|
|
|
|
circuit_set_n_circid_chan(TO_CIRCUIT(circ), 42, chan);
|
|
|
|
tt_int_op(channel_num_circuits(chan), OP_EQ, 1);
|
|
|
|
tt_assert(!TO_CIRCUIT(circ)->next_active_on_n_chan);
|
|
|
|
tt_assert(!TO_CIRCUIT(circ)->prev_active_on_n_chan);
|
|
|
|
/* Test the cmux state. */
|
|
|
|
tt_ptr_op(TO_CIRCUIT(circ)->n_mux, OP_EQ, chan->cmux);
|
|
|
|
tt_int_op(circuitmux_is_circuit_attached(chan->cmux, TO_CIRCUIT(circ)),
|
|
|
|
OP_EQ, 1);
|
|
|
|
|
|
|
|
/* Flush the channel without any cell on it. */
|
|
|
|
old_count = test_cells_written;
|
|
|
|
ssize_t flushed = channel_flush_some_cells(chan, 1);
|
|
|
|
tt_i64_op(flushed, OP_EQ, 0);
|
|
|
|
tt_int_op(test_cells_written, OP_EQ, old_count);
|
|
|
|
tt_int_op(channel_more_to_flush(chan), OP_EQ, 0);
|
|
|
|
tt_int_op(circuitmux_num_active_circuits(chan->cmux), OP_EQ, 0);
|
|
|
|
tt_int_op(circuitmux_num_cells(chan->cmux), OP_EQ, 0);
|
|
|
|
tt_int_op(circuitmux_is_circuit_active(chan->cmux, TO_CIRCUIT(circ)),
|
|
|
|
OP_EQ, 0);
|
|
|
|
tt_u64_op(chan->n_cells_xmitted, OP_EQ, 0);
|
|
|
|
tt_u64_op(chan->n_bytes_xmitted, OP_EQ, 0);
|
|
|
|
|
|
|
|
/* Queue cell onto the next queue that is the outbound direction. Than
|
|
|
|
* update its cmux so the circuit can be picked when flushing cells. */
|
|
|
|
cell_queue_append(queue, p_cell);
|
|
|
|
p_cell = NULL;
|
|
|
|
tt_int_op(queue->n, OP_EQ, 1);
|
|
|
|
cell_queue_append(queue, p_cell2);
|
|
|
|
p_cell2 = NULL;
|
|
|
|
tt_int_op(queue->n, OP_EQ, 2);
|
|
|
|
|
|
|
|
update_circuit_on_cmux(TO_CIRCUIT(circ), CELL_DIRECTION_OUT);
|
|
|
|
tt_int_op(circuitmux_num_active_circuits(chan->cmux), OP_EQ, 1);
|
|
|
|
tt_int_op(circuitmux_num_cells(chan->cmux), OP_EQ, 2);
|
|
|
|
tt_int_op(circuitmux_is_circuit_active(chan->cmux, TO_CIRCUIT(circ)),
|
|
|
|
OP_EQ, 1);
|
|
|
|
|
|
|
|
/* From this point on, we have a queued cell on an active circuit attached
|
|
|
|
* to the channel's cmux. */
|
|
|
|
|
|
|
|
/* Flush the first cell. This is going to go down the call stack. */
|
|
|
|
old_count = test_cells_written;
|
|
|
|
flushed = channel_flush_some_cells(chan, 1);
|
|
|
|
tt_i64_op(flushed, OP_EQ, 1);
|
|
|
|
tt_int_op(test_cells_written, OP_EQ, old_count + 1);
|
|
|
|
tt_int_op(circuitmux_num_cells(chan->cmux), OP_EQ, 1);
|
|
|
|
tt_int_op(channel_more_to_flush(chan), OP_EQ, 1);
|
|
|
|
/* Circuit should remain active because there is a second cell queued. */
|
|
|
|
tt_int_op(circuitmux_is_circuit_active(chan->cmux, TO_CIRCUIT(circ)),
|
|
|
|
OP_EQ, 1);
|
|
|
|
/* Should still be attached. */
|
|
|
|
tt_int_op(circuitmux_is_circuit_attached(chan->cmux, TO_CIRCUIT(circ)),
|
|
|
|
OP_EQ, 1);
|
|
|
|
tt_u64_op(chan->n_cells_xmitted, OP_EQ, 1);
|
|
|
|
tt_u64_op(chan->n_bytes_xmitted, OP_EQ, get_cell_network_size(0));
|
|
|
|
|
|
|
|
/* Flush second cell. This is going to go down the call stack. */
|
|
|
|
old_count = test_cells_written;
|
|
|
|
flushed = channel_flush_some_cells(chan, 1);
|
|
|
|
tt_i64_op(flushed, OP_EQ, 1);
|
|
|
|
tt_int_op(test_cells_written, OP_EQ, old_count + 1);
|
|
|
|
tt_int_op(circuitmux_num_cells(chan->cmux), OP_EQ, 0);
|
|
|
|
tt_int_op(channel_more_to_flush(chan), OP_EQ, 0);
|
|
|
|
/* No more cells should make the circuit inactive. */
|
|
|
|
tt_int_op(circuitmux_is_circuit_active(chan->cmux, TO_CIRCUIT(circ)),
|
|
|
|
OP_EQ, 0);
|
|
|
|
/* Should still be attached. */
|
|
|
|
tt_int_op(circuitmux_is_circuit_attached(chan->cmux, TO_CIRCUIT(circ)),
|
|
|
|
OP_EQ, 1);
|
|
|
|
tt_u64_op(chan->n_cells_xmitted, OP_EQ, 2);
|
|
|
|
tt_u64_op(chan->n_bytes_xmitted, OP_EQ, get_cell_network_size(0) * 2);
|
|
|
|
|
|
|
|
done:
|
|
|
|
if (circ) {
|
|
|
|
circuit_free(TO_CIRCUIT(circ));
|
|
|
|
}
|
|
|
|
tor_free(p_cell);
|
|
|
|
channel_free_all();
|
|
|
|
UNMOCK(scheduler_release_channel);
|
|
|
|
}
|
|
|
|
|
2017-11-21 22:06:46 +01:00
|
|
|
/* Test inbound cell. The callstack is:
|
|
|
|
* channel_process_cell()
|
|
|
|
* -> chan->cell_handler()
|
|
|
|
*
|
|
|
|
* This test is about checking if we can process an inbound cell down to the
|
|
|
|
* channel handler. */
|
2014-01-22 09:06:06 +01:00
|
|
|
static void
|
2017-11-21 22:06:46 +01:00
|
|
|
test_channel_inbound_cell(void *arg)
|
2014-01-22 09:06:06 +01:00
|
|
|
{
|
2017-11-21 22:06:46 +01:00
|
|
|
channel_t *chan = NULL;
|
2014-01-22 09:06:06 +01:00
|
|
|
cell_t *cell = NULL;
|
|
|
|
int old_count;
|
|
|
|
|
2017-11-21 22:06:46 +01:00
|
|
|
(void) arg;
|
2014-01-22 09:06:06 +01:00
|
|
|
|
2017-11-21 22:06:46 +01:00
|
|
|
/* The channel will be freed so we need to hijack this so the scheduler
|
|
|
|
* doesn't get confused. */
|
|
|
|
MOCK(scheduler_release_channel, scheduler_release_channel_mock);
|
2014-01-22 09:06:06 +01:00
|
|
|
|
|
|
|
/* Accept cells to lower layer */
|
|
|
|
test_chan_accept_cells = 1;
|
|
|
|
/* Use default overhead factor */
|
2016-05-30 19:57:32 +02:00
|
|
|
test_overhead_estimate = 1.0;
|
2014-01-22 09:06:06 +01:00
|
|
|
|
2017-11-21 22:06:46 +01:00
|
|
|
chan = new_fake_channel();
|
|
|
|
tt_assert(chan);
|
2014-01-22 09:06:06 +01:00
|
|
|
/* Start it off in OPENING */
|
2017-11-21 22:06:46 +01:00
|
|
|
chan->state = CHANNEL_STATE_OPENING;
|
2014-01-22 09:06:06 +01:00
|
|
|
|
|
|
|
/* Try to register it */
|
2017-11-21 22:06:46 +01:00
|
|
|
channel_register(chan);
|
|
|
|
tt_int_op(chan->registered, OP_EQ, 1);
|
2014-01-22 09:06:06 +01:00
|
|
|
|
|
|
|
/* Open it */
|
2017-11-21 22:06:46 +01:00
|
|
|
channel_change_state_open(chan);
|
|
|
|
tt_int_op(chan->state, OP_EQ, CHANNEL_STATE_OPEN);
|
|
|
|
tt_int_op(chan->has_been_open, OP_EQ, 1);
|
2014-01-22 09:06:06 +01:00
|
|
|
|
2017-11-21 22:06:46 +01:00
|
|
|
/* Receive a cell now. */
|
|
|
|
cell = tor_malloc_zero(sizeof(*cell));
|
2014-01-22 09:06:06 +01:00
|
|
|
make_fake_cell(cell);
|
|
|
|
old_count = test_chan_fixed_cells_recved;
|
2017-11-21 22:06:46 +01:00
|
|
|
channel_process_cell(chan, cell);
|
|
|
|
tt_int_op(test_chan_fixed_cells_recved, OP_EQ, old_count);
|
|
|
|
tt_u64_op(chan->timestamp_xfer_ms, OP_EQ, 0);
|
|
|
|
tt_u64_op(chan->timestamp_active, OP_EQ, 0);
|
|
|
|
tt_u64_op(chan->timestamp_recv, OP_EQ, 0);
|
|
|
|
|
|
|
|
/* Setup incoming cell handlers. We don't care about var cell, the channel
|
|
|
|
* layers is not handling those. */
|
|
|
|
channel_set_cell_handlers(chan, chan_test_cell_handler, NULL);
|
|
|
|
tt_ptr_op(chan->cell_handler, OP_EQ, chan_test_cell_handler);
|
|
|
|
/* Now process the cell, we should see it. */
|
|
|
|
old_count = test_chan_fixed_cells_recved;
|
|
|
|
channel_process_cell(chan, cell);
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_int_op(test_chan_fixed_cells_recved, OP_EQ, old_count + 1);
|
2017-11-21 22:06:46 +01:00
|
|
|
/* We should have a series of timestamp set. */
|
|
|
|
tt_u64_op(chan->timestamp_xfer_ms, OP_NE, 0);
|
|
|
|
tt_u64_op(chan->timestamp_active, OP_NE, 0);
|
|
|
|
tt_u64_op(chan->timestamp_recv, OP_NE, 0);
|
|
|
|
tt_u64_op(chan->next_padding_time_ms, OP_EQ, 0);
|
|
|
|
tt_u64_op(chan->n_cells_recved, OP_EQ, 1);
|
|
|
|
tt_u64_op(chan->n_bytes_recved, OP_EQ, get_cell_network_size(0));
|
2014-01-22 09:06:06 +01:00
|
|
|
|
|
|
|
/* Close it */
|
2017-11-21 22:06:46 +01:00
|
|
|
old_count = test_close_called;
|
|
|
|
channel_mark_for_close(chan);
|
|
|
|
tt_int_op(chan->state, OP_EQ, CHANNEL_STATE_CLOSING);
|
|
|
|
tt_int_op(chan->reason_for_closing, OP_EQ, CHANNEL_CLOSE_REQUESTED);
|
|
|
|
tt_int_op(test_close_called, OP_EQ, old_count + 1);
|
|
|
|
|
|
|
|
/* This closes the channe so it calls in the scheduler, make sure of it. */
|
|
|
|
old_count = test_releases_count;
|
|
|
|
chan_test_finish_close(chan);
|
|
|
|
tt_int_op(test_releases_count, OP_EQ, old_count + 1);
|
|
|
|
tt_int_op(chan->state, OP_EQ, CHANNEL_STATE_CLOSED);
|
|
|
|
|
|
|
|
/* The channel will be free, lets make sure it is not accessible. */
|
|
|
|
uint64_t chan_id = chan->global_identifier;
|
|
|
|
tt_ptr_op(channel_find_by_global_id(chan_id), OP_EQ, chan);
|
2014-01-22 09:06:06 +01:00
|
|
|
channel_run_cleanup();
|
2017-11-21 22:06:46 +01:00
|
|
|
chan = channel_find_by_global_id(chan_id);
|
|
|
|
tt_assert(chan == NULL);
|
2014-01-22 09:06:06 +01:00
|
|
|
|
|
|
|
done:
|
|
|
|
tor_free(cell);
|
|
|
|
UNMOCK(scheduler_release_channel);
|
|
|
|
}
|
|
|
|
|
2014-01-22 07:20:59 +01:00
|
|
|
/**
|
|
|
|
* Normal channel lifecycle test:
|
|
|
|
*
|
|
|
|
* OPENING->OPEN->MAINT->OPEN->CLOSING->CLOSED
|
|
|
|
*/
|
|
|
|
|
2013-12-19 00:50:32 +01:00
|
|
|
static void
|
|
|
|
test_channel_lifecycle(void *arg)
|
|
|
|
{
|
|
|
|
channel_t *ch1 = NULL, *ch2 = NULL;
|
2017-11-21 18:44:46 +01:00
|
|
|
packed_cell_t *p_cell = NULL;
|
2013-12-19 00:50:32 +01:00
|
|
|
int old_count, init_doesnt_want_writes_count;
|
|
|
|
int init_releases_count;
|
|
|
|
|
|
|
|
(void)arg;
|
|
|
|
|
|
|
|
/* Mock these for the whole lifecycle test */
|
|
|
|
MOCK(scheduler_channel_doesnt_want_writes,
|
|
|
|
scheduler_channel_doesnt_want_writes_mock);
|
|
|
|
MOCK(scheduler_release_channel,
|
|
|
|
scheduler_release_channel_mock);
|
|
|
|
|
|
|
|
/* Cache some initial counter values */
|
|
|
|
init_doesnt_want_writes_count = test_doesnt_want_writes_count;
|
|
|
|
init_releases_count = test_releases_count;
|
|
|
|
|
|
|
|
/* Accept cells to lower layer */
|
|
|
|
test_chan_accept_cells = 1;
|
|
|
|
/* Use default overhead factor */
|
2016-05-30 19:57:32 +02:00
|
|
|
test_overhead_estimate = 1.0;
|
2013-12-19 00:50:32 +01:00
|
|
|
|
|
|
|
ch1 = new_fake_channel();
|
2014-09-23 16:11:24 +02:00
|
|
|
tt_assert(ch1);
|
2013-12-19 00:50:32 +01:00
|
|
|
/* Start it off in OPENING */
|
|
|
|
ch1->state = CHANNEL_STATE_OPENING;
|
|
|
|
|
|
|
|
/* Try to register it */
|
|
|
|
channel_register(ch1);
|
2014-09-23 16:11:24 +02:00
|
|
|
tt_assert(ch1->registered);
|
2013-12-19 00:50:32 +01:00
|
|
|
|
|
|
|
/* Try to write a cell through (should queue) */
|
2017-11-21 18:44:46 +01:00
|
|
|
p_cell = packed_cell_new();
|
2013-12-19 00:50:32 +01:00
|
|
|
old_count = test_cells_written;
|
2017-11-21 18:44:46 +01:00
|
|
|
channel_write_packed_cell(ch1, p_cell);
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_int_op(old_count, OP_EQ, test_cells_written);
|
2013-12-19 00:50:32 +01:00
|
|
|
|
|
|
|
/* Move it to OPEN and flush */
|
2017-03-26 19:33:44 +02:00
|
|
|
channel_change_state_open(ch1);
|
2013-12-19 00:50:32 +01:00
|
|
|
|
2017-11-22 17:21:29 +01:00
|
|
|
/* Get another one */
|
2013-12-19 00:50:32 +01:00
|
|
|
ch2 = new_fake_channel();
|
2014-09-23 16:11:24 +02:00
|
|
|
tt_assert(ch2);
|
2013-12-19 00:50:32 +01:00
|
|
|
ch2->state = CHANNEL_STATE_OPENING;
|
|
|
|
|
|
|
|
/* Register */
|
|
|
|
channel_register(ch2);
|
2014-09-23 16:11:24 +02:00
|
|
|
tt_assert(ch2->registered);
|
2013-12-19 00:50:32 +01:00
|
|
|
|
|
|
|
/* Check counters */
|
2017-06-05 16:38:20 +02:00
|
|
|
tt_int_op(test_doesnt_want_writes_count, OP_EQ,
|
|
|
|
init_doesnt_want_writes_count);
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_int_op(test_releases_count, OP_EQ, init_releases_count);
|
2013-12-19 00:50:32 +01:00
|
|
|
|
|
|
|
/* Move ch1 to MAINT */
|
|
|
|
channel_change_state(ch1, CHANNEL_STATE_MAINT);
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_int_op(test_doesnt_want_writes_count, OP_EQ,
|
2014-09-23 16:11:24 +02:00
|
|
|
init_doesnt_want_writes_count + 1);
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_int_op(test_releases_count, OP_EQ, init_releases_count);
|
2013-12-19 00:50:32 +01:00
|
|
|
|
|
|
|
/* Move ch2 to OPEN */
|
2017-03-26 19:33:44 +02:00
|
|
|
channel_change_state_open(ch2);
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_int_op(test_doesnt_want_writes_count, OP_EQ,
|
2014-09-23 16:11:24 +02:00
|
|
|
init_doesnt_want_writes_count + 1);
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_int_op(test_releases_count, OP_EQ, init_releases_count);
|
2013-12-19 00:50:32 +01:00
|
|
|
|
|
|
|
/* Move ch1 back to OPEN */
|
2017-03-26 19:33:44 +02:00
|
|
|
channel_change_state_open(ch1);
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_int_op(test_doesnt_want_writes_count, OP_EQ,
|
2014-09-23 16:11:24 +02:00
|
|
|
init_doesnt_want_writes_count + 1);
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_int_op(test_releases_count, OP_EQ, init_releases_count);
|
2013-12-19 00:50:32 +01:00
|
|
|
|
|
|
|
/* Mark ch2 for close */
|
|
|
|
channel_mark_for_close(ch2);
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_int_op(ch2->state, OP_EQ, CHANNEL_STATE_CLOSING);
|
|
|
|
tt_int_op(test_doesnt_want_writes_count, OP_EQ,
|
2014-09-23 16:11:24 +02:00
|
|
|
init_doesnt_want_writes_count + 1);
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_int_op(test_releases_count, OP_EQ, init_releases_count + 1);
|
2013-12-19 00:50:32 +01:00
|
|
|
|
|
|
|
/* Shut down channels */
|
|
|
|
channel_free_all();
|
|
|
|
ch1 = ch2 = NULL;
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_int_op(test_doesnt_want_writes_count, OP_EQ,
|
2014-09-23 16:11:24 +02:00
|
|
|
init_doesnt_want_writes_count + 1);
|
2013-12-19 00:50:32 +01:00
|
|
|
/* channel_free() calls scheduler_release_channel() */
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_int_op(test_releases_count, OP_EQ, init_releases_count + 4);
|
2013-12-19 00:50:32 +01:00
|
|
|
|
|
|
|
done:
|
2014-12-22 18:27:26 +01:00
|
|
|
free_fake_channel(ch1);
|
|
|
|
free_fake_channel(ch2);
|
2017-11-22 17:21:29 +01:00
|
|
|
tor_free(p_cell);
|
2013-12-19 00:50:32 +01:00
|
|
|
|
|
|
|
UNMOCK(scheduler_channel_doesnt_want_writes);
|
|
|
|
UNMOCK(scheduler_release_channel);
|
|
|
|
}
|
|
|
|
|
2014-01-22 07:20:59 +01:00
|
|
|
/**
|
|
|
|
* Weird channel lifecycle test:
|
|
|
|
*
|
|
|
|
* OPENING->CLOSING->CLOSED
|
|
|
|
* OPENING->OPEN->CLOSING->ERROR
|
|
|
|
* OPENING->OPEN->MAINT->CLOSING->CLOSED
|
|
|
|
* OPENING->OPEN->MAINT->CLOSING->ERROR
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_channel_lifecycle_2(void *arg)
|
|
|
|
{
|
|
|
|
channel_t *ch = NULL;
|
|
|
|
|
|
|
|
(void)arg;
|
|
|
|
|
|
|
|
/* Mock these for the whole lifecycle test */
|
|
|
|
MOCK(scheduler_channel_doesnt_want_writes,
|
|
|
|
scheduler_channel_doesnt_want_writes_mock);
|
|
|
|
MOCK(scheduler_release_channel,
|
|
|
|
scheduler_release_channel_mock);
|
|
|
|
|
|
|
|
/* Accept cells to lower layer */
|
|
|
|
test_chan_accept_cells = 1;
|
|
|
|
/* Use default overhead factor */
|
2016-05-30 19:57:32 +02:00
|
|
|
test_overhead_estimate = 1.0;
|
2014-01-22 07:20:59 +01:00
|
|
|
|
|
|
|
ch = new_fake_channel();
|
2014-09-23 16:11:24 +02:00
|
|
|
tt_assert(ch);
|
2014-01-22 07:20:59 +01:00
|
|
|
/* Start it off in OPENING */
|
|
|
|
ch->state = CHANNEL_STATE_OPENING;
|
|
|
|
|
|
|
|
/* Try to register it */
|
|
|
|
channel_register(ch);
|
2014-09-23 16:11:24 +02:00
|
|
|
tt_assert(ch->registered);
|
2014-01-22 07:20:59 +01:00
|
|
|
|
|
|
|
/* Try to close it */
|
|
|
|
channel_mark_for_close(ch);
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING);
|
2014-01-22 07:20:59 +01:00
|
|
|
|
|
|
|
/* Finish closing it */
|
|
|
|
chan_test_finish_close(ch);
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSED);
|
2014-01-22 07:20:59 +01:00
|
|
|
channel_run_cleanup();
|
|
|
|
ch = NULL;
|
|
|
|
|
|
|
|
/* Now try OPENING->OPEN->CLOSING->ERROR */
|
|
|
|
ch = new_fake_channel();
|
2014-09-23 16:11:24 +02:00
|
|
|
tt_assert(ch);
|
2014-01-22 07:20:59 +01:00
|
|
|
ch->state = CHANNEL_STATE_OPENING;
|
|
|
|
channel_register(ch);
|
2014-09-23 16:11:24 +02:00
|
|
|
tt_assert(ch->registered);
|
2014-01-22 07:20:59 +01:00
|
|
|
|
|
|
|
/* Finish opening it */
|
2017-03-26 19:33:44 +02:00
|
|
|
channel_change_state_open(ch);
|
2014-01-22 07:20:59 +01:00
|
|
|
|
|
|
|
/* Error exit from lower layer */
|
|
|
|
chan_test_error(ch);
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING);
|
2014-01-22 07:20:59 +01:00
|
|
|
chan_test_finish_close(ch);
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_ERROR);
|
2014-01-22 07:20:59 +01:00
|
|
|
channel_run_cleanup();
|
|
|
|
ch = NULL;
|
|
|
|
|
|
|
|
/* OPENING->OPEN->MAINT->CLOSING->CLOSED close from maintenance state */
|
|
|
|
ch = new_fake_channel();
|
2014-09-23 16:11:24 +02:00
|
|
|
tt_assert(ch);
|
2014-01-22 07:20:59 +01:00
|
|
|
ch->state = CHANNEL_STATE_OPENING;
|
|
|
|
channel_register(ch);
|
2014-09-23 16:11:24 +02:00
|
|
|
tt_assert(ch->registered);
|
2014-01-22 07:20:59 +01:00
|
|
|
|
|
|
|
/* Finish opening it */
|
2017-03-26 19:33:44 +02:00
|
|
|
channel_change_state_open(ch);
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_OPEN);
|
2014-01-22 07:20:59 +01:00
|
|
|
|
|
|
|
/* Go to maintenance state */
|
|
|
|
channel_change_state(ch, CHANNEL_STATE_MAINT);
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_MAINT);
|
2014-01-22 07:20:59 +01:00
|
|
|
|
|
|
|
/* Lower layer close */
|
|
|
|
channel_mark_for_close(ch);
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING);
|
2014-01-22 07:20:59 +01:00
|
|
|
|
|
|
|
/* Finish */
|
|
|
|
chan_test_finish_close(ch);
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSED);
|
2014-01-22 07:20:59 +01:00
|
|
|
channel_run_cleanup();
|
|
|
|
ch = NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* OPENING->OPEN->MAINT->CLOSING->CLOSED lower-layer close during
|
|
|
|
* maintenance state
|
|
|
|
*/
|
|
|
|
ch = new_fake_channel();
|
2014-09-23 16:11:24 +02:00
|
|
|
tt_assert(ch);
|
2014-01-22 07:20:59 +01:00
|
|
|
ch->state = CHANNEL_STATE_OPENING;
|
|
|
|
channel_register(ch);
|
2014-09-23 16:11:24 +02:00
|
|
|
tt_assert(ch->registered);
|
2014-01-22 07:20:59 +01:00
|
|
|
|
|
|
|
/* Finish opening it */
|
2017-03-26 19:33:44 +02:00
|
|
|
channel_change_state_open(ch);
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_OPEN);
|
2014-01-22 07:20:59 +01:00
|
|
|
|
|
|
|
/* Go to maintenance state */
|
|
|
|
channel_change_state(ch, CHANNEL_STATE_MAINT);
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_MAINT);
|
2014-01-22 07:20:59 +01:00
|
|
|
|
|
|
|
/* Lower layer close */
|
|
|
|
channel_close_from_lower_layer(ch);
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING);
|
2014-01-22 07:20:59 +01:00
|
|
|
|
|
|
|
/* Finish */
|
|
|
|
chan_test_finish_close(ch);
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSED);
|
2014-01-22 07:20:59 +01:00
|
|
|
channel_run_cleanup();
|
|
|
|
ch = NULL;
|
|
|
|
|
|
|
|
/* OPENING->OPEN->MAINT->CLOSING->ERROR */
|
|
|
|
ch = new_fake_channel();
|
2014-09-23 16:11:24 +02:00
|
|
|
tt_assert(ch);
|
2014-01-22 07:20:59 +01:00
|
|
|
ch->state = CHANNEL_STATE_OPENING;
|
|
|
|
channel_register(ch);
|
2014-09-23 16:11:24 +02:00
|
|
|
tt_assert(ch->registered);
|
2014-01-22 07:20:59 +01:00
|
|
|
|
|
|
|
/* Finish opening it */
|
2017-03-26 19:33:44 +02:00
|
|
|
channel_change_state_open(ch);
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_OPEN);
|
2014-01-22 07:20:59 +01:00
|
|
|
|
|
|
|
/* Go to maintenance state */
|
|
|
|
channel_change_state(ch, CHANNEL_STATE_MAINT);
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_MAINT);
|
2014-01-22 07:20:59 +01:00
|
|
|
|
|
|
|
/* Lower layer close */
|
|
|
|
chan_test_error(ch);
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING);
|
2014-01-22 07:20:59 +01:00
|
|
|
|
|
|
|
/* Finish */
|
|
|
|
chan_test_finish_close(ch);
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_ERROR);
|
2014-01-22 07:20:59 +01:00
|
|
|
channel_run_cleanup();
|
|
|
|
ch = NULL;
|
|
|
|
|
|
|
|
/* Shut down channels */
|
|
|
|
channel_free_all();
|
|
|
|
|
|
|
|
done:
|
|
|
|
tor_free(ch);
|
|
|
|
|
|
|
|
UNMOCK(scheduler_channel_doesnt_want_writes);
|
|
|
|
UNMOCK(scheduler_release_channel);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-11 19:09:25 +01:00
|
|
|
static void
|
|
|
|
test_channel_id_map(void *arg)
|
|
|
|
{
|
|
|
|
(void)arg;
|
2016-12-08 23:50:01 +01:00
|
|
|
#define N_CHAN 6
|
2016-11-11 19:09:25 +01:00
|
|
|
char rsa_id[N_CHAN][DIGEST_LEN];
|
|
|
|
ed25519_public_key_t *ed_id[N_CHAN];
|
|
|
|
channel_t *chan[N_CHAN];
|
|
|
|
int i;
|
|
|
|
ed25519_public_key_t ed_zero;
|
|
|
|
memset(&ed_zero, 0, sizeof(ed_zero));
|
|
|
|
|
2017-08-24 21:49:59 +02:00
|
|
|
tt_int_op(DIGEST_LEN, OP_EQ, sizeof(rsa_id[0])); // Do I remember C?
|
2016-11-11 19:09:25 +01:00
|
|
|
|
|
|
|
for (i = 0; i < N_CHAN; ++i) {
|
|
|
|
crypto_rand(rsa_id[i], DIGEST_LEN);
|
|
|
|
ed_id[i] = tor_malloc_zero(sizeof(*ed_id[i]));
|
|
|
|
crypto_rand((char*)ed_id[i]->pubkey, sizeof(ed_id[i]->pubkey));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* For channel 3, have no Ed identity. */
|
|
|
|
tor_free(ed_id[3]);
|
|
|
|
|
|
|
|
/* Channel 2 and 4 have same ROSA identity */
|
|
|
|
memcpy(rsa_id[4], rsa_id[2], DIGEST_LEN);
|
|
|
|
|
|
|
|
/* Channel 2 and 4 and 5 have same RSA identity */
|
|
|
|
memcpy(rsa_id[4], rsa_id[2], DIGEST_LEN);
|
|
|
|
memcpy(rsa_id[5], rsa_id[2], DIGEST_LEN);
|
|
|
|
|
|
|
|
/* Channels 2 and 5 have same Ed25519 identity */
|
|
|
|
memcpy(ed_id[5], ed_id[2], sizeof(*ed_id[2]));
|
|
|
|
|
|
|
|
for (i = 0; i < N_CHAN; ++i) {
|
|
|
|
chan[i] = new_fake_channel();
|
|
|
|
channel_register(chan[i]);
|
|
|
|
channel_set_identity_digest(chan[i], rsa_id[i], ed_id[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Lookup by RSA id only */
|
|
|
|
tt_ptr_op(chan[0], OP_EQ,
|
|
|
|
channel_find_by_remote_identity(rsa_id[0], NULL));
|
|
|
|
tt_ptr_op(chan[1], OP_EQ,
|
|
|
|
channel_find_by_remote_identity(rsa_id[1], NULL));
|
|
|
|
tt_ptr_op(chan[3], OP_EQ,
|
|
|
|
channel_find_by_remote_identity(rsa_id[3], NULL));
|
|
|
|
channel_t *ch;
|
|
|
|
ch = channel_find_by_remote_identity(rsa_id[2], NULL);
|
|
|
|
tt_assert(ch == chan[2] || ch == chan[4] || ch == chan[5]);
|
|
|
|
ch = channel_next_with_rsa_identity(ch);
|
|
|
|
tt_assert(ch == chan[2] || ch == chan[4] || ch == chan[5]);
|
|
|
|
ch = channel_next_with_rsa_identity(ch);
|
|
|
|
tt_assert(ch == chan[2] || ch == chan[4] || ch == chan[5]);
|
|
|
|
ch = channel_next_with_rsa_identity(ch);
|
2017-08-24 21:55:27 +02:00
|
|
|
tt_ptr_op(ch, OP_EQ, NULL);
|
2016-11-11 19:09:25 +01:00
|
|
|
|
|
|
|
/* As above, but with zero Ed25519 ID (meaning "any ID") */
|
|
|
|
tt_ptr_op(chan[0], OP_EQ,
|
|
|
|
channel_find_by_remote_identity(rsa_id[0], &ed_zero));
|
|
|
|
tt_ptr_op(chan[1], OP_EQ,
|
|
|
|
channel_find_by_remote_identity(rsa_id[1], &ed_zero));
|
|
|
|
tt_ptr_op(chan[3], OP_EQ,
|
|
|
|
channel_find_by_remote_identity(rsa_id[3], &ed_zero));
|
|
|
|
ch = channel_find_by_remote_identity(rsa_id[2], &ed_zero);
|
|
|
|
tt_assert(ch == chan[2] || ch == chan[4] || ch == chan[5]);
|
|
|
|
ch = channel_next_with_rsa_identity(ch);
|
|
|
|
tt_assert(ch == chan[2] || ch == chan[4] || ch == chan[5]);
|
|
|
|
ch = channel_next_with_rsa_identity(ch);
|
|
|
|
tt_assert(ch == chan[2] || ch == chan[4] || ch == chan[5]);
|
|
|
|
ch = channel_next_with_rsa_identity(ch);
|
2017-08-24 21:55:27 +02:00
|
|
|
tt_ptr_op(ch, OP_EQ, NULL);
|
2016-11-11 19:09:25 +01:00
|
|
|
|
|
|
|
/* Lookup nonexistent RSA identity */
|
|
|
|
tt_ptr_op(NULL, OP_EQ,
|
|
|
|
channel_find_by_remote_identity("!!!!!!!!!!!!!!!!!!!!", NULL));
|
|
|
|
|
|
|
|
/* Look up by full identity pair */
|
|
|
|
tt_ptr_op(chan[0], OP_EQ,
|
|
|
|
channel_find_by_remote_identity(rsa_id[0], ed_id[0]));
|
|
|
|
tt_ptr_op(chan[1], OP_EQ,
|
|
|
|
channel_find_by_remote_identity(rsa_id[1], ed_id[1]));
|
|
|
|
tt_ptr_op(chan[3], OP_EQ,
|
|
|
|
channel_find_by_remote_identity(rsa_id[3], ed_id[3] /*NULL*/));
|
|
|
|
tt_ptr_op(chan[4], OP_EQ,
|
|
|
|
channel_find_by_remote_identity(rsa_id[4], ed_id[4]));
|
|
|
|
ch = channel_find_by_remote_identity(rsa_id[2], ed_id[2]);
|
|
|
|
tt_assert(ch == chan[2] || ch == chan[5]);
|
|
|
|
|
|
|
|
/* Look up RSA identity with wrong ed25519 identity */
|
|
|
|
tt_ptr_op(NULL, OP_EQ,
|
|
|
|
channel_find_by_remote_identity(rsa_id[4], ed_id[0]));
|
|
|
|
tt_ptr_op(NULL, OP_EQ,
|
|
|
|
channel_find_by_remote_identity(rsa_id[2], ed_id[1]));
|
|
|
|
tt_ptr_op(NULL, OP_EQ,
|
|
|
|
channel_find_by_remote_identity(rsa_id[3], ed_id[1]));
|
|
|
|
|
|
|
|
done:
|
|
|
|
for (i = 0; i < N_CHAN; ++i) {
|
|
|
|
channel_clear_identity_digest(chan[i]);
|
|
|
|
channel_unregister(chan[i]);
|
|
|
|
free_fake_channel(chan[i]);
|
|
|
|
tor_free(ed_id[i]);
|
|
|
|
}
|
2016-12-08 23:50:01 +01:00
|
|
|
#undef N_CHAN
|
2016-11-11 19:09:25 +01:00
|
|
|
}
|
|
|
|
|
2013-12-13 15:34:46 +01:00
|
|
|
struct testcase_t channel_tests[] = {
|
2017-11-21 22:06:46 +01:00
|
|
|
{ "inbound_cell", test_channel_inbound_cell, TT_FORK,
|
|
|
|
NULL, NULL },
|
2017-11-22 17:17:21 +01:00
|
|
|
{ "outbound_cell", test_channel_outbound_cell, TT_FORK,
|
|
|
|
NULL, NULL },
|
2017-11-21 22:06:46 +01:00
|
|
|
{ "id_map", test_channel_id_map, TT_FORK,
|
|
|
|
NULL, NULL },
|
|
|
|
{ "lifecycle", test_channel_lifecycle, TT_FORK,
|
|
|
|
NULL, NULL },
|
|
|
|
{ "lifecycle_2", test_channel_lifecycle_2, TT_FORK,
|
|
|
|
NULL, NULL },
|
2017-11-22 17:21:29 +01:00
|
|
|
{ "dumpstats", test_channel_dumpstats, TT_FORK,
|
|
|
|
NULL, NULL },
|
2013-12-13 15:34:46 +01:00
|
|
|
END_OF_TESTCASES
|
|
|
|
};
|
|
|
|
|