]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-path-util.c
conf-files: drop redundant call of chase()
[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"));
727e63e3
LB
49
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"));
76877b46
ZJS
54}
55
cb71ed91
YW
56static void test_path_simplify_one(const char *in, const char *out) {
57 char *p;
58
2f82562b 59 p = strdupa_safe(in);
cb71ed91
YW
60 path_simplify(p);
61 log_debug("/* test_path_simplify(%s) → %s (expected: %s) */", in, p, out);
62 assert_se(streq(p, out));
63}
64
4f7452a8 65TEST(path_simplify) {
cb71ed91
YW
66 _cleanup_free_ char *hoge = NULL, *hoge_out = NULL;
67 char foo[NAME_MAX * 2];
68
cb71ed91
YW
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/..");
88
89 memset(foo, 'a', sizeof(foo) -1);
90 char_array_0(foo);
91
92 test_path_simplify_one(foo, foo);
93
94 hoge = strjoin("/", foo);
95 assert_se(hoge);
96 test_path_simplify_one(hoge, hoge);
97 hoge = mfree(hoge);
98
99 hoge = strjoin("a////.//././//./b///././/./c/////././//./", foo, "//.//////d/e/.//f/");
100 assert_se(hoge);
101
102 hoge_out = strjoin("a/b/c/", foo, "//.//////d/e/.//f/");
103 assert_se(hoge_out);
104
105 test_path_simplify_one(hoge, hoge_out);
106}
107
353df443
YW
108static void test_path_compare_one(const char *a, const char *b, int expected) {
109 int r;
110
111 assert_se(path_compare(a, a) == 0);
112 assert_se(path_compare(b, b) == 0);
113
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));
118
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));
123}
124
4f7452a8 125TEST(path_compare) {
353df443
YW
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);
6d216bdd
ZJS
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);
353df443
YW
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);
151}
152
4f7452a8 153TEST(path_equal_root) {
84e72b5e
ZJS
154 /* Nail down the details of how path_equal("/", ...) works. */
155
156 assert_se(path_equal("/", "/"));
157 assert_se(path_equal("/", "//"));
158
353df443 159 assert_se(path_equal("/", "/./"));
84e72b5e
ZJS
160 assert_se(!path_equal("/", "/../"));
161
162 assert_se(!path_equal("/", "/.../"));
163
164 /* Make sure that files_same works as expected. */
165
e3f791a2
ZJS
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);
84e72b5e 170
e3f791a2
ZJS
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);
84e72b5e 175
e3f791a2
ZJS
176 assert_se(files_same("/", "/.../", 0) == -ENOENT);
177 assert_se(files_same("/", "/.../", AT_SYMLINK_NOFOLLOW) == -ENOENT);
84e72b5e
ZJS
178
179 /* The same for path_equal_or_files_same. */
180
e3f791a2
ZJS
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));
84e72b5e 185
e3f791a2
ZJS
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));
84e72b5e 190
e3f791a2
ZJS
191 assert_se(!path_equal_or_files_same("/", "/.../", 0));
192 assert_se(!path_equal_or_files_same("/", "/.../", AT_SYMLINK_NOFOLLOW));
84e72b5e
ZJS
193}
194
4f7452a8 195TEST(find_executable_full) {
92673045 196 char *p;
8c35c10d 197 char* test_file_name;
254d1313 198 _cleanup_close_ int fd = -EBADF;
8c35c10d 199 char fn[] = "/tmp/test-XXXXXX";
92673045 200
8c35c10d 201 assert_se(find_executable_full("sh", NULL, NULL, true, &p, NULL) == 0);
92673045
ZJS
202 puts(p);
203 assert_se(streq(basename(p), "sh"));
204 free(p);
205
8c35c10d 206 assert_se(find_executable_full("sh", NULL, NULL, false, &p, NULL) == 0);
92673045
ZJS
207 puts(p);
208 assert_se(streq(basename(p), "sh"));
209 free(p);
210
211 _cleanup_free_ char *oldpath = NULL;
212 p = getenv("PATH");
213 if (p)
214 assert_se(oldpath = strdup(p));
215
44ee03d1 216 assert_se(unsetenv("PATH") == 0);
92673045 217
8c35c10d 218 assert_se(find_executable_full("sh", NULL, NULL, true, &p, NULL) == 0);
92673045
ZJS
219 puts(p);
220 assert_se(streq(basename(p), "sh"));
221 free(p);
222
8c35c10d 223 assert_se(find_executable_full("sh", NULL, NULL, false, &p, NULL) == 0);
92673045
ZJS
224 puts(p);
225 assert_se(streq(basename(p), "sh"));
226 free(p);
227
228 if (oldpath)
229 assert_se(setenv("PATH", oldpath, true) >= 0);
8c35c10d 230
231 assert_se((fd = mkostemp_safe(fn)) >= 0);
232 assert_se(fchmod(fd, 0755) >= 0);
233
234 test_file_name = basename(fn);
235
236 assert_se(find_executable_full(test_file_name, NULL, STRV_MAKE("/doesnotexist", "/tmp", "/bin"), false, &p, NULL) == 0);
237 puts(p);
238 assert_se(streq(p, fn));
239 free(p);
240
241 (void) unlink(fn);
242 assert_se(find_executable_full(test_file_name, NULL, STRV_MAKE("/doesnotexist", "/tmp", "/bin"), false, &p, NULL) == -ENOENT);
92673045
ZJS
243}
244
4f7452a8 245TEST(find_executable) {
c9d954b2
ZJS
246 char *p;
247
f7bc0c32 248 assert_se(find_executable("/bin/sh", &p) == 0);
c9d954b2 249 puts(p);
85eca92e 250 assert_se(path_equal(p, "/bin/sh"));
c9d954b2
ZJS
251 free(p);
252
4f7452a8 253 assert_se(find_executable(saved_argv[0], &p) == 0);
c9d954b2 254 puts(p);
92673045 255 assert_se(endswith(p, "/test-path-util"));
8d95631e 256 assert_se(path_is_absolute(p));
c9d954b2
ZJS
257 free(p);
258
f7bc0c32 259 assert_se(find_executable("sh", &p) == 0);
c9d954b2 260 puts(p);
8d95631e
FB
261 assert_se(endswith(p, "/sh"));
262 assert_se(path_is_absolute(p));
c9d954b2
ZJS
263 free(p);
264
92673045
ZJS
265 assert_se(find_executable("/bin/touch", &p) == 0);
266 assert_se(streq(p, "/bin/touch"));
267 free(p);
268
269 assert_se(find_executable("touch", &p) == 0);
270 assert_se(path_is_absolute(p));
271 assert_se(streq(basename(p), "touch"));
272 free(p);
273
f7bc0c32
ZJS
274 assert_se(find_executable("xxxx-xxxx", &p) == -ENOENT);
275 assert_se(find_executable("/some/dir/xxxx-xxxx", &p) == -ENOENT);
92673045 276 assert_se(find_executable("/proc/filesystems", &p) == -EACCES);
c9d954b2
ZJS
277}
278
5ca9139a
ZJS
279static void test_find_executable_exec_one(const char *path) {
280 _cleanup_free_ char *t = NULL;
254d1313 281 _cleanup_close_ int fd = -EBADF;
5ca9139a
ZJS
282 pid_t pid;
283 int r;
284
8c35c10d 285 r = find_executable_full(path, NULL, NULL, false, &t, &fd);
5ca9139a
ZJS
286
287 log_info_errno(r, "%s: %s → %s: %d/%m", __func__, path, t ?: "-", fd);
288
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));
293
294 pid = fork();
295 assert_se(pid >= 0);
296 if (pid == 0) {
a6d9111c
ZJS
297 r = fexecve_or_execve(fd, t, STRV_MAKE(t, "--version"), STRV_MAKE(NULL));
298 log_error_errno(r, "[f]execve: %m");
5ca9139a
ZJS
299 _exit(EXIT_FAILURE);
300 }
301
302 assert_se(wait_for_terminate_and_check(t, pid, WAIT_LOG) == 0);
303}
304
4f7452a8 305TEST(find_executable_exec) {
5ca9139a
ZJS
306 test_find_executable_exec_one("touch");
307 test_find_executable_exec_one("/bin/touch");
a6d9111c
ZJS
308
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);
5ca9139a
ZJS
312}
313
4f7452a8 314TEST(prefixes) {
b82f71c7
LP
315 static const char* const values[] = {
316 "/a/b/c/d",
317 "/a/b/c",
318 "/a/b",
319 "/a",
320 "",
321 NULL
322 };
e203f7c3 323 unsigned i;
fecffe5d 324 char s[PATH_MAX];
e203f7c3 325 bool b;
fecffe5d 326
e203f7c3
LP
327 i = 0;
328 PATH_FOREACH_PREFIX_MORE(s, "/a/b/c/d") {
fecffe5d
LP
329 log_error("---%s---", s);
330 assert_se(streq(s, values[i++]));
331 }
e203f7c3 332 assert_se(values[i] == NULL);
fecffe5d 333
e203f7c3
LP
334 i = 1;
335 PATH_FOREACH_PREFIX(s, "/a/b/c/d") {
336 log_error("---%s---", s);
337 assert_se(streq(s, values[i++]));
338 }
fecffe5d
LP
339 assert_se(values[i] == NULL);
340
341 i = 0;
e203f7c3 342 PATH_FOREACH_PREFIX_MORE(s, "////a////b////c///d///////")
fecffe5d 343 assert_se(streq(s, values[i++]));
e203f7c3 344 assert_se(values[i] == NULL);
fecffe5d 345
e203f7c3
LP
346 i = 1;
347 PATH_FOREACH_PREFIX(s, "////a////b////c///d///////")
348 assert_se(streq(s, values[i++]));
fecffe5d
LP
349 assert_se(values[i] == NULL);
350
351 PATH_FOREACH_PREFIX(s, "////")
04499a70 352 assert_not_reached();
e203f7c3
LP
353
354 b = false;
355 PATH_FOREACH_PREFIX_MORE(s, "////") {
356 assert_se(!b);
fecffe5d 357 assert_se(streq(s, ""));
e203f7c3
LP
358 b = true;
359 }
360 assert_se(b);
fecffe5d
LP
361
362 PATH_FOREACH_PREFIX(s, "")
04499a70 363 assert_not_reached();
fecffe5d 364
e203f7c3
LP
365 b = false;
366 PATH_FOREACH_PREFIX_MORE(s, "") {
8d95631e
FB
367 assert_se(!b);
368 assert_se(streq(s, ""));
e203f7c3
LP
369 b = true;
370 }
fecffe5d
LP
371}
372
4f7452a8 373TEST(path_join) {
62a85ee0 374#define test_join(expected, ...) { \
59ae3a95 375 _cleanup_free_ char *z = NULL; \
62a85ee0
ZJS
376 z = path_join(__VA_ARGS__); \
377 log_debug("got \"%s\", expected \"%s\"", z, expected); \
59ae3a95
TA
378 assert_se(streq(z, expected)); \
379 }
380
62a85ee0
ZJS
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);
386
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);
392
652ef298
ZJS
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);
398
62a85ee0 399 test_join("", "", NULL);
652ef298
ZJS
400 test_join("", NULL, "");
401 test_join("", NULL, NULL);
62a85ee0
ZJS
402
403 test_join("foo/bar", "foo", "bar");
404 test_join("foo/bar", "", "foo", "bar");
652ef298 405 test_join("foo/bar", NULL, "foo", NULL, "bar");
62a85ee0
ZJS
406 test_join("foo/bar", "", "foo", "", "bar", "");
407 test_join("foo/bar", "", "", "", "", "foo", "", "", "", "bar", "", "", "");
408
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//");
0c6ea3a4
ZJS
415}
416
4f7452a8 417TEST(path_extend) {
7ae27680
LP
418 _cleanup_free_ char *p = NULL;
419
7ae27680
LP
420 assert_se(path_extend(&p, "foo", "bar", "baz") == p);
421 assert_se(streq(p, "foo/bar/baz"));
422
423 assert_se(path_extend(&p, "foo", "bar", "baz") == p);
424 assert_se(streq(p, "foo/bar/baz/foo/bar/baz"));
425
426 p = mfree(p);
427 assert_se(path_extend(&p, "foo") == p);
428 assert_se(streq(p, "foo"));
429
430 assert_se(path_extend(&p, "/foo") == p);
431 assert_se(streq(p, "foo/foo"));
340cd6b6
YW
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 */
436
437 assert_se(free_and_strdup(&p, "/") >= 0);
438 assert_se(path_extend(&p, "foo") == p);
439 assert_se(streq(p, "/foo"));
7ae27680
LP
440}
441
4f7452a8 442TEST(fsck_exists) {
eb66db55 443 /* Ensure we use a sane default for PATH. */
44ee03d1 444 assert_se(unsetenv("PATH") == 0);
eb66db55
MG
445
446 /* fsck.minix is provided by util-linux and will probably exist. */
13556724 447 assert_se(fsck_exists_for_fstype("minix") == 1);
eb66db55 448
13556724
JK
449 assert_se(fsck_exists_for_fstype("AbCdE") == 0);
450 assert_se(fsck_exists_for_fstype("/../bin/") == 0);
eb66db55
MG
451}
452
fe69c41e
YW
453static void test_path_make_relative_one(const char *from, const char *to, const char *expected) {
454 _cleanup_free_ char *z = NULL;
455 int r;
6b56a651 456
fe69c41e 457 log_info("/* %s(%s, %s) */", __func__, from, to);
771fded3 458
fe69c41e
YW
459 r = path_make_relative(from, to, &z);
460 assert_se((r >= 0) == !!expected);
461 assert_se(streq_ptr(z, expected));
462}
6b56a651 463
4f7452a8 464TEST(path_make_relative) {
fe69c41e
YW
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);
468
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");
6b56a651
TK
477}
478
d4f60bdc
YW
479static void test_path_make_relative_parent_one(const char *from, const char *to, const char *expected) {
480 _cleanup_free_ char *z = NULL;
481 int r;
482
483 log_info("/* %s(%s, %s) */", __func__, from, to);
484
485 r = path_make_relative_parent(from, to, &z);
486 assert_se((r >= 0) == !!expected);
487 assert_se(streq_ptr(z, expected));
488}
489
490TEST(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);
495
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");
504}
505
4f7452a8 506TEST(path_strv_resolve) {
3e8a78c8
MM
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;
3e8a78c8
MM
510
511 assert_se(mkdtemp(tmp_dir) != NULL);
512
bea1a013 513 search_dirs = strv_new("/dir1", "/dir2", "/dir3");
3e8a78c8
MM
514 assert_se(search_dirs);
515 STRV_FOREACH(d, search_dirs) {
b910cc72 516 char *p = path_join(tmp_dir, *d);
3e8a78c8
MM
517 assert_se(p);
518 assert_se(strv_push(&absolute_dirs, p) == 0);
519 }
520
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);
524
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"));
529
c6878637 530 assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
3e8a78c8
MM
531}
532
63f11e35
YW
533static void test_path_startswith_one(const char *path, const char *prefix, const char *skipped, const char *expected) {
534 const char *p, *q;
0470289b 535
63f11e35 536 log_debug("/* %s(%s, %s) */", __func__, path, prefix);
0470289b 537
63f11e35
YW
538 p = path_startswith(path, prefix);
539 assert_se(streq_ptr(p, expected));
540 if (p) {
541 q = strjoina(skipped, p);
542 assert_se(streq(q, path));
543 assert_se(p == path + strlen(skipped));
544 }
545}
0470289b 546
4f7452a8 547TEST(path_startswith) {
63f11e35
YW
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/", "");
558
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);
5895b62f
RC
567}
568
1d13f648
LP
569static void test_prefix_root_one(const char *r, const char *p, const char *expected) {
570 _cleanup_free_ char *s = NULL;
571 const char *t;
572
c6134d3e
LP
573 assert_se(s = path_join(r, p));
574 assert_se(path_equal_ptr(s, expected));
1d13f648
LP
575
576 t = prefix_roota(r, p);
577 assert_se(t);
c6134d3e 578 assert_se(path_equal_ptr(t, expected));
1d13f648
LP
579}
580
4f7452a8 581TEST(prefix_root) {
1d13f648
LP
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");
f9421dd8
YW
588 test_prefix_root_one("/", "foo", "/foo");
589 test_prefix_root_one("", "foo", "foo");
590 test_prefix_root_one(NULL, "foo", "foo");
1d13f648
LP
591
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");
598}
599
4f7452a8 600TEST(file_in_same_dir) {
63292663
RC
601 char *t;
602
162f6477 603 assert_se(file_in_same_dir("/", "a", &t) == -EADDRNOTAVAIL);
63292663 604
162f6477 605 assert_se(file_in_same_dir("/", "/a", &t) >= 0);
63292663
RC
606 assert_se(streq(t, "/a"));
607 free(t);
608
162f6477 609 assert_se(file_in_same_dir("", "a", &t) == -EINVAL);
63292663 610
162f6477
LP
611 assert_se(file_in_same_dir("a/", "x", &t) >= 0);
612 assert_se(streq(t, "x"));
63292663
RC
613 free(t);
614
162f6477 615 assert_se(file_in_same_dir("bar/foo", "bar", &t) >= 0);
63292663
RC
616 assert_se(streq(t, "bar/bar"));
617 free(t);
618}
619
0ee54dd4
YW
620static void test_path_find_first_component_one(
621 const char *path,
622 bool accept_dot_dot,
623 char **expected,
624 int ret) {
625
626 log_debug("/* %s(\"%s\", accept_dot_dot=%s) */", __func__, strnull(path), yes_no(accept_dot_dot));
627
628 for (const char *p = path;;) {
629 const char *e;
630 int r;
631
632 r = path_find_first_component(&p, accept_dot_dot, &e);
633 if (r <= 0) {
634 if (r == 0) {
635 if (path)
636 assert_se(p == path + strlen_ptr(path));
637 else
638 assert_se(!p);
639 assert_se(!e);
640 }
641 assert_se(r == ret);
642 assert_se(strv_isempty(expected));
643 return;
644 }
645
646 assert_se(e);
647 assert_se(strcspn(e, "/") == (size_t) r);
648 assert_se(strlen_ptr(*expected) == (size_t) r);
649 assert_se(strneq(e, *expected++, r));
650 }
651}
652
4f7452a8 653TEST(path_find_first_component) {
0ee54dd4
YW
654 _cleanup_free_ char *hoge = NULL;
655 char foo[NAME_MAX * 2];
656
0ee54dd4
YW
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);
672
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);
688
689 memset(foo, 'a', sizeof(foo) -1);
690 char_array_0(foo);
691
692 test_path_find_first_component_one(foo, false, NULL, -EINVAL);
693 test_path_find_first_component_one(foo, true, NULL, -EINVAL);
694
695 hoge = strjoin("a/b/c/", foo, "//d/e/.//f/");
696 assert_se(hoge);
697
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);
700}
701
484cd43c
YW
702static void test_path_find_last_component_one(
703 const char *path,
704 bool accept_dot_dot,
705 char **expected,
706 int ret) {
707
708 log_debug("/* %s(\"%s\", accept_dot_dot=%s) */", __func__, strnull(path), yes_no(accept_dot_dot));
709
710 for (const char *next = NULL;;) {
711 const char *e;
712 int r;
713
714 r = path_find_last_component(path, accept_dot_dot, &next, &e);
715 if (r <= 0) {
716 if (r == 0) {
717 assert_se(next == path);
718 assert_se(!e);
719 }
720 assert_se(r == ret);
721 assert_se(strv_isempty(expected));
722 return;
723 }
724
725 assert_se(e);
726 assert_se(strcspn(e, "/") == (size_t) r);
727 assert_se(strlen_ptr(*expected) == (size_t) r);
728 assert_se(strneq(e, *expected++, r));
729 }
730}
731
4f7452a8 732TEST(path_find_last_component) {
484cd43c
YW
733 _cleanup_free_ char *hoge = NULL;
734 char foo[NAME_MAX * 2];
735
484cd43c
YW
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);
750
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);
765
766 memset(foo, 'a', sizeof(foo) -1);
767 char_array_0(foo);
768
769 test_path_find_last_component_one(foo, false, NULL, -EINVAL);
770 test_path_find_last_component_one(foo, true, NULL, -EINVAL);
771
772 hoge = strjoin(foo, "/a/b/c/");
773 assert_se(hoge);
774
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);
777}
778
4f7452a8 779TEST(last_path_component) {
77e0a1b5 780 assert_se(last_path_component(NULL) == NULL);
b12d25a8
ZJS
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/"));
69f9ccf1 792 assert_se(streq(last_path_component(""), ""));
8460289f
LP
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/"));
b12d25a8
ZJS
797}
798
a60c8eee
LP
799static void test_path_extract_filename_one(const char *input, const char *output, int ret) {
800 _cleanup_free_ char *k = NULL;
801 int r;
802
803 r = path_extract_filename(input, &k);
a6e016af
ZJS
804 log_info("%s → %s/%s [expected: %s/%s]",
805 strnull(input),
806 strnull(k), r < 0 ? STRERROR(r) : "-",
807 strnull(output), ret < 0 ? STRERROR(ret) : "-");
a60c8eee
LP
808 assert_se(streq_ptr(k, output));
809 assert_se(r == ret);
810}
811
4f7452a8 812TEST(path_extract_filename) {
a60c8eee
LP
813 test_path_extract_filename_one(NULL, NULL, -EINVAL);
814 test_path_extract_filename_one("a/b/c", "c", 0);
ee277c6b 815 test_path_extract_filename_one("a/b/c/", "c", O_DIRECTORY);
3cdcbdd3
LP
816 test_path_extract_filename_one("/", NULL, -EADDRNOTAVAIL);
817 test_path_extract_filename_one("//", NULL, -EADDRNOTAVAIL);
818 test_path_extract_filename_one("///", NULL, -EADDRNOTAVAIL);
01950464
YW
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);
a60c8eee 825 test_path_extract_filename_one("/foo/a", "a", 0);
ee277c6b 826 test_path_extract_filename_one("/foo/a/", "a", O_DIRECTORY);
a60c8eee
LP
827 test_path_extract_filename_one("", NULL, -EINVAL);
828 test_path_extract_filename_one("a", "a", 0);
ee277c6b 829 test_path_extract_filename_one("a/", "a", O_DIRECTORY);
01950464 830 test_path_extract_filename_one("a/././//.", "a", O_DIRECTORY);
a60c8eee 831 test_path_extract_filename_one("/a", "a", 0);
ee277c6b 832 test_path_extract_filename_one("/a/", "a", O_DIRECTORY);
01950464 833 test_path_extract_filename_one("/a//./.", "a", O_DIRECTORY);
ee277c6b 834 test_path_extract_filename_one("/////////////a/////////////", "a", O_DIRECTORY);
01950464
YW
835 test_path_extract_filename_one("//./a/.///b./././.c//./d//.", "d", O_DIRECTORY);
836 test_path_extract_filename_one("xx/.", "xx", O_DIRECTORY);
a60c8eee
LP
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);
a60c8eee
LP
841}
842
8dcb891c
LP
843static void test_path_extract_directory_one(const char *input, const char *output, int ret) {
844 _cleanup_free_ char *k = NULL;
845 int r;
846
847 r = path_extract_directory(input, &k);
a6e016af
ZJS
848 log_info("%s → %s/%s [expected: %s/%s]",
849 strnull(input),
850 strnull(k), r < 0 ? STRERROR(r) : "-",
851 strnull(output), STRERROR(ret));
8dcb891c
LP
852 assert_se(streq_ptr(k, output));
853 assert_se(r == ret);
854
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 */
857 if (r >= 0) {
858 _cleanup_free_ char *f = NULL;
859
860 r = path_extract_filename(input, &f);
861 if (r >= 0) {
862 _cleanup_free_ char *j = NULL;
863
864 assert_se(j = path_join(k, f));
865 assert_se(path_equal(input, j));
866 }
867 }
868}
869
4f7452a8 870TEST(path_extract_directory) {
8dcb891c
LP
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);
01950464
YW
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);
8dcb891c
LP
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);
01950464 888 test_path_extract_directory_one("a/././//.", NULL, -EDESTADDRREQ);
8dcb891c
LP
889 test_path_extract_directory_one("/a", "/", 0);
890 test_path_extract_directory_one("/a/", "/", 0);
01950464 891 test_path_extract_directory_one("/a//./.", "/", 0);
8dcb891c 892 test_path_extract_directory_one("/////////////a/////////////", "/", 0);
01950464
YW
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);
8dcb891c
LP
899}
900
4f7452a8 901TEST(filename_is_valid) {
2ef2376d 902 char foo[NAME_MAX+2];
63292663
RC
903
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(".."));
1c322571
ZJS
909 assert_se(!filename_is_valid("bar/foo"));
910 assert_se(!filename_is_valid("bar/foo/"));
911 assert_se(!filename_is_valid("bar//"));
63292663 912
2ef2376d
LP
913 memset(foo, 'a', sizeof(foo) - 1);
914 char_array_0(foo);
63292663
RC
915
916 assert_se(!filename_is_valid(foo));
917
918 assert_se(filename_is_valid("foo_bar-333"));
919 assert_se(filename_is_valid("o.o"));
920}
921
32df2e14 922static void test_path_is_valid_and_safe_one(const char *p, bool ret) {
7802194a 923 log_debug("/* %s(\"%s\") */", __func__, strnull(p));
32df2e14
YW
924
925 assert_se(path_is_valid(p) == ret);
926 if (ret)
927 ret = !streq(p, "..") &&
928 !startswith(p, "../") &&
929 !endswith(p, "/..") &&
930 !strstr(p, "/../");
931 assert_se(path_is_safe(p) == ret);
932}
933
4f7452a8 934TEST(path_is_valid_and_safe) {
2ef2376d
LP
935 char foo[PATH_MAX+2];
936 const char *c;
937
32df2e14
YW
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);
2ef2376d
LP
951
952 memset(foo, 'a', sizeof(foo) -1);
953 char_array_0(foo);
954
32df2e14 955 test_path_is_valid_and_safe_one(foo, false);
2ef2376d
LP
956
957 c = strjoina("/xxx/", foo, "/yyy");
32df2e14 958 test_path_is_valid_and_safe_one(c, false);
2ef2376d 959
32df2e14
YW
960 test_path_is_valid_and_safe_one("foo_bar-333", true);
961 test_path_is_valid_and_safe_one("o.o", true);
2ef2376d
LP
962}
963
4f7452a8 964TEST(hidden_or_backup_file) {
b05b9cde
ZJS
965 assert_se(hidden_or_backup_file(".hidden"));
966 assert_se(hidden_or_backup_file("..hidden"));
967 assert_se(!hidden_or_backup_file("hidden."));
968
969 assert_se(hidden_or_backup_file("backup~"));
970 assert_se(hidden_or_backup_file(".backup~"));
971
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"));
975
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"));
980
981 assert_se(!hidden_or_backup_file("test.rpmnew."));
982 assert_se(!hidden_or_backup_file("test.dpkg-old.foo"));
983}
984
4f7452a8 985TEST(skip_dev_prefix) {
a119ec7c
LP
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"));
997}
998
4f7452a8 999TEST(empty_or_root) {
57ea45e1
LP
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//"));
1010}
1011
4f7452a8 1012TEST(path_startswith_set) {
d898ed65
LP
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));
1018
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));
1024
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));
1030}
1031
4f7452a8 1032TEST(path_startswith_strv) {
cc4d7d81
ZJS
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));
1038
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));
1044
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));
1050}
1051
3b703fe2
YW
1052static void test_path_glob_can_match_one(const char *pattern, const char *prefix, const char *expected) {
1053 _cleanup_free_ char *result = NULL;
1054
1055 log_debug("%s(%s, %s, %s)", __func__, pattern, prefix, strnull(expected));
1056
1057 assert_se(path_glob_can_match(pattern, prefix, &result) == !!expected);
1058 assert_se(streq_ptr(result, expected));
1059}
1060
1061TEST(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");
1067
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");
1073
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");
1080
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/*");
1088}
1089
4f7452a8 1090TEST(print_MAX) {
69866062
LP
1091 log_info("PATH_MAX=%zu\n"
1092 "FILENAME_MAX=%zu\n"
1093 "NAME_MAX=%zu",
1094 (size_t) PATH_MAX,
1095 (size_t) FILENAME_MAX,
1096 (size_t) NAME_MAX);
1097
1098 assert_cc(FILENAME_MAX == PATH_MAX);
76877b46 1099}
4f7452a8
JJ
1100
1101DEFINE_TEST_MAIN(LOG_DEBUG);