Bump copyright date to 2019
[tor.git] / src / test / test_accounting.c
blob8ae8fe43431b99b24c570d7c9cc8067e44bf4e7e
1 /* Copyright (c) 2014-2019, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 #include "core/or/or.h"
5 #include "test/test.h"
6 #define HIBERNATE_PRIVATE
7 #include "feature/hibernate/hibernate.h"
8 #include "app/config/config.h"
9 #define STATEFILE_PRIVATE
10 #include "app/config/statefile.h"
12 #include "app/config/or_state_st.h"
14 #define NS_MODULE accounting
16 #define NS_SUBMODULE limits
19 * Test to make sure accounting triggers hibernation
20 * correctly with both sum or max rules set
23 static or_state_t *or_state;
24 NS_DECL(or_state_t *, get_or_state, (void));
25 static or_state_t *
26 NS(get_or_state)(void)
28 return or_state;
31 static void
32 test_accounting_limits(void *arg)
34 or_options_t *options = get_options_mutable();
35 time_t fake_time = time(NULL);
36 (void) arg;
38 NS_MOCK(get_or_state);
39 or_state = or_state_new();
41 options->AccountingMax = 100;
42 options->AccountingRule = ACCT_MAX;
44 tor_assert(accounting_is_enabled(options));
45 configure_accounting(fake_time);
47 accounting_add_bytes(10, 0, 1);
48 fake_time += 1;
49 consider_hibernation(fake_time);
50 tor_assert(we_are_hibernating() == 0);
52 accounting_add_bytes(90, 0, 1);
53 fake_time += 1;
54 consider_hibernation(fake_time);
55 tor_assert(we_are_hibernating() == 1);
57 options->AccountingMax = 200;
58 options->AccountingRule = ACCT_SUM;
60 accounting_add_bytes(0, 10, 1);
61 fake_time += 1;
62 consider_hibernation(fake_time);
63 tor_assert(we_are_hibernating() == 0);
65 accounting_add_bytes(0, 90, 1);
66 fake_time += 1;
67 consider_hibernation(fake_time);
68 tor_assert(we_are_hibernating() == 1);
70 options->AccountingRule = ACCT_OUT;
72 accounting_add_bytes(100, 10, 1);
73 fake_time += 1;
74 consider_hibernation(fake_time);
75 tor_assert(we_are_hibernating() == 0);
77 accounting_add_bytes(0, 90, 1);
78 fake_time += 1;
79 consider_hibernation(fake_time);
80 tor_assert(we_are_hibernating() == 1);
82 options->AccountingMax = 300;
83 options->AccountingRule = ACCT_IN;
85 accounting_add_bytes(10, 100, 1);
86 fake_time += 1;
87 consider_hibernation(fake_time);
88 tor_assert(we_are_hibernating() == 0);
90 accounting_add_bytes(90, 0, 1);
91 fake_time += 1;
92 consider_hibernation(fake_time);
93 tor_assert(we_are_hibernating() == 1);
95 goto done;
96 done:
97 NS_UNMOCK(get_or_state);
98 or_state_free(or_state);
101 #undef NS_SUBMODULE
103 struct testcase_t accounting_tests[] = {
104 { "bwlimits", test_accounting_limits, TT_FORK, NULL, NULL },
105 END_OF_TESTCASES