@@ -33,6 +33,21 @@ using namespace std::chrono_literals;
3333
3434namespace {
3535void *fake_thread_entry_point (void *) { return nullptr ; }
36+
37+ /*
38+ The creat function doesn't seem to work on an ubuntu Docker image when the
39+ path is in a shared volume of the host. For now, to keep testing convenient
40+ with a local Docker container, we just put it somewhere that's not in the
41+ shared volume (/tmp). This is volatile and will be cleaned up as soon as the
42+ container is stopped.
43+ */
44+ constexpr const char *temporary_file_path () {
45+ #if SANITIZER_LINUX
46+ return " /tmp/radsan_temporary_test_file.txt" ;
47+ #elif SANITIZER_APPLE
48+ return " ./radsan_temporary_test_file.txt" ;
49+ #endif
50+ }
3651} // namespace
3752
3853/*
@@ -131,21 +146,24 @@ TEST(TestRadsanInterceptors, nanosleepDiesWhenRealtime) {
131146*/
132147
133148TEST (TestRadsanInterceptors, openDiesWhenRealtime) {
134- auto func = []() { open (" ./file.txt " , O_RDONLY); };
149+ auto func = []() { open (temporary_file_path () , O_RDONLY); };
135150 expectRealtimeDeath (func, " open" );
136151 expectNonrealtimeSurvival (func);
152+ std::remove (temporary_file_path ());
137153}
138154
139155TEST (TestRadsanInterceptors, openatDiesWhenRealtime) {
140- auto func = []() { openat (0 , " ./file.txt " , O_RDONLY); };
156+ auto func = []() { openat (0 , temporary_file_path () , O_RDONLY); };
141157 expectRealtimeDeath (func, " openat" );
142158 expectNonrealtimeSurvival (func);
159+ std::remove (temporary_file_path ());
143160}
144161
145162TEST (TestRadsanInterceptors, creatDiesWhenRealtime) {
146- auto func = []() { creat (" ./file.txt " , O_TRUNC ); };
163+ auto func = []() { creat (temporary_file_path (), S_IWOTH | S_IROTH ); };
147164 expectRealtimeDeath (func, " creat" );
148165 expectNonrealtimeSurvival (func);
166+ std::remove (temporary_file_path ());
149167}
150168
151169TEST (TestRadsanInterceptors, fcntlDiesWhenRealtime) {
@@ -162,16 +180,16 @@ TEST(TestRadsanInterceptors, closeDiesWhenRealtime) {
162180
163181TEST (TestRadsanInterceptors, fopenDiesWhenRealtime) {
164182 auto func = []() {
165- auto fd = fopen (" ./file.txt" , " r" );
166- if (fd != nullptr )
167- fclose (fd);
183+ auto fd = fopen (temporary_file_path (), " w" );
184+ EXPECT_THAT (fd, Ne (nullptr ));
168185 };
169186 expectRealtimeDeath (func, " fopen" );
170187 expectNonrealtimeSurvival (func);
188+ std::remove (temporary_file_path ());
171189}
172190
173191TEST (TestRadsanInterceptors, freadDiesWhenRealtime) {
174- auto fd = fopen (" ./file.txt " , " r " );
192+ auto fd = fopen (temporary_file_path () , " w " );
175193 auto func = [fd]() {
176194 char c{};
177195 fread (&c, 1 , 1 , fd);
@@ -180,22 +198,26 @@ TEST(TestRadsanInterceptors, freadDiesWhenRealtime) {
180198 expectNonrealtimeSurvival (func);
181199 if (fd != nullptr )
182200 fclose (fd);
201+ std::remove (temporary_file_path ());
183202}
184203
185204TEST (TestRadsanInterceptors, fwriteDiesWhenRealtime) {
186- auto fd = fopen (" ./file.txt " , " w" );
205+ auto fd = fopen (temporary_file_path () , " w" );
187206 ASSERT_NE (nullptr , fd);
188207 auto message = " Hello, world!" ;
189208 auto func = [&]() { fwrite (&message, 1 , 4 , fd); };
190209 expectRealtimeDeath (func, " fwrite" );
191210 expectNonrealtimeSurvival (func);
211+ std::remove (temporary_file_path ());
192212}
193213
194214TEST (TestRadsanInterceptors, fcloseDiesWhenRealtime) {
195- auto fd = fopen (" ./file.txt" , " r" );
215+ auto fd = fopen (temporary_file_path (), " w" );
216+ EXPECT_THAT (fd, Ne (nullptr ));
196217 auto func = [fd]() { fclose (fd); };
197218 expectRealtimeDeath (func, " fclose" );
198219 expectNonrealtimeSurvival (func);
220+ std::remove (temporary_file_path ());
199221}
200222
201223TEST (TestRadsanInterceptors, putsDiesWhenRealtime) {
@@ -205,13 +227,14 @@ TEST(TestRadsanInterceptors, putsDiesWhenRealtime) {
205227}
206228
207229TEST (TestRadsanInterceptors, fputsDiesWhenRealtime) {
208- auto fd = fopen (" ./file.txt " , " w" );
230+ auto fd = fopen (temporary_file_path () , " w" );
209231 ASSERT_THAT (fd, Ne (nullptr )) << errno;
210232 auto func = [fd]() { fputs (" Hello, world!\n " , fd); };
211233 expectRealtimeDeath (func);
212234 expectNonrealtimeSurvival (func);
213235 if (fd != nullptr )
214236 fclose (fd);
237+ std::remove (temporary_file_path ());
215238}
216239
217240/*
0 commit comments