LCOV - code coverage report
Current view: top level - test - test_filesystem.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 359 359 100.0 %
Date: 2026-11-26 02:32:31 Functions: 20 20 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 180 376 47.9 %

           Branch data     Line data    Source code
       1                 :            : // Copyright 2020-2025 David Robillard <d@drobilla.net>
       2                 :            : // SPDX-License-Identifier: ISC
       3                 :            : 
       4                 :            : #undef NDEBUG
       5                 :            : 
       6                 :            : #include "failing_allocator.h"
       7                 :            : 
       8                 :            : #include <zix/allocator.h>
       9                 :            : #include <zix/filesystem.h>
      10                 :            : #include <zix/path.h>
      11                 :            : #include <zix/status.h>
      12                 :            : #include <zix/string_view.h>
      13                 :            : 
      14                 :            : #ifndef _WIN32
      15                 :            : #  include <unistd.h>
      16                 :            : #endif
      17                 :            : 
      18                 :            : #if defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L
      19                 :            : #  include <sys/socket.h>
      20                 :            : #  include <sys/stat.h>
      21                 :            : #  include <sys/un.h>
      22                 :            : #endif
      23                 :            : 
      24                 :            : #include <assert.h>
      25                 :            : #include <errno.h>
      26                 :            : #include <stdbool.h>
      27                 :            : #include <stdio.h>
      28                 :            : #include <stdlib.h>
      29                 :            : #include <string.h>
      30                 :            : 
      31                 :            : static void
      32                 :          1 : test_temp_directory_path(void)
      33                 :            : {
      34                 :          1 :   char* tmpdir = zix_temp_directory_path(NULL);
      35                 :            : 
      36         [ -  + ]:          1 :   assert(zix_file_type(tmpdir) == ZIX_FILE_TYPE_DIRECTORY);
      37                 :            : 
      38                 :          1 :   free(tmpdir);
      39                 :          1 : }
      40                 :            : 
      41                 :            : static void
      42                 :          1 : test_current_path(void)
      43                 :            : {
      44                 :          1 :   char* cwd = zix_current_path(NULL);
      45                 :            : 
      46         [ -  + ]:          1 :   assert(zix_file_type(cwd) == ZIX_FILE_TYPE_DIRECTORY);
      47                 :            : 
      48                 :          1 :   free(cwd);
      49                 :          1 : }
      50                 :            : 
      51                 :            : static char*
      52                 :         14 : create_temp_dir(const char* const name_pattern)
      53                 :            : {
      54                 :         14 :   char* const temp = zix_temp_directory_path(NULL);
      55         [ -  + ]:         14 :   assert(temp);
      56                 :            : 
      57                 :         14 :   char* const path_pattern = zix_path_join(NULL, temp, name_pattern);
      58                 :         14 :   char* const result       = zix_create_temporary_directory(NULL, path_pattern);
      59                 :         14 :   free(path_pattern);
      60                 :         14 :   zix_free(NULL, temp);
      61         [ -  + ]:         14 :   assert(result);
      62                 :         14 :   return result;
      63                 :            : }
      64                 :            : 
      65                 :            : static int
      66                 :         15 : write_to_path(const char* const path, const char* const contents)
      67                 :            : {
      68                 :         15 :   int         ret = -1;
      69                 :         15 :   FILE* const f   = fopen(path, "wb");
      70         [ +  - ]:         15 :   if (f) {
      71                 :         15 :     fwrite(contents, 1, strlen(contents), f);
      72   [ -  +  -  + ]:         15 :     ret = fflush(f) ? errno : ferror(f) ? EBADF : 0;
      73   [ -  +  -  - ]:         15 :     ret = (fclose(f) && !ret) ? errno : ret;
      74                 :            :   }
      75                 :            : 
      76                 :         15 :   return ret;
      77                 :            : }
      78                 :            : 
      79                 :            : static void
      80                 :          1 : test_canonical_path(void)
      81                 :            : {
      82         [ -  + ]:          1 :   assert(!zix_canonical_path(NULL, NULL));
      83                 :            : 
      84                 :          1 :   char* const temp_dir = create_temp_dir("zixXXXXXX");
      85                 :          1 :   char* const sub_dir  = zix_path_join(NULL, temp_dir, "sub");
      86         [ -  + ]:          1 :   assert(!zix_create_directory(sub_dir));
      87         [ -  + ]:          1 :   assert(zix_file_type(temp_dir) == ZIX_FILE_TYPE_DIRECTORY);
      88                 :            : 
      89                 :          1 :   char* const file_path = zix_path_join(NULL, temp_dir, "zix_test_file");
      90         [ -  + ]:          1 :   assert(file_path);
      91         [ -  + ]:          1 :   assert(!write_to_path(file_path, "test\n"));
      92                 :            : 
      93                 :            : #ifndef _WIN32
      94                 :            :   // Test symlink resolution
      95                 :            : 
      96                 :          1 :   char* const link_path = zix_path_join(NULL, temp_dir, "zix_test_link");
      97                 :            : 
      98         [ -  + ]:          1 :   assert(!zix_create_symlink(file_path, link_path));
      99                 :            : 
     100                 :          1 :   char* const real_file_path = zix_canonical_path(NULL, file_path);
     101                 :          1 :   char* const real_link_path = zix_canonical_path(NULL, link_path);
     102                 :            : 
     103         [ -  + ]:          1 :   assert(real_file_path);
     104         [ -  + ]:          1 :   assert(real_link_path);
     105         [ -  + ]:          1 :   assert(!strcmp(real_file_path, real_link_path));
     106                 :            : 
     107         [ -  + ]:          1 :   assert(!zix_remove(link_path));
     108                 :          1 :   free(real_link_path);
     109                 :          1 :   free(real_file_path);
     110                 :          1 :   free(link_path);
     111                 :            : #endif
     112                 :            : 
     113                 :            :   // Test dot segment resolution
     114                 :            : 
     115                 :          1 :   char* const parent_dir_1 = zix_path_join(NULL, sub_dir, "..");
     116         [ -  + ]:          1 :   assert(parent_dir_1);
     117                 :            : 
     118                 :          1 :   const ZixStringView parent_view  = zix_path_parent_path(sub_dir);
     119                 :          1 :   char* const         parent_dir_2 = zix_string_view_copy(NULL, parent_view);
     120         [ -  + ]:          1 :   assert(parent_dir_2);
     121         [ -  + ]:          1 :   assert(parent_dir_2[0]);
     122                 :            : 
     123                 :          1 :   char* const real_parent_dir_1 = zix_canonical_path(NULL, parent_dir_1);
     124                 :          1 :   char* const real_parent_dir_2 = zix_canonical_path(NULL, parent_dir_2);
     125                 :            : 
     126         [ -  + ]:          1 :   assert(real_parent_dir_1);
     127         [ -  + ]:          1 :   assert(real_parent_dir_2);
     128         [ -  + ]:          1 :   assert(!strcmp(real_parent_dir_1, real_parent_dir_2));
     129                 :            : 
     130                 :            :   // Test missing files
     131                 :            : 
     132         [ -  + ]:          1 :   assert(!zix_canonical_path(NULL, "/presumuably/absent"));
     133         [ -  + ]:          1 :   assert(!zix_canonical_path(NULL, "/presumuably/absent/"));
     134         [ -  + ]:          1 :   assert(!zix_canonical_path(NULL, "presumuably_absent"));
     135                 :            : 
     136                 :            :   // Clean everything up
     137                 :            : 
     138         [ -  + ]:          1 :   assert(!zix_remove(file_path));
     139         [ -  + ]:          1 :   assert(!zix_remove(sub_dir));
     140         [ -  + ]:          1 :   assert(!zix_remove(temp_dir));
     141                 :            : 
     142                 :          1 :   free(real_parent_dir_2);
     143                 :          1 :   free(real_parent_dir_1);
     144                 :          1 :   free(parent_dir_2);
     145                 :          1 :   free(parent_dir_1);
     146                 :          1 :   free(file_path);
     147                 :          1 :   free(sub_dir);
     148                 :          1 :   free(temp_dir);
     149                 :          1 : }
     150                 :            : 
     151                 :            : static void
     152                 :          1 : test_file_type(void)
     153                 :            : {
     154                 :          1 :   char* const temp_dir  = create_temp_dir("zixXXXXXX");
     155                 :          1 :   char* const file_path = zix_path_join(NULL, temp_dir, "zix_test_file");
     156         [ -  + ]:          1 :   assert(file_path);
     157         [ -  + ]:          1 :   assert(zix_file_type(file_path) == ZIX_FILE_TYPE_NONE);
     158                 :            : 
     159                 :            :   // Directory
     160         [ -  + ]:          1 :   assert(zix_file_type(temp_dir) == ZIX_FILE_TYPE_DIRECTORY);
     161                 :            : 
     162                 :            :   // Regular file
     163         [ -  + ]:          1 :   assert(!write_to_path(file_path, "test\n"));
     164         [ -  + ]:          1 :   assert(zix_file_type(file_path) == ZIX_FILE_TYPE_REGULAR);
     165         [ -  + ]:          1 :   assert(!zix_remove(file_path));
     166                 :            : 
     167                 :            : #if defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L
     168                 :            : 
     169                 :            :   // FIFO
     170                 :            :   if (!mkfifo(file_path, 0600)) {
     171                 :            :     assert(zix_file_type(file_path) == ZIX_FILE_TYPE_FIFO);
     172                 :            :     assert(!zix_remove(file_path));
     173                 :            :   }
     174                 :            : 
     175                 :            :   // Socket
     176                 :            :   const int sock = socket(AF_UNIX, SOCK_STREAM, 0);
     177                 :            :   if (sock >= 0) {
     178                 :            :     const socklen_t           addr_len = sizeof(struct sockaddr_un);
     179                 :            :     struct sockaddr_un* const addr = (struct sockaddr_un*)calloc(1, addr_len);
     180                 :            :     assert(addr);
     181                 :            : 
     182                 :            :     if (strlen(file_path) < sizeof(addr->sun_path)) {
     183                 :            :       addr->sun_family = AF_UNIX;
     184                 :            :       strncpy(addr->sun_path, file_path, sizeof(addr->sun_path) - 1);
     185                 :            : 
     186                 :            :       const int fd = bind(sock, (struct sockaddr*)addr, addr_len);
     187                 :            :       if (fd >= 0) {
     188                 :            :         assert(zix_file_type(file_path) == ZIX_FILE_TYPE_SOCKET);
     189                 :            :         assert(!zix_remove(file_path));
     190                 :            :         close(fd);
     191                 :            :       }
     192                 :            :     } // otherwise, TMPDIR is oddly long, skip test
     193                 :            : 
     194                 :            :     close(sock);
     195                 :            :     free(addr);
     196                 :            :   }
     197                 :            : 
     198                 :            : #endif
     199                 :            : 
     200         [ -  + ]:          1 :   assert(!zix_remove(temp_dir));
     201                 :          1 :   free(file_path);
     202                 :          1 :   free(temp_dir);
     203                 :          1 : }
     204                 :            : 
     205                 :            : static void
     206                 :          1 : test_copy_file(const char* data_file_path)
     207                 :            : {
     208                 :          1 :   char* const temp_dir      = create_temp_dir("zixXXXXXX");
     209                 :          1 :   char* const tmp_file_path = zix_path_join(NULL, temp_dir, "zix_test_file");
     210                 :          1 :   char* const copy_path     = zix_path_join(NULL, temp_dir, "zix_test_copy");
     211         [ -  + ]:          1 :   assert(tmp_file_path);
     212         [ -  + ]:          1 :   assert(copy_path);
     213                 :            : 
     214         [ +  - ]:          1 :   data_file_path = data_file_path ? data_file_path : tmp_file_path;
     215                 :            : 
     216         [ -  + ]:          1 :   assert(!write_to_path(tmp_file_path, "test\n"));
     217                 :            : 
     218                 :            :   // Fail to copy to/from a nonexistent file
     219         [ -  + ]:          1 :   assert(zix_copy_file(NULL, tmp_file_path, "/does/not/exist", 0U));
     220         [ -  + ]:          1 :   assert(zix_copy_file(NULL, "/does/not/exist", copy_path, 0U));
     221         [ -  + ]:          1 :   assert(zix_file_type(copy_path) == ZIX_FILE_TYPE_NONE);
     222                 :            : 
     223                 :            :   // Fail to copy from/to a directory
     224         [ -  + ]:          1 :   assert(zix_copy_file(NULL, temp_dir, copy_path, 0U));
     225         [ -  + ]:          1 :   assert(zix_copy_file(NULL, tmp_file_path, temp_dir, 0U));
     226                 :            : 
     227         [ +  - ]:          1 :   if (data_file_path) {
     228                 :            :     // Fail to copy a file to itself
     229         [ -  + ]:          1 :     assert(zix_copy_file(NULL, data_file_path, data_file_path, 0U) ==
     230                 :            :            ZIX_STATUS_EXISTS);
     231                 :            : 
     232                 :            :     // Successful copy between filesystems
     233         [ -  + ]:          1 :     assert(!zix_copy_file(NULL, data_file_path, copy_path, 0U));
     234         [ -  + ]:          1 :     assert(zix_file_equals(NULL, data_file_path, copy_path));
     235                 :            : 
     236                 :            :     // Trying the same again fails because the copy path already exists
     237         [ -  + ]:          1 :     assert(zix_copy_file(NULL, data_file_path, copy_path, 0U) ==
     238                 :            :            ZIX_STATUS_EXISTS);
     239                 :            : 
     240                 :            :     // Unless overwriting is requested
     241         [ -  + ]:          1 :     assert(!zix_copy_file(
     242                 :            :       NULL, data_file_path, copy_path, ZIX_COPY_OPTION_OVERWRITE_EXISTING));
     243                 :            : 
     244         [ -  + ]:          1 :     assert(!zix_remove(copy_path));
     245                 :            :   }
     246                 :            : 
     247                 :            :   // Successful copy within a filesystem
     248         [ -  + ]:          1 :   assert(zix_file_type(copy_path) == ZIX_FILE_TYPE_NONE);
     249         [ -  + ]:          1 :   assert(!zix_copy_file(NULL, tmp_file_path, copy_path, 0U));
     250         [ -  + ]:          1 :   assert(zix_file_equals(NULL, tmp_file_path, copy_path));
     251         [ -  + ]:          1 :   assert(!zix_remove(copy_path));
     252                 :            : 
     253         [ +  - ]:          1 :   if (zix_file_type("/dev/random") == ZIX_FILE_TYPE_CHARACTER) {
     254                 :            :     // Fail to copy infinite file to a file
     255         [ -  + ]:          1 :     assert(zix_copy_file(NULL, "/dev/random", copy_path, 0U) ==
     256                 :            :            ZIX_STATUS_BAD_ARG);
     257                 :            : 
     258                 :            :     // Fail to copy infinite file to itself
     259         [ -  + ]:          1 :     assert(zix_copy_file(NULL, "/dev/random", "/dev/random", 0U) ==
     260                 :            :            ZIX_STATUS_BAD_ARG);
     261                 :            : 
     262                 :            :     // Fail to copy infinite file to another
     263         [ -  + ]:          1 :     assert(zix_copy_file(NULL, "/dev/random", "/dev/urandom", 0U) ==
     264                 :            :            ZIX_STATUS_BAD_ARG);
     265                 :            :   }
     266                 :            : 
     267         [ +  - ]:          1 :   if (zix_file_type("/dev/full") == ZIX_FILE_TYPE_CHARACTER) {
     268         [ +  - ]:          1 :     if (data_file_path) {
     269         [ -  + ]:          1 :       assert(zix_copy_file(NULL,
     270                 :            :                            data_file_path,
     271                 :            :                            "/dev/full",
     272                 :            :                            ZIX_COPY_OPTION_OVERWRITE_EXISTING) ==
     273                 :            :              ZIX_STATUS_NO_SPACE);
     274                 :            :     }
     275                 :            : 
     276                 :            :     // Copy short file (error after flushing)
     277         [ -  + ]:          1 :     assert(zix_copy_file(
     278                 :            :       NULL, tmp_file_path, "/dev/full", ZIX_COPY_OPTION_OVERWRITE_EXISTING));
     279                 :            : 
     280                 :            :     // Copy long file (error during writing)
     281                 :          1 :     FILE* const f = fopen(tmp_file_path, "wb");
     282         [ -  + ]:          1 :     assert(f);
     283         [ +  + ]:       4097 :     for (size_t i = 0; i < 4096; ++i) {
     284                 :       4096 :       fprintf(f, "test\n");
     285                 :            :     }
     286                 :          1 :     fclose(f);
     287         [ -  + ]:          1 :     assert(zix_copy_file(
     288                 :            :       NULL, tmp_file_path, "/dev/full", ZIX_COPY_OPTION_OVERWRITE_EXISTING));
     289                 :            :   }
     290                 :            : 
     291         [ -  + ]:          1 :   assert(!zix_remove(tmp_file_path));
     292         [ -  + ]:          1 :   assert(!zix_remove(temp_dir));
     293                 :            : 
     294                 :          1 :   free(copy_path);
     295                 :          1 :   free(tmp_file_path);
     296                 :          1 :   free(temp_dir);
     297                 :          1 : }
     298                 :            : 
     299                 :            : static void
     300                 :          1 : test_flock(void)
     301                 :            : {
     302                 :          1 :   char* const temp_dir  = create_temp_dir("zixXXXXXX");
     303                 :          1 :   char* const file_path = zix_path_join(NULL, temp_dir, "zix_test_file");
     304         [ -  + ]:          1 :   assert(file_path);
     305                 :            : 
     306                 :          1 :   FILE* const f1 = fopen(file_path, "wb");
     307                 :          1 :   FILE* const f2 = fopen(file_path, "a+b");
     308                 :            : 
     309         [ -  + ]:          1 :   assert(f1);
     310         [ -  + ]:          1 :   assert(f2);
     311                 :            : 
     312                 :          1 :   ZixStatus st = zix_file_lock(f1, ZIX_FILE_LOCK_TRY);
     313   [ -  +  -  - ]:          1 :   assert(!st || st == ZIX_STATUS_NOT_SUPPORTED);
     314                 :            : 
     315         [ +  - ]:          1 :   if (!st) {
     316         [ -  + ]:          1 :     assert(zix_file_lock(f2, ZIX_FILE_LOCK_TRY) == ZIX_STATUS_UNAVAILABLE);
     317         [ -  + ]:          1 :     assert(!zix_file_unlock(f1, ZIX_FILE_LOCK_TRY));
     318                 :            :   }
     319                 :            : 
     320                 :          1 :   fclose(f2);
     321                 :          1 :   fclose(f1);
     322         [ -  + ]:          1 :   assert(!zix_remove(file_path));
     323         [ -  + ]:          1 :   assert(!zix_remove(temp_dir));
     324                 :          1 :   free(file_path);
     325                 :          1 :   free(temp_dir);
     326                 :          1 : }
     327                 :            : 
     328                 :            : typedef struct {
     329                 :            :   size_t n_names;
     330                 :            :   char** names;
     331                 :            : } FileList;
     332                 :            : 
     333                 :            : static void
     334                 :          2 : visit(const char* const path, const char* const name, void* const data)
     335                 :            : {
     336                 :            :   (void)path;
     337                 :            : 
     338                 :          2 :   const size_t    name_len  = strlen(name);
     339                 :          2 :   FileList* const file_list = (FileList*)data;
     340                 :            : 
     341                 :          2 :   char** const new_names =
     342                 :          2 :     (char**)realloc(file_list->names, sizeof(char*) * ++file_list->n_names);
     343                 :            : 
     344         [ +  - ]:          2 :   if (new_names) {
     345                 :          2 :     char* const name_copy = (char*)calloc(name_len + 1, 1);
     346         [ -  + ]:          2 :     assert(name_copy);
     347                 :          2 :     memcpy(name_copy, name, name_len + 1);
     348                 :            : 
     349                 :          2 :     file_list->names                         = new_names;
     350                 :          2 :     file_list->names[file_list->n_names - 1] = name_copy;
     351                 :            :   }
     352                 :          2 : }
     353                 :            : 
     354                 :            : static void
     355                 :          1 : test_dir_for_each(void)
     356                 :            : {
     357                 :          1 :   char* const temp_dir = create_temp_dir("zixXXXXXX");
     358                 :          1 :   char* const path1    = zix_path_join(NULL, temp_dir, "zix_test_1");
     359                 :          1 :   char* const path2    = zix_path_join(NULL, temp_dir, "zix_test_2");
     360         [ -  + ]:          1 :   assert(path1);
     361         [ -  + ]:          1 :   assert(path2);
     362                 :            : 
     363         [ -  + ]:          1 :   assert(!write_to_path(path1, "test\n"));
     364         [ -  + ]:          1 :   assert(!write_to_path(path2, "test\n"));
     365                 :            : 
     366                 :          1 :   FileList file_list = {0, NULL};
     367                 :          1 :   zix_dir_for_each(temp_dir, &file_list, visit);
     368                 :            : 
     369         [ -  + ]:          1 :   assert(file_list.names);
     370   [ -  +  -  -  :          1 :   assert((!strcmp(file_list.names[0], "zix_test_1") &&
             +  -  -  + ]
     371                 :            :           !strcmp(file_list.names[1], "zix_test_2")) ||
     372                 :            :          (!strcmp(file_list.names[0], "zix_test_2") &&
     373                 :            :           !strcmp(file_list.names[1], "zix_test_1")));
     374                 :            : 
     375         [ -  + ]:          1 :   assert(!zix_remove(path2));
     376         [ -  + ]:          1 :   assert(!zix_remove(path1));
     377         [ -  + ]:          1 :   assert(!zix_remove(temp_dir));
     378                 :            : 
     379                 :          1 :   free(file_list.names[0]);
     380                 :          1 :   free(file_list.names[1]);
     381                 :          1 :   free(file_list.names);
     382                 :          1 :   free(path2);
     383                 :          1 :   free(path1);
     384                 :          1 :   free(temp_dir);
     385                 :          1 : }
     386                 :            : 
     387                 :            : static void
     388                 :          1 : test_create_temporary_directory(void)
     389                 :            : {
     390         [ -  + ]:          1 :   assert(!zix_create_temporary_directory(NULL, ""));
     391                 :            : 
     392                 :          1 :   char* const path1 = create_temp_dir("zixXXXXXX");
     393         [ -  + ]:          1 :   assert(path1);
     394         [ -  + ]:          1 :   assert(zix_file_type(path1) == ZIX_FILE_TYPE_DIRECTORY);
     395                 :            : 
     396                 :          1 :   char* const path2 = create_temp_dir("zixXXXXXX");
     397         [ -  + ]:          1 :   assert(path2);
     398         [ -  + ]:          1 :   assert(strcmp(path1, path2));
     399         [ -  + ]:          1 :   assert(zix_file_type(path2) == ZIX_FILE_TYPE_DIRECTORY);
     400                 :            : 
     401         [ -  + ]:          1 :   assert(!zix_remove(path2));
     402         [ -  + ]:          1 :   assert(!zix_remove(path1));
     403                 :          1 :   free(path2);
     404                 :          1 :   free(path1);
     405                 :          1 : }
     406                 :            : 
     407                 :            : static void
     408                 :          1 : test_create_directory_like(void)
     409                 :            : {
     410                 :          1 :   char* const temp_dir = create_temp_dir("zixXXXXXX");
     411         [ -  + ]:          1 :   assert(zix_file_type(temp_dir) == ZIX_FILE_TYPE_DIRECTORY);
     412                 :            : 
     413                 :          1 :   char* const sub_dir = zix_path_join(NULL, temp_dir, "sub");
     414         [ -  + ]:          1 :   assert(zix_create_directory_like(sub_dir, sub_dir) == ZIX_STATUS_NOT_FOUND);
     415         [ -  + ]:          1 :   assert(!zix_create_directory_like(sub_dir, temp_dir));
     416         [ -  + ]:          1 :   assert(zix_file_type(sub_dir) == ZIX_FILE_TYPE_DIRECTORY);
     417         [ -  + ]:          1 :   assert(!zix_remove(sub_dir));
     418                 :          1 :   zix_free(NULL, sub_dir);
     419                 :            : 
     420         [ -  + ]:          1 :   assert(!zix_remove(temp_dir));
     421                 :          1 :   free(temp_dir);
     422                 :          1 : }
     423                 :            : 
     424                 :            : static void
     425                 :          1 : test_create_directories(void)
     426                 :            : {
     427                 :          1 :   char* const temp_dir = create_temp_dir("zixXXXXXX");
     428         [ -  + ]:          1 :   assert(temp_dir);
     429         [ -  + ]:          1 :   assert(zix_file_type(temp_dir) == ZIX_FILE_TYPE_DIRECTORY);
     430                 :            : 
     431         [ -  + ]:          1 :   assert(zix_create_directories(NULL, "") == ZIX_STATUS_BAD_ARG);
     432                 :            : 
     433                 :          1 :   char* const child_dir      = zix_path_join(NULL, temp_dir, "child");
     434                 :          1 :   char* const grandchild_dir = zix_path_join(NULL, child_dir, "grandchild");
     435                 :            : 
     436                 :            :   {
     437                 :          1 :     ZixFailingAllocator bad_allocator = zix_failing_allocator();
     438                 :          1 :     zix_failing_allocator_reset(&bad_allocator, 0U);
     439         [ -  + ]:          1 :     assert(zix_create_directories(&bad_allocator.base, grandchild_dir) ==
     440                 :            :            ZIX_STATUS_NO_MEM);
     441                 :            :   }
     442                 :            : 
     443         [ -  + ]:          1 :   assert(!zix_create_directories(NULL, grandchild_dir));
     444         [ -  + ]:          1 :   assert(zix_file_type(grandchild_dir) == ZIX_FILE_TYPE_DIRECTORY);
     445         [ -  + ]:          1 :   assert(zix_file_type(child_dir) == ZIX_FILE_TYPE_DIRECTORY);
     446                 :            : 
     447                 :          1 :   char* const file_path = zix_path_join(NULL, temp_dir, "zix_test_file");
     448         [ -  + ]:          1 :   assert(!write_to_path(file_path, "test\n"));
     449                 :            : 
     450         [ -  + ]:          1 :   assert(zix_create_directories(NULL, file_path) == ZIX_STATUS_EXISTS);
     451                 :            : 
     452         [ -  + ]:          1 :   assert(!zix_remove(file_path));
     453         [ -  + ]:          1 :   assert(!zix_remove(grandchild_dir));
     454         [ -  + ]:          1 :   assert(!zix_remove(child_dir));
     455         [ -  + ]:          1 :   assert(!zix_remove(temp_dir));
     456                 :          1 :   free(file_path);
     457                 :          1 :   free(child_dir);
     458                 :          1 :   free(grandchild_dir);
     459                 :          1 :   free(temp_dir);
     460                 :          1 : }
     461                 :            : 
     462                 :            : static bool
     463                 :          8 : check_file_equals(const char* const path1, const char* const path2)
     464                 :            : {
     465                 :          8 :   const bool r1 = zix_file_equals(NULL, path1, path2);
     466                 :          8 :   const bool r2 = zix_file_equals(NULL, path2, path1);
     467         [ -  + ]:          8 :   assert(r2 == r1);
     468                 :            : 
     469                 :          8 :   ZixFailingAllocator bad_allocator = zix_failing_allocator();
     470                 :          8 :   zix_failing_allocator_reset(&bad_allocator, 0U);
     471                 :            : 
     472                 :          8 :   const bool r3 = zix_file_equals(&bad_allocator.base, path1, path2);
     473                 :          8 :   const bool r4 = zix_file_equals(&bad_allocator.base, path2, path1);
     474         [ -  + ]:          8 :   assert(r3 == r1);
     475         [ -  + ]:          8 :   assert(r4 == r1);
     476                 :            : 
     477                 :          8 :   return r1;
     478                 :            : }
     479                 :            : 
     480                 :            : static void
     481                 :          1 : test_file_equals(const char* const data_file_path)
     482                 :            : {
     483                 :          1 :   char* const temp_dir = create_temp_dir("zixXXXXXX");
     484                 :          1 :   char* const path1    = zix_path_join(NULL, temp_dir, "zix1");
     485                 :          1 :   char* const path2    = zix_path_join(NULL, temp_dir, "zix2");
     486                 :            : 
     487                 :            :   // Equal: test, test
     488         [ -  + ]:          1 :   assert(!write_to_path(path1, "test"));
     489         [ -  + ]:          1 :   assert(!write_to_path(path2, "test"));
     490         [ -  + ]:          1 :   assert(check_file_equals(path1, path2));
     491                 :            : 
     492                 :            :   // Missing file
     493         [ -  + ]:          1 :   assert(!check_file_equals(path1, "/does/not/exist"));
     494                 :            : 
     495                 :            :   // Longer RHS: test, testdiff
     496         [ -  + ]:          1 :   assert(!write_to_path(path2, "diff"));
     497         [ -  + ]:          1 :   assert(!check_file_equals(path1, path2));
     498                 :            : 
     499                 :            :   // Longer LHS: testdifflong, testdiff
     500         [ -  + ]:          1 :   assert(!write_to_path(path1, "difflong"));
     501         [ -  + ]:          1 :   assert(!check_file_equals(path1, path2));
     502                 :            : 
     503                 :            :   // Equal sizes but different content: testdifflong, testdifflang
     504         [ -  + ]:          1 :   assert(!write_to_path(path2, "difflang"));
     505         [ -  + ]:          1 :   assert(!check_file_equals(path1, path2));
     506                 :            : 
     507                 :            :   // Same path
     508         [ -  + ]:          1 :   assert(check_file_equals(path1, path1));
     509                 :            : 
     510                 :            :   // Different devices
     511         [ +  - ]:          1 :   if (data_file_path) {
     512         [ -  + ]:          1 :     assert(!check_file_equals(data_file_path, path2));
     513         [ -  + ]:          1 :     assert(!zix_remove(path2));
     514         [ -  + ]:          1 :     assert(!zix_copy_file(NULL, data_file_path, path2, 0U));
     515         [ -  + ]:          1 :     assert(check_file_equals(data_file_path, path2));
     516                 :            :   }
     517                 :            : 
     518         [ -  + ]:          1 :   assert(!zix_remove(path2));
     519         [ -  + ]:          1 :   assert(!zix_remove(path1));
     520         [ -  + ]:          1 :   assert(!zix_remove(temp_dir));
     521                 :            : 
     522                 :          1 :   free(path2);
     523                 :          1 :   free(path1);
     524                 :          1 :   free(temp_dir);
     525                 :          1 : }
     526                 :            : 
     527                 :            : static void
     528                 :          1 : test_file_size(void)
     529                 :            : {
     530                 :            :   static const ZixStringView contents = ZIX_STATIC_STRING("file size test");
     531                 :            : 
     532                 :          1 :   char* const temp_dir = create_temp_dir("zixXXXXXX");
     533                 :          1 :   char* const path     = zix_path_join(NULL, temp_dir, "zix_test");
     534                 :            : 
     535                 :            :   // Nonexistent files
     536         [ -  + ]:          1 :   assert(!zix_file_size(path));
     537         [ -  + ]:          1 :   assert(!zix_file_size("/does/not/exist"));
     538                 :            : 
     539                 :            :   // Empty file
     540         [ -  + ]:          1 :   assert(!write_to_path(path, ""));
     541         [ -  + ]:          1 :   assert(!zix_file_size(path));
     542                 :            : 
     543                 :            :   // Non-empty file
     544         [ -  + ]:          1 :   assert(!write_to_path(path, contents.data));
     545         [ -  + ]:          1 :   assert((size_t)zix_file_size(path) == contents.length);
     546                 :            : 
     547         [ -  + ]:          1 :   assert(!zix_remove(path));
     548         [ -  + ]:          1 :   assert(!zix_remove(temp_dir));
     549                 :            : 
     550                 :          1 :   free(path);
     551                 :          1 :   free(temp_dir);
     552                 :          1 : }
     553                 :            : 
     554                 :            : static void
     555                 :          1 : test_create_symlink(void)
     556                 :            : {
     557                 :            :   static const char* const contents = "zixtest";
     558                 :            : 
     559                 :          1 :   char* const temp_dir = create_temp_dir("zixXXXXXX");
     560                 :            : 
     561                 :            :   // Write contents to original file
     562                 :          1 :   char* const file_path = zix_path_join(NULL, temp_dir, "zix_test_file");
     563         [ -  + ]:          1 :   assert(!write_to_path(file_path, contents));
     564                 :            : 
     565                 :            :   // Ensure original file exists and is a regular file
     566         [ -  + ]:          1 :   assert(zix_symlink_type(file_path) == ZIX_FILE_TYPE_REGULAR);
     567                 :            : 
     568                 :            :   // Create symlink to original file
     569                 :          1 :   char* const     link_path = zix_path_join(NULL, temp_dir, "zix_test_link");
     570                 :          1 :   const ZixStatus st        = zix_create_symlink(file_path, link_path);
     571                 :            : 
     572                 :            :   // Check that the symlink seems equivalent to the original file
     573   [ -  +  -  -  :          1 :   assert(!st || st == ZIX_STATUS_NOT_SUPPORTED || st == ZIX_STATUS_BAD_PERMS);
                   -  - ]
     574   [ -  +  -  - ]:          1 :   assert(!st || zix_symlink_type(link_path) == ZIX_FILE_TYPE_NONE);
     575         [ +  - ]:          1 :   if (!st) {
     576         [ -  + ]:          1 :     assert(zix_symlink_type(link_path) == ZIX_FILE_TYPE_SYMLINK);
     577         [ -  + ]:          1 :     assert(zix_file_type(link_path) == ZIX_FILE_TYPE_REGULAR);
     578         [ -  + ]:          1 :     assert(zix_file_equals(NULL, file_path, link_path));
     579         [ -  + ]:          1 :     assert(!zix_remove(link_path));
     580                 :            :   }
     581                 :            : 
     582         [ -  + ]:          1 :   assert(!zix_remove(file_path));
     583         [ -  + ]:          1 :   assert(!zix_remove(temp_dir));
     584                 :            : 
     585                 :          1 :   free(link_path);
     586                 :          1 :   free(file_path);
     587                 :          1 :   free(temp_dir);
     588                 :          1 : }
     589                 :            : 
     590                 :            : static void
     591                 :          1 : test_create_directory_symlink(void)
     592                 :            : {
     593                 :          1 :   char* const     temp_dir  = create_temp_dir("zixXXXXXX");
     594                 :          1 :   char* const     link_path = zix_path_join(NULL, temp_dir, "zix_test_link");
     595                 :          1 :   const ZixStatus st        = zix_create_directory_symlink(temp_dir, link_path);
     596                 :            : 
     597   [ +  -  +  - ]:          1 :   if (st != ZIX_STATUS_NOT_SUPPORTED && st != ZIX_STATUS_BAD_PERMS) {
     598         [ -  + ]:          1 :     assert(!st);
     599         [ -  + ]:          1 :     assert(zix_file_type(link_path) == ZIX_FILE_TYPE_DIRECTORY);
     600                 :            : 
     601                 :            : #ifdef _WIN32
     602                 :            :     assert(zix_symlink_type(link_path) == ZIX_FILE_TYPE_DIRECTORY);
     603                 :            : #else
     604         [ -  + ]:          1 :     assert(zix_symlink_type(link_path) == ZIX_FILE_TYPE_SYMLINK);
     605                 :            : #endif
     606                 :            : 
     607         [ -  + ]:          1 :     assert(!zix_remove(link_path));
     608                 :            :   }
     609                 :            : 
     610         [ -  + ]:          1 :   assert(!zix_remove(temp_dir));
     611                 :          1 :   free(link_path);
     612                 :          1 :   free(temp_dir);
     613                 :          1 : }
     614                 :            : 
     615                 :            : static void
     616                 :          1 : test_create_hard_link(void)
     617                 :            : {
     618                 :            :   static const char* const contents = "zixtest";
     619                 :            : 
     620                 :          1 :   char* const temp_dir = create_temp_dir("zixXXXXXX");
     621                 :            : 
     622                 :            :   // Write contents to original file
     623                 :          1 :   char* const file_path = zix_path_join(NULL, temp_dir, "zix_test_file");
     624         [ -  + ]:          1 :   assert(!write_to_path(file_path, contents));
     625                 :            : 
     626                 :            :   // Ensure original file exists and is a regular file
     627         [ -  + ]:          1 :   assert(zix_symlink_type(file_path) == ZIX_FILE_TYPE_REGULAR);
     628                 :            : 
     629                 :            :   // Create symlink to original file
     630                 :          1 :   char* const     link_path = zix_path_join(NULL, temp_dir, "zix_test_link");
     631                 :          1 :   const ZixStatus st        = zix_create_hard_link(file_path, link_path);
     632                 :            : 
     633                 :            :   // Check that the link is equivalent to the original file
     634   [ -  +  -  -  :          1 :   assert(!st || st == ZIX_STATUS_NOT_SUPPORTED || st == ZIX_STATUS_MAX_LINKS);
                   -  - ]
     635   [ -  +  -  - ]:          1 :   assert(!st || zix_symlink_type(link_path) == ZIX_FILE_TYPE_NONE);
     636         [ +  - ]:          1 :   if (!st) {
     637         [ -  + ]:          1 :     assert(zix_file_type(link_path) == ZIX_FILE_TYPE_REGULAR);
     638         [ -  + ]:          1 :     assert(zix_file_equals(NULL, file_path, link_path));
     639         [ -  + ]:          1 :     assert(!zix_remove(link_path));
     640                 :            :   }
     641                 :            : 
     642         [ -  + ]:          1 :   assert(!zix_remove(file_path));
     643         [ -  + ]:          1 :   assert(!zix_remove(temp_dir));
     644                 :            : 
     645                 :          1 :   free(link_path);
     646                 :          1 :   free(file_path);
     647                 :          1 :   free(temp_dir);
     648                 :          1 : }
     649                 :            : 
     650                 :            : int
     651                 :          1 : main(const int argc, char** const argv)
     652                 :            : {
     653                 :            :   // Try to find some existing data file that's ideally not on a tmpfs
     654         [ +  - ]:          1 :   const char* const default_file_path = (argc > 1) ? argv[1] : "build.ninja";
     655                 :          1 :   const char* const data_file_path =
     656                 :          1 :     (zix_file_type(default_file_path) == ZIX_FILE_TYPE_REGULAR)
     657                 :            :       ? default_file_path
     658         [ +  - ]:          1 :       : NULL;
     659                 :            : 
     660                 :          1 :   test_temp_directory_path();
     661                 :          1 :   test_current_path();
     662                 :          1 :   test_canonical_path();
     663                 :          1 :   test_file_type();
     664                 :          1 :   test_copy_file(data_file_path);
     665                 :          1 :   test_flock();
     666                 :          1 :   test_dir_for_each();
     667                 :          1 :   test_create_temporary_directory();
     668                 :          1 :   test_create_directory_like();
     669                 :          1 :   test_create_directories();
     670                 :          1 :   test_file_equals(data_file_path);
     671                 :          1 :   test_file_size();
     672                 :          1 :   test_create_symlink();
     673                 :          1 :   test_create_directory_symlink();
     674                 :          1 :   test_create_hard_link();
     675                 :            : 
     676                 :          1 :   return 0;
     677                 :            : }

Generated by: LCOV version 1.16