From 7ed28d2f501a4920b792bee9da3d61814f024a4c Mon Sep 17 00:00:00 2001 From: Brent Cook Date: Tue, 7 Oct 2025 05:38:30 -0500 Subject: add mkstemp for msvc builds --- include/compat/unistd.h | 2 ++ tests/CMakeLists.txt | 21 +++++++++----------- tests/compat/mkstemp.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 12 deletions(-) create mode 100644 tests/compat/mkstemp.c diff --git a/include/compat/unistd.h b/include/compat/unistd.h index 63c07fc..544cb27 100644 --- a/include/compat/unistd.h +++ b/include/compat/unistd.h @@ -45,6 +45,8 @@ static inline unsigned int sleep(unsigned int seconds) Sleep(seconds * 1000); return seconds; } + +int mkstemp(char *template); #endif int ftruncate(int fd, off_t length); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 55529cd..7f9b93c 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -82,6 +82,9 @@ endfunction() # XXX - should probably be in their own static lib set(TEST_HELPER_SRC test.c test_util.c) +if(WIN32) + set(TEST_HELPER_SRC ${TEST_HELPER_SRC} compat/mkstemp.c) +endif() # aeadtest add_executable(aeadtest aeadtest.c) @@ -555,12 +558,9 @@ prepare_emscripten_test_target(lhash_test) add_platform_test(lhash_test lhash_test) # md_test -# XXX - ftruncate and mkstemp missing from Windows -if(NOT WIN32) - add_executable(md_test md_test.c ${TEST_HELPER_SRC}) - target_link_libraries(md_test ${OPENSSL_TEST_LIBS}) - add_platform_test(md_test md_test) -endif() +add_executable(md_test md_test.c ${TEST_HELPER_SRC}) +target_link_libraries(md_test ${OPENSSL_TEST_LIBS}) +add_platform_test(md_test md_test) # mlkem_tests add_executable(mlkem_tests mlkem_tests.c parse_test_file.c) @@ -742,12 +742,9 @@ endif() set_tests_properties(servertest PROPERTIES ENVIRONMENT "srcdir=${TEST_SOURCE_DIR}") # sha_test -# XXX - ftruncate and mkstemp missing from Windows -if(NOT WIN32) - add_executable(sha_test sha_test.c ${TEST_HELPER_SRC}) - target_link_libraries(sha_test ${OPENSSL_TEST_LIBS}) - add_platform_test(sha_test sha_test) -endif() +add_executable(sha_test sha_test.c ${TEST_HELPER_SRC}) +target_link_libraries(sha_test ${OPENSSL_TEST_LIBS}) +add_platform_test(sha_test sha_test) # shutdowntest set(SHUTDOWNTEST_SRC shutdowntest.c) diff --git a/tests/compat/mkstemp.c b/tests/compat/mkstemp.c new file mode 100644 index 0000000..fe3d15a --- /dev/null +++ b/tests/compat/mkstemp.c @@ -0,0 +1,52 @@ +/* +musl as a whole is licensed under the following standard MIT license: + +---------------------------------------------------------------------- +Copyright © 2005-2020 Rich Felker, et al. + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +---------------------------------------------------------------------- +*/ + +#include +#include +#include +#include +#include +#include +#include + +#include + +int mkstemp(char *template) +{ + int fd; +retry: + if (!_mktemp(template)) return -1; + fd = open(template, O_RDWR | O_CREAT | O_EXCL, 0600); + if (fd >= 0) return fd; + if (errno == EEXIST) { + /* this is safe because mktemp verified + * that we have a valid template string */ + strcpy(template+strlen(template)-6, "XXXXXX"); + goto retry; + } + return -1; +} -- cgit v1.2.3-55-g6feb