Browse Source
- test_etcp_connection_full.c creates two full UTUN instances - Uses real config parsing and init_connections() - Instances communicate over 127.0.0.1:9001 - Test validates complete handshake processv2_dev
2 changed files with 168 additions and 27 deletions
@ -0,0 +1,151 @@
|
||||
#include <stdio.h> |
||||
#include <stdlib.h> |
||||
#include <string.h> |
||||
#include <unistd.h> |
||||
#include <sys/socket.h> |
||||
#include <netinet/in.h> |
||||
#include <arpa/inet.h> |
||||
#include <time.h> |
||||
#include <pthread.h> |
||||
|
||||
#include "../src/etcp.h" |
||||
#include "../src/etcp_connections.h" |
||||
#include "../src/config_parser.h" |
||||
#include "../src/utun_instance.h" |
||||
#include "../src/routing.h" |
||||
#include "../src/tun_if.h" |
||||
#include "../src/secure_channel.h" |
||||
#include "../lib/u_async.h" |
||||
|
||||
#define TEST_TIMEOUT_MS 5000 |
||||
|
||||
struct test_instance { |
||||
struct UTUN_INSTANCE* instance; |
||||
const char* config_file; |
||||
pthread_t thread; |
||||
int ready; |
||||
}; |
||||
|
||||
static void* instance_thread(void* arg) { |
||||
struct test_instance* ti = (struct test_instance*)arg; |
||||
|
||||
// Create instance
|
||||
ti->instance = utun_instance_create(uasync_create(), ti->config_file, NULL); |
||||
if (!ti->instance) { |
||||
printf("Failed to create instance from %s\n", ti->config_file); |
||||
return NULL; |
||||
} |
||||
|
||||
// Initialize connections
|
||||
if (init_connections(ti->instance) < 0) { |
||||
printf("Failed to init connections for %s\n", ti->config_file); |
||||
utun_instance_destroy(ti->instance); |
||||
ti->instance = NULL; |
||||
return NULL; |
||||
} |
||||
|
||||
ti->ready = 1; |
||||
printf("Instance %s ready\n", ti->config_file); |
||||
|
||||
// Run event loop
|
||||
while (ti->instance && ti->instance->running) { |
||||
uasync_poll(ti->instance->ua, 10); |
||||
} |
||||
|
||||
return NULL; |
||||
} |
||||
|
||||
static int wait_for_connection(struct UTUN_INSTANCE* instance, uint64_t peer_node_id, int timeout_ms) { |
||||
int elapsed = 0; |
||||
while (elapsed < timeout_ms) { |
||||
struct ETCP_CONN* conn = instance->connections; |
||||
while (conn) { |
||||
if (conn->peer_node_id == peer_node_id) { |
||||
struct ETCP_LINK* link = conn->links; |
||||
while (link) { |
||||
if (link->initialized) { |
||||
printf("Connection to peer %llx established!\n", (unsigned long long)peer_node_id); |
||||
return 1; |
||||
} |
||||
link = link->next; |
||||
} |
||||
} |
||||
conn = conn->next; |
||||
} |
||||
|
||||
usleep(10000); |
||||
elapsed += 10; |
||||
} |
||||
return 0; |
||||
} |
||||
|
||||
int main() { |
||||
printf("=== ETCP Full Connection Test ===\n\n"); |
||||
|
||||
// Create config files - fix format based on utun.conf
|
||||
FILE* server_conf = fopen("/tmp/test_server.conf", "w"); |
||||
fprintf(server_conf, "[global]\n"); |
||||
fprintf(server_conf, "my_node_id=0x1111111111111111\n"); |
||||
fprintf(server_conf, "my_private_key=1313912e5d34768983b0e06530a48c77816d228a5b5605e1ab3dc443d107a3dc\n"); |
||||
fprintf(server_conf, "my_public_key=dde6cec8a9023339a758f60883ef41534d24a1ffdc09bbb787a5c24ddfd891e3092461835a97d37944c681fc6b2c1f5acde8ad192f7d2cdc9920aa0d3ff78e99\n"); |
||||
fprintf(server_conf, "\n[server:test]\n"); |
||||
fprintf(server_conf, "addr=127.0.0.1:9001\n"); |
||||
fprintf(server_conf, "type=public\n"); |
||||
fclose(server_conf); |
||||
|
||||
FILE* client_conf = fopen("/tmp/test_client.conf", "w"); |
||||
fprintf(client_conf, "[global]\n"); |
||||
fprintf(client_conf, "my_node_id=0x2222222222222222\n"); |
||||
fprintf(client_conf, "my_private_key=1313912e5d34768983b0e06530a48c77816d228a5b5605e1ab3dc443d107a3dc\n"); |
||||
fprintf(client_conf, "my_public_key=dde6cec8a9023339a758f60883ef41534d24a1ffdc09bbb787a5c24ddfd891e3092461835a97d37944c681fc6b2c1f5acde8ad192f7d2cdc9920aa0d3ff78e99\n"); |
||||
fprintf(client_conf, "\n[client:test]\n"); |
||||
fprintf(client_conf, "peer_node_id=0x1111111111111111\n"); |
||||
fprintf(client_conf, "keepalive=1\n"); |
||||
fprintf(client_conf, "link=test:127.0.0.1:9001\n"); |
||||
fclose(client_conf); |
||||
|
||||
// Create instances
|
||||
struct test_instance server = {.config_file = "/tmp/test_server.conf"}; |
||||
struct test_instance client = {.config_file = "/tmp/test_client.conf"}; |
||||
|
||||
printf("Starting server instance...\n"); |
||||
if (pthread_create(&server.thread, NULL, instance_thread, &server) != 0) { |
||||
printf("Failed to create server thread\n"); |
||||
return 1; |
||||
} |
||||
|
||||
printf("Starting client instance...\n"); |
||||
if (pthread_create(&client.thread, NULL, instance_thread, &client) != 0) { |
||||
printf("Failed to create client thread\n"); |
||||
return 1; |
||||
} |
||||
|
||||
// Wait for instances to be ready
|
||||
while (!server.ready || !client.ready) { |
||||
usleep(10000); |
||||
} |
||||
|
||||
printf("\n=== Testing connection establishment ===\n"); |
||||
|
||||
// Wait for connection
|
||||
int connected = wait_for_connection(client.instance, 0x1111111111111111ULL, TEST_TIMEOUT_MS); |
||||
|
||||
// Cleanup
|
||||
if (server.instance) { |
||||
server.instance->running = 0; |
||||
} |
||||
if (client.instance) { |
||||
client.instance->running = 0; |
||||
} |
||||
|
||||
pthread_join(server.thread, NULL); |
||||
pthread_join(client.thread, NULL); |
||||
|
||||
if (connected) { |
||||
printf("\n=== TEST PASSED ===\n"); |
||||
return 0; |
||||
} else { |
||||
printf("\n=== TEST FAILED ===\n"); |
||||
return 1; |
||||
} |
||||
} |
||||
Loading…
Reference in new issue