LCOV - code coverage report
Current view: top level - lib/util/tests - test_util_paths.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 56 61 91.8 %
Date: 2024-04-21 15:09:00 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Unix SMB/CIFS implementation.
       3             :  *
       4             :  * Copyright (C) 2020      Andreas Schneider <asn@samba.org>
       5             :  *
       6             :  * This program is free software; you can redistribute it and/or modify
       7             :  * it under the terms of the GNU General Public License as published by
       8             :  * the Free Software Foundation; either version 3 of the License, or
       9             :  * (at your option) any later version.
      10             :  *
      11             :  * This program is distributed in the hope that it will be useful,
      12             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             :  * GNU General Public License for more details.
      15             :  *
      16             :  * You should have received a copy of the GNU General Public License
      17             :  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
      18             :  */
      19             : 
      20             : #include <stdarg.h>
      21             : #include <stddef.h>
      22             : #include <stdint.h>
      23             : #include <setjmp.h>
      24             : #include <cmocka.h>
      25             : 
      26             : #include "lib/replace/replace.h"
      27             : #include <talloc.h>
      28             : 
      29             : #include "lib/util/util_paths.c"
      30             : 
      31           1 : static int setup(void **state)
      32             : {
      33           1 :         TALLOC_CTX *mem_ctx = talloc_new(NULL);
      34             : 
      35           1 :         assert_non_null(mem_ctx);
      36           1 :         *state = mem_ctx;
      37             : 
      38           1 :         return 0;
      39             : }
      40             : 
      41           1 : static int teardown(void **state)
      42             : {
      43           1 :         TALLOC_CTX *mem_ctx = *state;
      44           1 :         TALLOC_FREE(mem_ctx);
      45             : 
      46           1 :     return 0;
      47             : }
      48             : 
      49           1 : static void test_get_user_home_dir(void **state)
      50             : {
      51           1 :         TALLOC_CTX *mem_ctx = *state;
      52           1 :         struct passwd *pwd = getpwuid(getuid());
      53           1 :         char *user;
      54             : 
      55           1 :         user = get_user_home_dir(mem_ctx);
      56           1 :         assert_non_null(user);
      57           1 :         assert_string_equal(user, pwd->pw_dir);
      58             : 
      59           1 :         TALLOC_FREE(user);
      60           1 : }
      61             : 
      62           1 : static void test_path_expand_tilde(void **state)
      63             : {
      64           1 :         TALLOC_CTX *mem_ctx = *state;
      65           1 :         char h[256] = {0};
      66           1 :         char *d = NULL;
      67           1 :         const char *user = NULL;
      68           1 :         char *home = NULL;
      69             : 
      70           1 :         user = getenv("USER");
      71           1 :         if (user == NULL){
      72           0 :                 user = getenv("LOGNAME");
      73             :         }
      74             : 
      75             :         /* In certain CIs there no such variables */
      76           1 :         if (user == NULL) {
      77           0 :                 struct passwd *pw = getpwuid(getuid());
      78           0 :                 if (pw){
      79           0 :                         user = pw->pw_name;
      80             :                 }
      81             :         }
      82             : 
      83           1 :         home = getenv("HOME");
      84           1 :         assert_non_null(home);
      85           1 :         snprintf(h, sizeof(h), "%s/.cache", home);
      86             : 
      87           1 :         d = path_expand_tilde(mem_ctx, "~/.cache");
      88           1 :         assert_non_null(d);
      89           1 :         assert_string_equal(d, h);
      90           1 :         TALLOC_FREE(d);
      91             : 
      92           1 :         snprintf(h, sizeof(h), "%s/.cache/X~", home);
      93           1 :         d = path_expand_tilde(mem_ctx, "~/.cache/X~");
      94           1 :         assert_string_equal(d, h);
      95           1 :         TALLOC_FREE(d);
      96             : 
      97           1 :         d = path_expand_tilde(mem_ctx, "/guru/meditation");
      98           1 :         assert_non_null(d);
      99           1 :         assert_string_equal(d, "/guru/meditation");
     100           1 :         TALLOC_FREE(d);
     101             : 
     102           1 :         snprintf(h, sizeof(h), "~%s/.cache", user);
     103           1 :         d = path_expand_tilde(mem_ctx, h);
     104           1 :         assert_non_null(d);
     105             : 
     106           1 :         snprintf(h, sizeof(h), "%s/.cache", home);
     107           1 :         assert_string_equal(d, h);
     108           1 :         TALLOC_FREE(d);
     109           1 : }
     110             : 
     111           1 : int main(int argc, char *argv[])
     112             : {
     113           1 :         int rc;
     114           1 :         const struct CMUnitTest tests[] = {
     115             :                 cmocka_unit_test(test_get_user_home_dir),
     116             :                 cmocka_unit_test(test_path_expand_tilde),
     117             :         };
     118             : 
     119           1 :         if (argc == 2) {
     120           0 :                 cmocka_set_test_filter(argv[1]);
     121             :         }
     122           1 :         cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
     123             : 
     124           1 :         rc = cmocka_run_group_tests(tests, setup, teardown);
     125             : 
     126           1 :         return rc;
     127             : }

Generated by: LCOV version 1.14