LCOV - code coverage report
Current view: top level - tests/src - test0026.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 53 53
Test Date: 2026-03-31 13:51:38 Functions: 100.0 % 1 1
Branches: 50.0 % 22 11

             Branch data     Line data    Source code
       1                 :             : #include "sute.h"
       2                 :             : #include <sys/stat.h>
       3                 :             : #include <unistd.h>
       4                 :             : #include <limits.h>
       5                 :             : 
       6                 :             : /**
       7                 :             :  * @brief Unit tests for file_check_access()
       8                 :             :  *
       9                 :             :  * @details
      10                 :             :  * - Verifies readable absolute paths are detected immediately
      11                 :             :  * - Verifies missing files return FILE_NOT_FOUND without errors
      12                 :             :  * - Verifies unreadable paths return FILE_ACCESS_DENIED
      13                 :             :  */
      14                 :           1 : Return test0026(void)
      15                 :             : {
      16                 :           1 :         INITTEST;
      17                 :             : 
      18                 :           1 :         create(char,tmpdir);
      19                 :           1 :         const char *tmpdir_path = getcstring(tmpdir);
      20                 :             : 
      21                 :           1 :         ASSERT(SUCCESS == create_tmpdir(tmpdir));
      22                 :           1 :         tmpdir_path = getcstring(tmpdir);
      23                 :             : 
      24                 :             :         /* Absolute path should be reported readable */
      25                 :             :         {
      26                 :             :                 char abs_path[PATH_MAX];
      27                 :           1 :                 FILE *file = NULL;
      28                 :           1 :                 create(char,absolute_path);
      29                 :           1 :                 const int written = snprintf(abs_path,sizeof(abs_path),"%s/%s",tmpdir_path,"test0026_abs.txt");
      30                 :           1 :                 ASSERT(written > 0 && (size_t)written < sizeof(abs_path));
      31                 :             : 
      32                 :           1 :                 ASSERT(SUCCESS == copy_cstring(absolute_path,abs_path,strlen(abs_path) + 1U));
      33                 :           1 :                 ASSERT(SUCCESS == open_file_stream(absolute_path,"wb",&file));
      34                 :           1 :                 ASSERT(file != NULL);
      35         [ +  - ]:           1 :                 if(file != NULL)
      36                 :             :                 {
      37                 :           1 :                         ASSERT(fclose(file) == 0);
      38                 :             :                 }
      39                 :             : 
      40                 :           1 :                 const size_t len = strlen(abs_path);
      41                 :           1 :                 FileAccessStatus rc = file_check_access(abs_path,len,R_OK);
      42                 :           1 :                 ASSERT(rc == FILE_ACCESS_ALLOWED);
      43                 :             : 
      44                 :           1 :                 remove(abs_path);
      45   [ -  +  -  + ]:           1 :                 call(del(absolute_path));
      46                 :             :         }
      47                 :             : 
      48                 :             :         /* Missing file should report not found without errors */
      49                 :             :         {
      50                 :             :                 char missing_path[PATH_MAX];
      51                 :           1 :                 const int written = snprintf(missing_path,sizeof(missing_path),"%s/%s",tmpdir_path,"test0026_missing.txt");
      52                 :           1 :                 ASSERT(written > 0 && (size_t)written < sizeof(missing_path));
      53                 :           1 :                 remove(missing_path); // ensure absence
      54                 :             : 
      55                 :           1 :                 const size_t len = strlen(missing_path);
      56                 :           1 :                 FileAccessStatus rc = file_check_access(missing_path,len,R_OK);
      57                 :             : 
      58                 :           1 :                 ASSERT(rc == FILE_NOT_FOUND);
      59                 :             :         }
      60                 :             : 
      61                 :             :         /* Unreadable path (directory without permissions) should report denied */
      62                 :             :         {
      63                 :             :                 char locked_dir[PATH_MAX];
      64                 :           1 :                 const int dir_written = snprintf(locked_dir,sizeof(locked_dir),"%s/%s",tmpdir_path,"test0026_locked_dir");
      65                 :           1 :                 ASSERT(dir_written > 0 && (size_t)dir_written < sizeof(locked_dir));
      66                 :             : 
      67                 :           1 :                 int mk_rc = mkdir(locked_dir,0700);
      68                 :           1 :                 ASSERT(mk_rc == 0);
      69                 :             : 
      70                 :             :                 char locked_file_path[PATH_MAX];
      71                 :           1 :                 FILE *file = NULL;
      72                 :           1 :                 create(char,locked_path);
      73                 :           1 :                 const int file_written = snprintf(locked_file_path,sizeof(locked_file_path),"%s/%s",locked_dir,"file.txt");
      74                 :           1 :                 ASSERT(file_written > 0 && (size_t)file_written < sizeof(locked_file_path));
      75                 :             : 
      76                 :           1 :                 ASSERT(SUCCESS == copy_cstring(locked_path,locked_file_path,strlen(locked_file_path) + 1U));
      77                 :           1 :                 ASSERT(SUCCESS == open_file_stream(locked_path,"wb",&file));
      78                 :           1 :                 ASSERT(file != NULL);
      79         [ +  - ]:           1 :                 if(file != NULL)
      80                 :             :                 {
      81                 :           1 :                         ASSERT(fclose(file) == 0);
      82                 :             :                 }
      83                 :             : 
      84                 :             :                 /* lock directory */
      85                 :           1 :                 ASSERT(chmod(locked_dir,0000) == 0);
      86                 :             : 
      87                 :           1 :                 const size_t len = strlen(locked_file_path);
      88                 :           1 :                 FileAccessStatus rc = file_check_access(locked_file_path,len,R_OK);
      89                 :             : 
      90                 :             :                 /* restore permissions for cleanup */
      91                 :           1 :                 ASSERT(chmod(locked_dir,0700) == 0);
      92                 :           1 :                 ASSERT(remove(locked_file_path) == 0);
      93                 :           1 :                 ASSERT(rmdir(locked_dir) == 0);
      94   [ -  +  -  + ]:           1 :                 call(del(locked_path));
      95                 :             : 
      96                 :           1 :                 ASSERT(rc == FILE_ACCESS_DENIED);
      97                 :             :         }
      98                 :             : 
      99   [ +  -  +  - ]:           1 :         if(tmpdir_path != NULL && tmpdir_path[0] != '\0')
     100                 :             :         {
     101                 :           1 :                 const int remove_tmpdir_status = rmdir(tmpdir_path);
     102         [ +  - ]:           1 :                 if(SUCCESS == status)
     103                 :             :                 {
     104                 :           1 :                         ASSERT(remove_tmpdir_status == 0);
     105                 :             :                 }
     106                 :             :         }
     107                 :             : 
     108   [ -  +  -  + ]:           1 :         call(del(tmpdir));
     109                 :             : 
     110                 :           1 :         RETURN_STATUS;
     111                 :             : }
        

Generated by: LCOV version 2.0-1