LCOV - code coverage report
Current view: top level - test - test_environment.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 69 69 100.0 %
Date: 2026-11-26 02:32:31 Functions: 6 6 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 11 22 50.0 %

           Branch data     Line data    Source code
       1                 :            : // Copyright 2012-2024 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/environment.h>
      10                 :            : #include <zix/path.h>
      11                 :            : 
      12                 :            : #include <assert.h>
      13                 :            : #include <stdlib.h>
      14                 :            : #include <string.h>
      15                 :            : 
      16                 :            : #ifdef _WIN32
      17                 :            : 
      18                 :            : #  define HOME_NAME "USERPROFILE"
      19                 :            : #  define HOME_VAR "%USERPROFILE%"
      20                 :            : 
      21                 :            : #else
      22                 :            : 
      23                 :            : #  define HOME_NAME "HOME"
      24                 :            : #  define HOME_VAR "$HOME"
      25                 :            : 
      26                 :            : static char*
      27                 :          2 : concat(ZixAllocator* const allocator,
      28                 :            :        const char* const   prefix,
      29                 :            :        const char* const   suffix)
      30                 :            : {
      31                 :          2 :   const size_t prefix_len = strlen(prefix);
      32                 :          2 :   const size_t suffix_len = strlen(suffix);
      33                 :          2 :   const size_t result_len = prefix_len + suffix_len;
      34                 :            : 
      35                 :          2 :   char* const result = (char*)zix_calloc(allocator, 1U, result_len + 1U);
      36         [ -  + ]:          2 :   assert(result);
      37                 :          2 :   memcpy(result, prefix, prefix_len + 1U);
      38                 :          2 :   memcpy(result + prefix_len, suffix, suffix_len + 1U);
      39                 :          2 :   return result;
      40                 :            : }
      41                 :            : 
      42                 :            : #endif
      43                 :            : 
      44                 :            : static void
      45                 :         18 : check_expansion(const char* const path, const char* const expected)
      46                 :            : {
      47                 :         18 :   char* const expanded = zix_expand_environment_strings(NULL, path);
      48         [ -  + ]:         18 :   assert(expanded);
      49         [ -  + ]:         18 :   assert(!strcmp(expanded, expected));
      50                 :         18 :   zix_free(NULL, expanded);
      51                 :         18 : }
      52                 :            : 
      53                 :            : static void
      54                 :          1 : test_expansion(void)
      55                 :            : {
      56                 :            :   // Check non-expansion of hopefully unset variables
      57                 :          1 :   check_expansion("$ZIX_UNSET0", "$ZIX_UNSET0");
      58                 :          1 :   check_expansion("$ZIX_unset0", "$ZIX_unset0");
      59                 :          1 :   check_expansion("%ZIX_UNSET0%", "%ZIX_UNSET0%");
      60                 :          1 :   check_expansion("%ZIX_unset0%", "%ZIX_unset0%");
      61                 :            : 
      62                 :            :   // Check non-expansion of invalid variable names
      63                 :          1 :   check_expansion("$%INVALID", "$%INVALID");
      64                 :          1 :   check_expansion("$<INVALID>", "$<INVALID>");
      65                 :          1 :   check_expansion("$[INVALID]", "$[INVALID]");
      66                 :          1 :   check_expansion("$invalid", "$invalid");
      67                 :          1 :   check_expansion("${INVALID}", "${INVALID}");
      68                 :            : 
      69                 :          1 :   const char* const home = getenv(HOME_NAME);
      70         [ +  - ]:          1 :   if (home) {
      71                 :          1 :     char* const var_foo  = zix_path_join(NULL, HOME_VAR, "foo");
      72                 :          1 :     char* const home_foo = zix_path_join(NULL, home, "foo");
      73                 :            : 
      74                 :          1 :     check_expansion(var_foo, home_foo);
      75                 :            : 
      76                 :            : #ifndef _WIN32
      77                 :          1 :     char* const tilde_foo      = zix_path_join(NULL, "~", "foo");
      78                 :          1 :     char* const home_and_other = concat(NULL, home, ":/other");
      79                 :          1 :     char* const other_and_home = concat(NULL, "/other:", home);
      80                 :          1 :     check_expansion("~other", "~other");
      81                 :          1 :     check_expansion("~", home);
      82                 :          1 :     check_expansion("~/foo", home_foo);
      83                 :          1 :     check_expansion("~:/other", home_and_other);
      84                 :          1 :     check_expansion("/other:~", other_and_home);
      85                 :          1 :     check_expansion("$HO", "$HO");
      86                 :          1 :     check_expansion("$HOMEZIX", "$HOMEZIX");
      87                 :          1 :     zix_free(NULL, other_and_home);
      88                 :          1 :     zix_free(NULL, home_and_other);
      89                 :          1 :     zix_free(NULL, tilde_foo);
      90                 :            : #endif
      91                 :            : 
      92                 :          1 :     zix_free(NULL, home_foo);
      93                 :          1 :     zix_free(NULL, var_foo);
      94                 :            :   }
      95                 :          1 : }
      96                 :            : 
      97                 :            : static void
      98                 :          1 : test_failed_alloc(void)
      99                 :            : {
     100                 :          1 :   ZixFailingAllocator allocator = zix_failing_allocator();
     101                 :            : 
     102                 :          1 :   zix_failing_allocator_reset(&allocator, 0U);
     103         [ -  + ]:          1 :   assert(!zix_expand_environment_strings(&allocator.base, "/one:~"));
     104         [ -  + ]:          1 :   assert(!zix_expand_environment_strings(&allocator.base, "/only"));
     105         [ -  + ]:          1 :   assert(!zix_expand_environment_strings(&allocator.base, "~"));
     106                 :            : 
     107                 :            : #ifndef _WIN32
     108                 :          1 :   zix_failing_allocator_reset(&allocator, 1U);
     109         [ -  + ]:          1 :   assert(!zix_expand_environment_strings(&allocator.base, "/one:~"));
     110         [ -  + ]:          1 :   assert(!zix_expand_environment_strings(&allocator.base, "/one:$HOME/two"));
     111                 :            : 
     112                 :          1 :   zix_failing_allocator_reset(&allocator, 2U);
     113         [ -  + ]:          1 :   assert(!zix_expand_environment_strings(&allocator.base, "/one:$HOME/two"));
     114                 :            : 
     115                 :          1 :   zix_failing_allocator_reset(&allocator, 1U);
     116         [ -  + ]:          1 :   assert(!zix_expand_environment_strings(&allocator.base, "/one:$UNSET/two"));
     117                 :            : #endif
     118                 :          1 : }
     119                 :            : 
     120                 :            : #ifndef _WIN32
     121                 :            : 
     122                 :            : // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
     123                 :            : extern char** environ;
     124                 :            : 
     125                 :            : static void
     126                 :          1 : test_null_environ(void)
     127                 :            : {
     128                 :          1 :   environ = NULL;
     129                 :          1 :   check_expansion(HOME_VAR, HOME_VAR);
     130                 :          1 : }
     131                 :            : 
     132                 :            : #endif
     133                 :            : 
     134                 :            : int
     135                 :          1 : main(void)
     136                 :            : {
     137                 :          1 :   test_expansion();
     138                 :          1 :   test_failed_alloc();
     139                 :            : #ifndef _WIN32
     140                 :          1 :   test_null_environ();
     141                 :            : #endif
     142                 :          1 :   return 0;
     143                 :            : }

Generated by: LCOV version 1.16