Debian Codegraph @

src/test/test-path-util.c

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