Files
utun2/tests/test_utils.h
T
Evgeny 27f2edca40 Fix remaining Windows test build issues
- Remove sys/socket.h include from test_etcp_two_instances.c (Linux-only)
- Fix test_mkdtemp buffer overflow - use memcpy instead of strncpy
- Fix test_mkdtemp return value check (replace == NULL with != 0)
- Add Windows socket libraries (-lws2_32 -liphlpapi) to test builds
- All tests build successfully on Linux
2026-02-15 04:24:46 +03:00

74 lines
1.7 KiB
C

/**
* @file test_utils.h
* @brief Utility functions for cross-platform tests
*/
#ifndef TEST_UTILS_H
#define TEST_UTILS_H
#include "../lib/platform_compat.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
#include <windows.h>
#include <direct.h>
// Windows temp directory buffer
static char test_temp_dir[MAX_PATH];
// Cross-platform mkdtemp for Windows
static inline int test_mkdtemp(char *template_str) {
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(test_temp_dir, sizeof(test_temp_dir), "%s\\utun_test_%08x",
tmp_path, (unsigned int)rand());
if (_mkdir(test_temp_dir) == 0) {
// Copy path back to caller's buffer if provided
if (template_str) {
size_t len = strlen(test_temp_dir);
memcpy(template_str, test_temp_dir, len + 1); // Include null terminator
}
return 0; // Success
}
attempts++;
}
return -1; // Failed after 100 attempts
}
// Cross-platform unlink for Windows
static inline int test_unlink(const char *path) {
return _unlink(path);
}
// Cross-platform rmdir for Windows
static inline int test_rmdir(const char *path) {
return _rmdir(path);
}
#else
// Linux/macOS - use standard functions
#include <unistd.h>
static inline int test_mkdtemp(char *template_str) {
return (mkdtemp(template_str) == NULL) ? -1 : 0;
}
static inline int test_unlink(const char *path) {
return unlink(path);
}
static inline int test_rmdir(const char *path) {
return rmdir(path);
}
#endif
#endif // TEST_UTILS_H