LCOV - code coverage report
Current view: top level - test - test-glob-util.c (source / functions) Hit Total Coverage
Test: main_coverage.info Lines: 54 54 100.0 %
Date: 2019-08-22 15:41:25 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /* SPDX-License-Identifier: LGPL-2.1+ */
       2             : 
       3             : #include <fcntl.h>
       4             : #include <glob.h>
       5             : #include <sys/stat.h>
       6             : #include <unistd.h>
       7             : 
       8             : #include "alloc-util.h"
       9             : #include "dirent-util.h"
      10             : #include "fs-util.h"
      11             : #include "glob-util.h"
      12             : #include "macro.h"
      13             : #include "rm-rf.h"
      14             : #include "tmpfile-util.h"
      15             : 
      16           1 : static void test_glob_exists(void) {
      17           1 :         char name[] = "/tmp/test-glob_exists.XXXXXX";
      18           1 :         int fd = -1;
      19             :         int r;
      20             : 
      21           1 :         fd = mkostemp_safe(name);
      22           1 :         assert_se(fd >= 0);
      23           1 :         close(fd);
      24             : 
      25           1 :         r = glob_exists("/tmp/test-glob_exists*");
      26           1 :         assert_se(r == 1);
      27             : 
      28           1 :         r = unlink(name);
      29           1 :         assert_se(r == 0);
      30           1 :         r = glob_exists("/tmp/test-glob_exists*");
      31           1 :         assert_se(r == 0);
      32           1 : }
      33             : 
      34           2 : static void closedir_wrapper(void* v) {
      35           2 :         (void) closedir(v);
      36           2 : }
      37             : 
      38           1 : static void test_glob_no_dot(void) {
      39           1 :         char template[] = "/tmp/test-glob-util.XXXXXXX";
      40             :         const char *fn;
      41             : 
      42           1 :         _cleanup_globfree_ glob_t g = {
      43             :                 .gl_closedir = closedir_wrapper,
      44             :                 .gl_readdir = (struct dirent *(*)(void *)) readdir_no_dot,
      45             :                 .gl_opendir = (void *(*)(const char *)) opendir,
      46             :                 .gl_lstat = lstat,
      47             :                 .gl_stat = stat,
      48             :         };
      49             : 
      50             :         int r;
      51             : 
      52           1 :         assert_se(mkdtemp(template));
      53             : 
      54           5 :         fn = strjoina(template, "/*");
      55           1 :         r = glob(fn, GLOB_NOSORT|GLOB_BRACE|GLOB_ALTDIRFUNC, NULL, &g);
      56           1 :         assert_se(r == GLOB_NOMATCH);
      57             : 
      58           5 :         fn = strjoina(template, "/.*");
      59           1 :         r = glob(fn, GLOB_NOSORT|GLOB_BRACE|GLOB_ALTDIRFUNC, NULL, &g);
      60           1 :         assert_se(r == GLOB_NOMATCH);
      61             : 
      62           1 :         (void) rm_rf(template, REMOVE_ROOT|REMOVE_PHYSICAL);
      63           1 : }
      64             : 
      65           1 : static void test_safe_glob(void) {
      66           1 :         char template[] = "/tmp/test-glob-util.XXXXXXX";
      67             :         const char *fn, *fn2, *fname;
      68             : 
      69           1 :         _cleanup_globfree_ glob_t g = {};
      70             :         int r;
      71             : 
      72           1 :         assert_se(mkdtemp(template));
      73             : 
      74           5 :         fn = strjoina(template, "/*");
      75           1 :         r = safe_glob(fn, 0, &g);
      76           1 :         assert_se(r == -ENOENT);
      77             : 
      78           5 :         fn2 = strjoina(template, "/.*");
      79           1 :         r = safe_glob(fn2, GLOB_NOSORT|GLOB_BRACE, &g);
      80           1 :         assert_se(r == -ENOENT);
      81             : 
      82           5 :         fname = strjoina(template, "/.foobar");
      83           1 :         assert_se(touch(fname) == 0);
      84             : 
      85           1 :         r = safe_glob(fn, 0, &g);
      86           1 :         assert_se(r == -ENOENT);
      87             : 
      88           1 :         r = safe_glob(fn2, GLOB_NOSORT|GLOB_BRACE, &g);
      89           1 :         assert_se(r == 0);
      90           1 :         assert_se(g.gl_pathc == 1);
      91           1 :         assert_se(streq(g.gl_pathv[0], fname));
      92           1 :         assert_se(g.gl_pathv[1] == NULL);
      93             : 
      94           1 :         (void) rm_rf(template, REMOVE_ROOT|REMOVE_PHYSICAL);
      95           1 : }
      96             : 
      97           1 : int main(void) {
      98           1 :         test_glob_exists();
      99           1 :         test_glob_no_dot();
     100           1 :         test_safe_glob();
     101             : 
     102           1 :         return 0;
     103             : }

Generated by: LCOV version 1.14