]>
Commit | Line | Data |
---|---|---|
a032b68d | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
663996b3 | 2 | |
aa27b158 | 3 | #include <fcntl.h> |
81c58355 | 4 | #include <sys/stat.h> |
aa27b158 MP |
5 | #include <unistd.h> |
6 | ||
7 | #include "alloc-util.h" | |
81c58355 | 8 | #include "dirent-util.h" |
81c58355 | 9 | #include "fs-util.h" |
aa27b158 MP |
10 | #include "glob-util.h" |
11 | #include "macro.h" | |
81c58355 | 12 | #include "rm-rf.h" |
6e866b33 | 13 | #include "tmpfile-util.h" |
663996b3 | 14 | |
aa27b158 | 15 | static void test_glob_exists(void) { |
8b3d4ff0 MB |
16 | log_info("/* %s */", __func__); |
17 | ||
aa27b158 MP |
18 | char name[] = "/tmp/test-glob_exists.XXXXXX"; |
19 | int fd = -1; | |
20 | int r; | |
60f067b4 | 21 | |
8a584da2 | 22 | fd = mkostemp_safe(name); |
aa27b158 MP |
23 | assert_se(fd >= 0); |
24 | close(fd); | |
5eef597e | 25 | |
aa27b158 MP |
26 | r = glob_exists("/tmp/test-glob_exists*"); |
27 | assert_se(r == 1); | |
5eef597e | 28 | |
aa27b158 MP |
29 | r = unlink(name); |
30 | assert_se(r == 0); | |
31 | r = glob_exists("/tmp/test-glob_exists*"); | |
32 | assert_se(r == 0); | |
33 | } | |
5eef597e | 34 | |
98393f85 MB |
35 | static void closedir_wrapper(void* v) { |
36 | (void) closedir(v); | |
37 | } | |
38 | ||
81c58355 MB |
39 | static void test_glob_no_dot(void) { |
40 | char template[] = "/tmp/test-glob-util.XXXXXXX"; | |
41 | const char *fn; | |
42 | ||
43 | _cleanup_globfree_ glob_t g = { | |
98393f85 | 44 | .gl_closedir = closedir_wrapper, |
81c58355 MB |
45 | .gl_readdir = (struct dirent *(*)(void *)) readdir_no_dot, |
46 | .gl_opendir = (void *(*)(const char *)) opendir, | |
47 | .gl_lstat = lstat, | |
48 | .gl_stat = stat, | |
49 | }; | |
50 | ||
51 | int r; | |
52 | ||
8b3d4ff0 MB |
53 | log_info("/* %s */", __func__); |
54 | ||
81c58355 MB |
55 | assert_se(mkdtemp(template)); |
56 | ||
57 | fn = strjoina(template, "/*"); | |
58 | r = glob(fn, GLOB_NOSORT|GLOB_BRACE|GLOB_ALTDIRFUNC, NULL, &g); | |
59 | assert_se(r == GLOB_NOMATCH); | |
60 | ||
61 | fn = strjoina(template, "/.*"); | |
62 | r = glob(fn, GLOB_NOSORT|GLOB_BRACE|GLOB_ALTDIRFUNC, NULL, &g); | |
63 | assert_se(r == GLOB_NOMATCH); | |
64 | ||
65 | (void) rm_rf(template, REMOVE_ROOT|REMOVE_PHYSICAL); | |
66 | } | |
67 | ||
68 | static void test_safe_glob(void) { | |
69 | char template[] = "/tmp/test-glob-util.XXXXXXX"; | |
70 | const char *fn, *fn2, *fname; | |
71 | ||
72 | _cleanup_globfree_ glob_t g = {}; | |
73 | int r; | |
74 | ||
8b3d4ff0 MB |
75 | log_info("/* %s */", __func__); |
76 | ||
81c58355 MB |
77 | assert_se(mkdtemp(template)); |
78 | ||
79 | fn = strjoina(template, "/*"); | |
80 | r = safe_glob(fn, 0, &g); | |
81 | assert_se(r == -ENOENT); | |
82 | ||
83 | fn2 = strjoina(template, "/.*"); | |
84 | r = safe_glob(fn2, GLOB_NOSORT|GLOB_BRACE, &g); | |
85 | assert_se(r == -ENOENT); | |
86 | ||
87 | fname = strjoina(template, "/.foobar"); | |
88 | assert_se(touch(fname) == 0); | |
89 | ||
90 | r = safe_glob(fn, 0, &g); | |
91 | assert_se(r == -ENOENT); | |
92 | ||
93 | r = safe_glob(fn2, GLOB_NOSORT|GLOB_BRACE, &g); | |
94 | assert_se(r == 0); | |
95 | assert_se(g.gl_pathc == 1); | |
96 | assert_se(streq(g.gl_pathv[0], fname)); | |
97 | assert_se(g.gl_pathv[1] == NULL); | |
98 | ||
99 | (void) rm_rf(template, REMOVE_ROOT|REMOVE_PHYSICAL); | |
100 | } | |
101 | ||
8b3d4ff0 MB |
102 | static void test_glob_non_glob_prefix_one(const char *path, const char *expected) { |
103 | _cleanup_free_ char *t; | |
104 | ||
105 | assert_se(glob_non_glob_prefix(path, &t) == 0); | |
106 | assert_se(streq(t, expected)); | |
107 | } | |
108 | ||
109 | static void test_glob_non_glob(void) { | |
110 | log_info("/* %s */", __func__); | |
111 | ||
112 | test_glob_non_glob_prefix_one("/tmp/.X11-*", "/tmp/"); | |
113 | test_glob_non_glob_prefix_one("/tmp/*", "/tmp/"); | |
114 | test_glob_non_glob_prefix_one("/tmp*", "/"); | |
115 | test_glob_non_glob_prefix_one("/tmp/*/whatever", "/tmp/"); | |
116 | test_glob_non_glob_prefix_one("/tmp/*/whatever?", "/tmp/"); | |
117 | test_glob_non_glob_prefix_one("/?", "/"); | |
118 | ||
119 | char *x; | |
120 | assert_se(glob_non_glob_prefix("?", &x) == -ENOENT); | |
121 | } | |
122 | ||
aa27b158 MP |
123 | int main(void) { |
124 | test_glob_exists(); | |
81c58355 MB |
125 | test_glob_no_dot(); |
126 | test_safe_glob(); | |
8b3d4ff0 | 127 | test_glob_non_glob(); |
e735f4d4 | 128 | |
aa27b158 MP |
129 | return 0; |
130 | } |