2018-06-20 14:13:28 +02:00
|
|
|
/* Copyright (c) 2014-2018, The Tor Project, Inc. */
|
2017-10-27 16:59:36 +02:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
2014-04-15 14:20:34 +02:00
|
|
|
#define STATUS_PRIVATE
|
|
|
|
#define HIBERNATE_PRIVATE
|
|
|
|
#define LOG_PRIVATE
|
|
|
|
#define REPHIST_PRIVATE
|
|
|
|
|
2016-07-28 16:47:46 +02:00
|
|
|
#include "orconfig.h"
|
|
|
|
|
2014-04-15 14:20:34 +02:00
|
|
|
#include <float.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
2018-06-20 15:35:05 +02:00
|
|
|
#include "or/or.h"
|
2018-06-22 16:32:10 +02:00
|
|
|
#include "lib/log/torlog.h"
|
2014-04-15 14:20:34 +02:00
|
|
|
#include "tor_queue.h"
|
2018-06-20 15:35:05 +02:00
|
|
|
#include "or/status.h"
|
|
|
|
#include "or/circuitlist.h"
|
|
|
|
#include "or/config.h"
|
|
|
|
#include "or/hibernate.h"
|
|
|
|
#include "or/rephist.h"
|
|
|
|
#include "or/relay.h"
|
|
|
|
#include "or/router.h"
|
|
|
|
#include "or/main.h"
|
|
|
|
#include "or/nodelist.h"
|
|
|
|
#include "or/statefile.h"
|
2018-07-01 17:32:11 +02:00
|
|
|
#include "lib/tls/tortls.h"
|
2018-06-20 15:35:05 +02:00
|
|
|
|
|
|
|
#include "or/origin_circuit_st.h"
|
2018-07-01 20:51:53 +02:00
|
|
|
#include "or/or_state_st.h"
|
2018-06-20 15:35:05 +02:00
|
|
|
#include "or/routerinfo_st.h"
|
|
|
|
|
|
|
|
#include "test/test.h"
|
2014-04-15 14:20:34 +02:00
|
|
|
|
|
|
|
#define NS_MODULE status
|
|
|
|
|
|
|
|
#define NS_SUBMODULE count_circuits
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Test that count_circuits() is correctly counting the number of
|
|
|
|
* global circuits.
|
|
|
|
*/
|
|
|
|
|
2014-08-15 23:02:28 +02:00
|
|
|
static smartlist_t * mock_global_circuitlist = NULL;
|
2014-04-15 14:20:34 +02:00
|
|
|
|
2014-08-15 23:02:28 +02:00
|
|
|
NS_DECL(smartlist_t *, circuit_get_global_list, (void));
|
2014-04-15 14:20:34 +02:00
|
|
|
|
|
|
|
static void
|
|
|
|
NS(test_main)(void *arg)
|
|
|
|
{
|
|
|
|
/* Choose origin_circuit_t wlog. */
|
|
|
|
origin_circuit_t *mock_circuit1, *mock_circuit2;
|
|
|
|
int expected_circuits = 2, actual_circuits;
|
|
|
|
|
|
|
|
(void)arg;
|
|
|
|
|
|
|
|
mock_circuit1 = tor_malloc_zero(sizeof(origin_circuit_t));
|
|
|
|
mock_circuit2 = tor_malloc_zero(sizeof(origin_circuit_t));
|
2014-08-15 23:02:28 +02:00
|
|
|
mock_global_circuitlist = smartlist_new();
|
|
|
|
smartlist_add(mock_global_circuitlist, TO_CIRCUIT(mock_circuit1));
|
|
|
|
smartlist_add(mock_global_circuitlist, TO_CIRCUIT(mock_circuit2));
|
2014-04-15 14:20:34 +02:00
|
|
|
|
|
|
|
NS_MOCK(circuit_get_global_list);
|
|
|
|
|
|
|
|
actual_circuits = count_circuits();
|
|
|
|
|
|
|
|
tt_assert(expected_circuits == actual_circuits);
|
|
|
|
|
2014-08-15 23:02:28 +02:00
|
|
|
done:
|
|
|
|
tor_free(mock_circuit1);
|
|
|
|
tor_free(mock_circuit2);
|
|
|
|
smartlist_free(mock_global_circuitlist);
|
|
|
|
mock_global_circuitlist = NULL;
|
|
|
|
NS_UNMOCK(circuit_get_global_list);
|
2014-04-15 14:20:34 +02:00
|
|
|
}
|
|
|
|
|
2014-08-15 23:02:28 +02:00
|
|
|
static smartlist_t *
|
2014-04-15 14:20:34 +02:00
|
|
|
NS(circuit_get_global_list)(void)
|
|
|
|
{
|
2014-08-15 23:02:28 +02:00
|
|
|
return mock_global_circuitlist;
|
2014-04-15 14:20:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#undef NS_SUBMODULE
|
|
|
|
#define NS_SUBMODULE secs_to_uptime
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Test that secs_to_uptime() is converting the number of seconds that
|
|
|
|
* Tor is up for into the appropriate string form containing hours and minutes.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
NS(test_main)(void *arg)
|
|
|
|
{
|
|
|
|
const char *expected;
|
|
|
|
char *actual;
|
|
|
|
(void)arg;
|
|
|
|
|
|
|
|
expected = "0:00 hours";
|
|
|
|
actual = secs_to_uptime(0);
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_str_op(actual, OP_EQ, expected);
|
2014-04-15 14:20:34 +02:00
|
|
|
tor_free(actual);
|
|
|
|
|
|
|
|
expected = "0:00 hours";
|
|
|
|
actual = secs_to_uptime(1);
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_str_op(actual, OP_EQ, expected);
|
2014-04-15 14:20:34 +02:00
|
|
|
tor_free(actual);
|
|
|
|
|
|
|
|
expected = "0:01 hours";
|
|
|
|
actual = secs_to_uptime(60);
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_str_op(actual, OP_EQ, expected);
|
2014-04-15 14:20:34 +02:00
|
|
|
tor_free(actual);
|
|
|
|
|
|
|
|
expected = "0:59 hours";
|
|
|
|
actual = secs_to_uptime(60 * 59);
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_str_op(actual, OP_EQ, expected);
|
2014-04-15 14:20:34 +02:00
|
|
|
tor_free(actual);
|
|
|
|
|
|
|
|
expected = "1:00 hours";
|
|
|
|
actual = secs_to_uptime(60 * 60);
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_str_op(actual, OP_EQ, expected);
|
2014-04-15 14:20:34 +02:00
|
|
|
tor_free(actual);
|
|
|
|
|
|
|
|
expected = "23:59 hours";
|
|
|
|
actual = secs_to_uptime(60 * 60 * 23 + 60 * 59);
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_str_op(actual, OP_EQ, expected);
|
2014-04-15 14:20:34 +02:00
|
|
|
tor_free(actual);
|
|
|
|
|
|
|
|
expected = "1 day 0:00 hours";
|
|
|
|
actual = secs_to_uptime(60 * 60 * 23 + 60 * 60);
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_str_op(actual, OP_EQ, expected);
|
2014-04-15 14:20:34 +02:00
|
|
|
tor_free(actual);
|
|
|
|
|
|
|
|
expected = "1 day 0:00 hours";
|
|
|
|
actual = secs_to_uptime(86400 + 1);
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_str_op(actual, OP_EQ, expected);
|
2014-04-15 14:20:34 +02:00
|
|
|
tor_free(actual);
|
|
|
|
|
|
|
|
expected = "1 day 0:01 hours";
|
|
|
|
actual = secs_to_uptime(86400 + 60);
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_str_op(actual, OP_EQ, expected);
|
2014-04-15 14:20:34 +02:00
|
|
|
tor_free(actual);
|
|
|
|
|
|
|
|
expected = "10 days 0:00 hours";
|
|
|
|
actual = secs_to_uptime(86400 * 10);
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_str_op(actual, OP_EQ, expected);
|
2014-04-15 14:20:34 +02:00
|
|
|
tor_free(actual);
|
|
|
|
|
|
|
|
expected = "10 days 0:00 hours";
|
|
|
|
actual = secs_to_uptime(864000 + 1);
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_str_op(actual, OP_EQ, expected);
|
2014-04-15 14:20:34 +02:00
|
|
|
tor_free(actual);
|
|
|
|
|
|
|
|
expected = "10 days 0:01 hours";
|
|
|
|
actual = secs_to_uptime(864000 + 60);
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_str_op(actual, OP_EQ, expected);
|
2014-04-15 14:20:34 +02:00
|
|
|
tor_free(actual);
|
|
|
|
|
|
|
|
done:
|
|
|
|
if (actual != NULL)
|
|
|
|
tor_free(actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef NS_SUBMODULE
|
|
|
|
#define NS_SUBMODULE bytes_to_usage
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Test that bytes_to_usage() is correctly converting the number of bytes that
|
|
|
|
* Tor has read/written into the appropriate string form containing kilobytes,
|
|
|
|
* megabytes, or gigabytes.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
NS(test_main)(void *arg)
|
|
|
|
{
|
|
|
|
const char *expected;
|
|
|
|
char *actual;
|
|
|
|
(void)arg;
|
|
|
|
|
|
|
|
expected = "0 kB";
|
|
|
|
actual = bytes_to_usage(0);
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_str_op(actual, OP_EQ, expected);
|
2014-04-15 14:20:34 +02:00
|
|
|
tor_free(actual);
|
|
|
|
|
|
|
|
expected = "0 kB";
|
|
|
|
actual = bytes_to_usage(1);
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_str_op(actual, OP_EQ, expected);
|
2014-04-15 14:20:34 +02:00
|
|
|
tor_free(actual);
|
|
|
|
|
|
|
|
expected = "1 kB";
|
|
|
|
actual = bytes_to_usage(1024);
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_str_op(actual, OP_EQ, expected);
|
2014-04-15 14:20:34 +02:00
|
|
|
tor_free(actual);
|
|
|
|
|
|
|
|
expected = "1023 kB";
|
|
|
|
actual = bytes_to_usage((1 << 20) - 1);
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_str_op(actual, OP_EQ, expected);
|
2014-04-15 14:20:34 +02:00
|
|
|
tor_free(actual);
|
|
|
|
|
|
|
|
expected = "1.00 MB";
|
|
|
|
actual = bytes_to_usage((1 << 20));
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_str_op(actual, OP_EQ, expected);
|
2014-04-15 14:20:34 +02:00
|
|
|
tor_free(actual);
|
|
|
|
|
|
|
|
expected = "1.00 MB";
|
|
|
|
actual = bytes_to_usage((1 << 20) + 5242);
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_str_op(actual, OP_EQ, expected);
|
2014-04-15 14:20:34 +02:00
|
|
|
tor_free(actual);
|
|
|
|
|
|
|
|
expected = "1.01 MB";
|
|
|
|
actual = bytes_to_usage((1 << 20) + 5243);
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_str_op(actual, OP_EQ, expected);
|
2014-04-15 14:20:34 +02:00
|
|
|
tor_free(actual);
|
|
|
|
|
|
|
|
expected = "1024.00 MB";
|
|
|
|
actual = bytes_to_usage((1 << 30) - 1);
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_str_op(actual, OP_EQ, expected);
|
2014-04-15 14:20:34 +02:00
|
|
|
tor_free(actual);
|
|
|
|
|
|
|
|
expected = "1.00 GB";
|
|
|
|
actual = bytes_to_usage((1 << 30));
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_str_op(actual, OP_EQ, expected);
|
2014-04-15 14:20:34 +02:00
|
|
|
tor_free(actual);
|
|
|
|
|
|
|
|
expected = "1.00 GB";
|
|
|
|
actual = bytes_to_usage((1 << 30) + 5368709);
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_str_op(actual, OP_EQ, expected);
|
2014-04-15 14:20:34 +02:00
|
|
|
tor_free(actual);
|
|
|
|
|
|
|
|
expected = "1.01 GB";
|
|
|
|
actual = bytes_to_usage((1 << 30) + 5368710);
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_str_op(actual, OP_EQ, expected);
|
2014-04-15 14:20:34 +02:00
|
|
|
tor_free(actual);
|
|
|
|
|
|
|
|
expected = "10.00 GB";
|
2014-04-15 21:19:41 +02:00
|
|
|
actual = bytes_to_usage((U64_LITERAL(1) << 30) * 10L);
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_str_op(actual, OP_EQ, expected);
|
2014-04-15 14:20:34 +02:00
|
|
|
tor_free(actual);
|
|
|
|
|
|
|
|
done:
|
|
|
|
if (actual != NULL)
|
|
|
|
tor_free(actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef NS_SUBMODULE
|
|
|
|
#define NS_SUBMODULE ASPECT(log_heartbeat, fails)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Tests that log_heartbeat() fails when in the public server mode,
|
|
|
|
* not hibernating, and we couldn't get the current routerinfo.
|
|
|
|
*/
|
|
|
|
|
|
|
|
NS_DECL(double, tls_get_write_overhead_ratio, (void));
|
|
|
|
NS_DECL(int, we_are_hibernating, (void));
|
|
|
|
NS_DECL(int, public_server_mode, (const or_options_t *options));
|
|
|
|
NS_DECL(const routerinfo_t *, router_get_my_routerinfo, (void));
|
|
|
|
|
|
|
|
static void
|
|
|
|
NS(test_main)(void *arg)
|
|
|
|
{
|
|
|
|
int expected, actual;
|
|
|
|
(void)arg;
|
|
|
|
|
|
|
|
NS_MOCK(tls_get_write_overhead_ratio);
|
|
|
|
NS_MOCK(we_are_hibernating);
|
|
|
|
NS_MOCK(public_server_mode);
|
|
|
|
NS_MOCK(router_get_my_routerinfo);
|
|
|
|
|
|
|
|
expected = -1;
|
|
|
|
actual = log_heartbeat(0);
|
|
|
|
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_int_op(actual, OP_EQ, expected);
|
2014-04-15 14:20:34 +02:00
|
|
|
|
|
|
|
done:
|
|
|
|
NS_UNMOCK(tls_get_write_overhead_ratio);
|
|
|
|
NS_UNMOCK(we_are_hibernating);
|
|
|
|
NS_UNMOCK(public_server_mode);
|
|
|
|
NS_UNMOCK(router_get_my_routerinfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
static double
|
|
|
|
NS(tls_get_write_overhead_ratio)(void)
|
|
|
|
{
|
|
|
|
return 2.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
NS(we_are_hibernating)(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
NS(public_server_mode)(const or_options_t *options)
|
|
|
|
{
|
|
|
|
(void)options;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const routerinfo_t *
|
|
|
|
NS(router_get_my_routerinfo)(void)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef NS_SUBMODULE
|
|
|
|
#define NS_SUBMODULE ASPECT(log_heartbeat, not_in_consensus)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Tests that log_heartbeat() logs appropriately if we are not in the cached
|
|
|
|
* consensus.
|
|
|
|
*/
|
|
|
|
|
|
|
|
NS_DECL(double, tls_get_write_overhead_ratio, (void));
|
|
|
|
NS_DECL(int, we_are_hibernating, (void));
|
|
|
|
NS_DECL(int, public_server_mode, (const or_options_t *options));
|
|
|
|
NS_DECL(const routerinfo_t *, router_get_my_routerinfo, (void));
|
|
|
|
NS_DECL(const node_t *, node_get_by_id, (const char *identity_digest));
|
|
|
|
NS_DECL(void, logv, (int severity, log_domain_mask_t domain,
|
|
|
|
const char *funcname, const char *suffix, const char *format, va_list ap));
|
|
|
|
NS_DECL(int, server_mode, (const or_options_t *options));
|
|
|
|
|
|
|
|
static routerinfo_t *mock_routerinfo;
|
|
|
|
|
|
|
|
static void
|
|
|
|
NS(test_main)(void *arg)
|
|
|
|
{
|
|
|
|
int expected, actual;
|
|
|
|
(void)arg;
|
|
|
|
|
|
|
|
NS_MOCK(tls_get_write_overhead_ratio);
|
|
|
|
NS_MOCK(we_are_hibernating);
|
|
|
|
NS_MOCK(public_server_mode);
|
|
|
|
NS_MOCK(router_get_my_routerinfo);
|
|
|
|
NS_MOCK(node_get_by_id);
|
|
|
|
NS_MOCK(logv);
|
|
|
|
NS_MOCK(server_mode);
|
|
|
|
|
|
|
|
log_global_min_severity_ = LOG_DEBUG;
|
|
|
|
onion_handshakes_requested[ONION_HANDSHAKE_TYPE_TAP] = 1;
|
|
|
|
onion_handshakes_assigned[ONION_HANDSHAKE_TYPE_TAP] = 1;
|
|
|
|
onion_handshakes_requested[ONION_HANDSHAKE_TYPE_NTOR] = 1;
|
|
|
|
onion_handshakes_assigned[ONION_HANDSHAKE_TYPE_NTOR] = 1;
|
|
|
|
|
|
|
|
expected = 0;
|
|
|
|
actual = log_heartbeat(0);
|
|
|
|
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_int_op(actual, OP_EQ, expected);
|
2018-04-17 14:38:34 +02:00
|
|
|
tt_int_op(CALLED(logv), OP_EQ, 6);
|
2014-04-15 14:20:34 +02:00
|
|
|
|
|
|
|
done:
|
|
|
|
NS_UNMOCK(tls_get_write_overhead_ratio);
|
|
|
|
NS_UNMOCK(we_are_hibernating);
|
|
|
|
NS_UNMOCK(public_server_mode);
|
|
|
|
NS_UNMOCK(router_get_my_routerinfo);
|
|
|
|
NS_UNMOCK(node_get_by_id);
|
|
|
|
NS_UNMOCK(logv);
|
|
|
|
NS_UNMOCK(server_mode);
|
|
|
|
tor_free(mock_routerinfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
static double
|
|
|
|
NS(tls_get_write_overhead_ratio)(void)
|
|
|
|
{
|
|
|
|
return 1.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
NS(we_are_hibernating)(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
NS(public_server_mode)(const or_options_t *options)
|
|
|
|
{
|
|
|
|
(void)options;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const routerinfo_t *
|
|
|
|
NS(router_get_my_routerinfo)(void)
|
|
|
|
{
|
|
|
|
mock_routerinfo = tor_malloc(sizeof(routerinfo_t));
|
|
|
|
|
|
|
|
return mock_routerinfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const node_t *
|
|
|
|
NS(node_get_by_id)(const char *identity_digest)
|
|
|
|
{
|
|
|
|
(void)identity_digest;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
NS(logv)(int severity, log_domain_mask_t domain,
|
|
|
|
const char *funcname, const char *suffix, const char *format, va_list ap)
|
|
|
|
{
|
|
|
|
switch (CALLED(logv))
|
|
|
|
{
|
|
|
|
case 0:
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_int_op(severity, OP_EQ, LOG_NOTICE);
|
|
|
|
tt_int_op(domain, OP_EQ, LD_HEARTBEAT);
|
|
|
|
tt_ptr_op(strstr(funcname, "log_heartbeat"), OP_NE, NULL);
|
|
|
|
tt_ptr_op(suffix, OP_EQ, NULL);
|
|
|
|
tt_str_op(format, OP_EQ,
|
2014-04-15 14:20:34 +02:00
|
|
|
"Heartbeat: It seems like we are not in the cached consensus.");
|
|
|
|
break;
|
|
|
|
case 1:
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_int_op(severity, OP_EQ, LOG_NOTICE);
|
|
|
|
tt_int_op(domain, OP_EQ, LD_HEARTBEAT);
|
|
|
|
tt_ptr_op(strstr(funcname, "log_heartbeat"), OP_NE, NULL);
|
|
|
|
tt_ptr_op(suffix, OP_EQ, NULL);
|
|
|
|
tt_str_op(format, OP_EQ,
|
2014-04-15 14:20:34 +02:00
|
|
|
"Heartbeat: Tor's uptime is %s, with %d circuits open. "
|
|
|
|
"I've sent %s and received %s.%s");
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_str_op(va_arg(ap, char *), OP_EQ, "0:00 hours"); /* uptime */
|
|
|
|
tt_int_op(va_arg(ap, int), OP_EQ, 0); /* count_circuits() */
|
|
|
|
tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* bw_sent */
|
|
|
|
tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* bw_rcvd */
|
|
|
|
tt_str_op(va_arg(ap, char *), OP_EQ, ""); /* hibernating */
|
2014-04-15 14:20:34 +02:00
|
|
|
break;
|
|
|
|
case 2:
|
2015-02-24 16:05:34 +01:00
|
|
|
tt_int_op(severity, OP_EQ, LOG_INFO);
|
|
|
|
break;
|
|
|
|
case 3:
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_int_op(severity, OP_EQ, LOG_NOTICE);
|
|
|
|
tt_int_op(domain, OP_EQ, LD_HEARTBEAT);
|
2014-11-12 19:42:01 +01:00
|
|
|
tt_ptr_op(strstr(funcname, "rep_hist_log_circuit_handshake_stats"),
|
|
|
|
OP_NE, NULL);
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_ptr_op(suffix, OP_EQ, NULL);
|
|
|
|
tt_str_op(format, OP_EQ,
|
2014-04-15 14:20:34 +02:00
|
|
|
"Circuit handshake stats since last time: %d/%d TAP, %d/%d NTor.");
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_int_op(va_arg(ap, int), OP_EQ, 1); /* handshakes assigned (TAP) */
|
|
|
|
tt_int_op(va_arg(ap, int), OP_EQ, 1); /* handshakes requested (TAP) */
|
|
|
|
tt_int_op(va_arg(ap, int), OP_EQ, 1); /* handshakes assigned (NTOR) */
|
|
|
|
tt_int_op(va_arg(ap, int), OP_EQ, 1); /* handshakes requested (NTOR) */
|
2014-04-15 14:20:34 +02:00
|
|
|
break;
|
2015-03-10 15:07:41 +01:00
|
|
|
case 4:
|
|
|
|
tt_int_op(severity, OP_EQ, LOG_NOTICE);
|
|
|
|
tt_int_op(domain, OP_EQ, LD_HEARTBEAT);
|
|
|
|
tt_ptr_op(strstr(funcname, "rep_hist_log_link_protocol_counts"),
|
|
|
|
OP_NE, NULL);
|
|
|
|
break;
|
2018-04-17 14:38:34 +02:00
|
|
|
case 5:
|
|
|
|
tt_int_op(severity, OP_EQ, LOG_NOTICE);
|
|
|
|
tt_int_op(domain, OP_EQ, LD_HEARTBEAT);
|
|
|
|
tt_str_op(format, OP_EQ, "DoS mitigation since startup:%s%s%s%s");
|
|
|
|
tt_str_op(va_arg(ap, char *), OP_EQ,
|
|
|
|
" 0 circuits killed with too many cells.");
|
|
|
|
tt_str_op(va_arg(ap, char *), OP_EQ, " [cc not enabled]");
|
|
|
|
tt_str_op(va_arg(ap, char *), OP_EQ, " [conn not enabled]");
|
|
|
|
tt_str_op(va_arg(ap, char *), OP_EQ, "");
|
|
|
|
break;
|
2014-04-15 14:20:34 +02:00
|
|
|
default:
|
|
|
|
tt_abort_msg("unexpected call to logv()"); // TODO: prettyprint args
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
CALLED(logv)++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
NS(server_mode)(const or_options_t *options)
|
|
|
|
{
|
|
|
|
(void)options;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef NS_SUBMODULE
|
|
|
|
#define NS_SUBMODULE ASPECT(log_heartbeat, simple)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Tests that log_heartbeat() correctly logs heartbeat information
|
|
|
|
* normally.
|
|
|
|
*/
|
|
|
|
|
|
|
|
NS_DECL(double, tls_get_write_overhead_ratio, (void));
|
|
|
|
NS_DECL(int, we_are_hibernating, (void));
|
|
|
|
NS_DECL(int, public_server_mode, (const or_options_t *options));
|
|
|
|
NS_DECL(long, get_uptime, (void));
|
|
|
|
NS_DECL(uint64_t, get_bytes_read, (void));
|
|
|
|
NS_DECL(uint64_t, get_bytes_written, (void));
|
|
|
|
NS_DECL(void, logv, (int severity, log_domain_mask_t domain,
|
|
|
|
const char *funcname, const char *suffix, const char *format, va_list ap));
|
|
|
|
NS_DECL(int, server_mode, (const or_options_t *options));
|
|
|
|
|
2015-02-24 16:05:34 +01:00
|
|
|
static int NS(n_msgs) = 0;
|
|
|
|
|
2014-04-15 14:20:34 +02:00
|
|
|
static void
|
|
|
|
NS(test_main)(void *arg)
|
|
|
|
{
|
|
|
|
int expected, actual;
|
|
|
|
(void)arg;
|
|
|
|
|
|
|
|
NS_MOCK(tls_get_write_overhead_ratio);
|
|
|
|
NS_MOCK(we_are_hibernating);
|
|
|
|
NS_MOCK(public_server_mode);
|
|
|
|
NS_MOCK(get_uptime);
|
|
|
|
NS_MOCK(get_bytes_read);
|
|
|
|
NS_MOCK(get_bytes_written);
|
|
|
|
NS_MOCK(logv);
|
|
|
|
NS_MOCK(server_mode);
|
|
|
|
|
|
|
|
log_global_min_severity_ = LOG_DEBUG;
|
|
|
|
|
|
|
|
expected = 0;
|
|
|
|
actual = log_heartbeat(0);
|
|
|
|
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_int_op(actual, OP_EQ, expected);
|
2015-02-24 16:05:34 +01:00
|
|
|
tt_int_op(NS(n_msgs), OP_EQ, 1);
|
2014-04-15 14:20:34 +02:00
|
|
|
|
|
|
|
done:
|
|
|
|
NS_UNMOCK(tls_get_write_overhead_ratio);
|
|
|
|
NS_UNMOCK(we_are_hibernating);
|
|
|
|
NS_UNMOCK(public_server_mode);
|
|
|
|
NS_UNMOCK(get_uptime);
|
|
|
|
NS_UNMOCK(get_bytes_read);
|
|
|
|
NS_UNMOCK(get_bytes_written);
|
|
|
|
NS_UNMOCK(logv);
|
|
|
|
NS_UNMOCK(server_mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
static double
|
|
|
|
NS(tls_get_write_overhead_ratio)(void)
|
|
|
|
{
|
|
|
|
return 1.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
NS(we_are_hibernating)(void)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
NS(public_server_mode)(const or_options_t *options)
|
|
|
|
{
|
|
|
|
(void)options;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static long
|
|
|
|
NS(get_uptime)(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t
|
|
|
|
NS(get_bytes_read)(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t
|
|
|
|
NS(get_bytes_written)(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
NS(logv)(int severity, log_domain_mask_t domain, const char *funcname,
|
|
|
|
const char *suffix, const char *format, va_list ap)
|
|
|
|
{
|
2015-02-24 16:05:34 +01:00
|
|
|
if (severity == LOG_INFO)
|
|
|
|
return;
|
|
|
|
++NS(n_msgs);
|
|
|
|
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_int_op(severity, OP_EQ, LOG_NOTICE);
|
|
|
|
tt_int_op(domain, OP_EQ, LD_HEARTBEAT);
|
|
|
|
tt_ptr_op(strstr(funcname, "log_heartbeat"), OP_NE, NULL);
|
|
|
|
tt_ptr_op(suffix, OP_EQ, NULL);
|
|
|
|
tt_str_op(format, OP_EQ,
|
2014-04-15 14:20:34 +02:00
|
|
|
"Heartbeat: Tor's uptime is %s, with %d circuits open. "
|
|
|
|
"I've sent %s and received %s.%s");
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_str_op(va_arg(ap, char *), OP_EQ, "0:00 hours"); /* uptime */
|
|
|
|
tt_int_op(va_arg(ap, int), OP_EQ, 0); /* count_circuits() */
|
|
|
|
tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* bw_sent */
|
|
|
|
tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* bw_rcvd */
|
|
|
|
tt_str_op(va_arg(ap, char *), OP_EQ, " We are currently hibernating.");
|
2014-04-15 14:20:34 +02:00
|
|
|
|
|
|
|
done:
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
NS(server_mode)(const or_options_t *options)
|
|
|
|
{
|
|
|
|
(void)options;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef NS_SUBMODULE
|
|
|
|
#define NS_SUBMODULE ASPECT(log_heartbeat, calls_log_accounting)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Tests that log_heartbeat() correctly logs heartbeat information
|
|
|
|
* and accounting information when configured.
|
|
|
|
*/
|
|
|
|
|
|
|
|
NS_DECL(double, tls_get_write_overhead_ratio, (void));
|
|
|
|
NS_DECL(int, we_are_hibernating, (void));
|
|
|
|
NS_DECL(int, public_server_mode, (const or_options_t *options));
|
|
|
|
NS_DECL(long, get_uptime, (void));
|
|
|
|
NS_DECL(uint64_t, get_bytes_read, (void));
|
|
|
|
NS_DECL(uint64_t, get_bytes_written, (void));
|
|
|
|
NS_DECL(void, logv, (int severity, log_domain_mask_t domain,
|
|
|
|
const char *funcname, const char *suffix, const char *format, va_list ap));
|
|
|
|
NS_DECL(int, server_mode, (const or_options_t *options));
|
|
|
|
NS_DECL(or_state_t *, get_or_state, (void));
|
|
|
|
NS_DECL(int, accounting_is_enabled, (const or_options_t *options));
|
|
|
|
NS_DECL(time_t, accounting_get_end_time, (void));
|
|
|
|
|
2014-04-26 19:39:15 +02:00
|
|
|
static or_state_t * NS(mock_state) = NULL;
|
|
|
|
static or_options_t * NS(mock_options) = NULL;
|
|
|
|
|
2014-04-15 14:20:34 +02:00
|
|
|
static void
|
|
|
|
NS(test_main)(void *arg)
|
|
|
|
{
|
|
|
|
int expected, actual;
|
|
|
|
(void)arg;
|
|
|
|
|
|
|
|
NS_MOCK(tls_get_write_overhead_ratio);
|
|
|
|
NS_MOCK(we_are_hibernating);
|
|
|
|
NS_MOCK(public_server_mode);
|
|
|
|
NS_MOCK(get_uptime);
|
|
|
|
NS_MOCK(get_bytes_read);
|
|
|
|
NS_MOCK(get_bytes_written);
|
|
|
|
NS_MOCK(logv);
|
|
|
|
NS_MOCK(server_mode);
|
|
|
|
NS_MOCK(get_or_state);
|
|
|
|
NS_MOCK(accounting_is_enabled);
|
|
|
|
NS_MOCK(accounting_get_end_time);
|
|
|
|
|
|
|
|
log_global_min_severity_ = LOG_DEBUG;
|
|
|
|
|
|
|
|
expected = 0;
|
|
|
|
actual = log_heartbeat(0);
|
|
|
|
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_int_op(actual, OP_EQ, expected);
|
2015-02-24 16:05:34 +01:00
|
|
|
tt_int_op(CALLED(logv), OP_EQ, 3);
|
2014-04-15 14:20:34 +02:00
|
|
|
|
|
|
|
done:
|
|
|
|
NS_UNMOCK(tls_get_write_overhead_ratio);
|
|
|
|
NS_UNMOCK(we_are_hibernating);
|
|
|
|
NS_UNMOCK(public_server_mode);
|
|
|
|
NS_UNMOCK(get_uptime);
|
|
|
|
NS_UNMOCK(get_bytes_read);
|
|
|
|
NS_UNMOCK(get_bytes_written);
|
|
|
|
NS_UNMOCK(logv);
|
|
|
|
NS_UNMOCK(server_mode);
|
|
|
|
NS_UNMOCK(accounting_is_enabled);
|
|
|
|
NS_UNMOCK(accounting_get_end_time);
|
2014-04-26 19:39:15 +02:00
|
|
|
tor_free_(NS(mock_state));
|
|
|
|
tor_free_(NS(mock_options));
|
2014-04-15 14:20:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static double
|
|
|
|
NS(tls_get_write_overhead_ratio)(void)
|
|
|
|
{
|
|
|
|
return 1.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
NS(we_are_hibernating)(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
NS(public_server_mode)(const or_options_t *options)
|
|
|
|
{
|
|
|
|
(void)options;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static long
|
|
|
|
NS(get_uptime)(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t
|
|
|
|
NS(get_bytes_read)(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t
|
|
|
|
NS(get_bytes_written)(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
NS(logv)(int severity, log_domain_mask_t domain,
|
|
|
|
const char *funcname, const char *suffix, const char *format, va_list ap)
|
|
|
|
{
|
|
|
|
switch (CALLED(logv))
|
|
|
|
{
|
|
|
|
case 0:
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_int_op(severity, OP_EQ, LOG_NOTICE);
|
|
|
|
tt_int_op(domain, OP_EQ, LD_HEARTBEAT);
|
|
|
|
tt_ptr_op(strstr(funcname, "log_heartbeat"), OP_NE, NULL);
|
|
|
|
tt_ptr_op(suffix, OP_EQ, NULL);
|
|
|
|
tt_str_op(format, OP_EQ,
|
2014-04-15 14:20:34 +02:00
|
|
|
"Heartbeat: Tor's uptime is %s, with %d circuits open. "
|
|
|
|
"I've sent %s and received %s.%s");
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_str_op(va_arg(ap, char *), OP_EQ, "0:00 hours"); /* uptime */
|
|
|
|
tt_int_op(va_arg(ap, int), OP_EQ, 0); /* count_circuits() */
|
|
|
|
tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* bw_sent */
|
|
|
|
tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* bw_rcvd */
|
|
|
|
tt_str_op(va_arg(ap, char *), OP_EQ, ""); /* hibernating */
|
2014-04-15 14:20:34 +02:00
|
|
|
break;
|
|
|
|
case 1:
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_int_op(severity, OP_EQ, LOG_NOTICE);
|
|
|
|
tt_int_op(domain, OP_EQ, LD_HEARTBEAT);
|
|
|
|
tt_ptr_op(strstr(funcname, "log_accounting"), OP_NE, NULL);
|
|
|
|
tt_ptr_op(suffix, OP_EQ, NULL);
|
|
|
|
tt_str_op(format, OP_EQ,
|
2016-01-04 05:02:44 +01:00
|
|
|
"Heartbeat: Accounting enabled. Sent: %s, Received: %s, Used: %s / "
|
|
|
|
"%s, Rule: %s. The current accounting interval ends on %s, in %s.");
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* acc_sent */
|
|
|
|
tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* acc_rcvd */
|
2016-01-04 05:02:44 +01:00
|
|
|
tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* acc_used */
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* acc_max */
|
2016-01-04 05:02:44 +01:00
|
|
|
tt_str_op(va_arg(ap, char *), OP_EQ, "max"); /* acc_rule */
|
2016-01-12 08:39:16 +01:00
|
|
|
/* format_local_iso_time uses local tz, so we can't just compare
|
|
|
|
* the string against a constant */
|
|
|
|
char datetime[ISO_TIME_LEN+1];
|
|
|
|
format_local_iso_time(datetime, 60);
|
|
|
|
tt_str_op(va_arg(ap, char *), OP_EQ, datetime); /* end_buf */
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_str_op(va_arg(ap, char *), OP_EQ, "0:01 hours"); /* remaining */
|
2014-04-15 14:20:34 +02:00
|
|
|
break;
|
2015-02-24 16:05:34 +01:00
|
|
|
case 2:
|
|
|
|
tt_int_op(severity, OP_EQ, LOG_INFO);
|
|
|
|
break;
|
2014-04-15 14:20:34 +02:00
|
|
|
default:
|
|
|
|
tt_abort_msg("unexpected call to logv()"); // TODO: prettyprint args
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
CALLED(logv)++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
NS(server_mode)(const or_options_t *options)
|
|
|
|
{
|
|
|
|
(void)options;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
NS(accounting_is_enabled)(const or_options_t *options)
|
|
|
|
{
|
|
|
|
(void)options;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static time_t
|
|
|
|
NS(accounting_get_end_time)(void)
|
|
|
|
{
|
|
|
|
return 60;
|
|
|
|
}
|
|
|
|
|
|
|
|
static or_state_t *
|
|
|
|
NS(get_or_state)(void)
|
|
|
|
{
|
2014-04-26 19:39:15 +02:00
|
|
|
NS(mock_state) = tor_malloc_zero(sizeof(or_state_t));
|
|
|
|
NS(mock_state)->AccountingBytesReadInInterval = 0;
|
|
|
|
NS(mock_state)->AccountingBytesWrittenInInterval = 0;
|
2014-04-15 14:20:34 +02:00
|
|
|
|
2014-04-26 19:39:15 +02:00
|
|
|
return NS(mock_state);
|
2014-04-15 14:20:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#undef NS_SUBMODULE
|
|
|
|
#define NS_SUBMODULE ASPECT(log_heartbeat, packaged_cell_fullness)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Tests that log_heartbeat() correctly logs packaged cell
|
|
|
|
* fullness information.
|
|
|
|
*/
|
|
|
|
|
|
|
|
NS_DECL(double, tls_get_write_overhead_ratio, (void));
|
|
|
|
NS_DECL(int, we_are_hibernating, (void));
|
|
|
|
NS_DECL(int, public_server_mode, (const or_options_t *options));
|
|
|
|
NS_DECL(long, get_uptime, (void));
|
|
|
|
NS_DECL(uint64_t, get_bytes_read, (void));
|
|
|
|
NS_DECL(uint64_t, get_bytes_written, (void));
|
|
|
|
NS_DECL(void, logv, (int severity, log_domain_mask_t domain,
|
|
|
|
const char *funcname, const char *suffix, const char *format, va_list ap));
|
|
|
|
NS_DECL(int, server_mode, (const or_options_t *options));
|
|
|
|
NS_DECL(int, accounting_is_enabled, (const or_options_t *options));
|
|
|
|
|
|
|
|
static void
|
|
|
|
NS(test_main)(void *arg)
|
|
|
|
{
|
|
|
|
int expected, actual;
|
|
|
|
(void)arg;
|
|
|
|
|
|
|
|
NS_MOCK(tls_get_write_overhead_ratio);
|
|
|
|
NS_MOCK(we_are_hibernating);
|
|
|
|
NS_MOCK(public_server_mode);
|
|
|
|
NS_MOCK(get_uptime);
|
|
|
|
NS_MOCK(get_bytes_read);
|
|
|
|
NS_MOCK(get_bytes_written);
|
|
|
|
NS_MOCK(logv);
|
|
|
|
NS_MOCK(server_mode);
|
|
|
|
NS_MOCK(accounting_is_enabled);
|
|
|
|
log_global_min_severity_ = LOG_DEBUG;
|
|
|
|
|
|
|
|
stats_n_data_bytes_packaged = RELAY_PAYLOAD_SIZE;
|
2015-02-24 16:05:34 +01:00
|
|
|
stats_n_data_cells_packaged = 2;
|
2014-04-15 14:20:34 +02:00
|
|
|
expected = 0;
|
|
|
|
actual = log_heartbeat(0);
|
|
|
|
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_int_op(actual, OP_EQ, expected);
|
|
|
|
tt_int_op(CALLED(logv), OP_EQ, 2);
|
2014-04-15 14:20:34 +02:00
|
|
|
|
|
|
|
done:
|
|
|
|
stats_n_data_bytes_packaged = 0;
|
|
|
|
stats_n_data_cells_packaged = 0;
|
|
|
|
NS_UNMOCK(tls_get_write_overhead_ratio);
|
|
|
|
NS_UNMOCK(we_are_hibernating);
|
|
|
|
NS_UNMOCK(public_server_mode);
|
|
|
|
NS_UNMOCK(get_uptime);
|
|
|
|
NS_UNMOCK(get_bytes_read);
|
|
|
|
NS_UNMOCK(get_bytes_written);
|
|
|
|
NS_UNMOCK(logv);
|
|
|
|
NS_UNMOCK(server_mode);
|
|
|
|
NS_UNMOCK(accounting_is_enabled);
|
|
|
|
}
|
|
|
|
|
|
|
|
static double
|
|
|
|
NS(tls_get_write_overhead_ratio)(void)
|
|
|
|
{
|
|
|
|
return 1.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
NS(we_are_hibernating)(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
NS(public_server_mode)(const or_options_t *options)
|
|
|
|
{
|
|
|
|
(void)options;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static long
|
|
|
|
NS(get_uptime)(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t
|
|
|
|
NS(get_bytes_read)(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t
|
|
|
|
NS(get_bytes_written)(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
NS(logv)(int severity, log_domain_mask_t domain, const char *funcname,
|
|
|
|
const char *suffix, const char *format, va_list ap)
|
|
|
|
{
|
|
|
|
switch (CALLED(logv))
|
|
|
|
{
|
|
|
|
case 0:
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_int_op(severity, OP_EQ, LOG_NOTICE);
|
|
|
|
tt_int_op(domain, OP_EQ, LD_HEARTBEAT);
|
|
|
|
tt_ptr_op(strstr(funcname, "log_heartbeat"), OP_NE, NULL);
|
|
|
|
tt_ptr_op(suffix, OP_EQ, NULL);
|
|
|
|
tt_str_op(format, OP_EQ,
|
2014-04-15 14:20:34 +02:00
|
|
|
"Heartbeat: Tor's uptime is %s, with %d circuits open. "
|
|
|
|
"I've sent %s and received %s.%s");
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_str_op(va_arg(ap, char *), OP_EQ, "0:00 hours"); /* uptime */
|
|
|
|
tt_int_op(va_arg(ap, int), OP_EQ, 0); /* count_circuits() */
|
|
|
|
tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* bw_sent */
|
|
|
|
tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* bw_rcvd */
|
|
|
|
tt_str_op(va_arg(ap, char *), OP_EQ, ""); /* hibernating */
|
2014-04-15 14:20:34 +02:00
|
|
|
break;
|
|
|
|
case 1:
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_int_op(severity, OP_EQ, LOG_NOTICE);
|
|
|
|
tt_int_op(domain, OP_EQ, LD_HEARTBEAT);
|
|
|
|
tt_ptr_op(strstr(funcname, "log_heartbeat"), OP_NE, NULL);
|
|
|
|
tt_ptr_op(suffix, OP_EQ, NULL);
|
|
|
|
tt_str_op(format, OP_EQ,
|
2015-03-10 15:10:35 +01:00
|
|
|
"Average packaged cell fullness: %2.3f%%. "
|
|
|
|
"TLS write overhead: %.f%%");
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_double_op(fabs(va_arg(ap, double) - 50.0), OP_LE, DBL_EPSILON);
|
|
|
|
tt_double_op(fabs(va_arg(ap, double) - 0.0), OP_LE, DBL_EPSILON);
|
2014-04-15 14:20:34 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
tt_abort_msg("unexpected call to logv()"); // TODO: prettyprint args
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
CALLED(logv)++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
NS(server_mode)(const or_options_t *options)
|
|
|
|
{
|
|
|
|
(void)options;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
NS(accounting_is_enabled)(const or_options_t *options)
|
|
|
|
{
|
|
|
|
(void)options;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef NS_SUBMODULE
|
|
|
|
#define NS_SUBMODULE ASPECT(log_heartbeat, tls_write_overhead)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Tests that log_heartbeat() correctly logs the TLS write overhead information
|
|
|
|
* when the TLS write overhead ratio exceeds 1.
|
|
|
|
*/
|
|
|
|
|
|
|
|
NS_DECL(double, tls_get_write_overhead_ratio, (void));
|
|
|
|
NS_DECL(int, we_are_hibernating, (void));
|
|
|
|
NS_DECL(int, public_server_mode, (const or_options_t *options));
|
|
|
|
NS_DECL(long, get_uptime, (void));
|
|
|
|
NS_DECL(uint64_t, get_bytes_read, (void));
|
|
|
|
NS_DECL(uint64_t, get_bytes_written, (void));
|
|
|
|
NS_DECL(void, logv, (int severity, log_domain_mask_t domain,
|
|
|
|
const char *funcname, const char *suffix, const char *format, va_list ap));
|
|
|
|
NS_DECL(int, server_mode, (const or_options_t *options));
|
|
|
|
NS_DECL(int, accounting_is_enabled, (const or_options_t *options));
|
|
|
|
|
|
|
|
static void
|
|
|
|
NS(test_main)(void *arg)
|
|
|
|
{
|
|
|
|
int expected, actual;
|
|
|
|
(void)arg;
|
|
|
|
|
|
|
|
NS_MOCK(tls_get_write_overhead_ratio);
|
|
|
|
NS_MOCK(we_are_hibernating);
|
|
|
|
NS_MOCK(public_server_mode);
|
|
|
|
NS_MOCK(get_uptime);
|
|
|
|
NS_MOCK(get_bytes_read);
|
|
|
|
NS_MOCK(get_bytes_written);
|
|
|
|
NS_MOCK(logv);
|
|
|
|
NS_MOCK(server_mode);
|
|
|
|
NS_MOCK(accounting_is_enabled);
|
|
|
|
stats_n_data_cells_packaged = 0;
|
|
|
|
log_global_min_severity_ = LOG_DEBUG;
|
|
|
|
|
|
|
|
expected = 0;
|
|
|
|
actual = log_heartbeat(0);
|
|
|
|
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_int_op(actual, OP_EQ, expected);
|
|
|
|
tt_int_op(CALLED(logv), OP_EQ, 2);
|
2014-04-15 14:20:34 +02:00
|
|
|
|
|
|
|
done:
|
|
|
|
NS_UNMOCK(tls_get_write_overhead_ratio);
|
|
|
|
NS_UNMOCK(we_are_hibernating);
|
|
|
|
NS_UNMOCK(public_server_mode);
|
|
|
|
NS_UNMOCK(get_uptime);
|
|
|
|
NS_UNMOCK(get_bytes_read);
|
|
|
|
NS_UNMOCK(get_bytes_written);
|
|
|
|
NS_UNMOCK(logv);
|
|
|
|
NS_UNMOCK(server_mode);
|
|
|
|
NS_UNMOCK(accounting_is_enabled);
|
|
|
|
}
|
|
|
|
|
|
|
|
static double
|
|
|
|
NS(tls_get_write_overhead_ratio)(void)
|
|
|
|
{
|
|
|
|
return 2.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
NS(we_are_hibernating)(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
NS(public_server_mode)(const or_options_t *options)
|
|
|
|
{
|
|
|
|
(void)options;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static long
|
|
|
|
NS(get_uptime)(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t
|
|
|
|
NS(get_bytes_read)(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t
|
|
|
|
NS(get_bytes_written)(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
NS(logv)(int severity, log_domain_mask_t domain,
|
|
|
|
const char *funcname, const char *suffix, const char *format, va_list ap)
|
|
|
|
{
|
|
|
|
switch (CALLED(logv))
|
|
|
|
{
|
|
|
|
case 0:
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_int_op(severity, OP_EQ, LOG_NOTICE);
|
|
|
|
tt_int_op(domain, OP_EQ, LD_HEARTBEAT);
|
|
|
|
tt_ptr_op(strstr(funcname, "log_heartbeat"), OP_NE, NULL);
|
|
|
|
tt_ptr_op(suffix, OP_EQ, NULL);
|
|
|
|
tt_str_op(format, OP_EQ,
|
2014-04-15 14:20:34 +02:00
|
|
|
"Heartbeat: Tor's uptime is %s, with %d circuits open. "
|
|
|
|
"I've sent %s and received %s.%s");
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_str_op(va_arg(ap, char *), OP_EQ, "0:00 hours"); /* uptime */
|
|
|
|
tt_int_op(va_arg(ap, int), OP_EQ, 0); /* count_circuits() */
|
|
|
|
tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* bw_sent */
|
|
|
|
tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* bw_rcvd */
|
|
|
|
tt_str_op(va_arg(ap, char *), OP_EQ, ""); /* hibernating */
|
2014-04-15 14:20:34 +02:00
|
|
|
break;
|
|
|
|
case 1:
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_int_op(severity, OP_EQ, LOG_NOTICE);
|
|
|
|
tt_int_op(domain, OP_EQ, LD_HEARTBEAT);
|
|
|
|
tt_ptr_op(strstr(funcname, "log_heartbeat"), OP_NE, NULL);
|
|
|
|
tt_ptr_op(suffix, OP_EQ, NULL);
|
2015-02-24 16:05:34 +01:00
|
|
|
tt_str_op(format, OP_EQ,
|
2015-03-10 15:10:35 +01:00
|
|
|
"Average packaged cell fullness: %2.3f%%. "
|
|
|
|
"TLS write overhead: %.f%%");
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_int_op(fabs(va_arg(ap, double) - 100.0) <= DBL_EPSILON, OP_EQ, 1);
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_double_op(fabs(va_arg(ap, double) - 100.0), OP_LE, DBL_EPSILON);
|
2014-04-15 14:20:34 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
tt_abort_msg("unexpected call to logv()"); // TODO: prettyprint args
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
CALLED(logv)++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
NS(server_mode)(const or_options_t *options)
|
|
|
|
{
|
|
|
|
(void)options;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
NS(accounting_is_enabled)(const or_options_t *options)
|
|
|
|
{
|
|
|
|
(void)options;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef NS_SUBMODULE
|
|
|
|
|
|
|
|
struct testcase_t status_tests[] = {
|
|
|
|
TEST_CASE(count_circuits),
|
|
|
|
TEST_CASE(secs_to_uptime),
|
|
|
|
TEST_CASE(bytes_to_usage),
|
|
|
|
TEST_CASE_ASPECT(log_heartbeat, fails),
|
|
|
|
TEST_CASE_ASPECT(log_heartbeat, simple),
|
|
|
|
TEST_CASE_ASPECT(log_heartbeat, not_in_consensus),
|
|
|
|
TEST_CASE_ASPECT(log_heartbeat, calls_log_accounting),
|
|
|
|
TEST_CASE_ASPECT(log_heartbeat, packaged_cell_fullness),
|
|
|
|
TEST_CASE_ASPECT(log_heartbeat, tls_write_overhead),
|
|
|
|
END_OF_TESTCASES
|
|
|
|
};
|