tor/src/test/test_accounting.c
Nick Mathewson 23c77f79fd Remove all usage of the NS*() macros in test*.c
This is an automatically generated commit, made with the following
kludgey perl script. It results in a number of wide lines, which
I'll clean up in a subsequent commit.

#/usr/bin/perl -w -i
$mod = "NS_MODULE";
$submod = "NS_SUBMODULE";
$last_was_empty = 0;

while (<>) {

    s/\bASPECT\(\s*(\w+)\s*,\s*(\w+)\s*\)/$1_$2/;

    if (/# *define +NS_MODULE +(\w+)/) {
        $mod = $1;
        next;
    } elsif (/# *define +NS_SUBMODULE +(\w+)/) {
        $submod = $1;
        next;
    }

    next if (/#undef NS_(SUB)?MODULE/);

    s/NS\(\s*test_main\s*\)/test_${mod}_${submod}/;
    s/NS\(\s*(\w+)\s*\)/${mod}_${submod}_$1/g;
    s/NS_FULL\(\\s*(\w+)\s*,\s*(\w+),\s*(\w+)\s*\)/$1_$2_$3/;
    s/^(\s*)NS_MOCK\(\s*(\w+)\s*\)/$1MOCK($2,\n$1     ${mod}_${submod}_$2)/;
    s/NS_UNMOCK\(\s*(\w+)\s*\)/UNMOCK($1)/;
    s/TEST_CASE\(\s*(\w+)\s*\)/{ "$1", test_${mod}_$1, TT_FORK, NULL, NULL }/;
    s/TEST_CASE_ASPECT\(\s*(\w+)\s*,\s*(\w+)\s*\)/{ "$1_$2", test_${mod}_$1_$2, TT_FORK, NULL, NULL }/;
    s/NS_DECL\(\s*([^,]+)\s*,\s*([^,]+)\s*,\s*(\(.*)\);/static $1 ${mod}_${submod}_$2$3;\nATTR_UNUSED static int ${mod}_${submod}_$2_called = 0;/;
    s/\bCALLED\(\s*(\w+)\s*\)/${mod}_${submod}_$1_called/;

    if (/^$/) {
        print if (! $last_was_empty);
        $last_was_empty = 1;
    } else {
        $last_was_empty = 0;
        print;
    }

    if (eof) {
        $mod = "NS_MODULE";
        $submod = "NS_SUBMODULE";
        $last_was_empty = 0;
    }
}

# Please enter the commit message for your changes. Lines starting
# with '#' will be kept; you may remove them yourself if you want to.
# An empty message aborts the commit.
#
# Date:      Thu Jan 9 10:26:10 2020 -0500
#
# On branch disable_ns_macro
# Changes to be committed:
#	modified:   src/test/test_accounting.c
#	modified:   src/test/test_compat_libevent.c
#	modified:   src/test/test_dir.c
#	modified:   src/test/test_dir_handle_get.c
#	modified:   src/test/test_dns.c
#	modified:   src/test/test_options.c
#	modified:   src/test/test_procmon.c
#	modified:   src/test/test_rendcache.c
#	modified:   src/test/test_router.c
#	modified:   src/test/test_routerset.c
#	modified:   src/test/test_status.c
#	modified:   src/test/test_tortls.c
#	modified:   src/test/test_tortls_openssl.c
#	modified:   src/test/test_util_format.c
#	modified:   src/test/test_util_process.c
#
# Untracked files:
#	experiments/
#	locate_options.sh
#	un_ns.pl
#

# Please enter the commit message for your changes. Lines starting
# with '#' will be kept; you may remove them yourself if you want to.
# An empty message aborts the commit.
#
# Date:      Thu Jan 9 10:26:10 2020 -0500
#
# On branch disable_ns_macro
# Changes to be committed:
#	modified:   src/test/test_accounting.c
#	modified:   src/test/test_compat_libevent.c
#	modified:   src/test/test_dir.c
#	modified:   src/test/test_dir_handle_get.c
#	modified:   src/test/test_dns.c
#	modified:   src/test/test_options.c
#	modified:   src/test/test_procmon.c
#	modified:   src/test/test_rendcache.c
#	modified:   src/test/test_router.c
#	modified:   src/test/test_routerset.c
#	modified:   src/test/test_status.c
#	modified:   src/test/test_tortls.c
#	modified:   src/test/test_tortls_openssl.c
#	modified:   src/test/test_util_format.c
#	modified:   src/test/test_util_process.c
#
# Untracked files:
#	experiments/
#	locate_options.sh
#	un_ns.pl
#
2020-01-09 11:09:22 -05:00

103 lines
2.4 KiB
C

/* Copyright (c) 2014-2020, 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"
/*
* Test to make sure accounting triggers hibernation
* correctly with both sum or max rules set
*/
static or_state_t *or_state;
static or_state_t * acct_limits_get_or_state(void);
ATTR_UNUSED static int acct_limits_get_or_state_called = 0;
static or_state_t *
acct_limits_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;
MOCK(get_or_state,
acct_limits_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:
UNMOCK(get_or_state);
or_state_free(or_state);
}
struct testcase_t accounting_tests[] = {
{ "bwlimits", test_accounting_limits, TT_FORK, NULL, NULL },
END_OF_TESTCASES
};