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.
461 lines
15 KiB
461 lines
15 KiB
/** |
|
* @file test_bgp_route_exchange.c |
|
* @brief Интеграционный тест обмена маршрутами между двумя инстансами |
|
* |
|
* Тест проверяет: |
|
* - Создание двух инстансов с разными подсетями |
|
* - Установление ETCP соединения |
|
* - Обмен маршрутами через BGP |
|
* - Проверку таблиц роутинга (client -> server direction) |
|
*/ |
|
|
|
#include <stdio.h> |
|
#include <stdlib.h> |
|
#include <string.h> |
|
#include <time.h> |
|
#include <sys/stat.h> |
|
#include "../lib/platform_compat.h" |
|
#include "test_utils.h" |
|
|
|
#ifdef _WIN32 |
|
#include <windows.h> |
|
#include <direct.h> |
|
#define _mkdir(path) _mkdir(path) |
|
#else |
|
#include <unistd.h> |
|
#endif |
|
|
|
#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/route_lib.h" |
|
#include "../src/route_bgp.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 // 5 seconds timeout |
|
#define BGP_EXCHANGE_WAIT_MS 200 // Wait for BGP exchange (100ms + margin) |
|
|
|
// Test state |
|
static struct UTUN_INSTANCE* server_instance = NULL; |
|
static struct UTUN_INSTANCE* client_instance = NULL; |
|
static struct UASYNC* ua = NULL; |
|
static int test_phase = 0; // 0=init, 1=connected, 2=bgp_exchanged, 3=success, 4=failure |
|
static void* monitor_timeout_id = NULL; |
|
static void* test_timeout_id = NULL; |
|
|
|
// Temp config file paths |
|
#ifdef _WIN32 |
|
static char temp_dir[MAX_PATH]; |
|
#else |
|
static char temp_dir[] = "/tmp/utun_bgp_test_XXXXXX"; |
|
#endif |
|
static char server_config_path[256]; |
|
static char client_config_path[256]; |
|
|
|
// Cross-platform mkdtemp replacement |
|
static int create_temp_directory(void) { |
|
#ifdef _WIN32 |
|
char tmp_path[MAX_PATH]; |
|
GetTempPathA(MAX_PATH, tmp_path); |
|
|
|
// Generate unique directory name |
|
srand((unsigned int)GetTickCount()); |
|
int attempts = 0; |
|
while (attempts < 100) { |
|
snprintf(temp_dir, sizeof(temp_dir), "%s\\utun_bgp_test_%08x", |
|
tmp_path, (unsigned int)rand()); |
|
if (_mkdir(temp_dir) == 0) { |
|
return 0; // Success |
|
} |
|
attempts++; |
|
} |
|
return -1; // Failed after 100 attempts |
|
#else |
|
return (test_mkdtemp(temp_dir) != 0) ? -1 : 0; |
|
#endif |
|
} |
|
|
|
// Server config: 2 subnets - 192.168.10.0/24 and 192.168.11.0/24 |
|
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" |
|
"[routing]\n" |
|
"my_subnet=192.168.10.0/24\n" |
|
"my_subnet=192.168.11.0/24\n" |
|
"\n" |
|
"[server: test]\n" |
|
"addr=127.0.0.1:39011\n" |
|
"type=public\n"; |
|
|
|
// Client config: 2 subnets - 192.168.20.0/24 and 192.168.21.0/24 |
|
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" |
|
"[routing]\n" |
|
"my_subnet=192.168.20.0/24\n" |
|
"my_subnet=192.168.21.0/24\n" |
|
"\n" |
|
"[server: test]\n" |
|
"addr=127.0.0.1:39012\n" |
|
"type=public\n" |
|
"\n" |
|
"[client: test_client]\n" |
|
"keepalive=1\n" |
|
"peer_public_key=1c55e4ccae7c4470707759086738b10681bf88b81f198cc2ab54a647d1556e17c65e6b1833e0c771e5a39382c03067c388915a4c732191bc130480f20f8e00b9\n" |
|
"link=test:127.0.0.1:39011\n"; |
|
|
|
// Create temp config files |
|
static int create_temp_configs(void) { |
|
if (create_temp_directory() != 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) { |
|
fprintf(stderr, "Failed to create server config file\n"); |
|
return -1; |
|
} |
|
fprintf(f, "%s", server_config_content); |
|
fclose(f); |
|
|
|
f = fopen(client_config_path, "w"); |
|
if (!f) { |
|
fprintf(stderr, "Failed to create client config file\n"); |
|
test_unlink(server_config_path); |
|
return -1; |
|
} |
|
fprintf(f, "%s", client_config_content); |
|
fclose(f); |
|
|
|
return 0; |
|
} |
|
|
|
// Cleanup temp config files |
|
static void cleanup_temp_configs(void) { |
|
#ifdef _WIN32 |
|
if (server_config_path[0]) _unlink(server_config_path); |
|
if (client_config_path[0]) _unlink(client_config_path); |
|
if (temp_dir[0]) _rmdir(temp_dir); |
|
#else |
|
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); |
|
#endif |
|
} |
|
|
|
// Check if connection is established (link initialized) |
|
static int is_connection_established(struct UTUN_INSTANCE* inst) { |
|
if (!inst) return 0; |
|
|
|
struct ETCP_CONN* conn = inst->connections; |
|
while (conn) { |
|
struct ETCP_LINK* link = conn->links; |
|
while (link) { |
|
if (link->initialized) { |
|
return 1; |
|
} |
|
link = link->next; |
|
} |
|
conn = conn->next; |
|
} |
|
return 0; |
|
} |
|
|
|
// Print routing table contents |
|
static void print_routing_table(struct UTUN_INSTANCE* inst, const char* name) { |
|
if (!inst || !inst->rt) { |
|
DEBUG_INFO(DEBUG_CATEGORY_ROUTING, "%s: No routing table\n", name); |
|
return; |
|
} |
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ROUTING, "\n=== %s Routing Table (%zu routes) ===", name, inst->rt->count); |
|
|
|
for (size_t i = 0; i < inst->rt->count; i++) { |
|
struct ROUTE_ENTRY* entry = &inst->rt->entries[i]; |
|
char network_str[16]; |
|
struct in_addr addr; |
|
addr.s_addr = htonl(entry->network); |
|
inet_ntop(AF_INET, &addr, network_str, sizeof(network_str)); |
|
|
|
const char* type_str = entry->v_node_info ? "LEARNED" : "LOCAL"; |
|
|
|
uint64_t node_id = 0; |
|
if (entry->v_node_info) { |
|
node_id = be64toh(entry->v_node_info->node.node_id); |
|
} |
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ROUTING, " Route %zu: %s/%d [%s] node_id=%016llX", |
|
i + 1, network_str, entry->prefix_length, type_str, |
|
(unsigned long long)node_id); |
|
} |
|
DEBUG_INFO(DEBUG_CATEGORY_ROUTING, "=====================================\n"); |
|
} |
|
|
|
// Check if specific learned route exists |
|
static int check_learned_route(struct UTUN_INSTANCE* inst, uint32_t network, |
|
uint8_t prefix_len, uint64_t expected_node_id) { |
|
if (!inst || !inst->rt) { |
|
DEBUG_ERROR(DEBUG_CATEGORY_ROUTING, "check_learned_route: no instance or rt"); |
|
return 0; |
|
} |
|
|
|
uint64_t expected_be = htobe64(expected_node_id); |
|
for (size_t i = 0; i < inst->rt->count; i++) { |
|
struct ROUTE_ENTRY* entry = &inst->rt->entries[i]; |
|
if (entry->network == network && |
|
entry->prefix_length == prefix_len && |
|
entry->v_node_info != NULL && |
|
entry->v_node_info->node.node_id == expected_be) { |
|
return 1; |
|
} |
|
} |
|
return 0; |
|
} |
|
|
|
// Verify BGP route exchange results |
|
static int verify_bgp_exchange(void) { |
|
int success = 1; |
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ROUTING, "\n=== Verifying BGP Route Exchange ===\n"); |
|
|
|
// Print tables for debugging |
|
print_routing_table(server_instance, "SERVER"); |
|
print_routing_table(client_instance, "CLIENT"); |
|
|
|
// Check server learned client's routes (192.168.20.0/24, 192.168.21.0/24) |
|
DEBUG_INFO(DEBUG_CATEGORY_ROUTING, "Checking server learned client's routes..."); |
|
|
|
// Note: Routes are stored in host byte order, not network byte order |
|
if (!check_learned_route(server_instance,0x0014a8c0,24,0x2222222222222222ULL)) { |
|
DEBUG_ERROR(DEBUG_CATEGORY_ROUTING,"FAIL: Server missing learned route 192.168.20.0/24"); |
|
success = 0; |
|
} else { |
|
DEBUG_INFO(DEBUG_CATEGORY_ROUTING,"PASS: Server has learned route 192.168.20.0/24"); |
|
} |
|
|
|
if (!check_learned_route(server_instance,0x0015a8c0,24,0x2222222222222222ULL)) { |
|
DEBUG_ERROR(DEBUG_CATEGORY_ROUTING,"FAIL: Server missing learned route 192.168.21.0/24"); |
|
success = 0; |
|
} else { |
|
DEBUG_INFO(DEBUG_CATEGORY_ROUTING,"PASS: Server has learned route 192.168.21.0/24"); |
|
} |
|
|
|
// Check client learned server's routes (192.168.10.0/24, 192.168.11.0/24) |
|
// NOTE: For now we only test client -> server direction |
|
// This will be enabled after server -> client is implemented |
|
DEBUG_INFO(DEBUG_CATEGORY_ROUTING, "Checking client learned server's routes (disabled - server->client not implemented)..."); |
|
|
|
return success; |
|
} |
|
|
|
// Monitor callback - checks connection status and BGP exchange |
|
static void monitor_connections(void* arg) { |
|
(void)arg; |
|
|
|
if (test_phase >= 3) { |
|
monitor_timeout_id = NULL; |
|
return; |
|
} |
|
|
|
switch (test_phase) { |
|
case 0: { // Waiting for connection |
|
int server_connected = is_connection_established(server_instance); |
|
int client_connected = is_connection_established(client_instance); |
|
|
|
if (server_connected && client_connected) { |
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "=== Connection established! Waiting for BGP exchange... ==="); |
|
test_phase = 1; |
|
// Schedule next check after BGP exchange time |
|
monitor_timeout_id = uasync_set_timeout(ua, BGP_EXCHANGE_WAIT_MS, NULL, monitor_connections); |
|
return; |
|
} |
|
break; |
|
} |
|
|
|
case 1: { // Connection established, wait for BGP exchange |
|
DEBUG_INFO(DEBUG_CATEGORY_ROUTING, "=== Checking BGP route exchange ==="); |
|
test_phase = 2; |
|
|
|
// Verify routes |
|
if (verify_bgp_exchange()) { |
|
DEBUG_INFO(DEBUG_CATEGORY_ROUTING, "=== BGP ROUTE EXCHANGE SUCCESS ==="); |
|
test_phase = 3; // Success |
|
} else { |
|
DEBUG_ERROR(DEBUG_CATEGORY_ROUTING, "=== BGP ROUTE EXCHANGE FAILED ==="); |
|
test_phase = 4; // Failure |
|
} |
|
|
|
// Cancel timeout for quick exit |
|
if (test_timeout_id) { |
|
uasync_cancel_timeout(ua, test_timeout_id); |
|
test_timeout_id = NULL; |
|
} |
|
return; |
|
} |
|
|
|
case 2: // Verification done |
|
return; |
|
} |
|
|
|
// Schedule next check |
|
if (test_phase < 3) { |
|
monitor_timeout_id = uasync_set_timeout(ua, 100, NULL, monitor_connections); |
|
} |
|
} |
|
|
|
static void test_timeout(void* arg) { |
|
(void)arg; |
|
if (test_phase < 3) { |
|
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "\n=== TEST TIMEOUT ==="); |
|
test_phase = 4; // Failure |
|
if (monitor_timeout_id) { |
|
uasync_cancel_timeout(ua, monitor_timeout_id); |
|
monitor_timeout_id = NULL; |
|
} |
|
} |
|
} |
|
|
|
int main() { |
|
printf("=== BGP Route Exchange Test ===\n"); |
|
|
|
// Create temp config files |
|
if (create_temp_configs() != 0) { |
|
fprintf(stderr, "Failed to create temporary config files\n"); |
|
return 1; |
|
} |
|
|
|
debug_config_init(); |
|
debug_set_level(DEBUG_LEVEL_INFO); |
|
debug_set_categories(DEBUG_CATEGORY_ROUTING | DEBUG_CATEGORY_ETCP | DEBUG_CATEGORY_BGP); |
|
|
|
// Disable TUN for testing |
|
utun_instance_set_tun_init_enabled(0); |
|
|
|
// Create shared uasync context |
|
ua = uasync_create(); |
|
if (!ua) { |
|
fprintf(stderr, "Failed to create uasync context\n"); |
|
return 1; |
|
} |
|
|
|
// Create server instance |
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "Creating server instance..."); |
|
server_instance = utun_instance_create(ua, server_config_path); |
|
if (!server_instance) { |
|
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "Failed to create server instance"); |
|
return 1; |
|
} |
|
|
|
// Initialize server instance (includes connections, routing, and BGP) |
|
if (utun_instance_init(server_instance) < 0) { |
|
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "Failed to initialize server instance"); |
|
return 1; |
|
} |
|
|
|
// BGP module is now initialized automatically in utun_instance_create |
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "Server instance ready (node_id=%016llX, bgp=%p)", |
|
(unsigned long long)server_instance->node_id, (void*)server_instance->bgp); |
|
|
|
// Create client instance |
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "Creating client instance..."); |
|
client_instance = utun_instance_create(ua, client_config_path); |
|
if (!client_instance) { |
|
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "Failed to create client instance"); |
|
return 1; |
|
} |
|
|
|
// Initialize client instance (includes connections, routing, and BGP) |
|
if (utun_instance_init(client_instance) < 0) { |
|
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "Failed to initialize client instance"); |
|
return 1; |
|
} |
|
|
|
// BGP module is now initialized automatically in utun_instance_create |
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "Client instance ready (node_id=%016llX, bgp=%p)", |
|
(unsigned long long)client_instance->node_id, (void*)client_instance->bgp); |
|
|
|
// Give server time to initialize |
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "Waiting for server initialization..."); |
|
for (int i = 0; i < 50; i++) { |
|
uasync_poll(ua, 10); |
|
} |
|
|
|
// Start monitoring |
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "Starting connection monitoring..."); |
|
monitor_timeout_id = uasync_set_timeout(ua, 100, NULL, monitor_connections); |
|
test_timeout_id = uasync_set_timeout(ua, TEST_TIMEOUT_MS, NULL, test_timeout); |
|
|
|
// Main event loop - use uasync timers only, no artificial timing |
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "Running event loop..."); |
|
|
|
while (test_phase < 3) { |
|
uasync_poll(ua, -1); // Wait indefinitely for events/timeouts |
|
} |
|
|
|
// Cleanup |
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "\nCleaning up..."); |
|
|
|
if (monitor_timeout_id) { |
|
uasync_cancel_timeout(ua, monitor_timeout_id); |
|
monitor_timeout_id = NULL; |
|
} |
|
if (test_timeout_id) { |
|
uasync_cancel_timeout(ua, test_timeout_id); |
|
test_timeout_id = NULL; |
|
} |
|
|
|
// Print final routing tables |
|
print_routing_table(server_instance, "SERVER (final)"); |
|
print_routing_table(client_instance, "CLIENT (final)"); |
|
|
|
// Destroy instances |
|
if (server_instance) { |
|
server_instance->running = 0; |
|
utun_instance_destroy(server_instance); |
|
} |
|
if (client_instance) { |
|
client_instance->running = 0; |
|
utun_instance_destroy(client_instance); |
|
} |
|
|
|
if (ua) { |
|
uasync_destroy(ua, 0); |
|
ua = NULL; |
|
} |
|
|
|
cleanup_temp_configs(); |
|
|
|
// Test result |
|
if (test_phase == 3) { |
|
printf("\n=== TEST PASSED ===\n"); |
|
printf("BGP route exchange working (client -> server direction)\n"); |
|
return 0; |
|
} else { |
|
printf("\n=== TEST FAILED ===\n"); |
|
if (test_phase == 4) { |
|
printf("BGP route exchange verification failed\n"); |
|
} else { |
|
printf("Test timeout or connection failed\n"); |
|
} |
|
return 1; |
|
} |
|
}
|
|
|