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(inode_same("/", "/", 0) > 0);
211 assert_se(inode_same("/", "/", AT_SYMLINK_NOFOLLOW
) > 0);
212 assert_se(inode_same("/", "//", 0) > 0);
213 assert_se(inode_same("/", "//", AT_SYMLINK_NOFOLLOW
) > 0);
215 assert_se(inode_same("/", "/./", 0) > 0);
216 assert_se(inode_same("/", "/./", AT_SYMLINK_NOFOLLOW
) > 0);
217 assert_se(inode_same("/", "/../", 0) > 0);
218 assert_se(inode_same("/", "/../", AT_SYMLINK_NOFOLLOW
) > 0);
220 assert_se(inode_same("/", "/.../", 0) == -ENOENT
);
221 assert_se(inode_same("/", "/.../", AT_SYMLINK_NOFOLLOW
) == -ENOENT
);
223 /* The same for path_equal_or_files_same. */
225 assert_se(path_equal_or_inode_same("/", "/", 0));
226 assert_se(path_equal_or_inode_same("/", "/", AT_SYMLINK_NOFOLLOW
));
227 assert_se(path_equal_or_inode_same("/", "//", 0));
228 assert_se(path_equal_or_inode_same("/", "//", AT_SYMLINK_NOFOLLOW
));
230 assert_se(path_equal_or_inode_same("/", "/./", 0));
231 assert_se(path_equal_or_inode_same("/", "/./", AT_SYMLINK_NOFOLLOW
));
232 assert_se(path_equal_or_inode_same("/", "/../", 0));
233 assert_se(path_equal_or_inode_same("/", "/../", AT_SYMLINK_NOFOLLOW
));
235 assert_se(!path_equal_or_inode_same("/", "/.../", 0));
236 assert_se(!path_equal_or_inode_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 static void test_path_make_relative_one(const char *from
, const char *to
, const char *expected
) {
498 _cleanup_free_
char *z
= NULL
;
501 log_info("/* %s(%s, %s) */", __func__
, from
, to
);
503 r
= path_make_relative(from
, to
, &z
);
504 assert_se((r
>= 0) == !!expected
);
505 assert_se(streq_ptr(z
, expected
));
508 TEST(path_make_relative
) {
509 test_path_make_relative_one("some/relative/path", "/some/path", NULL
);
510 test_path_make_relative_one("/some/path", "some/relative/path", NULL
);
511 test_path_make_relative_one("/some/dotdot/../path", "/some/path", NULL
);
513 test_path_make_relative_one("/", "/", ".");
514 test_path_make_relative_one("/", "/some/path", "some/path");
515 test_path_make_relative_one("/some/path", "/some/path", ".");
516 test_path_make_relative_one("/some/path", "/some/path/in/subdir", "in/subdir");
517 test_path_make_relative_one("/some/path", "/", "../..");
518 test_path_make_relative_one("/some/path", "/some/other/path", "../other/path");
519 test_path_make_relative_one("/some/path/./dot", "/some/further/path", "../../further/path");
520 test_path_make_relative_one("//extra.//.//./.slashes//./won't////fo.ol///anybody//", "/././/extra././/.slashes////ar.e/.just/././.fine///", "../../../ar.e/.just/.fine");
523 static void test_path_make_relative_parent_one(const char *from
, const char *to
, const char *expected
) {
524 _cleanup_free_
char *z
= NULL
;
527 log_info("/* %s(%s, %s) */", __func__
, from
, to
);
529 r
= path_make_relative_parent(from
, to
, &z
);
530 assert_se((r
>= 0) == !!expected
);
531 assert_se(streq_ptr(z
, expected
));
534 TEST(path_make_relative_parent
) {
535 test_path_make_relative_parent_one("some/relative/path/hoge", "/some/path", NULL
);
536 test_path_make_relative_parent_one("/some/path/hoge", "some/relative/path", NULL
);
537 test_path_make_relative_parent_one("/some/dotdot/../path/hoge", "/some/path", NULL
);
538 test_path_make_relative_parent_one("/", "/aaa", NULL
);
540 test_path_make_relative_parent_one("/hoge", "/", ".");
541 test_path_make_relative_parent_one("/hoge", "/some/path", "some/path");
542 test_path_make_relative_parent_one("/some/path/hoge", "/some/path", ".");
543 test_path_make_relative_parent_one("/some/path/hoge", "/some/path/in/subdir", "in/subdir");
544 test_path_make_relative_parent_one("/some/path/hoge", "/", "../..");
545 test_path_make_relative_parent_one("/some/path/hoge", "/some/other/path", "../other/path");
546 test_path_make_relative_parent_one("/some/path/./dot/hoge", "/some/further/path", "../../further/path");
547 test_path_make_relative_parent_one("//extra.//.//./.slashes//./won't////fo.ol///anybody//hoge", "/././/extra././/.slashes////ar.e/.just/././.fine///", "../../../ar.e/.just/.fine");
550 TEST(path_strv_resolve
) {
551 char tmp_dir
[] = "/tmp/test-path-util-XXXXXX";
552 _cleanup_strv_free_
char **search_dirs
= NULL
;
553 _cleanup_strv_free_
char **absolute_dirs
= NULL
;
555 assert_se(mkdtemp(tmp_dir
) != NULL
);
557 search_dirs
= strv_new("/dir1", "/dir2", "/dir3");
558 assert_se(search_dirs
);
559 STRV_FOREACH(d
, search_dirs
) {
560 char *p
= path_join(tmp_dir
, *d
);
562 assert_se(strv_push(&absolute_dirs
, p
) == 0);
565 assert_se(mkdir(absolute_dirs
[0], 0700) == 0);
566 assert_se(mkdir(absolute_dirs
[1], 0700) == 0);
567 assert_se(symlink("dir2", absolute_dirs
[2]) == 0);
569 path_strv_resolve(search_dirs
, tmp_dir
);
570 assert_se(streq(search_dirs
[0], "/dir1"));
571 assert_se(streq(search_dirs
[1], "/dir2"));
572 assert_se(streq(search_dirs
[2], "/dir2"));
574 assert_se(rm_rf(tmp_dir
, REMOVE_ROOT
|REMOVE_PHYSICAL
) == 0);
577 static void test_path_startswith_one(const char *path
, const char *prefix
, const char *skipped
, const char *expected
) {
580 log_debug("/* %s(%s, %s) */", __func__
, path
, prefix
);
582 p
= path_startswith(path
, prefix
);
583 assert_se(streq_ptr(p
, expected
));
585 q
= strjoina(skipped
, p
);
586 assert_se(streq(q
, path
));
587 assert_se(p
== path
+ strlen(skipped
));
591 TEST(path_startswith
) {
592 test_path_startswith_one("/foo/bar/barfoo/", "/foo", "/foo/", "bar/barfoo/");
593 test_path_startswith_one("/foo/bar/barfoo/", "/foo/", "/foo/", "bar/barfoo/");
594 test_path_startswith_one("/foo/bar/barfoo/", "/", "/", "foo/bar/barfoo/");
595 test_path_startswith_one("/foo/bar/barfoo/", "////", "/", "foo/bar/barfoo/");
596 test_path_startswith_one("/foo/bar/barfoo/", "/foo//bar/////barfoo///", "/foo/bar/barfoo/", "");
597 test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/barfoo////", "/foo/bar/barfoo/", "");
598 test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar///barfoo/", "/foo/bar/barfoo/", "");
599 test_path_startswith_one("/foo/bar/barfoo/", "/foo////bar/barfoo/", "/foo/bar/barfoo/", "");
600 test_path_startswith_one("/foo/bar/barfoo/", "////foo/bar/barfoo/", "/foo/bar/barfoo/", "");
601 test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/barfoo", "/foo/bar/barfoo/", "");
603 test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/barfooa/", NULL
, NULL
);
604 test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/barfooa", NULL
, NULL
);
605 test_path_startswith_one("/foo/bar/barfoo/", "", NULL
, NULL
);
606 test_path_startswith_one("/foo/bar/barfoo/", "/bar/foo", NULL
, NULL
);
607 test_path_startswith_one("/foo/bar/barfoo/", "/f/b/b/", NULL
, NULL
);
608 test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/barfo", NULL
, NULL
);
609 test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/bar", NULL
, NULL
);
610 test_path_startswith_one("/foo/bar/barfoo/", "/fo", NULL
, NULL
);
613 static void test_prefix_root_one(const char *r
, const char *p
, const char *expected
) {
614 _cleanup_free_
char *s
= NULL
;
617 assert_se(s
= path_join(r
, p
));
618 assert_se(path_equal_ptr(s
, expected
));
620 t
= prefix_roota(r
, p
);
622 assert_se(path_equal_ptr(t
, expected
));
626 test_prefix_root_one("/", "/foo", "/foo");
627 test_prefix_root_one(NULL
, "/foo", "/foo");
628 test_prefix_root_one("", "/foo", "/foo");
629 test_prefix_root_one("///", "/foo", "/foo");
630 test_prefix_root_one("/", "////foo", "/foo");
631 test_prefix_root_one(NULL
, "////foo", "/foo");
632 test_prefix_root_one("/", "foo", "/foo");
633 test_prefix_root_one("", "foo", "foo");
634 test_prefix_root_one(NULL
, "foo", "foo");
636 test_prefix_root_one("/foo", "/bar", "/foo/bar");
637 test_prefix_root_one("/foo", "bar", "/foo/bar");
638 test_prefix_root_one("foo", "bar", "foo/bar");
639 test_prefix_root_one("/foo/", "/bar", "/foo/bar");
640 test_prefix_root_one("/foo/", "//bar", "/foo/bar");
641 test_prefix_root_one("/foo///", "//bar", "/foo/bar");
644 TEST(file_in_same_dir
) {
647 assert_se(file_in_same_dir("/", "a", &t
) == -EADDRNOTAVAIL
);
649 assert_se(file_in_same_dir("/", "/a", &t
) >= 0);
650 assert_se(streq(t
, "/a"));
653 assert_se(file_in_same_dir("", "a", &t
) == -EINVAL
);
655 assert_se(file_in_same_dir("a/", "x", &t
) >= 0);
656 assert_se(streq(t
, "x"));
659 assert_se(file_in_same_dir("bar/foo", "bar", &t
) >= 0);
660 assert_se(streq(t
, "bar/bar"));
664 static void test_path_find_first_component_one(
670 log_debug("/* %s(\"%s\", accept_dot_dot=%s) */", __func__
, strnull(path
), yes_no(accept_dot_dot
));
672 for (const char *p
= path
;;) {
676 r
= path_find_first_component(&p
, accept_dot_dot
, &e
);
680 assert_se(p
== path
+ strlen_ptr(path
));
681 assert_se(isempty(p
));
687 assert_se(strv_isempty(expected
));
692 assert_se(strcspn(e
, "/") == (size_t) r
);
693 assert_se(strlen_ptr(*expected
) == (size_t) r
);
694 assert_se(strneq(e
, *expected
++, r
));
697 log_debug("p=%s", p
);
698 if (!isempty(*expected
))
699 assert_se(startswith(p
, *expected
));
701 assert_se(p
== path
+ strlen_ptr(path
));
702 assert_se(isempty(p
));
707 TEST(path_find_first_component
) {
708 _cleanup_free_
char *hoge
= NULL
;
709 char foo
[NAME_MAX
* 2];
711 test_path_find_first_component_one(NULL
, false, NULL
, 0);
712 test_path_find_first_component_one("", false, NULL
, 0);
713 test_path_find_first_component_one("/", false, NULL
, 0);
714 test_path_find_first_component_one(".", false, NULL
, 0);
715 test_path_find_first_component_one("./", false, NULL
, 0);
716 test_path_find_first_component_one("./.", false, NULL
, 0);
717 test_path_find_first_component_one("..", false, NULL
, -EINVAL
);
718 test_path_find_first_component_one("/..", false, NULL
, -EINVAL
);
719 test_path_find_first_component_one("./..", false, NULL
, -EINVAL
);
720 test_path_find_first_component_one("////./././//.", false, NULL
, 0);
721 test_path_find_first_component_one("a/b/c", false, STRV_MAKE("a", "b", "c"), 0);
722 test_path_find_first_component_one("././//.///aa/bbb//./ccc", false, STRV_MAKE("aa", "bbb", "ccc"), 0);
723 test_path_find_first_component_one("././//.///aa/.../../bbb//./ccc/.", false, STRV_MAKE("aa", "..."), -EINVAL
);
724 test_path_find_first_component_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.", false, STRV_MAKE("aaa", ".bbb"), -EINVAL
);
725 test_path_find_first_component_one("a/foo./b//././/", false, STRV_MAKE("a", "foo.", "b"), 0);
727 test_path_find_first_component_one(NULL
, true, NULL
, 0);
728 test_path_find_first_component_one("", true, NULL
, 0);
729 test_path_find_first_component_one("/", true, NULL
, 0);
730 test_path_find_first_component_one(".", true, NULL
, 0);
731 test_path_find_first_component_one("./", true, NULL
, 0);
732 test_path_find_first_component_one("./.", true, NULL
, 0);
733 test_path_find_first_component_one("..", true, STRV_MAKE(".."), 0);
734 test_path_find_first_component_one("/..", true, STRV_MAKE(".."), 0);
735 test_path_find_first_component_one("./..", true, STRV_MAKE(".."), 0);
736 test_path_find_first_component_one("////./././//.", true, NULL
, 0);
737 test_path_find_first_component_one("a/b/c", true, STRV_MAKE("a", "b", "c"), 0);
738 test_path_find_first_component_one("././//.///aa/bbb//./ccc", true, STRV_MAKE("aa", "bbb", "ccc"), 0);
739 test_path_find_first_component_one("././//.///aa/.../../bbb//./ccc/.", true, STRV_MAKE("aa", "...", "..", "bbb", "ccc"), 0);
740 test_path_find_first_component_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.", true, STRV_MAKE("aaa", ".bbb", "..", "c.", "d.dd", "..eeee"), 0);
741 test_path_find_first_component_one("a/foo./b//././/", true, STRV_MAKE("a", "foo.", "b"), 0);
743 memset(foo
, 'a', sizeof(foo
) -1);
746 test_path_find_first_component_one(foo
, false, NULL
, -EINVAL
);
747 test_path_find_first_component_one(foo
, true, NULL
, -EINVAL
);
749 hoge
= strjoin("a/b/c/", foo
, "//d/e/.//f/");
752 test_path_find_first_component_one(hoge
, false, STRV_MAKE("a", "b", "c"), -EINVAL
);
753 test_path_find_first_component_one(hoge
, true, STRV_MAKE("a", "b", "c"), -EINVAL
);
756 static void test_path_find_last_component_one(
762 log_debug("/* %s(\"%s\", accept_dot_dot=%s) */", __func__
, strnull(path
), yes_no(accept_dot_dot
));
764 for (const char *next
= NULL
;;) {
768 r
= path_find_last_component(path
, accept_dot_dot
, &next
, &e
);
771 assert_se(next
== path
);
775 assert_se(strv_isempty(expected
));
780 assert_se(strcspn(e
, "/") == (size_t) r
);
781 assert_se(strlen_ptr(*expected
) == (size_t) r
);
782 assert_se(strneq(e
, *expected
++, r
));
785 log_debug("path=%s\nnext=%s", path
, next
);
786 if (!isempty(*expected
)) {
787 assert_se(next
< path
+ strlen(path
));
788 assert_se(next
>= path
+ strlen(*expected
));
789 assert_se(startswith(next
- strlen(*expected
), *expected
));
791 assert_se(next
== path
);
795 TEST(path_find_last_component
) {
796 _cleanup_free_
char *hoge
= NULL
;
797 char foo
[NAME_MAX
* 2];
799 test_path_find_last_component_one(NULL
, false, NULL
, 0);
800 test_path_find_last_component_one("", false, NULL
, 0);
801 test_path_find_last_component_one("/", false, NULL
, 0);
802 test_path_find_last_component_one(".", false, NULL
, 0);
803 test_path_find_last_component_one("./", false, NULL
, 0);
804 test_path_find_last_component_one("./.", false, NULL
, 0);
805 test_path_find_last_component_one("..", false, NULL
, -EINVAL
);
806 test_path_find_last_component_one("/..", false, NULL
, -EINVAL
);
807 test_path_find_last_component_one("./..", false, NULL
, -EINVAL
);
808 test_path_find_last_component_one("////./././//.", false, NULL
, 0);
809 test_path_find_last_component_one("a/b/c", false, STRV_MAKE("c", "b", "a"), 0);
810 test_path_find_last_component_one("././//.///aa./.bbb//./ccc/././/", false, STRV_MAKE("ccc", ".bbb", "aa."), 0);
811 test_path_find_last_component_one("././//.///aa/../.../bbb//./ccc/.", false, STRV_MAKE("ccc", "bbb", "..."), -EINVAL
);
812 test_path_find_last_component_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.", false, STRV_MAKE("..eeee", "d.dd", "c."), -EINVAL
);
814 test_path_find_last_component_one(NULL
, true, NULL
, 0);
815 test_path_find_last_component_one("", true, NULL
, 0);
816 test_path_find_last_component_one("/", true, NULL
, 0);
817 test_path_find_last_component_one(".", true, NULL
, 0);
818 test_path_find_last_component_one("./", true, NULL
, 0);
819 test_path_find_last_component_one("./.", true, NULL
, 0);
820 test_path_find_last_component_one("..", true, STRV_MAKE(".."), 0);
821 test_path_find_last_component_one("/..", true, STRV_MAKE(".."), 0);
822 test_path_find_last_component_one("./..", true, STRV_MAKE(".."), 0);
823 test_path_find_last_component_one("////./././//.", true, NULL
, 0);
824 test_path_find_last_component_one("a/b/c", true, STRV_MAKE("c", "b", "a"), 0);
825 test_path_find_last_component_one("././//.///aa./.bbb//./ccc/././/", true, STRV_MAKE("ccc", ".bbb", "aa."), 0);
826 test_path_find_last_component_one("././//.///aa/../.../bbb//./ccc/.", true, STRV_MAKE("ccc", "bbb", "...", "..", "aa"), 0);
827 test_path_find_last_component_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.", true, STRV_MAKE("..eeee", "d.dd", "c.", "..", ".bbb", "aaa"), 0);
829 memset(foo
, 'a', sizeof(foo
) -1);
832 test_path_find_last_component_one(foo
, false, NULL
, -EINVAL
);
833 test_path_find_last_component_one(foo
, true, NULL
, -EINVAL
);
835 hoge
= strjoin(foo
, "/a/b/c/");
838 test_path_find_last_component_one(hoge
, false, STRV_MAKE("c", "b", "a"), -EINVAL
);
839 test_path_find_last_component_one(hoge
, true, STRV_MAKE("c", "b", "a"), -EINVAL
);
842 TEST(last_path_component
) {
843 assert_se(last_path_component(NULL
) == NULL
);
844 assert_se(streq(last_path_component("a/b/c"), "c"));
845 assert_se(streq(last_path_component("a/b/c/"), "c/"));
846 assert_se(streq(last_path_component("/"), "/"));
847 assert_se(streq(last_path_component("//"), "/"));
848 assert_se(streq(last_path_component("///"), "/"));
849 assert_se(streq(last_path_component("."), "."));
850 assert_se(streq(last_path_component("./."), "."));
851 assert_se(streq(last_path_component("././"), "./"));
852 assert_se(streq(last_path_component("././/"), ".//"));
853 assert_se(streq(last_path_component("/foo/a"), "a"));
854 assert_se(streq(last_path_component("/foo/a/"), "a/"));
855 assert_se(streq(last_path_component(""), ""));
856 assert_se(streq(last_path_component("a"), "a"));
857 assert_se(streq(last_path_component("a/"), "a/"));
858 assert_se(streq(last_path_component("/a"), "a"));
859 assert_se(streq(last_path_component("/a/"), "a/"));
862 static void test_path_extract_filename_one(const char *input
, const char *output
, int ret
) {
863 _cleanup_free_
char *k
= NULL
;
866 r
= path_extract_filename(input
, &k
);
867 log_info("%s → %s/%s [expected: %s/%s]",
869 strnull(k
), r
< 0 ? STRERROR(r
) : "-",
870 strnull(output
), ret
< 0 ? STRERROR(ret
) : "-");
871 assert_se(streq_ptr(k
, output
));
875 TEST(path_extract_filename
) {
876 test_path_extract_filename_one(NULL
, NULL
, -EINVAL
);
877 test_path_extract_filename_one("a/b/c", "c", 0);
878 test_path_extract_filename_one("a/b/c/", "c", O_DIRECTORY
);
879 test_path_extract_filename_one("/", NULL
, -EADDRNOTAVAIL
);
880 test_path_extract_filename_one("//", NULL
, -EADDRNOTAVAIL
);
881 test_path_extract_filename_one("///", NULL
, -EADDRNOTAVAIL
);
882 test_path_extract_filename_one("/.", NULL
, -EADDRNOTAVAIL
);
883 test_path_extract_filename_one(".", NULL
, -EADDRNOTAVAIL
);
884 test_path_extract_filename_one("./", NULL
, -EADDRNOTAVAIL
);
885 test_path_extract_filename_one("./.", NULL
, -EADDRNOTAVAIL
);
886 test_path_extract_filename_one("././", NULL
, -EADDRNOTAVAIL
);
887 test_path_extract_filename_one("././/", NULL
, -EADDRNOTAVAIL
);
888 test_path_extract_filename_one("/foo/a", "a", 0);
889 test_path_extract_filename_one("/foo/a/", "a", O_DIRECTORY
);
890 test_path_extract_filename_one("", NULL
, -EINVAL
);
891 test_path_extract_filename_one("a", "a", 0);
892 test_path_extract_filename_one("a/", "a", O_DIRECTORY
);
893 test_path_extract_filename_one("a/././//.", "a", O_DIRECTORY
);
894 test_path_extract_filename_one("/a", "a", 0);
895 test_path_extract_filename_one("/a/", "a", O_DIRECTORY
);
896 test_path_extract_filename_one("/a//./.", "a", O_DIRECTORY
);
897 test_path_extract_filename_one("/////////////a/////////////", "a", O_DIRECTORY
);
898 test_path_extract_filename_one("//./a/.///b./././.c//./d//.", "d", O_DIRECTORY
);
899 test_path_extract_filename_one("xx/.", "xx", O_DIRECTORY
);
900 test_path_extract_filename_one("xx/..", NULL
, -EINVAL
);
901 test_path_extract_filename_one("..", NULL
, -EINVAL
);
902 test_path_extract_filename_one("/..", NULL
, -EINVAL
);
903 test_path_extract_filename_one("../", NULL
, -EINVAL
);
906 static void test_path_extract_directory_one(const char *input
, const char *output
, int ret
) {
907 _cleanup_free_
char *k
= NULL
;
910 r
= path_extract_directory(input
, &k
);
911 log_info("%s → %s/%s [expected: %s/%s]",
913 strnull(k
), r
< 0 ? STRERROR(r
) : "-",
914 strnull(output
), STRERROR(ret
));
915 assert_se(streq_ptr(k
, output
));
918 /* Extra safety check: let's make sure that if we split out the filename too (and it works) the
919 * joined parts are identical to the original again */
921 _cleanup_free_
char *f
= NULL
;
923 r
= path_extract_filename(input
, &f
);
925 _cleanup_free_
char *j
= NULL
;
927 assert_se(j
= path_join(k
, f
));
928 assert_se(path_equal(input
, j
));
933 TEST(path_extract_directory
) {
934 test_path_extract_directory_one(NULL
, NULL
, -EINVAL
);
935 test_path_extract_directory_one("a/b/c", "a/b", 0);
936 test_path_extract_directory_one("a/b/c/", "a/b", 0);
937 test_path_extract_directory_one("/", NULL
, -EADDRNOTAVAIL
);
938 test_path_extract_directory_one("//", NULL
, -EADDRNOTAVAIL
);
939 test_path_extract_directory_one("///", NULL
, -EADDRNOTAVAIL
);
940 test_path_extract_directory_one("/.", NULL
, -EADDRNOTAVAIL
);
941 test_path_extract_directory_one(".", NULL
, -EADDRNOTAVAIL
);
942 test_path_extract_directory_one("./", NULL
, -EADDRNOTAVAIL
);
943 test_path_extract_directory_one("./.", NULL
, -EADDRNOTAVAIL
);
944 test_path_extract_directory_one("././", NULL
, -EADDRNOTAVAIL
);
945 test_path_extract_directory_one("././/", NULL
, -EADDRNOTAVAIL
);
946 test_path_extract_directory_one("/foo/a", "/foo", 0);
947 test_path_extract_directory_one("/foo/a/", "/foo", 0);
948 test_path_extract_directory_one("", NULL
, -EINVAL
);
949 test_path_extract_directory_one("a", NULL
, -EDESTADDRREQ
);
950 test_path_extract_directory_one("a/", NULL
, -EDESTADDRREQ
);
951 test_path_extract_directory_one("a/././//.", NULL
, -EDESTADDRREQ
);
952 test_path_extract_directory_one("/a", "/", 0);
953 test_path_extract_directory_one("/a/", "/", 0);
954 test_path_extract_directory_one("/a//./.", "/", 0);
955 test_path_extract_directory_one("/////////////a/////////////", "/", 0);
956 test_path_extract_directory_one("//./a/.///b./././.c//./d//.", "/a/b./.c", 0);
957 test_path_extract_directory_one("xx/.", NULL
, -EDESTADDRREQ
);
958 test_path_extract_directory_one("xx/..", NULL
, -EINVAL
);
959 test_path_extract_directory_one("..", NULL
, -EINVAL
);
960 test_path_extract_directory_one("/..", NULL
, -EINVAL
);
961 test_path_extract_directory_one("../", NULL
, -EINVAL
);
964 TEST(filename_is_valid
) {
965 char foo
[NAME_MAX
+2];
967 assert_se(!filename_is_valid(""));
968 assert_se(!filename_is_valid("/bar/foo"));
969 assert_se(!filename_is_valid("/"));
970 assert_se(!filename_is_valid("."));
971 assert_se(!filename_is_valid(".."));
972 assert_se(!filename_is_valid("bar/foo"));
973 assert_se(!filename_is_valid("bar/foo/"));
974 assert_se(!filename_is_valid("bar//"));
976 memset(foo
, 'a', sizeof(foo
) - 1);
979 assert_se(!filename_is_valid(foo
));
981 assert_se(filename_is_valid("foo_bar-333"));
982 assert_se(filename_is_valid("o.o"));
985 static void test_path_is_valid_and_safe_one(const char *p
, bool ret
) {
986 log_debug("/* %s(\"%s\") */", __func__
, strnull(p
));
988 assert_se(path_is_valid(p
) == ret
);
990 ret
= !streq(p
, "..") &&
991 !startswith(p
, "../") &&
992 !endswith(p
, "/..") &&
994 assert_se(path_is_safe(p
) == ret
);
997 TEST(path_is_valid_and_safe
) {
998 char foo
[PATH_MAX
+2];
1001 test_path_is_valid_and_safe_one("", false);
1002 test_path_is_valid_and_safe_one("/bar/foo", true);
1003 test_path_is_valid_and_safe_one("/bar/foo/", true);
1004 test_path_is_valid_and_safe_one("/bar/foo/", true);
1005 test_path_is_valid_and_safe_one("//bar//foo//", true);
1006 test_path_is_valid_and_safe_one("/", true);
1007 test_path_is_valid_and_safe_one("/////", true);
1008 test_path_is_valid_and_safe_one("/////.///.////...///..//.", true);
1009 test_path_is_valid_and_safe_one(".", true);
1010 test_path_is_valid_and_safe_one("..", true);
1011 test_path_is_valid_and_safe_one("bar/foo", true);
1012 test_path_is_valid_and_safe_one("bar/foo/", true);
1013 test_path_is_valid_and_safe_one("bar//", true);
1015 memset(foo
, 'a', sizeof(foo
) -1);
1018 test_path_is_valid_and_safe_one(foo
, false);
1020 c
= strjoina("/xxx/", foo
, "/yyy");
1021 test_path_is_valid_and_safe_one(c
, false);
1023 test_path_is_valid_and_safe_one("foo_bar-333", true);
1024 test_path_is_valid_and_safe_one("o.o", true);
1027 TEST(hidden_or_backup_file
) {
1028 assert_se(hidden_or_backup_file(".hidden"));
1029 assert_se(hidden_or_backup_file("..hidden"));
1030 assert_se(!hidden_or_backup_file("hidden."));
1032 assert_se(hidden_or_backup_file("backup~"));
1033 assert_se(hidden_or_backup_file(".backup~"));
1035 assert_se(hidden_or_backup_file("lost+found"));
1036 assert_se(hidden_or_backup_file("aquota.user"));
1037 assert_se(hidden_or_backup_file("aquota.group"));
1039 assert_se(hidden_or_backup_file("test.rpmnew"));
1040 assert_se(hidden_or_backup_file("test.dpkg-old"));
1041 assert_se(hidden_or_backup_file("test.dpkg-remove"));
1042 assert_se(hidden_or_backup_file("test.swp"));
1044 assert_se(!hidden_or_backup_file("test.rpmnew."));
1045 assert_se(!hidden_or_backup_file("test.dpkg-old.foo"));
1048 TEST(skip_dev_prefix
) {
1049 assert_se(streq(skip_dev_prefix("/"), "/"));
1050 assert_se(streq(skip_dev_prefix("/dev"), ""));
1051 assert_se(streq(skip_dev_prefix("/dev/"), ""));
1052 assert_se(streq(skip_dev_prefix("/dev/foo"), "foo"));
1053 assert_se(streq(skip_dev_prefix("/dev/foo/bar"), "foo/bar"));
1054 assert_se(streq(skip_dev_prefix("//dev"), ""));
1055 assert_se(streq(skip_dev_prefix("//dev//"), ""));
1056 assert_se(streq(skip_dev_prefix("/dev///foo"), "foo"));
1057 assert_se(streq(skip_dev_prefix("///dev///foo///bar"), "foo///bar"));
1058 assert_se(streq(skip_dev_prefix("//foo"), "//foo"));
1059 assert_se(streq(skip_dev_prefix("foo"), "foo"));
1062 TEST(empty_or_root
) {
1063 assert_se(empty_or_root(NULL
));
1064 assert_se(empty_or_root(""));
1065 assert_se(empty_or_root("/"));
1066 assert_se(empty_or_root("//"));
1067 assert_se(empty_or_root("///"));
1068 assert_se(empty_or_root("/////////////////"));
1069 assert_se(!empty_or_root("xxx"));
1070 assert_se(!empty_or_root("/xxx"));
1071 assert_se(!empty_or_root("/xxx/"));
1072 assert_se(!empty_or_root("//yy//"));
1075 TEST(path_startswith_set
) {
1076 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/foo/bar", "/zzz"), ""));
1077 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/foo/", "/zzz"), "bar"));
1078 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/foo", "/zzz"), "bar"));
1079 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/", "/zzz"), "foo/bar"));
1080 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "", "/zzz"), NULL
));
1082 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/foo/bar", "/zzz"), NULL
));
1083 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/foo/", "/zzz"), "bar2"));
1084 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/foo", "/zzz"), "bar2"));
1085 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/", "/zzz"), "foo/bar2"));
1086 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "", "/zzz"), NULL
));
1088 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/foo/bar", "/zzz"), NULL
));
1089 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/foo/", "/zzz"), NULL
));
1090 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/foo", "/zzz"), NULL
));
1091 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/", "/zzz"), "foo2/bar"));
1092 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "", "/zzz"), NULL
));
1095 TEST(path_startswith_strv
) {
1096 assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/foo/bar", "/zzz")), ""));
1097 assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/foo/", "/zzz")), "bar"));
1098 assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/foo", "/zzz")), "bar"));
1099 assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/", "/zzz")), "foo/bar"));
1100 assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "", "/zzz")), NULL
));
1102 assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "/foo/bar", "/zzz")), NULL
));
1103 assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "/foo/", "/zzz")), "bar2"));
1104 assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "/foo", "/zzz")), "bar2"));
1105 assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "/", "/zzz")), "foo/bar2"));
1106 assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "", "/zzz")), NULL
));
1108 assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "/foo/bar", "/zzz")), NULL
));
1109 assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "/foo/", "/zzz")), NULL
));
1110 assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "/foo", "/zzz")), NULL
));
1111 assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "/", "/zzz")), "foo2/bar"));
1112 assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "", "/zzz")), NULL
));
1115 static void test_path_glob_can_match_one(const char *pattern
, const char *prefix
, const char *expected
) {
1116 _cleanup_free_
char *result
= NULL
;
1118 log_debug("%s(%s, %s, %s)", __func__
, pattern
, prefix
, strnull(expected
));
1120 assert_se(path_glob_can_match(pattern
, prefix
, &result
) == !!expected
);
1121 assert_se(streq_ptr(result
, expected
));
1124 TEST(path_glob_can_match
) {
1125 test_path_glob_can_match_one("/foo/hoge/aaa", "/foo/hoge/aaa/bbb", NULL
);
1126 test_path_glob_can_match_one("/foo/hoge/aaa", "/foo/hoge/aaa", "/foo/hoge/aaa");
1127 test_path_glob_can_match_one("/foo/hoge/aaa", "/foo/hoge", "/foo/hoge/aaa");
1128 test_path_glob_can_match_one("/foo/hoge/aaa", "/foo", "/foo/hoge/aaa");
1129 test_path_glob_can_match_one("/foo/hoge/aaa", "/", "/foo/hoge/aaa");
1131 test_path_glob_can_match_one("/foo/*/aaa", "/foo/hoge/aaa/bbb", NULL
);
1132 test_path_glob_can_match_one("/foo/*/aaa", "/foo/hoge/aaa", "/foo/hoge/aaa");
1133 test_path_glob_can_match_one("/foo/*/aaa", "/foo/hoge", "/foo/hoge/aaa");
1134 test_path_glob_can_match_one("/foo/*/aaa", "/foo", "/foo/*/aaa");
1135 test_path_glob_can_match_one("/foo/*/aaa", "/", "/foo/*/aaa");
1137 test_path_glob_can_match_one("/foo/*/*/aaa", "/foo/xxx/yyy/aaa/bbb", NULL
);
1138 test_path_glob_can_match_one("/foo/*/*/aaa", "/foo/xxx/yyy/aaa", "/foo/xxx/yyy/aaa");
1139 test_path_glob_can_match_one("/foo/*/*/aaa", "/foo/xxx/yyy", "/foo/xxx/yyy/aaa");
1140 test_path_glob_can_match_one("/foo/*/*/aaa", "/foo/xxx", "/foo/xxx/*/aaa");
1141 test_path_glob_can_match_one("/foo/*/*/aaa", "/foo", "/foo/*/*/aaa");
1142 test_path_glob_can_match_one("/foo/*/*/aaa", "/", "/foo/*/*/aaa");
1144 test_path_glob_can_match_one("/foo/*/aaa/*", "/foo/xxx/aaa/bbb/ccc", NULL
);
1145 test_path_glob_can_match_one("/foo/*/aaa/*", "/foo/xxx/aaa/bbb", "/foo/xxx/aaa/bbb");
1146 test_path_glob_can_match_one("/foo/*/aaa/*", "/foo/xxx/ccc", NULL
);
1147 test_path_glob_can_match_one("/foo/*/aaa/*", "/foo/xxx/aaa", "/foo/xxx/aaa/*");
1148 test_path_glob_can_match_one("/foo/*/aaa/*", "/foo/xxx", "/foo/xxx/aaa/*");
1149 test_path_glob_can_match_one("/foo/*/aaa/*", "/foo", "/foo/*/aaa/*");
1150 test_path_glob_can_match_one("/foo/*/aaa/*", "/", "/foo/*/aaa/*");
1154 log_info("PATH_MAX=%zu\n"
1155 "FILENAME_MAX=%zu\n"
1158 (size_t) FILENAME_MAX
,
1161 assert_cc(FILENAME_MAX
== PATH_MAX
);
1164 DEFINE_TEST_MAIN(LOG_DEBUG
);