1 /* Copyright (c) 2014-2020, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 #include "core/or/or.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;
23 acct_limits_get_or_state(void)
29 test_accounting_limits(void *arg
)
31 or_options_t
*options
= get_options_mutable();
32 time_t fake_time
= time(NULL
);
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);
47 consider_hibernation(fake_time
);
48 tor_assert(we_are_hibernating() == 0);
50 accounting_add_bytes(90, 0, 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);
60 consider_hibernation(fake_time
);
61 tor_assert(we_are_hibernating() == 0);
63 accounting_add_bytes(0, 90, 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);
72 consider_hibernation(fake_time
);
73 tor_assert(we_are_hibernating() == 0);
75 accounting_add_bytes(0, 90, 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);
85 consider_hibernation(fake_time
);
86 tor_assert(we_are_hibernating() == 0);
88 accounting_add_bytes(90, 0, 1);
90 consider_hibernation(fake_time
);
91 tor_assert(we_are_hibernating() == 1);
96 or_state_free(or_state
);
99 struct testcase_t accounting_tests
[] = {
100 { "bwlimits", test_accounting_limits
, TT_FORK
, NULL
, NULL
},