You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
224 lines
7.9 KiB
224 lines
7.9 KiB
// 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 "etcp.h" |
|
#include "etcp_connections.h" |
|
#include "../src/config_parser.h" |
|
#include "../src/utun_instance.h" |
|
#include "routing.h" |
|
#include "../src/tun_if.h" |
|
#include "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=38240cb82199e504686507f11f6eaa4f740fde6f0c425c495e49a523019a5d68\n" |
|
"my_public_key=ce8871f07fa056c636d297115f231b08c29cdf94e0d440fce83a07c34416d36a\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=704f2e012c8fa8768130cb0f988a997dccb628372bc5ceccacc78dcbfec5916f\n" |
|
"my_public_key=b3193173def895bd0fcea6f86af077c7d77216f10395275f627ac18242ec0f01\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=ce8871f07fa056c636d297115f231b08c29cdf94e0d440fce83a07c34416d36a\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 ll_entry* e = server_instance->connections->head; |
|
while (e) { |
|
struct conn_queue_entry* ce = (struct conn_queue_entry*)e->data; |
|
struct ETCP_CONN* conn = ce->conn; |
|
struct ETCP_LINK* link = conn->links; |
|
while (link) { |
|
server_links++; |
|
if (link->initialized) test_completed = 1; |
|
link = link->next; |
|
} |
|
e = e->next; |
|
} |
|
} |
|
if (client_instance) { |
|
struct ll_entry* e = client_instance->connections->head; |
|
while (e) { |
|
struct conn_queue_entry* ce = (struct conn_queue_entry*)e->data; |
|
struct ETCP_CONN* conn = ce->conn; |
|
struct ETCP_LINK* link = conn->links; |
|
while (link) { |
|
client_links++; |
|
if (link->initialized) test_completed = 1; |
|
link = link->next; |
|
} |
|
e = e->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 && client_instance->connections->head) ? ((struct conn_queue_entry*)client_instance->connections->head->data)->conn : NULL; |
|
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; |
|
}
|
|
|