#include "orconfig.h"
#include <math.h>
#define SCHEDULER_KIST_PRIVATE
#define CHANNEL_OBJECT_PRIVATE
#define CHANNEL_FILE_PRIVATE
#include "core/or/or.h"
#include "app/config/config.h"
#include "lib/evloop/compat_libevent.h"
#include "core/or/channel.h"
#include "core/or/channeltls.h"
#include "core/mainloop/connection.h"
#include "feature/nodelist/networkstatus.h"
#define SCHEDULER_PRIVATE
#include "core/or/scheduler.h"
#include "test/test.h"
#include "test/fakechans.h"
#define V(major, minor, patch) \
(((major) << 24) | ((minor) << 16) | ((patch) << 8))
static int scheduler_compare_channels_mock_ctr = 0;
static int scheduler_run_mock_ctr = 0;
static or_options_t mocked_options;
static const or_options_t *
mock_get_options(void)
{
return &mocked_options;
}
static void
cleanup_scheduler_options(void)
{
if (mocked_options.SchedulerTypes_) {
SMARTLIST_FOREACH(mocked_options.SchedulerTypes_, int *, i, tor_free(i));
smartlist_free(mocked_options.SchedulerTypes_);
mocked_options.SchedulerTypes_ = NULL;
}
}
static void
set_scheduler_options(int val)
{
int *type;
if (mocked_options.SchedulerTypes_ == NULL) {
mocked_options.SchedulerTypes_ = smartlist_new();
}
type = tor_malloc_zero(sizeof(int));
*type = val;
smartlist_add(mocked_options.SchedulerTypes_, type);
}
static void
clear_options(void)
{
cleanup_scheduler_options();
memset(&mocked_options, 0, sizeof(mocked_options));
}
static int32_t
mock_vanilla_networkstatus_get_param(
const networkstatus_t *ns, const char *param_name, int32_t default_val,
int32_t min_val, int32_t max_val)
{
(void)ns;
(void)default_val;
(void)min_val;
(void)max_val;
tor_assert(strcmp(param_name, "KISTSchedRunInterval")==0);
return 0;
}
static int32_t
mock_kist_networkstatus_get_param(
const networkstatus_t *ns, const char *param_name, int32_t default_val,
int32_t min_val, int32_t max_val)
{
(void)ns;
(void)default_val;
(void)min_val;
(void)max_val;
tor_assert(strcmp(param_name, "KISTSchedRunInterval")==0);
return 12;
}
static int
scheduler_compare_channels_mock(const void *c1_v,
const void *c2_v)
{
uintptr_t p1, p2;
p1 = (uintptr_t)(c1_v);
p2 = (uintptr_t)(c2_v);
++scheduler_compare_channels_mock_ctr;
if (p1 == p2) return 0;
else if (p1 < p2) return 1;
else return -1;
}
static void
scheduler_run_noop_mock(void)
{
++scheduler_run_mock_ctr;
}
static circuitmux_t *mock_ccm_tgt_1 = NULL;
static circuitmux_t *mock_ccm_tgt_2 = NULL;
static circuitmux_t *mock_cgp_tgt_1 = NULL;
static circuitmux_policy_t *mock_cgp_val_1 = NULL;
static circuitmux_t *mock_cgp_tgt_2 = NULL;
static circuitmux_policy_t *mock_cgp_val_2 = NULL;
static const circuitmux_policy_t *
circuitmux_get_policy_mock(circuitmux_t *cmux)
{
const circuitmux_policy_t *result = NULL;
tt_assert(cmux != NULL);
if (cmux) {
if (cmux == mock_cgp_tgt_1) result = mock_cgp_val_1;
else if (cmux == mock_cgp_tgt_2) result = mock_cgp_val_2;
else result = circuitmux_get_policy__real(cmux);
}
done:
return result;
}
static int
circuitmux_compare_muxes_mock(circuitmux_t *cmux_1,
circuitmux_t *cmux_2)
{
int result = 0;
tt_assert(cmux_1 != NULL);
tt_assert(cmux_2 != NULL);
if (cmux_1 != cmux_2) {
if (cmux_1 == mock_ccm_tgt_1 && cmux_2 == mock_ccm_tgt_2) result = -1;
else if (cmux_1 == mock_ccm_tgt_2 && cmux_2 == mock_ccm_tgt_1) {
result = 1;
} else {
if (cmux_1 == mock_ccm_tgt_1 || cmux_1 == mock_ccm_tgt_2) result = -1;
else if (cmux_2 == mock_ccm_tgt_1 || cmux_2 == mock_ccm_tgt_2) {
result = 1;
} else {
result = circuitmux_compare_muxes__real(cmux_1, cmux_2);
}
}
}
done:
return result;
}
typedef struct {
const channel_t *chan;
ssize_t cells;
} flush_mock_channel_t;
static smartlist_t *chans_for_flush_mock = NULL;
static void
channel_flush_some_cells_mock_free_all(void)
{
if (chans_for_flush_mock) {
SMARTLIST_FOREACH_BEGIN(chans_for_flush_mock,
flush_mock_channel_t *,
flush_mock_ch) {
SMARTLIST_DEL_CURRENT(chans_for_flush_mock, flush_mock_ch);
tor_free(flush_mock_ch);
} SMARTLIST_FOREACH_END(flush_mock_ch);
smartlist_free(chans_for_flush_mock);
chans_for_flush_mock = NULL;
}
}
static void
channel_flush_some_cells_mock_set(channel_t *chan, ssize_t num_cells)
{
int found = 0;
if (!chan) return;
if (num_cells <= 0) return;
if (!chans_for_flush_mock) {
chans_for_flush_mock = smartlist_new();
}
SMARTLIST_FOREACH_BEGIN(chans_for_flush_mock,
flush_mock_channel_t *,
flush_mock_ch) {
if (flush_mock_ch != NULL && flush_mock_ch->chan != NULL) {
if (flush_mock_ch->chan == chan) {
flush_mock_ch->cells = num_cells;
found = 1;
break;
}
} else {
SMARTLIST_DEL_CURRENT(chans_for_flush_mock, flush_mock_ch);
tor_free(flush_mock_ch);
}
} SMARTLIST_FOREACH_END(flush_mock_ch);
if (! found) {
flush_mock_channel_t *flush_mock_ch;
flush_mock_ch = tor_malloc_zero(sizeof(*flush_mock_ch));
flush_mock_ch->chan = chan;
flush_mock_ch->cells = num_cells;
smartlist_add(chans_for_flush_mock, flush_mock_ch);
}
}
static int
channel_more_to_flush_mock(channel_t *chan)
{
tor_assert(chan);
flush_mock_channel_t *found_mock_ch = NULL;
SMARTLIST_FOREACH_BEGIN(chans_for_flush_mock,
flush_mock_channel_t *,
flush_mock_ch) {
if (flush_mock_ch != NULL && flush_mock_ch->chan != NULL) {
if (flush_mock_ch->chan == chan) {
found_mock_ch = flush_mock_ch;
break;
}
} else {
SMARTLIST_DEL_CURRENT(chans_for_flush_mock, flush_mock_ch);
tor_free(flush_mock_ch);
}
} SMARTLIST_FOREACH_END(flush_mock_ch);
tor_assert(found_mock_ch);
return (found_mock_ch->cells > 0 ? (int)found_mock_ch->cells : 0 );
}
static void
channel_write_to_kernel_mock(channel_t *chan)
{
(void)chan;
}
static int
channel_should_write_to_kernel_mock(outbuf_table_t *ot, channel_t *chan)
{
(void)ot;
(void)chan;
return 1;
}
static ssize_t
channel_flush_some_cells_mock(channel_t *chan, ssize_t num_cells)
{
ssize_t flushed = 0, max;
char unlimited = 0;
flush_mock_channel_t *found = NULL;
tt_ptr_op(chan, OP_NE, NULL);
if (chan) {
if (num_cells < 0) {
num_cells = 0;
unlimited = 1;
}
if (chans_for_flush_mock != NULL) {
SMARTLIST_FOREACH_BEGIN(chans_for_flush_mock,
flush_mock_channel_t *,
flush_mock_ch) {
if (flush_mock_ch != NULL && flush_mock_ch->chan != NULL) {
if (flush_mock_ch->chan == chan) {
found = flush_mock_ch;
break;
}
} else {
SMARTLIST_DEL_CURRENT(chans_for_flush_mock, flush_mock_ch);
tor_free(flush_mock_ch);
}
} SMARTLIST_FOREACH_END(flush_mock_ch);
if (found) {
if (found->cells < 0) found->cells = 0;
if (unlimited) max = found->cells;
else max = MIN(found->cells, num_cells);
flushed += max;
found->cells -= max;
}
}
}
done:
return flushed;
}
static void
update_socket_info_impl_mock(socket_table_ent_t *ent)
{
ent->cwnd = ent->unacked = ent->mss = ent->notsent = 0;
ent->limit = INT_MAX;
}
static void
perform_channel_state_tests(int KISTSchedRunInterval, int sched_type)
{
channel_t *ch1 = NULL, *ch2 = NULL;
int old_count;
MOCK(get_options, mock_get_options);
clear_options();
mocked_options.KISTSchedRunInterval = KISTSchedRunInterval;
set_scheduler_options(sched_type);
scheduler_init();
MOCK(scheduler_compare_channels, scheduler_compare_channels_mock);
((scheduler_t *) the_scheduler)->run = scheduler_run_noop_mock;
tt_int_op(smartlist_len(channels_pending), OP_EQ, 0);
ch1 = new_fake_channel();
tt_assert(ch1);
ch1->state = CHANNEL_STATE_OPENING;
channel_register(ch1);
tt_assert(ch1->registered);
tt_int_op(ch1->scheduler_state, OP_EQ, SCHED_CHAN_IDLE);
ch2 = new_fake_channel();
tt_assert(ch2);
ch2->state = CHANNEL_STATE_OPENING;
channel_register(ch2);
tt_assert(ch2->registered);
scheduler_channel_has_waiting_cells(ch1);
tt_int_op(ch1->scheduler_state, OP_EQ, SCHED_CHAN_WAITING_TO_WRITE);
scheduler_channel_wants_writes(ch1);
tt_int_op(ch1->scheduler_state, OP_EQ, SCHED_CHAN_PENDING);
tt_int_op(smartlist_len(channels_pending), OP_EQ, 1);
scheduler_channel_wants_writes(ch2);
tt_int_op(ch2->scheduler_state, OP_EQ, SCHED_CHAN_WAITING_FOR_CELLS);
scheduler_channel_doesnt_want_writes(ch2);
tt_int_op(ch2->scheduler_state, OP_EQ, SCHED_CHAN_IDLE);
scheduler_channel_wants_writes(ch2);
tt_int_op(ch2->scheduler_state, OP_EQ, SCHED_CHAN_WAITING_FOR_CELLS);
scheduler_channel_has_waiting_cells(ch2);
tt_int_op(ch2->scheduler_state, OP_EQ, SCHED_CHAN_PENDING);
tt_int_op(smartlist_len(channels_pending), OP_EQ, 2);
scheduler_channel_doesnt_want_writes(ch2);
tt_int_op(ch2->scheduler_state, OP_EQ, SCHED_CHAN_WAITING_TO_WRITE);
tt_int_op(smartlist_len(channels_pending), OP_EQ, 1);
scheduler_channel_wants_writes(ch2);
tt_int_op(ch2->scheduler_state, OP_EQ, SCHED_CHAN_PENDING);
tt_int_op(smartlist_len(channels_pending), OP_EQ, 2);
old_count = scheduler_compare_channels_mock_ctr;
scheduler_touch_channel(ch1);
tt_assert(scheduler_compare_channels_mock_ctr > old_count);
scheduler_release_channel(ch2);
tt_int_op(ch2->scheduler_state, OP_EQ, SCHED_CHAN_IDLE);
tt_int_op(smartlist_len(channels_pending), OP_EQ, 1);
ch2->scheduler_state = SCHED_CHAN_PENDING;
scheduler_release_channel(ch2);
tt_int_op(ch2->scheduler_state, OP_EQ, SCHED_CHAN_IDLE);
tt_int_op(smartlist_len(channels_pending), OP_EQ, 1);
channel_mark_for_close(ch1);
tt_int_op(ch1->state, OP_EQ, CHANNEL_STATE_CLOSING);
channel_mark_for_close(ch2);
tt_int_op(ch2->state, OP_EQ, CHANNEL_STATE_CLOSING);
channel_closed(ch1);
tt_int_op(ch1->state, OP_EQ, CHANNEL_STATE_CLOSED);
ch1 = NULL;
channel_closed(ch2);
tt_int_op(ch2->state, OP_EQ, CHANNEL_STATE_CLOSED);
ch2 = NULL;
channel_free_all();
scheduler_free_all();
done:
tor_free(ch1);
tor_free(ch2);
UNMOCK(scheduler_compare_channels);
UNMOCK(get_options);
cleanup_scheduler_options();
return;
}
static void
test_scheduler_compare_channels(void *arg)
{
channel_t c1, c2;
circuitmux_t *cm1 = NULL, *cm2 = NULL;
int result;
(void)arg;
cm1 = tor_malloc_zero(sizeof(void *));
cm2 = tor_malloc_zero(sizeof(void *));
c1.cmux = cm1;
c2.cmux = cm2;
mock_cgp_tgt_1 = cm1;
mock_cgp_tgt_2 = cm2;
mock_cgp_val_1 = tor_malloc_zero(16);
mock_cgp_val_2 = tor_malloc_zero(16);
if ( ((uintptr_t) mock_cgp_val_1) > ((uintptr_t) mock_cgp_val_2) ) {
void *tmp = mock_cgp_val_1;
mock_cgp_val_1 = mock_cgp_val_2;
mock_cgp_val_2 = tmp;
}
MOCK(circuitmux_get_policy, circuitmux_get_policy_mock);
mock_ccm_tgt_1 = cm1;
mock_ccm_tgt_2 = cm2;
MOCK(circuitmux_compare_muxes, circuitmux_compare_muxes_mock);
result = scheduler_compare_channels(&c1, &c1);
tt_int_op(result, OP_EQ, 0);
result = scheduler_compare_channels(&c1, &c2);
tt_int_op(result, OP_EQ, -1);
result = scheduler_compare_channels(&c2, &c1);
tt_int_op(result, OP_EQ, 1);
tor_free(mock_cgp_val_2);
mock_cgp_val_2 = mock_cgp_val_1;
result = scheduler_compare_channels(&c1, &c2);
tt_int_op(result, OP_EQ, -1);
result = scheduler_compare_channels(&c2, &c1);
tt_int_op(result, OP_EQ, 1);
done:
UNMOCK(circuitmux_compare_muxes);
mock_ccm_tgt_1 = NULL;
mock_ccm_tgt_2 = NULL;
UNMOCK(circuitmux_get_policy);
mock_cgp_tgt_1 = NULL;
mock_cgp_tgt_2 = NULL;
tor_free(cm1);
tor_free(cm2);
if (mock_cgp_val_1 != mock_cgp_val_2)
tor_free(mock_cgp_val_1);
tor_free(mock_cgp_val_2);
mock_cgp_val_1 = NULL;
mock_cgp_val_2 = NULL;
return;
}
static void
test_scheduler_loop_vanilla(void *arg)
{
(void)arg;
channel_t *ch1 = NULL, *ch2 = NULL;
void (*run_func_ptr)(void);
MOCK(get_options, mock_get_options);
clear_options();
set_scheduler_options(SCHEDULER_VANILLA);
mocked_options.KISTSchedRunInterval = 0;
scheduler_init();
MOCK(scheduler_compare_channels, scheduler_compare_channels_mock);
run_func_ptr = the_scheduler->run;
((scheduler_t *) the_scheduler)->run = scheduler_run_noop_mock;
tt_int_op(smartlist_len(channels_pending), OP_EQ, 0);
ch1 = new_fake_channel();
ch1->magic = TLS_CHAN_MAGIC;
tt_assert(ch1);
ch1->state = CHANNEL_STATE_OPENING;
channel_register(ch1);
tt_assert(ch1->registered);
channel_change_state_open(ch1);
tt_int_op(ch1->scheduler_state, OP_EQ, SCHED_CHAN_IDLE);
ch2 = new_fake_channel();
ch2->magic = TLS_CHAN_MAGIC;
tt_assert(ch2);
ch2->state = CHANNEL_STATE_OPENING;
channel_register(ch2);
tt_assert(ch2->registered);
tt_int_op(ch1->state, OP_EQ, CHANNEL_STATE_OPEN);
tt_int_op(ch2->state, OP_EQ, CHANNEL_STATE_OPENING);
scheduler_channel_has_waiting_cells(ch1);
tt_int_op(ch1->scheduler_state, OP_EQ, SCHED_CHAN_WAITING_TO_WRITE);
scheduler_channel_wants_writes(ch1);
tt_int_op(ch1->scheduler_state, OP_EQ, SCHED_CHAN_PENDING);
tt_int_op(smartlist_len(channels_pending), OP_EQ, 1);
scheduler_channel_wants_writes(ch2);
tt_int_op(ch2->scheduler_state, OP_EQ, SCHED_CHAN_WAITING_FOR_CELLS);
scheduler_channel_doesnt_want_writes(ch2);
tt_int_op(ch2->scheduler_state, OP_EQ, SCHED_CHAN_IDLE);
scheduler_channel_wants_writes(ch2);
tt_int_op(ch2->scheduler_state, OP_EQ, SCHED_CHAN_WAITING_FOR_CELLS);
scheduler_channel_has_waiting_cells(ch2);
tt_int_op(ch2->scheduler_state, OP_EQ, SCHED_CHAN_PENDING);
tt_int_op(smartlist_len(channels_pending), OP_EQ, 2);
run_func_ptr();
tt_int_op(ch1->state, OP_EQ, CHANNEL_STATE_OPEN);
tt_int_op(ch2->state, OP_EQ, CHANNEL_STATE_OPENING);
tt_assert(ch1->scheduler_state != SCHED_CHAN_PENDING);
tt_assert(ch2->scheduler_state != SCHED_CHAN_PENDING);
tt_int_op(smartlist_len(channels_pending), OP_EQ, 0);
channel_change_state_open(ch2);
scheduler_channel_wants_writes(ch1);
scheduler_channel_wants_writes(ch2);
scheduler_channel_has_waiting_cells(ch1);
scheduler_channel_has_waiting_cells(ch2);
tt_int_op(ch1->state, OP_EQ, CHANNEL_STATE_OPEN);
tt_int_op(ch2->state, OP_EQ, CHANNEL_STATE_OPEN);
tt_int_op(ch1->scheduler_state, OP_EQ, SCHED_CHAN_PENDING);
tt_int_op(ch2->scheduler_state, OP_EQ, SCHED_CHAN_PENDING);
tt_int_op(smartlist_len(channels_pending), OP_EQ, 2);
MOCK(channel_flush_some_cells, channel_flush_some_cells_mock);
channel_flush_some_cells_mock_set(ch1, 16);
channel_flush_some_cells_mock_set(ch2, 48);
run_func_ptr();
tt_int_op(ch1->scheduler_state, OP_EQ, SCHED_CHAN_WAITING_FOR_CELLS);
tt_int_op(ch2->scheduler_state, OP_EQ, SCHED_CHAN_WAITING_FOR_CELLS);
channel_mark_for_close(ch1);
tt_int_op(ch1->state, OP_EQ, CHANNEL_STATE_CLOSING);
channel_mark_for_close(ch2);
tt_int_op(ch2->state, OP_EQ, CHANNEL_STATE_CLOSING);
channel_closed(ch1);
tt_int_op(ch1->state, OP_EQ, CHANNEL_STATE_CLOSED);
ch1 = NULL;
channel_closed(ch2);
tt_int_op(ch2->state, OP_EQ, CHANNEL_STATE_CLOSED);
ch2 = NULL;
channel_flush_some_cells_mock_free_all();
channel_free_all();
scheduler_free_all();
done:
tor_free(ch1);
tor_free(ch2);
cleanup_scheduler_options();
UNMOCK(channel_flush_some_cells);
UNMOCK(scheduler_compare_channels);
UNMOCK(get_options);
}
static void
test_scheduler_loop_kist(void *arg)
{
(void) arg;
#ifndef HAVE_KIST_SUPPORT
return;
#endif
channel_t *ch1 = new_fake_channel(), *ch2 = new_fake_channel();
channel_t *ch3 = new_fake_channel();
MOCK(get_options, mock_get_options);
MOCK(channel_flush_some_cells, channel_flush_some_cells_mock);
MOCK(channel_more_to_flush, channel_more_to_flush_mock);
MOCK(channel_write_to_kernel, channel_write_to_kernel_mock);
MOCK(channel_should_write_to_kernel, channel_should_write_to_kernel_mock);
MOCK(update_socket_info_impl, update_socket_info_impl_mock);
clear_options();
mocked_options.KISTSchedRunInterval = 11;
set_scheduler_options(SCHEDULER_KIST);
scheduler_init();
tt_assert(ch1);
ch1->magic = TLS_CHAN_MAGIC;
ch1->state = CHANNEL_STATE_OPENING;
channel_register(ch1);
tt_assert(ch1->registered);
channel_change_state_open(ch1);
scheduler_channel_has_waiting_cells(ch1);
scheduler_channel_wants_writes(ch1);
channel_flush_some_cells_mock_set(ch1, 5);
tt_assert(ch2);
ch2->magic = TLS_CHAN_MAGIC;
ch2->state = CHANNEL_STATE_OPENING;
channel_register(ch2);
tt_assert(ch2->registered);
channel_change_state_open(ch2);
scheduler_channel_has_waiting_cells(ch2);
scheduler_channel_wants_writes(ch2);
channel_flush_some_cells_mock_set(ch2, 5);
the_scheduler->run();
scheduler_channel_has_waiting_cells(ch1);
channel_flush_some_cells_mock_set(ch1, 5);
the_scheduler->run();
scheduler_channel_has_waiting_cells(ch1);
channel_flush_some_cells_mock_set(ch1, 5);
scheduler_channel_has_waiting_cells(ch2);
channel_flush_some_cells_mock_set(ch2, 5);
the_scheduler->run();
channel_flush_some_cells_mock_free_all();
tt_assert(ch3);
ch3->magic = TLS_CHAN_MAGIC;
ch3->state = CHANNEL_STATE_OPEN;
circuitmux_free(ch3->cmux);
ch3->cmux = circuitmux_alloc();
channel_register(ch3);
tt_assert(ch3->registered);
ch3->scheduler_state = SCHED_CHAN_WAITING_FOR_CELLS;
scheduler_channel_has_waiting_cells(ch3);
tt_int_op(ch3->scheduler_state, OP_EQ, SCHED_CHAN_PENDING);
tt_int_op(smartlist_len(get_channels_pending()), OP_EQ, 1);
ch3->state = CHANNEL_STATE_CLOSED;
the_scheduler->run();
tt_int_op(ch3->scheduler_state, OP_EQ, SCHED_CHAN_IDLE);
tt_int_op(smartlist_len(get_channels_pending()), OP_EQ, 0);
done:
ch1->state = ch2->state = ch3->state = CHANNEL_STATE_CLOSED;
ch1->registered = ch2->registered = ch3->registered = 0;
channel_free(ch1);
channel_free(ch2);
channel_free(ch3);
UNMOCK(update_socket_info_impl);
UNMOCK(channel_should_write_to_kernel);
UNMOCK(channel_write_to_kernel);
UNMOCK(channel_more_to_flush);
UNMOCK(channel_flush_some_cells);
UNMOCK(get_options);
scheduler_free_all();
return;
}
static void
test_scheduler_channel_states(void *arg)
{
(void)arg;
perform_channel_state_tests(-1, SCHEDULER_VANILLA);
perform_channel_state_tests(11, SCHEDULER_KIST_LITE);
#ifdef HAVE_KIST_SUPPORT
perform_channel_state_tests(11, SCHEDULER_KIST);
#endif
}
static void
test_scheduler_initfree(void *arg)
{
(void)arg;
tt_ptr_op(channels_pending, OP_EQ, NULL);
tt_ptr_op(run_sched_ev, OP_EQ, NULL);
MOCK(get_options, mock_get_options);
set_scheduler_options(SCHEDULER_KIST);
set_scheduler_options(SCHEDULER_KIST_LITE);
set_scheduler_options(SCHEDULER_VANILLA);
scheduler_init();
tt_ptr_op(channels_pending, OP_NE, NULL);
tt_ptr_op(run_sched_ev, OP_NE, NULL);
tt_ptr_op(the_scheduler, OP_EQ, get_kist_scheduler());
tt_int_op(sched_run_interval, OP_EQ, 10);
scheduler_free_all();
tt_ptr_op(channels_pending, OP_EQ, NULL);
tt_ptr_op(run_sched_ev, OP_EQ, NULL);
done:
UNMOCK(get_options);
cleanup_scheduler_options();
return;
}
static void
test_scheduler_can_use_kist(void *arg)
{
(void)arg;
int res_should, res_freq;
MOCK(get_options, mock_get_options);
clear_options();
mocked_options.KISTSchedRunInterval = 1234;
res_should = scheduler_can_use_kist();
res_freq = kist_scheduler_run_interval();
#ifdef HAVE_KIST_SUPPORT
tt_int_op(res_should, OP_EQ, 1);
#else
tt_int_op(res_should, OP_EQ, 0);
#endif
tt_int_op(res_freq, OP_EQ, 1234);
clear_options();
mocked_options.KISTSchedRunInterval = 0;
res_should = scheduler_can_use_kist();
res_freq = kist_scheduler_run_interval();
#ifdef HAVE_KIST_SUPPORT
tt_int_op(res_should, OP_EQ, 1);
#else
tt_int_op(res_should, OP_EQ, 0);
#endif
tt_int_op(res_freq, OP_EQ, 10);
MOCK(networkstatus_get_param, mock_kist_networkstatus_get_param);
clear_options();
mocked_options.KISTSchedRunInterval = 0;
res_should = scheduler_can_use_kist();
res_freq = kist_scheduler_run_interval();
#ifdef HAVE_KIST_SUPPORT
tt_int_op(res_should, OP_EQ, 1);
#else
tt_int_op(res_should, OP_EQ, 0);
#endif
tt_int_op(res_freq, OP_EQ, 12);
UNMOCK(networkstatus_get_param);
MOCK(networkstatus_get_param, mock_vanilla_networkstatus_get_param);
clear_options();
mocked_options.KISTSchedRunInterval = 0;
res_should = scheduler_can_use_kist();
res_freq = kist_scheduler_run_interval();
tt_int_op(res_should, OP_EQ, 0);
tt_int_op(res_freq, OP_EQ, 0);
UNMOCK(networkstatus_get_param);
done:
UNMOCK(get_options);
return;
}
static void
test_scheduler_ns_changed(void *arg)
{
(void) arg;
MOCK(get_options, mock_get_options);
clear_options();
set_scheduler_options(SCHEDULER_KIST);
set_scheduler_options(SCHEDULER_VANILLA);
tt_ptr_op(the_scheduler, OP_EQ, NULL);
the_scheduler = get_vanilla_scheduler();
MOCK(networkstatus_get_param, mock_kist_networkstatus_get_param);
scheduler_notify_networkstatus_changed();
UNMOCK(networkstatus_get_param);
#ifdef HAVE_KIST_SUPPORT
tt_ptr_op(the_scheduler, OP_EQ, get_kist_scheduler());
#else
tt_ptr_op(the_scheduler, OP_EQ, get_vanilla_scheduler());
#endif
the_scheduler = get_kist_scheduler();
MOCK(networkstatus_get_param, mock_vanilla_networkstatus_get_param);
scheduler_notify_networkstatus_changed();
UNMOCK(networkstatus_get_param);
tt_ptr_op(the_scheduler, OP_EQ, get_vanilla_scheduler());
the_scheduler = get_kist_scheduler();
MOCK(networkstatus_get_param, mock_kist_networkstatus_get_param);
scheduler_notify_networkstatus_changed();
UNMOCK(networkstatus_get_param);
#ifdef HAVE_KIST_SUPPORT
tt_ptr_op(the_scheduler, OP_EQ, get_kist_scheduler());
#else
tt_ptr_op(the_scheduler, OP_EQ, get_vanilla_scheduler());
#endif
the_scheduler = get_vanilla_scheduler();
MOCK(networkstatus_get_param, mock_vanilla_networkstatus_get_param);
scheduler_notify_networkstatus_changed();
UNMOCK(networkstatus_get_param);
tt_ptr_op(the_scheduler, OP_EQ, get_vanilla_scheduler());
done:
UNMOCK(get_options);
cleanup_scheduler_options();
return;
}
static int mock_flush_some_cells_num = 1;
static int mock_more_to_flush = 0;
static int mock_update_socket_info_limit = 0;
static ssize_t
channel_flush_some_cells_mock_var(channel_t *chan, ssize_t num_cells)
{
(void) chan;
(void) num_cells;
return mock_flush_some_cells_num;
}
static void
channel_write_to_kernel_mock_trigger_24700(channel_t *chan)
{
static int chan_id_seen[2] = {0};
if (++chan_id_seen[chan->global_identifier - 1] > 1) {
tt_assert(0);
}
scheduler_channel_wants_writes(chan);
done:
return;
}
static int
channel_more_to_flush_mock_var(channel_t *chan)
{
(void) chan;
return mock_more_to_flush;
}
static void
update_socket_info_impl_mock_var(socket_table_ent_t *ent)
{
ent->cwnd = ent->unacked = ent->mss = ent->notsent = 0;
ent->limit = mock_update_socket_info_limit;
}
static void
test_scheduler_kist_pending_list(void *arg)
{
(void) arg;
#ifndef HAVE_KIST_SUPPORT
return;
#endif
MOCK(get_options, mock_get_options);
MOCK(channel_flush_some_cells, channel_flush_some_cells_mock_var);
MOCK(channel_more_to_flush, channel_more_to_flush_mock_var);
MOCK(update_socket_info_impl, update_socket_info_impl_mock_var);
MOCK(channel_write_to_kernel, channel_write_to_kernel_mock);
MOCK(channel_should_write_to_kernel, channel_should_write_to_kernel_mock);
mocked_options.KISTSchedRunInterval = 10;
set_scheduler_options(SCHEDULER_KIST);
scheduler_init();
channel_t *chan1 = new_fake_channel();
channel_t *chan2 = new_fake_channel();
tt_assert(chan1);
tt_assert(chan2);
chan1->magic = chan2->magic = TLS_CHAN_MAGIC;
channel_register(chan1);
channel_register(chan2);
tt_int_op(chan1->scheduler_state, OP_EQ, SCHED_CHAN_IDLE);
tt_int_op(chan1->sched_heap_idx, OP_EQ, -1);
tt_int_op(chan2->scheduler_state, OP_EQ, SCHED_CHAN_IDLE);
tt_int_op(chan2->sched_heap_idx, OP_EQ, -1);
scheduler_channel_wants_writes(chan1);
tt_int_op(chan1->scheduler_state, OP_EQ, SCHED_CHAN_WAITING_FOR_CELLS);
tt_int_op(smartlist_len(get_channels_pending()), OP_EQ, 0);
scheduler_channel_has_waiting_cells(chan1);
tt_int_op(chan1->scheduler_state, OP_EQ, SCHED_CHAN_PENDING);
tt_int_op(smartlist_len(get_channels_pending()), OP_EQ, 1);
scheduler_channel_has_waiting_cells(chan1);
tt_int_op(chan1->scheduler_state, OP_EQ, SCHED_CHAN_PENDING);
tt_int_op(smartlist_len(get_channels_pending()), OP_EQ, 1);
scheduler_channel_has_waiting_cells(chan1);
tt_int_op(chan1->scheduler_state, OP_EQ, SCHED_CHAN_PENDING);
tt_int_op(smartlist_len(get_channels_pending()), OP_EQ, 1);
mock_update_socket_info_limit = INT_MAX;
mock_more_to_flush = 0;
the_scheduler->run();
tt_int_op(smartlist_len(get_channels_pending()), OP_EQ, 0);
tt_int_op(chan1->scheduler_state, OP_EQ, SCHED_CHAN_WAITING_FOR_CELLS);
scheduler_channel_has_waiting_cells(chan1);
mock_update_socket_info_limit = 0;
mock_more_to_flush = 1;
the_scheduler->run();
tt_int_op(smartlist_len(get_channels_pending()), OP_EQ, 1);
tt_int_op(chan1->scheduler_state, OP_EQ, SCHED_CHAN_PENDING);
scheduler_channel_wants_writes(chan1);
tt_int_op(smartlist_len(get_channels_pending()), OP_EQ, 1);
tt_int_op(chan1->scheduler_state, OP_EQ, SCHED_CHAN_PENDING);
mock_update_socket_info_limit = INT_MAX;
mock_more_to_flush = 0;
the_scheduler->run();
tt_int_op(smartlist_len(get_channels_pending()), OP_EQ, 0);
tt_int_op(chan1->scheduler_state, OP_EQ, SCHED_CHAN_WAITING_FOR_CELLS);
scheduler_channel_wants_writes(chan1);
tt_int_op(smartlist_len(get_channels_pending()), OP_EQ, 0);
tt_int_op(chan1->scheduler_state, OP_EQ, SCHED_CHAN_WAITING_FOR_CELLS);
scheduler_channel_has_waiting_cells(chan1);
tt_int_op(smartlist_len(get_channels_pending()), OP_EQ, 1);
tt_int_op(chan1->scheduler_state, OP_EQ, SCHED_CHAN_PENDING);
mock_flush_some_cells_num = 0;
the_scheduler->run();
tt_int_op(smartlist_len(get_channels_pending()), OP_EQ, 0);
tt_int_op(chan1->scheduler_state, OP_EQ, SCHED_CHAN_WAITING_FOR_CELLS);
scheduler_channel_doesnt_want_writes(chan1);
tt_int_op(smartlist_len(get_channels_pending()), OP_EQ, 0);
tt_int_op(chan1->scheduler_state, OP_EQ, SCHED_CHAN_IDLE);
scheduler_channel_has_waiting_cells(chan1);
tt_int_op(smartlist_len(get_channels_pending()), OP_EQ, 0);
tt_int_op(chan1->scheduler_state, OP_EQ, SCHED_CHAN_WAITING_TO_WRITE);
scheduler_channel_has_waiting_cells(chan1);
tt_int_op(smartlist_len(get_channels_pending()), OP_EQ, 0);
tt_int_op(chan1->scheduler_state, OP_EQ, SCHED_CHAN_WAITING_TO_WRITE);
scheduler_channel_wants_writes(chan1);
tt_int_op(smartlist_len(get_channels_pending()), OP_EQ, 1);
tt_int_op(chan1->scheduler_state, OP_EQ, SCHED_CHAN_PENDING);
scheduler_channel_wants_writes(chan2);
scheduler_channel_has_waiting_cells(chan2);
tt_int_op(smartlist_len(get_channels_pending()), OP_EQ, 2);
tt_int_op(chan2->scheduler_state, OP_EQ, SCHED_CHAN_PENDING);
mock_update_socket_info_limit = 600;
mock_more_to_flush = 1;
mock_flush_some_cells_num = 1;
MOCK(channel_write_to_kernel, channel_write_to_kernel_mock_trigger_24700);
the_scheduler->run();
tt_int_op(smartlist_len(get_channels_pending()), OP_EQ, 2);
tt_int_op(chan1->scheduler_state, OP_EQ, SCHED_CHAN_PENDING);
tt_int_op(chan2->scheduler_state, OP_EQ, SCHED_CHAN_PENDING);
done:
chan1->state = chan2->state = CHANNEL_STATE_CLOSED;
chan1->registered = chan2->registered = 0;
channel_free(chan1);
channel_free(chan2);
scheduler_free_all();
UNMOCK(get_options);
UNMOCK(channel_flush_some_cells);
UNMOCK(channel_more_to_flush);
UNMOCK(update_socket_info_impl);
UNMOCK(channel_write_to_kernel);
UNMOCK(channel_should_write_to_kernel);
}
struct testcase_t scheduler_tests[] = {
{ "compare_channels", test_scheduler_compare_channels,
TT_FORK, NULL, NULL },
{ "channel_states", test_scheduler_channel_states, TT_FORK, NULL, NULL },
{ "initfree", test_scheduler_initfree, TT_FORK, NULL, NULL },
{ "loop_vanilla", test_scheduler_loop_vanilla, TT_FORK, NULL, NULL },
{ "loop_kist", test_scheduler_loop_kist, TT_FORK, NULL, NULL },
{ "ns_changed", test_scheduler_ns_changed, TT_FORK, NULL, NULL},
{ "should_use_kist", test_scheduler_can_use_kist, TT_FORK, NULL, NULL },
{ "kist_pending_list", test_scheduler_kist_pending_list, TT_FORK,
NULL, NULL },
END_OF_TESTCASES
};