From 45a6320b5ea679315aa49edd35c4e47b0f0c42d2 Mon Sep 17 00:00:00 2001 From: evgeny Date: Sat, 10 Oct 2026 16:31:54 +0200 Subject: [PATCH] tests: assign JOIN, member files and DM endpoints dynamically --- tests/test_chat_join.c | 19 +++++++++---------- tests/test_dm_e2e.c | 16 ++++++++++++---- tests/test_member_files_network.c | 21 +++++++++++++-------- 3 files changed, 34 insertions(+), 22 deletions(-) diff --git a/tests/test_chat_join.c b/tests/test_chat_join.c index 94d5c017..641b1f54 100644 --- a/tests/test_chat_join.c +++ b/tests/test_chat_join.c @@ -28,6 +28,7 @@ #include "../lib/mem.h" #include "test_utils.h" #include "test_clock.h" +#include "test_net.h" #include #define OPENSSL_API_COMPAT 0x10100000L @@ -56,7 +57,6 @@ static uint64_t g_nid[MAX_NODES]; static char g_tdir[256] = "/tmp/utun_cj_XXXXXX"; static char g_cfg[MAX_NODES][256]; static char g_db_dir[MAX_NODES][320]; -static int g_port[MAX_NODES]; static uint64_t g_group_id = 0; /* ── крипто-хелперы ── */ @@ -286,10 +286,9 @@ static void write_comp_config(void) { snprintf(g_cfg[0], sizeof(g_cfg[0]), "%s/comp.conf", g_tdir); snprintf(g_db_dir[0], sizeof(g_db_dir[0]), "%s/dbcomp", g_tdir); utun_mkdir(g_db_dir[0], 0755); - g_port[0] = 54000 + (getpid() % 4000); wf(g_cfg[0], "[global]\ntun_ip=10.98.70.1/24\ntun_ifname=tun70\ndb_path=%s\n" - "[server: s1]\naddr=127.0.0.1:%d\ntype=public\n[allowed_keys]\nallow_all=1\n", - g_db_dir[0], g_port[0]); + "[server: s1]\naddr=127.0.0.1:0\ntype=public\n[allowed_keys]\nallow_all=1\n", + g_db_dir[0]); config_ensure_keys_and_node_id(g_cfg[0]); struct utun_config* c = parse_config(g_cfg[0]); g_nid[0] = c->global.my_node_id; free_config(c); } @@ -300,12 +299,11 @@ static void write_int_configs(void) { snprintf(g_cfg[i], sizeof(g_cfg[i]), "%s/%s.conf", g_tdir, i ? "c" : "a"); snprintf(g_db_dir[i], sizeof(g_db_dir[i]), "%s/db%s", g_tdir, i ? "c" : "a"); utun_mkdir(g_db_dir[i], 0755); - g_port[i] = 55000 + (getpid() % 3000) + i * 1000; } for (int i = 0; i < 2; i++) { wf(g_cfg[i], "[global]\ntun_ip=10.98.%d.1/24\ntun_ifname=tun%d0\ndb_path=%s\n" - "[server: s1]\naddr=127.0.0.1:%d\ntype=public\n[allowed_keys]\nallow_all=1\n", - i, i, g_db_dir[i], g_port[i]); + "[server: s1]\naddr=127.0.0.1:0\ntype=public\n[allowed_keys]\nallow_all=1\n", + i, i, g_db_dir[i]); config_ensure_keys_and_node_id(g_cfg[i]); struct utun_config* c = parse_config(g_cfg[i]); g_nid[i] = c->global.my_node_id; free_config(c); } @@ -314,9 +312,9 @@ static void write_int_configs(void) { char* pr = gv(g_cfg[1], "priv"); char* pu = gv(g_cfg[1], "pub"); char* p0_pu = gv(g_cfg[0], "pub"); wf(g_cfg[1], "[global]\nmy_private_key=%s\nmy_public_key=%s\ntun_ip=10.98.1.1/24\ntun_ifname=tun10\n" - "db_path=%s\n[server: s1]\naddr=127.0.0.1:%d\ntype=public\n" - "[client: to_a]\nkeepalive=1\npeer_public_key=%s\nlink=s1:127.0.0.1:%d\n[allowed_keys]\nallow_all=1\n", - pr, pu, g_db_dir[1], g_port[1], p0_pu, g_port[0]); + "db_path=%s\n[server: s1]\naddr=127.0.0.1:0\ntype=public\n" + "[client: to_a]\nkeepalive=1\npeer_public_key=%s\nlink=s1:127.0.0.1:0\n[allowed_keys]\nallow_all=1\n", + pr, pu, g_db_dir[1], p0_pu); u_free(pr); u_free(pu); u_free(p0_pu); } } @@ -325,6 +323,7 @@ static int create_instances(int n) { for (int i = 0; i < n; i++) { g_inst[i] = utun_instance_create(g_ua, g_cfg[i]); if (!g_inst[i]) { printf("FAIL: create n%d\n", i); return -1; } + if (i == 1) test_etcp_set_peer(g_inst[i], "to_a", g_inst[0], "s1"); utun_instance_init(g_inst[i]); member_sync_init(g_inst[i]); } diff --git a/tests/test_dm_e2e.c b/tests/test_dm_e2e.c index 48bb6e37..48fa116c 100644 --- a/tests/test_dm_e2e.c +++ b/tests/test_dm_e2e.c @@ -30,6 +30,7 @@ #include "../lib/platform_compat.h" #include "test_utils.h" #include "test_clock.h" +#include "test_net.h" #include #define OPENSSL_API_COMPAT 0x10100000L @@ -124,14 +125,13 @@ static int wf(const char* p, const char* f, ...) { /* ── генерация данных сценария + конфигов ── */ -static void fill_shared(int base_port) { +static void fill_shared(void) { memset(&g_sh, 0, sizeof(g_sh)); for (int i = 0; i < 3; i++) { gen_x25519(g_sh.x_pub[i], g_sh.x_priv[i]); sc_derive_ed25519_pubkey(g_sh.x_priv[i], g_sh.ed_pub[i]); g_sh.nid[i] = sc_derive_node_id_from_pubkey(g_sh.x_pub[i]); - g_sh.port[i] = base_port + i * 1000; } { uint8_t ch_x_priv[32]; gen_x25519(g_sh.ch_x_pub, ch_x_priv); } @@ -673,7 +673,7 @@ int main(int argc, char** argv) { if (test_mkdtemp(dir) != 0) { fprintf(stderr, "mkdtemp failed\n"); return 1; } test_setenv("UTUN_TEST_DIR", dir, 1); - fill_shared(58000 + (getpid() % 2000)); + fill_shared(); write_configs(dir); printf("=== test_dm_e2e ===\n"); fflush(stdout); @@ -702,6 +702,10 @@ int main(int argc, char** argv) { uasync_destroy(ua, 0); return 1; } + for (int i = 0; i < 3; i++) g_sh.port[i] = ss_get_port(&test_etcp_bound_socket(t.inst[i], "s1")->local_addr); + write_configs(dir); // Сохраняем назначенные endpoint для всех restart в этом сценарии. + test_etcp_set_peer(t.inst[IDX_C], "to_a", t.inst[IDX_A], "s1"); + test_etcp_set_peer(t.inst[IDX_B], "to_c", t.inst[IDX_C], "s1"); utun_instance_init(t.inst[IDX_A]); utun_instance_init(t.inst[IDX_C]); /* B стартует после C, чтобы B→C пришлось на уже поднятый C (как в сценарии) */ @@ -726,7 +730,7 @@ int main(int argc, char** argv) { char dir2[512]; snprintf(dir2, sizeof(dir2), "/tmp/utun_dm_e2e2_XXXXXX"); if (test_mkdtemp(dir2) != 0) { fprintf(stderr, "mkdtemp dir2 failed\n"); return 1; } test_setenv("UTUN_TEST_DIR", dir2, 1); - fill_shared(61000 + (getpid() % 1000)); + fill_shared(); write_configs(dir2); TEST("first offline DM: supernode restart, sender remains offline"); { @@ -753,6 +757,10 @@ int main(int argc, char** argv) { uasync_destroy(ua, 0); return 1; } + for (int i = 0; i < 3; i++) g_sh.port[i] = ss_get_port(&test_etcp_bound_socket(t.inst[i], "s1")->local_addr); + write_configs(dir2); + test_etcp_set_peer(t.inst[IDX_C], "to_a", t.inst[IDX_A], "s1"); + test_etcp_set_peer(t.inst[IDX_B], "to_c", t.inst[IDX_C], "s1"); utun_instance_init(t.inst[IDX_A]); utun_instance_init(t.inst[IDX_C]); utun_instance_init(t.inst[IDX_B]); diff --git a/tests/test_member_files_network.c b/tests/test_member_files_network.c index dfdf3e94..87bf1ddb 100644 --- a/tests/test_member_files_network.c +++ b/tests/test_member_files_network.c @@ -14,6 +14,7 @@ #include "../lib/mem.h" #include "test_utils.h" #include "test_clock.h" +#include "test_net.h" #include #include #include @@ -146,20 +147,20 @@ static int put_member(struct network_test* t, int destination, int member, int d join, ts, NULL, 0, name, tags, tags_sig, member == SERVER, 0, tree) < 0 ? -1 : 0; } -static int start(struct network_test* t, int index, int port) { +static int create(struct network_test* t, int index) { char pub[65], priv[65], server[65], path[768], db[768]; hex(t->keys[index].public_key, pub); hex(t->keys[index].private_key, priv); hex(t->keys[SERVER].public_key, server); snprintf(path, sizeof(path), "%s/node%d.conf", t->dir, index); snprintf(db, sizeof(db), "%s/db%d", t->dir, index); FILE* f = fopen(path, "w"); if (!f) return -1; fprintf(f, "[global]\nmy_private_key=%s\nmy_public_key=%s\ndb_path=%s\n" - "[server:s1]\naddr=127.0.0.1:%d\ntype=public\n[allowed_keys]\nallow_all=1\n" - "[chatserver]\nstorage_autoload=0\nmember_files_user_quota_mb=8\n", priv, pub, db, port + index); - if (index != SERVER) fprintf(f, "[client:to_m]\npeer_public_key=%s\nlink=s1:127.0.0.1:%d\n", server, port + SERVER); + "[server:s1]\naddr=127.0.0.1:0\ntype=public\n[allowed_keys]\nallow_all=1\n" + "[chatserver]\nstorage_autoload=0\nmember_files_user_quota_mb=8\n", priv, pub, db); + if (index != SERVER) fprintf(f, "[client:to_m]\npeer_public_key=%s\nlink=s1:127.0.0.1:0\n", server); fclose(f); t->nodes[index] = utun_instance_create(t->ua, path); - if (!t->nodes[index] || utun_instance_init(t->nodes[index])) return -1; + if (!t->nodes[index]) return -1; memcpy(t->ed[index], t->nodes[index]->my_ed25519_pubkey, 32); t->ids[index] = t->nodes[index]->node_id; - chat_event_set_handler(t->nodes[index], event); return 0; + return 0; } static int setup(struct network_test* t) { @@ -333,8 +334,12 @@ int main(void) { if (test_mkdtemp(t.dir)) return 1; t.ua = uasync_create(); for (unsigned i = 0; i < NODE_COUNT; i++) sc_generate_keypair(&t.keys[i]); - int port = 39000 + (int)(get_time_tb() % 8000); - for (unsigned i = 0; i < NODE_COUNT; i++) if (start(&t, i, port)) { t.failed = 1; goto stop; } + for (unsigned i = 0; i < NODE_COUNT; i++) if (create(&t, i)) { t.failed = 1; goto stop; } + for (unsigned i = 0; i < NODE_COUNT; i++) { + if (i != SERVER) test_etcp_set_peer(t.nodes[i], "to_m", t.nodes[SERVER], "s1"); + if (utun_instance_init(t.nodes[i])) { t.failed = 1; goto stop; } + chat_event_set_handler(t.nodes[i], event); + } if (setup(&t)) { t.failed = 1; goto stop; } t.timer = uasync_set_timeout(t.ua, 100, &t, tick, "member_files_test"); test_clock_mainloop(t.ua); if (!t.failed) {