1 /* SPDX-License-Identifier: LGPL-2.1+ */
8 #include "alloc-util.h"
9 #include "dirent-util.h"
11 #include "glob-util.h"
14 #include "tmpfile-util.h"
16 static void test_glob_exists(void) {
17 char name
[] = "/tmp/test-glob_exists.XXXXXX";
21 fd
= mkostemp_safe(name
);
25 r
= glob_exists("/tmp/test-glob_exists*");
30 r
= glob_exists("/tmp/test-glob_exists*");
34 static void closedir_wrapper(void* v
) {
38 static void test_glob_no_dot(void) {
39 char template[] = "/tmp/test-glob-util.XXXXXXX";
42 _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
,
52 assert_se(mkdtemp(template));
54 fn
= strjoina(template, "/*");
55 r
= glob(fn
, GLOB_NOSORT
|GLOB_BRACE
|GLOB_ALTDIRFUNC
, NULL
, &g
);
56 assert_se(r
== GLOB_NOMATCH
);
58 fn
= strjoina(template, "/.*");
59 r
= glob(fn
, GLOB_NOSORT
|GLOB_BRACE
|GLOB_ALTDIRFUNC
, NULL
, &g
);
60 assert_se(r
== GLOB_NOMATCH
);
62 (void) rm_rf(template, REMOVE_ROOT
|REMOVE_PHYSICAL
);
65 static void test_safe_glob(void) {
66 char template[] = "/tmp/test-glob-util.XXXXXXX";
67 const char *fn
, *fn2
, *fname
;
69 _cleanup_globfree_ glob_t g
= {};
72 assert_se(mkdtemp(template));
74 fn
= strjoina(template, "/*");
75 r
= safe_glob(fn
, 0, &g
);
76 assert_se(r
== -ENOENT
);
78 fn2
= strjoina(template, "/.*");
79 r
= safe_glob(fn2
, GLOB_NOSORT
|GLOB_BRACE
, &g
);
80 assert_se(r
== -ENOENT
);
82 fname
= strjoina(template, "/.foobar");
83 assert_se(touch(fname
) == 0);
85 r
= safe_glob(fn
, 0, &g
);
86 assert_se(r
== -ENOENT
);
88 r
= safe_glob(fn2
, GLOB_NOSORT
|GLOB_BRACE
, &g
);
90 assert_se(g
.gl_pathc
== 1);
91 assert_se(streq(g
.gl_pathv
[0], fname
));
92 assert_se(g
.gl_pathv
[1] == NULL
);
94 (void) rm_rf(template, REMOVE_ROOT
|REMOVE_PHYSICAL
);