aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrent Cook <busterb@gmail.com>2025-10-07 05:38:30 -0500
committerBrent Cook <busterb@gmail.com>2025-10-07 05:38:30 -0500
commit7ed28d2f501a4920b792bee9da3d61814f024a4c (patch)
treeb5ff3f835188bb8ba3a00a9d7185a3762ffa6c15
parent659e87fe1cf3348a4e9f1bacfe205316fefd8a51 (diff)
downloadportable-7ed28d2f501a4920b792bee9da3d61814f024a4c.tar.gz
portable-7ed28d2f501a4920b792bee9da3d61814f024a4c.tar.bz2
portable-7ed28d2f501a4920b792bee9da3d61814f024a4c.zip
add mkstemp for msvc builds
-rw-r--r--include/compat/unistd.h2
-rw-r--r--tests/CMakeLists.txt21
-rw-r--r--tests/compat/mkstemp.c52
3 files changed, 63 insertions, 12 deletions
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)
45 Sleep(seconds * 1000); 45 Sleep(seconds * 1000);
46 return seconds; 46 return seconds;
47} 47}
48
49int mkstemp(char *template);
48#endif 50#endif
49 51
50int ftruncate(int fd, off_t length); 52int 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()
82 82
83# XXX - should probably be in their own static lib 83# XXX - should probably be in their own static lib
84set(TEST_HELPER_SRC test.c test_util.c) 84set(TEST_HELPER_SRC test.c test_util.c)
85if(WIN32)
86 set(TEST_HELPER_SRC ${TEST_HELPER_SRC} compat/mkstemp.c)
87endif()
85 88
86# aeadtest 89# aeadtest
87add_executable(aeadtest aeadtest.c) 90add_executable(aeadtest aeadtest.c)
@@ -555,12 +558,9 @@ prepare_emscripten_test_target(lhash_test)
555add_platform_test(lhash_test lhash_test) 558add_platform_test(lhash_test lhash_test)
556 559
557# md_test 560# md_test
558# XXX - ftruncate and mkstemp missing from Windows 561add_executable(md_test md_test.c ${TEST_HELPER_SRC})
559if(NOT WIN32) 562target_link_libraries(md_test ${OPENSSL_TEST_LIBS})
560 add_executable(md_test md_test.c ${TEST_HELPER_SRC}) 563add_platform_test(md_test md_test)
561 target_link_libraries(md_test ${OPENSSL_TEST_LIBS})
562 add_platform_test(md_test md_test)
563endif()
564 564
565# mlkem_tests 565# mlkem_tests
566add_executable(mlkem_tests mlkem_tests.c parse_test_file.c) 566add_executable(mlkem_tests mlkem_tests.c parse_test_file.c)
@@ -742,12 +742,9 @@ endif()
742set_tests_properties(servertest PROPERTIES ENVIRONMENT "srcdir=${TEST_SOURCE_DIR}") 742set_tests_properties(servertest PROPERTIES ENVIRONMENT "srcdir=${TEST_SOURCE_DIR}")
743 743
744# sha_test 744# sha_test
745# XXX - ftruncate and mkstemp missing from Windows 745add_executable(sha_test sha_test.c ${TEST_HELPER_SRC})
746if(NOT WIN32) 746target_link_libraries(sha_test ${OPENSSL_TEST_LIBS})
747 add_executable(sha_test sha_test.c ${TEST_HELPER_SRC}) 747add_platform_test(sha_test sha_test)
748 target_link_libraries(sha_test ${OPENSSL_TEST_LIBS})
749 add_platform_test(sha_test sha_test)
750endif()
751 748
752# shutdowntest 749# shutdowntest
753set(SHUTDOWNTEST_SRC shutdowntest.c) 750set(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 @@
1/*
2musl as a whole is licensed under the following standard MIT license:
3
4----------------------------------------------------------------------
5Copyright © 2005-2020 Rich Felker, et al.
6
7Permission is hereby granted, free of charge, to any person obtaining
8a copy of this software and associated documentation files (the
9"Software"), to deal in the Software without restriction, including
10without limitation the rights to use, copy, modify, merge, publish,
11distribute, sublicense, and/or sell copies of the Software, and to
12permit persons to whom the Software is furnished to do so, subject to
13the following conditions:
14
15The above copyright notice and this permission notice shall be
16included in all copies or substantial portions of the Software.
17
18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25----------------------------------------------------------------------
26*/
27
28#include <string.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <fcntl.h>
32#include <unistd.h>
33#include <limits.h>
34#include <errno.h>
35
36#include <io.h>
37
38int mkstemp(char *template)
39{
40 int fd;
41retry:
42 if (!_mktemp(template)) return -1;
43 fd = open(template, O_RDWR | O_CREAT | O_EXCL, 0600);
44 if (fd >= 0) return fd;
45 if (errno == EEXIST) {
46 /* this is safe because mktemp verified
47 * that we have a valid template string */
48 strcpy(template+strlen(template)-6, "XXXXXX");
49 goto retry;
50 }
51 return -1;
52}