Merge branch 'maint-0.4.5' into release-0.4.5
[tor.git] / src / test / test_accounting.c
blob7933df5e35e7e6625e5d5bc4f8eee31922ac02af
1 /* Copyright (c) 2014-2020, 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"
15 * Test to make sure accounting triggers hibernation
16 * correctly with both sum or max rules set
19 static or_state_t *or_state;
20 static or_state_t * acct_limits_get_or_state(void);
21 ATTR_UNUSED static int acct_limits_get_or_state_called = 0;
22 static or_state_t *
23 acct_limits_get_or_state(void)
25 return or_state;
28 static void
29 test_accounting_limits(void *arg)
31 or_options_t *options = get_options_mutable();
32 time_t fake_time = time(NULL);
33 (void) arg;
35 MOCK(get_or_state,
36 acct_limits_get_or_state);
37 or_state = or_state_new();
39 options->AccountingMax = 100;
40 options->AccountingRule = ACCT_MAX;
42 tor_assert(accounting_is_enabled(options));
43 configure_accounting(fake_time);
45 accounting_add_bytes(10, 0, 1);
46 fake_time += 1;
47 consider_hibernation(fake_time);
48 tor_assert(we_are_hibernating() == 0);
50 accounting_add_bytes(90, 0, 1);
51 fake_time += 1;
52 consider_hibernation(fake_time);
53 tor_assert(we_are_hibernating() == 1);
55 options->AccountingMax = 200;
56 options->AccountingRule = ACCT_SUM;
58 accounting_add_bytes(0, 10, 1);
59 fake_time += 1;
60 consider_hibernation(fake_time);
61 tor_assert(we_are_hibernating() == 0);
63 accounting_add_bytes(0, 90, 1);
64 fake_time += 1;
65 consider_hibernation(fake_time);
66 tor_assert(we_are_hibernating() == 1);
68 options->AccountingRule = ACCT_OUT;
70 accounting_add_bytes(100, 10, 1);
71 fake_time += 1;
72 consider_hibernation(fake_time);
73 tor_assert(we_are_hibernating() == 0);
75 accounting_add_bytes(0, 90, 1);
76 fake_time += 1;
77 consider_hibernation(fake_time);
78 tor_assert(we_are_hibernating() == 1);
80 options->AccountingMax = 300;
81 options->AccountingRule = ACCT_IN;
83 accounting_add_bytes(10, 100, 1);
84 fake_time += 1;
85 consider_hibernation(fake_time);
86 tor_assert(we_are_hibernating() == 0);
88 accounting_add_bytes(90, 0, 1);
89 fake_time += 1;
90 consider_hibernation(fake_time);
91 tor_assert(we_are_hibernating() == 1);
93 goto done;
94 done:
95 UNMOCK(get_or_state);
96 or_state_free(or_state);
99 struct testcase_t accounting_tests[] = {
100 { "bwlimits", test_accounting_limits, TT_FORK, NULL, NULL },
101 END_OF_TESTCASES