]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-path-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
6 #include "alloc-util.h"
10 #include "path-util.h"
11 #include "process-util.h"
13 #include "stat-util.h"
14 #include "string-util.h"
19 static void test_print_paths(void) {
20 log_info("DEFAULT_PATH=%s", DEFAULT_PATH
);
21 log_info("DEFAULT_USER_PATH=%s", DEFAULT_USER_PATH
);
24 static void test_path(void) {
25 log_info("/* %s */", __func__
);
27 assert_se(path_is_absolute("/"));
28 assert_se(!path_is_absolute("./"));
30 assert_se(is_path("/dir"));
31 assert_se(is_path("a/b"));
32 assert_se(!is_path("."));
34 assert_se(streq(basename("./aa/bb/../file.da."), "file.da."));
35 assert_se(streq(basename("/aa///.file"), ".file"));
36 assert_se(streq(basename("/aa///file..."), "file..."));
37 assert_se(streq(basename("file.../"), ""));
39 assert_se(PATH_IN_SET("/bin", "/", "/bin", "/foo"));
40 assert_se(PATH_IN_SET("/bin", "/bin"));
41 assert_se(PATH_IN_SET("/bin", "/foo/bar", "/bin"));
42 assert_se(PATH_IN_SET("/", "/", "/", "/foo/bar"));
43 assert_se(!PATH_IN_SET("/", "/abc", "/def"));
45 assert_se(path_equal_ptr(NULL
, NULL
));
46 assert_se(path_equal_ptr("/a", "/a"));
47 assert_se(!path_equal_ptr("/a", "/b"));
48 assert_se(!path_equal_ptr("/a", NULL
));
49 assert_se(!path_equal_ptr(NULL
, "/a"));
51 assert_se(path_equal_filename("/a/c", "/b/c"));
52 assert_se(path_equal_filename("/a", "/a"));
53 assert_se(!path_equal_filename("/a/b", "/a/c"));
54 assert_se(!path_equal_filename("/b", "/c"));
57 static void test_path_simplify_one(const char *in
, const char *out
) {
62 log_debug("/* test_path_simplify(%s) → %s (expected: %s) */", in
, p
, out
);
63 assert_se(streq(p
, out
));
66 static void test_path_simplify(void) {
67 _cleanup_free_
char *hoge
= NULL
, *hoge_out
= NULL
;
68 char foo
[NAME_MAX
* 2];
70 log_info("/* %s */", __func__
);
72 test_path_simplify_one("", "");
73 test_path_simplify_one("aaa/bbb////ccc", "aaa/bbb/ccc");
74 test_path_simplify_one("//aaa/.////ccc", "/aaa/ccc");
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("./", ".");
81 test_path_simplify_one(".///.//./.", ".");
82 test_path_simplify_one(".///.//././/", ".");
83 test_path_simplify_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.",
84 "/aaa/.bbb/../c./d.dd/..eeee");
85 test_path_simplify_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/..",
86 "/aaa/.bbb/../c./d.dd/..eeee/..");
87 test_path_simplify_one(".//./aaa///.//./.bbb/..///c.//d.dd///..eeee/..",
88 "aaa/.bbb/../c./d.dd/..eeee/..");
89 test_path_simplify_one("..//./aaa///.//./.bbb/..///c.//d.dd///..eeee/..",
90 "../aaa/.bbb/../c./d.dd/..eeee/..");
92 memset(foo
, 'a', sizeof(foo
) -1);
95 test_path_simplify_one(foo
, foo
);
97 hoge
= strjoin("/", foo
);
99 test_path_simplify_one(hoge
, hoge
);
102 hoge
= strjoin("a////.//././//./b///././/./c/////././//./", foo
, "//.//////d/e/.//f/");
105 hoge_out
= strjoin("a/b/c/", foo
, "//.//////d/e/.//f/");
108 test_path_simplify_one(hoge
, hoge_out
);
111 static void test_path_compare_one(const char *a
, const char *b
, int expected
) {
114 assert_se(path_compare(a
, a
) == 0);
115 assert_se(path_compare(b
, b
) == 0);
117 r
= path_compare(a
, b
);
118 assert_se((r
> 0) == (expected
> 0) && (r
< 0) == (expected
< 0));
119 r
= path_compare(b
, a
);
120 assert_se((r
< 0) == (expected
> 0) && (r
> 0) == (expected
< 0));
122 assert_se(path_equal(a
, a
) == 1);
123 assert_se(path_equal(b
, b
) == 1);
124 assert_se(path_equal(a
, b
) == (expected
== 0));
125 assert_se(path_equal(b
, a
) == (expected
== 0));
128 static void test_path_compare(void) {
129 log_info("/* %s */", __func__
);
131 test_path_compare_one("/goo", "/goo", 0);
132 test_path_compare_one("/goo", "/goo", 0);
133 test_path_compare_one("//goo", "/goo", 0);
134 test_path_compare_one("//goo/////", "/goo", 0);
135 test_path_compare_one("goo/////", "goo", 0);
136 test_path_compare_one("/goo/boo", "/goo//boo", 0);
137 test_path_compare_one("//goo/boo", "/goo/boo//", 0);
138 test_path_compare_one("//goo/././//./boo//././//", "/goo/boo//.", 0);
139 test_path_compare_one("/.", "//.///", 0);
140 test_path_compare_one("/x", "x/", 1);
141 test_path_compare_one("x/", "/", -1);
142 test_path_compare_one("/x/./y", "x/y", 1);
143 test_path_compare_one("/x/./y", "/x/y", 0);
144 test_path_compare_one("/x/./././y", "/x/y/././.", 0);
145 test_path_compare_one("./x/./././y", "./x/y/././.", 0);
146 test_path_compare_one(".", "./.", 0);
147 test_path_compare_one(".", "././.", 0);
148 test_path_compare_one("./..", ".", 1);
149 test_path_compare_one("x/.y", "x/y", -1);
150 test_path_compare_one("foo", "/foo", -1);
151 test_path_compare_one("/foo", "/foo/bar", -1);
152 test_path_compare_one("/foo/aaa", "/foo/b", -1);
153 test_path_compare_one("/foo/aaa", "/foo/b/a", -1);
154 test_path_compare_one("/foo/a", "/foo/aaa", -1);
155 test_path_compare_one("/foo/a/b", "/foo/aaa", -1);
158 static void test_path_equal_root(void) {
159 /* Nail down the details of how path_equal("/", ...) works. */
161 log_info("/* %s */", __func__
);
163 assert_se(path_equal("/", "/"));
164 assert_se(path_equal("/", "//"));
166 assert_se(path_equal("/", "/./"));
167 assert_se(!path_equal("/", "/../"));
169 assert_se(!path_equal("/", "/.../"));
171 /* Make sure that files_same works as expected. */
173 assert_se(files_same("/", "/", 0) > 0);
174 assert_se(files_same("/", "/", AT_SYMLINK_NOFOLLOW
) > 0);
175 assert_se(files_same("/", "//", 0) > 0);
176 assert_se(files_same("/", "//", AT_SYMLINK_NOFOLLOW
) > 0);
178 assert_se(files_same("/", "/./", 0) > 0);
179 assert_se(files_same("/", "/./", AT_SYMLINK_NOFOLLOW
) > 0);
180 assert_se(files_same("/", "/../", 0) > 0);
181 assert_se(files_same("/", "/../", AT_SYMLINK_NOFOLLOW
) > 0);
183 assert_se(files_same("/", "/.../", 0) == -ENOENT
);
184 assert_se(files_same("/", "/.../", AT_SYMLINK_NOFOLLOW
) == -ENOENT
);
186 /* The same for path_equal_or_files_same. */
188 assert_se(path_equal_or_files_same("/", "/", 0));
189 assert_se(path_equal_or_files_same("/", "/", AT_SYMLINK_NOFOLLOW
));
190 assert_se(path_equal_or_files_same("/", "//", 0));
191 assert_se(path_equal_or_files_same("/", "//", AT_SYMLINK_NOFOLLOW
));
193 assert_se(path_equal_or_files_same("/", "/./", 0));
194 assert_se(path_equal_or_files_same("/", "/./", AT_SYMLINK_NOFOLLOW
));
195 assert_se(path_equal_or_files_same("/", "/../", 0));
196 assert_se(path_equal_or_files_same("/", "/../", AT_SYMLINK_NOFOLLOW
));
198 assert_se(!path_equal_or_files_same("/", "/.../", 0));
199 assert_se(!path_equal_or_files_same("/", "/.../", AT_SYMLINK_NOFOLLOW
));
202 static void test_find_executable_full(void) {
205 log_info("/* %s */", __func__
);
207 assert_se(find_executable_full("sh", true, &p
, NULL
) == 0);
209 assert_se(streq(basename(p
), "sh"));
212 assert_se(find_executable_full("sh", false, &p
, NULL
) == 0);
214 assert_se(streq(basename(p
), "sh"));
217 _cleanup_free_
char *oldpath
= NULL
;
220 assert_se(oldpath
= strdup(p
));
222 assert_se(unsetenv("PATH") == 0);
224 assert_se(find_executable_full("sh", true, &p
, NULL
) == 0);
226 assert_se(streq(basename(p
), "sh"));
229 assert_se(find_executable_full("sh", false, &p
, NULL
) == 0);
231 assert_se(streq(basename(p
), "sh"));
235 assert_se(setenv("PATH", oldpath
, true) >= 0);
238 static void test_find_executable(const char *self
) {
241 log_info("/* %s */", __func__
);
243 assert_se(find_executable("/bin/sh", &p
) == 0);
245 assert_se(path_equal(p
, "/bin/sh"));
248 assert_se(find_executable(self
, &p
) == 0);
250 assert_se(endswith(p
, "/test-path-util"));
251 assert_se(path_is_absolute(p
));
254 assert_se(find_executable("sh", &p
) == 0);
256 assert_se(endswith(p
, "/sh"));
257 assert_se(path_is_absolute(p
));
260 assert_se(find_executable("/bin/touch", &p
) == 0);
261 assert_se(streq(p
, "/bin/touch"));
264 assert_se(find_executable("touch", &p
) == 0);
265 assert_se(path_is_absolute(p
));
266 assert_se(streq(basename(p
), "touch"));
269 assert_se(find_executable("xxxx-xxxx", &p
) == -ENOENT
);
270 assert_se(find_executable("/some/dir/xxxx-xxxx", &p
) == -ENOENT
);
271 assert_se(find_executable("/proc/filesystems", &p
) == -EACCES
);
274 static void test_find_executable_exec_one(const char *path
) {
275 _cleanup_free_
char *t
= NULL
;
276 _cleanup_close_
int fd
= -1;
280 r
= find_executable_full(path
, false, &t
, &fd
);
282 log_info_errno(r
, "%s: %s → %s: %d/%m", __func__
, path
, t
?: "-", fd
);
284 assert_se(fd
> STDERR_FILENO
);
285 assert_se(path_is_absolute(t
));
286 if (path_is_absolute(path
))
287 assert_se(streq(t
, path
));
292 r
= fexecve_or_execve(fd
, t
, STRV_MAKE(t
, "--version"), STRV_MAKE(NULL
));
293 log_error_errno(r
, "[f]execve: %m");
297 assert_se(wait_for_terminate_and_check(t
, pid
, WAIT_LOG
) == 0);
300 static void test_find_executable_exec(void) {
301 log_info("/* %s */", __func__
);
303 test_find_executable_exec_one("touch");
304 test_find_executable_exec_one("/bin/touch");
306 _cleanup_free_
char *script
= NULL
;
307 assert_se(get_testdata_dir("test-path-util/script.sh", &script
) >= 0);
308 test_find_executable_exec_one(script
);
311 static void test_prefixes(void) {
312 static const char* const values
[] = {
324 log_info("/* %s */", __func__
);
327 PATH_FOREACH_PREFIX_MORE(s
, "/a/b/c/d") {
328 log_error("---%s---", s
);
329 assert_se(streq(s
, values
[i
++]));
331 assert_se(values
[i
] == NULL
);
334 PATH_FOREACH_PREFIX(s
, "/a/b/c/d") {
335 log_error("---%s---", s
);
336 assert_se(streq(s
, values
[i
++]));
338 assert_se(values
[i
] == NULL
);
341 PATH_FOREACH_PREFIX_MORE(s
, "////a////b////c///d///////")
342 assert_se(streq(s
, values
[i
++]));
343 assert_se(values
[i
] == NULL
);
346 PATH_FOREACH_PREFIX(s
, "////a////b////c///d///////")
347 assert_se(streq(s
, values
[i
++]));
348 assert_se(values
[i
] == NULL
);
350 PATH_FOREACH_PREFIX(s
, "////")
351 assert_not_reached("Wut?");
354 PATH_FOREACH_PREFIX_MORE(s
, "////") {
356 assert_se(streq(s
, ""));
361 PATH_FOREACH_PREFIX(s
, "")
362 assert_not_reached("wut?");
365 PATH_FOREACH_PREFIX_MORE(s
, "") {
367 assert_se(streq(s
, ""));
372 static void test_path_join(void) {
373 log_info("/* %s */", __func__
);
375 #define test_join(expected, ...) { \
376 _cleanup_free_ char *z = NULL; \
377 z = path_join(__VA_ARGS__); \
378 log_debug("got \"%s\", expected \"%s\"", z, expected); \
379 assert_se(streq(z, expected)); \
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/a/b/c", "/root", "/a/b", "c");
385 test_join("/root/c", "/root", "/", "c");
386 test_join("/root/", "/root", "/", NULL
);
388 test_join("/a/b/c", "", "/a/b", "/c");
389 test_join("a/b/c", "", "a/b", "c");
390 test_join("/a/b/c", "", "/a/b", "c");
391 test_join("/c", "", "/", "c");
392 test_join("/", "", "/", NULL
);
394 test_join("/a/b/c", NULL
, "/a/b", "/c");
395 test_join("a/b/c", NULL
, "a/b", "c");
396 test_join("/a/b/c", NULL
, "/a/b", "c");
397 test_join("/c", NULL
, "/", "c");
398 test_join("/", NULL
, "/", NULL
);
400 test_join("", "", NULL
);
401 test_join("", NULL
, "");
402 test_join("", NULL
, NULL
);
404 test_join("foo/bar", "foo", "bar");
405 test_join("foo/bar", "", "foo", "bar");
406 test_join("foo/bar", NULL
, "foo", NULL
, "bar");
407 test_join("foo/bar", "", "foo", "", "bar", "");
408 test_join("foo/bar", "", "", "", "", "foo", "", "", "", "bar", "", "", "");
410 test_join("//foo///bar//", "", "/", "", "/foo/", "", "/", "", "/bar/", "", "/", "");
411 test_join("/foo/bar/", "/", "foo", "/", "bar", "/");
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");
415 test_join("//foo////bar////baz//", "//foo/", "///bar/", "///baz//");
418 static void test_path_extend(void) {
419 _cleanup_free_
char *p
= NULL
;
421 log_info("/* %s */", __func__
);
423 assert_se(path_extend(&p
, "foo", "bar", "baz") == p
);
424 assert_se(streq(p
, "foo/bar/baz"));
426 assert_se(path_extend(&p
, "foo", "bar", "baz") == p
);
427 assert_se(streq(p
, "foo/bar/baz/foo/bar/baz"));
430 assert_se(path_extend(&p
, "foo") == p
);
431 assert_se(streq(p
, "foo"));
433 assert_se(path_extend(&p
, "/foo") == p
);
434 assert_se(streq(p
, "foo/foo"));
435 assert_se(path_extend(&p
, "/waaaah/wahhh//") == p
);
436 assert_se(streq(p
, "foo/foo/waaaah/wahhh//")); /* path_extend() does not drop redundant slashes */
437 assert_se(path_extend(&p
, "/aaa/bbb/") == p
);
438 assert_se(streq(p
, "foo/foo/waaaah/wahhh///aaa/bbb/")); /* but not add an extra slash */
440 assert_se(free_and_strdup(&p
, "/") >= 0);
441 assert_se(path_extend(&p
, "foo") == p
);
442 assert_se(streq(p
, "/foo"));
445 static void test_fsck_exists(void) {
446 log_info("/* %s */", __func__
);
448 /* Ensure we use a sane default for PATH. */
449 assert_se(unsetenv("PATH") == 0);
451 /* fsck.minix is provided by util-linux and will probably exist. */
452 assert_se(fsck_exists("minix") == 1);
454 assert_se(fsck_exists("AbCdE") == 0);
455 assert_se(fsck_exists("/../bin/") == 0);
458 static void test_path_make_relative_one(const char *from
, const char *to
, const char *expected
) {
459 _cleanup_free_
char *z
= NULL
;
462 log_info("/* %s(%s, %s) */", __func__
, from
, to
);
464 r
= path_make_relative(from
, to
, &z
);
465 assert_se((r
>= 0) == !!expected
);
466 assert_se(streq_ptr(z
, expected
));
469 static void test_make_relative(void) {
470 log_info("/* %s */", __func__
);
472 test_path_make_relative_one("some/relative/path", "/some/path", NULL
);
473 test_path_make_relative_one("/some/path", "some/relative/path", NULL
);
474 test_path_make_relative_one("/some/dotdot/../path", "/some/path", NULL
);
476 test_path_make_relative_one("/", "/", ".");
477 test_path_make_relative_one("/", "/some/path", "some/path");
478 test_path_make_relative_one("/some/path", "/some/path", ".");
479 test_path_make_relative_one("/some/path", "/some/path/in/subdir", "in/subdir");
480 test_path_make_relative_one("/some/path", "/", "../..");
481 test_path_make_relative_one("/some/path", "/some/other/path", "../other/path");
482 test_path_make_relative_one("/some/path/./dot", "/some/further/path", "../../further/path");
483 test_path_make_relative_one("//extra.//.//./.slashes//./won't////fo.ol///anybody//", "/././/extra././/.slashes////ar.e/.just/././.fine///", "../../../ar.e/.just/.fine");
486 static void test_strv_resolve(void) {
487 char tmp_dir
[] = "/tmp/test-path-util-XXXXXX";
488 _cleanup_strv_free_
char **search_dirs
= NULL
;
489 _cleanup_strv_free_
char **absolute_dirs
= NULL
;
492 assert_se(mkdtemp(tmp_dir
) != NULL
);
494 search_dirs
= strv_new("/dir1", "/dir2", "/dir3");
495 assert_se(search_dirs
);
496 STRV_FOREACH(d
, search_dirs
) {
497 char *p
= path_join(tmp_dir
, *d
);
499 assert_se(strv_push(&absolute_dirs
, p
) == 0);
502 assert_se(mkdir(absolute_dirs
[0], 0700) == 0);
503 assert_se(mkdir(absolute_dirs
[1], 0700) == 0);
504 assert_se(symlink("dir2", absolute_dirs
[2]) == 0);
506 path_strv_resolve(search_dirs
, tmp_dir
);
507 assert_se(streq(search_dirs
[0], "/dir1"));
508 assert_se(streq(search_dirs
[1], "/dir2"));
509 assert_se(streq(search_dirs
[2], "/dir2"));
511 assert_se(rm_rf(tmp_dir
, REMOVE_ROOT
|REMOVE_PHYSICAL
) == 0);
514 static void test_path_startswith_one(const char *path
, const char *prefix
, const char *skipped
, const char *expected
) {
517 log_debug("/* %s(%s, %s) */", __func__
, path
, prefix
);
519 p
= path_startswith(path
, prefix
);
520 assert_se(streq_ptr(p
, expected
));
522 q
= strjoina(skipped
, p
);
523 assert_se(streq(q
, path
));
524 assert_se(p
== path
+ strlen(skipped
));
528 static void test_path_startswith(void) {
529 log_info("/* %s */", __func__
);
531 test_path_startswith_one("/foo/bar/barfoo/", "/foo", "/foo/", "bar/barfoo/");
532 test_path_startswith_one("/foo/bar/barfoo/", "/foo/", "/foo/", "bar/barfoo/");
533 test_path_startswith_one("/foo/bar/barfoo/", "/", "/", "foo/bar/barfoo/");
534 test_path_startswith_one("/foo/bar/barfoo/", "////", "/", "foo/bar/barfoo/");
535 test_path_startswith_one("/foo/bar/barfoo/", "/foo//bar/////barfoo///", "/foo/bar/barfoo/", "");
536 test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/barfoo////", "/foo/bar/barfoo/", "");
537 test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar///barfoo/", "/foo/bar/barfoo/", "");
538 test_path_startswith_one("/foo/bar/barfoo/", "/foo////bar/barfoo/", "/foo/bar/barfoo/", "");
539 test_path_startswith_one("/foo/bar/barfoo/", "////foo/bar/barfoo/", "/foo/bar/barfoo/", "");
540 test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/barfoo", "/foo/bar/barfoo/", "");
542 test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/barfooa/", NULL
, NULL
);
543 test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/barfooa", NULL
, NULL
);
544 test_path_startswith_one("/foo/bar/barfoo/", "", NULL
, NULL
);
545 test_path_startswith_one("/foo/bar/barfoo/", "/bar/foo", NULL
, NULL
);
546 test_path_startswith_one("/foo/bar/barfoo/", "/f/b/b/", NULL
, NULL
);
547 test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/barfo", NULL
, NULL
);
548 test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/bar", NULL
, NULL
);
549 test_path_startswith_one("/foo/bar/barfoo/", "/fo", NULL
, NULL
);
552 static void test_prefix_root_one(const char *r
, const char *p
, const char *expected
) {
553 _cleanup_free_
char *s
= NULL
;
556 assert_se(s
= path_join(r
, p
));
557 assert_se(path_equal_ptr(s
, expected
));
559 t
= prefix_roota(r
, p
);
561 assert_se(path_equal_ptr(t
, expected
));
564 static void test_prefix_root(void) {
565 log_info("/* %s */", __func__
);
567 test_prefix_root_one("/", "/foo", "/foo");
568 test_prefix_root_one(NULL
, "/foo", "/foo");
569 test_prefix_root_one("", "/foo", "/foo");
570 test_prefix_root_one("///", "/foo", "/foo");
571 test_prefix_root_one("/", "////foo", "/foo");
572 test_prefix_root_one(NULL
, "////foo", "/foo");
573 test_prefix_root_one("/", "foo", "/foo");
574 test_prefix_root_one("", "foo", "foo");
575 test_prefix_root_one(NULL
, "foo", "foo");
577 test_prefix_root_one("/foo", "/bar", "/foo/bar");
578 test_prefix_root_one("/foo", "bar", "/foo/bar");
579 test_prefix_root_one("foo", "bar", "foo/bar");
580 test_prefix_root_one("/foo/", "/bar", "/foo/bar");
581 test_prefix_root_one("/foo/", "//bar", "/foo/bar");
582 test_prefix_root_one("/foo///", "//bar", "/foo/bar");
585 static void test_file_in_same_dir(void) {
588 log_info("/* %s */", __func__
);
590 t
= file_in_same_dir("/", "a");
591 assert_se(streq(t
, "/a"));
594 t
= file_in_same_dir("/", "/a");
595 assert_se(streq(t
, "/a"));
598 t
= file_in_same_dir("", "a");
599 assert_se(streq(t
, "a"));
602 t
= file_in_same_dir("a/", "a");
603 assert_se(streq(t
, "a/a"));
606 t
= file_in_same_dir("bar/foo", "bar");
607 assert_se(streq(t
, "bar/bar"));
611 static void test_path_find_first_component_one(
617 log_debug("/* %s(\"%s\", accept_dot_dot=%s) */", __func__
, strnull(path
), yes_no(accept_dot_dot
));
619 for (const char *p
= path
;;) {
623 r
= path_find_first_component(&p
, accept_dot_dot
, &e
);
627 assert_se(p
== path
+ strlen_ptr(path
));
633 assert_se(strv_isempty(expected
));
638 assert_se(strcspn(e
, "/") == (size_t) r
);
639 assert_se(strlen_ptr(*expected
) == (size_t) r
);
640 assert_se(strneq(e
, *expected
++, r
));
644 static void test_path_find_first_component(void) {
645 _cleanup_free_
char *hoge
= NULL
;
646 char foo
[NAME_MAX
* 2];
648 log_info("/* %s */", __func__
);
650 test_path_find_first_component_one(NULL
, false, NULL
, 0);
651 test_path_find_first_component_one("", false, NULL
, 0);
652 test_path_find_first_component_one("/", false, NULL
, 0);
653 test_path_find_first_component_one(".", false, NULL
, 0);
654 test_path_find_first_component_one("./", false, NULL
, 0);
655 test_path_find_first_component_one("./.", false, NULL
, 0);
656 test_path_find_first_component_one("..", false, NULL
, -EINVAL
);
657 test_path_find_first_component_one("/..", false, NULL
, -EINVAL
);
658 test_path_find_first_component_one("./..", false, NULL
, -EINVAL
);
659 test_path_find_first_component_one("////./././//.", false, NULL
, 0);
660 test_path_find_first_component_one("a/b/c", false, STRV_MAKE("a", "b", "c"), 0);
661 test_path_find_first_component_one("././//.///aa/bbb//./ccc", false, STRV_MAKE("aa", "bbb", "ccc"), 0);
662 test_path_find_first_component_one("././//.///aa/.../../bbb//./ccc/.", false, STRV_MAKE("aa", "..."), -EINVAL
);
663 test_path_find_first_component_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.", false, STRV_MAKE("aaa", ".bbb"), -EINVAL
);
664 test_path_find_first_component_one("a/foo./b", false, STRV_MAKE("a", "foo.", "b"), 0);
666 test_path_find_first_component_one(NULL
, true, NULL
, 0);
667 test_path_find_first_component_one("", true, NULL
, 0);
668 test_path_find_first_component_one("/", true, NULL
, 0);
669 test_path_find_first_component_one(".", true, NULL
, 0);
670 test_path_find_first_component_one("./", true, NULL
, 0);
671 test_path_find_first_component_one("./.", true, NULL
, 0);
672 test_path_find_first_component_one("..", true, STRV_MAKE(".."), 0);
673 test_path_find_first_component_one("/..", true, STRV_MAKE(".."), 0);
674 test_path_find_first_component_one("./..", true, STRV_MAKE(".."), 0);
675 test_path_find_first_component_one("////./././//.", true, NULL
, 0);
676 test_path_find_first_component_one("a/b/c", true, STRV_MAKE("a", "b", "c"), 0);
677 test_path_find_first_component_one("././//.///aa/bbb//./ccc", true, STRV_MAKE("aa", "bbb", "ccc"), 0);
678 test_path_find_first_component_one("././//.///aa/.../../bbb//./ccc/.", true, STRV_MAKE("aa", "...", "..", "bbb", "ccc"), 0);
679 test_path_find_first_component_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.", true, STRV_MAKE("aaa", ".bbb", "..", "c.", "d.dd", "..eeee"), 0);
680 test_path_find_first_component_one("a/foo./b", true, STRV_MAKE("a", "foo.", "b"), 0);
682 memset(foo
, 'a', sizeof(foo
) -1);
685 test_path_find_first_component_one(foo
, false, NULL
, -EINVAL
);
686 test_path_find_first_component_one(foo
, true, NULL
, -EINVAL
);
688 hoge
= strjoin("a/b/c/", foo
, "//d/e/.//f/");
691 test_path_find_first_component_one(hoge
, false, STRV_MAKE("a", "b", "c"), -EINVAL
);
692 test_path_find_first_component_one(hoge
, true, STRV_MAKE("a", "b", "c"), -EINVAL
);
695 static void test_path_find_last_component_one(
701 log_debug("/* %s(\"%s\", accept_dot_dot=%s) */", __func__
, strnull(path
), yes_no(accept_dot_dot
));
703 for (const char *next
= NULL
;;) {
707 r
= path_find_last_component(path
, accept_dot_dot
, &next
, &e
);
710 assert_se(next
== path
);
714 assert_se(strv_isempty(expected
));
719 assert_se(strcspn(e
, "/") == (size_t) r
);
720 assert_se(strlen_ptr(*expected
) == (size_t) r
);
721 assert_se(strneq(e
, *expected
++, r
));
725 static void test_path_find_last_component(void) {
726 _cleanup_free_
char *hoge
= NULL
;
727 char foo
[NAME_MAX
* 2];
729 log_info("/* %s */", __func__
);
731 test_path_find_last_component_one(NULL
, false, NULL
, 0);
732 test_path_find_last_component_one("", false, NULL
, 0);
733 test_path_find_last_component_one("/", false, NULL
, 0);
734 test_path_find_last_component_one(".", false, NULL
, 0);
735 test_path_find_last_component_one("./", false, NULL
, 0);
736 test_path_find_last_component_one("./.", false, NULL
, 0);
737 test_path_find_last_component_one("..", false, NULL
, -EINVAL
);
738 test_path_find_last_component_one("/..", false, NULL
, -EINVAL
);
739 test_path_find_last_component_one("./..", false, NULL
, -EINVAL
);
740 test_path_find_last_component_one("////./././//.", false, NULL
, 0);
741 test_path_find_last_component_one("a/b/c", false, STRV_MAKE("c", "b", "a"), 0);
742 test_path_find_last_component_one("././//.///aa./.bbb//./ccc/././/", false, STRV_MAKE("ccc", ".bbb", "aa."), 0);
743 test_path_find_last_component_one("././//.///aa/../.../bbb//./ccc/.", false, STRV_MAKE("ccc", "bbb", "..."), -EINVAL
);
744 test_path_find_last_component_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.", false, STRV_MAKE("..eeee", "d.dd", "c."), -EINVAL
);
746 test_path_find_last_component_one(NULL
, true, NULL
, 0);
747 test_path_find_last_component_one("", true, NULL
, 0);
748 test_path_find_last_component_one("/", true, NULL
, 0);
749 test_path_find_last_component_one(".", true, NULL
, 0);
750 test_path_find_last_component_one("./", true, NULL
, 0);
751 test_path_find_last_component_one("./.", true, NULL
, 0);
752 test_path_find_last_component_one("..", true, STRV_MAKE(".."), 0);
753 test_path_find_last_component_one("/..", true, STRV_MAKE(".."), 0);
754 test_path_find_last_component_one("./..", true, STRV_MAKE(".."), 0);
755 test_path_find_last_component_one("////./././//.", true, NULL
, 0);
756 test_path_find_last_component_one("a/b/c", true, STRV_MAKE("c", "b", "a"), 0);
757 test_path_find_last_component_one("././//.///aa./.bbb//./ccc/././/", true, STRV_MAKE("ccc", ".bbb", "aa."), 0);
758 test_path_find_last_component_one("././//.///aa/../.../bbb//./ccc/.", true, STRV_MAKE("ccc", "bbb", "...", "..", "aa"), 0);
759 test_path_find_last_component_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.", true, STRV_MAKE("..eeee", "d.dd", "c.", "..", ".bbb", "aaa"), 0);
761 memset(foo
, 'a', sizeof(foo
) -1);
764 test_path_find_last_component_one(foo
, false, NULL
, -EINVAL
);
765 test_path_find_last_component_one(foo
, true, NULL
, -EINVAL
);
767 hoge
= strjoin(foo
, "/a/b/c/");
770 test_path_find_last_component_one(hoge
, false, STRV_MAKE("c", "b", "a"), -EINVAL
);
771 test_path_find_last_component_one(hoge
, true, STRV_MAKE("c", "b", "a"), -EINVAL
);
774 static void test_last_path_component(void) {
775 assert_se(last_path_component(NULL
) == NULL
);
776 assert_se(streq(last_path_component("a/b/c"), "c"));
777 assert_se(streq(last_path_component("a/b/c/"), "c/"));
778 assert_se(streq(last_path_component("/"), "/"));
779 assert_se(streq(last_path_component("//"), "/"));
780 assert_se(streq(last_path_component("///"), "/"));
781 assert_se(streq(last_path_component("."), "."));
782 assert_se(streq(last_path_component("./."), "."));
783 assert_se(streq(last_path_component("././"), "./"));
784 assert_se(streq(last_path_component("././/"), ".//"));
785 assert_se(streq(last_path_component("/foo/a"), "a"));
786 assert_se(streq(last_path_component("/foo/a/"), "a/"));
787 assert_se(streq(last_path_component(""), ""));
788 assert_se(streq(last_path_component("a"), "a"));
789 assert_se(streq(last_path_component("a/"), "a/"));
790 assert_se(streq(last_path_component("/a"), "a"));
791 assert_se(streq(last_path_component("/a/"), "a/"));
794 static void test_path_extract_filename_one(const char *input
, const char *output
, int ret
) {
795 _cleanup_free_
char *k
= NULL
;
798 r
= path_extract_filename(input
, &k
);
799 log_info_errno(r
, "%s → %s/%m [expected: %s/%s]",
801 strnull(k
), /* strerror(r) is printed via %m, to avoid that the two strerror()'s overwrite each other's buffers */
802 strnull(output
), ret
< 0 ? strerror_safe(ret
) : "-");
803 assert_se(streq_ptr(k
, output
));
807 static void test_path_extract_filename(void) {
808 log_info("/* %s */", __func__
);
810 test_path_extract_filename_one(NULL
, NULL
, -EINVAL
);
811 test_path_extract_filename_one("a/b/c", "c", 0);
812 test_path_extract_filename_one("a/b/c/", "c", O_DIRECTORY
);
813 test_path_extract_filename_one("/", NULL
, -EADDRNOTAVAIL
);
814 test_path_extract_filename_one("//", NULL
, -EADDRNOTAVAIL
);
815 test_path_extract_filename_one("///", NULL
, -EADDRNOTAVAIL
);
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("/foo/a", "a", 0);
823 test_path_extract_filename_one("/foo/a/", "a", O_DIRECTORY
);
824 test_path_extract_filename_one("", NULL
, -EINVAL
);
825 test_path_extract_filename_one("a", "a", 0);
826 test_path_extract_filename_one("a/", "a", O_DIRECTORY
);
827 test_path_extract_filename_one("a/././//.", "a", O_DIRECTORY
);
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", O_DIRECTORY
);
832 test_path_extract_filename_one("//./a/.///b./././.c//./d//.", "d", O_DIRECTORY
);
833 test_path_extract_filename_one("xx/.", "xx", O_DIRECTORY
);
834 test_path_extract_filename_one("xx/..", NULL
, -EINVAL
);
835 test_path_extract_filename_one("..", NULL
, -EINVAL
);
836 test_path_extract_filename_one("/..", NULL
, -EINVAL
);
837 test_path_extract_filename_one("../", NULL
, -EINVAL
);
840 static void test_path_extract_directory_one(const char *input
, const char *output
, int ret
) {
841 _cleanup_free_
char *k
= NULL
;
844 r
= path_extract_directory(input
, &k
);
845 log_info_errno(r
, "%s → %s/%m [expected: %s/%s]",
847 strnull(k
), /* we output strerror_safe(r) via %m here, since otherwise the error buffer might be overwritten twice */
848 strnull(output
), strerror_safe(ret
));
849 assert_se(streq_ptr(k
, output
));
852 /* Extra safety check: let's make sure that if we split out the filename too (and it works) the
853 * joined parts are identical to the original again */
855 _cleanup_free_
char *f
= NULL
;
857 r
= path_extract_filename(input
, &f
);
859 _cleanup_free_
char *j
= NULL
;
861 assert_se(j
= path_join(k
, f
));
862 assert_se(path_equal(input
, j
));
867 static void test_path_extract_directory(void) {
868 log_info("/* %s */", __func__
);
870 test_path_extract_directory_one(NULL
, NULL
, -EINVAL
);
871 test_path_extract_directory_one("a/b/c", "a/b", 0);
872 test_path_extract_directory_one("a/b/c/", "a/b", 0);
873 test_path_extract_directory_one("/", NULL
, -EADDRNOTAVAIL
);
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("/foo/a", "/foo", 0);
883 test_path_extract_directory_one("/foo/a/", "/foo", 0);
884 test_path_extract_directory_one("", NULL
, -EINVAL
);
885 test_path_extract_directory_one("a", NULL
, -EDESTADDRREQ
);
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", "/", 0);
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/.///b./././.c//./d//.", "/a/b./.c", 0);
893 test_path_extract_directory_one("xx/.", NULL
, -EDESTADDRREQ
);
894 test_path_extract_directory_one("xx/..", NULL
, -EINVAL
);
895 test_path_extract_directory_one("..", NULL
, -EINVAL
);
896 test_path_extract_directory_one("/..", NULL
, -EINVAL
);
897 test_path_extract_directory_one("../", NULL
, -EINVAL
);
900 static void test_filename_is_valid(void) {
901 char foo
[NAME_MAX
+2];
903 log_info("/* %s */", __func__
);
905 assert_se(!filename_is_valid(""));
906 assert_se(!filename_is_valid("/bar/foo"));
907 assert_se(!filename_is_valid("/"));
908 assert_se(!filename_is_valid("."));
909 assert_se(!filename_is_valid(".."));
910 assert_se(!filename_is_valid("bar/foo"));
911 assert_se(!filename_is_valid("bar/foo/"));
912 assert_se(!filename_is_valid("bar//"));
914 memset(foo
, 'a', sizeof(foo
) - 1);
917 assert_se(!filename_is_valid(foo
));
919 assert_se(filename_is_valid("foo_bar-333"));
920 assert_se(filename_is_valid("o.o"));
923 static void test_path_is_valid_and_safe_one(const char *p
, bool ret
) {
924 log_debug("/* %s(\"%s\") */", __func__
, strnull(p
));
926 assert_se(path_is_valid(p
) == ret
);
928 ret
= !streq(p
, "..") &&
929 !startswith(p
, "../") &&
930 !endswith(p
, "/..") &&
932 assert_se(path_is_safe(p
) == ret
);
935 static void test_path_is_valid_and_safe(void) {
936 char foo
[PATH_MAX
+2];
939 log_info("/* %s */", __func__
);
941 test_path_is_valid_and_safe_one("", false);
942 test_path_is_valid_and_safe_one("/bar/foo", true);
943 test_path_is_valid_and_safe_one("/bar/foo/", true);
944 test_path_is_valid_and_safe_one("/bar/foo/", true);
945 test_path_is_valid_and_safe_one("//bar//foo//", 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("/////.///.////...///..//.", true);
949 test_path_is_valid_and_safe_one(".", true);
950 test_path_is_valid_and_safe_one("..", true);
951 test_path_is_valid_and_safe_one("bar/foo", true);
952 test_path_is_valid_and_safe_one("bar/foo/", true);
953 test_path_is_valid_and_safe_one("bar//", true);
955 memset(foo
, 'a', sizeof(foo
) -1);
958 test_path_is_valid_and_safe_one(foo
, false);
960 c
= strjoina("/xxx/", foo
, "/yyy");
961 test_path_is_valid_and_safe_one(c
, false);
963 test_path_is_valid_and_safe_one("foo_bar-333", true);
964 test_path_is_valid_and_safe_one("o.o", true);
967 static void test_hidden_or_backup_file(void) {
968 log_info("/* %s */", __func__
);
970 assert_se(hidden_or_backup_file(".hidden"));
971 assert_se(hidden_or_backup_file("..hidden"));
972 assert_se(!hidden_or_backup_file("hidden."));
974 assert_se(hidden_or_backup_file("backup~"));
975 assert_se(hidden_or_backup_file(".backup~"));
977 assert_se(hidden_or_backup_file("lost+found"));
978 assert_se(hidden_or_backup_file("aquota.user"));
979 assert_se(hidden_or_backup_file("aquota.group"));
981 assert_se(hidden_or_backup_file("test.rpmnew"));
982 assert_se(hidden_or_backup_file("test.dpkg-old"));
983 assert_se(hidden_or_backup_file("test.dpkg-remove"));
984 assert_se(hidden_or_backup_file("test.swp"));
986 assert_se(!hidden_or_backup_file("test.rpmnew."));
987 assert_se(!hidden_or_backup_file("test.dpkg-old.foo"));
990 static void test_systemd_installation_has_version(const char *path
) {
992 const unsigned versions
[] = {0, 231, PROJECT_VERSION
, 999};
995 log_info("/* %s */", __func__
);
997 for (i
= 0; i
< ELEMENTSOF(versions
); i
++) {
998 r
= systemd_installation_has_version(path
, versions
[i
]);
1000 log_info("%s has systemd >= %u: %s",
1001 path
?: "Current installation", versions
[i
], yes_no(r
));
1005 static void test_skip_dev_prefix(void) {
1006 log_info("/* %s */", __func__
);
1008 assert_se(streq(skip_dev_prefix("/"), "/"));
1009 assert_se(streq(skip_dev_prefix("/dev"), ""));
1010 assert_se(streq(skip_dev_prefix("/dev/"), ""));
1011 assert_se(streq(skip_dev_prefix("/dev/foo"), "foo"));
1012 assert_se(streq(skip_dev_prefix("/dev/foo/bar"), "foo/bar"));
1013 assert_se(streq(skip_dev_prefix("//dev"), ""));
1014 assert_se(streq(skip_dev_prefix("//dev//"), ""));
1015 assert_se(streq(skip_dev_prefix("/dev///foo"), "foo"));
1016 assert_se(streq(skip_dev_prefix("///dev///foo///bar"), "foo///bar"));
1017 assert_se(streq(skip_dev_prefix("//foo"), "//foo"));
1018 assert_se(streq(skip_dev_prefix("foo"), "foo"));
1021 static void test_empty_or_root(void) {
1022 log_info("/* %s */", __func__
);
1024 assert_se(empty_or_root(NULL
));
1025 assert_se(empty_or_root(""));
1026 assert_se(empty_or_root("/"));
1027 assert_se(empty_or_root("//"));
1028 assert_se(empty_or_root("///"));
1029 assert_se(empty_or_root("/////////////////"));
1030 assert_se(!empty_or_root("xxx"));
1031 assert_se(!empty_or_root("/xxx"));
1032 assert_se(!empty_or_root("/xxx/"));
1033 assert_se(!empty_or_root("//yy//"));
1036 static void test_path_startswith_set(void) {
1037 log_info("/* %s */", __func__
);
1039 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/foo/bar", "/zzz"), ""));
1040 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/foo/", "/zzz"), "bar"));
1041 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/foo", "/zzz"), "bar"));
1042 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/", "/zzz"), "foo/bar"));
1043 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "", "/zzz"), NULL
));
1045 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/foo/bar", "/zzz"), NULL
));
1046 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/foo/", "/zzz"), "bar2"));
1047 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/foo", "/zzz"), "bar2"));
1048 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/", "/zzz"), "foo/bar2"));
1049 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "", "/zzz"), NULL
));
1051 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/foo/bar", "/zzz"), NULL
));
1052 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/foo/", "/zzz"), NULL
));
1053 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/foo", "/zzz"), NULL
));
1054 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/", "/zzz"), "foo2/bar"));
1055 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "", "/zzz"), NULL
));
1058 static void test_path_startswith_strv(void) {
1059 log_info("/* %s */", __func__
);
1061 assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/foo/bar", "/zzz")), ""));
1062 assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/foo/", "/zzz")), "bar"));
1063 assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/foo", "/zzz")), "bar"));
1064 assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/", "/zzz")), "foo/bar"));
1065 assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "", "/zzz")), NULL
));
1067 assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "/foo/bar", "/zzz")), NULL
));
1068 assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "/foo/", "/zzz")), "bar2"));
1069 assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "/foo", "/zzz")), "bar2"));
1070 assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "/", "/zzz")), "foo/bar2"));
1071 assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "", "/zzz")), NULL
));
1073 assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "/foo/bar", "/zzz")), NULL
));
1074 assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "/foo/", "/zzz")), NULL
));
1075 assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "/foo", "/zzz")), NULL
));
1076 assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "/", "/zzz")), "foo2/bar"));
1077 assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "", "/zzz")), NULL
));
1080 int main(int argc
, char **argv
) {
1081 test_setup_logging(LOG_DEBUG
);
1083 log_info("PATH_MAX=%zu\n"
1084 "FILENAME_MAX=%zu\n"
1087 (size_t) FILENAME_MAX
,
1090 assert_cc(FILENAME_MAX
== PATH_MAX
);
1094 test_path_simplify();
1095 test_path_compare();
1096 test_path_equal_root();
1097 test_find_executable_full();
1098 test_find_executable(argv
[0]);
1099 test_find_executable_exec();
1104 test_make_relative();
1105 test_strv_resolve();
1106 test_path_startswith();
1108 test_file_in_same_dir();
1109 test_path_find_first_component();
1110 test_path_find_last_component();
1111 test_last_path_component();
1112 test_path_extract_filename();
1113 test_path_extract_directory();
1114 test_filename_is_valid();
1115 test_path_is_valid_and_safe();
1116 test_hidden_or_backup_file();
1117 test_skip_dev_prefix();
1118 test_empty_or_root();
1119 test_path_startswith_set();
1120 test_path_startswith_strv();
1122 test_systemd_installation_has_version(argv
[1]); /* NULL is OK */