Skip to content

Commit 0ae584d

Browse files
committed
Introduce test fixture for temporary files (#25)
* Introduce test fixture for temporary files * Style fixup for method name
1 parent 7e430f8 commit 0ae584d

File tree

1 file changed

+76
-84
lines changed

1 file changed

+76
-84
lines changed

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

Lines changed: 76 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
#include <atomic>
2929
#include <chrono>
30+
#include <string>
3031
#include <thread>
3132

3233
#include <fcntl.h>
@@ -38,24 +39,26 @@ using namespace testing;
3839
using namespace radsan_testing;
3940
using namespace std::chrono_literals;
4041

41-
namespace {
4242
void *FakeThreadEntryPoint(void *) { return nullptr; }
4343

44-
/*
45-
The creat function doesn't seem to work on an ubuntu Docker image when the
46-
path is in a shared volume of the host. For now, to keep testing convenient
47-
with a local Docker container, we just put it somewhere that's not in the
48-
shared volume (/tmp). This is volatile and will be cleaned up as soon as the
49-
container is stopped.
50-
*/
51-
constexpr const char *TemporaryFilePath() {
52-
#if SANITIZER_LINUX
53-
return "/tmp/radsan_temporary_test_file.txt";
54-
#elif SANITIZER_APPLE
55-
return "./radsan_temporary_test_file.txt";
56-
#endif
57-
}
58-
} // namespace
44+
class RadsanFileTest : public ::testing::Test {
45+
protected:
46+
void SetUp() override {
47+
const ::testing::TestInfo *const test_info =
48+
::testing::UnitTest::GetInstance()->current_test_info();
49+
file_path = std::string("/tmp/radsan_temporary_test_file_") +
50+
test_info->name() + ".txt";
51+
}
52+
53+
// Gets a file path with the test's name in in
54+
// This file will be removed if it exists at the end of the test
55+
const char *GetTemporaryFilePath() const { return file_path.c_str(); }
56+
57+
void TearDown() override { std::remove(GetTemporaryFilePath()); }
58+
59+
private:
60+
std::string file_path;
61+
};
5962

6063
/*
6164
Allocation and deallocation
@@ -168,51 +171,46 @@ TEST(TestRadsanInterceptors, NanosleepDiesWhenRealtime) {
168171
Filesystem
169172
*/
170173

171-
TEST(TestRadsanInterceptors, OpenDiesWhenRealtime) {
172-
auto Func = []() { open(TemporaryFilePath(), O_RDONLY); };
173-
ExpectRealtimeDeath(Func, "open");
174-
ExpectNonRealtimeSurvival(Func);
175-
std::remove(TemporaryFilePath());
174+
TEST_F(RadsanFileTest, OpenDiesWhenRealtime) {
175+
auto func = [this]() { open(GetTemporaryFilePath(), O_RDONLY); };
176+
ExpectRealtimeDeath(func, "open");
177+
ExpectNonRealtimeSurvival(func);
176178
}
177179

178-
TEST(TestRadsanInterceptors, OpenatDiesWhenRealtime) {
179-
auto Func = []() { openat(0, TemporaryFilePath(), O_RDONLY); };
180-
ExpectRealtimeDeath(Func, "openat");
181-
ExpectNonRealtimeSurvival(Func);
182-
std::remove(TemporaryFilePath());
180+
TEST_F(RadsanFileTest, OpenatDiesWhenRealtime) {
181+
auto func = [this]() { openat(0, GetTemporaryFilePath(), O_RDONLY); };
182+
ExpectRealtimeDeath(func, "openat");
183+
ExpectNonRealtimeSurvival(func);
183184
}
184185

185-
TEST(TestRadsanInterceptors, openCreatesFileWithProperMode) {
186+
TEST_F(RadsanFileTest, OpenCreatesFileWithProperMode) {
186187
const int mode = S_IRGRP | S_IROTH | S_IRUSR | S_IWUSR;
187188

188-
const int fd = open(TemporaryFilePath(), O_CREAT | O_WRONLY, mode);
189+
const int fd = open(GetTemporaryFilePath(), O_CREAT | O_WRONLY, mode);
189190
ASSERT_THAT(fd, Ne(-1));
190191
close(fd);
191192

192193
struct stat st;
193-
ASSERT_THAT(stat(TemporaryFilePath(), &st), Eq(0));
194+
ASSERT_THAT(stat(GetTemporaryFilePath(), &st), Eq(0));
194195

195196
// Mask st_mode to get permission bits only
196197
ASSERT_THAT(st.st_mode & 0777, Eq(mode));
197-
198-
std::remove(TemporaryFilePath());
199198
}
200199

201-
TEST(TestRadsanInterceptors, CreatDiesWhenRealtime) {
202-
auto Func = []() { creat(TemporaryFilePath(), S_IWOTH | S_IROTH); };
203-
ExpectRealtimeDeath(Func, "creat");
204-
ExpectNonRealtimeSurvival(Func);
205-
std::remove(TemporaryFilePath());
200+
TEST_F(RadsanFileTest, CreatDiesWhenRealtime) {
201+
auto func = [this]() { creat(GetTemporaryFilePath(), S_IWOTH | S_IROTH); };
202+
ExpectRealtimeDeath(func, "creat");
203+
ExpectNonRealtimeSurvival(func);
206204
}
207205

208206
TEST(TestRadsanInterceptors, FcntlDiesWhenRealtime) {
209-
auto Func = []() { fcntl(0, F_GETFL); };
210-
ExpectRealtimeDeath(Func, "fcntl");
211-
ExpectNonRealtimeSurvival(Func);
207+
auto func = []() { fcntl(0, F_GETFL); };
208+
ExpectRealtimeDeath(func, "fcntl");
209+
ExpectNonRealtimeSurvival(func);
212210
}
213211

214-
TEST(TestRadsanInterceptors, FcntlFlockDiesWhenRealtime) {
215-
int fd = creat(TemporaryFilePath(), S_IRUSR | S_IWUSR);
212+
TEST_F(RadsanFileTest, FcntlFlockDiesWhenRealtime) {
213+
int fd = creat(GetTemporaryFilePath(), S_IRUSR | S_IWUSR);
216214
ASSERT_THAT(fd, Ne(-1));
217215

218216
auto func = [fd]() {
@@ -230,17 +228,10 @@ TEST(TestRadsanInterceptors, FcntlFlockDiesWhenRealtime) {
230228
ExpectNonRealtimeSurvival(func);
231229

232230
close(fd);
233-
std::remove(TemporaryFilePath());
234-
}
235-
236-
TEST(TestRadsanInterceptors, CloseDiesWhenRealtime) {
237-
auto Func = []() { close(0); };
238-
ExpectRealtimeDeath(Func, "close");
239-
ExpectNonRealtimeSurvival(Func);
240231
}
241232

242-
TEST(TestRadsanInterceptors, fcntlSetFdDiesWhenRealtime) {
243-
int fd = creat(TemporaryFilePath(), S_IRUSR | S_IWUSR);
233+
TEST_F(RadsanFileTest, FcntlSetFdDiesWhenRealtime) {
234+
int fd = creat(GetTemporaryFilePath(), S_IRUSR | S_IWUSR);
244235
ASSERT_THAT(fd, Ne(-1));
245236

246237
auto func = [fd]() {
@@ -261,63 +252,64 @@ TEST(TestRadsanInterceptors, fcntlSetFdDiesWhenRealtime) {
261252
close(fd);
262253
}
263254

264-
TEST(TestRadsanInterceptors, FopenDiesWhenRealtime) {
265-
auto Func = []() {
266-
FILE *fd = fopen(TemporaryFilePath(), "w");
255+
TEST(TestRadsanInterceptors, CloseDiesWhenRealtime) {
256+
auto func = []() { close(0); };
257+
ExpectRealtimeDeath(func, "close");
258+
ExpectNonRealtimeSurvival(func);
259+
}
260+
261+
TEST_F(RadsanFileTest, FopenDiesWhenRealtime) {
262+
auto func = [this]() {
263+
auto fd = fopen(GetTemporaryFilePath(), "w");
267264
EXPECT_THAT(fd, Ne(nullptr));
268265
};
269-
ExpectRealtimeDeath(Func, "fopen");
270-
ExpectNonRealtimeSurvival(Func);
271-
std::remove(TemporaryFilePath());
266+
ExpectRealtimeDeath(func, "fopen");
267+
ExpectNonRealtimeSurvival(func);
272268
}
273269

274-
TEST(TestRadsanInterceptors, FreadDiesWhenRealtime) {
275-
FILE *fd = fopen(TemporaryFilePath(), "w");
276-
auto Func = [fd]() {
270+
TEST_F(RadsanFileTest, FreadDiesWhenRealtime) {
271+
auto fd = fopen(GetTemporaryFilePath(), "w");
272+
auto func = [fd]() {
277273
char c{};
278274
fread(&c, 1, 1, fd);
279275
};
280-
ExpectRealtimeDeath(Func, "fread");
281-
ExpectNonRealtimeSurvival(Func);
276+
ExpectRealtimeDeath(func, "fread");
277+
ExpectNonRealtimeSurvival(func);
282278
if (fd != nullptr)
283279
fclose(fd);
284-
std::remove(TemporaryFilePath());
285280
}
286281

287-
TEST(TestRadsanInterceptors, FwriteDiesWhenRealtime) {
288-
FILE *fd = fopen(TemporaryFilePath(), "w");
282+
TEST_F(RadsanFileTest, FwriteDiesWhenRealtime) {
283+
auto fd = fopen(GetTemporaryFilePath(), "w");
289284
ASSERT_NE(nullptr, fd);
290-
const char *message = "Hello, world!";
291-
auto Func = [&]() { fwrite(&message, 1, 4, fd); };
292-
ExpectRealtimeDeath(Func, "fwrite");
293-
ExpectNonRealtimeSurvival(Func);
294-
std::remove(TemporaryFilePath());
285+
auto message = "Hello, world!";
286+
auto func = [&]() { fwrite(&message, 1, 4, fd); };
287+
ExpectRealtimeDeath(func, "fwrite");
288+
ExpectNonRealtimeSurvival(func);
295289
}
296290

297-
TEST(TestRadsanInterceptors, FcloseDiesWhenRealtime) {
298-
FILE *fd = fopen(TemporaryFilePath(), "w");
291+
TEST_F(RadsanFileTest, FcloseDiesWhenRealtime) {
292+
auto fd = fopen(GetTemporaryFilePath(), "w");
299293
EXPECT_THAT(fd, Ne(nullptr));
300-
auto Func = [fd]() { fclose(fd); };
301-
ExpectRealtimeDeath(Func, "fclose");
302-
ExpectNonRealtimeSurvival(Func);
303-
std::remove(TemporaryFilePath());
294+
auto func = [fd]() { fclose(fd); };
295+
ExpectRealtimeDeath(func, "fclose");
296+
ExpectNonRealtimeSurvival(func);
304297
}
305298

306299
TEST(TestRadsanInterceptors, PutsDiesWhenRealtime) {
307-
auto Func = []() { puts("Hello, world!\n"); };
308-
ExpectRealtimeDeath(Func);
309-
ExpectNonRealtimeSurvival(Func);
300+
auto func = []() { puts("Hello, world!\n"); };
301+
ExpectRealtimeDeath(func);
302+
ExpectNonRealtimeSurvival(func);
310303
}
311304

312-
TEST(TestRadsanInterceptors, FputsDiesWhenRealtime) {
313-
FILE *fd = fopen(TemporaryFilePath(), "w");
305+
TEST_F(RadsanFileTest, FputsDiesWhenRealtime) {
306+
auto fd = fopen(GetTemporaryFilePath(), "w");
314307
ASSERT_THAT(fd, Ne(nullptr)) << errno;
315-
auto Func = [fd]() { fputs("Hello, world!\n", fd); };
316-
ExpectRealtimeDeath(Func);
317-
ExpectNonRealtimeSurvival(Func);
308+
auto func = [fd]() { fputs("Hello, world!\n", fd); };
309+
ExpectRealtimeDeath(func);
310+
ExpectNonRealtimeSurvival(func);
318311
if (fd != nullptr)
319312
fclose(fd);
320-
std::remove(TemporaryFilePath());
321313
}
322314

323315
/*

0 commit comments

Comments
 (0)