From 336b6871f3ac804d305c207703f8e87b7d7fe7c9 Mon Sep 17 00:00:00 2001 From: evgeny Date: Sat, 10 Oct 2026 16:14:53 +0200 Subject: [PATCH] tests: allocate broadcast, avatar and invite listeners dynamically --- tests/test_broadcast.c | 41 +++++++++++++++++++-------------------- tests/test_dm_invite.c | 3 +-- tests/test_group_avatar.c | 12 ++++++++---- 3 files changed, 29 insertions(+), 27 deletions(-) diff --git a/tests/test_broadcast.c b/tests/test_broadcast.c index e6249b6b..d3289bf0 100644 --- a/tests/test_broadcast.c +++ b/tests/test_broadcast.c @@ -3,6 +3,7 @@ #include #include "../lib/platform_compat.h" #include "test_utils.h" +#include "test_net.h" #ifdef _WIN32 #include #else @@ -53,8 +54,8 @@ static void test_timeout_cb(void* arg) { struct ncfg { uint64_t node_id; const char* tun_ip; const char* priv_hex; const char* pub_hex; - int srv_cnt; struct { const char* name; int port; } srvs[3]; - int cli_cnt; struct { const char* name; const char* peer_pubkey_hex; int lnk_cnt; struct { const char* local_srv; int remote_port; } lnks[2]; } clis[2]; + int srv_cnt; struct { const char* name; } srvs[3]; + int cli_cnt; struct { const char* name; const char* peer_pubkey_hex; int lnk_cnt; struct { const char* local_srv; } lnks[2]; } clis[2]; }; static void build_cfg(char* buf, size_t size, const struct ncfg* c) { @@ -70,11 +71,11 @@ static void build_cfg(char* buf, size_t size, const struct ncfg* c) { "\n", c->priv_hex, c->pub_hex, c->tun_ip); for (int i = 0; i < c->srv_cnt; i++) - off += snprintf(buf + off, size - off, "[server: %s]\naddr=127.0.0.1:%d\ntype=public\n\n", c->srvs[i].name, c->srvs[i].port); + off += snprintf(buf + off, size - off, "[server: %s]\naddr=127.0.0.1:0\ntype=public\n\n", c->srvs[i].name); for (int i = 0; i < c->cli_cnt; i++) { off += snprintf(buf + off, size - off, "[client: %s]\nkeepalive=1\npeer_public_key=%s\n", c->clis[i].name, c->clis[i].peer_pubkey_hex); for (int j = 0; j < c->clis[i].lnk_cnt; j++) - off += snprintf(buf + off, size - off, "link=%s:127.0.0.1:%d\n", c->clis[i].lnks[j].local_srv, c->clis[i].lnks[j].remote_port); + off += snprintf(buf + off, size - off, "link=%s:127.0.0.1:0\n", c->clis[i].lnks[j].local_srv); off += snprintf(buf + off, size - off, "\n"); } off += snprintf(buf + off, size - off, "[allowed_keys]\nallow_all=1\n"); @@ -163,48 +164,41 @@ int main(void) { bytes_to_hex(keys[i].private_key, SC_PRIVKEY_SIZE, priv_hex[i], sizeof(priv_hex[i])); } - int base = 43000 + (getpid() % 10000); - int p_ab_a = base++, p_ab_b = base++; - int p_ac_a = base++, p_ac_c = base++; - int p_bc_b = base++, p_bc_c = base++; - int p_cd_c = base++, p_cd_d = base++; - int p_de_d = base++, p_de_e = base++; - /* A: servers for B and C, clients to B and C */ char cfg_a[2048]; build_cfg(cfg_a, sizeof(cfg_a), &(struct ncfg){ .priv_hex = priv_hex[0], .pub_hex = pub_hex[0], .tun_ip = "10.200.0.1/24", - .srv_cnt = 2, .srvs = {{"a_srv_b", p_ab_a}, {"a_srv_c", p_ac_a}}, + .srv_cnt = 2, .srvs = {{"a_srv_b"}, {"a_srv_c"}}, .cli_cnt = 2, .clis = { - {"to_b", pub_hex[1], 1, {{"a_srv_b", p_ab_b}}}, - {"to_c", pub_hex[2], 1, {{"a_srv_c", p_ac_c}}}, + {"to_b", pub_hex[1], 1, {{"a_srv_b"}}}, + {"to_c", pub_hex[2], 1, {{"a_srv_c"}}}, } }); /* B: server for A, server for C, client to C */ char cfg_b[2048]; build_cfg(cfg_b, sizeof(cfg_b), &(struct ncfg){ .priv_hex = priv_hex[1], .pub_hex = pub_hex[1], .tun_ip = "10.200.0.2/24", - .srv_cnt = 2, .srvs = {{"b_srv", p_ab_b}, {"b_srv_c", p_bc_b}}, - .cli_cnt = 1, .clis = {{"to_c", pub_hex[2], 1, {{"b_srv_c", p_bc_c}}}} + .srv_cnt = 2, .srvs = {{"b_srv"}, {"b_srv_c"}}, + .cli_cnt = 1, .clis = {{"to_c", pub_hex[2], 1, {{"b_srv_c"}}}} }); /* C: servers for A, B, D; client to D */ char cfg_c[2048]; build_cfg(cfg_c, sizeof(cfg_c), &(struct ncfg){ .priv_hex = priv_hex[2], .pub_hex = pub_hex[2], .tun_ip = "10.200.0.3/24", - .srv_cnt = 3, .srvs = {{"c_srv_a", p_ac_c}, {"c_srv_b", p_bc_c}, {"c_srv_d", p_cd_c}}, - .cli_cnt = 1, .clis = {{"to_d", pub_hex[3], 1, {{"c_srv_d", p_cd_d}}}} + .srv_cnt = 3, .srvs = {{"c_srv_a"}, {"c_srv_b"}, {"c_srv_d"}}, + .cli_cnt = 1, .clis = {{"to_d", pub_hex[3], 1, {{"c_srv_d"}}}} }); /* D: server for C, server for E; client to E */ char cfg_d[2048]; build_cfg(cfg_d, sizeof(cfg_d), &(struct ncfg){ .priv_hex = priv_hex[3], .pub_hex = pub_hex[3], .tun_ip = "10.200.0.4/24", - .srv_cnt = 2, .srvs = {{"d_srv", p_cd_d}, {"d_srv_e", p_de_d}}, - .cli_cnt = 1, .clis = {{"to_e", pub_hex[4], 1, {{"d_srv_e", p_de_e}}}} + .srv_cnt = 2, .srvs = {{"d_srv"}, {"d_srv_e"}}, + .cli_cnt = 1, .clis = {{"to_e", pub_hex[4], 1, {{"d_srv_e"}}}} }); /* E: server for D, no clients */ char cfg_e[2048]; build_cfg(cfg_e, sizeof(cfg_e), &(struct ncfg){ .priv_hex = priv_hex[4], .pub_hex = pub_hex[4], .tun_ip = "10.200.0.5/24", - .srv_cnt = 1, .srvs = {{"e_srv", p_de_e}}, + .srv_cnt = 1, .srvs = {{"e_srv"}}, .cli_cnt = 0, .clis = {} }); @@ -215,6 +209,11 @@ int main(void) { inst[2] = utun_instance_create_from_str(ua, cfg_c); ASSERT(inst[2], "inst C"); inst[3] = utun_instance_create_from_str(ua, cfg_d); ASSERT(inst[3], "inst D"); inst[4] = utun_instance_create_from_str(ua, cfg_e); ASSERT(inst[4], "inst E"); + test_etcp_set_peer(inst[0], "to_b", inst[1], "b_srv"); + test_etcp_set_peer(inst[0], "to_c", inst[2], "c_srv_a"); + test_etcp_set_peer(inst[1], "to_c", inst[2], "c_srv_b"); + test_etcp_set_peer(inst[2], "to_d", inst[3], "d_srv"); + test_etcp_set_peer(inst[3], "to_e", inst[4], "e_srv"); for (int i = 0; i < 5; i++) { ASSERT(utun_instance_init(inst[i]) == 0, "init"); nid[i] = inst[i]->node_id; } test_timeout_id = uasync_set_timeout(ua, TEST_TIMEOUT_TB, NULL, test_timeout_cb, "test_timeout"); diff --git a/tests/test_dm_invite.c b/tests/test_dm_invite.c index b558fe98..c3e5ea92 100644 --- a/tests/test_dm_invite.c +++ b/tests/test_dm_invite.c @@ -160,8 +160,7 @@ int main(void) { int failed = 1; #define CHECK(condition, reason) do { if (!(condition)) { DEBUG_ERROR(DEBUG_CATEGORY_DM, "invite test: %s", reason); goto done; } } while (0) CHECK(ua && !test_mkdtemp(directory), "temporary directory/event loop"); - int port = 32000 + getpid() % 10000; - CHECK(!start_node(ua, directory, 0, port) && !start_node(ua, directory, 1, port + 1), "start nodes"); + CHECK(!start_node(ua, directory, 0, 0) && !start_node(ua, directory, 1, 0), "start nodes"); chat_core_create_channel_auto(nodes[0], "source"); uint64_t source = 0, target = 0; CHECK(!scalar(nodes[0], "SELECT channel_id FROM channels WHERE name='source'", &source), "create source group"); diff --git a/tests/test_group_avatar.c b/tests/test_group_avatar.c index 03c23d5b..532615cb 100644 --- a/tests/test_group_avatar.c +++ b/tests/test_group_avatar.c @@ -2,6 +2,7 @@ #include "../src/chat/group_avatar.c" #include "../src/chat/member_sync.h" #include "test_utils.h" +#include "test_net.h" #include #define GROUP 6767676767ULL @@ -78,15 +79,18 @@ int main(void) { uint8_t seed[64]; SHA512(channel.private_key, 32, seed); memcpy(group_private, seed, 32); assert(sc_derive_ed25519_pubkey(channel.private_key, group_public) == 0); for (unsigned i = 0; i < 2; i++) assert(sc_generate_keypair(&keys[i]) == SC_OK); - int port = 19000 + (int)(getpid() % 1000); char directory[512] = "/tmp/utun-group-avatar-XXXXXX"; assert(!test_mkdtemp(directory)); for (unsigned i = 0; i < 2; i++) { char pub[65], priv[65], peer[65], config[2048]; hex(keys[i].public_key, pub); hex(keys[i].private_key, priv); hex(keys[1-i].public_key, peer); snprintf(config, sizeof(config), "[global]\nmy_private_key=%s\nmy_public_key=%s\ndb_path=%s/node%u\n" - "[server:s1]\naddr=127.0.0.1:%d\ntype=public\n[allowed_keys]\nallow_all=1\n" + "[server:s1]\naddr=127.0.0.1:0\ntype=public\n[allowed_keys]\nallow_all=1\n" "[chatserver]\ngroup_autoconnect=0\nstorage_autoload=0\nmember_files_enabled=0\n" - "[client:peer]\npeer_public_key=%s\nlink=s1:127.0.0.1:%d\n", priv, pub, directory, i, port + i, peer, port + 1-i); - nodes[i] = utun_instance_create_from_str(ua, config); assert(nodes[i] && !utun_instance_init(nodes[i])); + "[client:peer]\npeer_public_key=%s\nlink=s1:127.0.0.1:0\n", priv, pub, directory, i, peer); + nodes[i] = utun_instance_create_from_str(ua, config); assert(nodes[i]); + } + for (unsigned i = 0; i < 2; i++) { + test_etcp_set_peer(nodes[i], "peer", nodes[1-i], "s1"); + assert(!utun_instance_init(nodes[i])); } uint8_t signature[64] = {0}; struct TOPO_PEER_REQUEST* connections[2] = {0};