diff --git a/tests/tr1/include/temp_file_name.h b/tests/tr1/include/temp_file_name.h new file mode 100644 index 00000000000..79991f56f20 --- /dev/null +++ b/tests/tr1/include/temp_file_name.h @@ -0,0 +1,21 @@ +// Copyright (c) Microsoft Corporation. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +#pragma once + +#include +#include + +[[nodiscard]] inline std::string temp_file_name() { + std::string ret{"temp_file_"}; + std::uniform_int_distribution dist{0, 15}; + std::random_device rd; + + for (int i = 0; i < 64; ++i) { // 64 hexits = 256 bits of entropy + ret.push_back("0123456789ABCDEF"[dist(rd)]); + } + + ret += ".tmp"; + + return ret; +} diff --git a/tests/tr1/tests/cstdio/test.cpp b/tests/tr1/tests/cstdio/test.cpp index 7a6b0585a7d..e8c490d88cc 100644 --- a/tests/tr1/tests/cstdio/test.cpp +++ b/tests/tr1/tests/cstdio/test.cpp @@ -5,6 +5,7 @@ #define TEST_NAMEX "" #include "tdefs.h" +#include "temp_file_name.h" #include #include #include @@ -91,7 +92,8 @@ void test_cpp() { // test C++ header int in1; long off; - assert((tn = STDx tmpnam((char*) nullptr)) != nullptr); + const auto temp_name = temp_file_name(); + tn = temp_name.c_str(); assert((pf = STDx fopen(tn, "w+")) != nullptr); STDx setbuf(pf, (char*) nullptr); @@ -124,18 +126,17 @@ void test_cpp() { // test C++ header } { // test character I/O - const char* tmpbuff; char tname[L_tmpnam]; - char tmpbuf[L_tmpnam]; + char tn[100]; STDx FILE* pf; - char tn[100] = {0}; - assert(STDx tmpnam(tmpbuf) == tmpbuf); - CHECK(CSTD strlen(tmpbuf) < L_tmpnam); - assert((tmpbuff = STDx tmpnam((char*) nullptr)) != nullptr); + const auto temp_name1 = temp_file_name(); + CHECK(temp_name1.size() < sizeof(tname)); + CSTD strcpy_s(tname, sizeof(tname), temp_name1.c_str()); - CSTD strcpy_s(tn, sizeof(tn), tmpbuff); - CSTD strcpy_s(tname, sizeof(tname), tmpbuf); + const auto temp_name2 = temp_file_name(); + CHECK(temp_name2.size() < sizeof(tn)); + CSTD strcpy_s(tn, sizeof(tn), temp_name2.c_str()); CHECK(CSTD strcmp(tn, tname) != 0); assert((pf = STDx fopen(tname, "w")) != nullptr); diff --git a/tests/tr1/tests/cwchar1/test.cpp b/tests/tr1/tests/cwchar1/test.cpp index 3d9b7b8ab15..d773b7b00a8 100644 --- a/tests/tr1/tests/cwchar1/test.cpp +++ b/tests/tr1/tests/cwchar1/test.cpp @@ -5,6 +5,7 @@ #define TEST_NAMEX ", part 1" #include "tdefs.h" +#include "temp_file_name.h" #include #include #include @@ -13,9 +14,6 @@ #pragma warning(disable : 4793) // function compiled as native -#define NO_TMPNAM_TESTS 1 -#undef tmpnam -#define tmpnam(x) _tempnam(".", "") #undef clearerr // tested in stdio2.c #undef feof @@ -101,7 +99,8 @@ void test_cpp() { // test C++ header int in1; long off; - assert((tn = CSTD tmpnam((char*) nullptr)) != nullptr); + const auto temp_name = temp_file_name(); + tn = temp_name.c_str(); assert((pf = CSTD fopen(tn, "w+")) != nullptr); CHECK_INT(STDx fwide(pf, 0), 0); CHECK_INT(STDx fwprintf(pf, L"123\n"), 4); @@ -141,19 +140,15 @@ void test_cpp() { // test C++ header CHECK(wmacs[1] < wmacs[0]); -#if NO_TMPNAM_TESTS - char *tname, *tn; - assert((tn = CSTD tmpnam((char*) nullptr)) != nullptr); - tname = (char*) CSTD malloc(CSTD strlen(tn) + 1); + char* tname; + const char* tn; + const auto temp_name1 = temp_file_name(); + tn = temp_name1.c_str(); + tname = (char*) CSTD malloc(CSTD strlen(tn) + 1); CSTD strcpy(tname, tn); -#else // NO_TMPNAM_TESTS - char tname[L_tmpnam], *tn; - CHECK_PTR(CSTD tmpnam(tname), tname); - assert(CSTD strlen(tname) < L_tmpnam); -#endif // NO_TMPNAM_TESTS - - assert((tn = CSTD tmpnam((char*) nullptr)) != nullptr); + const auto temp_name2 = temp_file_name(); + tn = temp_name2.c_str(); CHECK(CSTD strcmp(tn, tname) != 0); assert((pf = CSTD fopen(tname, "w")) != nullptr); CHECK_INT(STDx fgetwc(pf), wintval); diff --git a/tests/tr1/tests/fstream1/test.cpp b/tests/tr1/tests/fstream1/test.cpp index 2a91e822c18..af558e99fd6 100644 --- a/tests/tr1/tests/fstream1/test.cpp +++ b/tests/tr1/tests/fstream1/test.cpp @@ -5,19 +5,14 @@ #define TEST_NAME ", part 1" #include "tdefs.h" +#include "temp_file_name.h" #include #include #include -#undef tmpnam -#define tmpnam(x) _tempnam(".", "") - void test_main() { // test basic workings of char fstream definitions - const char* tn = CSTD tmpnam(0); - - assert(tn != nullptr); - - STD string tn_str(tn); + STD string tn_str = temp_file_name(); + const char* tn = tn_str.c_str(); // test output file opening STD ofstream ofs(tn); diff --git a/tests/tr1/tests/fstream2/test.cpp b/tests/tr1/tests/fstream2/test.cpp index bb45daaef33..b0401459493 100644 --- a/tests/tr1/tests/fstream2/test.cpp +++ b/tests/tr1/tests/fstream2/test.cpp @@ -5,17 +5,14 @@ #define TEST_NAME ", part 2" #include "tdefs.h" +#include "temp_file_name.h" #include #include #include -#undef tmpnam -#define tmpnam(x) _tempnam(".", "") - void test_main() { // test basic workings of wide fstream definitions - const char* tn = CSTD tmpnam(0); - - assert(tn != nullptr); + const auto temp_name = temp_file_name(); + const char* tn = temp_name.c_str(); // test output file opening STD wofstream ofs(tn);