]>
Commit | Line | Data |
---|---|---|
1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ | |
2 | ||
3 | #include <stdio.h> | |
4 | #include <unistd.h> | |
5 | ||
6 | #include "alloc-util.h" | |
7 | #include "exec-util.h" | |
8 | #include "fd-util.h" | |
9 | #include "fs-util.h" | |
10 | #include "macro.h" | |
11 | #include "path-util.h" | |
12 | #include "process-util.h" | |
13 | #include "rm-rf.h" | |
14 | #include "stat-util.h" | |
15 | #include "string-util.h" | |
16 | #include "strv.h" | |
17 | #include "tests.h" | |
18 | #include "tmpfile-util.h" | |
19 | ||
20 | TEST(print_paths) { | |
21 | log_info("DEFAULT_PATH=%s", DEFAULT_PATH); | |
22 | log_info("DEFAULT_USER_PATH=%s", DEFAULT_USER_PATH); | |
23 | } | |
24 | ||
25 | TEST(path) { | |
26 | assert_se(path_is_absolute("/")); | |
27 | assert_se(!path_is_absolute("./")); | |
28 | ||
29 | assert_se(streq(basename("./aa/bb/../file.da."), "file.da.")); | |
30 | assert_se(streq(basename("/aa///.file"), ".file")); | |
31 | assert_se(streq(basename("/aa///file..."), "file...")); | |
32 | assert_se(streq(basename("file.../"), "")); | |
33 | ||
34 | assert_se(PATH_IN_SET("/bin", "/", "/bin", "/foo")); | |
35 | assert_se(PATH_IN_SET("/bin", "/bin")); | |
36 | assert_se(PATH_IN_SET("/bin", "/foo/bar", "/bin")); | |
37 | assert_se(PATH_IN_SET("/", "/", "/", "/foo/bar")); | |
38 | assert_se(!PATH_IN_SET("/", "/abc", "/def")); | |
39 | ||
40 | assert_se(path_equal_ptr(NULL, NULL)); | |
41 | assert_se(path_equal_ptr("/a", "/a")); | |
42 | assert_se(!path_equal_ptr("/a", "/b")); | |
43 | assert_se(!path_equal_ptr("/a", NULL)); | |
44 | assert_se(!path_equal_ptr(NULL, "/a")); | |
45 | } | |
46 | ||
47 | TEST(is_path) { | |
48 | assert_se(!is_path("foo")); | |
49 | assert_se(!is_path("dos.ext")); | |
50 | assert_se( is_path("/dir")); | |
51 | assert_se( is_path("a/b")); | |
52 | assert_se( is_path("a/b.ext")); | |
53 | ||
54 | assert_se(!is_path(".")); | |
55 | assert_se(!is_path("")); | |
56 | assert_se(!is_path("..")); | |
57 | ||
58 | assert_se( is_path("/dev")); | |
59 | assert_se( is_path("/./dev")); | |
60 | assert_se( is_path("/./dev/.")); | |
61 | assert_se( is_path("/./dev.")); | |
62 | assert_se( is_path("//dev")); | |
63 | assert_se( is_path("///dev")); | |
64 | assert_se( is_path("/dev/")); | |
65 | assert_se( is_path("///dev/")); | |
66 | assert_se( is_path("/./dev/")); | |
67 | assert_se( is_path("/../dev/")); | |
68 | assert_se( is_path("/dev/sda")); | |
69 | assert_se( is_path("/dev/sda5")); | |
70 | assert_se( is_path("/dev/sda5b3")); | |
71 | assert_se( is_path("/dev/sda5b3/idontexit")); | |
72 | assert_se( is_path("/../dev/sda")); | |
73 | assert_se( is_path("/../../dev/sda5")); | |
74 | assert_se( is_path("/../../../dev/sda5b3")); | |
75 | assert_se( is_path("/.././.././dev/sda5b3/idontexit")); | |
76 | assert_se( is_path("/sys")); | |
77 | assert_se( is_path("/sys/")); | |
78 | assert_se( is_path("/./sys")); | |
79 | assert_se( is_path("/./sys/.")); | |
80 | assert_se( is_path("/./sys.")); | |
81 | assert_se( is_path("/sys/what")); | |
82 | assert_se( is_path("/sys/something/..")); | |
83 | assert_se( is_path("/sys/something/../")); | |
84 | assert_se( is_path("/sys////")); | |
85 | assert_se( is_path("/sys////.")); | |
86 | assert_se( is_path("/sys/..")); | |
87 | assert_se( is_path("/sys/../")); | |
88 | assert_se( is_path("/usr/../dev/sda")); | |
89 | } | |
90 | ||
91 | TEST(is_device_path) { | |
92 | assert_se(!is_device_path("foo")); | |
93 | assert_se(!is_device_path("dos.ext")); | |
94 | assert_se(!is_device_path("/dir")); | |
95 | assert_se(!is_device_path("a/b")); | |
96 | assert_se(!is_device_path("a/b.ext")); | |
97 | ||
98 | assert_se(!is_device_path(".")); | |
99 | assert_se(!is_device_path("")); | |
100 | assert_se(!is_device_path("..")); | |
101 | ||
102 | assert_se(!is_device_path("/dev")); | |
103 | assert_se(!is_device_path("/./dev")); | |
104 | assert_se(!is_device_path("/./dev/.")); | |
105 | assert_se(!is_device_path("/./dev.")); | |
106 | assert_se( is_device_path("/./dev/foo")); | |
107 | assert_se( is_device_path("/./dev/./foo")); | |
108 | assert_se(!is_device_path("/./dev./foo")); | |
109 | assert_se(!is_device_path("//dev")); | |
110 | assert_se(!is_device_path("///dev")); | |
111 | assert_se(!is_device_path("/dev/")); | |
112 | assert_se(!is_device_path("///dev/")); | |
113 | assert_se(!is_device_path("/./dev/")); | |
114 | assert_se(!is_device_path("/../dev/")); | |
115 | assert_se( is_device_path("/dev/sda")); | |
116 | assert_se( is_device_path("/dev/sda5")); | |
117 | assert_se( is_device_path("/dev/sda5b3")); | |
118 | assert_se( is_device_path("/dev/sda5b3/idontexit")); | |
119 | assert_se(!is_device_path("/../dev/sda")); | |
120 | assert_se(!is_device_path("/../../dev/sda5")); | |
121 | assert_se(!is_device_path("/../../../dev/sda5b3")); | |
122 | assert_se(!is_device_path("/.././.././dev/sda5b3/idontexit")); | |
123 | assert_se(!is_device_path("/sys")); | |
124 | assert_se(!is_device_path("/sys/")); | |
125 | assert_se(!is_device_path("/./sys")); | |
126 | assert_se(!is_device_path("/./sys/.")); | |
127 | assert_se(!is_device_path("/./sys.")); | |
128 | assert_se( is_device_path("/./sys/foo")); | |
129 | assert_se( is_device_path("/./sys/./foo")); | |
130 | assert_se(!is_device_path("/./sys./foo")); | |
131 | assert_se( is_device_path("/sys/what")); | |
132 | assert_se( is_device_path("/sys/something/..")); | |
133 | assert_se( is_device_path("/sys/something/../")); | |
134 | assert_se(!is_device_path("/sys////")); | |
135 | assert_se(!is_device_path("/sys////.")); | |
136 | assert_se( is_device_path("/sys/..")); | |
137 | assert_se( is_device_path("/sys/../")); | |
138 | assert_se(!is_device_path("/usr/../dev/sda")); | |
139 | } | |
140 | ||
141 | static void test_path_simplify_one(const char *in, const char *out, PathSimplifyFlags flags) { | |
142 | char *p; | |
143 | ||
144 | p = strdupa_safe(in); | |
145 | path_simplify_full(p, flags); | |
146 | log_debug("/* test_path_simplify(%s) → %s (expected: %s) */", in, p, out); | |
147 | assert_se(streq(p, out)); | |
148 | } | |
149 | ||
150 | TEST(path_simplify) { | |
151 | _cleanup_free_ char *hoge = NULL, *hoge_out = NULL; | |
152 | char foo[NAME_MAX * 2]; | |
153 | ||
154 | test_path_simplify_one("", "", 0); | |
155 | test_path_simplify_one("aaa/bbb////ccc", "aaa/bbb/ccc", 0); | |
156 | test_path_simplify_one("//aaa/.////ccc", "/aaa/ccc", 0); | |
157 | test_path_simplify_one("///", "/", 0); | |
158 | test_path_simplify_one("///", "/", PATH_SIMPLIFY_KEEP_TRAILING_SLASH); | |
159 | test_path_simplify_one("///.//", "/", 0); | |
160 | test_path_simplify_one("///.//.///", "/", 0); | |
161 | test_path_simplify_one("////.././///../.", "/", 0); | |
162 | test_path_simplify_one(".", ".", 0); | |
163 | test_path_simplify_one("./", ".", 0); | |
164 | test_path_simplify_one("./", "./", PATH_SIMPLIFY_KEEP_TRAILING_SLASH); | |
165 | test_path_simplify_one(".///.//./.", ".", 0); | |
166 | test_path_simplify_one(".///.//././/", ".", 0); | |
167 | test_path_simplify_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.", | |
168 | "/aaa/.bbb/../c./d.dd/..eeee", 0); | |
169 | test_path_simplify_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/..", | |
170 | "/aaa/.bbb/../c./d.dd/..eeee/..", 0); | |
171 | test_path_simplify_one(".//./aaa///.//./.bbb/..///c.//d.dd///..eeee/..", | |
172 | "aaa/.bbb/../c./d.dd/..eeee/..", 0); | |
173 | test_path_simplify_one("..//./aaa///.//./.bbb/..///c.//d.dd///..eeee/..", | |
174 | "../aaa/.bbb/../c./d.dd/..eeee/..", 0); | |
175 | test_path_simplify_one("abc///", "abc/", PATH_SIMPLIFY_KEEP_TRAILING_SLASH); | |
176 | ||
177 | test_path_simplify_one("/../abc", "/abc", PATH_SIMPLIFY_KEEP_TRAILING_SLASH); | |
178 | test_path_simplify_one("/../abc///", "/abc/", PATH_SIMPLIFY_KEEP_TRAILING_SLASH); | |
179 | test_path_simplify_one("/../abc///", "/abc", 0); | |
180 | test_path_simplify_one("/../abc", "/abc", PATH_SIMPLIFY_KEEP_TRAILING_SLASH); | |
181 | test_path_simplify_one("/../abc///..", "/abc/..", PATH_SIMPLIFY_KEEP_TRAILING_SLASH); | |
182 | test_path_simplify_one("/../abc///../", "/abc/../", PATH_SIMPLIFY_KEEP_TRAILING_SLASH); | |
183 | test_path_simplify_one("/../abc///../", "/abc/..", 0); | |
184 | ||
185 | test_path_simplify_one("/../../abc", "/abc", PATH_SIMPLIFY_KEEP_TRAILING_SLASH); | |
186 | test_path_simplify_one("/../../abc///", "/abc/", PATH_SIMPLIFY_KEEP_TRAILING_SLASH); | |
187 | test_path_simplify_one("/../../abc///", "/abc", 0); | |
188 | test_path_simplify_one("/../../abc", "/abc", PATH_SIMPLIFY_KEEP_TRAILING_SLASH); | |
189 | test_path_simplify_one("/../../abc///../..", "/abc/../..", PATH_SIMPLIFY_KEEP_TRAILING_SLASH); | |
190 | test_path_simplify_one("/../../abc///../../", "/abc/../../", PATH_SIMPLIFY_KEEP_TRAILING_SLASH); | |
191 | test_path_simplify_one("/../../abc///../../", "/abc/../..", 0); | |
192 | ||
193 | test_path_simplify_one("/.././../abc", "/abc", PATH_SIMPLIFY_KEEP_TRAILING_SLASH); | |
194 | test_path_simplify_one("/.././../abc///", "/abc/", PATH_SIMPLIFY_KEEP_TRAILING_SLASH); | |
195 | test_path_simplify_one("/.././../abc///", "/abc", 0); | |
196 | test_path_simplify_one("/.././../abc", "/abc", PATH_SIMPLIFY_KEEP_TRAILING_SLASH); | |
197 | test_path_simplify_one("/.././../abc///../..", "/abc/../..", PATH_SIMPLIFY_KEEP_TRAILING_SLASH); | |
198 | test_path_simplify_one("/.././../abc///../../", "/abc/../../", PATH_SIMPLIFY_KEEP_TRAILING_SLASH); | |
199 | test_path_simplify_one("/.././../abc///../../", "/abc/../..", 0); | |
200 | ||
201 | test_path_simplify_one("/./.././../abc", "/abc", PATH_SIMPLIFY_KEEP_TRAILING_SLASH); | |
202 | test_path_simplify_one("/./.././../abc///", "/abc/", PATH_SIMPLIFY_KEEP_TRAILING_SLASH); | |
203 | test_path_simplify_one("/./.././../abc///", "/abc", 0); | |
204 | test_path_simplify_one("/./.././../abc", "/abc", PATH_SIMPLIFY_KEEP_TRAILING_SLASH); | |
205 | test_path_simplify_one("/./.././../abc///../..", "/abc/../..", PATH_SIMPLIFY_KEEP_TRAILING_SLASH); | |
206 | test_path_simplify_one("/./.././../abc///../../", "/abc/../../", PATH_SIMPLIFY_KEEP_TRAILING_SLASH); | |
207 | test_path_simplify_one("/./.././../abc///../../", "/abc/../..", 0); | |
208 | ||
209 | test_path_simplify_one("/.../abc", "/.../abc", PATH_SIMPLIFY_KEEP_TRAILING_SLASH); | |
210 | test_path_simplify_one("/.../abc///", "/.../abc/", PATH_SIMPLIFY_KEEP_TRAILING_SLASH); | |
211 | test_path_simplify_one("/.../abc///", "/.../abc", 0); | |
212 | test_path_simplify_one("/.../abc", "/.../abc", PATH_SIMPLIFY_KEEP_TRAILING_SLASH); | |
213 | test_path_simplify_one("/.../abc///...", "/.../abc/...", PATH_SIMPLIFY_KEEP_TRAILING_SLASH); | |
214 | test_path_simplify_one("/.../abc///.../", "/.../abc/.../", PATH_SIMPLIFY_KEEP_TRAILING_SLASH); | |
215 | test_path_simplify_one("/.../abc///.../", "/.../abc/...", 0); | |
216 | ||
217 | memset(foo, 'a', sizeof(foo) -1); | |
218 | char_array_0(foo); | |
219 | ||
220 | test_path_simplify_one(foo, foo, 0); | |
221 | ||
222 | hoge = strjoin("/", foo); | |
223 | assert_se(hoge); | |
224 | test_path_simplify_one(hoge, hoge, 0); | |
225 | hoge = mfree(hoge); | |
226 | ||
227 | hoge = strjoin("a////.//././//./b///././/./c/////././//./", foo, "//.//////d/e/.//f/"); | |
228 | assert_se(hoge); | |
229 | ||
230 | hoge_out = strjoin("a/b/c/", foo, "//.//////d/e/.//f/"); | |
231 | assert_se(hoge_out); | |
232 | ||
233 | test_path_simplify_one(hoge, hoge_out, 0); | |
234 | } | |
235 | ||
236 | static void test_path_compare_one(const char *a, const char *b, int expected) { | |
237 | int r; | |
238 | ||
239 | assert_se(path_compare(a, a) == 0); | |
240 | assert_se(path_compare(b, b) == 0); | |
241 | ||
242 | r = path_compare(a, b); | |
243 | assert_se((r > 0) == (expected > 0) && (r < 0) == (expected < 0)); | |
244 | r = path_compare(b, a); | |
245 | assert_se((r < 0) == (expected > 0) && (r > 0) == (expected < 0)); | |
246 | ||
247 | assert_se(path_equal(a, a) == 1); | |
248 | assert_se(path_equal(b, b) == 1); | |
249 | assert_se(path_equal(a, b) == (expected == 0)); | |
250 | assert_se(path_equal(b, a) == (expected == 0)); | |
251 | } | |
252 | ||
253 | TEST(path_compare) { | |
254 | test_path_compare_one("/goo", "/goo", 0); | |
255 | test_path_compare_one("/goo", "/goo", 0); | |
256 | test_path_compare_one("//goo", "/goo", 0); | |
257 | test_path_compare_one("//goo/////", "/goo", 0); | |
258 | test_path_compare_one("goo/////", "goo", 0); | |
259 | test_path_compare_one("/goo/boo", "/goo//boo", 0); | |
260 | test_path_compare_one("//goo/boo", "/goo/boo//", 0); | |
261 | test_path_compare_one("//goo/././//./boo//././//", "/goo/boo//.", 0); | |
262 | test_path_compare_one("/.", "//.///", 0); | |
263 | test_path_compare_one("/x", "x/", 1); | |
264 | test_path_compare_one("x/", "/", -1); | |
265 | test_path_compare_one("/x/./y", "x/y", 1); | |
266 | test_path_compare_one("/x/./y", "/x/y", 0); | |
267 | test_path_compare_one("/x/./././y", "/x/y/././.", 0); | |
268 | test_path_compare_one("./x/./././y", "./x/y/././.", 0); | |
269 | test_path_compare_one(".", "./.", 0); | |
270 | test_path_compare_one(".", "././.", 0); | |
271 | test_path_compare_one("./..", ".", 1); | |
272 | test_path_compare_one("x/.y", "x/y", -1); | |
273 | test_path_compare_one("foo", "/foo", -1); | |
274 | test_path_compare_one("/foo", "/foo/bar", -1); | |
275 | test_path_compare_one("/foo/aaa", "/foo/b", -1); | |
276 | test_path_compare_one("/foo/aaa", "/foo/b/a", -1); | |
277 | test_path_compare_one("/foo/a", "/foo/aaa", -1); | |
278 | test_path_compare_one("/foo/a/b", "/foo/aaa", -1); | |
279 | } | |
280 | ||
281 | static void test_path_compare_filename_one(const char *a, const char *b, int expected) { | |
282 | int r; | |
283 | ||
284 | assert_se(path_compare_filename(a, a) == 0); | |
285 | assert_se(path_compare_filename(b, b) == 0); | |
286 | ||
287 | r = path_compare_filename(a, b); | |
288 | assert_se((r > 0) == (expected > 0) && (r < 0) == (expected < 0)); | |
289 | r = path_compare_filename(b, a); | |
290 | assert_se((r < 0) == (expected > 0) && (r > 0) == (expected < 0)); | |
291 | ||
292 | assert_se(path_equal_filename(a, a) == 1); | |
293 | assert_se(path_equal_filename(b, b) == 1); | |
294 | assert_se(path_equal_filename(a, b) == (expected == 0)); | |
295 | assert_se(path_equal_filename(b, a) == (expected == 0)); | |
296 | } | |
297 | ||
298 | TEST(path_compare_filename) { | |
299 | test_path_compare_filename_one("/goo", "/goo", 0); | |
300 | test_path_compare_filename_one("/goo", "/goo", 0); | |
301 | test_path_compare_filename_one("//goo", "/goo", 0); | |
302 | test_path_compare_filename_one("//goo/////", "/goo", 0); | |
303 | test_path_compare_filename_one("goo/////", "goo", 0); | |
304 | test_path_compare_filename_one("/goo/boo", "/goo//boo", 0); | |
305 | test_path_compare_filename_one("//goo/boo", "/goo/boo//", 0); | |
306 | test_path_compare_filename_one("//goo/././//./boo//././//", "/goo/boo//.", 0); | |
307 | test_path_compare_filename_one("/.", "//.///", -1); | |
308 | test_path_compare_filename_one("/x", "x/", 0); | |
309 | test_path_compare_filename_one("x/", "/", 1); | |
310 | test_path_compare_filename_one("/x/./y", "x/y", 0); | |
311 | test_path_compare_filename_one("/x/./y", "/x/y", 0); | |
312 | test_path_compare_filename_one("/x/./././y", "/x/y/././.", 0); | |
313 | test_path_compare_filename_one("./x/./././y", "./x/y/././.", 0); | |
314 | test_path_compare_filename_one(".", "./.", -1); | |
315 | test_path_compare_filename_one(".", "././.", -1); | |
316 | test_path_compare_filename_one("./..", ".", 1); | |
317 | test_path_compare_filename_one("x/.y", "x/y", -1); | |
318 | test_path_compare_filename_one("foo", "/foo", 0); | |
319 | test_path_compare_filename_one("/foo", "/foo/bar", 1); | |
320 | test_path_compare_filename_one("/foo/aaa", "/foo/b", -1); | |
321 | test_path_compare_filename_one("/foo/aaa", "/foo/b/a", 1); | |
322 | test_path_compare_filename_one("/foo/a", "/foo/aaa", -1); | |
323 | test_path_compare_filename_one("/foo/a/b", "/foo/aaa", 1); | |
324 | test_path_compare_filename_one("/a/c", "/b/c", 0); | |
325 | test_path_compare_filename_one("/a", "/a", 0); | |
326 | test_path_compare_filename_one("/a/b", "/a/c", -1); | |
327 | test_path_compare_filename_one("/b", "/c", -1); | |
328 | } | |
329 | ||
330 | TEST(path_equal_root) { | |
331 | /* Nail down the details of how path_equal("/", ...) works. */ | |
332 | ||
333 | assert_se(path_equal("/", "/")); | |
334 | assert_se(path_equal("/", "//")); | |
335 | ||
336 | assert_se(path_equal("/", "/./")); | |
337 | assert_se(!path_equal("/", "/../")); | |
338 | ||
339 | assert_se(!path_equal("/", "/.../")); | |
340 | ||
341 | /* Make sure that files_same works as expected. */ | |
342 | ||
343 | assert_se(inode_same("/", "/", 0) > 0); | |
344 | assert_se(inode_same("/", "/", AT_SYMLINK_NOFOLLOW) > 0); | |
345 | assert_se(inode_same("/", "//", 0) > 0); | |
346 | assert_se(inode_same("/", "//", AT_SYMLINK_NOFOLLOW) > 0); | |
347 | ||
348 | assert_se(inode_same("/", "/./", 0) > 0); | |
349 | assert_se(inode_same("/", "/./", AT_SYMLINK_NOFOLLOW) > 0); | |
350 | assert_se(inode_same("/", "/../", 0) > 0); | |
351 | assert_se(inode_same("/", "/../", AT_SYMLINK_NOFOLLOW) > 0); | |
352 | ||
353 | assert_se(inode_same("/", "/.../", 0) == -ENOENT); | |
354 | assert_se(inode_same("/", "/.../", AT_SYMLINK_NOFOLLOW) == -ENOENT); | |
355 | ||
356 | /* The same for path_equal_or_files_same. */ | |
357 | ||
358 | assert_se(path_equal_or_inode_same("/", "/", 0)); | |
359 | assert_se(path_equal_or_inode_same("/", "/", AT_SYMLINK_NOFOLLOW)); | |
360 | assert_se(path_equal_or_inode_same("/", "//", 0)); | |
361 | assert_se(path_equal_or_inode_same("/", "//", AT_SYMLINK_NOFOLLOW)); | |
362 | ||
363 | assert_se(path_equal_or_inode_same("/", "/./", 0)); | |
364 | assert_se(path_equal_or_inode_same("/", "/./", AT_SYMLINK_NOFOLLOW)); | |
365 | assert_se(path_equal_or_inode_same("/", "/../", 0)); | |
366 | assert_se(path_equal_or_inode_same("/", "/../", AT_SYMLINK_NOFOLLOW)); | |
367 | ||
368 | assert_se(!path_equal_or_inode_same("/", "/.../", 0)); | |
369 | assert_se(!path_equal_or_inode_same("/", "/.../", AT_SYMLINK_NOFOLLOW)); | |
370 | } | |
371 | ||
372 | TEST(find_executable_full) { | |
373 | char *p; | |
374 | char* test_file_name; | |
375 | _cleanup_close_ int fd = -EBADF; | |
376 | char fn[] = "/tmp/test-XXXXXX"; | |
377 | ||
378 | assert_se(find_executable_full("sh", NULL, NULL, true, &p, NULL) == 0); | |
379 | puts(p); | |
380 | assert_se(streq(basename(p), "sh")); | |
381 | free(p); | |
382 | ||
383 | assert_se(find_executable_full("sh", NULL, NULL, false, &p, NULL) == 0); | |
384 | puts(p); | |
385 | assert_se(streq(basename(p), "sh")); | |
386 | free(p); | |
387 | ||
388 | _cleanup_free_ char *oldpath = NULL; | |
389 | p = getenv("PATH"); | |
390 | if (p) | |
391 | assert_se(oldpath = strdup(p)); | |
392 | ||
393 | assert_se(unsetenv("PATH") == 0); | |
394 | ||
395 | assert_se(find_executable_full("sh", NULL, NULL, true, &p, NULL) == 0); | |
396 | puts(p); | |
397 | assert_se(streq(basename(p), "sh")); | |
398 | free(p); | |
399 | ||
400 | assert_se(find_executable_full("sh", NULL, NULL, false, &p, NULL) == 0); | |
401 | puts(p); | |
402 | assert_se(streq(basename(p), "sh")); | |
403 | free(p); | |
404 | ||
405 | if (oldpath) | |
406 | assert_se(setenv("PATH", oldpath, true) >= 0); | |
407 | ||
408 | assert_se((fd = mkostemp_safe(fn)) >= 0); | |
409 | assert_se(fchmod(fd, 0755) >= 0); | |
410 | ||
411 | test_file_name = basename(fn); | |
412 | ||
413 | assert_se(find_executable_full(test_file_name, NULL, STRV_MAKE("/doesnotexist", "/tmp", "/bin"), false, &p, NULL) == 0); | |
414 | puts(p); | |
415 | assert_se(streq(p, fn)); | |
416 | free(p); | |
417 | ||
418 | (void) unlink(fn); | |
419 | assert_se(find_executable_full(test_file_name, NULL, STRV_MAKE("/doesnotexist", "/tmp", "/bin"), false, &p, NULL) == -ENOENT); | |
420 | } | |
421 | ||
422 | TEST(find_executable) { | |
423 | char *p; | |
424 | ||
425 | assert_se(find_executable("/bin/sh", &p) == 0); | |
426 | puts(p); | |
427 | assert_se(path_equal(p, "/bin/sh")); | |
428 | free(p); | |
429 | ||
430 | assert_se(find_executable(saved_argv[0], &p) == 0); | |
431 | puts(p); | |
432 | assert_se(endswith(p, "/test-path-util")); | |
433 | assert_se(path_is_absolute(p)); | |
434 | free(p); | |
435 | ||
436 | assert_se(find_executable("sh", &p) == 0); | |
437 | puts(p); | |
438 | assert_se(endswith(p, "/sh")); | |
439 | assert_se(path_is_absolute(p)); | |
440 | free(p); | |
441 | ||
442 | assert_se(find_executable("/bin/touch", &p) == 0); | |
443 | assert_se(streq(p, "/bin/touch")); | |
444 | free(p); | |
445 | ||
446 | assert_se(find_executable("touch", &p) == 0); | |
447 | assert_se(path_is_absolute(p)); | |
448 | assert_se(streq(basename(p), "touch")); | |
449 | free(p); | |
450 | ||
451 | assert_se(find_executable("xxxx-xxxx", &p) == -ENOENT); | |
452 | assert_se(find_executable("/some/dir/xxxx-xxxx", &p) == -ENOENT); | |
453 | assert_se(find_executable("/proc/filesystems", &p) == -EACCES); | |
454 | } | |
455 | ||
456 | static void test_find_executable_exec_one(const char *path) { | |
457 | _cleanup_free_ char *t = NULL; | |
458 | _cleanup_close_ int fd = -EBADF; | |
459 | pid_t pid; | |
460 | int r; | |
461 | ||
462 | r = find_executable_full(path, NULL, NULL, false, &t, &fd); | |
463 | ||
464 | log_info_errno(r, "%s: %s → %s: %d/%m", __func__, path, t ?: "-", fd); | |
465 | ||
466 | assert_se(fd > STDERR_FILENO); | |
467 | assert_se(path_is_absolute(t)); | |
468 | if (path_is_absolute(path)) | |
469 | assert_se(streq(t, path)); | |
470 | ||
471 | pid = fork(); | |
472 | assert_se(pid >= 0); | |
473 | if (pid == 0) { | |
474 | r = fexecve_or_execve(fd, t, STRV_MAKE(t, "--version"), STRV_MAKE(NULL)); | |
475 | log_error_errno(r, "[f]execve: %m"); | |
476 | _exit(EXIT_FAILURE); | |
477 | } | |
478 | ||
479 | assert_se(wait_for_terminate_and_check(t, pid, WAIT_LOG) == 0); | |
480 | } | |
481 | ||
482 | TEST(find_executable_exec) { | |
483 | test_find_executable_exec_one("touch"); | |
484 | test_find_executable_exec_one("/bin/touch"); | |
485 | ||
486 | _cleanup_free_ char *script = NULL; | |
487 | assert_se(get_testdata_dir("test-path-util/script.sh", &script) >= 0); | |
488 | test_find_executable_exec_one(script); | |
489 | } | |
490 | ||
491 | TEST(prefixes) { | |
492 | static const char* const values[] = { | |
493 | "/a/b/c/d", | |
494 | "/a/b/c", | |
495 | "/a/b", | |
496 | "/a", | |
497 | "", | |
498 | NULL | |
499 | }; | |
500 | unsigned i; | |
501 | char s[PATH_MAX]; | |
502 | bool b; | |
503 | ||
504 | i = 0; | |
505 | PATH_FOREACH_PREFIX_MORE(s, "/a/b/c/d") { | |
506 | log_error("---%s---", s); | |
507 | assert_se(streq(s, values[i++])); | |
508 | } | |
509 | assert_se(values[i] == NULL); | |
510 | ||
511 | i = 1; | |
512 | PATH_FOREACH_PREFIX(s, "/a/b/c/d") { | |
513 | log_error("---%s---", s); | |
514 | assert_se(streq(s, values[i++])); | |
515 | } | |
516 | assert_se(values[i] == NULL); | |
517 | ||
518 | i = 0; | |
519 | PATH_FOREACH_PREFIX_MORE(s, "////a////b////c///d///////") | |
520 | assert_se(streq(s, values[i++])); | |
521 | assert_se(values[i] == NULL); | |
522 | ||
523 | i = 1; | |
524 | PATH_FOREACH_PREFIX(s, "////a////b////c///d///////") | |
525 | assert_se(streq(s, values[i++])); | |
526 | assert_se(values[i] == NULL); | |
527 | ||
528 | PATH_FOREACH_PREFIX(s, "////") | |
529 | assert_not_reached(); | |
530 | ||
531 | b = false; | |
532 | PATH_FOREACH_PREFIX_MORE(s, "////") { | |
533 | assert_se(!b); | |
534 | assert_se(streq(s, "")); | |
535 | b = true; | |
536 | } | |
537 | assert_se(b); | |
538 | ||
539 | PATH_FOREACH_PREFIX(s, "") | |
540 | assert_not_reached(); | |
541 | ||
542 | b = false; | |
543 | PATH_FOREACH_PREFIX_MORE(s, "") { | |
544 | assert_se(!b); | |
545 | assert_se(streq(s, "")); | |
546 | b = true; | |
547 | } | |
548 | } | |
549 | ||
550 | TEST(path_join) { | |
551 | #define test_join(expected, ...) { \ | |
552 | _cleanup_free_ char *z = NULL; \ | |
553 | z = path_join(__VA_ARGS__); \ | |
554 | log_debug("got \"%s\", expected \"%s\"", z, expected); \ | |
555 | assert_se(streq(z, expected)); \ | |
556 | } | |
557 | ||
558 | test_join("/root/a/b/c", "/root", "/a/b", "/c"); | |
559 | test_join("/root/a/b/c", "/root", "a/b", "c"); | |
560 | test_join("/root/a/b/c", "/root", "/a/b", "c"); | |
561 | test_join("/root/c", "/root", "/", "c"); | |
562 | test_join("/root/", "/root", "/", NULL); | |
563 | ||
564 | test_join("/a/b/c", "", "/a/b", "/c"); | |
565 | test_join("a/b/c", "", "a/b", "c"); | |
566 | test_join("/a/b/c", "", "/a/b", "c"); | |
567 | test_join("/c", "", "/", "c"); | |
568 | test_join("/", "", "/", NULL); | |
569 | ||
570 | test_join("/a/b/c", NULL, "/a/b", "/c"); | |
571 | test_join("a/b/c", NULL, "a/b", "c"); | |
572 | test_join("/a/b/c", NULL, "/a/b", "c"); | |
573 | test_join("/c", NULL, "/", "c"); | |
574 | test_join("/", NULL, "/", NULL); | |
575 | ||
576 | test_join("", "", NULL); | |
577 | test_join("", NULL, ""); | |
578 | test_join("", NULL, NULL); | |
579 | ||
580 | test_join("foo/bar", "foo", "bar"); | |
581 | test_join("foo/bar", "", "foo", "bar"); | |
582 | test_join("foo/bar", NULL, "foo", NULL, "bar"); | |
583 | test_join("foo/bar", "", "foo", "", "bar", ""); | |
584 | test_join("foo/bar", "", "", "", "", "foo", "", "", "", "bar", "", "", ""); | |
585 | ||
586 | test_join("//foo///bar//", "", "/", "", "/foo/", "", "/", "", "/bar/", "", "/", ""); | |
587 | test_join("/foo/bar/", "/", "foo", "/", "bar", "/"); | |
588 | test_join("foo/bar/baz", "foo", "bar", "baz"); | |
589 | test_join("foo/bar/baz", "foo/", "bar", "/baz"); | |
590 | test_join("foo//bar//baz", "foo/", "/bar/", "/baz"); | |
591 | test_join("//foo////bar////baz//", "//foo/", "///bar/", "///baz//"); | |
592 | } | |
593 | ||
594 | TEST(path_extend) { | |
595 | _cleanup_free_ char *p = NULL; | |
596 | ||
597 | assert_se(path_extend(&p, "foo", "bar", "baz") == p); | |
598 | assert_se(streq(p, "foo/bar/baz")); | |
599 | ||
600 | assert_se(path_extend(&p, "foo", "bar", "baz") == p); | |
601 | assert_se(streq(p, "foo/bar/baz/foo/bar/baz")); | |
602 | ||
603 | p = mfree(p); | |
604 | assert_se(path_extend(&p, "foo") == p); | |
605 | assert_se(streq(p, "foo")); | |
606 | ||
607 | assert_se(path_extend(&p, "/foo") == p); | |
608 | assert_se(streq(p, "foo/foo")); | |
609 | assert_se(path_extend(&p, "/waaaah/wahhh//") == p); | |
610 | assert_se(streq(p, "foo/foo/waaaah/wahhh//")); /* path_extend() does not drop redundant slashes */ | |
611 | assert_se(path_extend(&p, "/aaa/bbb/") == p); | |
612 | assert_se(streq(p, "foo/foo/waaaah/wahhh///aaa/bbb/")); /* but not add an extra slash */ | |
613 | ||
614 | assert_se(free_and_strdup(&p, "/") >= 0); | |
615 | assert_se(path_extend(&p, "foo") == p); | |
616 | assert_se(streq(p, "/foo")); | |
617 | } | |
618 | ||
619 | TEST(fsck_exists) { | |
620 | /* Ensure we use a sane default for PATH. */ | |
621 | assert_se(unsetenv("PATH") == 0); | |
622 | ||
623 | /* fsck.minix is provided by util-linux and will probably exist. */ | |
624 | assert_se(fsck_exists_for_fstype("minix") == 1); | |
625 | ||
626 | assert_se(fsck_exists_for_fstype("AbCdE") == 0); | |
627 | assert_se(fsck_exists_for_fstype("/../bin/") == 0); | |
628 | } | |
629 | ||
630 | static void test_path_make_relative_one(const char *from, const char *to, const char *expected) { | |
631 | _cleanup_free_ char *z = NULL; | |
632 | int r; | |
633 | ||
634 | log_info("/* %s(%s, %s) */", __func__, from, to); | |
635 | ||
636 | r = path_make_relative(from, to, &z); | |
637 | assert_se((r >= 0) == !!expected); | |
638 | assert_se(streq_ptr(z, expected)); | |
639 | } | |
640 | ||
641 | TEST(path_make_relative) { | |
642 | test_path_make_relative_one("some/relative/path", "/some/path", NULL); | |
643 | test_path_make_relative_one("/some/path", "some/relative/path", NULL); | |
644 | test_path_make_relative_one("/some/dotdot/../path", "/some/path", NULL); | |
645 | ||
646 | test_path_make_relative_one("/", "/", "."); | |
647 | test_path_make_relative_one("/", "/some/path", "some/path"); | |
648 | test_path_make_relative_one("/some/path", "/some/path", "."); | |
649 | test_path_make_relative_one("/some/path", "/some/path/in/subdir", "in/subdir"); | |
650 | test_path_make_relative_one("/some/path", "/", "../.."); | |
651 | test_path_make_relative_one("/some/path", "/some/other/path", "../other/path"); | |
652 | test_path_make_relative_one("/some/path/./dot", "/some/further/path", "../../further/path"); | |
653 | test_path_make_relative_one("//extra.//.//./.slashes//./won't////fo.ol///anybody//", "/././/extra././/.slashes////ar.e/.just/././.fine///", "../../../ar.e/.just/.fine"); | |
654 | } | |
655 | ||
656 | static void test_path_make_relative_parent_one(const char *from, const char *to, const char *expected) { | |
657 | _cleanup_free_ char *z = NULL; | |
658 | int r; | |
659 | ||
660 | log_info("/* %s(%s, %s) */", __func__, from, to); | |
661 | ||
662 | r = path_make_relative_parent(from, to, &z); | |
663 | assert_se((r >= 0) == !!expected); | |
664 | assert_se(streq_ptr(z, expected)); | |
665 | } | |
666 | ||
667 | TEST(path_make_relative_parent) { | |
668 | test_path_make_relative_parent_one("some/relative/path/hoge", "/some/path", NULL); | |
669 | test_path_make_relative_parent_one("/some/path/hoge", "some/relative/path", NULL); | |
670 | test_path_make_relative_parent_one("/some/dotdot/../path/hoge", "/some/path", NULL); | |
671 | test_path_make_relative_parent_one("/", "/aaa", NULL); | |
672 | ||
673 | test_path_make_relative_parent_one("/hoge", "/", "."); | |
674 | test_path_make_relative_parent_one("/hoge", "/some/path", "some/path"); | |
675 | test_path_make_relative_parent_one("/some/path/hoge", "/some/path", "."); | |
676 | test_path_make_relative_parent_one("/some/path/hoge", "/some/path/in/subdir", "in/subdir"); | |
677 | test_path_make_relative_parent_one("/some/path/hoge", "/", "../.."); | |
678 | test_path_make_relative_parent_one("/some/path/hoge", "/some/other/path", "../other/path"); | |
679 | test_path_make_relative_parent_one("/some/path/./dot/hoge", "/some/further/path", "../../further/path"); | |
680 | test_path_make_relative_parent_one("//extra.//.//./.slashes//./won't////fo.ol///anybody//hoge", "/././/extra././/.slashes////ar.e/.just/././.fine///", "../../../ar.e/.just/.fine"); | |
681 | } | |
682 | ||
683 | TEST(path_strv_resolve) { | |
684 | char tmp_dir[] = "/tmp/test-path-util-XXXXXX"; | |
685 | _cleanup_strv_free_ char **search_dirs = NULL; | |
686 | _cleanup_strv_free_ char **absolute_dirs = NULL; | |
687 | ||
688 | assert_se(mkdtemp(tmp_dir) != NULL); | |
689 | ||
690 | search_dirs = strv_new("/dir1", "/dir2", "/dir3"); | |
691 | assert_se(search_dirs); | |
692 | STRV_FOREACH(d, search_dirs) { | |
693 | char *p = path_join(tmp_dir, *d); | |
694 | assert_se(p); | |
695 | assert_se(strv_push(&absolute_dirs, p) == 0); | |
696 | } | |
697 | ||
698 | assert_se(mkdir(absolute_dirs[0], 0700) == 0); | |
699 | assert_se(mkdir(absolute_dirs[1], 0700) == 0); | |
700 | assert_se(symlink("dir2", absolute_dirs[2]) == 0); | |
701 | ||
702 | path_strv_resolve(search_dirs, tmp_dir); | |
703 | assert_se(streq(search_dirs[0], "/dir1")); | |
704 | assert_se(streq(search_dirs[1], "/dir2")); | |
705 | assert_se(streq(search_dirs[2], "/dir2")); | |
706 | ||
707 | assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0); | |
708 | } | |
709 | ||
710 | static void test_path_startswith_one(const char *path, const char *prefix, const char *skipped, const char *expected) { | |
711 | const char *p, *q; | |
712 | ||
713 | log_debug("/* %s(%s, %s) */", __func__, path, prefix); | |
714 | ||
715 | p = path_startswith(path, prefix); | |
716 | assert_se(streq_ptr(p, expected)); | |
717 | if (p) { | |
718 | q = strjoina(skipped, p); | |
719 | assert_se(streq(q, path)); | |
720 | assert_se(p == path + strlen(skipped)); | |
721 | } | |
722 | } | |
723 | ||
724 | TEST(path_startswith) { | |
725 | test_path_startswith_one("/foo/bar/barfoo/", "/foo", "/foo/", "bar/barfoo/"); | |
726 | test_path_startswith_one("/foo/bar/barfoo/", "/foo/", "/foo/", "bar/barfoo/"); | |
727 | test_path_startswith_one("/foo/bar/barfoo/", "/", "/", "foo/bar/barfoo/"); | |
728 | test_path_startswith_one("/foo/bar/barfoo/", "////", "/", "foo/bar/barfoo/"); | |
729 | test_path_startswith_one("/foo/bar/barfoo/", "/foo//bar/////barfoo///", "/foo/bar/barfoo/", ""); | |
730 | test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/barfoo////", "/foo/bar/barfoo/", ""); | |
731 | test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar///barfoo/", "/foo/bar/barfoo/", ""); | |
732 | test_path_startswith_one("/foo/bar/barfoo/", "/foo////bar/barfoo/", "/foo/bar/barfoo/", ""); | |
733 | test_path_startswith_one("/foo/bar/barfoo/", "////foo/bar/barfoo/", "/foo/bar/barfoo/", ""); | |
734 | test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/barfoo", "/foo/bar/barfoo/", ""); | |
735 | ||
736 | test_path_startswith_one("/foo/./bar///barfoo/./.", "/foo", "/foo/./", "bar///barfoo/./."); | |
737 | test_path_startswith_one("/foo/./bar///barfoo/./.", "/foo/", "/foo/./", "bar///barfoo/./."); | |
738 | test_path_startswith_one("/foo/./bar///barfoo/./.", "/", "/", "foo/./bar///barfoo/./."); | |
739 | test_path_startswith_one("/foo/./bar///barfoo/./.", "////", "/", "foo/./bar///barfoo/./."); | |
740 | test_path_startswith_one("/foo/./bar///barfoo/./.", "/foo//bar/////barfoo///", "/foo/./bar///barfoo/./.", ""); | |
741 | test_path_startswith_one("/foo/./bar///barfoo/./.", "/foo/bar/barfoo////", "/foo/./bar///barfoo/./.", ""); | |
742 | test_path_startswith_one("/foo/./bar///barfoo/./.", "/foo/bar///barfoo/", "/foo/./bar///barfoo/./.", ""); | |
743 | test_path_startswith_one("/foo/./bar///barfoo/./.", "/foo////bar/barfoo/", "/foo/./bar///barfoo/./.", ""); | |
744 | test_path_startswith_one("/foo/./bar///barfoo/./.", "////foo/bar/barfoo/", "/foo/./bar///barfoo/./.", ""); | |
745 | test_path_startswith_one("/foo/./bar///barfoo/./.", "/foo/bar/barfoo", "/foo/./bar///barfoo/./.", ""); | |
746 | ||
747 | test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/barfooa/", NULL, NULL); | |
748 | test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/barfooa", NULL, NULL); | |
749 | test_path_startswith_one("/foo/bar/barfoo/", "", NULL, NULL); | |
750 | test_path_startswith_one("/foo/bar/barfoo/", "/bar/foo", NULL, NULL); | |
751 | test_path_startswith_one("/foo/bar/barfoo/", "/f/b/b/", NULL, NULL); | |
752 | test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/barfo", NULL, NULL); | |
753 | test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/bar", NULL, NULL); | |
754 | test_path_startswith_one("/foo/bar/barfoo/", "/fo", NULL, NULL); | |
755 | } | |
756 | ||
757 | static void test_prefix_root_one(const char *r, const char *p, const char *expected) { | |
758 | _cleanup_free_ char *s = NULL; | |
759 | const char *t; | |
760 | ||
761 | assert_se(s = path_join(r, p)); | |
762 | assert_se(path_equal_ptr(s, expected)); | |
763 | ||
764 | t = prefix_roota(r, p); | |
765 | assert_se(t); | |
766 | assert_se(path_equal_ptr(t, expected)); | |
767 | } | |
768 | ||
769 | TEST(prefix_root) { | |
770 | test_prefix_root_one("/", "/foo", "/foo"); | |
771 | test_prefix_root_one(NULL, "/foo", "/foo"); | |
772 | test_prefix_root_one("", "/foo", "/foo"); | |
773 | test_prefix_root_one("///", "/foo", "/foo"); | |
774 | test_prefix_root_one("/", "////foo", "/foo"); | |
775 | test_prefix_root_one(NULL, "////foo", "/foo"); | |
776 | test_prefix_root_one("/", "foo", "/foo"); | |
777 | test_prefix_root_one("", "foo", "foo"); | |
778 | test_prefix_root_one(NULL, "foo", "foo"); | |
779 | ||
780 | test_prefix_root_one("/foo", "/bar", "/foo/bar"); | |
781 | test_prefix_root_one("/foo", "bar", "/foo/bar"); | |
782 | test_prefix_root_one("foo", "bar", "foo/bar"); | |
783 | test_prefix_root_one("/foo/", "/bar", "/foo/bar"); | |
784 | test_prefix_root_one("/foo/", "//bar", "/foo/bar"); | |
785 | test_prefix_root_one("/foo///", "//bar", "/foo/bar"); | |
786 | } | |
787 | ||
788 | TEST(file_in_same_dir) { | |
789 | char *t; | |
790 | ||
791 | assert_se(file_in_same_dir("/", "a", &t) == -EADDRNOTAVAIL); | |
792 | ||
793 | assert_se(file_in_same_dir("/", "/a", &t) >= 0); | |
794 | assert_se(streq(t, "/a")); | |
795 | free(t); | |
796 | ||
797 | assert_se(file_in_same_dir("", "a", &t) == -EINVAL); | |
798 | ||
799 | assert_se(file_in_same_dir("a/", "x", &t) >= 0); | |
800 | assert_se(streq(t, "x")); | |
801 | free(t); | |
802 | ||
803 | assert_se(file_in_same_dir("bar/foo", "bar", &t) >= 0); | |
804 | assert_se(streq(t, "bar/bar")); | |
805 | free(t); | |
806 | } | |
807 | ||
808 | static void test_path_find_first_component_one( | |
809 | const char *path, | |
810 | bool accept_dot_dot, | |
811 | char **expected, | |
812 | int ret) { | |
813 | ||
814 | log_debug("/* %s(\"%s\", accept_dot_dot=%s) */", __func__, strnull(path), yes_no(accept_dot_dot)); | |
815 | ||
816 | for (const char *p = path;;) { | |
817 | const char *e; | |
818 | int r; | |
819 | ||
820 | r = path_find_first_component(&p, accept_dot_dot, &e); | |
821 | if (r <= 0) { | |
822 | if (r == 0) { | |
823 | if (path) { | |
824 | assert_se(p == path + strlen_ptr(path)); | |
825 | assert_se(isempty(p)); | |
826 | } else | |
827 | assert_se(!p); | |
828 | assert_se(!e); | |
829 | } | |
830 | assert_se(r == ret); | |
831 | assert_se(strv_isempty(expected)); | |
832 | return; | |
833 | } | |
834 | ||
835 | assert_se(e); | |
836 | assert_se(strcspn(e, "/") == (size_t) r); | |
837 | assert_se(strlen_ptr(*expected) == (size_t) r); | |
838 | assert_se(strneq(e, *expected++, r)); | |
839 | ||
840 | assert_se(p); | |
841 | log_debug("p=%s", p); | |
842 | if (!isempty(*expected)) | |
843 | assert_se(startswith(p, *expected)); | |
844 | else if (ret >= 0) { | |
845 | assert_se(p == path + strlen_ptr(path)); | |
846 | assert_se(isempty(p)); | |
847 | } | |
848 | } | |
849 | } | |
850 | ||
851 | TEST(path_find_first_component) { | |
852 | _cleanup_free_ char *hoge = NULL; | |
853 | char foo[NAME_MAX * 2]; | |
854 | ||
855 | test_path_find_first_component_one(NULL, false, NULL, 0); | |
856 | test_path_find_first_component_one("", false, NULL, 0); | |
857 | test_path_find_first_component_one("/", false, NULL, 0); | |
858 | test_path_find_first_component_one(".", false, NULL, 0); | |
859 | test_path_find_first_component_one("./", false, NULL, 0); | |
860 | test_path_find_first_component_one("./.", false, NULL, 0); | |
861 | test_path_find_first_component_one("..", false, NULL, -EINVAL); | |
862 | test_path_find_first_component_one("/..", false, NULL, -EINVAL); | |
863 | test_path_find_first_component_one("./..", false, NULL, -EINVAL); | |
864 | test_path_find_first_component_one("////./././//.", false, NULL, 0); | |
865 | test_path_find_first_component_one("a/b/c", false, STRV_MAKE("a", "b", "c"), 0); | |
866 | test_path_find_first_component_one("././//.///aa/bbb//./ccc", false, STRV_MAKE("aa", "bbb", "ccc"), 0); | |
867 | test_path_find_first_component_one("././//.///aa/.../../bbb//./ccc/.", false, STRV_MAKE("aa", "..."), -EINVAL); | |
868 | test_path_find_first_component_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.", false, STRV_MAKE("aaa", ".bbb"), -EINVAL); | |
869 | test_path_find_first_component_one("a/foo./b//././/", false, STRV_MAKE("a", "foo.", "b"), 0); | |
870 | ||
871 | test_path_find_first_component_one(NULL, true, NULL, 0); | |
872 | test_path_find_first_component_one("", true, NULL, 0); | |
873 | test_path_find_first_component_one("/", true, NULL, 0); | |
874 | test_path_find_first_component_one(".", true, NULL, 0); | |
875 | test_path_find_first_component_one("./", true, NULL, 0); | |
876 | test_path_find_first_component_one("./.", true, NULL, 0); | |
877 | test_path_find_first_component_one("..", true, STRV_MAKE(".."), 0); | |
878 | test_path_find_first_component_one("/..", true, STRV_MAKE(".."), 0); | |
879 | test_path_find_first_component_one("./..", true, STRV_MAKE(".."), 0); | |
880 | test_path_find_first_component_one("////./././//.", true, NULL, 0); | |
881 | test_path_find_first_component_one("a/b/c", true, STRV_MAKE("a", "b", "c"), 0); | |
882 | test_path_find_first_component_one("././//.///aa/bbb//./ccc", true, STRV_MAKE("aa", "bbb", "ccc"), 0); | |
883 | test_path_find_first_component_one("././//.///aa/.../../bbb//./ccc/.", true, STRV_MAKE("aa", "...", "..", "bbb", "ccc"), 0); | |
884 | test_path_find_first_component_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.", true, STRV_MAKE("aaa", ".bbb", "..", "c.", "d.dd", "..eeee"), 0); | |
885 | test_path_find_first_component_one("a/foo./b//././/", true, STRV_MAKE("a", "foo.", "b"), 0); | |
886 | ||
887 | memset(foo, 'a', sizeof(foo) -1); | |
888 | char_array_0(foo); | |
889 | ||
890 | test_path_find_first_component_one(foo, false, NULL, -EINVAL); | |
891 | test_path_find_first_component_one(foo, true, NULL, -EINVAL); | |
892 | ||
893 | hoge = strjoin("a/b/c/", foo, "//d/e/.//f/"); | |
894 | assert_se(hoge); | |
895 | ||
896 | test_path_find_first_component_one(hoge, false, STRV_MAKE("a", "b", "c"), -EINVAL); | |
897 | test_path_find_first_component_one(hoge, true, STRV_MAKE("a", "b", "c"), -EINVAL); | |
898 | } | |
899 | ||
900 | static void test_path_find_last_component_one( | |
901 | const char *path, | |
902 | bool accept_dot_dot, | |
903 | char **expected, | |
904 | int ret) { | |
905 | ||
906 | log_debug("/* %s(\"%s\", accept_dot_dot=%s) */", __func__, strnull(path), yes_no(accept_dot_dot)); | |
907 | ||
908 | for (const char *next = NULL;;) { | |
909 | const char *e; | |
910 | int r; | |
911 | ||
912 | r = path_find_last_component(path, accept_dot_dot, &next, &e); | |
913 | if (r <= 0) { | |
914 | if (r == 0) { | |
915 | assert_se(next == path); | |
916 | assert_se(!e); | |
917 | } | |
918 | assert_se(r == ret); | |
919 | assert_se(strv_isempty(expected)); | |
920 | return; | |
921 | } | |
922 | ||
923 | assert_se(e); | |
924 | assert_se(strcspn(e, "/") == (size_t) r); | |
925 | assert_se(strlen_ptr(*expected) == (size_t) r); | |
926 | assert_se(strneq(e, *expected++, r)); | |
927 | ||
928 | assert_se(next); | |
929 | log_debug("path=%s\nnext=%s", path, next); | |
930 | if (!isempty(*expected)) { | |
931 | assert_se(next < path + strlen(path)); | |
932 | assert_se(next >= path + strlen(*expected)); | |
933 | assert_se(startswith(next - strlen(*expected), *expected)); | |
934 | } else if (ret >= 0) | |
935 | assert_se(next == path); | |
936 | } | |
937 | } | |
938 | ||
939 | TEST(path_find_last_component) { | |
940 | _cleanup_free_ char *hoge = NULL; | |
941 | char foo[NAME_MAX * 2]; | |
942 | ||
943 | test_path_find_last_component_one(NULL, false, NULL, 0); | |
944 | test_path_find_last_component_one("", false, NULL, 0); | |
945 | test_path_find_last_component_one("/", false, NULL, 0); | |
946 | test_path_find_last_component_one(".", false, NULL, 0); | |
947 | test_path_find_last_component_one("./", false, NULL, 0); | |
948 | test_path_find_last_component_one("./.", false, NULL, 0); | |
949 | test_path_find_last_component_one("..", false, NULL, -EINVAL); | |
950 | test_path_find_last_component_one("/..", false, NULL, -EINVAL); | |
951 | test_path_find_last_component_one("./..", false, NULL, -EINVAL); | |
952 | test_path_find_last_component_one("////./././//.", false, NULL, 0); | |
953 | test_path_find_last_component_one("a/b/c", false, STRV_MAKE("c", "b", "a"), 0); | |
954 | test_path_find_last_component_one("././//.///aa./.bbb//./ccc/././/", false, STRV_MAKE("ccc", ".bbb", "aa."), 0); | |
955 | test_path_find_last_component_one("././//.///aa/../.../bbb//./ccc/.", false, STRV_MAKE("ccc", "bbb", "..."), -EINVAL); | |
956 | test_path_find_last_component_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.", false, STRV_MAKE("..eeee", "d.dd", "c."), -EINVAL); | |
957 | ||
958 | test_path_find_last_component_one(NULL, true, NULL, 0); | |
959 | test_path_find_last_component_one("", true, NULL, 0); | |
960 | test_path_find_last_component_one("/", true, NULL, 0); | |
961 | test_path_find_last_component_one(".", true, NULL, 0); | |
962 | test_path_find_last_component_one("./", true, NULL, 0); | |
963 | test_path_find_last_component_one("./.", true, NULL, 0); | |
964 | test_path_find_last_component_one("..", true, STRV_MAKE(".."), 0); | |
965 | test_path_find_last_component_one("/..", true, STRV_MAKE(".."), 0); | |
966 | test_path_find_last_component_one("./..", true, STRV_MAKE(".."), 0); | |
967 | test_path_find_last_component_one("////./././//.", true, NULL, 0); | |
968 | test_path_find_last_component_one("a/b/c", true, STRV_MAKE("c", "b", "a"), 0); | |
969 | test_path_find_last_component_one("././//.///aa./.bbb//./ccc/././/", true, STRV_MAKE("ccc", ".bbb", "aa."), 0); | |
970 | test_path_find_last_component_one("././//.///aa/../.../bbb//./ccc/.", true, STRV_MAKE("ccc", "bbb", "...", "..", "aa"), 0); | |
971 | test_path_find_last_component_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.", true, STRV_MAKE("..eeee", "d.dd", "c.", "..", ".bbb", "aaa"), 0); | |
972 | ||
973 | memset(foo, 'a', sizeof(foo) -1); | |
974 | char_array_0(foo); | |
975 | ||
976 | test_path_find_last_component_one(foo, false, NULL, -EINVAL); | |
977 | test_path_find_last_component_one(foo, true, NULL, -EINVAL); | |
978 | ||
979 | hoge = strjoin(foo, "/a/b/c/"); | |
980 | assert_se(hoge); | |
981 | ||
982 | test_path_find_last_component_one(hoge, false, STRV_MAKE("c", "b", "a"), -EINVAL); | |
983 | test_path_find_last_component_one(hoge, true, STRV_MAKE("c", "b", "a"), -EINVAL); | |
984 | } | |
985 | ||
986 | TEST(last_path_component) { | |
987 | assert_se(last_path_component(NULL) == NULL); | |
988 | assert_se(streq(last_path_component("a/b/c"), "c")); | |
989 | assert_se(streq(last_path_component("a/b/c/"), "c/")); | |
990 | assert_se(streq(last_path_component("/"), "/")); | |
991 | assert_se(streq(last_path_component("//"), "/")); | |
992 | assert_se(streq(last_path_component("///"), "/")); | |
993 | assert_se(streq(last_path_component("."), ".")); | |
994 | assert_se(streq(last_path_component("./."), ".")); | |
995 | assert_se(streq(last_path_component("././"), "./")); | |
996 | assert_se(streq(last_path_component("././/"), ".//")); | |
997 | assert_se(streq(last_path_component("/foo/a"), "a")); | |
998 | assert_se(streq(last_path_component("/foo/a/"), "a/")); | |
999 | assert_se(streq(last_path_component(""), "")); | |
1000 | assert_se(streq(last_path_component("a"), "a")); | |
1001 | assert_se(streq(last_path_component("a/"), "a/")); | |
1002 | assert_se(streq(last_path_component("/a"), "a")); | |
1003 | assert_se(streq(last_path_component("/a/"), "a/")); | |
1004 | } | |
1005 | ||
1006 | static void test_path_extract_filename_one(const char *input, const char *output, int ret) { | |
1007 | _cleanup_free_ char *k = NULL; | |
1008 | int r; | |
1009 | ||
1010 | r = path_extract_filename(input, &k); | |
1011 | log_info("%s → %s/%s [expected: %s/%s]", | |
1012 | strnull(input), | |
1013 | strnull(k), r < 0 ? STRERROR(r) : "-", | |
1014 | strnull(output), ret < 0 ? STRERROR(ret) : "-"); | |
1015 | assert_se(streq_ptr(k, output)); | |
1016 | assert_se(r == ret); | |
1017 | } | |
1018 | ||
1019 | TEST(path_extract_filename) { | |
1020 | test_path_extract_filename_one(NULL, NULL, -EINVAL); | |
1021 | test_path_extract_filename_one("a/b/c", "c", 0); | |
1022 | test_path_extract_filename_one("a/b/c/", "c", O_DIRECTORY); | |
1023 | test_path_extract_filename_one("/", NULL, -EADDRNOTAVAIL); | |
1024 | test_path_extract_filename_one("//", NULL, -EADDRNOTAVAIL); | |
1025 | test_path_extract_filename_one("///", NULL, -EADDRNOTAVAIL); | |
1026 | test_path_extract_filename_one("/.", NULL, -EADDRNOTAVAIL); | |
1027 | test_path_extract_filename_one(".", NULL, -EADDRNOTAVAIL); | |
1028 | test_path_extract_filename_one("./", NULL, -EADDRNOTAVAIL); | |
1029 | test_path_extract_filename_one("./.", NULL, -EADDRNOTAVAIL); | |
1030 | test_path_extract_filename_one("././", NULL, -EADDRNOTAVAIL); | |
1031 | test_path_extract_filename_one("././/", NULL, -EADDRNOTAVAIL); | |
1032 | test_path_extract_filename_one("/foo/a", "a", 0); | |
1033 | test_path_extract_filename_one("/foo/a/", "a", O_DIRECTORY); | |
1034 | test_path_extract_filename_one("", NULL, -EINVAL); | |
1035 | test_path_extract_filename_one("a", "a", 0); | |
1036 | test_path_extract_filename_one("a/", "a", O_DIRECTORY); | |
1037 | test_path_extract_filename_one("a/././//.", "a", O_DIRECTORY); | |
1038 | test_path_extract_filename_one("/a", "a", 0); | |
1039 | test_path_extract_filename_one("/a/", "a", O_DIRECTORY); | |
1040 | test_path_extract_filename_one("/a//./.", "a", O_DIRECTORY); | |
1041 | test_path_extract_filename_one("/////////////a/////////////", "a", O_DIRECTORY); | |
1042 | test_path_extract_filename_one("//./a/.///b./././.c//./d//.", "d", O_DIRECTORY); | |
1043 | test_path_extract_filename_one("xx/.", "xx", O_DIRECTORY); | |
1044 | test_path_extract_filename_one("xx/..", NULL, -EINVAL); | |
1045 | test_path_extract_filename_one("..", NULL, -EINVAL); | |
1046 | test_path_extract_filename_one("/..", NULL, -EINVAL); | |
1047 | test_path_extract_filename_one("../", NULL, -EINVAL); | |
1048 | } | |
1049 | ||
1050 | static void test_path_extract_directory_one(const char *input, const char *output, int ret) { | |
1051 | _cleanup_free_ char *k = NULL; | |
1052 | int r; | |
1053 | ||
1054 | r = path_extract_directory(input, &k); | |
1055 | log_info("%s → %s/%s [expected: %s/%s]", | |
1056 | strnull(input), | |
1057 | strnull(k), r < 0 ? STRERROR(r) : "-", | |
1058 | strnull(output), STRERROR(ret)); | |
1059 | assert_se(streq_ptr(k, output)); | |
1060 | assert_se(r == ret); | |
1061 | ||
1062 | /* Extra safety check: let's make sure that if we split out the filename too (and it works) the | |
1063 | * joined parts are identical to the original again */ | |
1064 | if (r >= 0) { | |
1065 | _cleanup_free_ char *f = NULL; | |
1066 | ||
1067 | r = path_extract_filename(input, &f); | |
1068 | if (r >= 0) { | |
1069 | _cleanup_free_ char *j = NULL; | |
1070 | ||
1071 | assert_se(j = path_join(k, f)); | |
1072 | assert_se(path_equal(input, j)); | |
1073 | } | |
1074 | } | |
1075 | } | |
1076 | ||
1077 | TEST(path_extract_directory) { | |
1078 | test_path_extract_directory_one(NULL, NULL, -EINVAL); | |
1079 | test_path_extract_directory_one("a/b/c", "a/b", 0); | |
1080 | test_path_extract_directory_one("a/b/c/", "a/b", 0); | |
1081 | test_path_extract_directory_one("/", NULL, -EADDRNOTAVAIL); | |
1082 | test_path_extract_directory_one("//", NULL, -EADDRNOTAVAIL); | |
1083 | test_path_extract_directory_one("///", NULL, -EADDRNOTAVAIL); | |
1084 | test_path_extract_directory_one("/.", NULL, -EADDRNOTAVAIL); | |
1085 | test_path_extract_directory_one(".", NULL, -EADDRNOTAVAIL); | |
1086 | test_path_extract_directory_one("./", NULL, -EADDRNOTAVAIL); | |
1087 | test_path_extract_directory_one("./.", NULL, -EADDRNOTAVAIL); | |
1088 | test_path_extract_directory_one("././", NULL, -EADDRNOTAVAIL); | |
1089 | test_path_extract_directory_one("././/", NULL, -EADDRNOTAVAIL); | |
1090 | test_path_extract_directory_one("/foo/a", "/foo", 0); | |
1091 | test_path_extract_directory_one("/foo/a/", "/foo", 0); | |
1092 | test_path_extract_directory_one("", NULL, -EINVAL); | |
1093 | test_path_extract_directory_one("a", NULL, -EDESTADDRREQ); | |
1094 | test_path_extract_directory_one("a/", NULL, -EDESTADDRREQ); | |
1095 | test_path_extract_directory_one("a/././//.", NULL, -EDESTADDRREQ); | |
1096 | test_path_extract_directory_one("/a", "/", 0); | |
1097 | test_path_extract_directory_one("/a/", "/", 0); | |
1098 | test_path_extract_directory_one("/a//./.", "/", 0); | |
1099 | test_path_extract_directory_one("/////////////a/////////////", "/", 0); | |
1100 | test_path_extract_directory_one("//./a/.///b./././.c//./d//.", "/a/b./.c", 0); | |
1101 | test_path_extract_directory_one("xx/.", NULL, -EDESTADDRREQ); | |
1102 | test_path_extract_directory_one("xx/..", NULL, -EINVAL); | |
1103 | test_path_extract_directory_one("..", NULL, -EINVAL); | |
1104 | test_path_extract_directory_one("/..", NULL, -EINVAL); | |
1105 | test_path_extract_directory_one("../", NULL, -EINVAL); | |
1106 | } | |
1107 | ||
1108 | TEST(filename_is_valid) { | |
1109 | char foo[NAME_MAX+2]; | |
1110 | ||
1111 | assert_se(!filename_is_valid("")); | |
1112 | assert_se(!filename_is_valid("/bar/foo")); | |
1113 | assert_se(!filename_is_valid("/")); | |
1114 | assert_se(!filename_is_valid(".")); | |
1115 | assert_se(!filename_is_valid("..")); | |
1116 | assert_se(!filename_is_valid("bar/foo")); | |
1117 | assert_se(!filename_is_valid("bar/foo/")); | |
1118 | assert_se(!filename_is_valid("bar//")); | |
1119 | ||
1120 | memset(foo, 'a', sizeof(foo) - 1); | |
1121 | char_array_0(foo); | |
1122 | ||
1123 | assert_se(!filename_is_valid(foo)); | |
1124 | ||
1125 | assert_se(filename_is_valid("foo_bar-333")); | |
1126 | assert_se(filename_is_valid("o.o")); | |
1127 | } | |
1128 | ||
1129 | static void test_path_is_valid_and_safe_one(const char *p, bool ret) { | |
1130 | log_debug("/* %s(\"%s\") */", __func__, strnull(p)); | |
1131 | ||
1132 | assert_se(path_is_valid(p) == ret); | |
1133 | if (ret) | |
1134 | ret = !streq(p, "..") && | |
1135 | !startswith(p, "../") && | |
1136 | !endswith(p, "/..") && | |
1137 | !strstr(p, "/../"); | |
1138 | assert_se(path_is_safe(p) == ret); | |
1139 | } | |
1140 | ||
1141 | TEST(path_is_valid_and_safe) { | |
1142 | char foo[PATH_MAX+2]; | |
1143 | const char *c; | |
1144 | ||
1145 | test_path_is_valid_and_safe_one("", false); | |
1146 | test_path_is_valid_and_safe_one("/bar/foo", true); | |
1147 | test_path_is_valid_and_safe_one("/bar/foo/", true); | |
1148 | test_path_is_valid_and_safe_one("/bar/foo/", true); | |
1149 | test_path_is_valid_and_safe_one("//bar//foo//", true); | |
1150 | test_path_is_valid_and_safe_one("/", true); | |
1151 | test_path_is_valid_and_safe_one("/////", true); | |
1152 | test_path_is_valid_and_safe_one("/////.///.////...///..//.", true); | |
1153 | test_path_is_valid_and_safe_one(".", true); | |
1154 | test_path_is_valid_and_safe_one("..", true); | |
1155 | test_path_is_valid_and_safe_one("bar/foo", true); | |
1156 | test_path_is_valid_and_safe_one("bar/foo/", true); | |
1157 | test_path_is_valid_and_safe_one("bar//", true); | |
1158 | ||
1159 | memset(foo, 'a', sizeof(foo) -1); | |
1160 | char_array_0(foo); | |
1161 | ||
1162 | test_path_is_valid_and_safe_one(foo, false); | |
1163 | ||
1164 | c = strjoina("/xxx/", foo, "/yyy"); | |
1165 | test_path_is_valid_and_safe_one(c, false); | |
1166 | ||
1167 | test_path_is_valid_and_safe_one("foo_bar-333", true); | |
1168 | test_path_is_valid_and_safe_one("o.o", true); | |
1169 | } | |
1170 | ||
1171 | TEST(hidden_or_backup_file) { | |
1172 | assert_se(hidden_or_backup_file(".hidden")); | |
1173 | assert_se(hidden_or_backup_file("..hidden")); | |
1174 | assert_se(!hidden_or_backup_file("hidden.")); | |
1175 | ||
1176 | assert_se(hidden_or_backup_file("backup~")); | |
1177 | assert_se(hidden_or_backup_file(".backup~")); | |
1178 | ||
1179 | assert_se(hidden_or_backup_file("lost+found")); | |
1180 | assert_se(hidden_or_backup_file("aquota.user")); | |
1181 | assert_se(hidden_or_backup_file("aquota.group")); | |
1182 | ||
1183 | assert_se(hidden_or_backup_file("test.rpmnew")); | |
1184 | assert_se(hidden_or_backup_file("test.dpkg-old")); | |
1185 | assert_se(hidden_or_backup_file("test.dpkg-remove")); | |
1186 | assert_se(hidden_or_backup_file("test.swp")); | |
1187 | ||
1188 | assert_se(!hidden_or_backup_file("test.rpmnew.")); | |
1189 | assert_se(!hidden_or_backup_file("test.dpkg-old.foo")); | |
1190 | } | |
1191 | ||
1192 | TEST(skip_dev_prefix) { | |
1193 | assert_se(streq(skip_dev_prefix("/"), "/")); | |
1194 | assert_se(streq(skip_dev_prefix("/dev"), "")); | |
1195 | assert_se(streq(skip_dev_prefix("/dev/"), "")); | |
1196 | assert_se(streq(skip_dev_prefix("/dev/foo"), "foo")); | |
1197 | assert_se(streq(skip_dev_prefix("/dev/foo/bar"), "foo/bar")); | |
1198 | assert_se(streq(skip_dev_prefix("//dev"), "")); | |
1199 | assert_se(streq(skip_dev_prefix("//dev//"), "")); | |
1200 | assert_se(streq(skip_dev_prefix("/dev///foo"), "foo")); | |
1201 | assert_se(streq(skip_dev_prefix("///dev///foo///bar"), "foo///bar")); | |
1202 | assert_se(streq(skip_dev_prefix("//foo"), "//foo")); | |
1203 | assert_se(streq(skip_dev_prefix("foo"), "foo")); | |
1204 | } | |
1205 | ||
1206 | TEST(empty_or_root) { | |
1207 | assert_se(empty_or_root(NULL)); | |
1208 | assert_se(empty_or_root("")); | |
1209 | assert_se(empty_or_root("/")); | |
1210 | assert_se(empty_or_root("//")); | |
1211 | assert_se(empty_or_root("///")); | |
1212 | assert_se(empty_or_root("/////////////////")); | |
1213 | assert_se(!empty_or_root("xxx")); | |
1214 | assert_se(!empty_or_root("/xxx")); | |
1215 | assert_se(!empty_or_root("/xxx/")); | |
1216 | assert_se(!empty_or_root("//yy//")); | |
1217 | } | |
1218 | ||
1219 | TEST(path_startswith_set) { | |
1220 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/foo/bar", "/zzz"), "")); | |
1221 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/foo/", "/zzz"), "bar")); | |
1222 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/foo", "/zzz"), "bar")); | |
1223 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/", "/zzz"), "foo/bar")); | |
1224 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "", "/zzz"), NULL)); | |
1225 | ||
1226 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/foo/bar", "/zzz"), NULL)); | |
1227 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/foo/", "/zzz"), "bar2")); | |
1228 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/foo", "/zzz"), "bar2")); | |
1229 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/", "/zzz"), "foo/bar2")); | |
1230 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "", "/zzz"), NULL)); | |
1231 | ||
1232 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/foo/bar", "/zzz"), NULL)); | |
1233 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/foo/", "/zzz"), NULL)); | |
1234 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/foo", "/zzz"), NULL)); | |
1235 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/", "/zzz"), "foo2/bar")); | |
1236 | assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "", "/zzz"), NULL)); | |
1237 | } | |
1238 | ||
1239 | TEST(path_startswith_strv) { | |
1240 | assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/foo/bar", "/zzz")), "")); | |
1241 | assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/foo/", "/zzz")), "bar")); | |
1242 | assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/foo", "/zzz")), "bar")); | |
1243 | assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/", "/zzz")), "foo/bar")); | |
1244 | assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "", "/zzz")), NULL)); | |
1245 | ||
1246 | assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "/foo/bar", "/zzz")), NULL)); | |
1247 | assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "/foo/", "/zzz")), "bar2")); | |
1248 | assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "/foo", "/zzz")), "bar2")); | |
1249 | assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "/", "/zzz")), "foo/bar2")); | |
1250 | assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "", "/zzz")), NULL)); | |
1251 | ||
1252 | assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "/foo/bar", "/zzz")), NULL)); | |
1253 | assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "/foo/", "/zzz")), NULL)); | |
1254 | assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "/foo", "/zzz")), NULL)); | |
1255 | assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "/", "/zzz")), "foo2/bar")); | |
1256 | assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "", "/zzz")), NULL)); | |
1257 | } | |
1258 | ||
1259 | static void test_path_glob_can_match_one(const char *pattern, const char *prefix, const char *expected) { | |
1260 | _cleanup_free_ char *result = NULL; | |
1261 | ||
1262 | log_debug("%s(%s, %s, %s)", __func__, pattern, prefix, strnull(expected)); | |
1263 | ||
1264 | assert_se(path_glob_can_match(pattern, prefix, &result) == !!expected); | |
1265 | assert_se(streq_ptr(result, expected)); | |
1266 | } | |
1267 | ||
1268 | TEST(path_glob_can_match) { | |
1269 | test_path_glob_can_match_one("/foo/hoge/aaa", "/foo/hoge/aaa/bbb", NULL); | |
1270 | test_path_glob_can_match_one("/foo/hoge/aaa", "/foo/hoge/aaa", "/foo/hoge/aaa"); | |
1271 | test_path_glob_can_match_one("/foo/hoge/aaa", "/foo/hoge", "/foo/hoge/aaa"); | |
1272 | test_path_glob_can_match_one("/foo/hoge/aaa", "/foo", "/foo/hoge/aaa"); | |
1273 | test_path_glob_can_match_one("/foo/hoge/aaa", "/", "/foo/hoge/aaa"); | |
1274 | ||
1275 | test_path_glob_can_match_one("/foo/*/aaa", "/foo/hoge/aaa/bbb", NULL); | |
1276 | test_path_glob_can_match_one("/foo/*/aaa", "/foo/hoge/aaa", "/foo/hoge/aaa"); | |
1277 | test_path_glob_can_match_one("/foo/*/aaa", "/foo/hoge", "/foo/hoge/aaa"); | |
1278 | test_path_glob_can_match_one("/foo/*/aaa", "/foo", "/foo/*/aaa"); | |
1279 | test_path_glob_can_match_one("/foo/*/aaa", "/", "/foo/*/aaa"); | |
1280 | ||
1281 | test_path_glob_can_match_one("/foo/*/*/aaa", "/foo/xxx/yyy/aaa/bbb", NULL); | |
1282 | test_path_glob_can_match_one("/foo/*/*/aaa", "/foo/xxx/yyy/aaa", "/foo/xxx/yyy/aaa"); | |
1283 | test_path_glob_can_match_one("/foo/*/*/aaa", "/foo/xxx/yyy", "/foo/xxx/yyy/aaa"); | |
1284 | test_path_glob_can_match_one("/foo/*/*/aaa", "/foo/xxx", "/foo/xxx/*/aaa"); | |
1285 | test_path_glob_can_match_one("/foo/*/*/aaa", "/foo", "/foo/*/*/aaa"); | |
1286 | test_path_glob_can_match_one("/foo/*/*/aaa", "/", "/foo/*/*/aaa"); | |
1287 | ||
1288 | test_path_glob_can_match_one("/foo/*/aaa/*", "/foo/xxx/aaa/bbb/ccc", NULL); | |
1289 | test_path_glob_can_match_one("/foo/*/aaa/*", "/foo/xxx/aaa/bbb", "/foo/xxx/aaa/bbb"); | |
1290 | test_path_glob_can_match_one("/foo/*/aaa/*", "/foo/xxx/ccc", NULL); | |
1291 | test_path_glob_can_match_one("/foo/*/aaa/*", "/foo/xxx/aaa", "/foo/xxx/aaa/*"); | |
1292 | test_path_glob_can_match_one("/foo/*/aaa/*", "/foo/xxx", "/foo/xxx/aaa/*"); | |
1293 | test_path_glob_can_match_one("/foo/*/aaa/*", "/foo", "/foo/*/aaa/*"); | |
1294 | test_path_glob_can_match_one("/foo/*/aaa/*", "/", "/foo/*/aaa/*"); | |
1295 | } | |
1296 | ||
1297 | TEST(print_MAX) { | |
1298 | log_info("PATH_MAX=%zu\n" | |
1299 | "FILENAME_MAX=%zu\n" | |
1300 | "NAME_MAX=%zu", | |
1301 | (size_t) PATH_MAX, | |
1302 | (size_t) FILENAME_MAX, | |
1303 | (size_t) NAME_MAX); | |
1304 | ||
1305 | assert_cc(FILENAME_MAX == PATH_MAX); | |
1306 | } | |
1307 | ||
1308 | DEFINE_TEST_MAIN(LOG_DEBUG); |