test: Move some test code to test helpers.

Move code to create connection streams and rend_data structures to
test_helpers so that we can use them from the e2e rendezvous circuit
unittests.
This commit is contained in:
George Kadianakis 2017-05-02 16:35:22 +03:00 committed by Nick Mathewson
parent 43a73f6eb6
commit dc3a2037f5
8 changed files with 133 additions and 106 deletions

View File

@ -270,6 +270,7 @@ noinst_HEADERS+= \
src/test/test.h \
src/test/test_helpers.h \
src/test/test_dir_common.h \
src/test/test_connection.h \
src/test/test_descriptors.inc \
src/test/example_extrainfo.inc \
src/test/failing_routerdescs.inc \

View File

@ -71,3 +71,19 @@ create_descriptor(rend_service_descriptor_t **generated, char **service_id,
crypto_pk_free(pk2);
}
rend_data_t *
mock_rend_data(const char *onion_address)
{
rend_data_v2_t *v2_data = tor_malloc_zero(sizeof(*v2_data));
rend_data_t *rend_query = &v2_data->base_;
rend_query->version = 2;
strlcpy(v2_data->onion_address, onion_address,
sizeof(v2_data->onion_address));
v2_data->auth_type = REND_NO_AUTH;
rend_query->hsdirs_fp = smartlist_new();
smartlist_add(rend_query->hsdirs_fp, tor_memdup("aaaaaaaaaaaaaaaaaaaaaaaa",
DIGEST_LEN));
return rend_query;
}

View File

@ -10,6 +10,7 @@ void generate_desc(int time_diff, rend_encoded_v2_service_descriptor_t **desc,
char **service_id, int intro_points);
void create_descriptor(rend_service_descriptor_t **generated,
char **service_id, int intro_points);
rend_data_t *mock_rend_data(const char *onion_address);
#endif

View File

@ -17,9 +17,8 @@
#include "rendcache.h"
#include "directory.h"
static void test_conn_lookup_addr_helper(const char *address,
int family,
tor_addr_t *addr);
#include "test_connection.h"
#include "test_helpers.h"
static void * test_conn_get_basic_setup(const struct testcase_t *tc);
static int test_conn_get_basic_teardown(const struct testcase_t *tc,
@ -62,48 +61,7 @@ static int test_conn_get_rsrc_teardown(const struct testcase_t *tc,
#define TEST_CONN_UNATTACHED_STATE (AP_CONN_STATE_CIRCUIT_WAIT)
#define TEST_CONN_ATTACHED_STATE (AP_CONN_STATE_CONNECT_WAIT)
#define TEST_CONN_FD_INIT 50
static int mock_connection_connect_sockaddr_called = 0;
static int fake_socket_number = TEST_CONN_FD_INIT;
static int
mock_connection_connect_sockaddr(connection_t *conn,
const struct sockaddr *sa,
socklen_t sa_len,
const struct sockaddr *bindaddr,
socklen_t bindaddr_len,
int *socket_error)
{
(void)sa_len;
(void)bindaddr;
(void)bindaddr_len;
tor_assert(conn);
tor_assert(sa);
tor_assert(socket_error);
mock_connection_connect_sockaddr_called++;
conn->s = fake_socket_number++;
tt_assert(SOCKET_OK(conn->s));
/* We really should call tor_libevent_initialize() here. Because we don't,
* we are relying on other parts of the code not checking if the_event_base
* (and therefore event->ev_base) is NULL. */
tt_assert(connection_add_connecting(conn) == 0);
done:
/* Fake "connected" status */
return 1;
}
static int
fake_close_socket(evutil_socket_t sock)
{
(void)sock;
return 0;
}
static void
void
test_conn_lookup_addr_helper(const char *address, int family, tor_addr_t *addr)
{
int rv = 0;
@ -122,51 +80,6 @@ test_conn_lookup_addr_helper(const char *address, int family, tor_addr_t *addr)
tor_addr_make_null(addr, TEST_CONN_FAMILY);
}
static connection_t *
test_conn_get_connection(uint8_t state, uint8_t type, uint8_t purpose)
{
connection_t *conn = NULL;
tor_addr_t addr;
int socket_err = 0;
int in_progress = 0;
MOCK(connection_connect_sockaddr,
mock_connection_connect_sockaddr);
MOCK(tor_close_socket, fake_close_socket);
init_connection_lists();
conn = connection_new(type, TEST_CONN_FAMILY);
tt_assert(conn);
test_conn_lookup_addr_helper(TEST_CONN_ADDRESS, TEST_CONN_FAMILY, &addr);
tt_assert(!tor_addr_is_null(&addr));
tor_addr_copy_tight(&conn->addr, &addr);
conn->port = TEST_CONN_PORT;
mock_connection_connect_sockaddr_called = 0;
in_progress = connection_connect(conn, TEST_CONN_ADDRESS_PORT, &addr,
TEST_CONN_PORT, &socket_err);
tt_assert(mock_connection_connect_sockaddr_called == 1);
tt_assert(!socket_err);
tt_assert(in_progress == 0 || in_progress == 1);
/* fake some of the attributes so the connection looks OK */
conn->state = state;
conn->purpose = purpose;
assert_connection_ok(conn, time(NULL));
UNMOCK(connection_connect_sockaddr);
UNMOCK(tor_close_socket);
return conn;
/* On failure */
done:
UNMOCK(connection_connect_sockaddr);
UNMOCK(tor_close_socket);
return NULL;
}
static void *
test_conn_get_basic_setup(const struct testcase_t *tc)
{

View File

@ -0,0 +1,13 @@
/* Copyright (c) 2014-2017, The Tor Project, Inc. */
/* See LICENSE for licensing information */
/** Some constants used by test_connection and helpers */
#define TEST_CONN_FAMILY (AF_INET)
#define TEST_CONN_ADDRESS "127.0.0.1"
#define TEST_CONN_PORT (12345)
#define TEST_CONN_ADDRESS_PORT "127.0.0.1:12345"
#define TEST_CONN_FD_INIT 50
void test_conn_lookup_addr_helper(const char *address,
int family, tor_addr_t *addr);

View File

@ -7,9 +7,14 @@
*/
#define ROUTERLIST_PRIVATE
#define CONNECTION_PRIVATE
#define MAIN_PRIVATE
#include "orconfig.h"
#include "or.h"
#include "connection.h"
#include "main.h"
#include "relay.h"
#include "routerlist.h"
#include "nodelist.h"
@ -17,6 +22,7 @@
#include "test.h"
#include "test_helpers.h"
#include "test_connection.h"
#ifdef HAVE_CFLAG_WOVERLENGTH_STRINGS
DISABLE_GCC_WARNING(overlength-strings)
@ -143,3 +149,93 @@ mock_tor_addr_lookup__fail_on_bad_addrs(const char *name,
return tor_addr_lookup__real(name, family, out);
}
/*********** Helper funcs for making new connections/streams *****************/
/* Helper for test_conn_get_connection() */
static int
fake_close_socket(evutil_socket_t sock)
{
(void)sock;
return 0;
}
static int mock_connection_connect_sockaddr_called = 0;
static int fake_socket_number = TEST_CONN_FD_INIT;
/* Helper for test_conn_get_connection() */
static int
mock_connection_connect_sockaddr(connection_t *conn,
const struct sockaddr *sa,
socklen_t sa_len,
const struct sockaddr *bindaddr,
socklen_t bindaddr_len,
int *socket_error)
{
(void)sa_len;
(void)bindaddr;
(void)bindaddr_len;
tor_assert(conn);
tor_assert(sa);
tor_assert(socket_error);
mock_connection_connect_sockaddr_called++;
conn->s = fake_socket_number++;
tt_assert(SOCKET_OK(conn->s));
/* We really should call tor_libevent_initialize() here. Because we don't,
* we are relying on other parts of the code not checking if the_event_base
* (and therefore event->ev_base) is NULL. */
tt_assert(connection_add_connecting(conn) == 0);
done:
/* Fake "connected" status */
return 1;
}
/** Create and return a new connection/stream */
connection_t *
test_conn_get_connection(uint8_t state, uint8_t type, uint8_t purpose)
{
connection_t *conn = NULL;
tor_addr_t addr;
int socket_err = 0;
int in_progress = 0;
MOCK(connection_connect_sockaddr,
mock_connection_connect_sockaddr);
MOCK(tor_close_socket, fake_close_socket);
init_connection_lists();
conn = connection_new(type, TEST_CONN_FAMILY);
tt_assert(conn);
test_conn_lookup_addr_helper(TEST_CONN_ADDRESS, TEST_CONN_FAMILY, &addr);
tt_assert(!tor_addr_is_null(&addr));
tor_addr_copy_tight(&conn->addr, &addr);
conn->port = TEST_CONN_PORT;
mock_connection_connect_sockaddr_called = 0;
in_progress = connection_connect(conn, TEST_CONN_ADDRESS_PORT, &addr,
TEST_CONN_PORT, &socket_err);
tt_assert(mock_connection_connect_sockaddr_called == 1);
tt_assert(!socket_err);
tt_assert(in_progress == 0 || in_progress == 1);
/* fake some of the attributes so the connection looks OK */
conn->state = state;
conn->purpose = purpose;
assert_connection_ok(conn, time(NULL));
UNMOCK(connection_connect_sockaddr);
UNMOCK(tor_close_socket);
return conn;
/* On failure */
done:
UNMOCK(connection_connect_sockaddr);
UNMOCK(tor_close_socket);
return NULL;
}

View File

@ -20,6 +20,9 @@ void connection_write_to_buf_mock(const char *string, size_t len,
int mock_tor_addr_lookup__fail_on_bad_addrs(const char *name,
uint16_t family, tor_addr_t *out);
connection_t *test_conn_get_connection(uint8_t state,
uint8_t type, uint8_t purpose);
extern const char TEST_DESCRIPTORS[];
#endif

View File

@ -21,22 +21,6 @@ static const int TIME_IN_THE_PAST = -(REND_CACHE_MAX_AGE + \
REND_CACHE_MAX_SKEW + 60);
static const int TIME_IN_THE_FUTURE = REND_CACHE_MAX_SKEW + 60;
static rend_data_t *
mock_rend_data(const char *onion_address)
{
rend_data_v2_t *v2_data = tor_malloc_zero(sizeof(*v2_data));
rend_data_t *rend_query = &v2_data->base_;
rend_query->version = 2;
strlcpy(v2_data->onion_address, onion_address,
sizeof(v2_data->onion_address));
v2_data->auth_type = REND_NO_AUTH;
rend_query->hsdirs_fp = smartlist_new();
smartlist_add(rend_query->hsdirs_fp, tor_memdup("aaaaaaaaaaaaaaaaaaaaaaaa",
DIGEST_LEN));
return rend_query;
}
static void
test_rend_cache_lookup_entry(void *data)
{