2018-06-20 14:13:28 +02:00
|
|
|
/* Copyright (c) 2014-2018, The Tor Project, Inc. */
|
2017-10-27 16:59:36 +02:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
2018-07-05 22:34:59 +02:00
|
|
|
#include "core/or/or.h"
|
2018-06-20 15:35:05 +02:00
|
|
|
#include "test/test.h"
|
2014-09-23 14:34:22 +02:00
|
|
|
#define HIBERNATE_PRIVATE
|
2018-07-05 22:34:59 +02:00
|
|
|
#include "feature/hibernate/hibernate.h"
|
|
|
|
#include "app/config/config.h"
|
2014-09-23 14:34:22 +02:00
|
|
|
#define STATEFILE_PRIVATE
|
2018-07-05 22:34:59 +02:00
|
|
|
#include "app/config/statefile.h"
|
2014-09-23 14:34:22 +02:00
|
|
|
|
2018-07-05 22:34:59 +02:00
|
|
|
#include "app/config/or_state_st.h"
|
2018-07-01 20:51:53 +02:00
|
|
|
|
2014-09-23 14:34:22 +02:00
|
|
|
#define NS_MODULE accounting
|
|
|
|
|
|
|
|
#define NS_SUBMODULE limits
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Test to make sure accounting triggers hibernation
|
|
|
|
* correctly with both sum or max rules set
|
|
|
|
*/
|
|
|
|
|
|
|
|
static or_state_t *or_state;
|
|
|
|
NS_DECL(or_state_t *, get_or_state, (void));
|
|
|
|
static or_state_t *
|
|
|
|
NS(get_or_state)(void)
|
|
|
|
{
|
|
|
|
return or_state;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_accounting_limits(void *arg)
|
|
|
|
{
|
|
|
|
or_options_t *options = get_options_mutable();
|
|
|
|
time_t fake_time = time(NULL);
|
|
|
|
(void) arg;
|
|
|
|
|
|
|
|
NS_MOCK(get_or_state);
|
|
|
|
or_state = or_state_new();
|
|
|
|
|
|
|
|
options->AccountingMax = 100;
|
2014-09-23 14:46:35 +02:00
|
|
|
options->AccountingRule = ACCT_MAX;
|
2014-09-23 14:34:22 +02:00
|
|
|
|
|
|
|
tor_assert(accounting_is_enabled(options));
|
|
|
|
configure_accounting(fake_time);
|
|
|
|
|
|
|
|
accounting_add_bytes(10, 0, 1);
|
|
|
|
fake_time += 1;
|
|
|
|
consider_hibernation(fake_time);
|
|
|
|
tor_assert(we_are_hibernating() == 0);
|
|
|
|
|
|
|
|
accounting_add_bytes(90, 0, 1);
|
|
|
|
fake_time += 1;
|
|
|
|
consider_hibernation(fake_time);
|
|
|
|
tor_assert(we_are_hibernating() == 1);
|
|
|
|
|
|
|
|
options->AccountingMax = 200;
|
2014-09-23 14:46:35 +02:00
|
|
|
options->AccountingRule = ACCT_SUM;
|
2014-09-23 14:34:22 +02:00
|
|
|
|
|
|
|
accounting_add_bytes(0, 10, 1);
|
|
|
|
fake_time += 1;
|
|
|
|
consider_hibernation(fake_time);
|
|
|
|
tor_assert(we_are_hibernating() == 0);
|
|
|
|
|
|
|
|
accounting_add_bytes(0, 90, 1);
|
|
|
|
fake_time += 1;
|
|
|
|
consider_hibernation(fake_time);
|
|
|
|
tor_assert(we_are_hibernating() == 1);
|
2016-01-04 05:02:44 +01:00
|
|
|
|
|
|
|
options->AccountingRule = ACCT_OUT;
|
|
|
|
|
|
|
|
accounting_add_bytes(100, 10, 1);
|
|
|
|
fake_time += 1;
|
|
|
|
consider_hibernation(fake_time);
|
|
|
|
tor_assert(we_are_hibernating() == 0);
|
|
|
|
|
|
|
|
accounting_add_bytes(0, 90, 1);
|
|
|
|
fake_time += 1;
|
|
|
|
consider_hibernation(fake_time);
|
|
|
|
tor_assert(we_are_hibernating() == 1);
|
|
|
|
|
|
|
|
options->AccountingMax = 300;
|
|
|
|
options->AccountingRule = ACCT_IN;
|
|
|
|
|
|
|
|
accounting_add_bytes(10, 100, 1);
|
|
|
|
fake_time += 1;
|
|
|
|
consider_hibernation(fake_time);
|
|
|
|
tor_assert(we_are_hibernating() == 0);
|
|
|
|
|
|
|
|
accounting_add_bytes(90, 0, 1);
|
|
|
|
fake_time += 1;
|
|
|
|
consider_hibernation(fake_time);
|
|
|
|
tor_assert(we_are_hibernating() == 1);
|
|
|
|
|
2014-09-23 14:34:22 +02:00
|
|
|
goto done;
|
|
|
|
done:
|
|
|
|
NS_UNMOCK(get_or_state);
|
|
|
|
or_state_free(or_state);
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef NS_SUBMODULE
|
|
|
|
|
|
|
|
struct testcase_t accounting_tests[] = {
|
|
|
|
{ "bwlimits", test_accounting_limits, TT_FORK, NULL, NULL },
|
|
|
|
END_OF_TESTCASES
|
|
|
|
};
|