@@ -39,24 +39,26 @@ using namespace testing;
3939using  namespace  radsan_testing ; 
4040using  namespace  std ::chrono_literals; 
4141
42- namespace  {
4342void  *FakeThreadEntryPoint (void  *) { return  nullptr ; }
4443
45- /* 
46-   The creat function doesn't seem to work on an ubuntu Docker image when the 
47-   path is in a shared volume of the host. For now, to keep testing convenient 
48-   with a local Docker container, we just put it somewhere that's not in the 
49-   shared volume (/tmp). This is volatile and will be cleaned up as soon as the 
50-   container is stopped. 
51- */ 
52- constexpr  const  char  *TemporaryFilePath () {
53- #if  SANITIZER_LINUX
54-   return  " /tmp/radsan_temporary_test_file.txt" 
55- #elif  SANITIZER_APPLE
56-   return  " ./radsan_temporary_test_file.txt" 
57- #endif 
58- }
59- } //  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+ };
6062
6163class  RadsanFileTest  : public ::testing::Test {
6264protected: 
@@ -190,51 +192,46 @@ TEST(TestRadsanInterceptors, NanosleepDiesWhenRealtime) {
190192    Filesystem 
191193*/ 
192194
193- TEST (TestRadsanInterceptors, OpenDiesWhenRealtime) {
194-   auto  Func = []() { open (TemporaryFilePath (), O_RDONLY); };
195-   ExpectRealtimeDeath (Func, " open" 
196-   ExpectNonRealtimeSurvival (Func);
197-   std::remove (TemporaryFilePath ());
195+ TEST_F (RadsanFileTest, OpenDiesWhenRealtime) {
196+   auto  func = [this ]() { open (GetTemporaryFilePath (), O_RDONLY); };
197+   ExpectRealtimeDeath (func, " open" 
198+   ExpectNonRealtimeSurvival (func);
198199}
199200
200- TEST (TestRadsanInterceptors, OpenatDiesWhenRealtime) {
201-   auto  Func = []() { openat (0 , TemporaryFilePath (), O_RDONLY); };
202-   ExpectRealtimeDeath (Func, " openat" 
203-   ExpectNonRealtimeSurvival (Func);
204-   std::remove (TemporaryFilePath ());
201+ TEST_F (RadsanFileTest, OpenatDiesWhenRealtime) {
202+   auto  func = [this ]() { openat (0 , GetTemporaryFilePath (), O_RDONLY); };
203+   ExpectRealtimeDeath (func, " openat" 
204+   ExpectNonRealtimeSurvival (func);
205205}
206206
207- TEST (TestRadsanInterceptors, openCreatesFileWithProperMode ) {
207+ TEST_F (RadsanFileTest, OpenCreatesFileWithProperMode ) {
208208  const  int  mode = S_IRGRP | S_IROTH | S_IRUSR | S_IWUSR;
209209
210-   const  int  fd = open (TemporaryFilePath (), O_CREAT | O_WRONLY, mode);
210+   const  int  fd = open (GetTemporaryFilePath (), O_CREAT | O_WRONLY, mode);
211211  ASSERT_THAT (fd, Ne (-1 ));
212212  close (fd);
213213
214214  struct  stat  st;
215-   ASSERT_THAT (stat (TemporaryFilePath (), &st), Eq (0 ));
215+   ASSERT_THAT (stat (GetTemporaryFilePath (), &st), Eq (0 ));
216216
217217  //  Mask st_mode to get permission bits only
218218  ASSERT_THAT (st.st_mode  & 0777 , Eq (mode));
219- 
220-   std::remove (TemporaryFilePath ());
221219}
222220
223- TEST (TestRadsanInterceptors, CreatDiesWhenRealtime) {
224-   auto  Func = []() { creat (TemporaryFilePath (), S_IWOTH | S_IROTH); };
225-   ExpectRealtimeDeath (Func, " creat" 
226-   ExpectNonRealtimeSurvival (Func);
227-   std::remove (TemporaryFilePath ());
221+ TEST_F (RadsanFileTest, CreatDiesWhenRealtime) {
222+   auto  func = [this ]() { creat (GetTemporaryFilePath (), S_IWOTH | S_IROTH); };
223+   ExpectRealtimeDeath (func, " creat" 
224+   ExpectNonRealtimeSurvival (func);
228225}
229226
230227TEST (TestRadsanInterceptors, FcntlDiesWhenRealtime) {
231-   auto  Func  = []() { fcntl (0 , F_GETFL); };
232-   ExpectRealtimeDeath (Func , " fcntl" 
233-   ExpectNonRealtimeSurvival (Func );
228+   auto  func  = []() { fcntl (0 , F_GETFL); };
229+   ExpectRealtimeDeath (func , " fcntl" 
230+   ExpectNonRealtimeSurvival (func );
234231}
235232
236- TEST (TestRadsanInterceptors , FcntlFlockDiesWhenRealtime) {
237-   int  fd = creat (TemporaryFilePath (), S_IRUSR | S_IWUSR);
233+ TEST_F (RadsanFileTest , FcntlFlockDiesWhenRealtime) {
234+   int  fd = creat (GetTemporaryFilePath (), S_IRUSR | S_IWUSR);
238235  ASSERT_THAT (fd, Ne (-1 ));
239236
240237  auto  func = [fd]() {
@@ -252,17 +249,10 @@ TEST(TestRadsanInterceptors, FcntlFlockDiesWhenRealtime) {
252249  ExpectNonRealtimeSurvival (func);
253250
254251  close (fd);
255-   std::remove (TemporaryFilePath ());
256- }
257- 
258- TEST (TestRadsanInterceptors, CloseDiesWhenRealtime) {
259-   auto  Func = []() { close (0 ); };
260-   expectRealtimeDeath (Func, " close" 
261-   expectNonrealtimeSurvival (Func);
262252}
263253
264- TEST (TestRadsanInterceptors, fcntlSetFdDiesWhenRealtime ) {
265-   int  fd = creat (TemporaryFilePath (), S_IRUSR | S_IWUSR);
254+ TEST_F (RadsanFileTest, FcntlSetFdDiesWhenRealtime ) {
255+   int  fd = creat (GetTemporaryFilePath (), S_IRUSR | S_IWUSR);
266256  ASSERT_THAT (fd, Ne (-1 ));
267257
268258  auto  func = [fd]() {
@@ -283,63 +273,64 @@ TEST(TestRadsanInterceptors, fcntlSetFdDiesWhenRealtime) {
283273  close (fd);
284274}
285275
286- TEST (TestRadsanInterceptors, FopenDiesWhenRealtime) {
287-   auto  Func = []() {
288-     FILE *fd = fopen (TemporaryFilePath (), " w" 
276+ TEST (TestRadsanInterceptors, CloseDiesWhenRealtime) {
277+   auto  func = []() { close (0 ); };
278+   ExpectRealtimeDeath (func, " close" 
279+   ExpectNonRealtimeSurvival (func);
280+ }
281+ 
282+ TEST_F (RadsanFileTest, FopenDiesWhenRealtime) {
283+   auto  func = [this ]() {
284+     auto  fd = fopen (GetTemporaryFilePath (), " w" 
289285    EXPECT_THAT (fd, Ne (nullptr ));
290286  };
291-   ExpectRealtimeDeath (Func, " fopen" 
292-   ExpectNonRealtimeSurvival (Func);
293-   std::remove (TemporaryFilePath ());
287+   ExpectRealtimeDeath (func, " fopen" 
288+   ExpectNonRealtimeSurvival (func);
294289}
295290
296- TEST (TestRadsanInterceptors , FreadDiesWhenRealtime) {
297-   FILE * fd = fopen (TemporaryFilePath (), " w" 
298-   auto  Func  = [fd]() {
291+ TEST_F (RadsanFileTest , FreadDiesWhenRealtime) {
292+   auto   fd = fopen (GetTemporaryFilePath (), " w" 
293+   auto  func  = [fd]() {
299294    char  c{};
300295    fread (&c, 1 , 1 , fd);
301296  };
302-   ExpectRealtimeDeath (Func , " fread" 
303-   ExpectNonRealtimeSurvival (Func );
297+   ExpectRealtimeDeath (func , " fread" 
298+   ExpectNonRealtimeSurvival (func );
304299  if  (fd != nullptr )
305300    fclose (fd);
306-   std::remove (TemporaryFilePath ());
307301}
308302
309- TEST (TestRadsanInterceptors , FwriteDiesWhenRealtime) {
310-   FILE * fd = fopen (TemporaryFilePath (), " w" 
303+ TEST_F (RadsanFileTest , FwriteDiesWhenRealtime) {
304+   auto   fd = fopen (GetTemporaryFilePath (), " w" 
311305  ASSERT_NE (nullptr , fd);
312-   const  char  *message = " Hello, world!" 
313-   auto  Func = [&]() { fwrite (&message, 1 , 4 , fd); };
314-   ExpectRealtimeDeath (Func, " fwrite" 
315-   ExpectNonRealtimeSurvival (Func);
316-   std::remove (TemporaryFilePath ());
306+   auto  message = " Hello, world!" 
307+   auto  func = [&]() { fwrite (&message, 1 , 4 , fd); };
308+   ExpectRealtimeDeath (func, " fwrite" 
309+   ExpectNonRealtimeSurvival (func);
317310}
318311
319- TEST (TestRadsanInterceptors , FcloseDiesWhenRealtime) {
320-   FILE * fd = fopen (TemporaryFilePath (), " w" 
312+ TEST_F (RadsanFileTest , FcloseDiesWhenRealtime) {
313+   auto   fd = fopen (GetTemporaryFilePath (), " w" 
321314  EXPECT_THAT (fd, Ne (nullptr ));
322-   auto  Func = [fd]() { fclose (fd); };
323-   ExpectRealtimeDeath (Func, " fclose" 
324-   ExpectNonRealtimeSurvival (Func);
325-   std::remove (TemporaryFilePath ());
315+   auto  func = [fd]() { fclose (fd); };
316+   ExpectRealtimeDeath (func, " fclose" 
317+   ExpectNonRealtimeSurvival (func);
326318}
327319
328320TEST (TestRadsanInterceptors, PutsDiesWhenRealtime) {
329-   auto  Func  = []() { puts (" Hello, world!\n " 
330-   expectRealtimeDeath (Func );
331-   expectNonrealtimeSurvival (Func );
321+   auto  func  = []() { puts (" Hello, world!\n " 
322+   expectRealtimeDeath (func );
323+   expectNonrealtimeSurvival (func );
332324}
333325
334- TEST (TestRadsanInterceptors , FputsDiesWhenRealtime) {
335-   FILE * fd = fopen (TemporaryFilePath (), " w" 
326+ TEST_F (RadsanFileTest , FputsDiesWhenRealtime) {
327+   auto   fd = fopen (GetTemporaryFilePath (), " w" 
336328  ASSERT_THAT (fd, Ne (nullptr )) << errno;
337-   auto  Func  = [fd]() { fputs (" Hello, world!\n " 
338-   ExpectRealtimeDeath (Func );
339-   ExpectNonRealtimeSurvival (Func );
329+   auto  func  = [fd]() { fputs (" Hello, world!\n " 
330+   ExpectRealtimeDeath (func );
331+   ExpectNonRealtimeSurvival (func );
340332  if  (fd != nullptr )
341333    fclose (fd);
342-   std::remove (TemporaryFilePath ());
343334}
344335
345336/* 
0 commit comments