mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
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:
parent
bff87ea8e4
commit
2824aa2544
|
@ -13,10 +13,12 @@
|
||||||
#define ABORT_ON_LOG_ERROR true
|
#define ABORT_ON_LOG_ERROR true
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
Run_Auto_Options default_run_auto_options() {
|
Run_Auto_Options default_run_auto_options()
|
||||||
|
{
|
||||||
return (Run_Auto_Options) {
|
return (Run_Auto_Options) {
|
||||||
.graph = GRAPH_COMPLETE,
|
.graph = GRAPH_COMPLETE,
|
||||||
.init_autotox = nullptr,
|
.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,
|
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;
|
autotox->index = index;
|
||||||
Tox_Err_New err;
|
|
||||||
autotox->tox = tox_new_log(options, &err, &autotox->index);
|
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);
|
ck_assert_msg(autotox->tox != nullptr, "failed to create tox instance #%u (error = %d)", index, err);
|
||||||
|
|
||||||
set_mono_time_callback(autotox);
|
set_mono_time_callback(autotox);
|
||||||
|
@ -276,28 +310,30 @@ 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;
|
const bool udp_enabled = options != nullptr ? tox_options_get_udp_enabled(options) : true;
|
||||||
|
|
||||||
if (udp_enabled) {
|
printf("bootstrapping all toxes off tox 0\n");
|
||||||
printf("bootstrapping all toxes off tox 0\n");
|
uint8_t dht_key[TOX_PUBLIC_KEY_SIZE];
|
||||||
uint8_t dht_key[TOX_PUBLIC_KEY_SIZE];
|
tox_self_get_dht_id(autotoxes[0].tox, dht_key);
|
||||||
tox_self_get_dht_id(autotoxes[0].tox, dht_key);
|
const uint16_t dht_port = tox_self_get_udp_port(autotoxes[0].tox, nullptr);
|
||||||
const uint16_t dht_port = tox_self_get_udp_port(autotoxes[0].tox, nullptr);
|
|
||||||
|
|
||||||
for (uint32_t i = 1; i < tox_count; ++i) {
|
for (uint32_t i = 1; i < tox_count; ++i) {
|
||||||
Tox_Err_Bootstrap err;
|
Tox_Err_Bootstrap err;
|
||||||
tox_bootstrap(autotoxes[i].tox, "localhost", dht_port, dht_key, &err);
|
tox_bootstrap(autotoxes[i].tox, "localhost", dht_port, dht_key, &err);
|
||||||
ck_assert(err == TOX_ERR_BOOTSTRAP_OK);
|
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) {
|
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),
|
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);
|
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;
|
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);
|
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) {
|
if (level == TOX_LOG_LEVEL_ERROR && ABORT_ON_LOG_ERROR) {
|
||||||
|
|
|
@ -41,12 +41,13 @@ typedef enum Graph_Type {
|
||||||
typedef struct Run_Auto_Options {
|
typedef struct Run_Auto_Options {
|
||||||
Graph_Type graph;
|
Graph_Type graph;
|
||||||
void (*init_autotox)(AutoTox *autotox, uint32_t n);
|
void (*init_autotox)(AutoTox *autotox, uint32_t n);
|
||||||
|
uint16_t tcp_port;
|
||||||
} Run_Auto_Options;
|
} Run_Auto_Options;
|
||||||
|
|
||||||
Run_Auto_Options default_run_auto_options(void);
|
Run_Auto_Options default_run_auto_options(void);
|
||||||
|
|
||||||
void run_auto_test(struct Tox_Options *options, uint32_t tox_count, void test(AutoTox *autotoxes),
|
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);
|
void bootstrap_tox_live_network(Tox *tox, bool enable_tcp);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user