Browse Source

tests: retain automatically assigned endpoints across restart

master
evgeny 7 days ago
parent
commit
482d2787fb
  1. 30
      tests/test_etcp_reconnect.c
  2. 37
      tests/test_etcp_router_reconnect.c

30
tests/test_etcp_reconnect.c

@ -3,6 +3,7 @@
#include <string.h>
#include "../lib/platform_compat.h"
#include "test_utils.h"
#include "test_net.h"
#ifdef _WIN32
#include <windows.h>
#include <direct.h>
@ -53,18 +54,7 @@ static int _dump_interval = 50;
static int _dump_timer = 0;
static uint8_t packet_buffer[PACKET_SIZE];
static int create_temp_configs(void) {
if (test_mkdtemp(temp_dir) != 0) {
fprintf(stderr, "Failed to create temp directory\n");
return -1;
}
int base_port = 40000 + (getpid() % 20000);
server_port = base_port;
client_port = base_port + 1;
snprintf(server_config_path, sizeof(server_config_path), "%s/server.conf", temp_dir);
snprintf(client_config_path, sizeof(client_config_path), "%s/client.conf", temp_dir);
static int write_configs(void) {
FILE* f = fopen(server_config_path, "w");
if (!f) { fprintf(stderr, "Failed to create server config file\n"); return -1; }
fprintf(f,
@ -113,6 +103,13 @@ static void cleanup_temp_configs(void) {
if (temp_dir[0]) test_rmdir(temp_dir);
}
static int create_temp_configs(void) {
if (test_mkdtemp(temp_dir) != 0) { DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "reconnect fixture: mkdtemp failed"); return -1; }
snprintf(server_config_path, sizeof(server_config_path), "%s/server.conf", temp_dir);
snprintf(client_config_path, sizeof(client_config_path), "%s/client.conf", temp_dir);
return write_configs();
}
static int is_connection_established(struct UTUN_INSTANCE* inst) {
if (!inst || !inst->connections) return 0;
struct ll_entry* entry = inst->connections->head;
@ -326,7 +323,6 @@ int main(void) {
debug_config_init();
printf("=== ETCP Reconnect Test ===\n");
printf("Server port: %d, Client port: %d\n", server_port, client_port);
utun_instance_set_tun_init_enabled(0);
@ -339,6 +335,13 @@ int main(void) {
printf("Server ready (node_id=%llx)\n", (unsigned long long)server_instance->node_id);
client_instance = utun_instance_create(ua, client_config_path);
if (client_instance) {
server_port = ss_get_port(&test_etcp_bound_socket(server_instance, "test")->local_addr);
client_port = ss_get_port(&test_etcp_bound_socket(client_instance, "test")->local_addr);
test_etcp_set_peer(client_instance, "test_client", server_instance, "test");
if (write_configs() != 0) { DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "reconnect fixture: cannot persist endpoints"); test_completed = 2; goto cleanup; }
printf("Server port: %d, Client port: %d (retained for restart)\n", server_port, client_port);
}
if (!client_instance || utun_instance_init(client_instance) < 0) {
fprintf(stderr, "Failed to create client\n");
utun_instance_destroy(server_instance);
@ -362,6 +365,7 @@ int main(void) {
printf("Main loop exit: test_completed=%d phase=%d elapsed=%d iter=%d\n",
test_completed, phase, elapsed, iter);
cleanup:
if (packet_timeout_id) uasync_cancel_timeout(ua, packet_timeout_id);
if (global_timeout_id) uasync_cancel_timeout(ua, global_timeout_id);

37
tests/test_etcp_router_reconnect.c

@ -7,6 +7,7 @@
#include "../lib/platform_compat.h"
#include "test_utils.h"
#include "test_net.h"
#ifdef _WIN32
#include <windows.h>
#include <direct.h>
@ -77,15 +78,7 @@ static void gen_payload(uint32_t seq, uint8_t* buf, int len) {
for (int i=0;i<len;i++) buf[i]=(uint8_t)((i^seq^0xA5)&0xFF);
}
static int create_temp_configs(void) {
if (test_mkdtemp(temp_dir) != 0) { fprintf(stderr, "mkdtemp fail\n"); return -1; }
int base = 42000+(getpid()%15000);
a_port=base; b_port_a=base+1; b_port_c=base+2; c_port=base+3;
if (sc_generate_keypair(&keys_a)!=SC_OK||sc_generate_keypair(&keys_b)!=SC_OK||sc_generate_keypair(&keys_c)!=SC_OK) { fprintf(stderr,"keygen fail\n"); return -1; }
hex_encode(keys_a.private_key,32,privhex_a); hex_encode(keys_a.public_key,32,pubhex_a);
hex_encode(keys_b.private_key,32,privhex_b); hex_encode(keys_b.public_key,32,pubhex_b);
hex_encode(keys_c.private_key,32,privhex_c); hex_encode(keys_c.public_key,32,pubhex_c);
static int write_configs(void) {
snprintf(a_conf,sizeof(a_conf),"%s/a.conf",temp_dir);
FILE* f=fopen(a_conf,"w"); if(!f){perror("a_conf");return -1;}
fprintf(f,"[global]\nmy_private_key=%s\nmy_public_key=%s\ntun_ip=10.99.0.1/24\ntun_ifname=tun99\n\n"
@ -110,6 +103,17 @@ static int create_temp_configs(void) {
}
static void cleanup(void) { test_unlink(a_conf); test_unlink(b_conf); test_unlink(c_conf); test_rmdir(temp_dir); }
static int create_temp_configs(void) {
if (test_mkdtemp(temp_dir) != 0) { DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "router restart fixture: mkdtemp failed"); return -1; }
if (sc_generate_keypair(&keys_a) != SC_OK || sc_generate_keypair(&keys_b) != SC_OK || sc_generate_keypair(&keys_c) != SC_OK) {
DEBUG_ERROR(DEBUG_CATEGORY_CRYPTO, "router restart fixture: key generation failed"); return -1;
}
hex_encode(keys_a.private_key,32,privhex_a); hex_encode(keys_a.public_key,32,pubhex_a);
hex_encode(keys_b.private_key,32,privhex_b); hex_encode(keys_b.public_key,32,pubhex_b);
hex_encode(keys_c.private_key,32,privhex_c); hex_encode(keys_c.public_key,32,pubhex_c);
return write_configs();
}
static void recv_handler(struct ETCP_CONN* conn, struct ll_entry* entry) {
if (!entry || !entry->dgram) { fprintf(stderr, "RECV: null entry\n"); g_test_ok=-1; g_fail_code=10; return; }
if (entry->len == ROUTER_SVC_HDR_SIZE) { queue_dgram_free(entry); queue_entry_free(entry); return; }
@ -235,7 +239,7 @@ static void timeout_cb(void* arg) {
int main(void) {
srand(1);
if (create_temp_configs()!=0) return 1;
printf("=== ETCP Router Reconnect Test ===\nPorts: a=%d b_a=%d b_c=%d c=%d\n", a_port, b_port_a, b_port_c, c_port);
printf("=== ETCP Router Reconnect Test ===\n");
debug_config_init();
debug_set_level(DEBUG_LEVEL_ERROR);
utun_instance_set_tun_init_enabled(0);
@ -243,7 +247,18 @@ int main(void) {
ua=uasync_create(); if(!ua){cleanup();return 1;}
g_a=utun_instance_create(ua,a_conf); if(!g_a||utun_instance_init(g_a)<0){fprintf(stderr,"FAIL: a\n");goto fail;}
g_c=utun_instance_create(ua,c_conf); if(!g_c||utun_instance_init(g_c)<0){fprintf(stderr,"FAIL: c\n");goto fail;}
g_b=utun_instance_create(ua,b_conf); if(!g_b||utun_instance_init(g_b)<0){fprintf(stderr,"FAIL: b\n");goto fail;}
g_b=utun_instance_create(ua,b_conf);
if (g_b) {
a_port = ss_get_port(&test_etcp_bound_socket(g_a, "hub_a")->local_addr);
c_port = ss_get_port(&test_etcp_bound_socket(g_c, "hub_c")->local_addr);
b_port_a = ss_get_port(&test_etcp_bound_socket(g_b, "s_a")->local_addr);
b_port_c = ss_get_port(&test_etcp_bound_socket(g_b, "s_c")->local_addr);
test_etcp_set_peer(g_b, "to_a", g_a, "hub_a");
test_etcp_set_peer(g_b, "to_c", g_c, "hub_c");
if (write_configs() != 0) { DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "router restart fixture: cannot persist endpoints"); goto fail; }
printf("Ports retained for restart: a=%d b_a=%d b_c=%d c=%d\n", a_port, b_port_a, b_port_c, c_port);
}
if(!g_b||utun_instance_init(g_b)<0){fprintf(stderr,"FAIL: b\n");goto fail;}
const char* log = getenv("ROUTER_TEST_LOG");
if (log) {

Loading…
Cancel
Save