From 0c09252dd63f69544bff9d67c388f4167b947306 Mon Sep 17 00:00:00 2001 From: evgeny Date: Sat, 10 Oct 2026 15:40:34 +0200 Subject: [PATCH] tests: assign Merkle and DM media fixture ports dynamically --- tests/test_dm_media.c | 22 +++++++++++------- tests/test_merkle_sync.c | 48 ++++++++++++++++++++-------------------- 2 files changed, 38 insertions(+), 32 deletions(-) diff --git a/tests/test_dm_media.c b/tests/test_dm_media.c index b71f7e0b..6ebe563f 100644 --- a/tests/test_dm_media.c +++ b/tests/test_dm_media.c @@ -27,6 +27,7 @@ #include "../src/media_delivery/file_transfer.h" #include "test_utils.h" #include "test_clock.h" +#include "test_net.h" #define GROUP 4242424242424243ULL #define CHANNEL "4242424242424243" @@ -38,6 +39,7 @@ struct media_test { struct UTUN_INSTANCE* nodes[NODES]; struct SC_MYKEYS keys[NODES]; uint64_t ids[NODES]; + uint16_t ports[NODES]; /* ОС назначает один endpoint на узел; restart сохраняет этот endpoint. */ uint8_t ed[NODES][32], ch_x[32], ch_ed[32], ch_priv[32], ch_sig[64]; char dir[512], conv[64], file[768]; uint8_t body[2048], id[16], receipt[DM_MEDIA_RECEIPT_SIZE]; @@ -72,7 +74,7 @@ static void hex(const uint8_t* p, size_t len, char* out) { } /* Четыре ядра используют разные БД; клиентские физические линки сходятся на S. */ -static int start_node(struct media_test* t, int index, int base) { +static int start_node(struct media_test* t, int index) { char path[768], pub[65], priv[65], server[65], db[768]; hex(t->keys[index].public_key, 32, pub); hex(t->keys[index].private_key, 32, priv); snprintf(db, sizeof(db), "%s/db%d", t->dir, index); utun_mkdir(db, 0700); @@ -81,13 +83,18 @@ static int start_node(struct media_test* t, int index, int base) { 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\n", priv, pub, db, base + index); + "[chatserver]\nstorage_autoload=0\n", priv, pub, db, t->ports[index]); if (index != S) { hex(t->keys[S].public_key, 32, server); - fprintf(f, "[client:to_s]\npeer_public_key=%s\nlink=s1:127.0.0.1:%d\n", server, base + S); + fprintf(f, "[client:to_s]\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]) { + t->ports[index] = ss_get_port(&test_etcp_bound_socket(t->nodes[index], "s1")->local_addr); + DEBUG_INFO(DEBUG_CATEGORY_DEBUG, "DM media fixture: node=%d port=%u retained for restart", index, t->ports[index]); + } + if (t->nodes[index] && index != S) test_etcp_set_peer(t->nodes[index], "to_s", t->nodes[S], "s1"); return t->nodes[index] ? utun_instance_init(t->nodes[index]) : -1; } @@ -268,8 +275,7 @@ static void tick(void* arg) { utun_instance_destroy(a); t->nodes[A] = NULL; utun_instance_destroy(m); t->nodes[M] = NULL; utun_instance_destroy(s); t->nodes[S] = NULL; - int base = 48000 + getpid() % 5000; - if (start_node(t, S, base) || start_node(t, M, base) || start_node(t, B, base)) { failure(t, "custody restart"); return; } + if (start_node(t, S) || start_node(t, M) || start_node(t, B)) { failure(t, "custody restart"); return; } t->phase++; } break; @@ -289,7 +295,7 @@ static void tick(void* arg) { control(t, M, t->ids[S], DM_MEDIA_STORE, put, 8 + t->body_len); if (value(m, "SELECT count(*) FROM dm_custody") != 0 || value(s, "SELECT count(*) FROM dm_media_jobs WHERE receipt IS NULL") != 0) { failure(t, "late custody resurrected artifact"); return; } - if (start_node(t, A, 48000 + getpid() % 5000)) { failure(t, "source restart"); return; } + if (start_node(t, A)) { failure(t, "source restart"); return; } t->phase++; } break; @@ -330,6 +336,7 @@ int main(void) { if (getenv("UTUN_TEST_DEBUG")) { debug_set_category_level_by_name("dm", "debug"); debug_set_category_level_by_name("media", "debug"); debug_set_category_level_by_name("bgp", "info"); debug_set_category_level_by_name("etcp_route", "info"); + debug_set_category_level_by_name("connection", "debug"); debug_set_category_level_by_name("etcp", "info"); } utun_instance_set_tun_init_enabled(0); snprintf(t.dir, sizeof(t.dir), "/tmp/utun_dm_media_XXXXXX"); @@ -360,8 +367,7 @@ int main(void) { for (unsigned i = 0; i < FILE_BYTES; i++) fputc((int)((i * 73 + 19) & 255), f); fclose(f); t.ua = uasync_create(); - int base = 48000 + getpid() % 5000; - if (!t.ua || start_node(&t, S, base) || start_node(&t, A, base) || start_node(&t, M, base) || start_node(&t, B, base)) return 1; + if (!t.ua || start_node(&t, S) || start_node(&t, A) || start_node(&t, M) || start_node(&t, B)) return 1; t.timer = uasync_set_timeout(t.ua, 100, &t, tick, "dm_media_test"); test_clock_mainloop(t.ua); for (int i = 0; i < NODES; i++) if (t.nodes[i]) utun_instance_destroy(t.nodes[i]); diff --git a/tests/test_merkle_sync.c b/tests/test_merkle_sync.c index c248c94e..b9d4a9ab 100644 --- a/tests/test_merkle_sync.c +++ b/tests/test_merkle_sync.c @@ -18,6 +18,7 @@ #include "etcp_connections.h" #include "test_utils.h" #include "test_clock.h" +#include "test_net.h" #include #include @@ -591,13 +592,10 @@ static void _intg_setup_contexts(void) { done_sync = 0; } -static int _intg_create_cfgs(int* port_a, int* port_b, int* port_c, - char* cfg_a, char* cfg_b, char* cfg_c, size_t sz) { +static int _intg_create_cfgs(char* cfg_a, char* cfg_b, char* cfg_c, size_t sz) { /* reset template (mkdtemp modifies it) */ strcpy(intg_temp_dir, "/tmp/utun_msync_XXXXXX"); if (test_mkdtemp(intg_temp_dir) != 0) { printf(" mkdtemp failed\n"); return -1; } - int base = 43000 + (getpid() % 15000); - *port_a = base; *port_b = base + 1; *port_c = base + 2; snprintf(cfg_a, sz, "%s/a.conf", intg_temp_dir); snprintf(cfg_b, sz, "%s/b.conf", intg_temp_dir); snprintf(cfg_c, sz, "%s/c.conf", intg_temp_dir); @@ -609,25 +607,25 @@ static int _intg_create_cfgs(int* port_a, int* port_b, int* port_c, _write_cfg(cfg_a, "[global]\nmy_node_id=0xAAAAAAAAAAAAAAAA\ntun_ip=10.220.0.1/24\n" "tun_ifname=tun220\nkeepalive_adaptive=0\ndb_path=%s/db_a\n\n" - "[server: srv_a]\naddr=127.0.0.1:%d\ntype=public\n\n" - "[allowed_keys]\nallow_all=1\n", intg_temp_dir, *port_a); + "[server: srv_a]\naddr=127.0.0.1:0\ntype=public\n\n" + "[allowed_keys]\nallow_all=1\n", intg_temp_dir); config_ensure_keys_and_node_id(cfg_a); char* pub_a = _get_pubkey(cfg_a); if (!pub_a) return -1; _write_cfg(cfg_b, "[global]\nmy_node_id=0xBBBBBBBBBBBBBBBB\ntun_ip=10.220.0.2/24\n" "tun_ifname=tun221\nkeepalive_adaptive=0\ndb_path=%s/db_b\n\n" - "[server: srv_b]\naddr=127.0.0.1:%d\ntype=public\n\n" - "[client: to_a]\nkeepalive=1\npeer_public_key=%s\nlink=srv_b:127.0.0.1:%d\n\n" - "[allowed_keys]\nallow_all=1\n", intg_temp_dir, *port_b, pub_a, *port_a); + "[server: srv_b]\naddr=127.0.0.1:0\ntype=public\n\n" + "[client: to_a]\nkeepalive=1\npeer_public_key=%s\nlink=srv_b:127.0.0.1:0\n\n" + "[allowed_keys]\nallow_all=1\n", intg_temp_dir, pub_a); config_ensure_keys_and_node_id(cfg_b); _write_cfg(cfg_c, "[global]\nmy_node_id=0xCCCCCCCCCCCCCCCC\ntun_ip=10.220.0.3/24\n" "tun_ifname=tun222\nkeepalive_adaptive=0\ndb_path=%s/db_c\n\n" - "[server: srv_c]\naddr=127.0.0.1:%d\ntype=public\n\n" - "[client: to_a]\nkeepalive=1\npeer_public_key=%s\nlink=srv_c:127.0.0.1:%d\n\n" - "[allowed_keys]\nallow_all=1\n", intg_temp_dir, *port_c, pub_a, *port_a); + "[server: srv_c]\naddr=127.0.0.1:0\ntype=public\n\n" + "[client: to_a]\nkeepalive=1\npeer_public_key=%s\nlink=srv_c:127.0.0.1:0\n\n" + "[allowed_keys]\nallow_all=1\n", intg_temp_dir, pub_a); config_ensure_keys_and_node_id(cfg_c); u_free(pub_a); @@ -655,13 +653,13 @@ static void _intg_cleanup(void) { static int _intg_init_two(void) { i_phase = 0; i_timeout_id = NULL; i_a = i_b = i_c = NULL; - int port_a, port_b, port_c; char cfg_a[256], cfg_b[256], cfg_c[256]; - if (_intg_create_cfgs(&port_a, &port_b, &port_c, cfg_a, cfg_b, cfg_c, sizeof(cfg_a)) != 0) return -1; + if (_intg_create_cfgs(cfg_a, cfg_b, cfg_c, sizeof(cfg_a)) != 0) return -1; utun_instance_set_tun_init_enabled(0); i_ua = uasync_create(); if (!i_ua) { _intg_cleanup(); return -1; } i_a = utun_instance_create(i_ua, cfg_a); i_b = utun_instance_create(i_ua, cfg_b); + if (i_a && i_b) test_etcp_set_peer(i_b, "to_a", i_a, "srv_a"); if (!i_a || !i_b || utun_instance_init(i_a) != 0 || utun_instance_init(i_b) != 0) { _intg_cleanup(); return -1; } i_timeout_id = uasync_set_timeout(i_ua, INTG_TIMEOUT_TB, NULL, _intg_timeout, "ms_intg_timeout"); @@ -836,14 +834,17 @@ static void test_randomized_two(void) { static int _intg_init_three(void) { i_phase = 0; i_timeout_id = NULL; i_a = i_b = i_c = NULL; - int port_a, port_b, port_c; char cfg_a[256], cfg_b[256], cfg_c[256]; - if (_intg_create_cfgs(&port_a, &port_b, &port_c, cfg_a, cfg_b, cfg_c, sizeof(cfg_a)) != 0) return -1; + if (_intg_create_cfgs(cfg_a, cfg_b, cfg_c, sizeof(cfg_a)) != 0) return -1; utun_instance_set_tun_init_enabled(0); i_ua = uasync_create(); if (!i_ua) { _intg_cleanup(); return -1; } i_a = utun_instance_create(i_ua, cfg_a); i_b = utun_instance_create(i_ua, cfg_b); i_c = utun_instance_create(i_ua, cfg_c); + if (i_a && i_b && i_c) { + test_etcp_set_peer(i_b, "to_a", i_a, "srv_a"); + test_etcp_set_peer(i_c, "to_a", i_a, "srv_a"); + } if (!i_a || !i_b || !i_c || utun_instance_init(i_a) != 0 || utun_instance_init(i_b) != 0 || utun_instance_init(i_c) != 0) { _intg_cleanup(); return -1; } i_timeout_id = uasync_set_timeout(i_ua, INTG_TIMEOUT_TB*5, NULL, _intg_timeout, "ms_intg_timeout"); @@ -1089,9 +1090,7 @@ static int _str_init(void) { if (test_mkdtemp(tmpld) != 0) { printf(" mkdtemp failed\n"); u_free(s); gs = NULL; return -1; } /* --- create configs --- */ - int base_port = 44000 + (getpid() % 10000); - char cfgs[STRESS_N][256]; int ports[STRESS_N]; - for (int i = 0; i < STRESS_N; i++) ports[i] = base_port + i; + char cfgs[STRESS_N][256]; utun_instance_set_tun_init_enabled(0); s->ua = uasync_create(); @@ -1103,8 +1102,8 @@ static int _str_init(void) { char dbp[256]; snprintf(dbp, sizeof(dbp), "%s/db_0", tmpld); utun_mkdir(dbp, 0755); _write_cfg(cfgs[0], "[global]\nmy_node_id=0xAAAAAAAAAAAAAAAA\ntun_ip=10.240.0.1/24\ntun_ifname=tun240\nkeepalive_adaptive=0\ndb_path=%s\n\n" - "[server:s0]\naddr=127.0.0.1:%d\ntype=public\n\n" - "[allowed_keys]\nallow_all=1\n", dbp, ports[0]); + "[server:s0]\naddr=127.0.0.1:0\ntype=public\n\n" + "[allowed_keys]\nallow_all=1\n", dbp); config_ensure_keys_and_node_id(cfgs[0]); } @@ -1116,10 +1115,10 @@ static int _str_init(void) { char nid[32]; snprintf(nid, sizeof(nid), "BBBBBBBBBBBB%02d00", i); _write_cfg(cfgs[i], "[global]\nmy_node_id=0x%s\ntun_ip=10.240.0.%d/24\ntun_ifname=tun240\nkeepalive_adaptive=0\ndb_path=%s\n\n" - "[server:s%d]\naddr=127.0.0.1:%d\ntype=public\n\n" - "[client:to_hub]\nkeepalive=1\npeer_public_key=%s\nlink=s%d:127.0.0.1:%d\n\n" + "[server:s%d]\naddr=127.0.0.1:0\ntype=public\n\n" + "[client:to_hub]\nkeepalive=1\npeer_public_key=%s\nlink=s%d:127.0.0.1:0\n\n" "[allowed_keys]\nallow_all=1\n", - nid, i + 1, dbp, i, ports[i], hub_pub, i, ports[0]); + nid, i + 1, dbp, i, hub_pub, i); config_ensure_keys_and_node_id(cfgs[i]); } u_free(hub_pub); @@ -1127,6 +1126,7 @@ static int _str_init(void) { /* --- create instances --- */ for (int i = 0; i < STRESS_N; i++) { s->inst[i] = utun_instance_create(s->ua, cfgs[i]); + if (s->inst[i] && i) test_etcp_set_peer(s->inst[i], "to_hub", s->inst[STRESS_HUB], "s0"); if (!s->inst[i] || utun_instance_init(s->inst[i]) != 0) { printf(" FAIL: instance %d init\n", i); goto fail;