diff --git a/.gitignore b/.gitignore index bf5461d7..91b8d274 100644 --- a/.gitignore +++ b/.gitignore @@ -77,6 +77,7 @@ logs/ # Test binaries (executables without extension), but track source/config files tests/test_* !tests/test_*.c +!tests/test_*.h !tests/test_*.conf !tests/test_*.json tests/bench_* diff --git a/tests/test_utils.h b/tests/test_utils.h new file mode 100644 index 00000000..1d45c357 --- /dev/null +++ b/tests/test_utils.h @@ -0,0 +1,73 @@ +/** + * @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 +#include +#include + +#ifdef _WIN32 +#include +#include + +// 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 + +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