Test for check_network_participation_callback()

This commit is contained in:
Nick Mathewson
2018-11-19 16:31:50 -05:00
parent 55512ef022
commit 02843c4a4e
7 changed files with 98 additions and 8 deletions
+2 -2
View File
@@ -4433,8 +4433,8 @@ connection_get_by_type_state(int type, int state)
* Return a connection of type <b>type</b> that is not an internally linked
* connection, and is not marked for close.
**/
connection_t *
connection_get_by_type_nonlinked(int type)
MOCK_IMPL(connection_t *,
connection_get_by_type_nonlinked,(int type))
{
CONN_GET_TEMPLATE(conn, conn->type == type && !conn->linked);
}
+1 -1
View File
@@ -240,7 +240,7 @@ size_t connection_get_outbuf_len(connection_t *conn);
connection_t *connection_get_by_global_id(uint64_t id);
connection_t *connection_get_by_type(int type);
connection_t *connection_get_by_type_nonlinked(int type);
MOCK_DECL(connection_t *,connection_get_by_type_nonlinked,(int type));
MOCK_DECL(connection_t *,connection_get_by_type_addr_port_purpose,(int type,
const tor_addr_t *addr,
uint16_t port, int purpose));
+1 -2
View File
@@ -1367,7 +1367,6 @@ CALLBACK(write_bridge_ns);
CALLBACK(write_stats_file);
CALLBACK(control_per_second_events);
CALLBACK(second_elapsed);
CALLBACK(check_network_participation);
#undef CALLBACK
@@ -2003,7 +2002,7 @@ add_entropy_callback(time_t now, const or_options_t *options)
/** Periodic callback: if there has been no network usage in a while,
* enter a dormant state. */
static int
STATIC int
check_network_participation_callback(time_t now, const or_options_t *options)
{
/* If we're a server, we can't become dormant. */
+3
View File
@@ -104,6 +104,9 @@ STATIC void close_closeable_connections(void);
STATIC void initialize_periodic_events(void);
STATIC void teardown_periodic_events(void);
STATIC int get_my_roles(const or_options_t *);
STATIC int check_network_participation_callback(time_t now,
const or_options_t *options);
#ifdef TOR_UNIT_TESTS
extern smartlist_t *connection_array;
+2 -2
View File
@@ -3667,8 +3667,8 @@ hs_service_lookup_current_desc(const ed25519_public_key_t *pk)
}
/* Return the number of service we have configured and usable. */
unsigned int
hs_service_get_num_services(void)
MOCK_IMPL(unsigned int,
hs_service_get_num_services,(void))
{
if (hs_service_map == NULL) {
return 0;
+1 -1
View File
@@ -310,7 +310,7 @@ hs_service_t *hs_service_new(const or_options_t *options);
void hs_service_free_(hs_service_t *service);
#define hs_service_free(s) FREE_AND_NULL(hs_service_t, hs_service_free_, (s))
unsigned int hs_service_get_num_services(void);
MOCK_DECL(unsigned int, hs_service_get_num_services,(void));
void hs_service_stage_services(const smartlist_t *service_list);
int hs_service_load_all_keys(void);
int hs_service_get_version_from_key(const hs_service_t *service);
+88
View File
@@ -6,13 +6,21 @@
* \brief Tests for functions closely related to the Tor main loop
*/
#define CONFIG_PRIVATE
#define MAINLOOP_PRIVATE
#include "test/test.h"
#include "test/log_test_helpers.h"
#include "core/or/or.h"
#include "core/mainloop/connection.h"
#include "core/mainloop/mainloop.h"
#include "core/mainloop/netstatus.h"
#include "feature/hs/hs_service.h"
#include "app/config/config.h"
static const uint64_t BILLION = 1000000000;
static void
@@ -186,6 +194,85 @@ test_mainloop_user_activity(void *arg)
UNMOCK(schedule_rescan_periodic_events);
}
static unsigned int
mock_get_num_services(void)
{
return 1;
}
static connection_t *
mock_connection_gbtu(int type)
{
(void) type;
return (void *)"hello fellow connections";
}
static void
test_mainloop_check_participation(void *arg)
{
(void)arg;
or_options_t *options = options_new();
const time_t start = 1542658829;
const time_t ONE_DAY = 24*60*60;
// Suppose we've been idle for a day or two
reset_user_activity(start - 2*ONE_DAY);
set_network_participation(true);
check_network_participation_callback(start, options);
tt_int_op(is_participating_on_network(), OP_EQ, false);
tt_i64_op(get_last_user_activity_time(), OP_EQ, start-2*ONE_DAY);
// suppose we've been idle for 2 days... but we are a server.
reset_user_activity(start - 2*ONE_DAY);
options->ORPort_set = 1;
set_network_participation(true);
check_network_participation_callback(start+2, options);
tt_int_op(is_participating_on_network(), OP_EQ, true);
tt_i64_op(get_last_user_activity_time(), OP_EQ, start+2);
options->ORPort_set = 0;
// idle for 2 days, but we have a hidden service.
reset_user_activity(start - 2*ONE_DAY);
set_network_participation(true);
MOCK(hs_service_get_num_services, mock_get_num_services);
check_network_participation_callback(start+3, options);
tt_int_op(is_participating_on_network(), OP_EQ, true);
tt_i64_op(get_last_user_activity_time(), OP_EQ, start+3);
UNMOCK(hs_service_get_num_services);
// idle for 2 days but we have at least one user connection
MOCK(connection_get_by_type_nonlinked, mock_connection_gbtu);
reset_user_activity(start - 2*ONE_DAY);
set_network_participation(true);
options->DormantTimeoutDisabledByIdleStreams = 1;
check_network_participation_callback(start+10, options);
tt_int_op(is_participating_on_network(), OP_EQ, true);
tt_i64_op(get_last_user_activity_time(), OP_EQ, start+10);
// as above, but DormantTimeoutDisabledByIdleStreams is not set
reset_user_activity(start - 2*ONE_DAY);
set_network_participation(true);
options->DormantTimeoutDisabledByIdleStreams = 0;
check_network_participation_callback(start+13, options);
tt_int_op(is_participating_on_network(), OP_EQ, false);
tt_i64_op(get_last_user_activity_time(), OP_EQ, start-2*ONE_DAY);
UNMOCK(connection_get_by_type_nonlinked);
options->DormantTimeoutDisabledByIdleStreams = 1;
// idle for 2 days but DormantClientTimeout is 3 days
reset_user_activity(start - 2*ONE_DAY);
set_network_participation(true);
options->DormantClientTimeout = ONE_DAY * 3;
check_network_participation_callback(start+30, options);
tt_int_op(is_participating_on_network(), OP_EQ, true);
tt_i64_op(get_last_user_activity_time(), OP_EQ, start-2*ONE_DAY);
done:
or_options_free(options);
UNMOCK(hs_service_get_num_services);
UNMOCK(connection_get_by_type_nonlinked);
}
#define MAINLOOP_TEST(name) \
{ #name, test_mainloop_## name , TT_FORK, NULL, NULL }
@@ -193,5 +280,6 @@ struct testcase_t mainloop_tests[] = {
MAINLOOP_TEST(update_time_normal),
MAINLOOP_TEST(update_time_jumps),
MAINLOOP_TEST(user_activity),
MAINLOOP_TEST(check_participation),
END_OF_TESTCASES
};