Add tests for bootstrap tracker

Part of ticket 27617.
This commit is contained in:
Taylor Yu 2018-12-20 09:21:16 -06:00 committed by Nick Mathewson
parent 85542ee5a0
commit f0f971409a
5 changed files with 249 additions and 1 deletions

View File

@ -94,6 +94,7 @@ src_test_test_SOURCES += \
src/test/test_address.c \
src/test/test_address_set.c \
src/test/test_bridges.c \
src/test/test_btrack.c \
src/test/test_buffers.c \
src/test/test_bwmgt.c \
src/test/test_cell_formats.c \

View File

@ -857,6 +857,7 @@ struct testgroup_t testgroups[] = {
{ "consdiffmgr/", consdiffmgr_tests },
{ "container/", container_tests },
{ "control/", controller_tests },
{ "control/btrack/", btrack_tests },
{ "control/event/", controller_event_tests },
{ "crypto/", crypto_tests },
{ "crypto/ope/", crypto_ope_tests },

View File

@ -180,6 +180,7 @@ extern struct testcase_t addr_tests[];
extern struct testcase_t address_set_tests[];
extern struct testcase_t address_tests[];
extern struct testcase_t bridges_tests[];
extern struct testcase_t btrack_tests[];
extern struct testcase_t buffer_tests[];
extern struct testcase_t bwmgt_tests[];
extern struct testcase_t cell_format_tests[];

100
src/test/test_btrack.c Normal file
View File

@ -0,0 +1,100 @@
/* Copyright (c) 2013-2018, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#include "core/or/or.h"
#include "test/test.h"
#include "test/log_test_helpers.h"
#define OCIRC_EVENT_PRIVATE
#define ORCONN_EVENT_PRIVATE
#include "core/or/ocirc_event.h"
#include "core/or/orconn_event.h"
static void
test_btrack_launch(void *arg)
{
orconn_event_msg_t conn;
ocirc_event_msg_t circ;
(void)arg;
conn.type = ORCONN_MSGTYPE_STATE;
conn.u.state.gid = 1;
conn.u.state.chan = 1;
conn.u.state.proxy_type = PROXY_NONE;
conn.u.state.state = OR_CONN_STATE_CONNECTING;
setup_full_capture_of_logs(LOG_DEBUG);
orconn_event_publish(&conn);
expect_log_msg_containing("ORCONN gid=1 chan=1 proxy_type=0 state=1");
expect_no_log_msg_containing("ORCONN BEST_");
teardown_capture_of_logs();
circ.type = OCIRC_MSGTYPE_CHAN;
circ.u.chan.chan = 1;
circ.u.chan.onehop = true;
setup_full_capture_of_logs(LOG_DEBUG);
ocirc_event_publish(&circ);
expect_log_msg_containing("ORCONN LAUNCH chan=1 onehop=1");
expect_log_msg_containing("ORCONN BEST_ANY state -1->1 gid=1");
teardown_capture_of_logs();
conn.u.state.gid = 2;
conn.u.state.chan = 2;
setup_full_capture_of_logs(LOG_DEBUG);
orconn_event_publish(&conn);
expect_log_msg_containing("ORCONN gid=2 chan=2 proxy_type=0 state=1");
expect_no_log_msg_containing("ORCONN BEST_");
teardown_capture_of_logs();
circ.u.chan.chan = 2;
circ.u.chan.onehop = false;
setup_full_capture_of_logs(LOG_DEBUG);
ocirc_event_publish(&circ);
expect_log_msg_containing("ORCONN LAUNCH chan=2 onehop=0");
expect_log_msg_containing("ORCONN BEST_AP state -1->1 gid=2");
teardown_capture_of_logs();
done:
;
}
static void
test_btrack_delete(void *arg)
{
orconn_event_msg_t conn;
(void)arg;
conn.type = ORCONN_MSGTYPE_STATE;
conn.u.state.gid = 1;
conn.u.state.chan = 1;
conn.u.state.proxy_type = PROXY_NONE;
conn.u.state.state = OR_CONN_STATE_CONNECTING;
setup_full_capture_of_logs(LOG_DEBUG);
orconn_event_publish(&conn);
expect_log_msg_containing("ORCONN gid=1 chan=1 proxy_type=0");
teardown_capture_of_logs();
conn.type = ORCONN_MSGTYPE_STATUS;
conn.u.status.gid = 1;
conn.u.status.status = OR_CONN_EVENT_CLOSED;
conn.u.status.reason = 0;
setup_full_capture_of_logs(LOG_DEBUG);
orconn_event_publish(&conn);
expect_log_msg_containing("ORCONN DELETE gid=1 status=3 reason=0");
teardown_capture_of_logs();
done:
;
}
struct testcase_t btrack_tests[] = {
{ "launch", test_btrack_launch, TT_FORK, 0, NULL },
{ "delete", test_btrack_delete, TT_FORK, 0, NULL },
END_OF_TESTCASES
};

View File

@ -4,10 +4,14 @@
#define CONNECTION_PRIVATE
#define TOR_CHANNEL_INTERNAL_
#define CONTROL_PRIVATE
#define OCIRC_EVENT_PRIVATE
#define ORCONN_EVENT_PRIVATE
#include "core/or/or.h"
#include "core/or/channel.h"
#include "core/or/channeltls.h"
#include "core/or/circuitlist.h"
#include "core/or/ocirc_event.h"
#include "core/or/orconn_event.h"
#include "core/mainloop/connection.h"
#include "feature/control/control.h"
#include "test/test.h"
@ -388,6 +392,144 @@ test_cntev_dirboot_defer_orconn(void *arg)
UNMOCK(queue_control_event_string);
}
static void
setup_orconn_state(orconn_event_msg_t *msg, uint64_t gid, uint64_t chan,
int proxy_type)
{
msg->type = ORCONN_MSGTYPE_STATE;
msg->u.state.gid = gid;
msg->u.state.chan = chan;
msg->u.state.proxy_type = proxy_type;
}
static void
send_orconn_state(orconn_event_msg_t *msg, uint8_t state)
{
msg->u.state.state = state;
orconn_event_publish(msg);
}
static void
send_ocirc_chan(uint32_t gid, uint64_t chan, bool onehop)
{
ocirc_event_msg_t msg;
msg.type = OCIRC_MSGTYPE_CHAN;
msg.u.chan.gid = gid;
msg.u.chan.chan = chan;
msg.u.chan.onehop = onehop;
ocirc_event_publish(&msg);
}
static void
test_cntev_orconn_state(void *arg)
{
orconn_event_msg_t conn;
(void)arg;
MOCK(queue_control_event_string, mock_queue_control_event_string);
control_testing_set_global_event_mask(EVENT_MASK_(EVENT_STATUS_CLIENT));
setup_orconn_state(&conn, 1, 1, PROXY_NONE);
send_orconn_state(&conn, OR_CONN_STATE_CONNECTING);
send_ocirc_chan(1, 1, true);
assert_bootmsg("5 TAG=conn");
send_orconn_state(&conn, OR_CONN_STATE_TLS_HANDSHAKING);
assert_bootmsg("10 TAG=conn_done");
send_orconn_state(&conn, OR_CONN_STATE_OR_HANDSHAKING_V3);
assert_bootmsg("14 TAG=handshake");
send_orconn_state(&conn, OR_CONN_STATE_OPEN);
assert_bootmsg("15 TAG=handshake_done");
conn.u.state.gid = 2;
conn.u.state.chan = 2;
send_orconn_state(&conn, OR_CONN_STATE_CONNECTING);
/* It doesn't know it's an origin circuit yet */
assert_bootmsg("15 TAG=handshake_done");
send_ocirc_chan(2, 2, false);
assert_bootmsg("80 TAG=ap_conn");
send_orconn_state(&conn, OR_CONN_STATE_TLS_HANDSHAKING);
assert_bootmsg("85 TAG=ap_conn_done");
send_orconn_state(&conn, OR_CONN_STATE_OR_HANDSHAKING_V3);
assert_bootmsg("89 TAG=ap_handshake");
send_orconn_state(&conn, OR_CONN_STATE_OPEN);
assert_bootmsg("90 TAG=ap_handshake_done");
done:
tor_free(saved_event_str);
UNMOCK(queue_control_event_string);
}
static void
test_cntev_orconn_state_pt(void *arg)
{
orconn_event_msg_t conn;
(void)arg;
MOCK(queue_control_event_string, mock_queue_control_event_string);
control_testing_set_global_event_mask(EVENT_MASK_(EVENT_STATUS_CLIENT));
setup_orconn_state(&conn, 1, 1, PROXY_PLUGGABLE);
send_ocirc_chan(1, 1, true);
send_orconn_state(&conn, OR_CONN_STATE_CONNECTING);
assert_bootmsg("1 TAG=conn_pt");
send_orconn_state(&conn, OR_CONN_STATE_PROXY_HANDSHAKING);
assert_bootmsg("2 TAG=conn_done_pt");
send_orconn_state(&conn, OR_CONN_STATE_TLS_HANDSHAKING);
assert_bootmsg("10 TAG=conn_done");
send_orconn_state(&conn, OR_CONN_STATE_OR_HANDSHAKING_V3);
assert_bootmsg("14 TAG=handshake");
send_orconn_state(&conn, OR_CONN_STATE_OPEN);
assert_bootmsg("15 TAG=handshake_done");
send_ocirc_chan(2, 2, false);
conn.u.state.gid = 2;
conn.u.state.chan = 2;
send_orconn_state(&conn, OR_CONN_STATE_CONNECTING);
assert_bootmsg("76 TAG=ap_conn_pt");
send_orconn_state(&conn, OR_CONN_STATE_PROXY_HANDSHAKING);
assert_bootmsg("77 TAG=ap_conn_done_pt");
done:
tor_free(saved_event_str);
UNMOCK(queue_control_event_string);
}
static void
test_cntev_orconn_state_proxy(void *arg)
{
orconn_event_msg_t conn;
(void)arg;
MOCK(queue_control_event_string, mock_queue_control_event_string);
control_testing_set_global_event_mask(EVENT_MASK_(EVENT_STATUS_CLIENT));
setup_orconn_state(&conn, 1, 1, PROXY_CONNECT);
send_ocirc_chan(1, 1, true);
send_orconn_state(&conn, OR_CONN_STATE_CONNECTING);
assert_bootmsg("3 TAG=conn_proxy");
send_orconn_state(&conn, OR_CONN_STATE_PROXY_HANDSHAKING);
assert_bootmsg("4 TAG=conn_done_proxy");
send_orconn_state(&conn, OR_CONN_STATE_TLS_HANDSHAKING);
assert_bootmsg("10 TAG=conn_done");
send_orconn_state(&conn, OR_CONN_STATE_OR_HANDSHAKING_V3);
assert_bootmsg("14 TAG=handshake");
send_orconn_state(&conn, OR_CONN_STATE_OPEN);
assert_bootmsg("15 TAG=handshake_done");
send_ocirc_chan(2, 2, false);
conn.u.state.gid = 2;
conn.u.state.chan = 2;
send_orconn_state(&conn, OR_CONN_STATE_CONNECTING);
assert_bootmsg("78 TAG=ap_conn_proxy");
send_orconn_state(&conn, OR_CONN_STATE_PROXY_HANDSHAKING);
assert_bootmsg("79 TAG=ap_conn_done_proxy");
done:
tor_free(saved_event_str);
UNMOCK(queue_control_event_string);
}
#define TEST(name, flags) \
{ #name, test_cntev_ ## name, flags, 0, NULL }
@ -398,5 +540,8 @@ struct testcase_t controller_event_tests[] = {
TEST(event_mask, TT_FORK),
TEST(dirboot_defer_desc, TT_FORK),
TEST(dirboot_defer_orconn, TT_FORK),
TEST(orconn_state, TT_FORK),
TEST(orconn_state_pt, TT_FORK),
TEST(orconn_state_proxy, TT_FORK),
END_OF_TESTCASES
};