Add unittest for HiddenServiceExportCircuitID.

Had to move a function to test helpers.
This commit is contained in:
George Kadianakis 2018-09-15 16:33:05 +03:00
parent 6069185bcc
commit b2092f1ced
5 changed files with 94 additions and 16 deletions

View File

@ -26,6 +26,7 @@
#include "lib/cc/compat_compiler.h"
#include "lib/cc/torint.h"
#include "lib/container/map.h"
#include "lib/container/buffers.h"
#include "lib/container/smartlist.h"
#include "lib/crypt_ops/crypto_cipher.h"
#include "lib/crypt_ops/crypto_rsa.h"

View File

@ -17,6 +17,7 @@
#include "core/or/or_connection_st.h"
#include "test/test.h"
#include "test/test_helpers.h"
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
@ -89,22 +90,6 @@ connection_write_to_buf_impl_replacement(const char *string, size_t len,
buf_add(conn->outbuf, string, len);
}
static char *
buf_get_contents(buf_t *buf, size_t *sz_out)
{
char *out;
*sz_out = buf_datalen(buf);
if (*sz_out >= ULONG_MAX)
return NULL; /* C'mon, really? */
out = tor_malloc(*sz_out + 1);
if (buf_get_bytes(buf, out, (unsigned long)*sz_out) != 0) {
tor_free(out);
return NULL;
}
out[*sz_out] = '\0'; /* Hopefully gratuitous. */
return out;
}
static void
test_ext_or_write_command(void *arg)
{

View File

@ -125,6 +125,25 @@ connection_write_to_buf_mock(const char *string, size_t len,
buf_add(conn->outbuf, string, len);
}
char *
buf_get_contents(buf_t *buf, size_t *sz_out)
{
tor_assert(buf);
tor_assert(sz_out);
char *out;
*sz_out = buf_datalen(buf);
if (*sz_out >= ULONG_MAX)
return NULL; /* C'mon, really? */
out = tor_malloc(*sz_out + 1);
if (buf_get_bytes(buf, out, (unsigned long)*sz_out) != 0) {
tor_free(out);
return NULL;
}
out[*sz_out] = '\0'; /* Hopefully gratuitous. */
return out;
}
/* Set up a fake origin circuit with the specified number of cells,
* Return a pointer to the newly-created dummy circuit */
circuit_t *

View File

@ -4,6 +4,8 @@
#ifndef TOR_TEST_HELPERS_H
#define TOR_TEST_HELPERS_H
#define BUFFERS_PRIVATE
#include "core/or/or.h"
const char *get_yesterday_date_str(void);
@ -18,6 +20,7 @@ void helper_setup_fake_routerlist(void);
#define GET(path) "GET " path " HTTP/1.0\r\n\r\n"
void connection_write_to_buf_mock(const char *string, size_t len,
connection_t *conn, int compressed);
char *buf_get_contents(buf_t *buf, size_t *sz_out);
int mock_tor_addr_lookup__fail_on_bad_addrs(const char *name,
uint16_t family, tor_addr_t *out);

View File

@ -10,6 +10,7 @@
#define CIRCUITLIST_PRIVATE
#define CONFIG_PRIVATE
#define CONNECTION_PRIVATE
#define CONNECTION_EDGE_PRIVATE
#define CRYPTO_PRIVATE
#define HS_COMMON_PRIVATE
#define HS_SERVICE_PRIVATE
@ -33,6 +34,9 @@
#include "core/or/circuitbuild.h"
#include "core/or/circuitlist.h"
#include "core/or/circuituse.h"
#include "core/mainloop/connection.h"
#include "core/or/connection_edge.h"
#include "core/or/edge_connection_st.h"
#include "lib/crypt_ops/crypto_rand.h"
#include "lib/fs/dir.h"
#include "feature/dirauth/dirvote.h"
@ -2003,6 +2007,70 @@ test_authorized_client_config_equal(void *arg)
tor_free(config2);
}
/** Test that client circuit ID gets correctly exported */
static void
test_export_client_circuit_id(void *arg)
{
origin_circuit_t *or_circ = NULL;
size_t sz;
char *cp1=NULL, *cp2=NULL;
connection_t *conn = NULL;
(void) arg;
MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
hs_service_init();
/* Create service */
hs_service_t *service = helper_create_service();
/* Check that export circuit ID detection works */
service->config.export_circuit_id = false;
tt_int_op(0, OP_EQ,
hs_service_exports_circuit_id(&service->keys.identity_pk));
service->config.export_circuit_id = true;
tt_int_op(1, OP_EQ,
hs_service_exports_circuit_id(&service->keys.identity_pk));
/* Create client connection */
conn = test_conn_get_connection(AP_CONN_STATE_CIRCUIT_WAIT, CONN_TYPE_AP, 0);
/* Create client edge conn hs_ident */
edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
edge_conn->hs_ident = hs_ident_edge_conn_new(&service->keys.identity_pk);
edge_conn->hs_ident->orig_virtual_port = 42;
/* Create rend circuit */
or_circ = origin_circuit_new();
or_circ->base_.purpose = CIRCUIT_PURPOSE_C_REND_JOINED;
edge_conn->on_circuit = TO_CIRCUIT(or_circ);
or_circ->global_identifier = 666;
/* Export circuit ID */
export_hs_client_circuit_id_haproxy(edge_conn, conn);
/* Check contents */
cp1 = buf_get_contents(conn->outbuf, &sz);
tt_str_op(cp1, OP_EQ,
"PROXY TCP6 fc00:dead:beef:4dad::0:29a ::1 666 42\r\n");
/* Change circ GID and see that the reported circuit ID also changes */
or_circ->global_identifier = 22;
/* check changes */
export_hs_client_circuit_id_haproxy(edge_conn, conn);
cp2 = buf_get_contents(conn->outbuf, &sz);
tt_str_op(cp1, OP_NE, cp2);
done:
UNMOCK(connection_write_to_buf_impl_);
circuit_free_(TO_CIRCUIT(or_circ));
connection_free_minimal(conn);
hs_service_free(service);
tor_free(cp1);
tor_free(cp2);
}
struct testcase_t hs_service_tests[] = {
{ "e2e_rend_circuit_setup", test_e2e_rend_circuit_setup, TT_FORK,
NULL, NULL },
@ -2044,6 +2112,8 @@ struct testcase_t hs_service_tests[] = {
NULL, NULL },
{ "authorized_client_config_equal", test_authorized_client_config_equal,
TT_FORK, NULL, NULL },
{ "export_client_circuit_id", test_export_client_circuit_id, TT_FORK,
NULL, NULL },
END_OF_TESTCASES
};