6 changed files with 428 additions and 87 deletions
@ -1,107 +1,213 @@ |
|||||||
// test_etcp_ping.c - unit test for etcp_send_ping with callback on PONG/timeout
|
// test_etcp_ping.c - full initialization with links (1 ua + 2 instances, valid keys from configs, real PONG via established link, readable formatting, no NULL link, passes make check)
|
||||||
#include <stdio.h> |
#include <stdio.h> |
||||||
#include <stdlib.h> |
#include <stdlib.h> |
||||||
#include <string.h> |
#include <string.h> |
||||||
#include <stdint.h> |
#include <time.h> |
||||||
|
#include <sys/stat.h> |
||||||
|
#include <unistd.h> |
||||||
|
#include "test_utils.h" |
||||||
|
#include "../src/etcp.h" |
||||||
#include "../src/etcp_connections.h" |
#include "../src/etcp_connections.h" |
||||||
|
#include "../src/config_parser.h" |
||||||
#include "../src/utun_instance.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" |
#include "../lib/u_async.h" |
||||||
#include "../lib/debug_config.h" |
#include "../lib/debug_config.h" |
||||||
#include "../lib/mem.h" |
|
||||||
#include "../src/secure_channel.h" |
|
||||||
|
|
||||||
#define TEST_TIMEOUT_MS 500 |
#define TEST_TIMEOUT_MS 5000 |
||||||
|
|
||||||
|
static struct UTUN_INSTANCE* server_instance = NULL; |
||||||
|
static struct UTUN_INSTANCE* client_instance = NULL; |
||||||
|
static struct UASYNC* ua = NULL; |
||||||
static int test_completed = 0; |
static int test_completed = 0; |
||||||
static int cb_called = 0; |
static void* monitor_timeout_id = NULL; |
||||||
static int cb_success = -1; |
static void* test_timeout_id = NULL; |
||||||
static uint64_t test_nonce = 0; |
static char temp_dir[] = "/tmp/utun_test_XXXXXX"; |
||||||
|
static char server_config_path[256]; |
||||||
|
static char client_config_path[256]; |
||||||
|
static int pong_received = 0; |
||||||
|
|
||||||
|
static const char* server_config_content = |
||||||
|
"[global]\n" |
||||||
|
"my_node_id=0x1111111111111111\n" |
||||||
|
"my_private_key=67b705a92b41bcaae105af2d6a17743faa7b26ccebba8b3b9b0af05e9cd1d5fb\n" |
||||||
|
"my_public_key=1c55e4ccae7c4470707759086738b10681bf88b81f198cc2ab54a647d1556e17c65e6b1833e0c771e5a39382c03067c388915a4c732191bc130480f20f8e00b9\n" |
||||||
|
"tun_ip=10.99.0.1/24\n" |
||||||
|
"tun_ifname=tun99\n" |
||||||
|
"\n" |
||||||
|
"[server:test]\n" |
||||||
|
"addr=127.0.0.1:9011\n" |
||||||
|
"type=public\n"; |
||||||
|
|
||||||
|
static const char* client_config_content = |
||||||
|
"[global]\n" |
||||||
|
"my_node_id=0x2222222222222222\n" |
||||||
|
"my_private_key=4813d31d28b7e9829247f488c6be7672f2bdf61b2508333128e386d1759afed2\n" |
||||||
|
"my_public_key=c594f33c91f3a2222795c2c110c527bf214ad1009197ce14556cb13df3c461b3c373bed8f205a8dd1fc0c364f90bf471d7c6f5db49564c33e4235d268569ac71\n" |
||||||
|
"tun_ip=10.99.0.2/24\n" |
||||||
|
"tun_ifname=tun98\n" |
||||||
|
"\n" |
||||||
|
"[server:test]\n" |
||||||
|
"addr=127.0.0.1:9012\n" |
||||||
|
"type=public\n" |
||||||
|
"\n" |
||||||
|
"[client:test_client]\n" |
||||||
|
"keepalive=1\n" |
||||||
|
"peer_public_key=1c55e4ccae7c4470707759086738b10681bf88b81f198cc2ab54a647d1556e17c65e6b1833e0c771e5a39382c03067c388915a4c732191bc130480f20f8e00b9\n" |
||||||
|
"link=test:127.0.0.1:9011\n"; |
||||||
|
|
||||||
|
static int create_temp_configs(void) { |
||||||
|
if (test_mkdtemp(temp_dir) != 0) { |
||||||
|
fprintf(stderr, "Failed to create temp directory\n"); |
||||||
|
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); |
||||||
|
FILE* f = fopen(server_config_path, "w"); |
||||||
|
if (!f) return -1; |
||||||
|
fprintf(f, "%s", server_config_content); |
||||||
|
fclose(f); |
||||||
|
f = fopen(client_config_path, "w"); |
||||||
|
if (!f) { |
||||||
|
test_unlink(server_config_path); |
||||||
|
return -1; |
||||||
|
} |
||||||
|
fprintf(f, "%s", client_config_content); |
||||||
|
fclose(f); |
||||||
|
return 0; |
||||||
|
} |
||||||
|
|
||||||
|
static void cleanup_temp_configs(void) { |
||||||
|
if (server_config_path[0]) test_unlink(server_config_path); |
||||||
|
if (client_config_path[0]) test_unlink(client_config_path); |
||||||
|
if (temp_dir[0]) test_rmdir(temp_dir); |
||||||
|
} |
||||||
|
|
||||||
static void ping_test_callback(int success, void* arg, uint64_t nonce) { |
static void monitor_connections(void* arg) { |
||||||
(void)arg; |
(void)arg; |
||||||
cb_called++; |
if (test_completed) { |
||||||
cb_success = success; |
monitor_timeout_id = NULL; |
||||||
test_nonce = nonce; |
return; |
||||||
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "ping_test_callback: success=%d nonce=%016llx", success, (unsigned long long)nonce); |
} |
||||||
|
int server_links = 0; |
||||||
|
int client_links = 0; |
||||||
|
if (server_instance) { |
||||||
|
struct ETCP_CONN* conn = server_instance->connections; |
||||||
|
while (conn) { |
||||||
|
struct ETCP_LINK* link = conn->links; |
||||||
|
while (link) { |
||||||
|
server_links++; |
||||||
|
if (link->initialized) test_completed = 1; |
||||||
|
link = link->next; |
||||||
|
} |
||||||
|
conn = conn->next; |
||||||
|
} |
||||||
|
} |
||||||
|
if (client_instance) { |
||||||
|
struct ETCP_CONN* conn = client_instance->connections; |
||||||
|
while (conn) { |
||||||
|
struct ETCP_LINK* link = conn->links; |
||||||
|
while (link) { |
||||||
|
client_links++; |
||||||
|
if (link->initialized) test_completed = 1; |
||||||
|
link = link->next; |
||||||
|
} |
||||||
|
conn = conn->next; |
||||||
|
} |
||||||
|
} |
||||||
|
if (server_links > 0 && client_links > 0) test_completed = 1; |
||||||
|
} |
||||||
|
|
||||||
|
static void test_timeout(void* arg) { |
||||||
|
(void)arg; |
||||||
|
if (!test_completed) { |
||||||
|
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "test timeout"); |
||||||
|
test_completed = 2; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
static void ping_callback(int success, void* arg, uint64_t nonce) { |
||||||
|
(void)arg; |
||||||
|
(void)nonce; |
||||||
|
pong_received = success; |
||||||
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "ping_callback: success=%d", success); |
||||||
test_completed = 1; |
test_completed = 1; |
||||||
} |
} |
||||||
|
|
||||||
int main(void) { |
int main(void) { |
||||||
|
int test_result = 1; |
||||||
debug_config_init(); |
debug_config_init(); |
||||||
debug_set_level(DEBUG_LEVEL_TRACE); |
debug_set_level(DEBUG_LEVEL_TRACE); |
||||||
debug_set_categories(DEBUG_CATEGORY_ALL); |
debug_set_categories(DEBUG_CATEGORY_ALL); |
||||||
|
utun_instance_set_tun_init_enabled(0); |
||||||
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "=== ETCP Ping Test ==="); |
if (create_temp_configs() != 0) return 1; |
||||||
|
ua = uasync_create(); |
||||||
struct UASYNC* ua = uasync_create(); |
|
||||||
if (!ua) { |
if (!ua) { |
||||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "uasync_create failed"); |
cleanup_temp_configs(); |
||||||
return 1; |
|
||||||
} |
|
||||||
|
|
||||||
struct UTUN_INSTANCE* instance = u_calloc(1, sizeof(struct UTUN_INSTANCE)); |
|
||||||
if (!instance) { |
|
||||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "instance alloc failed"); |
|
||||||
uasync_destroy(ua, 0); |
|
||||||
return 1; |
return 1; |
||||||
} |
} |
||||||
instance->ua = ua; |
server_instance = utun_instance_create(ua, server_config_path); |
||||||
instance->node_id = 0x1234567890ABCDEFULL; |
client_instance = utun_instance_create(ua, client_config_path); |
||||||
// dummy keys for test (full init in production)
|
if (!server_instance || !client_instance) { |
||||||
for (int i = 0; i < SC_PRIVKEY_SIZE; i++) { |
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "instance creation failed"); |
||||||
instance->my_keys.private_key[i] = i; |
goto cleanup; |
||||||
instance->my_keys.public_key[i] = i + 64; |
|
||||||
} |
} |
||||||
|
int ret; |
||||||
struct ETCP_SOCKET* dummy_sock = u_calloc(1, sizeof(struct ETCP_SOCKET)); |
ret = init_connections(server_instance); |
||||||
if (!dummy_sock) { |
if (ret != 0) { |
||||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "dummy sock alloc failed"); |
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "Server init_connections failed with code %d", ret); |
||||||
utun_instance_destroy(instance); |
goto cleanup; |
||||||
uasync_destroy(ua, 0); |
|
||||||
return 1; |
|
||||||
} |
} |
||||||
dummy_sock->instance = instance; |
ret = init_connections(client_instance); |
||||||
dummy_sock->fd = socket(AF_INET, SOCK_DGRAM, 0); |
|
||||||
dummy_sock->local_addr.ss_family = AF_INET; |
|
||||||
instance->etcp_sockets = dummy_sock; |
|
||||||
|
|
||||||
// dummy pubkey
|
|
||||||
uint8_t dummy_pubkey[SC_PUBKEY_SIZE]; |
|
||||||
for (int i = 0; i < SC_PUBKEY_SIZE; i++) dummy_pubkey[i] = i; |
|
||||||
|
|
||||||
struct sockaddr_storage test_addr; |
|
||||||
memset(&test_addr, 0, sizeof(test_addr)); |
|
||||||
struct sockaddr_in* sin = (struct sockaddr_in*)&test_addr; |
|
||||||
sin->sin_family = AF_INET; |
|
||||||
sin->sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
|
||||||
sin->sin_port = htons(12345); |
|
||||||
|
|
||||||
// test timeout path (no receiver, expect success=0)
|
|
||||||
int ret = etcp_send_ping(instance, dummy_pubkey, &test_addr, 100, ping_test_callback, NULL); |
|
||||||
if (ret != 0) { |
if (ret != 0) { |
||||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "etcp_send_ping failed ret=%d", ret); |
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "Client init_connections failed with code %d", ret); |
||||||
utun_instance_destroy(instance); |
goto cleanup; |
||||||
uasync_destroy(ua, 0); |
|
||||||
return 1; |
|
||||||
} |
} |
||||||
|
monitor_timeout_id = uasync_set_timeout(ua, 100, NULL, monitor_connections); |
||||||
// run uasync to trigger timeout cb
|
test_timeout_id = uasync_set_timeout(ua, TEST_TIMEOUT_MS, NULL, test_timeout); |
||||||
for (int i = 0; i < 20 && !test_completed; i++) { |
while (!test_completed) { |
||||||
uasync_poll(ua, 50); |
uasync_poll(ua, 10); |
||||||
} |
} |
||||||
|
if (test_completed == 2) { |
||||||
if (cb_called != 1 || cb_success != 0) { |
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "test timed out"); |
||||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "callback not triggered correctly: called=%d success=%d", cb_called, cb_success); |
goto cleanup; |
||||||
utun_instance_destroy(instance); |
|
||||||
uasync_destroy(ua, 0); |
|
||||||
return 1; |
|
||||||
} |
} |
||||||
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "Links established, running ping test"); |
||||||
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "Ping timeout test PASSED (cb called with success=0)"); |
struct ETCP_CONN* conn = client_instance->connections; |
||||||
|
if (conn && conn->links) { |
||||||
// cleanup
|
struct ETCP_LINK* link = conn->links; |
||||||
utun_instance_destroy(instance); |
uint8_t peer_pubkey[SC_PUBKEY_SIZE]; |
||||||
uasync_destroy(ua, 0); |
memcpy(peer_pubkey, link->etcp->crypto_ctx.peer_public_key, SC_PUBKEY_SIZE); |
||||||
|
ret = etcp_send_ping(client_instance, peer_pubkey, &link->remote_addr, 500, ping_callback, NULL); |
||||||
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "ETCP ping unit test PASSED"); |
if (ret == 0) { |
||||||
return 0; |
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "ping sent via real link"); |
||||||
|
test_completed = 0; |
||||||
|
for (int i = 0; i < 30 && !test_completed; i++) uasync_poll(ua, 50); |
||||||
|
if (pong_received) { |
||||||
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "PONG received via real link - TEST PASSED"); |
||||||
|
test_result = 0; |
||||||
|
} else { |
||||||
|
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "PONG not received - TEST FAILED"); |
||||||
|
} |
||||||
|
} else { |
||||||
|
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "etcp_send_ping failed with code %d", ret); |
||||||
|
} |
||||||
|
} else { |
||||||
|
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "No client connection or links available"); |
||||||
|
} |
||||||
|
cleanup: |
||||||
|
if (monitor_timeout_id) uasync_cancel_timeout(ua, monitor_timeout_id); |
||||||
|
if (test_timeout_id) uasync_cancel_timeout(ua, test_timeout_id); |
||||||
|
if (server_instance) utun_instance_destroy(server_instance); |
||||||
|
if (client_instance) utun_instance_destroy(client_instance); |
||||||
|
if (ua) uasync_destroy(ua, 0); |
||||||
|
cleanup_temp_configs(); |
||||||
|
if (test_result == 0) { |
||||||
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "ETCP ping test with full initialization and links PASSED"); |
||||||
|
} else { |
||||||
|
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "ETCP ping test FAILED"); |
||||||
|
} |
||||||
|
return test_result; |
||||||
} |
} |
||||||
|
|||||||
Loading…
Reference in new issue