From a44991a9e925ba99f1338f0455f717991d0c166f Mon Sep 17 00:00:00 2001 From: Chris Apple <14171107+cjappl@users.noreply.github.com> Date: Fri, 31 May 2024 19:49:41 -0700 Subject: [PATCH 1/2] Introduce test fixture for temporary files --- .../radsan/tests/radsan_test_interceptors.cpp | 76 +++++++++---------- 1 file changed, 35 insertions(+), 41 deletions(-) diff --git a/compiler-rt/lib/radsan/tests/radsan_test_interceptors.cpp b/compiler-rt/lib/radsan/tests/radsan_test_interceptors.cpp index b62dd7af57548..39cf53ac8e1d0 100644 --- a/compiler-rt/lib/radsan/tests/radsan_test_interceptors.cpp +++ b/compiler-rt/lib/radsan/tests/radsan_test_interceptors.cpp @@ -24,6 +24,7 @@ #include #include +#include #include #include @@ -37,23 +38,27 @@ using namespace std::chrono_literals; namespace { void *fake_thread_entry_point(void *) { return nullptr; } - -/* - The creat function doesn't seem to work on an ubuntu Docker image when the - path is in a shared volume of the host. For now, to keep testing convenient - with a local Docker container, we just put it somewhere that's not in the - shared volume (/tmp). This is volatile and will be cleaned up as soon as the - container is stopped. -*/ -constexpr const char *temporary_file_path() { -#if SANITIZER_LINUX - return "/tmp/radsan_temporary_test_file.txt"; -#elif SANITIZER_APPLE - return "./radsan_temporary_test_file.txt"; -#endif -} } // namespace +class RadsanFileTest : public ::testing::Test { +protected: + void SetUp() override { + const ::testing::TestInfo *const test_info = + ::testing::UnitTest::GetInstance()->current_test_info(); + file_path = std::string("/tmp/radsan_temporary_test_file_") + + test_info->name() + ".txt"; + } + + // Gets a file path with the test's name in in + // This file will be removed if it exists at the end of the test + const char *temporary_file_path() const { return file_path.c_str(); } + + void TearDown() override { std::remove(temporary_file_path()); } + +private: + std::string file_path; +}; + /* Allocation and deallocation */ @@ -165,21 +170,19 @@ TEST(TestRadsanInterceptors, nanosleepDiesWhenRealtime) { Filesystem */ -TEST(TestRadsanInterceptors, openDiesWhenRealtime) { - auto func = []() { open(temporary_file_path(), O_RDONLY); }; +TEST_F(RadsanFileTest, openDiesWhenRealtime) { + auto func = [this]() { open(temporary_file_path(), O_RDONLY); }; expectRealtimeDeath(func, "open"); expectNonrealtimeSurvival(func); - std::remove(temporary_file_path()); } -TEST(TestRadsanInterceptors, openatDiesWhenRealtime) { - auto func = []() { openat(0, temporary_file_path(), O_RDONLY); }; +TEST_F(RadsanFileTest, openatDiesWhenRealtime) { + auto func = [this]() { openat(0, temporary_file_path(), O_RDONLY); }; expectRealtimeDeath(func, "openat"); expectNonrealtimeSurvival(func); - std::remove(temporary_file_path()); } -TEST(TestRadsanInterceptors, openCreatesFileWithProperMode) { +TEST_F(RadsanFileTest, openCreatesFileWithProperMode) { const int mode = S_IRGRP | S_IROTH | S_IRUSR | S_IWUSR; const int fd = open(temporary_file_path(), O_CREAT | O_WRONLY, mode); @@ -191,15 +194,12 @@ TEST(TestRadsanInterceptors, openCreatesFileWithProperMode) { // Mask st_mode to get permission bits only ASSERT_THAT(st.st_mode & 0777, Eq(mode)); - - std::remove(temporary_file_path()); } -TEST(TestRadsanInterceptors, creatDiesWhenRealtime) { - auto func = []() { creat(temporary_file_path(), S_IWOTH | S_IROTH); }; +TEST_F(RadsanFileTest, creatDiesWhenRealtime) { + auto func = [this]() { creat(temporary_file_path(), S_IWOTH | S_IROTH); }; expectRealtimeDeath(func, "creat"); expectNonrealtimeSurvival(func); - std::remove(temporary_file_path()); } TEST(TestRadsanInterceptors, fcntlDiesWhenRealtime) { @@ -208,7 +208,7 @@ TEST(TestRadsanInterceptors, fcntlDiesWhenRealtime) { expectNonrealtimeSurvival(func); } -TEST(TestRadsanInterceptors, fcntlFlockDiesWhenRealtime) { +TEST_F(RadsanFileTest, fcntlFlockDiesWhenRealtime) { int fd = creat(temporary_file_path(), S_IRUSR | S_IWUSR); ASSERT_THAT(fd, Ne(-1)); @@ -227,10 +227,9 @@ TEST(TestRadsanInterceptors, fcntlFlockDiesWhenRealtime) { expectNonrealtimeSurvival(func); close(fd); - std::remove(temporary_file_path()); } -TEST(TestRadsanInterceptors, fcntlSetFdDiesWhenRealtime) { +TEST_F(RadsanFileTest, fcntlSetFdDiesWhenRealtime) { int fd = creat(temporary_file_path(), S_IRUSR | S_IWUSR); ASSERT_THAT(fd, Ne(-1)); @@ -258,17 +257,16 @@ TEST(TestRadsanInterceptors, closeDiesWhenRealtime) { expectNonrealtimeSurvival(func); } -TEST(TestRadsanInterceptors, fopenDiesWhenRealtime) { - auto func = []() { +TEST_F(RadsanFileTest, fopenDiesWhenRealtime) { + auto func = [this]() { auto fd = fopen(temporary_file_path(), "w"); EXPECT_THAT(fd, Ne(nullptr)); }; expectRealtimeDeath(func, "fopen"); expectNonrealtimeSurvival(func); - std::remove(temporary_file_path()); } -TEST(TestRadsanInterceptors, freadDiesWhenRealtime) { +TEST_F(RadsanFileTest, freadDiesWhenRealtime) { auto fd = fopen(temporary_file_path(), "w"); auto func = [fd]() { char c{}; @@ -278,26 +276,23 @@ TEST(TestRadsanInterceptors, freadDiesWhenRealtime) { expectNonrealtimeSurvival(func); if (fd != nullptr) fclose(fd); - std::remove(temporary_file_path()); } -TEST(TestRadsanInterceptors, fwriteDiesWhenRealtime) { +TEST_F(RadsanFileTest, fwriteDiesWhenRealtime) { auto fd = fopen(temporary_file_path(), "w"); ASSERT_NE(nullptr, fd); auto message = "Hello, world!"; auto func = [&]() { fwrite(&message, 1, 4, fd); }; expectRealtimeDeath(func, "fwrite"); expectNonrealtimeSurvival(func); - std::remove(temporary_file_path()); } -TEST(TestRadsanInterceptors, fcloseDiesWhenRealtime) { +TEST_F(RadsanFileTest, fcloseDiesWhenRealtime) { auto fd = fopen(temporary_file_path(), "w"); EXPECT_THAT(fd, Ne(nullptr)); auto func = [fd]() { fclose(fd); }; expectRealtimeDeath(func, "fclose"); expectNonrealtimeSurvival(func); - std::remove(temporary_file_path()); } TEST(TestRadsanInterceptors, putsDiesWhenRealtime) { @@ -306,7 +301,7 @@ TEST(TestRadsanInterceptors, putsDiesWhenRealtime) { expectNonrealtimeSurvival(func); } -TEST(TestRadsanInterceptors, fputsDiesWhenRealtime) { +TEST_F(RadsanFileTest, fputsDiesWhenRealtime) { auto fd = fopen(temporary_file_path(), "w"); ASSERT_THAT(fd, Ne(nullptr)) << errno; auto func = [fd]() { fputs("Hello, world!\n", fd); }; @@ -314,7 +309,6 @@ TEST(TestRadsanInterceptors, fputsDiesWhenRealtime) { expectNonrealtimeSurvival(func); if (fd != nullptr) fclose(fd); - std::remove(temporary_file_path()); } /* From a86afe124efa26f8f4020b08ab6bb10772e82c92 Mon Sep 17 00:00:00 2001 From: Chris Apple <14171107+cjappl@users.noreply.github.com> Date: Fri, 31 May 2024 22:26:37 -0700 Subject: [PATCH 2/2] Style fixup for method name --- .../radsan/tests/radsan_test_interceptors.cpp | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/compiler-rt/lib/radsan/tests/radsan_test_interceptors.cpp b/compiler-rt/lib/radsan/tests/radsan_test_interceptors.cpp index 39cf53ac8e1d0..20de4e73a78c6 100644 --- a/compiler-rt/lib/radsan/tests/radsan_test_interceptors.cpp +++ b/compiler-rt/lib/radsan/tests/radsan_test_interceptors.cpp @@ -51,9 +51,9 @@ class RadsanFileTest : public ::testing::Test { // Gets a file path with the test's name in in // This file will be removed if it exists at the end of the test - const char *temporary_file_path() const { return file_path.c_str(); } + const char *GetTemporaryFilePath() const { return file_path.c_str(); } - void TearDown() override { std::remove(temporary_file_path()); } + void TearDown() override { std::remove(GetTemporaryFilePath()); } private: std::string file_path; @@ -171,13 +171,13 @@ TEST(TestRadsanInterceptors, nanosleepDiesWhenRealtime) { */ TEST_F(RadsanFileTest, openDiesWhenRealtime) { - auto func = [this]() { open(temporary_file_path(), O_RDONLY); }; + auto func = [this]() { open(GetTemporaryFilePath(), O_RDONLY); }; expectRealtimeDeath(func, "open"); expectNonrealtimeSurvival(func); } TEST_F(RadsanFileTest, openatDiesWhenRealtime) { - auto func = [this]() { openat(0, temporary_file_path(), O_RDONLY); }; + auto func = [this]() { openat(0, GetTemporaryFilePath(), O_RDONLY); }; expectRealtimeDeath(func, "openat"); expectNonrealtimeSurvival(func); } @@ -185,19 +185,19 @@ TEST_F(RadsanFileTest, openatDiesWhenRealtime) { TEST_F(RadsanFileTest, openCreatesFileWithProperMode) { const int mode = S_IRGRP | S_IROTH | S_IRUSR | S_IWUSR; - const int fd = open(temporary_file_path(), O_CREAT | O_WRONLY, mode); + const int fd = open(GetTemporaryFilePath(), O_CREAT | O_WRONLY, mode); ASSERT_THAT(fd, Ne(-1)); close(fd); struct stat st; - ASSERT_THAT(stat(temporary_file_path(), &st), Eq(0)); + ASSERT_THAT(stat(GetTemporaryFilePath(), &st), Eq(0)); // Mask st_mode to get permission bits only ASSERT_THAT(st.st_mode & 0777, Eq(mode)); } TEST_F(RadsanFileTest, creatDiesWhenRealtime) { - auto func = [this]() { creat(temporary_file_path(), S_IWOTH | S_IROTH); }; + auto func = [this]() { creat(GetTemporaryFilePath(), S_IWOTH | S_IROTH); }; expectRealtimeDeath(func, "creat"); expectNonrealtimeSurvival(func); } @@ -209,7 +209,7 @@ TEST(TestRadsanInterceptors, fcntlDiesWhenRealtime) { } TEST_F(RadsanFileTest, fcntlFlockDiesWhenRealtime) { - int fd = creat(temporary_file_path(), S_IRUSR | S_IWUSR); + int fd = creat(GetTemporaryFilePath(), S_IRUSR | S_IWUSR); ASSERT_THAT(fd, Ne(-1)); auto func = [fd]() { @@ -230,7 +230,7 @@ TEST_F(RadsanFileTest, fcntlFlockDiesWhenRealtime) { } TEST_F(RadsanFileTest, fcntlSetFdDiesWhenRealtime) { - int fd = creat(temporary_file_path(), S_IRUSR | S_IWUSR); + int fd = creat(GetTemporaryFilePath(), S_IRUSR | S_IWUSR); ASSERT_THAT(fd, Ne(-1)); auto func = [fd]() { @@ -259,7 +259,7 @@ TEST(TestRadsanInterceptors, closeDiesWhenRealtime) { TEST_F(RadsanFileTest, fopenDiesWhenRealtime) { auto func = [this]() { - auto fd = fopen(temporary_file_path(), "w"); + auto fd = fopen(GetTemporaryFilePath(), "w"); EXPECT_THAT(fd, Ne(nullptr)); }; expectRealtimeDeath(func, "fopen"); @@ -267,7 +267,7 @@ TEST_F(RadsanFileTest, fopenDiesWhenRealtime) { } TEST_F(RadsanFileTest, freadDiesWhenRealtime) { - auto fd = fopen(temporary_file_path(), "w"); + auto fd = fopen(GetTemporaryFilePath(), "w"); auto func = [fd]() { char c{}; fread(&c, 1, 1, fd); @@ -279,7 +279,7 @@ TEST_F(RadsanFileTest, freadDiesWhenRealtime) { } TEST_F(RadsanFileTest, fwriteDiesWhenRealtime) { - auto fd = fopen(temporary_file_path(), "w"); + auto fd = fopen(GetTemporaryFilePath(), "w"); ASSERT_NE(nullptr, fd); auto message = "Hello, world!"; auto func = [&]() { fwrite(&message, 1, 4, fd); }; @@ -288,7 +288,7 @@ TEST_F(RadsanFileTest, fwriteDiesWhenRealtime) { } TEST_F(RadsanFileTest, fcloseDiesWhenRealtime) { - auto fd = fopen(temporary_file_path(), "w"); + auto fd = fopen(GetTemporaryFilePath(), "w"); EXPECT_THAT(fd, Ne(nullptr)); auto func = [fd]() { fclose(fd); }; expectRealtimeDeath(func, "fclose"); @@ -302,7 +302,7 @@ TEST(TestRadsanInterceptors, putsDiesWhenRealtime) { } TEST_F(RadsanFileTest, fputsDiesWhenRealtime) { - auto fd = fopen(temporary_file_path(), "w"); + auto fd = fopen(GetTemporaryFilePath(), "w"); ASSERT_THAT(fd, Ne(nullptr)) << errno; auto func = [fd]() { fputs("Hello, world!\n", fd); }; expectRealtimeDeath(func);