]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-path-util.c
switch-root: check if old and new root fs is same via files_same_at()
[thirdparty/systemd.git] / src / test / test-path-util.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
76877b46 2
a696dbef 3#include <stdio.h>
07630cea 4#include <unistd.h>
a696dbef 5
b5efdb8a 6#include "alloc-util.h"
a6d9111c 7#include "exec-util.h"
3ffd4af2 8#include "fd-util.h"
8c35c10d 9#include "fs-util.h"
76877b46 10#include "macro.h"
07630cea 11#include "path-util.h"
5ca9139a 12#include "process-util.h"
c6878637 13#include "rm-rf.h"
84e72b5e 14#include "stat-util.h"
07630cea
LP
15#include "string-util.h"
16#include "strv.h"
6d7c4033 17#include "tests.h"
8c35c10d 18#include "tmpfile-util.h"
76877b46 19
4f7452a8 20TEST(print_paths) {
3602ca6f
ZJS
21 log_info("DEFAULT_PATH=%s", DEFAULT_PATH);
22 log_info("DEFAULT_USER_PATH=%s", DEFAULT_USER_PATH);
23}
24
4f7452a8 25TEST(path) {
76877b46
ZJS
26 assert_se(path_is_absolute("/"));
27 assert_se(!path_is_absolute("./"));
28
29 assert_se(is_path("/dir"));
30 assert_se(is_path("a/b"));
31 assert_se(!is_path("."));
32
2b6bf07d
ZJS
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.../"), ""));
76877b46 37
3ae5990c
ZJS
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"));
24737c29
ZJS
43
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"));
76877b46
ZJS
49}
50
cb71ed91
YW
51static void test_path_simplify_one(const char *in, const char *out) {
52 char *p;
53
2f82562b 54 p = strdupa_safe(in);
cb71ed91
YW
55 path_simplify(p);
56 log_debug("/* test_path_simplify(%s) → %s (expected: %s) */", in, p, out);
57 assert_se(streq(p, out));
58}
59
4f7452a8 60TEST(path_simplify) {
cb71ed91
YW
61 _cleanup_free_ char *hoge = NULL, *hoge_out = NULL;
62 char foo[NAME_MAX * 2];
63
cb71ed91
YW
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/..");
83
84 memset(foo, 'a', sizeof(foo) -1);
85 char_array_0(foo);
86
87 test_path_simplify_one(foo, foo);
88
89 hoge = strjoin("/", foo);
90 assert_se(hoge);
91 test_path_simplify_one(hoge, hoge);
92 hoge = mfree(hoge);
93
94 hoge = strjoin("a////.//././//./b///././/./c/////././//./", foo, "//.//////d/e/.//f/");
95 assert_se(hoge);
96
97 hoge_out = strjoin("a/b/c/", foo, "//.//////d/e/.//f/");
98 assert_se(hoge_out);
99
100 test_path_simplify_one(hoge, hoge_out);
101}
102
353df443
YW
103static void test_path_compare_one(const char *a, const char *b, int expected) {
104 int r;
105
106 assert_se(path_compare(a, a) == 0);
107 assert_se(path_compare(b, b) == 0);
108
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));
113
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));
118}
119
4f7452a8 120TEST(path_compare) {
353df443
YW
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);
6d216bdd
ZJS
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);
353df443
YW
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);
146}
147
6808e004
YW
148static void test_path_compare_filename_one(const char *a, const char *b, int expected) {
149 int r;
150
151 assert_se(path_compare_filename(a, a) == 0);
152 assert_se(path_compare_filename(b, b) == 0);
153
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));
158
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));
163}
164
165TEST(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);
195}
196
4f7452a8 197TEST(path_equal_root) {
84e72b5e
ZJS
198 /* Nail down the details of how path_equal("/", ...) works. */
199
200 assert_se(path_equal("/", "/"));
201 assert_se(path_equal("/", "//"));
202
353df443 203 assert_se(path_equal("/", "/./"));
84e72b5e
ZJS
204 assert_se(!path_equal("/", "/../"));
205
206 assert_se(!path_equal("/", "/.../"));
207
208 /* Make sure that files_same works as expected. */
209
e3f791a2
ZJS
210 assert_se(files_same("/", "/", 0) > 0);
211 assert_se(files_same("/", "/", AT_SYMLINK_NOFOLLOW) > 0);
212 assert_se(files_same("/", "//", 0) > 0);
213 assert_se(files_same("/", "//", AT_SYMLINK_NOFOLLOW) > 0);
84e72b5e 214
e3f791a2
ZJS
215 assert_se(files_same("/", "/./", 0) > 0);
216 assert_se(files_same("/", "/./", AT_SYMLINK_NOFOLLOW) > 0);
217 assert_se(files_same("/", "/../", 0) > 0);
218 assert_se(files_same("/", "/../", AT_SYMLINK_NOFOLLOW) > 0);
84e72b5e 219
e3f791a2
ZJS
220 assert_se(files_same("/", "/.../", 0) == -ENOENT);
221 assert_se(files_same("/", "/.../", AT_SYMLINK_NOFOLLOW) == -ENOENT);
84e72b5e
ZJS
222
223 /* The same for path_equal_or_files_same. */
224
e3f791a2
ZJS
225 assert_se(path_equal_or_files_same("/", "/", 0));
226 assert_se(path_equal_or_files_same("/", "/", AT_SYMLINK_NOFOLLOW));
227 assert_se(path_equal_or_files_same("/", "//", 0));
228 assert_se(path_equal_or_files_same("/", "//", AT_SYMLINK_NOFOLLOW));
84e72b5e 229
e3f791a2
ZJS
230 assert_se(path_equal_or_files_same("/", "/./", 0));
231 assert_se(path_equal_or_files_same("/", "/./", AT_SYMLINK_NOFOLLOW));
232 assert_se(path_equal_or_files_same("/", "/../", 0));
233 assert_se(path_equal_or_files_same("/", "/../", AT_SYMLINK_NOFOLLOW));
84e72b5e 234
e3f791a2
ZJS
235 assert_se(!path_equal_or_files_same("/", "/.../", 0));
236 assert_se(!path_equal_or_files_same("/", "/.../", AT_SYMLINK_NOFOLLOW));
84e72b5e
ZJS
237}
238
4f7452a8 239TEST(find_executable_full) {
92673045 240 char *p;
8c35c10d 241 char* test_file_name;
254d1313 242 _cleanup_close_ int fd = -EBADF;
8c35c10d 243 char fn[] = "/tmp/test-XXXXXX";
92673045 244
8c35c10d 245 assert_se(find_executable_full("sh", NULL, NULL, true, &p, NULL) == 0);
92673045
ZJS
246 puts(p);
247 assert_se(streq(basename(p), "sh"));
248 free(p);
249
8c35c10d 250 assert_se(find_executable_full("sh", NULL, NULL, false, &p, NULL) == 0);
92673045
ZJS
251 puts(p);
252 assert_se(streq(basename(p), "sh"));
253 free(p);
254
255 _cleanup_free_ char *oldpath = NULL;
256 p = getenv("PATH");
257 if (p)
258 assert_se(oldpath = strdup(p));
259
44ee03d1 260 assert_se(unsetenv("PATH") == 0);
92673045 261
8c35c10d 262 assert_se(find_executable_full("sh", NULL, NULL, true, &p, NULL) == 0);
92673045
ZJS
263 puts(p);
264 assert_se(streq(basename(p), "sh"));
265 free(p);
266
8c35c10d 267 assert_se(find_executable_full("sh", NULL, NULL, false, &p, NULL) == 0);
92673045
ZJS
268 puts(p);
269 assert_se(streq(basename(p), "sh"));
270 free(p);
271
272 if (oldpath)
273 assert_se(setenv("PATH", oldpath, true) >= 0);
8c35c10d 274
275 assert_se((fd = mkostemp_safe(fn)) >= 0);
276 assert_se(fchmod(fd, 0755) >= 0);
277
278 test_file_name = basename(fn);
279
280 assert_se(find_executable_full(test_file_name, NULL, STRV_MAKE("/doesnotexist", "/tmp", "/bin"), false, &p, NULL) == 0);
281 puts(p);
282 assert_se(streq(p, fn));
283 free(p);
284
285 (void) unlink(fn);
286 assert_se(find_executable_full(test_file_name, NULL, STRV_MAKE("/doesnotexist", "/tmp", "/bin"), false, &p, NULL) == -ENOENT);
92673045
ZJS
287}
288
4f7452a8 289TEST(find_executable) {
c9d954b2
ZJS
290 char *p;
291
f7bc0c32 292 assert_se(find_executable("/bin/sh", &p) == 0);
c9d954b2 293 puts(p);
85eca92e 294 assert_se(path_equal(p, "/bin/sh"));
c9d954b2
ZJS
295 free(p);
296
4f7452a8 297 assert_se(find_executable(saved_argv[0], &p) == 0);
c9d954b2 298 puts(p);
92673045 299 assert_se(endswith(p, "/test-path-util"));
8d95631e 300 assert_se(path_is_absolute(p));
c9d954b2
ZJS
301 free(p);
302
f7bc0c32 303 assert_se(find_executable("sh", &p) == 0);
c9d954b2 304 puts(p);
8d95631e
FB
305 assert_se(endswith(p, "/sh"));
306 assert_se(path_is_absolute(p));
c9d954b2
ZJS
307 free(p);
308
92673045
ZJS
309 assert_se(find_executable("/bin/touch", &p) == 0);
310 assert_se(streq(p, "/bin/touch"));
311 free(p);
312
313 assert_se(find_executable("touch", &p) == 0);
314 assert_se(path_is_absolute(p));
315 assert_se(streq(basename(p), "touch"));
316 free(p);
317
f7bc0c32
ZJS
318 assert_se(find_executable("xxxx-xxxx", &p) == -ENOENT);
319 assert_se(find_executable("/some/dir/xxxx-xxxx", &p) == -ENOENT);
92673045 320 assert_se(find_executable("/proc/filesystems", &p) == -EACCES);
c9d954b2
ZJS
321}
322
5ca9139a
ZJS
323static void test_find_executable_exec_one(const char *path) {
324 _cleanup_free_ char *t = NULL;
254d1313 325 _cleanup_close_ int fd = -EBADF;
5ca9139a
ZJS
326 pid_t pid;
327 int r;
328
8c35c10d 329 r = find_executable_full(path, NULL, NULL, false, &t, &fd);
5ca9139a
ZJS
330
331 log_info_errno(r, "%s: %s → %s: %d/%m", __func__, path, t ?: "-", fd);
332
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));
337
338 pid = fork();
339 assert_se(pid >= 0);
340 if (pid == 0) {
a6d9111c
ZJS
341 r = fexecve_or_execve(fd, t, STRV_MAKE(t, "--version"), STRV_MAKE(NULL));
342 log_error_errno(r, "[f]execve: %m");
5ca9139a
ZJS
343 _exit(EXIT_FAILURE);
344 }
345
346 assert_se(wait_for_terminate_and_check(t, pid, WAIT_LOG) == 0);
347}
348
4f7452a8 349TEST(find_executable_exec) {
5ca9139a
ZJS
350 test_find_executable_exec_one("touch");
351 test_find_executable_exec_one("/bin/touch");
a6d9111c
ZJS
352
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);
5ca9139a
ZJS
356}
357
4f7452a8 358TEST(prefixes) {
b82f71c7
LP
359 static const char* const values[] = {
360 "/a/b/c/d",
361 "/a/b/c",
362 "/a/b",
363 "/a",
364 "",
365 NULL
366 };
e203f7c3 367 unsigned i;
fecffe5d 368 char s[PATH_MAX];
e203f7c3 369 bool b;
fecffe5d 370
e203f7c3
LP
371 i = 0;
372 PATH_FOREACH_PREFIX_MORE(s, "/a/b/c/d") {
fecffe5d
LP
373 log_error("---%s---", s);
374 assert_se(streq(s, values[i++]));
375 }
e203f7c3 376 assert_se(values[i] == NULL);
fecffe5d 377
e203f7c3
LP
378 i = 1;
379 PATH_FOREACH_PREFIX(s, "/a/b/c/d") {
380 log_error("---%s---", s);
381 assert_se(streq(s, values[i++]));
382 }
fecffe5d
LP
383 assert_se(values[i] == NULL);
384
385 i = 0;
e203f7c3 386 PATH_FOREACH_PREFIX_MORE(s, "////a////b////c///d///////")
fecffe5d 387 assert_se(streq(s, values[i++]));
e203f7c3 388 assert_se(values[i] == NULL);
fecffe5d 389
e203f7c3
LP
390 i = 1;
391 PATH_FOREACH_PREFIX(s, "////a////b////c///d///////")
392 assert_se(streq(s, values[i++]));
fecffe5d
LP
393 assert_se(values[i] == NULL);
394
395 PATH_FOREACH_PREFIX(s, "////")
04499a70 396 assert_not_reached();
e203f7c3
LP
397
398 b = false;
399 PATH_FOREACH_PREFIX_MORE(s, "////") {
400 assert_se(!b);
fecffe5d 401 assert_se(streq(s, ""));
e203f7c3
LP
402 b = true;
403 }
404 assert_se(b);
fecffe5d
LP
405
406 PATH_FOREACH_PREFIX(s, "")
04499a70 407 assert_not_reached();
fecffe5d 408
e203f7c3
LP
409 b = false;
410 PATH_FOREACH_PREFIX_MORE(s, "") {
8d95631e
FB
411 assert_se(!b);
412 assert_se(streq(s, ""));
e203f7c3
LP
413 b = true;
414 }
fecffe5d
LP
415}
416
4f7452a8 417TEST(path_join) {
62a85ee0 418#define test_join(expected, ...) { \
59ae3a95 419 _cleanup_free_ char *z = NULL; \
62a85ee0
ZJS
420 z = path_join(__VA_ARGS__); \
421 log_debug("got \"%s\", expected \"%s\"", z, expected); \
59ae3a95
TA
422 assert_se(streq(z, expected)); \
423 }
424
62a85ee0
ZJS
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);
430
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);
436
652ef298
ZJS
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);
442
62a85ee0 443 test_join("", "", NULL);
652ef298
ZJS
444 test_join("", NULL, "");
445 test_join("", NULL, NULL);
62a85ee0
ZJS
446
447 test_join("foo/bar", "foo", "bar");
448 test_join("foo/bar", "", "foo", "bar");
652ef298 449 test_join("foo/bar", NULL, "foo", NULL, "bar");
62a85ee0
ZJS
450 test_join("foo/bar", "", "foo", "", "bar", "");
451 test_join("foo/bar", "", "", "", "", "foo", "", "", "", "bar", "", "", "");
452
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//");
0c6ea3a4
ZJS
459}
460
4f7452a8 461TEST(path_extend) {
7ae27680
LP
462 _cleanup_free_ char *p = NULL;
463
7ae27680
LP
464 assert_se(path_extend(&p, "foo", "bar", "baz") == p);
465 assert_se(streq(p, "foo/bar/baz"));
466
467 assert_se(path_extend(&p, "foo", "bar", "baz") == p);
468 assert_se(streq(p, "foo/bar/baz/foo/bar/baz"));
469
470 p = mfree(p);
471 assert_se(path_extend(&p, "foo") == p);
472 assert_se(streq(p, "foo"));
473
474 assert_se(path_extend(&p, "/foo") == p);
475 assert_se(streq(p, "foo/foo"));
340cd6b6
YW
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 */
480
481 assert_se(free_and_strdup(&p, "/") >= 0);
482 assert_se(path_extend(&p, "foo") == p);
483 assert_se(streq(p, "/foo"));
7ae27680
LP
484}
485
4f7452a8 486TEST(fsck_exists) {
eb66db55 487 /* Ensure we use a sane default for PATH. */
44ee03d1 488 assert_se(unsetenv("PATH") == 0);
eb66db55
MG
489
490 /* fsck.minix is provided by util-linux and will probably exist. */
13556724 491 assert_se(fsck_exists_for_fstype("minix") == 1);
eb66db55 492
13556724
JK
493 assert_se(fsck_exists_for_fstype("AbCdE") == 0);
494 assert_se(fsck_exists_for_fstype("/../bin/") == 0);
eb66db55
MG
495}
496
fe69c41e
YW
497static void test_path_make_relative_one(const char *from, const char *to, const char *expected) {
498 _cleanup_free_ char *z = NULL;
499 int r;
6b56a651 500
fe69c41e 501 log_info("/* %s(%s, %s) */", __func__, from, to);
771fded3 502
fe69c41e
YW
503 r = path_make_relative(from, to, &z);
504 assert_se((r >= 0) == !!expected);
505 assert_se(streq_ptr(z, expected));
506}
6b56a651 507
4f7452a8 508TEST(path_make_relative) {
fe69c41e
YW
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);
512
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");
6b56a651
TK
521}
522
d4f60bdc
YW
523static void test_path_make_relative_parent_one(const char *from, const char *to, const char *expected) {
524 _cleanup_free_ char *z = NULL;
525 int r;
526
527 log_info("/* %s(%s, %s) */", __func__, from, to);
528
529 r = path_make_relative_parent(from, to, &z);
530 assert_se((r >= 0) == !!expected);
531 assert_se(streq_ptr(z, expected));
532}
533
534TEST(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);
539
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");
548}
549
4f7452a8 550TEST(path_strv_resolve) {
3e8a78c8
MM
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;
3e8a78c8
MM
554
555 assert_se(mkdtemp(tmp_dir) != NULL);
556
bea1a013 557 search_dirs = strv_new("/dir1", "/dir2", "/dir3");
3e8a78c8
MM
558 assert_se(search_dirs);
559 STRV_FOREACH(d, search_dirs) {
b910cc72 560 char *p = path_join(tmp_dir, *d);
3e8a78c8
MM
561 assert_se(p);
562 assert_se(strv_push(&absolute_dirs, p) == 0);
563 }
564
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);
568
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"));
573
c6878637 574 assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
3e8a78c8
MM
575}
576
63f11e35
YW
577static void test_path_startswith_one(const char *path, const char *prefix, const char *skipped, const char *expected) {
578 const char *p, *q;
0470289b 579
63f11e35 580 log_debug("/* %s(%s, %s) */", __func__, path, prefix);
0470289b 581
63f11e35
YW
582 p = path_startswith(path, prefix);
583 assert_se(streq_ptr(p, expected));
584 if (p) {
585 q = strjoina(skipped, p);
586 assert_se(streq(q, path));
587 assert_se(p == path + strlen(skipped));
588 }
589}
0470289b 590
4f7452a8 591TEST(path_startswith) {
63f11e35
YW
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/", "");
602
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);
5895b62f
RC
611}
612
1d13f648
LP
613static void test_prefix_root_one(const char *r, const char *p, const char *expected) {
614 _cleanup_free_ char *s = NULL;
615 const char *t;
616
c6134d3e
LP
617 assert_se(s = path_join(r, p));
618 assert_se(path_equal_ptr(s, expected));
1d13f648
LP
619
620 t = prefix_roota(r, p);
621 assert_se(t);
c6134d3e 622 assert_se(path_equal_ptr(t, expected));
1d13f648
LP
623}
624
4f7452a8 625TEST(prefix_root) {
1d13f648
LP
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");
f9421dd8
YW
632 test_prefix_root_one("/", "foo", "/foo");
633 test_prefix_root_one("", "foo", "foo");
634 test_prefix_root_one(NULL, "foo", "foo");
1d13f648
LP
635
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");
642}
643
4f7452a8 644TEST(file_in_same_dir) {
63292663
RC
645 char *t;
646
162f6477 647 assert_se(file_in_same_dir("/", "a", &t) == -EADDRNOTAVAIL);
63292663 648
162f6477 649 assert_se(file_in_same_dir("/", "/a", &t) >= 0);
63292663
RC
650 assert_se(streq(t, "/a"));
651 free(t);
652
162f6477 653 assert_se(file_in_same_dir("", "a", &t) == -EINVAL);
63292663 654
162f6477
LP
655 assert_se(file_in_same_dir("a/", "x", &t) >= 0);
656 assert_se(streq(t, "x"));
63292663
RC
657 free(t);
658
162f6477 659 assert_se(file_in_same_dir("bar/foo", "bar", &t) >= 0);
63292663
RC
660 assert_se(streq(t, "bar/bar"));
661 free(t);
662}
663
0ee54dd4
YW
664static void test_path_find_first_component_one(
665 const char *path,
666 bool accept_dot_dot,
667 char **expected,
668 int ret) {
669
670 log_debug("/* %s(\"%s\", accept_dot_dot=%s) */", __func__, strnull(path), yes_no(accept_dot_dot));
671
672 for (const char *p = path;;) {
673 const char *e;
674 int r;
675
676 r = path_find_first_component(&p, accept_dot_dot, &e);
677 if (r <= 0) {
678 if (r == 0) {
de68bf78 679 if (path) {
0ee54dd4 680 assert_se(p == path + strlen_ptr(path));
de68bf78
YW
681 assert_se(isempty(p));
682 } else
0ee54dd4
YW
683 assert_se(!p);
684 assert_se(!e);
685 }
686 assert_se(r == ret);
687 assert_se(strv_isempty(expected));
688 return;
689 }
690
691 assert_se(e);
692 assert_se(strcspn(e, "/") == (size_t) r);
693 assert_se(strlen_ptr(*expected) == (size_t) r);
694 assert_se(strneq(e, *expected++, r));
de68bf78
YW
695
696 assert_se(p);
697 log_debug("p=%s", p);
698 if (!isempty(*expected))
699 assert_se(startswith(p, *expected));
700 else if (ret >= 0) {
701 assert_se(p == path + strlen_ptr(path));
702 assert_se(isempty(p));
703 }
0ee54dd4
YW
704 }
705}
706
4f7452a8 707TEST(path_find_first_component) {
0ee54dd4
YW
708 _cleanup_free_ char *hoge = NULL;
709 char foo[NAME_MAX * 2];
710
0ee54dd4
YW
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);
de68bf78 725 test_path_find_first_component_one("a/foo./b//././/", false, STRV_MAKE("a", "foo.", "b"), 0);
0ee54dd4
YW
726
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);
de68bf78 741 test_path_find_first_component_one("a/foo./b//././/", true, STRV_MAKE("a", "foo.", "b"), 0);
0ee54dd4
YW
742
743 memset(foo, 'a', sizeof(foo) -1);
744 char_array_0(foo);
745
746 test_path_find_first_component_one(foo, false, NULL, -EINVAL);
747 test_path_find_first_component_one(foo, true, NULL, -EINVAL);
748
749 hoge = strjoin("a/b/c/", foo, "//d/e/.//f/");
750 assert_se(hoge);
751
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);
754}
755
484cd43c
YW
756static void test_path_find_last_component_one(
757 const char *path,
758 bool accept_dot_dot,
759 char **expected,
760 int ret) {
761
762 log_debug("/* %s(\"%s\", accept_dot_dot=%s) */", __func__, strnull(path), yes_no(accept_dot_dot));
763
764 for (const char *next = NULL;;) {
765 const char *e;
766 int r;
767
768 r = path_find_last_component(path, accept_dot_dot, &next, &e);
769 if (r <= 0) {
770 if (r == 0) {
771 assert_se(next == path);
772 assert_se(!e);
773 }
774 assert_se(r == ret);
775 assert_se(strv_isempty(expected));
776 return;
777 }
778
779 assert_se(e);
780 assert_se(strcspn(e, "/") == (size_t) r);
781 assert_se(strlen_ptr(*expected) == (size_t) r);
782 assert_se(strneq(e, *expected++, r));
de68bf78
YW
783
784 assert_se(next);
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));
790 } else if (ret >= 0)
791 assert_se(next == path);
484cd43c
YW
792 }
793}
794
4f7452a8 795TEST(path_find_last_component) {
484cd43c
YW
796 _cleanup_free_ char *hoge = NULL;
797 char foo[NAME_MAX * 2];
798
484cd43c
YW
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);
813
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);
828
829 memset(foo, 'a', sizeof(foo) -1);
830 char_array_0(foo);
831
832 test_path_find_last_component_one(foo, false, NULL, -EINVAL);
833 test_path_find_last_component_one(foo, true, NULL, -EINVAL);
834
835 hoge = strjoin(foo, "/a/b/c/");
836 assert_se(hoge);
837
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);
840}
841
4f7452a8 842TEST(last_path_component) {
77e0a1b5 843 assert_se(last_path_component(NULL) == NULL);
b12d25a8
ZJS
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/"));
69f9ccf1 855 assert_se(streq(last_path_component(""), ""));
8460289f
LP
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/"));
b12d25a8
ZJS
860}
861
a60c8eee
LP
862static void test_path_extract_filename_one(const char *input, const char *output, int ret) {
863 _cleanup_free_ char *k = NULL;
864 int r;
865
866 r = path_extract_filename(input, &k);
a6e016af
ZJS
867 log_info("%s → %s/%s [expected: %s/%s]",
868 strnull(input),
869 strnull(k), r < 0 ? STRERROR(r) : "-",
870 strnull(output), ret < 0 ? STRERROR(ret) : "-");
a60c8eee
LP
871 assert_se(streq_ptr(k, output));
872 assert_se(r == ret);
873}
874
4f7452a8 875TEST(path_extract_filename) {
a60c8eee
LP
876 test_path_extract_filename_one(NULL, NULL, -EINVAL);
877 test_path_extract_filename_one("a/b/c", "c", 0);
ee277c6b 878 test_path_extract_filename_one("a/b/c/", "c", O_DIRECTORY);
3cdcbdd3
LP
879 test_path_extract_filename_one("/", NULL, -EADDRNOTAVAIL);
880 test_path_extract_filename_one("//", NULL, -EADDRNOTAVAIL);
881 test_path_extract_filename_one("///", NULL, -EADDRNOTAVAIL);
01950464
YW
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);
a60c8eee 888 test_path_extract_filename_one("/foo/a", "a", 0);
ee277c6b 889 test_path_extract_filename_one("/foo/a/", "a", O_DIRECTORY);
a60c8eee
LP
890 test_path_extract_filename_one("", NULL, -EINVAL);
891 test_path_extract_filename_one("a", "a", 0);
ee277c6b 892 test_path_extract_filename_one("a/", "a", O_DIRECTORY);
01950464 893 test_path_extract_filename_one("a/././//.", "a", O_DIRECTORY);
a60c8eee 894 test_path_extract_filename_one("/a", "a", 0);
ee277c6b 895 test_path_extract_filename_one("/a/", "a", O_DIRECTORY);
01950464 896 test_path_extract_filename_one("/a//./.", "a", O_DIRECTORY);
ee277c6b 897 test_path_extract_filename_one("/////////////a/////////////", "a", O_DIRECTORY);
01950464
YW
898 test_path_extract_filename_one("//./a/.///b./././.c//./d//.", "d", O_DIRECTORY);
899 test_path_extract_filename_one("xx/.", "xx", O_DIRECTORY);
a60c8eee
LP
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);
a60c8eee
LP
904}
905
8dcb891c
LP
906static void test_path_extract_directory_one(const char *input, const char *output, int ret) {
907 _cleanup_free_ char *k = NULL;
908 int r;
909
910 r = path_extract_directory(input, &k);
a6e016af
ZJS
911 log_info("%s → %s/%s [expected: %s/%s]",
912 strnull(input),
913 strnull(k), r < 0 ? STRERROR(r) : "-",
914 strnull(output), STRERROR(ret));
8dcb891c
LP
915 assert_se(streq_ptr(k, output));
916 assert_se(r == ret);
917
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 */
920 if (r >= 0) {
921 _cleanup_free_ char *f = NULL;
922
923 r = path_extract_filename(input, &f);
924 if (r >= 0) {
925 _cleanup_free_ char *j = NULL;
926
927 assert_se(j = path_join(k, f));
928 assert_se(path_equal(input, j));
929 }
930 }
931}
932
4f7452a8 933TEST(path_extract_directory) {
8dcb891c
LP
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);
01950464
YW
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);
8dcb891c
LP
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);
01950464 951 test_path_extract_directory_one("a/././//.", NULL, -EDESTADDRREQ);
8dcb891c
LP
952 test_path_extract_directory_one("/a", "/", 0);
953 test_path_extract_directory_one("/a/", "/", 0);
01950464 954 test_path_extract_directory_one("/a//./.", "/", 0);
8dcb891c 955 test_path_extract_directory_one("/////////////a/////////////", "/", 0);
01950464
YW
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);
8dcb891c
LP
962}
963
4f7452a8 964TEST(filename_is_valid) {
2ef2376d 965 char foo[NAME_MAX+2];
63292663
RC
966
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(".."));
1c322571
ZJS
972 assert_se(!filename_is_valid("bar/foo"));
973 assert_se(!filename_is_valid("bar/foo/"));
974 assert_se(!filename_is_valid("bar//"));
63292663 975
2ef2376d
LP
976 memset(foo, 'a', sizeof(foo) - 1);
977 char_array_0(foo);
63292663
RC
978
979 assert_se(!filename_is_valid(foo));
980
981 assert_se(filename_is_valid("foo_bar-333"));
982 assert_se(filename_is_valid("o.o"));
983}
984
32df2e14 985static void test_path_is_valid_and_safe_one(const char *p, bool ret) {
7802194a 986 log_debug("/* %s(\"%s\") */", __func__, strnull(p));
32df2e14
YW
987
988 assert_se(path_is_valid(p) == ret);
989 if (ret)
990 ret = !streq(p, "..") &&
991 !startswith(p, "../") &&
992 !endswith(p, "/..") &&
993 !strstr(p, "/../");
994 assert_se(path_is_safe(p) == ret);
995}
996
4f7452a8 997TEST(path_is_valid_and_safe) {
2ef2376d
LP
998 char foo[PATH_MAX+2];
999 const char *c;
1000
32df2e14
YW
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);
2ef2376d
LP
1014
1015 memset(foo, 'a', sizeof(foo) -1);
1016 char_array_0(foo);
1017
32df2e14 1018 test_path_is_valid_and_safe_one(foo, false);
2ef2376d
LP
1019
1020 c = strjoina("/xxx/", foo, "/yyy");
32df2e14 1021 test_path_is_valid_and_safe_one(c, false);
2ef2376d 1022
32df2e14
YW
1023 test_path_is_valid_and_safe_one("foo_bar-333", true);
1024 test_path_is_valid_and_safe_one("o.o", true);
2ef2376d
LP
1025}
1026
4f7452a8 1027TEST(hidden_or_backup_file) {
b05b9cde
ZJS
1028 assert_se(hidden_or_backup_file(".hidden"));
1029 assert_se(hidden_or_backup_file("..hidden"));
1030 assert_se(!hidden_or_backup_file("hidden."));
1031
1032 assert_se(hidden_or_backup_file("backup~"));
1033 assert_se(hidden_or_backup_file(".backup~"));
1034
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"));
1038
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"));
1043
1044 assert_se(!hidden_or_backup_file("test.rpmnew."));
1045 assert_se(!hidden_or_backup_file("test.dpkg-old.foo"));
1046}
1047
4f7452a8 1048TEST(skip_dev_prefix) {
a119ec7c
LP
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"));
1060}
1061
4f7452a8 1062TEST(empty_or_root) {
57ea45e1
LP
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//"));
1073}
1074
4f7452a8 1075TEST(path_startswith_set) {
d898ed65
LP
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));
1081
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));
1087
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));
1093}
1094
4f7452a8 1095TEST(path_startswith_strv) {
cc4d7d81
ZJS
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));
1101
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));
1107
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));
1113}
1114
3b703fe2
YW
1115static void test_path_glob_can_match_one(const char *pattern, const char *prefix, const char *expected) {
1116 _cleanup_free_ char *result = NULL;
1117
1118 log_debug("%s(%s, %s, %s)", __func__, pattern, prefix, strnull(expected));
1119
1120 assert_se(path_glob_can_match(pattern, prefix, &result) == !!expected);
1121 assert_se(streq_ptr(result, expected));
1122}
1123
1124TEST(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");
1130
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");
1136
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");
1143
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/*");
1151}
1152
4f7452a8 1153TEST(print_MAX) {
69866062
LP
1154 log_info("PATH_MAX=%zu\n"
1155 "FILENAME_MAX=%zu\n"
1156 "NAME_MAX=%zu",
1157 (size_t) PATH_MAX,
1158 (size_t) FILENAME_MAX,
1159 (size_t) NAME_MAX);
1160
1161 assert_cc(FILENAME_MAX == PATH_MAX);
76877b46 1162}
4f7452a8
JJ
1163
1164DEFINE_TEST_MAIN(LOG_DEBUG);