2018-06-20 14:13:28 +02:00
|
|
|
/* Copyright (c) 2013-2018, The Tor Project, Inc. */
|
2013-07-19 18:05:38 +02:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
|
|
|
#define TOR_CHANNEL_INTERNAL_
|
|
|
|
#define CIRCUITMUX_PRIVATE
|
2018-04-26 16:18:39 +02:00
|
|
|
#define CIRCUITMUX_EWMA_PRIVATE
|
2013-07-19 18:05:38 +02:00
|
|
|
#define RELAY_PRIVATE
|
2018-07-05 22:34:59 +02:00
|
|
|
#include "core/or/or.h"
|
|
|
|
#include "core/or/channel.h"
|
|
|
|
#include "core/or/circuitmux.h"
|
|
|
|
#include "core/or/circuitmux_ewma.h"
|
|
|
|
#include "core/or/relay.h"
|
|
|
|
#include "core/or/scheduler.h"
|
2018-06-20 15:35:05 +02:00
|
|
|
#include "test/test.h"
|
|
|
|
|
2018-07-05 22:34:59 +02:00
|
|
|
#include "core/or/destroy_cell_queue_st.h"
|
2018-06-15 21:27:46 +02:00
|
|
|
|
2018-09-14 14:40:10 +02:00
|
|
|
#include <math.h>
|
|
|
|
|
2013-07-19 18:05:38 +02:00
|
|
|
/* XXXX duplicated function from test_circuitlist.c */
|
|
|
|
static channel_t *
|
|
|
|
new_fake_channel(void)
|
|
|
|
{
|
|
|
|
channel_t *chan = tor_malloc_zero(sizeof(channel_t));
|
|
|
|
channel_init(chan);
|
|
|
|
return chan;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
has_queued_writes(channel_t *c)
|
|
|
|
{
|
|
|
|
(void) c;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Test destroy cell queue with no interference from other queues. */
|
|
|
|
static void
|
|
|
|
test_cmux_destroy_cell_queue(void *arg)
|
|
|
|
{
|
|
|
|
circuitmux_t *cmux = NULL;
|
|
|
|
channel_t *ch = NULL;
|
|
|
|
circuit_t *circ = NULL;
|
2017-12-19 19:53:52 +01:00
|
|
|
destroy_cell_queue_t *cq = NULL;
|
2013-07-19 18:05:38 +02:00
|
|
|
packed_cell_t *pc = NULL;
|
2017-12-19 19:53:52 +01:00
|
|
|
destroy_cell_t *dc = NULL;
|
2013-07-19 18:05:38 +02:00
|
|
|
|
2013-12-05 06:43:13 +01:00
|
|
|
scheduler_init();
|
|
|
|
|
2013-07-19 18:05:38 +02:00
|
|
|
(void) arg;
|
|
|
|
|
|
|
|
cmux = circuitmux_alloc();
|
|
|
|
tt_assert(cmux);
|
|
|
|
ch = new_fake_channel();
|
2018-02-15 20:23:36 +01:00
|
|
|
circuitmux_set_policy(cmux, &ewma_policy);
|
2013-07-19 18:05:38 +02:00
|
|
|
ch->has_queued_writes = has_queued_writes;
|
|
|
|
ch->wide_circ_ids = 1;
|
|
|
|
|
|
|
|
circ = circuitmux_get_first_active_circuit(cmux, &cq);
|
2017-08-24 21:55:27 +02:00
|
|
|
tt_ptr_op(circ, OP_EQ, NULL);
|
|
|
|
tt_ptr_op(cq, OP_EQ, NULL);
|
2013-07-19 18:05:38 +02:00
|
|
|
|
|
|
|
circuitmux_append_destroy_cell(ch, cmux, 100, 10);
|
|
|
|
circuitmux_append_destroy_cell(ch, cmux, 190, 6);
|
|
|
|
circuitmux_append_destroy_cell(ch, cmux, 30, 1);
|
|
|
|
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_int_op(circuitmux_num_cells(cmux), OP_EQ, 3);
|
2013-07-19 18:05:38 +02:00
|
|
|
|
|
|
|
circ = circuitmux_get_first_active_circuit(cmux, &cq);
|
2017-08-24 21:55:27 +02:00
|
|
|
tt_ptr_op(circ, OP_EQ, NULL);
|
2013-07-19 18:05:38 +02:00
|
|
|
tt_assert(cq);
|
|
|
|
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_int_op(cq->n, OP_EQ, 3);
|
2013-07-19 18:05:38 +02:00
|
|
|
|
2017-12-19 19:53:52 +01:00
|
|
|
dc = destroy_cell_queue_pop(cq);
|
|
|
|
tt_assert(dc);
|
2017-12-21 16:43:06 +01:00
|
|
|
tt_uint_op(dc->circid, OP_EQ, 100);
|
2013-07-19 18:05:38 +02:00
|
|
|
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_int_op(circuitmux_num_cells(cmux), OP_EQ, 2);
|
2013-07-19 18:05:38 +02:00
|
|
|
|
|
|
|
done:
|
|
|
|
circuitmux_free(cmux);
|
|
|
|
channel_free(ch);
|
|
|
|
packed_cell_free(pc);
|
2017-12-21 16:39:29 +01:00
|
|
|
tor_free(dc);
|
2013-07-19 18:05:38 +02:00
|
|
|
}
|
|
|
|
|
2018-04-26 16:18:39 +02:00
|
|
|
static void
|
|
|
|
test_cmux_compute_ticks(void *arg)
|
|
|
|
{
|
|
|
|
const int64_t NS_PER_S = 1000 * 1000 * 1000;
|
2018-07-03 16:33:50 +02:00
|
|
|
const int64_t START_NS = UINT64_C(1217709000)*NS_PER_S;
|
2018-04-26 16:18:39 +02:00
|
|
|
int64_t now;
|
|
|
|
double rem;
|
|
|
|
unsigned tick;
|
|
|
|
(void)arg;
|
|
|
|
circuitmux_ewma_free_all();
|
|
|
|
monotime_enable_test_mocking();
|
|
|
|
|
|
|
|
monotime_coarse_set_mock_time_nsec(START_NS);
|
|
|
|
cell_ewma_initialize_ticks();
|
|
|
|
const unsigned tick_zero = cell_ewma_get_current_tick_and_fraction(&rem);
|
|
|
|
tt_double_op(rem, OP_GT, -1e-9);
|
|
|
|
tt_double_op(rem, OP_LT, 1e-9);
|
|
|
|
|
|
|
|
/* 1.5 second later and we should still be in the same tick. */
|
|
|
|
now = START_NS + NS_PER_S + NS_PER_S/2;
|
|
|
|
monotime_coarse_set_mock_time_nsec(now);
|
|
|
|
tick = cell_ewma_get_current_tick_and_fraction(&rem);
|
|
|
|
tt_uint_op(tick, OP_EQ, tick_zero);
|
2018-09-14 14:40:10 +02:00
|
|
|
#ifdef USING_32BIT_MSEC_HACK
|
|
|
|
const double tolerance = .0005;
|
|
|
|
#else
|
|
|
|
const double tolerance = .00000001;
|
|
|
|
#endif
|
|
|
|
tt_double_op(fabs(rem - .15), OP_LT, tolerance);
|
2018-04-26 16:18:39 +02:00
|
|
|
|
|
|
|
/* 25 second later and we should be in another tick. */
|
|
|
|
now = START_NS + NS_PER_S * 25;
|
|
|
|
monotime_coarse_set_mock_time_nsec(now);
|
|
|
|
tick = cell_ewma_get_current_tick_and_fraction(&rem);
|
|
|
|
tt_uint_op(tick, OP_EQ, tick_zero + 2);
|
2018-09-14 14:40:10 +02:00
|
|
|
tt_double_op(fabs(rem - .5), OP_LT, tolerance);
|
2018-04-26 16:18:39 +02:00
|
|
|
|
|
|
|
done:
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2013-07-19 18:05:38 +02:00
|
|
|
struct testcase_t circuitmux_tests[] = {
|
|
|
|
{ "destroy_cell_queue", test_cmux_destroy_cell_queue, TT_FORK, NULL, NULL },
|
2018-04-26 16:18:39 +02:00
|
|
|
{ "compute_ticks", test_cmux_compute_ticks, TT_FORK, NULL, NULL },
|
2013-07-19 18:05:38 +02:00
|
|
|
END_OF_TESTCASES
|
|
|
|
};
|
|
|
|
|