Prop210: Refactor connection_get_* to produce lists and counts
[tor.git] / src / test / test_accounting.c
blob25908e942cb1a3a789ae75273fb1477bfdc2c7ee
1 #include "or.h"
2 #include "test.h"
3 #define HIBERNATE_PRIVATE
4 #include "hibernate.h"
5 #include "config.h"
6 #define STATEFILE_PRIVATE
7 #include "statefile.h"
9 #define NS_MODULE accounting
11 #define NS_SUBMODULE limits
14 * Test to make sure accounting triggers hibernation
15 * correctly with both sum or max rules set
18 static or_state_t *or_state;
19 NS_DECL(or_state_t *, get_or_state, (void));
20 static or_state_t *
21 NS(get_or_state)(void)
23 return or_state;
26 static void
27 test_accounting_limits(void *arg)
29 or_options_t *options = get_options_mutable();
30 time_t fake_time = time(NULL);
31 (void) arg;
33 NS_MOCK(get_or_state);
34 or_state = or_state_new();
36 options->AccountingMax = 100;
37 options->AccountingRule = ACCT_MAX;
39 tor_assert(accounting_is_enabled(options));
40 configure_accounting(fake_time);
42 accounting_add_bytes(10, 0, 1);
43 fake_time += 1;
44 consider_hibernation(fake_time);
45 tor_assert(we_are_hibernating() == 0);
47 accounting_add_bytes(90, 0, 1);
48 fake_time += 1;
49 consider_hibernation(fake_time);
50 tor_assert(we_are_hibernating() == 1);
52 options->AccountingMax = 200;
53 options->AccountingRule = ACCT_SUM;
55 accounting_add_bytes(0, 10, 1);
56 fake_time += 1;
57 consider_hibernation(fake_time);
58 tor_assert(we_are_hibernating() == 0);
60 accounting_add_bytes(0, 90, 1);
61 fake_time += 1;
62 consider_hibernation(fake_time);
63 tor_assert(we_are_hibernating() == 1);
64 goto done;
65 done:
66 NS_UNMOCK(get_or_state);
67 or_state_free(or_state);
70 #undef NS_SUBMODULE
72 struct testcase_t accounting_tests[] = {
73 { "bwlimits", test_accounting_limits, TT_FORK, NULL, NULL },
74 END_OF_TESTCASES