Skip to content

Commit f189d08

Browse files
cjappldavidtrevelyan
authored andcommitted
Introduce test fixture for temporary files (#25)
* Introduce test fixture for temporary files * Style fixup for method name
1 parent 985d480 commit f189d08

File tree

1 file changed

+44
-50
lines changed

1 file changed

+44
-50
lines changed

compiler-rt/lib/radsan/tests/radsan_test_interceptors.cpp

Lines changed: 44 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#include <atomic>
2626
#include <chrono>
27+
#include <string>
2728
#include <thread>
2829

2930
#include <fcntl.h>
@@ -37,23 +38,27 @@ using namespace std::chrono_literals;
3738

3839
namespace {
3940
void *fake_thread_entry_point(void *) { return nullptr; }
40-
41-
/*
42-
The creat function doesn't seem to work on an ubuntu Docker image when the
43-
path is in a shared volume of the host. For now, to keep testing convenient
44-
with a local Docker container, we just put it somewhere that's not in the
45-
shared volume (/tmp). This is volatile and will be cleaned up as soon as the
46-
container is stopped.
47-
*/
48-
constexpr const char *temporary_file_path() {
49-
#if SANITIZER_LINUX
50-
return "/tmp/radsan_temporary_test_file.txt";
51-
#elif SANITIZER_APPLE
52-
return "./radsan_temporary_test_file.txt";
53-
#endif
54-
}
5541
} // namespace
5642

43+
class RadsanFileTest : public ::testing::Test {
44+
protected:
45+
void SetUp() override {
46+
const ::testing::TestInfo *const test_info =
47+
::testing::UnitTest::GetInstance()->current_test_info();
48+
file_path = std::string("/tmp/radsan_temporary_test_file_") +
49+
test_info->name() + ".txt";
50+
}
51+
52+
// Gets a file path with the test's name in in
53+
// This file will be removed if it exists at the end of the test
54+
const char *GetTemporaryFilePath() const { return file_path.c_str(); }
55+
56+
void TearDown() override { std::remove(GetTemporaryFilePath()); }
57+
58+
private:
59+
std::string file_path;
60+
};
61+
5762
/*
5863
Allocation and deallocation
5964
*/
@@ -165,41 +170,36 @@ TEST(TestRadsanInterceptors, nanosleepDiesWhenRealtime) {
165170
Filesystem
166171
*/
167172

168-
TEST(TestRadsanInterceptors, openDiesWhenRealtime) {
169-
auto func = []() { open(temporary_file_path(), O_RDONLY); };
173+
TEST_F(RadsanFileTest, openDiesWhenRealtime) {
174+
auto func = [this]() { open(GetTemporaryFilePath(), O_RDONLY); };
170175
expectRealtimeDeath(func, "open");
171176
expectNonrealtimeSurvival(func);
172-
std::remove(temporary_file_path());
173177
}
174178

175-
TEST(TestRadsanInterceptors, openatDiesWhenRealtime) {
176-
auto func = []() { openat(0, temporary_file_path(), O_RDONLY); };
179+
TEST_F(RadsanFileTest, openatDiesWhenRealtime) {
180+
auto func = [this]() { openat(0, GetTemporaryFilePath(), O_RDONLY); };
177181
expectRealtimeDeath(func, "openat");
178182
expectNonrealtimeSurvival(func);
179-
std::remove(temporary_file_path());
180183
}
181184

182-
TEST(TestRadsanInterceptors, openCreatesFileWithProperMode) {
185+
TEST_F(RadsanFileTest, openCreatesFileWithProperMode) {
183186
const int mode = S_IRGRP | S_IROTH | S_IRUSR | S_IWUSR;
184187

185-
const int fd = open(temporary_file_path(), O_CREAT | O_WRONLY, mode);
188+
const int fd = open(GetTemporaryFilePath(), O_CREAT | O_WRONLY, mode);
186189
ASSERT_THAT(fd, Ne(-1));
187190
close(fd);
188191

189192
struct stat st;
190-
ASSERT_THAT(stat(temporary_file_path(), &st), Eq(0));
193+
ASSERT_THAT(stat(GetTemporaryFilePath(), &st), Eq(0));
191194

192195
// Mask st_mode to get permission bits only
193196
ASSERT_THAT(st.st_mode & 0777, Eq(mode));
194-
195-
std::remove(temporary_file_path());
196197
}
197198

198-
TEST(TestRadsanInterceptors, creatDiesWhenRealtime) {
199-
auto func = []() { creat(temporary_file_path(), S_IWOTH | S_IROTH); };
199+
TEST_F(RadsanFileTest, creatDiesWhenRealtime) {
200+
auto func = [this]() { creat(GetTemporaryFilePath(), S_IWOTH | S_IROTH); };
200201
expectRealtimeDeath(func, "creat");
201202
expectNonrealtimeSurvival(func);
202-
std::remove(temporary_file_path());
203203
}
204204

205205
TEST(TestRadsanInterceptors, fcntlDiesWhenRealtime) {
@@ -208,8 +208,8 @@ TEST(TestRadsanInterceptors, fcntlDiesWhenRealtime) {
208208
expectNonrealtimeSurvival(func);
209209
}
210210

211-
TEST(TestRadsanInterceptors, fcntlFlockDiesWhenRealtime) {
212-
int fd = creat(temporary_file_path(), S_IRUSR | S_IWUSR);
211+
TEST_F(RadsanFileTest, fcntlFlockDiesWhenRealtime) {
212+
int fd = creat(GetTemporaryFilePath(), S_IRUSR | S_IWUSR);
213213
ASSERT_THAT(fd, Ne(-1));
214214

215215
auto func = [fd]() {
@@ -227,11 +227,10 @@ TEST(TestRadsanInterceptors, fcntlFlockDiesWhenRealtime) {
227227
expectNonrealtimeSurvival(func);
228228

229229
close(fd);
230-
std::remove(temporary_file_path());
231230
}
232231

233-
TEST(TestRadsanInterceptors, fcntlSetFdDiesWhenRealtime) {
234-
int fd = creat(temporary_file_path(), S_IRUSR | S_IWUSR);
232+
TEST_F(RadsanFileTest, fcntlSetFdDiesWhenRealtime) {
233+
int fd = creat(GetTemporaryFilePath(), S_IRUSR | S_IWUSR);
235234
ASSERT_THAT(fd, Ne(-1));
236235

237236
auto func = [fd]() {
@@ -258,18 +257,17 @@ TEST(TestRadsanInterceptors, closeDiesWhenRealtime) {
258257
expectNonrealtimeSurvival(func);
259258
}
260259

261-
TEST(TestRadsanInterceptors, fopenDiesWhenRealtime) {
262-
auto func = []() {
263-
auto fd = fopen(temporary_file_path(), "w");
260+
TEST_F(RadsanFileTest, fopenDiesWhenRealtime) {
261+
auto func = [this]() {
262+
auto fd = fopen(GetTemporaryFilePath(), "w");
264263
EXPECT_THAT(fd, Ne(nullptr));
265264
};
266265
expectRealtimeDeath(func, "fopen");
267266
expectNonrealtimeSurvival(func);
268-
std::remove(temporary_file_path());
269267
}
270268

271-
TEST(TestRadsanInterceptors, freadDiesWhenRealtime) {
272-
auto fd = fopen(temporary_file_path(), "w");
269+
TEST_F(RadsanFileTest, freadDiesWhenRealtime) {
270+
auto fd = fopen(GetTemporaryFilePath(), "w");
273271
auto func = [fd]() {
274272
char c{};
275273
fread(&c, 1, 1, fd);
@@ -278,26 +276,23 @@ TEST(TestRadsanInterceptors, freadDiesWhenRealtime) {
278276
expectNonrealtimeSurvival(func);
279277
if (fd != nullptr)
280278
fclose(fd);
281-
std::remove(temporary_file_path());
282279
}
283280

284-
TEST(TestRadsanInterceptors, fwriteDiesWhenRealtime) {
285-
auto fd = fopen(temporary_file_path(), "w");
281+
TEST_F(RadsanFileTest, fwriteDiesWhenRealtime) {
282+
auto fd = fopen(GetTemporaryFilePath(), "w");
286283
ASSERT_NE(nullptr, fd);
287284
auto message = "Hello, world!";
288285
auto func = [&]() { fwrite(&message, 1, 4, fd); };
289286
expectRealtimeDeath(func, "fwrite");
290287
expectNonrealtimeSurvival(func);
291-
std::remove(temporary_file_path());
292288
}
293289

294-
TEST(TestRadsanInterceptors, fcloseDiesWhenRealtime) {
295-
auto fd = fopen(temporary_file_path(), "w");
290+
TEST_F(RadsanFileTest, fcloseDiesWhenRealtime) {
291+
auto fd = fopen(GetTemporaryFilePath(), "w");
296292
EXPECT_THAT(fd, Ne(nullptr));
297293
auto func = [fd]() { fclose(fd); };
298294
expectRealtimeDeath(func, "fclose");
299295
expectNonrealtimeSurvival(func);
300-
std::remove(temporary_file_path());
301296
}
302297

303298
TEST(TestRadsanInterceptors, putsDiesWhenRealtime) {
@@ -306,15 +301,14 @@ TEST(TestRadsanInterceptors, putsDiesWhenRealtime) {
306301
expectNonrealtimeSurvival(func);
307302
}
308303

309-
TEST(TestRadsanInterceptors, fputsDiesWhenRealtime) {
310-
auto fd = fopen(temporary_file_path(), "w");
304+
TEST_F(RadsanFileTest, fputsDiesWhenRealtime) {
305+
auto fd = fopen(GetTemporaryFilePath(), "w");
311306
ASSERT_THAT(fd, Ne(nullptr)) << errno;
312307
auto func = [fd]() { fputs("Hello, world!\n", fd); };
313308
expectRealtimeDeath(func);
314309
expectNonrealtimeSurvival(func);
315310
if (fd != nullptr)
316311
fclose(fd);
317-
std::remove(temporary_file_path());
318312
}
319313

320314
/*

0 commit comments

Comments
 (0)