1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
6 #include "alloc-util.h"
11 #include "path-util.h"
12 #include "process-util.h"
14 #include "stat-util.h"
15 #include "string-util.h"
18 #include "tmpfile-util.h"
21 log_info("DEFAULT_PATH=%s", DEFAULT_PATH
);
22 log_info("DEFAULT_USER_PATH=%s", DEFAULT_USER_PATH
);
26 assert_se(path_is_absolute("/"));
27 assert_se(!path_is_absolute("./"));
29 assert_se(is_path("/dir"));
30 assert_se(is_path("a/b"));
31 assert_se(!is_path("."));
33 assert_se(streq(basename("./aa/bb/../file.da."), "file.da."));
34 assert_se(streq(basename("/aa///.file"), ".file"));
35 assert_se(streq(basename("/aa///file..."), "file..."));
36 assert_se(streq(basename("file.../"), ""));
38 assert_se(PATH_IN_SET("/bin", "/", "/bin", "/foo"));
39 assert_se(PATH_IN_SET("/bin", "/bin"));
40 assert_se(PATH_IN_SET("/bin", "/foo/bar", "/bin"));
41 assert_se(PATH_IN_SET("/", "/", "/", "/foo/bar"));
42 assert_se(!PATH_IN_SET("/", "/abc", "/def"));
44 assert_se(path_equal_ptr(NULL
, NULL
));
45 assert_se(path_equal_ptr("/a", "/a"));
46 assert_se(!path_equal_ptr("/a", "/b"));
47 assert_se(!path_equal_ptr("/a", NULL
));
48 assert_se(!path_equal_ptr(NULL
, "/a"));
51 static void test_path_simplify_one(const char *in
, const char *out
) {
56 log_debug("/* test_path_simplify(%s) → %s (expected: %s) */", in
, p
, out
);
57 assert_se(streq(p
, out
));
61 _cleanup_free_
char *hoge
= NULL
, *hoge_out
= NULL
;
62 char foo
[NAME_MAX
* 2];
64 test_path_simplify_one("", "");
65 test_path_simplify_one("aaa/bbb////ccc", "aaa/bbb/ccc");
66 test_path_simplify_one("//aaa/.////ccc", "/aaa/ccc");
67 test_path_simplify_one("///", "/");
68 test_path_simplify_one("///.//", "/");
69 test_path_simplify_one("///.//.///", "/");
70 test_path_simplify_one("////.././///../.", "/../..");
71 test_path_simplify_one(".", ".");
72 test_path_simplify_one("./", ".");
73 test_path_simplify_one(".///.//./.", ".");
74 test_path_simplify_one(".///.//././/", ".");
75 test_path_simplify_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.",
76 "/aaa/.bbb/../c./d.dd/..eeee");
77 test_path_simplify_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/..",
78 "/aaa/.bbb/../c./d.dd/..eeee/..");
79 test_path_simplify_one(".//./aaa///.//./.bbb/..///c.//d.dd///..eeee/..",
80 "aaa/.bbb/../c./d.dd/..eeee/..");
81 test_path_simplify_one("..//./aaa///.//./.bbb/..///c.//d.dd///..eeee/..",
82 "../aaa/.bbb/../c./d.dd/..eeee/..");
84 memset(foo
, 'a', sizeof(foo
) -1);
87 test_path_simplify_one(foo
, foo
);
89 hoge
= strjoin("/", foo
);
91 test_path_simplify_one(hoge
, hoge
);
94 hoge
= strjoin("a////.//././//./b///././/./c/////././//./", foo
, "//.//////d/e/.//f/");
97 hoge_out
= strjoin("a/b/c/", foo
, "//.//////d/e/.//f/");
100 test_path_simplify_one(hoge
, hoge_out
);
103 static void test_path_compare_one(const char *a
, const char *b
, int expected
) {
106 assert_se(path_compare(a
, a
) == 0);
107 assert_se(path_compare(b
, b
) == 0);
109 r
= path_compare(a
, b
);
110 assert_se((r
> 0) == (expected
> 0) && (r
< 0) == (expected
< 0));
111 r
= path_compare(b
, a
);
112 assert_se((r
< 0) == (expected
> 0) && (r
> 0) == (expected
< 0));
114 assert_se(path_equal(a
, a
) == 1);
115 assert_se(path_equal(b
, b
) == 1);
116 assert_se(path_equal(a
, b
) == (expected
== 0));
117 assert_se(path_equal(b
, a
) == (expected
== 0));
121 test_path_compare_one("/goo", "/goo", 0);
122 test_path_compare_one("/goo", "/goo", 0);
123 test_path_compare_one("//goo", "/goo", 0);
124 test_path_compare_one("//goo/////", "/goo", 0);
125 test_path_compare_one("goo/////", "goo", 0);
126 test_path_compare_one("/goo/boo", "/goo//boo", 0);
127 test_path_compare_one("//goo/boo", "/goo/boo//", 0);
128 test_path_compare_one("//goo/././//./boo//././//", "/goo/boo//.", 0);
129 test_path_compare_one("/.", "//.///", 0);
130 test_path_compare_one("/x", "x/", 1);
131 test_path_compare_one("x/", "/", -1);
132 test_path_compare_one("/x/./y", "x/y", 1);
133 test_path_compare_one("/x/./y", "/x/y", 0);
134 test_path_compare_one("/x/./././y", "/x/y/././.", 0);
135 test_path_compare_one("./x/./././y", "./x/y/././.", 0);
136 test_path_compare_one(".", "./.", 0);
137 test_path_compare_one(".", "././.", 0);
138 test_path_compare_one("./..", ".", 1);
139 test_path_compare_one("x/.y", "x/y", -1);
140 test_path_compare_one("foo", "/foo", -1);
141 test_path_compare_one("/foo", "/foo/bar", -1);
142 test_path_compare_one("/foo/aaa", "/foo/b", -1);
143 test_path_compare_one("/foo/aaa", "/foo/b/a", -1);
144 test_path_compare_one("/foo/a", "/foo/aaa", -1);
145 test_path_compare_one("/foo/a/b", "/foo/aaa", -1);
148 static void test_path_compare_filename_one(const char *a
, const char *b
, int expected
) {
151 assert_se(path_compare_filename(a
, a
) == 0);
152 assert_se(path_compare_filename(b
, b
) == 0);
154 r
= path_compare_filename(a
, b
);
155 assert_se((r
> 0) == (expected
> 0) && (r
< 0) == (expected
< 0));
156 r
= path_compare_filename(b
, a
);
157 assert_se((r
< 0) == (expected
> 0) && (r
> 0) == (expected
< 0));
159 assert_se(path_equal_filename(a
, a
) == 1);
160 assert_se(path_equal_filename(b
, b
) == 1);
161 assert_se(path_equal_filename(a
, b
) == (expected
== 0));
162 assert_se(path_equal_filename(b
, a
) == (expected
== 0));
165 TEST(path_compare_filename
) {
166 test_path_compare_filename_one("/goo", "/goo", 0);
167 test_path_compare_filename_one("/goo", "/goo", 0);
168 test_path_compare_filename_one("//goo", "/goo", 0);
169 test_path_compare_filename_one("//goo/////", "/goo", 0);
170 test_path_compare_filename_one("goo/////", "goo", 0);
171 test_path_compare_filename_one("/goo/boo", "/goo//boo", 0);
172 test_path_compare_filename_one("//goo/boo", "/goo/boo//", 0);
173 test_path_compare_filename_one("//goo/././//./boo//././//", "/goo/boo//.", 0);
174 test_path_compare_filename_one("/.", "//.///", -1);
175 test_path_compare_filename_one("/x", "x/", 0);
176 test_path_compare_filename_one("x/", "/", 1);
177 test_path_compare_filename_one("/x/./y", "x/y", 0);
178 test_path_compare_filename_one("/x/./y", "/x/y", 0);
179 test_path_compare_filename_one("/x/./././y", "/x/y/././.", 0);
180 test_path_compare_filename_one("./x/./././y", "./x/y/././.", 0);
181 test_path_compare_filename_one(".", "./.", -1);
182 test_path_compare_filename_one(".", "././.", -1);
183 test_path_compare_filename_one("./..", ".", 1);
184 test_path_compare_filename_one("x/.y", "x/y", -1);
185 test_path_compare_filename_one("foo", "/foo", 0);
186 test_path_compare_filename_one("/foo", "/foo/bar", 1);
187 test_path_compare_filename_one("/foo/aaa", "/foo/b", -1);
188 test_path_compare_filename_one("/foo/aaa", "/foo/b/a", 1);
189 test_path_compare_filename_one("/foo/a", "/foo/aaa", -1);
190 test_path_compare_filename_one("/foo/a/b", "/foo/aaa", 1);
191 test_path_compare_filename_one("/a/c", "/b/c", 0);
192 test_path_compare_filename_one("/a", "/a", 0);
193 test_path_compare_filename_one("/a/b", "/a/c", -1);
194 test_path_compare_filename_one("/b", "/c", -1);
197 TEST(path_equal_root
) {
198 /* Nail down the details of how path_equal("/", ...) works. */
200 assert_se(path_equal("/", "/"));
201 assert_se(path_equal("/", "//"));
203 assert_se(path_equal("/", "/./"));
204 assert_se(!path_equal("/", "/../"));
206 assert_se(!path_equal("/", "/.../"));
208 /* Make sure that files_same works as expected. */
210 assert_se(files_same("/", "/", 0) > 0);
211 assert_se(files_same("/", "/", AT_SYMLINK_NOFOLLOW
) > 0);
212 assert_se(files_same("/", "//", 0) > 0);
213 assert_se(files_same("/", "//", AT_SYMLINK_NOFOLLOW
) > 0);
215 assert_se(files_same("/", "/./", 0) > 0);
216 assert_se(files_same("/", "/./", AT_SYMLINK_NOFOLLOW
) > 0);
217 assert_se(files_same("/", "/../", 0) > 0);
218 assert_se(files_same("/", "/../", AT_SYMLINK_NOFOLLOW
) > 0);
220 assert_se(files_same("/", "/.../", 0) == -ENOENT
);
221 assert_se(files_same("/", "/.../", AT_SYMLINK_NOFOLLOW
) == -ENOENT
);
223 /* The same for path_equal_or_files_same. */
225 assert_se(path_equal_or_files_same("/", "/", 0));
226 assert_se(path_equal_or_files_same("/", "/", AT_SYMLINK_NOFOLLOW
));
227 assert_se(path_equal_or_files_same("/", "//", 0));
228 assert_se(path_equal_or_files_same("/", "//", AT_SYMLINK_NOFOLLOW
));
230 assert_se(path_equal_or_files_same("/", "/./", 0));
231 assert_se(path_equal_or_files_same("/", "/./", AT_SYMLINK_NOFOLLOW
));
232 assert_se(path_equal_or_files_same("/", "/../", 0));
233 assert_se(path_equal_or_files_same("/", "/../", AT_SYMLINK_NOFOLLOW
));
235 assert_se(!path_equal_or_files_same("/", "/.../", 0));
236 assert_se(!path_equal_or_files_same("/", "/.../", AT_SYMLINK_NOFOLLOW
));
239 TEST(find_executable_full
) {
241 char* test_file_name
;
242 _cleanup_close_
int fd
= -EBADF
;
243 char fn
[] = "/tmp/test-XXXXXX";
245 assert_se(find_executable_full("sh", NULL
, NULL
, true, &p
, NULL
) == 0);
247 assert_se(streq(basename(p
), "sh"));
250 assert_se(find_executable_full("sh", NULL
, NULL
, false, &p
, NULL
) == 0);
252 assert_se(streq(basename(p
), "sh"));
255 _cleanup_free_
char *oldpath
= NULL
;
258 assert_se(oldpath
= strdup(p
));
260 assert_se(unsetenv("PATH") == 0);
262 assert_se(find_executable_full("sh", NULL
, NULL
, true, &p
, NULL
) == 0);
264 assert_se(streq(basename(p
), "sh"));
267 assert_se(find_executable_full("sh", NULL
, NULL
, false, &p
, NULL
) == 0);
269 assert_se(streq(basename(p
), "sh"));
273 assert_se(setenv("PATH", oldpath
, true) >= 0);
275 assert_se((fd
= mkostemp_safe(fn
)) >= 0);
276 assert_se(fchmod(fd
, 0755) >= 0);
278 test_file_name
= basename(fn
);
280 assert_se(find_executable_full(test_file_name
, NULL
, STRV_MAKE("/doesnotexist", "/tmp", "/bin"), false, &p
, NULL
) == 0);
282 assert_se(streq(p
, fn
));
286 assert_se(find_executable_full(test_file_name
, NULL
, STRV_MAKE("/doesnotexist", "/tmp", "/bin"), false, &p
, NULL
) == -ENOENT
);
289 TEST(find_executable
) {
292 assert_se(find_executable("/bin/sh", &p
) == 0);
294 assert_se(path_equal(p
, "/bin/sh"));
297 assert_se(find_executable(saved_argv
[0], &p
) == 0);
299 assert_se(endswith(p
, "/test-path-util"));
300 assert_se(path_is_absolute(p
));
303 assert_se(find_executable("sh", &p
) == 0);
305 assert_se(endswith(p
, "/sh"));
306 assert_se(path_is_absolute(p
));
309 assert_se(find_executable("/bin/touch", &p
) == 0);
310 assert_se(streq(p
, "/bin/touch"));
313 assert_se(find_executable("touch", &p
) == 0);
314 assert_se(path_is_absolute(p
));
315 assert_se(streq(basename(p
), "touch"));
318 assert_se(find_executable("xxxx-xxxx", &p
) == -ENOENT
);
319 assert_se(find_executable("/some/dir/xxxx-xxxx", &p
) == -ENOENT
);
320 assert_se(find_executable("/proc/filesystems", &p
) == -EACCES
);
323 static void test_find_executable_exec_one(const char *path
) {
324 _cleanup_free_
char *t
= NULL
;
325 _cleanup_close_
int fd
= -EBADF
;
329 r
= find_executable_full(path
, NULL
, NULL
, false, &t
, &fd
);
331 log_info_errno(r
, "%s: %s → %s: %d/%m", __func__
, path
, t
?: "-", fd
);
333 assert_se(fd
> STDERR_FILENO
);
334 assert_se(path_is_absolute(t
));
335 if (path_is_absolute(path
))
336 assert_se(streq(t
, path
));
341 r
= fexecve_or_execve(fd
, t
, STRV_MAKE(t
, "--version"), STRV_MAKE(NULL
));
342 log_error_errno(r
, "[f]execve: %m");
346 assert_se(wait_for_terminate_and_check(t
, pid
, WAIT_LOG
) == 0);
349 TEST(find_executable_exec
) {
350 test_find_executable_exec_one("touch");
351 test_find_executable_exec_one("/bin/touch");
353 _cleanup_free_
char *script
= NULL
;
354 assert_se(get_testdata_dir("test-path-util/script.sh", &script
) >= 0);
355 test_find_executable_exec_one(script
);
359 static const char* const values
[] = {
372 PATH_FOREACH_PREFIX_MORE(s
, "/a/b/c/d") {
373 log_error("---%s---", s
);
374 assert_se(streq(s
, values
[i
++]));
376 assert_se(values
[i
] == NULL
);
379 PATH_FOREACH_PREFIX(s
, "/a/b/c/d") {
380 log_error("---%s---", s
);
381 assert_se(streq(s
, values
[i
++]));
383 assert_se(values
[i
] == NULL
);
386 PATH_FOREACH_PREFIX_MORE(s
, "////a////b////c///d///////")
387 assert_se(streq(s
, values
[i
++]));
388 assert_se(values
[i
] == NULL
);
391 PATH_FOREACH_PREFIX(s
, "////a////b////c///d///////")
392 assert_se(streq(s
, values
[i
++]));
393 assert_se(values
[i
] == NULL
);
395 PATH_FOREACH_PREFIX(s
, "////")
396 assert_not_reached();
399 PATH_FOREACH_PREFIX_MORE(s
, "////") {
401 assert_se(streq(s
, ""));
406 PATH_FOREACH_PREFIX(s
, "")
407 assert_not_reached();
410 PATH_FOREACH_PREFIX_MORE(s
, "") {
412 assert_se(streq(s
, ""));
418 #define test_join(expected, ...) { \
419 _cleanup_free_ char *z = NULL; \
420 z = path_join(__VA_ARGS__); \
421 log_debug("got \"%s\", expected \"%s\"", z, expected); \
422 assert_se(streq(z, expected)); \
425 test_join("/root/a/b/c", "/root", "/a/b", "/c");
426 test_join("/root/a/b/c", "/root", "a/b", "c");
427 test_join("/root/a/b/c", "/root", "/a/b", "c");
428 test_join("/root/c", "/root", "/", "c");
429 test_join("/root/", "/root", "/", NULL
);
431 test_join("/a/b/c", "", "/a/b", "/c");
432 test_join("a/b/c", "", "a/b", "c");
433 test_join("/a/b/c", "", "/a/b", "c");
434 test_join("/c", "", "/", "c");
435 test_join("/", "", "/", NULL
);
437 test_join("/a/b/c", NULL
, "/a/b", "/c");
438 test_join("a/b/c", NULL
, "a/b", "c");
439 test_join("/a/b/c", NULL
, "/a/b", "c");
440 test_join("/c", NULL
, "/", "c");
441 test_join("/", NULL
, "/", NULL
);
443 test_join("", "", NULL
);
444 test_join("", NULL
, "");
445 test_join("", NULL
, NULL
);
447 test_join("foo/bar", "foo", "bar");
448 test_join("foo/bar", "", "foo", "bar");
449 test_join("foo/bar", NULL
, "foo", NULL
, "bar");
450 test_join("foo/bar", "", "foo", "", "bar", "");
451 test_join("foo/bar", "", "", "", "", "foo", "", "", "", "bar", "", "", "");
453 test_join("//foo///bar//", "", "/", "", "/foo/", "", "/", "", "/bar/", "", "/", "");
454 test_join("/foo/bar/", "/", "foo", "/", "bar", "/");
455 test_join("foo/bar/baz", "foo", "bar", "baz");
456 test_join("foo/bar/baz", "foo/", "bar", "/baz");
457 test_join("foo//bar//baz", "foo/", "/bar/", "/baz");
458 test_join("//foo////bar////baz//", "//foo/", "///bar/", "///baz//");
462 _cleanup_free_
char *p
= NULL
;
464 assert_se(path_extend(&p
, "foo", "bar", "baz") == p
);
465 assert_se(streq(p
, "foo/bar/baz"));
467 assert_se(path_extend(&p
, "foo", "bar", "baz") == p
);
468 assert_se(streq(p
, "foo/bar/baz/foo/bar/baz"));
471 assert_se(path_extend(&p
, "foo") == p
);
472 assert_se(streq(p
, "foo"));
474 assert_se(path_extend(&p
, "/foo") == p
);
475 assert_se(streq(p
, "foo/foo"));
476 assert_se(path_extend(&p
, "/waaaah/wahhh//") == p
);
477 assert_se(streq(p
, "foo/foo/waaaah/wahhh//")); /* path_extend() does not drop redundant slashes */
478 assert_se(path_extend(&p
, "/aaa/bbb/") == p
);
479 assert_se(streq(p
, "foo/foo/waaaah/wahhh///aaa/bbb/")); /* but not add an extra slash */
481 assert_se(free_and_strdup(&p
, "/") >= 0);
482 assert_se(path_extend(&p
, "foo") == p
);
483 assert_se(streq(p
, "/foo"));
487 /* Ensure we use a sane default for PATH. */
488 assert_se(unsetenv("PATH") == 0);
490 /* fsck.minix is provided by util-linux and will probably exist. */
491 assert_se(fsck_exists_for_fstype("minix") == 1);
493 assert_se(fsck_exists_for_fstype("AbCdE") == 0);
494 assert_se(fsck_exists_for_fstype("/../bin/") == 0);
497 TEST(path_prefix_root_cwd
) {
498 _cleanup_free_
char *cwd
= NULL
, *ret
= NULL
, *expected
= NULL
;
500 assert_se(safe_getcwd(&cwd
) >= 0);
502 assert_se(path_prefix_root_cwd("hoge", NULL
, &ret
) >= 0);
503 assert_se(expected
= path_join(cwd
, "hoge"));
504 assert_se(streq(ret
, expected
));
507 expected
= mfree(expected
);
509 assert_se(path_prefix_root_cwd("/hoge", NULL
, &ret
) >= 0);
510 assert_se(streq(ret
, "/hoge"));
514 assert_se(path_prefix_root_cwd("hoge", "/a/b//./c///", &ret
) >= 0);
515 assert_se(streq(ret
, "/a/b/c/hoge"));
519 assert_se(path_prefix_root_cwd("hoge", "a/b//./c///", &ret
) >= 0);
520 assert_se(expected
= path_join(cwd
, "a/b/c/hoge"));
521 assert_se(streq(ret
, expected
));
524 expected
= mfree(expected
);
526 assert_se(path_prefix_root_cwd("/../hoge/aaa/../././b", "/a/b//./c///", &ret
) >= 0);
527 assert_se(streq(ret
, "/a/b/c/../hoge/aaa/../././b"));
531 assert_se(path_prefix_root_cwd("/../hoge/aaa/../././b", "a/b//./c///", &ret
) >= 0);
532 assert_se(expected
= path_join(cwd
, "a/b/c/../hoge/aaa/../././b"));
533 assert_se(streq(ret
, expected
));
536 static void test_path_make_relative_one(const char *from
, const char *to
, const char *expected
) {
537 _cleanup_free_
char *z
= NULL
;
540 log_info("/* %s(%s, %s) */", __func__
, from
, to
);
542 r
= path_make_relative(from
, to
, &z
);
543 assert_se((r
>= 0) == !!expected
);
544 assert_se(streq_ptr(z
, expected
));
547 TEST(path_make_relative
) {
548 test_path_make_relative_one("some/relative/path", "/some/path", NULL
);
549 test_path_make_relative_one("/some/path", "some/relative/path", NULL
);
550 test_path_make_relative_one("/some/dotdot/../path", "/some/path", NULL
);
552 test_path_make_relative_one("/", "/", ".");
553 test_path_make_relative_one("/", "/some/path", "some/path");
554 test_path_make_relative_one("/some/path", "/some/path", ".");
555 test_path_make_relative_one("/some/path", "/some/path/in/subdir", "in/subdir");
556 test_path_make_relative_one("/some/path", "/", "../..");
557 test_path_make_relative_one("/some/path", "/some/other/path", "../other/path");
558 test_path_make_relative_one("/some/path/./dot", "/some/further/path", "../../further/path");
559 test_path_make_relative_one("//extra.//.//./.slashes//./won't////fo.ol///anybody//", "/././/extra././/.slashes////ar.e/.just/././.fine///", "../../../ar.e/.just/.fine");
562 static void test_path_make_relative_parent_one(const char *from
, const char *to
, const char *expected
) {
563 _cleanup_free_
char *z
= NULL
;
566 log_info("/* %s(%s, %s) */", __func__
, from
, to
);
568 r
= path_make_relative_parent(from
, to
, &z
);
569 assert_se((r
>= 0) == !!expected
);
570 assert_se(streq_ptr(z
, expected
));
573 TEST(path_make_relative_parent
) {
574 test_path_make_relative_parent_one("some/relative/path/hoge", "/some/path", NULL
);
575 test_path_make_relative_parent_one("/some/path/hoge", "some/relative/path", NULL
);
576 test_path_make_relative_parent_one("/some/dotdot/../path/hoge", "/some/path", NULL
);
577 test_path_make_relative_parent_one("/", "/aaa", NULL
);
579 test_path_make_relative_parent_one("/hoge", "/", ".");
580 test_path_make_relative_parent_one("/hoge", "/some/path", "some/path");
581 test_path_make_relative_parent_one("/some/path/hoge", "/some/path", ".");
582 test_path_make_relative_parent_one("/some/path/hoge", "/some/path/in/subdir", "in/subdir");
583 test_path_make_relative_parent_one("/some/path/hoge", "/", "../..");
584 test_path_make_relative_parent_one("/some/path/hoge", "/some/other/path", "../other/path");
585 test_path_make_relative_parent_one("/some/path/./dot/hoge", "/some/further/path", "../../further/path");
586 test_path_make_relative_parent_one("//extra.//.//./.slashes//./won't////fo.ol///anybody//hoge", "/././/extra././/.slashes////ar.e/.just/././.fine///", "../../../ar.e/.just/.fine");
589 TEST(path_strv_resolve
) {
590 char tmp_dir
[] = "/tmp/test-path-util-XXXXXX";
591 _cleanup_strv_free_
char **search_dirs
= NULL
;
592 _cleanup_strv_free_
char **absolute_dirs
= NULL
;
594 assert_se(mkdtemp(tmp_dir
) != NULL
);
596 search_dirs
= strv_new("/dir1", "/dir2", "/dir3");
597 assert_se(search_dirs
);
598 STRV_FOREACH(d
, search_dirs
) {
599 char *p
= path_join(tmp_dir
, *d
);
601 assert_se(strv_push(&absolute_dirs
, p
) == 0);
604 assert_se(mkdir(absolute_dirs
[0], 0700) == 0);
605 assert_se(mkdir(absolute_dirs
[1], 0700) == 0);
606 assert_se(symlink("dir2", absolute_dirs
[2]) == 0);
608 path_strv_resolve(search_dirs
, tmp_dir
);
609 assert_se(streq(search_dirs
[0], "/dir1"));
610 assert_se(streq(search_dirs
[1], "/dir2"));
611 assert_se(streq(search_dirs
[2], "/dir2"));
613 assert_se(rm_rf(tmp_dir
, REMOVE_ROOT
|REMOVE_PHYSICAL
) == 0);
616 static void test_path_startswith_one(const char *path
, const char *prefix
, const char *skipped
, const char *expected
) {
619 log_debug("/* %s(%s, %s) */", __func__
, path
, prefix
);
621 p
= path_startswith(path
, prefix
);
622 assert_se(streq_ptr(p
, expected
));
624 q
= strjoina(skipped
, p
);
625 assert_se(streq(q
, path
));
626 assert_se(p
== path
+ strlen(skipped
));
630 TEST(path_startswith
) {
631 test_path_startswith_one("/foo/bar/barfoo/", "/foo", "/foo/", "bar/barfoo/");
632 test_path_startswith_one("/foo/bar/barfoo/", "/foo/", "/foo/", "bar/barfoo/");
633 test_path_startswith_one("/foo/bar/barfoo/", "/", "/", "foo/bar/barfoo/");
634 test_path_startswith_one("/foo/bar/barfoo/", "////", "/", "foo/bar/barfoo/");
635 test_path_startswith_one("/foo/bar/barfoo/", "/foo//bar/////barfoo///", "/foo/bar/barfoo/", "");
636 test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/barfoo////", "/foo/bar/barfoo/", "");
637 test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar///barfoo/", "/foo/bar/barfoo/", "");
638 test_path_startswith_one("/foo/bar/barfoo/", "/foo////bar/barfoo/", "/foo/bar/barfoo/", "");
639 test_path_startswith_one("/foo/bar/barfoo/", "////foo/bar/barfoo/", "/foo/bar/barfoo/", "");
640 test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/barfoo", "/foo/bar/barfoo/", "");
642 test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/barfooa/", NULL
, NULL
);
643 test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/barfooa", NULL
, NULL
);
644 test_path_startswith_one("/foo/bar/barfoo/", "", NULL
, NULL
);
645 test_path_startswith_one("/foo/bar/barfoo/", "/bar/foo", NULL
, NULL
);
646 test_path_startswith_one("/foo/bar/barfoo/", "/f/b/b/", NULL
, NULL
);
647 test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/barfo", NULL
, NULL
);
648 test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/bar", NULL
, NULL
);
649 test_path_startswith_one("/foo/bar/barfoo/", "/fo", NULL
, NULL
);
652 static void test_prefix_root_one(const char *r
, const char *p
, const char *expected
) {
653 _cleanup_free_
char *s
= NULL
;
656 assert_se(s
= path_join(r
, p
));
657 assert_se(path_equal_ptr(s
, expected
));
659 t
= prefix_roota(r
, p
);
661 assert_se(path_equal_ptr(t
, expected
));
665 test_prefix_root_one("/", "/foo", "/foo");
666 test_prefix_root_one(NULL
, "/foo", "/foo");
667 test_prefix_root_one("", "/foo", "/foo");
668 test_prefix_root_one("///", "/foo", "/foo");
669 test_prefix_root_one("/", "////foo", "/foo");
670 test_prefix_root_one(NULL
, "////foo", "/foo");
671 test_prefix_root_one("/", "foo", "/foo");
672 test_prefix_root_one("", "foo", "foo");
673 test_prefix_root_one(NULL
, "foo", "foo");
675 test_prefix_root_one("/foo", "/bar", "/foo/bar");
676 test_prefix_root_one("/foo", "bar", "/foo/bar");
677 test_prefix_root_one("foo", "bar", "foo/bar");
678 test_prefix_root_one("/foo/", "/bar", "/foo/bar");
679 test_prefix_root_one("/foo/", "//bar", "/foo/bar");
680 test_prefix_root_one("/foo///", "//bar", "/foo/bar");
683 TEST(file_in_same_dir
) {
686 assert_se(file_in_same_dir("/", "a", &t
) == -EADDRNOTAVAIL
);
688 assert_se(file_in_same_dir("/", "/a", &t
) >= 0);
689 assert_se(streq(t
, "/a"));
692 assert_se(file_in_same_dir("", "a", &t
) == -EINVAL
);
694 assert_se(file_in_same_dir("a/", "x", &t
) >= 0);
695 assert_se(streq(t
, "x"));
698 assert_se(file_in_same_dir("bar/foo", "bar", &t
) >= 0);
699 assert_se(streq(t
, "bar/bar"));
703 static void test_path_find_first_component_one(
709 log_debug("/* %s(\"%s\", accept_dot_dot=%s) */", __func__
, strnull(path
), yes_no(accept_dot_dot
));
711 for (const char *p
= path
;;) {
715 r
= path_find_first_component(&p
, accept_dot_dot
, &e
);
719 assert_se(p
== path
+ strlen_ptr(path
));
720 assert_se(isempty(p
));
726 assert_se(strv_isempty(expected
));
731 assert_se(strcspn(e
, "/") == (size_t) r
);
732 assert_se(strlen_ptr(*expected
) == (size_t) r
);
733 assert_se(strneq(e
, *expected
++, r
));
736 log_debug("p=%s", p
);
737 if (!isempty(*expected
))
738 assert_se(startswith(p
, *expected
));
740 assert_se(p
== path
+ strlen_ptr(path
));
741 assert_se(isempty(p
));
746 TEST(path_find_first_component
) {
747 _cleanup_free_
char *hoge
= NULL
;
748 char foo
[NAME_MAX
* 2];
750 test_path_find_first_component_one(NULL
, false, NULL
, 0);
751 test_path_find_first_component_one("", false, NULL
, 0);
752 test_path_find_first_component_one("/", false, NULL
, 0);
753 test_path_find_first_component_one(".", false, NULL
, 0);
754 test_path_find_first_component_one("./", false, NULL
, 0);
755 test_path_find_first_component_one("./.", false, NULL
, 0);
756 test_path_find_first_component_one("..", false, NULL
, -EINVAL
);
757 test_path_find_first_component_one("/..", false, NULL
, -EINVAL
);
758 test_path_find_first_component_one("./..", false, NULL
, -EINVAL
);
759 test_path_find_first_component_one("////./././//.", false, NULL
, 0);
760 test_path_find_first_component_one("a/b/c", false, STRV_MAKE("a", "b", "c"), 0);
761 test_path_find_first_component_one("././//.///aa/bbb//./ccc", false, STRV_MAKE("aa", "bbb", "ccc"), 0);
762 test_path_find_first_component_one("././//.///aa/.../../bbb//./ccc/.", false, STRV_MAKE("aa", "..."), -EINVAL
);
763 test_path_find_first_component_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.", false, STRV_MAKE("aaa", ".bbb"), -EINVAL
);
764 test_path_find_first_component_one("a/foo./b//././/", false, STRV_MAKE("a", "foo.", "b"), 0);
766 test_path_find_first_component_one(NULL
, true, NULL
, 0);
767 test_path_find_first_component_one("", true, NULL
, 0);
768 test_path_find_first_component_one("/", true, NULL
, 0);
769 test_path_find_first_component_one(".", true, NULL
, 0);
770 test_path_find_first_component_one("./", true, NULL
, 0);
771 test_path_find_first_component_one("./.", true, NULL
, 0);
772 test_path_find_first_component_one("..", true, STRV_MAKE(".."), 0);
773 test_path_find_first_component_one("/..", true, STRV_MAKE(".."), 0);
774 test_path_find_first_component_one("./..", true, STRV_MAKE(".."), 0);
775 test_path_find_first_component_one("////./././//.", true, NULL
, 0);
776 test_path_find_first_component_one("a/b/c", true, STRV_MAKE("a", "b", "c"), 0);
777 test_path_find_first_component_one("././//.///aa/bbb//./ccc", true, STRV_MAKE("aa", "bbb", "ccc"), 0);
778 test_path_find_first_component_one("././//.///aa/.../../bbb//./ccc/.", true, STRV_MAKE("aa", "...", "..", "bbb", "ccc"), 0);
779 test_path_find_first_component_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.", true, STRV_MAKE("aaa", ".bbb", "..", "c.", "d.dd", "..eeee"), 0);
780 test_path_find_first_component_one("a/foo./b//././/", true, STRV_MAKE("a", "foo.", "b"), 0);
782 memset(foo
, 'a', sizeof(foo
) -1);
785 test_path_find_first_component_one(foo
, false, NULL
, -EINVAL
);
786 test_path_find_first_component_one(foo
, true, NULL
, -EINVAL
);
788 hoge
= strjoin("a/b/c/", foo
, "//d/e/.//f/");
791 test_path_find_first_component_one(hoge
, false, STRV_MAKE("a", "b", "c"), -EINVAL
);
792 test_path_find_first_component_one(hoge
, true, STRV_MAKE("a", "b", "c"), -EINVAL
);
795 static void test_path_find_last_component_one(
801 log_debug("/* %s(\"%s\", accept_dot_dot=%s) */", __func__
, strnull(path
), yes_no(accept_dot_dot
));
803 for (const char *next
= NULL
;;) {
807 r
= path_find_last_component(path
, accept_dot_dot
, &next
, &e
);
810 assert_se(next
== path
);
814 assert_se(strv_isempty(expected
));
819 assert_se(strcspn(e
, "/") == (size_t) r
);
820 assert_se(strlen_ptr(*expected
) == (size_t) r
);
821 assert_se(strneq(e
, *expected
++, r
));
824 log_debug("path=%s\nnext=%s", path
, next
);
825 if (!isempty(*expected
)) {
826 assert_se(next
< path
+ strlen(path
));
827 assert_se(next
>= path
+ strlen(*expected
));
828 assert_se(startswith(next
- strlen(*expected
), *expected
));
830 assert_se(next
== path
);
834 TEST(path_find_last_component
) {
835 _cleanup_free_
char *hoge
= NULL
;
836 char foo
[NAME_MAX
* 2];
838 test_path_find_last_component_one(NULL
, false, NULL
, 0);
839 test_path_find_last_component_one("", false, NULL
, 0);
840 test_path_find_last_component_one("/", false, NULL
, 0);
841 test_path_find_last_component_one(".", false, NULL
, 0);
842 test_path_find_last_component_one("./", false, NULL
, 0);
843 test_path_find_last_component_one("./.", false, NULL
, 0);
844 test_path_find_last_component_one("..", false, NULL
, -EINVAL
);
845 test_path_find_last_component_one("/..", false, NULL
, -EINVAL
);
846 test_path_find_last_component_one("./..", false, NULL
, -EINVAL
);
847 test_path_find_last_component_one("////./././//.", false, NULL
, 0);
848 test_path_find_last_component_one("a/b/c", false, STRV_MAKE("c", "b", "a"), 0);
849 test_path_find_last_component_one("././//.///aa./.bbb//./ccc/././/", false, STRV_MAKE("ccc", ".bbb", "aa."), 0);
850 test_path_find_last_component_one("././//.///aa/../.../bbb//./ccc/.", false, STRV_MAKE("ccc", "bbb", "..."), -EINVAL
);
851 test_path_find_last_component_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.", false, STRV_MAKE("..eeee", "d.dd", "c."), -EINVAL
);
853 test_path_find_last_component_one(NULL
, true, NULL
, 0);
854 test_path_find_last_component_one("", true, NULL
, 0);
855 test_path_find_last_component_one("/", true, NULL
, 0);
856 test_path_find_last_component_one(".", true, NULL
, 0);
857 test_path_find_last_component_one("./", true, NULL
, 0);
858 test_path_find_last_component_one("./.", true, NULL
, 0);
859 test_path_find_last_component_one("..", true, STRV_MAKE(".."), 0);
860 test_path_find_last_component_one("/..", true, STRV_MAKE(".."), 0);
861 test_path_find_last_component_one("./..", true, STRV_MAKE(".."), 0);
862 test_path_find_last_component_one("////./././//.", true, NULL
, 0);
863 test_path_find_last_component_one("a/b/c", true, STRV_MAKE("c", "b", "a"), 0);
864 test_path_find_last_component_one("././//.///aa./.bbb//./ccc/././/", true, STRV_MAKE("ccc", ".bbb", "aa."), 0);
865 test_path_find_last_component_one("././//.///aa/../.../bbb//./ccc/.", true, STRV_MAKE("ccc", "bbb", "...", "..", "aa"), 0);
866 test_path_find_last_component_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.", true, STRV_MAKE("..eeee", "d.dd", "c.", "..", ".bbb", "aaa"), 0);
868 memset(foo
, 'a', sizeof(foo
) -1);
871 test_path_find_last_component_one(foo
, false, NULL
, -EINVAL
);
872 test_path_find_last_component_one(foo
, true, NULL
, -EINVAL
);
874 hoge
= strjoin(foo
, "/a/b/c/");
877 test_path_find_last_component_one(hoge
, false, STRV_MAKE("c", "b", "a"), -EINVAL
);
878 test_path_find_last_component_one(hoge
, true, STRV_MAKE("c", "b", "a"), -EINVAL
);
881 TEST(last_path_component
) {
882 assert_se(last_path_component(NULL
) == NULL
);
883 assert_se(streq(last_path_component("a/b/c"), "c"));
884 assert_se(streq(last_path_component("a/b/c/"), "c/"));
885 assert_se(streq(last_path_component("/"), "/"));
886 assert_se(streq(last_path_component("//"), "/"));
887 assert_se(streq(last_path_component("///"), "/"));
888 assert_se(streq(last_path_component("."), "."));
889 assert_se(streq(last_path_component("./."), "."));
890 assert_se(streq(last_path_component("././"), "./"));
891 assert_se(streq(last_path_component("././/"), ".//"));
892 assert_se(streq(last_path_component("/foo/a"), "a"));
893 assert_se(streq(last_path_component("/foo/a/"), "a/"));
894 assert_se(streq(last_path_component(""), ""));
895 assert_se(streq(last_path_component("a"), "a"));
896 assert_se(streq(last_path_component("a/"), "a/"));
897 assert_se(streq(last_path_component("/a"), "a"));
898 assert_se(streq(last_path_component("/a/"), "a/"));
901 static void test_path_extract_filename_one(const char *input
, const char *output
, int ret
) {
902 _cleanup_free_
char *k
= NULL
;
905 r
= path_extract_filename(input
, &k
);
906 log_info("%s → %s/%s [expected: %s/%s]",
908 strnull(k
), r
< 0 ? STRERROR(r
) : "-",
909 strnull(output
), ret
< 0 ? STRERROR(ret
) : "-");
910 assert_se(streq_ptr(k
, output
));
914 TEST(path_extract_filename
) {
915 test_path_extract_filename_one(NULL
, NULL
, -EINVAL
);
916 test_path_extract_filename_one("a/b/c", "c", 0);
917 test_path_extract_filename_one("a/b/c/", "c", O_DIRECTORY
);
918 test_path_extract_filename_one("/", NULL
, -EADDRNOTAVAIL
);
919 test_path_extract_filename_one("//", NULL
, -EADDRNOTAVAIL
);
920 test_path_extract_filename_one("///", NULL
, -EADDRNOTAVAIL
);
921 test_path_extract_filename_one("/.", NULL
, -EADDRNOTAVAIL
);
922 test_path_extract_filename_one(".", NULL
, -EADDRNOTAVAIL
);
923 test_path_extract_filename_one("./", NULL
, -EADDRNOTAVAIL
);
924 test_path_extract_filename_one("./.", NULL
, -EADDRNOTAVAIL
);
925 test_path_extract_filename_one("././", NULL
, -EADDRNOTAVAIL
);
926 test_path_extract_filename_one("././/", NULL
, -EADDRNOTAVAIL
);
927 test_path_extract_filename_one("/foo/a", "a", 0);
928 test_path_extract_filename_one("/foo/a/", "a", O_DIRECTORY
);
929 test_path_extract_filename_one("", NULL
, -EINVAL
);
930 test_path_extract_filename_one("a", "a", 0);
931 test_path_extract_filename_one("a/", "a", O_DIRECTORY
);
932 test_path_extract_filename_one("a/././//.", "a", O_DIRECTORY
);
933 test_path_extract_filename_one("/a", "a", 0);
934 test_path_extract_filename_one("/a/", "a", O_DIRECTORY
);
935 test_path_extract_filename_one("/a//./.", "a", O_DIRECTORY
);
936 test_path_extract_filename_one("/////////////a/////////////", "a", O_DIRECTORY
);
937 test_path_extract_filename_one("//./a/.///b./././.c//./d//.", "d", O_DIRECTORY
);
938 test_path_extract_filename_one("xx/.", "xx", O_DIRECTORY
);
939 test_path_extract_filename_one("xx/..", NULL
, -EINVAL
);
940 test_path_extract_filename_one("..", NULL
, -EINVAL
);
941 test_path_extract_filename_one("/..", NULL
, -EINVAL
);
942 test_path_extract_filename_one("../", NULL
, -EINVAL
);
945 static void test_path_extract_directory_one(const char *input
, const char *output
, int ret
) {
946 _cleanup_free_
char *k
= NULL
;
949 r
= path_extract_directory(input
, &k
);
950 log_info("%s → %s/%s [expected: %s/%s]",
952 strnull(k
), r
< 0 ? STRERROR(r
) : "-",
953 strnull(output
), STRERROR(ret
));
954 assert_se(streq_ptr(k
, output
));
957 /* Extra safety check: let's make sure that if we split out the filename too (and it works) the
958 * joined parts are identical to the original again */
960 _cleanup_free_
char *f
= NULL
;
962 r
= path_extract_filename(input
, &f
);
964 _cleanup_free_
char *j
= NULL
;
966 assert_se(j
= path_join(k
, f
));
967 assert_se(path_equal(input
, j
));
972 TEST(path_extract_directory
) {
973 test_path_extract_directory_one(NULL
, NULL
, -EINVAL
);
974 test_path_extract_directory_one("a/b/c", "a/b", 0);
975 test_path_extract_directory_one("a/b/c/", "a/b", 0);
976 test_path_extract_directory_one("/", NULL
, -EADDRNOTAVAIL
);
977 test_path_extract_directory_one("//", NULL
, -EADDRNOTAVAIL
);
978 test_path_extract_directory_one("///", NULL
, -EADDRNOTAVAIL
);
979 test_path_extract_directory_one("/.", NULL
, -EADDRNOTAVAIL
);
980 test_path_extract_directory_one(".", NULL
, -EADDRNOTAVAIL
);
981 test_path_extract_directory_one("./", NULL
, -EADDRNOTAVAIL
);
982 test_path_extract_directory_one("./.", NULL
, -EADDRNOTAVAIL
);
983 test_path_extract_directory_one("././", NULL
, -EADDRNOTAVAIL
);
984 test_path_extract_directory_one("././/", NULL
, -EADDRNOTAVAIL
);
985 test_path_extract_directory_one("/foo/a", "/foo", 0);
986 test_path_extract_directory_one("/foo/a/", "/foo", 0);
987 test_path_extract_directory_one("", NULL
, -EINVAL
);
988 test_path_extract_directory_one("a", NULL
, -EDESTADDRREQ
);
989 test_path_extract_directory_one("a/", NULL
, -EDESTADDRREQ
);
990 test_path_extract_directory_one("a/././//.", NULL
, -EDESTADDRREQ
);
991 test_path_extract_directory_one("/a", "/", 0);
992 test_path_extract_directory_one("/a/", "/", 0);
993 test_path_extract_directory_one("/a//./.", "/", 0);
994 test_path_extract_directory_one("/////////////a/////////////", "/", 0);
995 test_path_extract_directory_one("//./a/.///b./././.c//./d//.", "/a/b./.c", 0);
996 test_path_extract_directory_one("xx/.", NULL
, -EDESTADDRREQ
);
997 test_path_extract_directory_one("xx/..", NULL
, -EINVAL
);
998 test_path_extract_directory_one("..", NULL
, -EINVAL
);
999 test_path_extract_directory_one("/..", NULL
, -EINVAL
);
1000 test_path_extract_directory_one("../", NULL
, -EINVAL
);
1003 TEST(filename_is_valid
) {
1004 char foo
[NAME_MAX
+2];
1006 assert_se(!filename_is_valid(""));
1007 assert_se(!filename_is_valid("/bar/foo"));
1008 assert_se(!filename_is_valid("/"));
1009 assert_se(!filename_is_valid("."));
1010 assert_se(!filename_is_valid(".."));
1011 assert_se(!filename_is_valid("bar/foo"));
1012 assert_se(!filename_is_valid("bar/foo/"));
1013 assert_se(!filename_is_valid("bar//"));
1015 memset(foo
, 'a', sizeof(foo
) - 1);
1018 assert_se(!filename_is_valid(foo
));
1020 assert_se(filename_is_valid("foo_bar-333"));
1021 assert_se(filename_is_valid("o.o"));
1024 static void test_path_is_valid_and_safe_one(const char *p
, bool ret
) {
1025 log_debug("/* %s(\"%s\") */", __func__
, strnull(p
));
1027 assert_se(path_is_valid(p
) == ret
);
1029 ret
= !streq(p
, "..") &&
1030 !startswith(p
, "../") &&
1031 !endswith(p
, "/..") &&
1033 assert_se(path_is_safe(p
) == ret
);
1036 TEST(path_is_valid_and_safe
) {
1037 char foo
[PATH_MAX
+2];
1040 test_path_is_valid_and_safe_one("", false);
1041 test_path_is_valid_and_safe_one("/bar/foo", true);
1042 test_path_is_valid_and_safe_one("/bar/foo/", true);
1043 test_path_is_valid_and_safe_one("/bar/foo/", true);
1044 test_path_is_valid_and_safe_one("//bar//foo//", true);
1045 test_path_is_valid_and_safe_one("/", true);
1046 test_path_is_valid_and_safe_one("/////", true);
1047 test_path_is_valid_and_safe_one("/////.///.////...///..//.", true);
1048 test_path_is_valid_and_safe_one(".", true);
1049 test_path_is_valid_and_safe_one("..", true);
1050 test_path_is_valid_and_safe_one("bar/foo", true);
1051 test_path_is_valid_and_safe_one("bar/foo/", true);
1052 test_path_is_valid_and_safe_one("bar//", true);
1054 memset(foo
, 'a', sizeof(foo
) -1);
1057 test_path_is_valid_and_safe_one(foo
, false);
1059 c
= strjoina("/xxx/", foo
, "/yyy");
1060 test_path_is_valid_and_safe_one(c
, false);
1062 test_path_is_valid_and_safe_one("foo_bar-333", true);
1063 test_path_is_valid_and_safe_one("o.o", true);
1066 TEST(hidden_or_backup_file
) {
1067 assert_se(hidden_or_backup_file(".hidden"));
1068 assert_se(hidden_or_backup_file("..hidden"));
1069 assert_se(!hidden_or_backup_file("hidden."));
1071 assert_se(hidden_or_backup_file("backup~"));
1072 assert_se(hidden_or_backup_file(".backup~"));
1074 assert_se(hidden_or_backup_file("lost+found"));
1075 assert_se(hidden_or_backup_file("aquota.user"));
1076 assert_se(hidden_or_backup_file("aquota.group"));
1078 assert_se(hidden_or_backup_file("test.rpmnew"));
1079 assert_se(hidden_or_backup_file("test.dpkg-old"));
1080 assert_se(hidden_or_backup_file("test.dpkg-remove"));
1081 assert_se(hidden_or_backup_file("test.swp"));
1083 assert_se(!hidden_or_backup_file("test.rpmnew."));
1084 assert_se(!hidden_or_backup_file("test.dpkg-old.foo"));
1087 TEST(skip_dev_prefix
) {
1088 assert_se(streq(skip_dev_prefix("/"), "/"));
1089 assert_se(streq(skip_dev_prefix("/dev"), ""));
1090 assert_se(streq(skip_dev_prefix("/dev/"), ""));
1091 assert_se(streq(skip_dev_prefix("/dev/foo"), "foo"));
1092 assert_se(streq(skip_dev_prefix("/dev/foo/bar"), "foo/bar"));
1093 assert_se(streq(skip_dev_prefix("//dev"), ""));
1094 assert_se(streq(skip_dev_prefix("//dev//"), ""));
1095 assert_se(streq(skip_dev_prefix("/dev///foo"), "foo"));
1096 assert_se(streq(skip_dev_prefix("///dev///foo///bar"), "foo///bar"));
1097 assert_se(streq(skip_dev_prefix("//foo"), "//foo"));
1098 assert_se(streq(skip_dev_prefix("foo"), "foo"));
1101 TEST(empty_or_root
) {
1102 assert_se(empty_or_root(NULL
));
1103 assert_se(empty_or_root(""));
1104 assert_se(empty_or_root("/"));
1105 assert_se(empty_or_root("//"));
1106 assert_se(empty_or_root("///"));
1107 assert_se(empty_or_root("/////////////////"));
1108 assert_se(!empty_or_root("xxx"));
1109 assert_se(!empty_or_root("/xxx"));
1110 assert_se(!empty_or_root("/xxx/"));
1111 assert_se(!empty_or_root("//yy//"));
1114 TEST(path_startswith_set
) {
1115 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/foo/bar", "/zzz"), ""));
1116 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/foo/", "/zzz"), "bar"));
1117 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/foo", "/zzz"), "bar"));
1118 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/", "/zzz"), "foo/bar"));
1119 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "", "/zzz"), NULL
));
1121 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/foo/bar", "/zzz"), NULL
));
1122 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/foo/", "/zzz"), "bar2"));
1123 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/foo", "/zzz"), "bar2"));
1124 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/", "/zzz"), "foo/bar2"));
1125 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "", "/zzz"), NULL
));
1127 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/foo/bar", "/zzz"), NULL
));
1128 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/foo/", "/zzz"), NULL
));
1129 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/foo", "/zzz"), NULL
));
1130 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/", "/zzz"), "foo2/bar"));
1131 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "", "/zzz"), NULL
));
1134 TEST(path_startswith_strv
) {
1135 assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/foo/bar", "/zzz")), ""));
1136 assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/foo/", "/zzz")), "bar"));
1137 assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/foo", "/zzz")), "bar"));
1138 assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/", "/zzz")), "foo/bar"));
1139 assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "", "/zzz")), NULL
));
1141 assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "/foo/bar", "/zzz")), NULL
));
1142 assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "/foo/", "/zzz")), "bar2"));
1143 assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "/foo", "/zzz")), "bar2"));
1144 assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "/", "/zzz")), "foo/bar2"));
1145 assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "", "/zzz")), NULL
));
1147 assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "/foo/bar", "/zzz")), NULL
));
1148 assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "/foo/", "/zzz")), NULL
));
1149 assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "/foo", "/zzz")), NULL
));
1150 assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "/", "/zzz")), "foo2/bar"));
1151 assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "", "/zzz")), NULL
));
1154 static void test_path_glob_can_match_one(const char *pattern
, const char *prefix
, const char *expected
) {
1155 _cleanup_free_
char *result
= NULL
;
1157 log_debug("%s(%s, %s, %s)", __func__
, pattern
, prefix
, strnull(expected
));
1159 assert_se(path_glob_can_match(pattern
, prefix
, &result
) == !!expected
);
1160 assert_se(streq_ptr(result
, expected
));
1163 TEST(path_glob_can_match
) {
1164 test_path_glob_can_match_one("/foo/hoge/aaa", "/foo/hoge/aaa/bbb", NULL
);
1165 test_path_glob_can_match_one("/foo/hoge/aaa", "/foo/hoge/aaa", "/foo/hoge/aaa");
1166 test_path_glob_can_match_one("/foo/hoge/aaa", "/foo/hoge", "/foo/hoge/aaa");
1167 test_path_glob_can_match_one("/foo/hoge/aaa", "/foo", "/foo/hoge/aaa");
1168 test_path_glob_can_match_one("/foo/hoge/aaa", "/", "/foo/hoge/aaa");
1170 test_path_glob_can_match_one("/foo/*/aaa", "/foo/hoge/aaa/bbb", NULL
);
1171 test_path_glob_can_match_one("/foo/*/aaa", "/foo/hoge/aaa", "/foo/hoge/aaa");
1172 test_path_glob_can_match_one("/foo/*/aaa", "/foo/hoge", "/foo/hoge/aaa");
1173 test_path_glob_can_match_one("/foo/*/aaa", "/foo", "/foo/*/aaa");
1174 test_path_glob_can_match_one("/foo/*/aaa", "/", "/foo/*/aaa");
1176 test_path_glob_can_match_one("/foo/*/*/aaa", "/foo/xxx/yyy/aaa/bbb", NULL
);
1177 test_path_glob_can_match_one("/foo/*/*/aaa", "/foo/xxx/yyy/aaa", "/foo/xxx/yyy/aaa");
1178 test_path_glob_can_match_one("/foo/*/*/aaa", "/foo/xxx/yyy", "/foo/xxx/yyy/aaa");
1179 test_path_glob_can_match_one("/foo/*/*/aaa", "/foo/xxx", "/foo/xxx/*/aaa");
1180 test_path_glob_can_match_one("/foo/*/*/aaa", "/foo", "/foo/*/*/aaa");
1181 test_path_glob_can_match_one("/foo/*/*/aaa", "/", "/foo/*/*/aaa");
1183 test_path_glob_can_match_one("/foo/*/aaa/*", "/foo/xxx/aaa/bbb/ccc", NULL
);
1184 test_path_glob_can_match_one("/foo/*/aaa/*", "/foo/xxx/aaa/bbb", "/foo/xxx/aaa/bbb");
1185 test_path_glob_can_match_one("/foo/*/aaa/*", "/foo/xxx/ccc", NULL
);
1186 test_path_glob_can_match_one("/foo/*/aaa/*", "/foo/xxx/aaa", "/foo/xxx/aaa/*");
1187 test_path_glob_can_match_one("/foo/*/aaa/*", "/foo/xxx", "/foo/xxx/aaa/*");
1188 test_path_glob_can_match_one("/foo/*/aaa/*", "/foo", "/foo/*/aaa/*");
1189 test_path_glob_can_match_one("/foo/*/aaa/*", "/", "/foo/*/aaa/*");
1193 log_info("PATH_MAX=%zu\n"
1194 "FILENAME_MAX=%zu\n"
1197 (size_t) FILENAME_MAX
,
1200 assert_cc(FILENAME_MAX
== PATH_MAX
);
1203 DEFINE_TEST_MAIN(LOG_DEBUG
);