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"));
50 assert_se(path_equal_filename("/a/c", "/b/c"));
51 assert_se(path_equal_filename("/a", "/a"));
52 assert_se(!path_equal_filename("/a/b", "/a/c"));
53 assert_se(!path_equal_filename("/b", "/c"));
56 static void test_path_simplify_one(const char *in
, const char *out
) {
61 log_debug("/* test_path_simplify(%s) → %s (expected: %s) */", in
, p
, out
);
62 assert_se(streq(p
, out
));
66 _cleanup_free_
char *hoge
= NULL
, *hoge_out
= NULL
;
67 char foo
[NAME_MAX
* 2];
69 test_path_simplify_one("", "");
70 test_path_simplify_one("aaa/bbb////ccc", "aaa/bbb/ccc");
71 test_path_simplify_one("//aaa/.////ccc", "/aaa/ccc");
72 test_path_simplify_one("///", "/");
73 test_path_simplify_one("///.//", "/");
74 test_path_simplify_one("///.//.///", "/");
75 test_path_simplify_one("////.././///../.", "/../..");
76 test_path_simplify_one(".", ".");
77 test_path_simplify_one("./", ".");
78 test_path_simplify_one(".///.//./.", ".");
79 test_path_simplify_one(".///.//././/", ".");
80 test_path_simplify_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.",
81 "/aaa/.bbb/../c./d.dd/..eeee");
82 test_path_simplify_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/..",
83 "/aaa/.bbb/../c./d.dd/..eeee/..");
84 test_path_simplify_one(".//./aaa///.//./.bbb/..///c.//d.dd///..eeee/..",
85 "aaa/.bbb/../c./d.dd/..eeee/..");
86 test_path_simplify_one("..//./aaa///.//./.bbb/..///c.//d.dd///..eeee/..",
87 "../aaa/.bbb/../c./d.dd/..eeee/..");
89 memset(foo
, 'a', sizeof(foo
) -1);
92 test_path_simplify_one(foo
, foo
);
94 hoge
= strjoin("/", foo
);
96 test_path_simplify_one(hoge
, hoge
);
99 hoge
= strjoin("a////.//././//./b///././/./c/////././//./", foo
, "//.//////d/e/.//f/");
102 hoge_out
= strjoin("a/b/c/", foo
, "//.//////d/e/.//f/");
105 test_path_simplify_one(hoge
, hoge_out
);
108 static void test_path_compare_one(const char *a
, const char *b
, int expected
) {
111 assert_se(path_compare(a
, a
) == 0);
112 assert_se(path_compare(b
, b
) == 0);
114 r
= path_compare(a
, b
);
115 assert_se((r
> 0) == (expected
> 0) && (r
< 0) == (expected
< 0));
116 r
= path_compare(b
, a
);
117 assert_se((r
< 0) == (expected
> 0) && (r
> 0) == (expected
< 0));
119 assert_se(path_equal(a
, a
) == 1);
120 assert_se(path_equal(b
, b
) == 1);
121 assert_se(path_equal(a
, b
) == (expected
== 0));
122 assert_se(path_equal(b
, a
) == (expected
== 0));
126 test_path_compare_one("/goo", "/goo", 0);
127 test_path_compare_one("/goo", "/goo", 0);
128 test_path_compare_one("//goo", "/goo", 0);
129 test_path_compare_one("//goo/////", "/goo", 0);
130 test_path_compare_one("goo/////", "goo", 0);
131 test_path_compare_one("/goo/boo", "/goo//boo", 0);
132 test_path_compare_one("//goo/boo", "/goo/boo//", 0);
133 test_path_compare_one("//goo/././//./boo//././//", "/goo/boo//.", 0);
134 test_path_compare_one("/.", "//.///", 0);
135 test_path_compare_one("/x", "x/", 1);
136 test_path_compare_one("x/", "/", -1);
137 test_path_compare_one("/x/./y", "x/y", 1);
138 test_path_compare_one("/x/./y", "/x/y", 0);
139 test_path_compare_one("/x/./././y", "/x/y/././.", 0);
140 test_path_compare_one("./x/./././y", "./x/y/././.", 0);
141 test_path_compare_one(".", "./.", 0);
142 test_path_compare_one(".", "././.", 0);
143 test_path_compare_one("./..", ".", 1);
144 test_path_compare_one("x/.y", "x/y", -1);
145 test_path_compare_one("foo", "/foo", -1);
146 test_path_compare_one("/foo", "/foo/bar", -1);
147 test_path_compare_one("/foo/aaa", "/foo/b", -1);
148 test_path_compare_one("/foo/aaa", "/foo/b/a", -1);
149 test_path_compare_one("/foo/a", "/foo/aaa", -1);
150 test_path_compare_one("/foo/a/b", "/foo/aaa", -1);
153 TEST(path_equal_root
) {
154 /* Nail down the details of how path_equal("/", ...) works. */
156 assert_se(path_equal("/", "/"));
157 assert_se(path_equal("/", "//"));
159 assert_se(path_equal("/", "/./"));
160 assert_se(!path_equal("/", "/../"));
162 assert_se(!path_equal("/", "/.../"));
164 /* Make sure that files_same works as expected. */
166 assert_se(files_same("/", "/", 0) > 0);
167 assert_se(files_same("/", "/", AT_SYMLINK_NOFOLLOW
) > 0);
168 assert_se(files_same("/", "//", 0) > 0);
169 assert_se(files_same("/", "//", AT_SYMLINK_NOFOLLOW
) > 0);
171 assert_se(files_same("/", "/./", 0) > 0);
172 assert_se(files_same("/", "/./", AT_SYMLINK_NOFOLLOW
) > 0);
173 assert_se(files_same("/", "/../", 0) > 0);
174 assert_se(files_same("/", "/../", AT_SYMLINK_NOFOLLOW
) > 0);
176 assert_se(files_same("/", "/.../", 0) == -ENOENT
);
177 assert_se(files_same("/", "/.../", AT_SYMLINK_NOFOLLOW
) == -ENOENT
);
179 /* The same for path_equal_or_files_same. */
181 assert_se(path_equal_or_files_same("/", "/", 0));
182 assert_se(path_equal_or_files_same("/", "/", AT_SYMLINK_NOFOLLOW
));
183 assert_se(path_equal_or_files_same("/", "//", 0));
184 assert_se(path_equal_or_files_same("/", "//", AT_SYMLINK_NOFOLLOW
));
186 assert_se(path_equal_or_files_same("/", "/./", 0));
187 assert_se(path_equal_or_files_same("/", "/./", AT_SYMLINK_NOFOLLOW
));
188 assert_se(path_equal_or_files_same("/", "/../", 0));
189 assert_se(path_equal_or_files_same("/", "/../", AT_SYMLINK_NOFOLLOW
));
191 assert_se(!path_equal_or_files_same("/", "/.../", 0));
192 assert_se(!path_equal_or_files_same("/", "/.../", AT_SYMLINK_NOFOLLOW
));
195 TEST(find_executable_full
) {
197 char* test_file_name
;
198 _cleanup_close_
int fd
= -EBADF
;
199 char fn
[] = "/tmp/test-XXXXXX";
201 assert_se(find_executable_full("sh", NULL
, NULL
, true, &p
, NULL
) == 0);
203 assert_se(streq(basename(p
), "sh"));
206 assert_se(find_executable_full("sh", NULL
, NULL
, false, &p
, NULL
) == 0);
208 assert_se(streq(basename(p
), "sh"));
211 _cleanup_free_
char *oldpath
= NULL
;
214 assert_se(oldpath
= strdup(p
));
216 assert_se(unsetenv("PATH") == 0);
218 assert_se(find_executable_full("sh", NULL
, NULL
, true, &p
, NULL
) == 0);
220 assert_se(streq(basename(p
), "sh"));
223 assert_se(find_executable_full("sh", NULL
, NULL
, false, &p
, NULL
) == 0);
225 assert_se(streq(basename(p
), "sh"));
229 assert_se(setenv("PATH", oldpath
, true) >= 0);
231 assert_se((fd
= mkostemp_safe(fn
)) >= 0);
232 assert_se(fchmod(fd
, 0755) >= 0);
234 test_file_name
= basename(fn
);
236 assert_se(find_executable_full(test_file_name
, NULL
, STRV_MAKE("/doesnotexist", "/tmp", "/bin"), false, &p
, NULL
) == 0);
238 assert_se(streq(p
, fn
));
242 assert_se(find_executable_full(test_file_name
, NULL
, STRV_MAKE("/doesnotexist", "/tmp", "/bin"), false, &p
, NULL
) == -ENOENT
);
245 TEST(find_executable
) {
248 assert_se(find_executable("/bin/sh", &p
) == 0);
250 assert_se(path_equal(p
, "/bin/sh"));
253 assert_se(find_executable(saved_argv
[0], &p
) == 0);
255 assert_se(endswith(p
, "/test-path-util"));
256 assert_se(path_is_absolute(p
));
259 assert_se(find_executable("sh", &p
) == 0);
261 assert_se(endswith(p
, "/sh"));
262 assert_se(path_is_absolute(p
));
265 assert_se(find_executable("/bin/touch", &p
) == 0);
266 assert_se(streq(p
, "/bin/touch"));
269 assert_se(find_executable("touch", &p
) == 0);
270 assert_se(path_is_absolute(p
));
271 assert_se(streq(basename(p
), "touch"));
274 assert_se(find_executable("xxxx-xxxx", &p
) == -ENOENT
);
275 assert_se(find_executable("/some/dir/xxxx-xxxx", &p
) == -ENOENT
);
276 assert_se(find_executable("/proc/filesystems", &p
) == -EACCES
);
279 static void test_find_executable_exec_one(const char *path
) {
280 _cleanup_free_
char *t
= NULL
;
281 _cleanup_close_
int fd
= -EBADF
;
285 r
= find_executable_full(path
, NULL
, NULL
, false, &t
, &fd
);
287 log_info_errno(r
, "%s: %s → %s: %d/%m", __func__
, path
, t
?: "-", fd
);
289 assert_se(fd
> STDERR_FILENO
);
290 assert_se(path_is_absolute(t
));
291 if (path_is_absolute(path
))
292 assert_se(streq(t
, path
));
297 r
= fexecve_or_execve(fd
, t
, STRV_MAKE(t
, "--version"), STRV_MAKE(NULL
));
298 log_error_errno(r
, "[f]execve: %m");
302 assert_se(wait_for_terminate_and_check(t
, pid
, WAIT_LOG
) == 0);
305 TEST(find_executable_exec
) {
306 test_find_executable_exec_one("touch");
307 test_find_executable_exec_one("/bin/touch");
309 _cleanup_free_
char *script
= NULL
;
310 assert_se(get_testdata_dir("test-path-util/script.sh", &script
) >= 0);
311 test_find_executable_exec_one(script
);
315 static const char* const values
[] = {
328 PATH_FOREACH_PREFIX_MORE(s
, "/a/b/c/d") {
329 log_error("---%s---", s
);
330 assert_se(streq(s
, values
[i
++]));
332 assert_se(values
[i
] == NULL
);
335 PATH_FOREACH_PREFIX(s
, "/a/b/c/d") {
336 log_error("---%s---", s
);
337 assert_se(streq(s
, values
[i
++]));
339 assert_se(values
[i
] == NULL
);
342 PATH_FOREACH_PREFIX_MORE(s
, "////a////b////c///d///////")
343 assert_se(streq(s
, values
[i
++]));
344 assert_se(values
[i
] == NULL
);
347 PATH_FOREACH_PREFIX(s
, "////a////b////c///d///////")
348 assert_se(streq(s
, values
[i
++]));
349 assert_se(values
[i
] == NULL
);
351 PATH_FOREACH_PREFIX(s
, "////")
352 assert_not_reached();
355 PATH_FOREACH_PREFIX_MORE(s
, "////") {
357 assert_se(streq(s
, ""));
362 PATH_FOREACH_PREFIX(s
, "")
363 assert_not_reached();
366 PATH_FOREACH_PREFIX_MORE(s
, "") {
368 assert_se(streq(s
, ""));
374 #define test_join(expected, ...) { \
375 _cleanup_free_ char *z = NULL; \
376 z = path_join(__VA_ARGS__); \
377 log_debug("got \"%s\", expected \"%s\"", z, expected); \
378 assert_se(streq(z, expected)); \
381 test_join("/root/a/b/c", "/root", "/a/b", "/c");
382 test_join("/root/a/b/c", "/root", "a/b", "c");
383 test_join("/root/a/b/c", "/root", "/a/b", "c");
384 test_join("/root/c", "/root", "/", "c");
385 test_join("/root/", "/root", "/", NULL
);
387 test_join("/a/b/c", "", "/a/b", "/c");
388 test_join("a/b/c", "", "a/b", "c");
389 test_join("/a/b/c", "", "/a/b", "c");
390 test_join("/c", "", "/", "c");
391 test_join("/", "", "/", NULL
);
393 test_join("/a/b/c", NULL
, "/a/b", "/c");
394 test_join("a/b/c", NULL
, "a/b", "c");
395 test_join("/a/b/c", NULL
, "/a/b", "c");
396 test_join("/c", NULL
, "/", "c");
397 test_join("/", NULL
, "/", NULL
);
399 test_join("", "", NULL
);
400 test_join("", NULL
, "");
401 test_join("", NULL
, NULL
);
403 test_join("foo/bar", "foo", "bar");
404 test_join("foo/bar", "", "foo", "bar");
405 test_join("foo/bar", NULL
, "foo", NULL
, "bar");
406 test_join("foo/bar", "", "foo", "", "bar", "");
407 test_join("foo/bar", "", "", "", "", "foo", "", "", "", "bar", "", "", "");
409 test_join("//foo///bar//", "", "/", "", "/foo/", "", "/", "", "/bar/", "", "/", "");
410 test_join("/foo/bar/", "/", "foo", "/", "bar", "/");
411 test_join("foo/bar/baz", "foo", "bar", "baz");
412 test_join("foo/bar/baz", "foo/", "bar", "/baz");
413 test_join("foo//bar//baz", "foo/", "/bar/", "/baz");
414 test_join("//foo////bar////baz//", "//foo/", "///bar/", "///baz//");
418 _cleanup_free_
char *p
= NULL
;
420 assert_se(path_extend(&p
, "foo", "bar", "baz") == p
);
421 assert_se(streq(p
, "foo/bar/baz"));
423 assert_se(path_extend(&p
, "foo", "bar", "baz") == p
);
424 assert_se(streq(p
, "foo/bar/baz/foo/bar/baz"));
427 assert_se(path_extend(&p
, "foo") == p
);
428 assert_se(streq(p
, "foo"));
430 assert_se(path_extend(&p
, "/foo") == p
);
431 assert_se(streq(p
, "foo/foo"));
432 assert_se(path_extend(&p
, "/waaaah/wahhh//") == p
);
433 assert_se(streq(p
, "foo/foo/waaaah/wahhh//")); /* path_extend() does not drop redundant slashes */
434 assert_se(path_extend(&p
, "/aaa/bbb/") == p
);
435 assert_se(streq(p
, "foo/foo/waaaah/wahhh///aaa/bbb/")); /* but not add an extra slash */
437 assert_se(free_and_strdup(&p
, "/") >= 0);
438 assert_se(path_extend(&p
, "foo") == p
);
439 assert_se(streq(p
, "/foo"));
443 /* Ensure we use a sane default for PATH. */
444 assert_se(unsetenv("PATH") == 0);
446 /* fsck.minix is provided by util-linux and will probably exist. */
447 assert_se(fsck_exists_for_fstype("minix") == 1);
449 assert_se(fsck_exists_for_fstype("AbCdE") == 0);
450 assert_se(fsck_exists_for_fstype("/../bin/") == 0);
453 static void test_path_make_relative_one(const char *from
, const char *to
, const char *expected
) {
454 _cleanup_free_
char *z
= NULL
;
457 log_info("/* %s(%s, %s) */", __func__
, from
, to
);
459 r
= path_make_relative(from
, to
, &z
);
460 assert_se((r
>= 0) == !!expected
);
461 assert_se(streq_ptr(z
, expected
));
464 TEST(path_make_relative
) {
465 test_path_make_relative_one("some/relative/path", "/some/path", NULL
);
466 test_path_make_relative_one("/some/path", "some/relative/path", NULL
);
467 test_path_make_relative_one("/some/dotdot/../path", "/some/path", NULL
);
469 test_path_make_relative_one("/", "/", ".");
470 test_path_make_relative_one("/", "/some/path", "some/path");
471 test_path_make_relative_one("/some/path", "/some/path", ".");
472 test_path_make_relative_one("/some/path", "/some/path/in/subdir", "in/subdir");
473 test_path_make_relative_one("/some/path", "/", "../..");
474 test_path_make_relative_one("/some/path", "/some/other/path", "../other/path");
475 test_path_make_relative_one("/some/path/./dot", "/some/further/path", "../../further/path");
476 test_path_make_relative_one("//extra.//.//./.slashes//./won't////fo.ol///anybody//", "/././/extra././/.slashes////ar.e/.just/././.fine///", "../../../ar.e/.just/.fine");
479 static void test_path_make_relative_parent_one(const char *from
, const char *to
, const char *expected
) {
480 _cleanup_free_
char *z
= NULL
;
483 log_info("/* %s(%s, %s) */", __func__
, from
, to
);
485 r
= path_make_relative_parent(from
, to
, &z
);
486 assert_se((r
>= 0) == !!expected
);
487 assert_se(streq_ptr(z
, expected
));
490 TEST(path_make_relative_parent
) {
491 test_path_make_relative_parent_one("some/relative/path/hoge", "/some/path", NULL
);
492 test_path_make_relative_parent_one("/some/path/hoge", "some/relative/path", NULL
);
493 test_path_make_relative_parent_one("/some/dotdot/../path/hoge", "/some/path", NULL
);
494 test_path_make_relative_parent_one("/", "/aaa", NULL
);
496 test_path_make_relative_parent_one("/hoge", "/", ".");
497 test_path_make_relative_parent_one("/hoge", "/some/path", "some/path");
498 test_path_make_relative_parent_one("/some/path/hoge", "/some/path", ".");
499 test_path_make_relative_parent_one("/some/path/hoge", "/some/path/in/subdir", "in/subdir");
500 test_path_make_relative_parent_one("/some/path/hoge", "/", "../..");
501 test_path_make_relative_parent_one("/some/path/hoge", "/some/other/path", "../other/path");
502 test_path_make_relative_parent_one("/some/path/./dot/hoge", "/some/further/path", "../../further/path");
503 test_path_make_relative_parent_one("//extra.//.//./.slashes//./won't////fo.ol///anybody//hoge", "/././/extra././/.slashes////ar.e/.just/././.fine///", "../../../ar.e/.just/.fine");
506 TEST(path_strv_resolve
) {
507 char tmp_dir
[] = "/tmp/test-path-util-XXXXXX";
508 _cleanup_strv_free_
char **search_dirs
= NULL
;
509 _cleanup_strv_free_
char **absolute_dirs
= NULL
;
511 assert_se(mkdtemp(tmp_dir
) != NULL
);
513 search_dirs
= strv_new("/dir1", "/dir2", "/dir3");
514 assert_se(search_dirs
);
515 STRV_FOREACH(d
, search_dirs
) {
516 char *p
= path_join(tmp_dir
, *d
);
518 assert_se(strv_push(&absolute_dirs
, p
) == 0);
521 assert_se(mkdir(absolute_dirs
[0], 0700) == 0);
522 assert_se(mkdir(absolute_dirs
[1], 0700) == 0);
523 assert_se(symlink("dir2", absolute_dirs
[2]) == 0);
525 path_strv_resolve(search_dirs
, tmp_dir
);
526 assert_se(streq(search_dirs
[0], "/dir1"));
527 assert_se(streq(search_dirs
[1], "/dir2"));
528 assert_se(streq(search_dirs
[2], "/dir2"));
530 assert_se(rm_rf(tmp_dir
, REMOVE_ROOT
|REMOVE_PHYSICAL
) == 0);
533 static void test_path_startswith_one(const char *path
, const char *prefix
, const char *skipped
, const char *expected
) {
536 log_debug("/* %s(%s, %s) */", __func__
, path
, prefix
);
538 p
= path_startswith(path
, prefix
);
539 assert_se(streq_ptr(p
, expected
));
541 q
= strjoina(skipped
, p
);
542 assert_se(streq(q
, path
));
543 assert_se(p
== path
+ strlen(skipped
));
547 TEST(path_startswith
) {
548 test_path_startswith_one("/foo/bar/barfoo/", "/foo", "/foo/", "bar/barfoo/");
549 test_path_startswith_one("/foo/bar/barfoo/", "/foo/", "/foo/", "bar/barfoo/");
550 test_path_startswith_one("/foo/bar/barfoo/", "/", "/", "foo/bar/barfoo/");
551 test_path_startswith_one("/foo/bar/barfoo/", "////", "/", "foo/bar/barfoo/");
552 test_path_startswith_one("/foo/bar/barfoo/", "/foo//bar/////barfoo///", "/foo/bar/barfoo/", "");
553 test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/barfoo////", "/foo/bar/barfoo/", "");
554 test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar///barfoo/", "/foo/bar/barfoo/", "");
555 test_path_startswith_one("/foo/bar/barfoo/", "/foo////bar/barfoo/", "/foo/bar/barfoo/", "");
556 test_path_startswith_one("/foo/bar/barfoo/", "////foo/bar/barfoo/", "/foo/bar/barfoo/", "");
557 test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/barfoo", "/foo/bar/barfoo/", "");
559 test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/barfooa/", NULL
, NULL
);
560 test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/barfooa", NULL
, NULL
);
561 test_path_startswith_one("/foo/bar/barfoo/", "", NULL
, NULL
);
562 test_path_startswith_one("/foo/bar/barfoo/", "/bar/foo", NULL
, NULL
);
563 test_path_startswith_one("/foo/bar/barfoo/", "/f/b/b/", NULL
, NULL
);
564 test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/barfo", NULL
, NULL
);
565 test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/bar", NULL
, NULL
);
566 test_path_startswith_one("/foo/bar/barfoo/", "/fo", NULL
, NULL
);
569 static void test_prefix_root_one(const char *r
, const char *p
, const char *expected
) {
570 _cleanup_free_
char *s
= NULL
;
573 assert_se(s
= path_join(r
, p
));
574 assert_se(path_equal_ptr(s
, expected
));
576 t
= prefix_roota(r
, p
);
578 assert_se(path_equal_ptr(t
, expected
));
582 test_prefix_root_one("/", "/foo", "/foo");
583 test_prefix_root_one(NULL
, "/foo", "/foo");
584 test_prefix_root_one("", "/foo", "/foo");
585 test_prefix_root_one("///", "/foo", "/foo");
586 test_prefix_root_one("/", "////foo", "/foo");
587 test_prefix_root_one(NULL
, "////foo", "/foo");
588 test_prefix_root_one("/", "foo", "/foo");
589 test_prefix_root_one("", "foo", "foo");
590 test_prefix_root_one(NULL
, "foo", "foo");
592 test_prefix_root_one("/foo", "/bar", "/foo/bar");
593 test_prefix_root_one("/foo", "bar", "/foo/bar");
594 test_prefix_root_one("foo", "bar", "foo/bar");
595 test_prefix_root_one("/foo/", "/bar", "/foo/bar");
596 test_prefix_root_one("/foo/", "//bar", "/foo/bar");
597 test_prefix_root_one("/foo///", "//bar", "/foo/bar");
600 TEST(file_in_same_dir
) {
603 assert_se(file_in_same_dir("/", "a", &t
) == -EADDRNOTAVAIL
);
605 assert_se(file_in_same_dir("/", "/a", &t
) >= 0);
606 assert_se(streq(t
, "/a"));
609 assert_se(file_in_same_dir("", "a", &t
) == -EINVAL
);
611 assert_se(file_in_same_dir("a/", "x", &t
) >= 0);
612 assert_se(streq(t
, "x"));
615 assert_se(file_in_same_dir("bar/foo", "bar", &t
) >= 0);
616 assert_se(streq(t
, "bar/bar"));
620 static void test_path_find_first_component_one(
626 log_debug("/* %s(\"%s\", accept_dot_dot=%s) */", __func__
, strnull(path
), yes_no(accept_dot_dot
));
628 for (const char *p
= path
;;) {
632 r
= path_find_first_component(&p
, accept_dot_dot
, &e
);
636 assert_se(p
== path
+ strlen_ptr(path
));
642 assert_se(strv_isempty(expected
));
647 assert_se(strcspn(e
, "/") == (size_t) r
);
648 assert_se(strlen_ptr(*expected
) == (size_t) r
);
649 assert_se(strneq(e
, *expected
++, r
));
653 TEST(path_find_first_component
) {
654 _cleanup_free_
char *hoge
= NULL
;
655 char foo
[NAME_MAX
* 2];
657 test_path_find_first_component_one(NULL
, false, NULL
, 0);
658 test_path_find_first_component_one("", false, NULL
, 0);
659 test_path_find_first_component_one("/", false, NULL
, 0);
660 test_path_find_first_component_one(".", false, NULL
, 0);
661 test_path_find_first_component_one("./", false, NULL
, 0);
662 test_path_find_first_component_one("./.", false, NULL
, 0);
663 test_path_find_first_component_one("..", false, NULL
, -EINVAL
);
664 test_path_find_first_component_one("/..", false, NULL
, -EINVAL
);
665 test_path_find_first_component_one("./..", false, NULL
, -EINVAL
);
666 test_path_find_first_component_one("////./././//.", false, NULL
, 0);
667 test_path_find_first_component_one("a/b/c", false, STRV_MAKE("a", "b", "c"), 0);
668 test_path_find_first_component_one("././//.///aa/bbb//./ccc", false, STRV_MAKE("aa", "bbb", "ccc"), 0);
669 test_path_find_first_component_one("././//.///aa/.../../bbb//./ccc/.", false, STRV_MAKE("aa", "..."), -EINVAL
);
670 test_path_find_first_component_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.", false, STRV_MAKE("aaa", ".bbb"), -EINVAL
);
671 test_path_find_first_component_one("a/foo./b", false, STRV_MAKE("a", "foo.", "b"), 0);
673 test_path_find_first_component_one(NULL
, true, NULL
, 0);
674 test_path_find_first_component_one("", true, NULL
, 0);
675 test_path_find_first_component_one("/", true, NULL
, 0);
676 test_path_find_first_component_one(".", true, NULL
, 0);
677 test_path_find_first_component_one("./", true, NULL
, 0);
678 test_path_find_first_component_one("./.", true, NULL
, 0);
679 test_path_find_first_component_one("..", true, STRV_MAKE(".."), 0);
680 test_path_find_first_component_one("/..", true, STRV_MAKE(".."), 0);
681 test_path_find_first_component_one("./..", true, STRV_MAKE(".."), 0);
682 test_path_find_first_component_one("////./././//.", true, NULL
, 0);
683 test_path_find_first_component_one("a/b/c", true, STRV_MAKE("a", "b", "c"), 0);
684 test_path_find_first_component_one("././//.///aa/bbb//./ccc", true, STRV_MAKE("aa", "bbb", "ccc"), 0);
685 test_path_find_first_component_one("././//.///aa/.../../bbb//./ccc/.", true, STRV_MAKE("aa", "...", "..", "bbb", "ccc"), 0);
686 test_path_find_first_component_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.", true, STRV_MAKE("aaa", ".bbb", "..", "c.", "d.dd", "..eeee"), 0);
687 test_path_find_first_component_one("a/foo./b", true, STRV_MAKE("a", "foo.", "b"), 0);
689 memset(foo
, 'a', sizeof(foo
) -1);
692 test_path_find_first_component_one(foo
, false, NULL
, -EINVAL
);
693 test_path_find_first_component_one(foo
, true, NULL
, -EINVAL
);
695 hoge
= strjoin("a/b/c/", foo
, "//d/e/.//f/");
698 test_path_find_first_component_one(hoge
, false, STRV_MAKE("a", "b", "c"), -EINVAL
);
699 test_path_find_first_component_one(hoge
, true, STRV_MAKE("a", "b", "c"), -EINVAL
);
702 static void test_path_find_last_component_one(
708 log_debug("/* %s(\"%s\", accept_dot_dot=%s) */", __func__
, strnull(path
), yes_no(accept_dot_dot
));
710 for (const char *next
= NULL
;;) {
714 r
= path_find_last_component(path
, accept_dot_dot
, &next
, &e
);
717 assert_se(next
== path
);
721 assert_se(strv_isempty(expected
));
726 assert_se(strcspn(e
, "/") == (size_t) r
);
727 assert_se(strlen_ptr(*expected
) == (size_t) r
);
728 assert_se(strneq(e
, *expected
++, r
));
732 TEST(path_find_last_component
) {
733 _cleanup_free_
char *hoge
= NULL
;
734 char foo
[NAME_MAX
* 2];
736 test_path_find_last_component_one(NULL
, false, NULL
, 0);
737 test_path_find_last_component_one("", false, NULL
, 0);
738 test_path_find_last_component_one("/", false, NULL
, 0);
739 test_path_find_last_component_one(".", false, NULL
, 0);
740 test_path_find_last_component_one("./", false, NULL
, 0);
741 test_path_find_last_component_one("./.", false, NULL
, 0);
742 test_path_find_last_component_one("..", false, NULL
, -EINVAL
);
743 test_path_find_last_component_one("/..", false, NULL
, -EINVAL
);
744 test_path_find_last_component_one("./..", false, NULL
, -EINVAL
);
745 test_path_find_last_component_one("////./././//.", false, NULL
, 0);
746 test_path_find_last_component_one("a/b/c", false, STRV_MAKE("c", "b", "a"), 0);
747 test_path_find_last_component_one("././//.///aa./.bbb//./ccc/././/", false, STRV_MAKE("ccc", ".bbb", "aa."), 0);
748 test_path_find_last_component_one("././//.///aa/../.../bbb//./ccc/.", false, STRV_MAKE("ccc", "bbb", "..."), -EINVAL
);
749 test_path_find_last_component_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.", false, STRV_MAKE("..eeee", "d.dd", "c."), -EINVAL
);
751 test_path_find_last_component_one(NULL
, true, NULL
, 0);
752 test_path_find_last_component_one("", true, NULL
, 0);
753 test_path_find_last_component_one("/", true, NULL
, 0);
754 test_path_find_last_component_one(".", true, NULL
, 0);
755 test_path_find_last_component_one("./", true, NULL
, 0);
756 test_path_find_last_component_one("./.", true, NULL
, 0);
757 test_path_find_last_component_one("..", true, STRV_MAKE(".."), 0);
758 test_path_find_last_component_one("/..", true, STRV_MAKE(".."), 0);
759 test_path_find_last_component_one("./..", true, STRV_MAKE(".."), 0);
760 test_path_find_last_component_one("////./././//.", true, NULL
, 0);
761 test_path_find_last_component_one("a/b/c", true, STRV_MAKE("c", "b", "a"), 0);
762 test_path_find_last_component_one("././//.///aa./.bbb//./ccc/././/", true, STRV_MAKE("ccc", ".bbb", "aa."), 0);
763 test_path_find_last_component_one("././//.///aa/../.../bbb//./ccc/.", true, STRV_MAKE("ccc", "bbb", "...", "..", "aa"), 0);
764 test_path_find_last_component_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.", true, STRV_MAKE("..eeee", "d.dd", "c.", "..", ".bbb", "aaa"), 0);
766 memset(foo
, 'a', sizeof(foo
) -1);
769 test_path_find_last_component_one(foo
, false, NULL
, -EINVAL
);
770 test_path_find_last_component_one(foo
, true, NULL
, -EINVAL
);
772 hoge
= strjoin(foo
, "/a/b/c/");
775 test_path_find_last_component_one(hoge
, false, STRV_MAKE("c", "b", "a"), -EINVAL
);
776 test_path_find_last_component_one(hoge
, true, STRV_MAKE("c", "b", "a"), -EINVAL
);
779 TEST(last_path_component
) {
780 assert_se(last_path_component(NULL
) == NULL
);
781 assert_se(streq(last_path_component("a/b/c"), "c"));
782 assert_se(streq(last_path_component("a/b/c/"), "c/"));
783 assert_se(streq(last_path_component("/"), "/"));
784 assert_se(streq(last_path_component("//"), "/"));
785 assert_se(streq(last_path_component("///"), "/"));
786 assert_se(streq(last_path_component("."), "."));
787 assert_se(streq(last_path_component("./."), "."));
788 assert_se(streq(last_path_component("././"), "./"));
789 assert_se(streq(last_path_component("././/"), ".//"));
790 assert_se(streq(last_path_component("/foo/a"), "a"));
791 assert_se(streq(last_path_component("/foo/a/"), "a/"));
792 assert_se(streq(last_path_component(""), ""));
793 assert_se(streq(last_path_component("a"), "a"));
794 assert_se(streq(last_path_component("a/"), "a/"));
795 assert_se(streq(last_path_component("/a"), "a"));
796 assert_se(streq(last_path_component("/a/"), "a/"));
799 static void test_path_extract_filename_one(const char *input
, const char *output
, int ret
) {
800 _cleanup_free_
char *k
= NULL
;
803 r
= path_extract_filename(input
, &k
);
804 log_info("%s → %s/%s [expected: %s/%s]",
806 strnull(k
), r
< 0 ? STRERROR(r
) : "-",
807 strnull(output
), ret
< 0 ? STRERROR(ret
) : "-");
808 assert_se(streq_ptr(k
, output
));
812 TEST(path_extract_filename
) {
813 test_path_extract_filename_one(NULL
, NULL
, -EINVAL
);
814 test_path_extract_filename_one("a/b/c", "c", 0);
815 test_path_extract_filename_one("a/b/c/", "c", O_DIRECTORY
);
816 test_path_extract_filename_one("/", NULL
, -EADDRNOTAVAIL
);
817 test_path_extract_filename_one("//", NULL
, -EADDRNOTAVAIL
);
818 test_path_extract_filename_one("///", NULL
, -EADDRNOTAVAIL
);
819 test_path_extract_filename_one("/.", NULL
, -EADDRNOTAVAIL
);
820 test_path_extract_filename_one(".", NULL
, -EADDRNOTAVAIL
);
821 test_path_extract_filename_one("./", NULL
, -EADDRNOTAVAIL
);
822 test_path_extract_filename_one("./.", NULL
, -EADDRNOTAVAIL
);
823 test_path_extract_filename_one("././", NULL
, -EADDRNOTAVAIL
);
824 test_path_extract_filename_one("././/", NULL
, -EADDRNOTAVAIL
);
825 test_path_extract_filename_one("/foo/a", "a", 0);
826 test_path_extract_filename_one("/foo/a/", "a", O_DIRECTORY
);
827 test_path_extract_filename_one("", NULL
, -EINVAL
);
828 test_path_extract_filename_one("a", "a", 0);
829 test_path_extract_filename_one("a/", "a", O_DIRECTORY
);
830 test_path_extract_filename_one("a/././//.", "a", O_DIRECTORY
);
831 test_path_extract_filename_one("/a", "a", 0);
832 test_path_extract_filename_one("/a/", "a", O_DIRECTORY
);
833 test_path_extract_filename_one("/a//./.", "a", O_DIRECTORY
);
834 test_path_extract_filename_one("/////////////a/////////////", "a", O_DIRECTORY
);
835 test_path_extract_filename_one("//./a/.///b./././.c//./d//.", "d", O_DIRECTORY
);
836 test_path_extract_filename_one("xx/.", "xx", O_DIRECTORY
);
837 test_path_extract_filename_one("xx/..", NULL
, -EINVAL
);
838 test_path_extract_filename_one("..", NULL
, -EINVAL
);
839 test_path_extract_filename_one("/..", NULL
, -EINVAL
);
840 test_path_extract_filename_one("../", NULL
, -EINVAL
);
843 static void test_path_extract_directory_one(const char *input
, const char *output
, int ret
) {
844 _cleanup_free_
char *k
= NULL
;
847 r
= path_extract_directory(input
, &k
);
848 log_info("%s → %s/%s [expected: %s/%s]",
850 strnull(k
), r
< 0 ? STRERROR(r
) : "-",
851 strnull(output
), STRERROR(ret
));
852 assert_se(streq_ptr(k
, output
));
855 /* Extra safety check: let's make sure that if we split out the filename too (and it works) the
856 * joined parts are identical to the original again */
858 _cleanup_free_
char *f
= NULL
;
860 r
= path_extract_filename(input
, &f
);
862 _cleanup_free_
char *j
= NULL
;
864 assert_se(j
= path_join(k
, f
));
865 assert_se(path_equal(input
, j
));
870 TEST(path_extract_directory
) {
871 test_path_extract_directory_one(NULL
, NULL
, -EINVAL
);
872 test_path_extract_directory_one("a/b/c", "a/b", 0);
873 test_path_extract_directory_one("a/b/c/", "a/b", 0);
874 test_path_extract_directory_one("/", NULL
, -EADDRNOTAVAIL
);
875 test_path_extract_directory_one("//", NULL
, -EADDRNOTAVAIL
);
876 test_path_extract_directory_one("///", NULL
, -EADDRNOTAVAIL
);
877 test_path_extract_directory_one("/.", NULL
, -EADDRNOTAVAIL
);
878 test_path_extract_directory_one(".", NULL
, -EADDRNOTAVAIL
);
879 test_path_extract_directory_one("./", NULL
, -EADDRNOTAVAIL
);
880 test_path_extract_directory_one("./.", NULL
, -EADDRNOTAVAIL
);
881 test_path_extract_directory_one("././", NULL
, -EADDRNOTAVAIL
);
882 test_path_extract_directory_one("././/", NULL
, -EADDRNOTAVAIL
);
883 test_path_extract_directory_one("/foo/a", "/foo", 0);
884 test_path_extract_directory_one("/foo/a/", "/foo", 0);
885 test_path_extract_directory_one("", NULL
, -EINVAL
);
886 test_path_extract_directory_one("a", NULL
, -EDESTADDRREQ
);
887 test_path_extract_directory_one("a/", NULL
, -EDESTADDRREQ
);
888 test_path_extract_directory_one("a/././//.", NULL
, -EDESTADDRREQ
);
889 test_path_extract_directory_one("/a", "/", 0);
890 test_path_extract_directory_one("/a/", "/", 0);
891 test_path_extract_directory_one("/a//./.", "/", 0);
892 test_path_extract_directory_one("/////////////a/////////////", "/", 0);
893 test_path_extract_directory_one("//./a/.///b./././.c//./d//.", "/a/b./.c", 0);
894 test_path_extract_directory_one("xx/.", NULL
, -EDESTADDRREQ
);
895 test_path_extract_directory_one("xx/..", NULL
, -EINVAL
);
896 test_path_extract_directory_one("..", NULL
, -EINVAL
);
897 test_path_extract_directory_one("/..", NULL
, -EINVAL
);
898 test_path_extract_directory_one("../", NULL
, -EINVAL
);
901 TEST(filename_is_valid
) {
902 char foo
[NAME_MAX
+2];
904 assert_se(!filename_is_valid(""));
905 assert_se(!filename_is_valid("/bar/foo"));
906 assert_se(!filename_is_valid("/"));
907 assert_se(!filename_is_valid("."));
908 assert_se(!filename_is_valid(".."));
909 assert_se(!filename_is_valid("bar/foo"));
910 assert_se(!filename_is_valid("bar/foo/"));
911 assert_se(!filename_is_valid("bar//"));
913 memset(foo
, 'a', sizeof(foo
) - 1);
916 assert_se(!filename_is_valid(foo
));
918 assert_se(filename_is_valid("foo_bar-333"));
919 assert_se(filename_is_valid("o.o"));
922 static void test_path_is_valid_and_safe_one(const char *p
, bool ret
) {
923 log_debug("/* %s(\"%s\") */", __func__
, strnull(p
));
925 assert_se(path_is_valid(p
) == ret
);
927 ret
= !streq(p
, "..") &&
928 !startswith(p
, "../") &&
929 !endswith(p
, "/..") &&
931 assert_se(path_is_safe(p
) == ret
);
934 TEST(path_is_valid_and_safe
) {
935 char foo
[PATH_MAX
+2];
938 test_path_is_valid_and_safe_one("", false);
939 test_path_is_valid_and_safe_one("/bar/foo", true);
940 test_path_is_valid_and_safe_one("/bar/foo/", true);
941 test_path_is_valid_and_safe_one("/bar/foo/", true);
942 test_path_is_valid_and_safe_one("//bar//foo//", true);
943 test_path_is_valid_and_safe_one("/", true);
944 test_path_is_valid_and_safe_one("/////", true);
945 test_path_is_valid_and_safe_one("/////.///.////...///..//.", true);
946 test_path_is_valid_and_safe_one(".", true);
947 test_path_is_valid_and_safe_one("..", true);
948 test_path_is_valid_and_safe_one("bar/foo", true);
949 test_path_is_valid_and_safe_one("bar/foo/", true);
950 test_path_is_valid_and_safe_one("bar//", true);
952 memset(foo
, 'a', sizeof(foo
) -1);
955 test_path_is_valid_and_safe_one(foo
, false);
957 c
= strjoina("/xxx/", foo
, "/yyy");
958 test_path_is_valid_and_safe_one(c
, false);
960 test_path_is_valid_and_safe_one("foo_bar-333", true);
961 test_path_is_valid_and_safe_one("o.o", true);
964 TEST(hidden_or_backup_file
) {
965 assert_se(hidden_or_backup_file(".hidden"));
966 assert_se(hidden_or_backup_file("..hidden"));
967 assert_se(!hidden_or_backup_file("hidden."));
969 assert_se(hidden_or_backup_file("backup~"));
970 assert_se(hidden_or_backup_file(".backup~"));
972 assert_se(hidden_or_backup_file("lost+found"));
973 assert_se(hidden_or_backup_file("aquota.user"));
974 assert_se(hidden_or_backup_file("aquota.group"));
976 assert_se(hidden_or_backup_file("test.rpmnew"));
977 assert_se(hidden_or_backup_file("test.dpkg-old"));
978 assert_se(hidden_or_backup_file("test.dpkg-remove"));
979 assert_se(hidden_or_backup_file("test.swp"));
981 assert_se(!hidden_or_backup_file("test.rpmnew."));
982 assert_se(!hidden_or_backup_file("test.dpkg-old.foo"));
985 TEST(skip_dev_prefix
) {
986 assert_se(streq(skip_dev_prefix("/"), "/"));
987 assert_se(streq(skip_dev_prefix("/dev"), ""));
988 assert_se(streq(skip_dev_prefix("/dev/"), ""));
989 assert_se(streq(skip_dev_prefix("/dev/foo"), "foo"));
990 assert_se(streq(skip_dev_prefix("/dev/foo/bar"), "foo/bar"));
991 assert_se(streq(skip_dev_prefix("//dev"), ""));
992 assert_se(streq(skip_dev_prefix("//dev//"), ""));
993 assert_se(streq(skip_dev_prefix("/dev///foo"), "foo"));
994 assert_se(streq(skip_dev_prefix("///dev///foo///bar"), "foo///bar"));
995 assert_se(streq(skip_dev_prefix("//foo"), "//foo"));
996 assert_se(streq(skip_dev_prefix("foo"), "foo"));
999 TEST(empty_or_root
) {
1000 assert_se(empty_or_root(NULL
));
1001 assert_se(empty_or_root(""));
1002 assert_se(empty_or_root("/"));
1003 assert_se(empty_or_root("//"));
1004 assert_se(empty_or_root("///"));
1005 assert_se(empty_or_root("/////////////////"));
1006 assert_se(!empty_or_root("xxx"));
1007 assert_se(!empty_or_root("/xxx"));
1008 assert_se(!empty_or_root("/xxx/"));
1009 assert_se(!empty_or_root("//yy//"));
1012 TEST(path_startswith_set
) {
1013 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/foo/bar", "/zzz"), ""));
1014 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/foo/", "/zzz"), "bar"));
1015 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/foo", "/zzz"), "bar"));
1016 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/", "/zzz"), "foo/bar"));
1017 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "", "/zzz"), NULL
));
1019 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/foo/bar", "/zzz"), NULL
));
1020 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/foo/", "/zzz"), "bar2"));
1021 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/foo", "/zzz"), "bar2"));
1022 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/", "/zzz"), "foo/bar2"));
1023 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "", "/zzz"), NULL
));
1025 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/foo/bar", "/zzz"), NULL
));
1026 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/foo/", "/zzz"), NULL
));
1027 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/foo", "/zzz"), NULL
));
1028 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/", "/zzz"), "foo2/bar"));
1029 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "", "/zzz"), NULL
));
1032 TEST(path_startswith_strv
) {
1033 assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/foo/bar", "/zzz")), ""));
1034 assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/foo/", "/zzz")), "bar"));
1035 assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/foo", "/zzz")), "bar"));
1036 assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/", "/zzz")), "foo/bar"));
1037 assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "", "/zzz")), NULL
));
1039 assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "/foo/bar", "/zzz")), NULL
));
1040 assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "/foo/", "/zzz")), "bar2"));
1041 assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "/foo", "/zzz")), "bar2"));
1042 assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "/", "/zzz")), "foo/bar2"));
1043 assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "", "/zzz")), NULL
));
1045 assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "/foo/bar", "/zzz")), NULL
));
1046 assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "/foo/", "/zzz")), NULL
));
1047 assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "/foo", "/zzz")), NULL
));
1048 assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "/", "/zzz")), "foo2/bar"));
1049 assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "", "/zzz")), NULL
));
1052 static void test_path_glob_can_match_one(const char *pattern
, const char *prefix
, const char *expected
) {
1053 _cleanup_free_
char *result
= NULL
;
1055 log_debug("%s(%s, %s, %s)", __func__
, pattern
, prefix
, strnull(expected
));
1057 assert_se(path_glob_can_match(pattern
, prefix
, &result
) == !!expected
);
1058 assert_se(streq_ptr(result
, expected
));
1061 TEST(path_glob_can_match
) {
1062 test_path_glob_can_match_one("/foo/hoge/aaa", "/foo/hoge/aaa/bbb", NULL
);
1063 test_path_glob_can_match_one("/foo/hoge/aaa", "/foo/hoge/aaa", "/foo/hoge/aaa");
1064 test_path_glob_can_match_one("/foo/hoge/aaa", "/foo/hoge", "/foo/hoge/aaa");
1065 test_path_glob_can_match_one("/foo/hoge/aaa", "/foo", "/foo/hoge/aaa");
1066 test_path_glob_can_match_one("/foo/hoge/aaa", "/", "/foo/hoge/aaa");
1068 test_path_glob_can_match_one("/foo/*/aaa", "/foo/hoge/aaa/bbb", NULL
);
1069 test_path_glob_can_match_one("/foo/*/aaa", "/foo/hoge/aaa", "/foo/hoge/aaa");
1070 test_path_glob_can_match_one("/foo/*/aaa", "/foo/hoge", "/foo/hoge/aaa");
1071 test_path_glob_can_match_one("/foo/*/aaa", "/foo", "/foo/*/aaa");
1072 test_path_glob_can_match_one("/foo/*/aaa", "/", "/foo/*/aaa");
1074 test_path_glob_can_match_one("/foo/*/*/aaa", "/foo/xxx/yyy/aaa/bbb", NULL
);
1075 test_path_glob_can_match_one("/foo/*/*/aaa", "/foo/xxx/yyy/aaa", "/foo/xxx/yyy/aaa");
1076 test_path_glob_can_match_one("/foo/*/*/aaa", "/foo/xxx/yyy", "/foo/xxx/yyy/aaa");
1077 test_path_glob_can_match_one("/foo/*/*/aaa", "/foo/xxx", "/foo/xxx/*/aaa");
1078 test_path_glob_can_match_one("/foo/*/*/aaa", "/foo", "/foo/*/*/aaa");
1079 test_path_glob_can_match_one("/foo/*/*/aaa", "/", "/foo/*/*/aaa");
1081 test_path_glob_can_match_one("/foo/*/aaa/*", "/foo/xxx/aaa/bbb/ccc", NULL
);
1082 test_path_glob_can_match_one("/foo/*/aaa/*", "/foo/xxx/aaa/bbb", "/foo/xxx/aaa/bbb");
1083 test_path_glob_can_match_one("/foo/*/aaa/*", "/foo/xxx/ccc", NULL
);
1084 test_path_glob_can_match_one("/foo/*/aaa/*", "/foo/xxx/aaa", "/foo/xxx/aaa/*");
1085 test_path_glob_can_match_one("/foo/*/aaa/*", "/foo/xxx", "/foo/xxx/aaa/*");
1086 test_path_glob_can_match_one("/foo/*/aaa/*", "/foo", "/foo/*/aaa/*");
1087 test_path_glob_can_match_one("/foo/*/aaa/*", "/", "/foo/*/aaa/*");
1091 log_info("PATH_MAX=%zu\n"
1092 "FILENAME_MAX=%zu\n"
1095 (size_t) FILENAME_MAX
,
1098 assert_cc(FILENAME_MAX
== PATH_MAX
);
1101 DEFINE_TEST_MAIN(LOG_DEBUG
);