test: Improve test hermeticity by using local TCP relay.

This will attempt to bind to a few different TCP ports and connect toxes
to it if UDP is disabled for the test.
This commit is contained in:
iphydf 2022-04-10 20:36:55 +00:00
parent bff87ea8e4
commit 2824aa2544
No known key found for this signature in database
GPG Key ID: 3855DBA2D74403C9
2 changed files with 57 additions and 20 deletions

View File

@ -13,10 +13,12 @@
#define ABORT_ON_LOG_ERROR true
#endif
Run_Auto_Options default_run_auto_options() {
Run_Auto_Options default_run_auto_options()
{
return (Run_Auto_Options) {
.graph = GRAPH_COMPLETE,
.init_autotox = nullptr,
.tcp_port = 33188,
};
}
@ -210,11 +212,43 @@ void reload(AutoTox *autotox)
}
static void initialise_autotox(struct Tox_Options *options, AutoTox *autotox, uint32_t index, uint32_t state_size,
const Run_Auto_Options *autotest_opts)
Run_Auto_Options *autotest_opts)
{
autotox->index = index;
Tox_Err_New err;
Tox_Err_New err = TOX_ERR_NEW_OK;
if (index == 0) {
struct Tox_Options *default_opts = tox_options_new(nullptr);
ck_assert(default_opts != nullptr);
if (options == nullptr) {
options = default_opts;
}
// Try a few ports for the TCP relay.
for (uint16_t tcp_port = autotest_opts->tcp_port; tcp_port < autotest_opts->tcp_port + 200; ++tcp_port) {
tox_options_set_tcp_port(options, tcp_port);
autotox->tox = tox_new_log(options, &err, &autotox->index);
if (autotox->tox != nullptr) {
autotest_opts->tcp_port = tcp_port;
break;
}
ck_assert_msg(err == TOX_ERR_NEW_PORT_ALLOC, "unexpected tox_new error (expected PORT_ALLOC): %d", err);
}
tox_options_free(default_opts);
} else {
// No TCP relay enabled for all the other toxes.
if (options != nullptr) {
tox_options_set_tcp_port(options, 0);
}
autotox->tox = tox_new_log(options, &err, &autotox->index);
}
ck_assert_msg(autotox->tox != nullptr, "failed to create tox instance #%u (error = %d)", index, err);
set_mono_time_callback(autotox);
@ -276,7 +310,6 @@ static void bootstrap_autotoxes(struct Tox_Options *options, uint32_t tox_count,
{
const bool udp_enabled = options != nullptr ? tox_options_get_udp_enabled(options) : true;
if (udp_enabled) {
printf("bootstrapping all toxes off tox 0\n");
uint8_t dht_key[TOX_PUBLIC_KEY_SIZE];
tox_self_get_dht_id(autotoxes[0].tox, dht_key);
@ -287,17 +320,20 @@ static void bootstrap_autotoxes(struct Tox_Options *options, uint32_t tox_count,
tox_bootstrap(autotoxes[i].tox, "localhost", dht_port, dht_key, &err);
ck_assert(err == TOX_ERR_BOOTSTRAP_OK);
}
} else {
printf("bootstrapping all toxes to tcp relays\n");
if (!udp_enabled) {
printf("bootstrapping all toxes to local TCP relay running on port %d\n", autotest_opts->tcp_port);
for (uint32_t i = 0; i < tox_count; ++i) {
bootstrap_tox_live_network(autotoxes[i].tox, true);
Tox_Err_Bootstrap err;
tox_add_tcp_relay(autotoxes[i].tox, "localhost", autotest_opts->tcp_port, dht_key, &err);
ck_assert(err == TOX_ERR_BOOTSTRAP_OK);
}
}
}
void run_auto_test(struct Tox_Options *options, uint32_t tox_count, void test(AutoTox *autotoxes),
uint32_t state_size, const Run_Auto_Options *autotest_opts)
uint32_t state_size, Run_Auto_Options *autotest_opts)
{
printf("initialising %u toxes\n", tox_count);
@ -365,7 +401,7 @@ void print_debug_log(Tox *m, Tox_Log_Level level, const char *file, uint32_t lin
return;
}
uint32_t index = user_data ? *(uint32_t *)user_data : 0;
const uint32_t index = user_data ? *(uint32_t *)user_data : 0;
fprintf(stderr, "[#%u] %s %s:%u\t%s:\t%s\n", index, tox_log_level_name(level), file, line, func, message);
if (level == TOX_LOG_LEVEL_ERROR && ABORT_ON_LOG_ERROR) {

View File

@ -41,12 +41,13 @@ typedef enum Graph_Type {
typedef struct Run_Auto_Options {
Graph_Type graph;
void (*init_autotox)(AutoTox *autotox, uint32_t n);
uint16_t tcp_port;
} Run_Auto_Options;
Run_Auto_Options default_run_auto_options(void);
void run_auto_test(struct Tox_Options *options, uint32_t tox_count, void test(AutoTox *autotoxes),
uint32_t state_size, const Run_Auto_Options *autotest_opts);
uint32_t state_size, Run_Auto_Options *autotest_opts);
void bootstrap_tox_live_network(Tox *tox, bool enable_tcp);