2019-01-16 18:33:22 +01:00
|
|
|
/* Copyright (c) 2014-2019, The Tor Project, Inc. */
|
2014-01-21 11:54:35 +01:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
|
|
|
#define CIRCUITBUILD_PRIVATE
|
|
|
|
#define RELAY_PRIVATE
|
2018-09-20 20:41:44 +02:00
|
|
|
#define REPHIST_PRIVATE
|
|
|
|
#include "core/or/or.h"
|
2018-07-05 22:34:59 +02:00
|
|
|
#include "core/or/circuitbuild.h"
|
|
|
|
#include "core/or/circuitlist.h"
|
2018-09-18 02:17:14 +02:00
|
|
|
#include "core/or/channeltls.h"
|
|
|
|
#include "feature/stats/rephist.h"
|
2018-07-05 22:34:59 +02:00
|
|
|
#include "core/or/relay.h"
|
2018-09-20 20:41:44 +02:00
|
|
|
#include "feature/stats/rephist.h"
|
|
|
|
#include "lib/container/order.h"
|
2014-01-21 11:54:35 +01:00
|
|
|
/* For init/free stuff */
|
2018-07-05 22:34:59 +02:00
|
|
|
#include "core/or/scheduler.h"
|
2014-01-21 11:54:35 +01:00
|
|
|
|
2018-07-05 22:34:59 +02:00
|
|
|
#include "core/or/cell_st.h"
|
|
|
|
#include "core/or/or_circuit_st.h"
|
2018-06-15 17:34:33 +02:00
|
|
|
|
2014-01-21 11:54:35 +01:00
|
|
|
/* Test suite stuff */
|
2018-06-20 15:35:05 +02:00
|
|
|
#include "test/test.h"
|
|
|
|
#include "test/fakechans.h"
|
2014-01-21 11:54:35 +01:00
|
|
|
|
|
|
|
static or_circuit_t * new_fake_orcirc(channel_t *nchan, channel_t *pchan);
|
|
|
|
|
|
|
|
static void test_relay_append_cell_to_circuit_queue(void *arg);
|
|
|
|
|
|
|
|
static or_circuit_t *
|
|
|
|
new_fake_orcirc(channel_t *nchan, channel_t *pchan)
|
|
|
|
{
|
|
|
|
or_circuit_t *orcirc = NULL;
|
|
|
|
circuit_t *circ = NULL;
|
|
|
|
|
|
|
|
orcirc = tor_malloc_zero(sizeof(*orcirc));
|
|
|
|
circ = &(orcirc->base_);
|
|
|
|
circ->magic = OR_CIRCUIT_MAGIC;
|
|
|
|
|
2018-09-14 01:03:32 +02:00
|
|
|
circuit_set_n_circid_chan(circ, get_unique_circ_id_by_chan(nchan), nchan);
|
2014-01-21 11:54:35 +01:00
|
|
|
cell_queue_init(&(circ->n_chan_cells));
|
2018-09-14 01:03:32 +02:00
|
|
|
|
2014-01-21 11:54:35 +01:00
|
|
|
circ->n_hop = NULL;
|
|
|
|
circ->streams_blocked_on_n_chan = 0;
|
|
|
|
circ->streams_blocked_on_p_chan = 0;
|
|
|
|
circ->n_delete_pending = 0;
|
|
|
|
circ->p_delete_pending = 0;
|
|
|
|
circ->received_destroy = 0;
|
|
|
|
circ->state = CIRCUIT_STATE_OPEN;
|
|
|
|
circ->purpose = CIRCUIT_PURPOSE_OR;
|
|
|
|
circ->package_window = CIRCWINDOW_START_MAX;
|
|
|
|
circ->deliver_window = CIRCWINDOW_START_MAX;
|
|
|
|
circ->n_chan_create_cell = NULL;
|
|
|
|
|
2018-09-14 01:03:32 +02:00
|
|
|
circuit_set_p_circid_chan(orcirc, get_unique_circ_id_by_chan(pchan), pchan);
|
2014-01-21 11:54:35 +01:00
|
|
|
cell_queue_init(&(orcirc->p_chan_cells));
|
|
|
|
|
|
|
|
return orcirc;
|
|
|
|
}
|
|
|
|
|
2018-09-14 22:28:47 +02:00
|
|
|
static void
|
|
|
|
assert_circuit_ok_mock(const circuit_t *c)
|
|
|
|
{
|
|
|
|
(void) c;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-14 01:03:32 +02:00
|
|
|
static void
|
|
|
|
test_relay_close_circuit(void *arg)
|
|
|
|
{
|
|
|
|
channel_t *nchan = NULL, *pchan = NULL;
|
|
|
|
or_circuit_t *orcirc = NULL;
|
|
|
|
cell_t *cell = NULL;
|
|
|
|
int old_count, new_count;
|
|
|
|
|
|
|
|
(void)arg;
|
|
|
|
|
|
|
|
/* Make fake channels to be nchan and pchan for the circuit */
|
|
|
|
nchan = new_fake_channel();
|
|
|
|
tt_assert(nchan);
|
|
|
|
|
|
|
|
pchan = new_fake_channel();
|
|
|
|
tt_assert(pchan);
|
|
|
|
|
|
|
|
/* Make a fake orcirc */
|
|
|
|
orcirc = new_fake_orcirc(nchan, pchan);
|
|
|
|
tt_assert(orcirc);
|
|
|
|
circuitmux_attach_circuit(nchan->cmux, TO_CIRCUIT(orcirc),
|
|
|
|
CELL_DIRECTION_OUT);
|
|
|
|
circuitmux_attach_circuit(pchan->cmux, TO_CIRCUIT(orcirc),
|
|
|
|
CELL_DIRECTION_IN);
|
|
|
|
|
|
|
|
/* Make a cell */
|
|
|
|
cell = tor_malloc_zero(sizeof(cell_t));
|
|
|
|
make_fake_cell(cell);
|
|
|
|
|
|
|
|
MOCK(scheduler_channel_has_waiting_cells,
|
|
|
|
scheduler_channel_has_waiting_cells_mock);
|
2018-09-14 22:28:47 +02:00
|
|
|
MOCK(assert_circuit_ok,
|
|
|
|
assert_circuit_ok_mock);
|
2018-09-14 01:03:32 +02:00
|
|
|
|
|
|
|
/* Append it */
|
|
|
|
old_count = get_mock_scheduler_has_waiting_cells_count();
|
|
|
|
append_cell_to_circuit_queue(TO_CIRCUIT(orcirc), nchan, cell,
|
|
|
|
CELL_DIRECTION_OUT, 0);
|
|
|
|
new_count = get_mock_scheduler_has_waiting_cells_count();
|
|
|
|
tt_int_op(new_count, OP_EQ, old_count + 1);
|
|
|
|
|
|
|
|
/* Now try the reverse direction */
|
|
|
|
old_count = get_mock_scheduler_has_waiting_cells_count();
|
|
|
|
append_cell_to_circuit_queue(TO_CIRCUIT(orcirc), pchan, cell,
|
|
|
|
CELL_DIRECTION_IN, 0);
|
|
|
|
new_count = get_mock_scheduler_has_waiting_cells_count();
|
|
|
|
tt_int_op(new_count, OP_EQ, old_count + 1);
|
|
|
|
|
|
|
|
/* Ensure our write totals are 0 */
|
2018-09-20 20:18:09 +02:00
|
|
|
tt_u64_op(find_largest_max(write_array), OP_EQ, 0);
|
2018-09-14 01:03:32 +02:00
|
|
|
|
|
|
|
/* Mark the circuit for close */
|
|
|
|
circuit_mark_for_close(TO_CIRCUIT(orcirc), 0);
|
|
|
|
|
|
|
|
/* Check our write totals. */
|
|
|
|
advance_obs(write_array);
|
|
|
|
commit_max(write_array);
|
|
|
|
/* Check for two cells plus overhead */
|
2018-09-20 20:18:09 +02:00
|
|
|
tt_u64_op(find_largest_max(write_array), OP_EQ,
|
2018-09-14 01:03:32 +02:00
|
|
|
2*(get_cell_network_size(nchan->wide_circ_ids)
|
|
|
|
+TLS_PER_CELL_OVERHEAD));
|
|
|
|
|
|
|
|
UNMOCK(scheduler_channel_has_waiting_cells);
|
|
|
|
|
|
|
|
/* Get rid of the fake channels */
|
|
|
|
MOCK(scheduler_release_channel, scheduler_release_channel_mock);
|
|
|
|
channel_mark_for_close(nchan);
|
|
|
|
channel_mark_for_close(pchan);
|
|
|
|
UNMOCK(scheduler_release_channel);
|
|
|
|
|
|
|
|
/* Shut down channels */
|
|
|
|
channel_free_all();
|
|
|
|
|
|
|
|
done:
|
|
|
|
tor_free(cell);
|
|
|
|
if (orcirc) {
|
|
|
|
circuitmux_detach_circuit(nchan->cmux, TO_CIRCUIT(orcirc));
|
|
|
|
circuitmux_detach_circuit(pchan->cmux, TO_CIRCUIT(orcirc));
|
|
|
|
cell_queue_clear(&orcirc->base_.n_chan_cells);
|
|
|
|
cell_queue_clear(&orcirc->p_chan_cells);
|
|
|
|
}
|
|
|
|
tor_free(orcirc);
|
|
|
|
free_fake_channel(nchan);
|
|
|
|
free_fake_channel(pchan);
|
2018-09-14 22:28:47 +02:00
|
|
|
UNMOCK(assert_circuit_ok);
|
2018-09-14 01:03:32 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-21 11:54:35 +01:00
|
|
|
static void
|
|
|
|
test_relay_append_cell_to_circuit_queue(void *arg)
|
|
|
|
{
|
|
|
|
channel_t *nchan = NULL, *pchan = NULL;
|
|
|
|
or_circuit_t *orcirc = NULL;
|
|
|
|
cell_t *cell = NULL;
|
|
|
|
int old_count, new_count;
|
|
|
|
|
|
|
|
(void)arg;
|
|
|
|
|
|
|
|
/* Make fake channels to be nchan and pchan for the circuit */
|
|
|
|
nchan = new_fake_channel();
|
2014-09-23 16:22:26 +02:00
|
|
|
tt_assert(nchan);
|
2014-01-21 11:54:35 +01:00
|
|
|
|
|
|
|
pchan = new_fake_channel();
|
2014-09-23 16:22:26 +02:00
|
|
|
tt_assert(pchan);
|
2014-01-21 11:54:35 +01:00
|
|
|
|
|
|
|
/* Make a fake orcirc */
|
|
|
|
orcirc = new_fake_orcirc(nchan, pchan);
|
2014-09-23 16:22:26 +02:00
|
|
|
tt_assert(orcirc);
|
2016-09-08 21:25:56 +02:00
|
|
|
circuitmux_attach_circuit(nchan->cmux, TO_CIRCUIT(orcirc),
|
|
|
|
CELL_DIRECTION_OUT);
|
|
|
|
circuitmux_attach_circuit(pchan->cmux, TO_CIRCUIT(orcirc),
|
|
|
|
CELL_DIRECTION_IN);
|
2014-01-21 11:54:35 +01:00
|
|
|
|
|
|
|
/* Make a cell */
|
|
|
|
cell = tor_malloc_zero(sizeof(cell_t));
|
|
|
|
make_fake_cell(cell);
|
|
|
|
|
|
|
|
MOCK(scheduler_channel_has_waiting_cells,
|
|
|
|
scheduler_channel_has_waiting_cells_mock);
|
|
|
|
|
|
|
|
/* Append it */
|
|
|
|
old_count = get_mock_scheduler_has_waiting_cells_count();
|
|
|
|
append_cell_to_circuit_queue(TO_CIRCUIT(orcirc), nchan, cell,
|
|
|
|
CELL_DIRECTION_OUT, 0);
|
|
|
|
new_count = get_mock_scheduler_has_waiting_cells_count();
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_int_op(new_count, OP_EQ, old_count + 1);
|
2014-01-21 11:54:35 +01:00
|
|
|
|
|
|
|
/* Now try the reverse direction */
|
|
|
|
old_count = get_mock_scheduler_has_waiting_cells_count();
|
|
|
|
append_cell_to_circuit_queue(TO_CIRCUIT(orcirc), pchan, cell,
|
|
|
|
CELL_DIRECTION_IN, 0);
|
|
|
|
new_count = get_mock_scheduler_has_waiting_cells_count();
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_int_op(new_count, OP_EQ, old_count + 1);
|
2014-01-21 11:54:35 +01:00
|
|
|
|
|
|
|
UNMOCK(scheduler_channel_has_waiting_cells);
|
|
|
|
|
|
|
|
/* Get rid of the fake channels */
|
|
|
|
MOCK(scheduler_release_channel, scheduler_release_channel_mock);
|
|
|
|
channel_mark_for_close(nchan);
|
|
|
|
channel_mark_for_close(pchan);
|
|
|
|
UNMOCK(scheduler_release_channel);
|
|
|
|
|
|
|
|
/* Shut down channels */
|
|
|
|
channel_free_all();
|
|
|
|
|
|
|
|
done:
|
2014-12-22 17:13:01 +01:00
|
|
|
tor_free(cell);
|
2016-09-08 21:25:56 +02:00
|
|
|
if (orcirc) {
|
|
|
|
circuitmux_detach_circuit(nchan->cmux, TO_CIRCUIT(orcirc));
|
|
|
|
circuitmux_detach_circuit(pchan->cmux, TO_CIRCUIT(orcirc));
|
2016-09-09 21:29:57 +02:00
|
|
|
cell_queue_clear(&orcirc->base_.n_chan_cells);
|
|
|
|
cell_queue_clear(&orcirc->p_chan_cells);
|
2016-09-08 21:25:56 +02:00
|
|
|
}
|
2014-01-21 11:54:35 +01:00
|
|
|
tor_free(orcirc);
|
2014-12-22 18:27:26 +01:00
|
|
|
free_fake_channel(nchan);
|
|
|
|
free_fake_channel(pchan);
|
2014-01-21 11:54:35 +01:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct testcase_t relay_tests[] = {
|
|
|
|
{ "append_cell_to_circuit_queue", test_relay_append_cell_to_circuit_queue,
|
|
|
|
TT_FORK, NULL, NULL },
|
2018-09-14 01:03:32 +02:00
|
|
|
{ "close_circ_rephist", test_relay_close_circuit,
|
|
|
|
TT_FORK, NULL, NULL },
|
2014-01-21 11:54:35 +01:00
|
|
|
END_OF_TESTCASES
|
|
|
|
};
|