1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2010 Lennart Poettering
11 #include "alloc-util.h"
12 #include "dirent-util.h"
15 #include "glob-util.h"
19 static void test_glob_exists(void) {
20 char name
[] = "/tmp/test-glob_exists.XXXXXX";
24 fd
= mkostemp_safe(name
);
28 r
= glob_exists("/tmp/test-glob_exists*");
33 r
= glob_exists("/tmp/test-glob_exists*");
37 static void closedir_wrapper(void* v
) {
41 static void test_glob_no_dot(void) {
42 char template[] = "/tmp/test-glob-util.XXXXXXX";
45 _cleanup_globfree_ glob_t g
= {
46 .gl_closedir
= closedir_wrapper
,
47 .gl_readdir
= (struct dirent
*(*)(void *)) readdir_no_dot
,
48 .gl_opendir
= (void *(*)(const char *)) opendir
,
55 assert_se(mkdtemp(template));
57 fn
= strjoina(template, "/*");
58 r
= glob(fn
, GLOB_NOSORT
|GLOB_BRACE
|GLOB_ALTDIRFUNC
, NULL
, &g
);
59 assert_se(r
== GLOB_NOMATCH
);
61 fn
= strjoina(template, "/.*");
62 r
= glob(fn
, GLOB_NOSORT
|GLOB_BRACE
|GLOB_ALTDIRFUNC
, NULL
, &g
);
63 assert_se(r
== GLOB_NOMATCH
);
65 (void) rm_rf(template, REMOVE_ROOT
|REMOVE_PHYSICAL
);
68 static void test_safe_glob(void) {
69 char template[] = "/tmp/test-glob-util.XXXXXXX";
70 const char *fn
, *fn2
, *fname
;
72 _cleanup_globfree_ glob_t g
= {};
75 assert_se(mkdtemp(template));
77 fn
= strjoina(template, "/*");
78 r
= safe_glob(fn
, 0, &g
);
79 assert_se(r
== -ENOENT
);
81 fn2
= strjoina(template, "/.*");
82 r
= safe_glob(fn2
, GLOB_NOSORT
|GLOB_BRACE
, &g
);
83 assert_se(r
== -ENOENT
);
85 fname
= strjoina(template, "/.foobar");
86 assert_se(touch(fname
) == 0);
88 r
= safe_glob(fn
, 0, &g
);
89 assert_se(r
== -ENOENT
);
91 r
= safe_glob(fn2
, GLOB_NOSORT
|GLOB_BRACE
, &g
);
93 assert_se(g
.gl_pathc
== 1);
94 assert_se(streq(g
.gl_pathv
[0], fname
));
95 assert_se(g
.gl_pathv
[1] == NULL
);
97 (void) rm_rf(template, REMOVE_ROOT
|REMOVE_PHYSICAL
);