Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests/tr1: Fix sporadic failures caused by tmpnam/_tempnam #2210

Merged
merged 9 commits into from
Sep 25, 2021
21 changes: 21 additions & 0 deletions tests/tr1/include/temp_file_name.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) Microsoft Corporation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#pragma once

#include <random>
#include <string>

[[nodiscard]] inline std::string temp_file_name() {
std::string ret{"temp_file_"};
std::uniform_int_distribution<int> 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;
}
19 changes: 10 additions & 9 deletions tests/tr1/tests/cstdio/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#define TEST_NAMEX "<cstdio>"

#include "tdefs.h"
#include "temp_file_name.h"
#include <assert.h>
#include <cstdio>
#include <errno.h>
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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];
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved
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();
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved
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);
Expand Down
25 changes: 10 additions & 15 deletions tests/tr1/tests/cwchar1/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#define TEST_NAMEX "<cwchar>, part 1"

#include "tdefs.h"
#include "temp_file_name.h"
#include <assert.h>
#include <cwchar>
#include <stdarg.h>
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
11 changes: 3 additions & 8 deletions tests/tr1/tests/fstream1/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,14 @@
#define TEST_NAME "<fstream>, part 1"

#include "tdefs.h"
#include "temp_file_name.h"
#include <assert.h>
#include <fstream>
#include <string>

#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);
Expand Down
9 changes: 3 additions & 6 deletions tests/tr1/tests/fstream2/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,14 @@
#define TEST_NAME "<fstream>, part 2"

#include "tdefs.h"
#include "temp_file_name.h"
#include <assert.h>
#include <fstream>
#include <wchar.h>

#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();
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved

// test output file opening
STD wofstream ofs(tn);
Expand Down