aboutsummaryrefslogtreecommitdiff
path: root/src/test/test_accounting.c
blob: 8ae8fe43431b99b24c570d7c9cc8067e44bf4e7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/* Copyright (c) 2014-2019, The Tor Project, Inc. */
/* See LICENSE for licensing information */

#include "core/or/or.h"
#include "test/test.h"
#define HIBERNATE_PRIVATE
#include "feature/hibernate/hibernate.h"
#include "app/config/config.h"
#define STATEFILE_PRIVATE
#include "app/config/statefile.h"

#include "app/config/or_state_st.h"

#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;
  options->AccountingRule = ACCT_MAX;

  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;
  options->AccountingRule = ACCT_SUM;

  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);

  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);

  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
};