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.
138 lines
5.8 KiB
138 lines
5.8 KiB
/** |
|
* Тест загрузки debug настроек из конфигурационного файла |
|
*/ |
|
|
|
#include <stdio.h> |
|
#include <stdlib.h> |
|
#include <string.h> |
|
#include "../lib/platform_compat.h" |
|
#include "test_utils.h" |
|
#ifdef _WIN32 |
|
#include <windows.h> |
|
#include <direct.h> |
|
#else |
|
#include <unistd.h> |
|
#endif |
|
#include <sys/stat.h> |
|
#include "../lib/debug_config.h" |
|
#include "../lib/socket_compat.h" |
|
#include "../src/config_parser.h" |
|
|
|
// Temp config file path |
|
static char temp_dir[] = "/tmp/utun_test_XXXXXX"; |
|
static char config_path[256]; |
|
|
|
// Config content |
|
static const char* 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" |
|
"# Debug and logging configuration\n" |
|
"log_file=/tmp/utun_debug.log\n" |
|
"debug_level=debug\n" |
|
"debug_categories=etcp,ll_queue,memory,traffic\n" |
|
"enable_timestamp=1\n" |
|
"enable_function_names=1\n" |
|
"enable_file_lines=1\n" |
|
"enable_colors=1\n" |
|
"\n" |
|
"[server: test]\n" |
|
"addr=127.0.0.1:9001\n" |
|
"type=public\n"; |
|
|
|
// Create temp config file |
|
static int create_temp_config(void) { |
|
if (test_mkdtemp(temp_dir) != 0) { |
|
fprintf(stderr, "Failed to create temp directory\n"); |
|
return -1; |
|
} |
|
|
|
snprintf(config_path, sizeof(config_path), "%s/test.conf", temp_dir); |
|
|
|
FILE* f = fopen(config_path, "w"); |
|
if (!f) { |
|
fprintf(stderr, "Failed to create config file\n"); |
|
return -1; |
|
} |
|
fprintf(f, "%s", config_content); |
|
fclose(f); |
|
|
|
return 0; |
|
} |
|
|
|
// Cleanup temp config file |
|
static void cleanup_temp_config(void) { |
|
if (config_path[0]) test_unlink(config_path); |
|
if (temp_dir[0]) test_rmdir(temp_dir); |
|
} |
|
|
|
int main() { |
|
// Initialize socket platform (required for getaddrinfo on Windows) |
|
if (socket_platform_init() != 0) { |
|
fprintf(stderr, "Failed to initialize socket platform\n"); |
|
return 1; |
|
} |
|
|
|
// Create temp config file first |
|
if (create_temp_config() != 0) { |
|
fprintf(stderr, "Failed to create temporary config file\n"); |
|
socket_platform_cleanup(); |
|
return 1; |
|
} |
|
|
|
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "=== Тест загрузки debug настроек из конфигурационного файла ==="); |
|
|
|
// Инициализация отладки по умолчанию |
|
debug_config_init(); |
|
debug_set_level(DEBUG_LEVEL_ERROR); // Начальный уровень |
|
|
|
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "1. До загрузки конфигурации:"); |
|
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "ERROR сообщение до загрузки конфигурации"); |
|
DEBUG_INFO(DEBUG_CATEGORY_CONFIG, "INFO сообщение до загрузки конфигурации (не должно выводиться)"); |
|
|
|
// Загрузка конфигурации из файла |
|
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "2. Загрузка конфигурации из файла: %s", config_path); |
|
struct utun_config *config = parse_config(config_path); |
|
|
|
if (!config) { |
|
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "❌ Ошибка загрузки конфигурации"); |
|
return 1; |
|
} |
|
|
|
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "✅ Конфигурация загружена успешно"); |
|
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, " Log file: %s", config->global.log_file[0] ? config->global.log_file : "(none)"); |
|
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, " Debug level: %s", config->global.debug_level[0] ? config->global.debug_level : "(none)"); |
|
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, " Enable timestamp: %d", config->global.enable_timestamp); |
|
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, " Enable colors: %d", config->global.enable_colors); |
|
|
|
// Тестирование новых настроек |
|
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "4. Тестирование после применения настроек:"); |
|
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "ERROR от ETCP (должен выводиться)"); |
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "INFO от ETCP (должен выводиться - уровень debug)"); |
|
DEBUG_DEBUG(DEBUG_CATEGORY_ETCP, "DEBUG от ETCP (должен выводиться - уровень debug)"); |
|
DEBUG_INFO(DEBUG_CATEGORY_LL_QUEUE, "INFO от LL_QUEUE (должен выводиться)"); |
|
DEBUG_INFO(DEBUG_CATEGORY_MEMORY, "INFO от MEMORY (должен выводиться)"); |
|
DEBUG_INFO(DEBUG_CATEGORY_TRAFFIC, "INFO от TRAFFIC (должен выводиться)"); |
|
DEBUG_DEBUG(DEBUG_CATEGORY_TRAFFIC, "DEBUG от TRAFFIC (должен выводиться - уровень debug)"); |
|
DEBUG_INFO(DEBUG_CATEGORY_CONNECTION, "INFO от CONNECTION (НЕ должен выводиться - категория не включена)"); |
|
DEBUG_INFO(DEBUG_CATEGORY_CRYPTO, "INFO от CRYPTO (НЕ должен выводиться - категория не включена)"); |
|
|
|
// Проверка работы с файлом |
|
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "5. Проверка вывода в файл:"); |
|
DEBUG_INFO(DEBUG_CATEGORY_CONFIG, "Это сообщение должно быть записано в файл: /tmp/utun_debug.log"); |
|
|
|
// Освобождение ресурсов |
|
free_config(config); |
|
|
|
// Cleanup temp config file |
|
cleanup_temp_config(); |
|
|
|
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "=== Тест завершен ==="); |
|
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "Проверьте файл /tmp/utun_debug.log для записанных сообщений"); |
|
|
|
socket_platform_cleanup(); |
|
return 0; |
|
} |