- Added CFG_ALLOWED_KEY struct with binary key storage (64 bytes) for fast memcmp comparison - Implemented parsing of [allowed_keys] section with allow_all flag and per-key entries - Added key validation in etcp_connections.c server handler (errorcode=8 for rejected keys) - Default behavior: deny all if [allowed_keys] section is missing or allow_all=0 with no keys - Added proper memory cleanup and debug logging for allowed_keys configuration - Updated all test configs to include [allowed_keys] allow_all=1 for backward compatibility
221 lines
7.7 KiB
C
221 lines
7.7 KiB
C
// 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 <stdlib.h>
|
|
#include <string.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/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"
|
|
#include "../lib/debug_config.h"
|
|
|
|
#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 void* monitor_timeout_id = NULL;
|
|
static void* test_timeout_id = NULL;
|
|
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"
|
|
"\n"
|
|
"[allowed_keys]\n"
|
|
"allow_all=1\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 monitor_connections(void* arg) {
|
|
(void)arg;
|
|
if (test_completed) {
|
|
monitor_timeout_id = NULL;
|
|
return;
|
|
}
|
|
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, uint16_t rtt, void* arg, uint64_t nonce,
|
|
const uint8_t* resp_data, size_t resp_data_len) {
|
|
(void)rtt;
|
|
(void)arg;
|
|
(void)nonce;
|
|
(void)resp_data;
|
|
(void)resp_data_len;
|
|
pong_received = success;
|
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "ping_callback: success=%d rtt=%u", success, (unsigned)rtt);
|
|
test_completed = 1;
|
|
}
|
|
|
|
int main(void) {
|
|
int test_result = 1;
|
|
debug_config_init();
|
|
debug_set_level(DEBUG_LEVEL_TRACE);
|
|
debug_set_categories(DEBUG_CATEGORY_ALL);
|
|
utun_instance_set_tun_init_enabled(0);
|
|
if (create_temp_configs() != 0) return 1;
|
|
ua = uasync_create();
|
|
if (!ua) {
|
|
cleanup_temp_configs();
|
|
return 1;
|
|
}
|
|
server_instance = utun_instance_create(ua, server_config_path);
|
|
client_instance = utun_instance_create(ua, client_config_path);
|
|
if (!server_instance || !client_instance) {
|
|
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "instance creation failed");
|
|
goto cleanup;
|
|
}
|
|
int ret;
|
|
ret = init_connections(server_instance);
|
|
if (ret != 0) {
|
|
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "Server init_connections failed with code %d", ret);
|
|
goto cleanup;
|
|
}
|
|
ret = init_connections(client_instance);
|
|
if (ret != 0) {
|
|
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "Client init_connections failed with code %d", ret);
|
|
goto cleanup;
|
|
}
|
|
monitor_timeout_id = uasync_set_timeout(ua, 100, NULL, monitor_connections, "test_monitor");
|
|
test_timeout_id = uasync_set_timeout(ua, TEST_TIMEOUT_MS, NULL, test_timeout, "test_timeout");
|
|
while (!test_completed) {
|
|
uasync_poll(ua, 10);
|
|
}
|
|
if (test_completed == 2) {
|
|
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "test timed out");
|
|
goto cleanup;
|
|
}
|
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "Links established, running ping test");
|
|
struct ETCP_CONN* conn = client_instance->connections;
|
|
if (conn && conn->links) {
|
|
struct ETCP_LINK* link = conn->links;
|
|
uint8_t peer_pubkey[SC_PUBKEY_SIZE];
|
|
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, NULL, 0);
|
|
if (ret == 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;
|
|
}
|