aboutsummaryrefslogtreecommitdiff
path: root/src/test/test_accounting.c
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2014-09-23 08:34:22 -0400
committerNick Mathewson <nickm@torproject.org>2014-09-29 09:05:11 -0400
commit8527a2996675d9502551ccdd4224036a45aae47b (patch)
tree9b2ab6a02949da52b42b5b1b1b4cd7a29465e778 /src/test/test_accounting.c
parente6150c7fc079d7841bedd0df838b29aef0f497d7 (diff)
downloadtor-8527a2996675d9502551ccdd4224036a45aae47b.tar.gz
tor-8527a2996675d9502551ccdd4224036a45aae47b.zip
Add an "AccountingRule" feature to permit limiting bw usage by read+write
Patch from "chobe". Closes ticket 961.
Diffstat (limited to 'src/test/test_accounting.c')
-rw-r--r--src/test/test_accounting.c76
1 files changed, 76 insertions, 0 deletions
diff --git a/src/test/test_accounting.c b/src/test/test_accounting.c
new file mode 100644
index 0000000000..a1a2ec643e
--- /dev/null
+++ b/src/test/test_accounting.c
@@ -0,0 +1,76 @@
+#include "or.h"
+#include "test.h"
+#define HIBERNATE_PRIVATE
+#include "hibernate.h"
+#include "config.h"
+#define STATEFILE_PRIVATE
+#include "statefile.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 = tor_strdup("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 = tor_strdup("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);
+ 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
+};
+