]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-path-util.c
path-util: use path_is_safe() in path_is_normalized()
[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"
76877b46 9#include "macro.h"
07630cea 10#include "path-util.h"
5ca9139a 11#include "process-util.h"
c6878637 12#include "rm-rf.h"
84e72b5e 13#include "stat-util.h"
07630cea
LP
14#include "string-util.h"
15#include "strv.h"
6d7c4033 16#include "tests.h"
07630cea 17#include "util.h"
76877b46 18
3602ca6f
ZJS
19static void test_print_paths(void) {
20 log_info("DEFAULT_PATH=%s", DEFAULT_PATH);
21 log_info("DEFAULT_USER_PATH=%s", DEFAULT_USER_PATH);
22}
23
2230852b
MS
24#define test_path_compare(a, b, result) { \
25 assert_se(path_compare(a, b) == result); \
26 assert_se(path_compare(b, a) == -result); \
27 assert_se(path_equal(a, b) == !result); \
28 assert_se(path_equal(b, a) == !result); \
29 }
76877b46 30
858d36c1
YW
31static void test_path_simplify(const char *in, const char *out, const char *out_dot) {
32 char *p;
33
771fded3
ZJS
34 log_info("/* %s */", __func__);
35
858d36c1
YW
36 p = strdupa(in);
37 assert_se(streq(path_simplify(p, false), out));
38
39 p = strdupa(in);
40 assert_se(streq(path_simplify(p, true), out_dot));
41}
42
76877b46 43static void test_path(void) {
771fded3
ZJS
44 log_info("/* %s */", __func__);
45
2230852b
MS
46 test_path_compare("/goo", "/goo", 0);
47 test_path_compare("/goo", "/goo", 0);
48 test_path_compare("//goo", "/goo", 0);
49 test_path_compare("//goo/////", "/goo", 0);
50 test_path_compare("goo/////", "goo", 0);
76877b46 51
2230852b
MS
52 test_path_compare("/goo/boo", "/goo//boo", 0);
53 test_path_compare("//goo/boo", "/goo/boo//", 0);
76877b46 54
2230852b 55 test_path_compare("/", "///", 0);
76877b46 56
2230852b
MS
57 test_path_compare("/x", "x/", 1);
58 test_path_compare("x/", "/", -1);
76877b46 59
2230852b
MS
60 test_path_compare("/x/./y", "x/y", 1);
61 test_path_compare("x/.y", "x/y", -1);
62
63 test_path_compare("foo", "/foo", -1);
64 test_path_compare("/foo", "/foo/bar", -1);
65 test_path_compare("/foo/aaa", "/foo/b", -1);
66 test_path_compare("/foo/aaa", "/foo/b/a", -1);
67 test_path_compare("/foo/a", "/foo/aaa", -1);
68 test_path_compare("/foo/a/b", "/foo/aaa", -1);
76877b46
ZJS
69
70 assert_se(path_is_absolute("/"));
71 assert_se(!path_is_absolute("./"));
72
73 assert_se(is_path("/dir"));
74 assert_se(is_path("a/b"));
75 assert_se(!is_path("."));
76
2b6bf07d
ZJS
77 assert_se(streq(basename("./aa/bb/../file.da."), "file.da."));
78 assert_se(streq(basename("/aa///.file"), ".file"));
79 assert_se(streq(basename("/aa///file..."), "file..."));
80 assert_se(streq(basename("file.../"), ""));
76877b46 81
858d36c1
YW
82 test_path_simplify("aaa/bbb////ccc", "aaa/bbb/ccc", "aaa/bbb/ccc");
83 test_path_simplify("//aaa/.////ccc", "/aaa/./ccc", "/aaa/ccc");
84 test_path_simplify("///", "/", "/");
85 test_path_simplify("///.//", "/.", "/");
86 test_path_simplify("///.//.///", "/./.", "/");
87 test_path_simplify("////.././///../.", "/.././../.", "/../..");
afbae3e9
TH
88 test_path_simplify(".", ".", ".");
89 test_path_simplify("./", ".", ".");
90 test_path_simplify(".///.//./.", "./././.", ".");
91 test_path_simplify(".///.//././/", "./././.", ".");
858d36c1
YW
92 test_path_simplify("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.",
93 "/./aaa/././.bbb/../c./d.dd/..eeee/.",
94 "/aaa/.bbb/../c./d.dd/..eeee");
95 test_path_simplify("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/..",
96 "/./aaa/././.bbb/../c./d.dd/..eeee/..",
97 "/aaa/.bbb/../c./d.dd/..eeee/..");
98 test_path_simplify(".//./aaa///.//./.bbb/..///c.//d.dd///..eeee/..",
99 "././aaa/././.bbb/../c./d.dd/..eeee/..",
100 "aaa/.bbb/../c./d.dd/..eeee/..");
101 test_path_simplify("..//./aaa///.//./.bbb/..///c.//d.dd///..eeee/..",
102 ".././aaa/././.bbb/../c./d.dd/..eeee/..",
103 "../aaa/.bbb/../c./d.dd/..eeee/..");
3ae5990c
ZJS
104
105 assert_se(PATH_IN_SET("/bin", "/", "/bin", "/foo"));
106 assert_se(PATH_IN_SET("/bin", "/bin"));
107 assert_se(PATH_IN_SET("/bin", "/foo/bar", "/bin"));
108 assert_se(PATH_IN_SET("/", "/", "/", "/foo/bar"));
109 assert_se(!PATH_IN_SET("/", "/abc", "/def"));
24737c29
ZJS
110
111 assert_se(path_equal_ptr(NULL, NULL));
112 assert_se(path_equal_ptr("/a", "/a"));
113 assert_se(!path_equal_ptr("/a", "/b"));
114 assert_se(!path_equal_ptr("/a", NULL));
115 assert_se(!path_equal_ptr(NULL, "/a"));
727e63e3
LB
116
117 assert_se(path_equal_filename("/a/c", "/b/c"));
118 assert_se(path_equal_filename("/a", "/a"));
119 assert_se(!path_equal_filename("/a/b", "/a/c"));
120 assert_se(!path_equal_filename("/b", "/c"));
76877b46
ZJS
121}
122
84e72b5e
ZJS
123static void test_path_equal_root(void) {
124 /* Nail down the details of how path_equal("/", ...) works. */
125
771fded3
ZJS
126 log_info("/* %s */", __func__);
127
84e72b5e
ZJS
128 assert_se(path_equal("/", "/"));
129 assert_se(path_equal("/", "//"));
130
131 assert_se(!path_equal("/", "/./"));
132 assert_se(!path_equal("/", "/../"));
133
134 assert_se(!path_equal("/", "/.../"));
135
136 /* Make sure that files_same works as expected. */
137
e3f791a2
ZJS
138 assert_se(files_same("/", "/", 0) > 0);
139 assert_se(files_same("/", "/", AT_SYMLINK_NOFOLLOW) > 0);
140 assert_se(files_same("/", "//", 0) > 0);
141 assert_se(files_same("/", "//", AT_SYMLINK_NOFOLLOW) > 0);
84e72b5e 142
e3f791a2
ZJS
143 assert_se(files_same("/", "/./", 0) > 0);
144 assert_se(files_same("/", "/./", AT_SYMLINK_NOFOLLOW) > 0);
145 assert_se(files_same("/", "/../", 0) > 0);
146 assert_se(files_same("/", "/../", AT_SYMLINK_NOFOLLOW) > 0);
84e72b5e 147
e3f791a2
ZJS
148 assert_se(files_same("/", "/.../", 0) == -ENOENT);
149 assert_se(files_same("/", "/.../", AT_SYMLINK_NOFOLLOW) == -ENOENT);
84e72b5e
ZJS
150
151 /* The same for path_equal_or_files_same. */
152
e3f791a2
ZJS
153 assert_se(path_equal_or_files_same("/", "/", 0));
154 assert_se(path_equal_or_files_same("/", "/", AT_SYMLINK_NOFOLLOW));
155 assert_se(path_equal_or_files_same("/", "//", 0));
156 assert_se(path_equal_or_files_same("/", "//", AT_SYMLINK_NOFOLLOW));
84e72b5e 157
e3f791a2
ZJS
158 assert_se(path_equal_or_files_same("/", "/./", 0));
159 assert_se(path_equal_or_files_same("/", "/./", AT_SYMLINK_NOFOLLOW));
160 assert_se(path_equal_or_files_same("/", "/../", 0));
161 assert_se(path_equal_or_files_same("/", "/../", AT_SYMLINK_NOFOLLOW));
84e72b5e 162
e3f791a2
ZJS
163 assert_se(!path_equal_or_files_same("/", "/.../", 0));
164 assert_se(!path_equal_or_files_same("/", "/.../", AT_SYMLINK_NOFOLLOW));
84e72b5e
ZJS
165}
166
92673045
ZJS
167static void test_find_executable_full(void) {
168 char *p;
169
170 log_info("/* %s */", __func__);
171
5ca9139a 172 assert_se(find_executable_full("sh", true, &p, NULL) == 0);
92673045
ZJS
173 puts(p);
174 assert_se(streq(basename(p), "sh"));
175 free(p);
176
5ca9139a 177 assert_se(find_executable_full("sh", false, &p, NULL) == 0);
92673045
ZJS
178 puts(p);
179 assert_se(streq(basename(p), "sh"));
180 free(p);
181
182 _cleanup_free_ char *oldpath = NULL;
183 p = getenv("PATH");
184 if (p)
185 assert_se(oldpath = strdup(p));
186
44ee03d1 187 assert_se(unsetenv("PATH") == 0);
92673045 188
5ca9139a 189 assert_se(find_executable_full("sh", true, &p, NULL) == 0);
92673045
ZJS
190 puts(p);
191 assert_se(streq(basename(p), "sh"));
192 free(p);
193
5ca9139a 194 assert_se(find_executable_full("sh", false, &p, NULL) == 0);
92673045
ZJS
195 puts(p);
196 assert_se(streq(basename(p), "sh"));
197 free(p);
198
199 if (oldpath)
200 assert_se(setenv("PATH", oldpath, true) >= 0);
201}
202
f7bc0c32 203static void test_find_executable(const char *self) {
c9d954b2
ZJS
204 char *p;
205
771fded3
ZJS
206 log_info("/* %s */", __func__);
207
f7bc0c32 208 assert_se(find_executable("/bin/sh", &p) == 0);
c9d954b2 209 puts(p);
85eca92e 210 assert_se(path_equal(p, "/bin/sh"));
c9d954b2
ZJS
211 free(p);
212
f7bc0c32 213 assert_se(find_executable(self, &p) == 0);
c9d954b2 214 puts(p);
92673045 215 assert_se(endswith(p, "/test-path-util"));
8d95631e 216 assert_se(path_is_absolute(p));
c9d954b2
ZJS
217 free(p);
218
f7bc0c32 219 assert_se(find_executable("sh", &p) == 0);
c9d954b2 220 puts(p);
8d95631e
FB
221 assert_se(endswith(p, "/sh"));
222 assert_se(path_is_absolute(p));
c9d954b2
ZJS
223 free(p);
224
92673045
ZJS
225 assert_se(find_executable("/bin/touch", &p) == 0);
226 assert_se(streq(p, "/bin/touch"));
227 free(p);
228
229 assert_se(find_executable("touch", &p) == 0);
230 assert_se(path_is_absolute(p));
231 assert_se(streq(basename(p), "touch"));
232 free(p);
233
f7bc0c32
ZJS
234 assert_se(find_executable("xxxx-xxxx", &p) == -ENOENT);
235 assert_se(find_executable("/some/dir/xxxx-xxxx", &p) == -ENOENT);
92673045 236 assert_se(find_executable("/proc/filesystems", &p) == -EACCES);
c9d954b2
ZJS
237}
238
5ca9139a
ZJS
239static void test_find_executable_exec_one(const char *path) {
240 _cleanup_free_ char *t = NULL;
241 _cleanup_close_ int fd = -1;
242 pid_t pid;
243 int r;
244
245 r = find_executable_full(path, false, &t, &fd);
246
247 log_info_errno(r, "%s: %s → %s: %d/%m", __func__, path, t ?: "-", fd);
248
249 assert_se(fd > STDERR_FILENO);
250 assert_se(path_is_absolute(t));
251 if (path_is_absolute(path))
252 assert_se(streq(t, path));
253
254 pid = fork();
255 assert_se(pid >= 0);
256 if (pid == 0) {
a6d9111c
ZJS
257 r = fexecve_or_execve(fd, t, STRV_MAKE(t, "--version"), STRV_MAKE(NULL));
258 log_error_errno(r, "[f]execve: %m");
5ca9139a
ZJS
259 _exit(EXIT_FAILURE);
260 }
261
262 assert_se(wait_for_terminate_and_check(t, pid, WAIT_LOG) == 0);
263}
264
265static void test_find_executable_exec(void) {
266 log_info("/* %s */", __func__);
267
268 test_find_executable_exec_one("touch");
269 test_find_executable_exec_one("/bin/touch");
a6d9111c
ZJS
270
271 _cleanup_free_ char *script = NULL;
272 assert_se(get_testdata_dir("test-path-util/script.sh", &script) >= 0);
273 test_find_executable_exec_one(script);
5ca9139a
ZJS
274}
275
fecffe5d 276static void test_prefixes(void) {
b82f71c7
LP
277 static const char* const values[] = {
278 "/a/b/c/d",
279 "/a/b/c",
280 "/a/b",
281 "/a",
282 "",
283 NULL
284 };
e203f7c3 285 unsigned i;
fecffe5d 286 char s[PATH_MAX];
e203f7c3 287 bool b;
fecffe5d 288
771fded3
ZJS
289 log_info("/* %s */", __func__);
290
e203f7c3
LP
291 i = 0;
292 PATH_FOREACH_PREFIX_MORE(s, "/a/b/c/d") {
fecffe5d
LP
293 log_error("---%s---", s);
294 assert_se(streq(s, values[i++]));
295 }
e203f7c3 296 assert_se(values[i] == NULL);
fecffe5d 297
e203f7c3
LP
298 i = 1;
299 PATH_FOREACH_PREFIX(s, "/a/b/c/d") {
300 log_error("---%s---", s);
301 assert_se(streq(s, values[i++]));
302 }
fecffe5d
LP
303 assert_se(values[i] == NULL);
304
305 i = 0;
e203f7c3 306 PATH_FOREACH_PREFIX_MORE(s, "////a////b////c///d///////")
fecffe5d 307 assert_se(streq(s, values[i++]));
e203f7c3 308 assert_se(values[i] == NULL);
fecffe5d 309
e203f7c3
LP
310 i = 1;
311 PATH_FOREACH_PREFIX(s, "////a////b////c///d///////")
312 assert_se(streq(s, values[i++]));
fecffe5d
LP
313 assert_se(values[i] == NULL);
314
315 PATH_FOREACH_PREFIX(s, "////")
e203f7c3
LP
316 assert_not_reached("Wut?");
317
318 b = false;
319 PATH_FOREACH_PREFIX_MORE(s, "////") {
320 assert_se(!b);
fecffe5d 321 assert_se(streq(s, ""));
e203f7c3
LP
322 b = true;
323 }
324 assert_se(b);
fecffe5d
LP
325
326 PATH_FOREACH_PREFIX(s, "")
327 assert_not_reached("wut?");
328
e203f7c3
LP
329 b = false;
330 PATH_FOREACH_PREFIX_MORE(s, "") {
8d95631e
FB
331 assert_se(!b);
332 assert_se(streq(s, ""));
e203f7c3
LP
333 b = true;
334 }
fecffe5d
LP
335}
336
0c6ea3a4 337static void test_path_join(void) {
771fded3 338 log_info("/* %s */", __func__);
59ae3a95 339
62a85ee0 340#define test_join(expected, ...) { \
59ae3a95 341 _cleanup_free_ char *z = NULL; \
62a85ee0
ZJS
342 z = path_join(__VA_ARGS__); \
343 log_debug("got \"%s\", expected \"%s\"", z, expected); \
59ae3a95
TA
344 assert_se(streq(z, expected)); \
345 }
346
62a85ee0
ZJS
347 test_join("/root/a/b/c", "/root", "/a/b", "/c");
348 test_join("/root/a/b/c", "/root", "a/b", "c");
349 test_join("/root/a/b/c", "/root", "/a/b", "c");
350 test_join("/root/c", "/root", "/", "c");
351 test_join("/root/", "/root", "/", NULL);
352
353 test_join("/a/b/c", "", "/a/b", "/c");
354 test_join("a/b/c", "", "a/b", "c");
355 test_join("/a/b/c", "", "/a/b", "c");
356 test_join("/c", "", "/", "c");
357 test_join("/", "", "/", NULL);
358
652ef298
ZJS
359 test_join("/a/b/c", NULL, "/a/b", "/c");
360 test_join("a/b/c", NULL, "a/b", "c");
361 test_join("/a/b/c", NULL, "/a/b", "c");
362 test_join("/c", NULL, "/", "c");
363 test_join("/", NULL, "/", NULL);
364
62a85ee0 365 test_join("", "", NULL);
652ef298
ZJS
366 test_join("", NULL, "");
367 test_join("", NULL, NULL);
62a85ee0
ZJS
368
369 test_join("foo/bar", "foo", "bar");
370 test_join("foo/bar", "", "foo", "bar");
652ef298 371 test_join("foo/bar", NULL, "foo", NULL, "bar");
62a85ee0
ZJS
372 test_join("foo/bar", "", "foo", "", "bar", "");
373 test_join("foo/bar", "", "", "", "", "foo", "", "", "", "bar", "", "", "");
374
375 test_join("//foo///bar//", "", "/", "", "/foo/", "", "/", "", "/bar/", "", "/", "");
376 test_join("/foo/bar/", "/", "foo", "/", "bar", "/");
377 test_join("foo/bar/baz", "foo", "bar", "baz");
378 test_join("foo/bar/baz", "foo/", "bar", "/baz");
379 test_join("foo//bar//baz", "foo/", "/bar/", "/baz");
380 test_join("//foo////bar////baz//", "//foo/", "///bar/", "///baz//");
0c6ea3a4
ZJS
381}
382
7ae27680
LP
383static void test_path_extend(void) {
384 _cleanup_free_ char *p = NULL;
385
386 log_info("/* %s */", __func__);
387
388 assert_se(path_extend(&p, "foo", "bar", "baz") == p);
389 assert_se(streq(p, "foo/bar/baz"));
390
391 assert_se(path_extend(&p, "foo", "bar", "baz") == p);
392 assert_se(streq(p, "foo/bar/baz/foo/bar/baz"));
393
394 p = mfree(p);
395 assert_se(path_extend(&p, "foo") == p);
396 assert_se(streq(p, "foo"));
397
398 assert_se(path_extend(&p, "/foo") == p);
399 assert_se(streq(p, "foo/foo"));
340cd6b6
YW
400 assert_se(path_extend(&p, "/waaaah/wahhh//") == p);
401 assert_se(streq(p, "foo/foo/waaaah/wahhh//")); /* path_extend() does not drop redundant slashes */
402 assert_se(path_extend(&p, "/aaa/bbb/") == p);
403 assert_se(streq(p, "foo/foo/waaaah/wahhh///aaa/bbb/")); /* but not add an extra slash */
404
405 assert_se(free_and_strdup(&p, "/") >= 0);
406 assert_se(path_extend(&p, "foo") == p);
407 assert_se(streq(p, "/foo"));
7ae27680
LP
408}
409
eb66db55 410static void test_fsck_exists(void) {
771fded3
ZJS
411 log_info("/* %s */", __func__);
412
eb66db55 413 /* Ensure we use a sane default for PATH. */
44ee03d1 414 assert_se(unsetenv("PATH") == 0);
eb66db55
MG
415
416 /* fsck.minix is provided by util-linux and will probably exist. */
85eca92e 417 assert_se(fsck_exists("minix") == 1);
eb66db55 418
85eca92e
LP
419 assert_se(fsck_exists("AbCdE") == 0);
420 assert_se(fsck_exists("/../bin/") == 0);
eb66db55
MG
421}
422
6b56a651
TK
423static void test_make_relative(void) {
424 char *result;
425
771fded3
ZJS
426 log_info("/* %s */", __func__);
427
6b56a651
TK
428 assert_se(path_make_relative("some/relative/path", "/some/path", &result) < 0);
429 assert_se(path_make_relative("/some/path", "some/relative/path", &result) < 0);
2a5beb66 430 assert_se(path_make_relative("/some/dotdot/../path", "/some/path", &result) < 0);
6b56a651 431
59ae3a95
TA
432#define test(from_dir, to_path, expected) { \
433 _cleanup_free_ char *z = NULL; \
434 path_make_relative(from_dir, to_path, &z); \
435 assert_se(streq(z, expected)); \
6b56a651
TK
436 }
437
438 test("/", "/", ".");
439 test("/", "/some/path", "some/path");
440 test("/some/path", "/some/path", ".");
441 test("/some/path", "/some/path/in/subdir", "in/subdir");
442 test("/some/path", "/", "../..");
443 test("/some/path", "/some/other/path", "../other/path");
2a5beb66 444 test("/some/path/./dot", "/some/further/path", "../../further/path");
48054262 445 test("//extra.//.//./.slashes//./won't////fo.ol///anybody//", "/././/extra././/.slashes////ar.e/.just/././.fine///", "../../../ar.e/.just/.fine");
6b56a651
TK
446}
447
3e8a78c8
MM
448static void test_strv_resolve(void) {
449 char tmp_dir[] = "/tmp/test-path-util-XXXXXX";
450 _cleanup_strv_free_ char **search_dirs = NULL;
451 _cleanup_strv_free_ char **absolute_dirs = NULL;
452 char **d;
453
454 assert_se(mkdtemp(tmp_dir) != NULL);
455
bea1a013 456 search_dirs = strv_new("/dir1", "/dir2", "/dir3");
3e8a78c8
MM
457 assert_se(search_dirs);
458 STRV_FOREACH(d, search_dirs) {
b910cc72 459 char *p = path_join(tmp_dir, *d);
3e8a78c8
MM
460 assert_se(p);
461 assert_se(strv_push(&absolute_dirs, p) == 0);
462 }
463
464 assert_se(mkdir(absolute_dirs[0], 0700) == 0);
465 assert_se(mkdir(absolute_dirs[1], 0700) == 0);
466 assert_se(symlink("dir2", absolute_dirs[2]) == 0);
467
468 path_strv_resolve(search_dirs, tmp_dir);
469 assert_se(streq(search_dirs[0], "/dir1"));
470 assert_se(streq(search_dirs[1], "/dir2"));
471 assert_se(streq(search_dirs[2], "/dir2"));
472
c6878637 473 assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
3e8a78c8
MM
474}
475
5895b62f 476static void test_path_startswith(void) {
0470289b
ZJS
477 const char *p;
478
771fded3
ZJS
479 log_info("/* %s */", __func__);
480
0470289b
ZJS
481 p = path_startswith("/foo/bar/barfoo/", "/foo");
482 assert_se(streq_ptr(p, "bar/barfoo/"));
483
484 p = path_startswith("/foo/bar/barfoo/", "/foo/");
485 assert_se(streq_ptr(p, "bar/barfoo/"));
486
487 p = path_startswith("/foo/bar/barfoo/", "/");
488 assert_se(streq_ptr(p, "foo/bar/barfoo/"));
489
490 p = path_startswith("/foo/bar/barfoo/", "////");
491 assert_se(streq_ptr(p, "foo/bar/barfoo/"));
492
493 p = path_startswith("/foo/bar/barfoo/", "/foo//bar/////barfoo///");
494 assert_se(streq_ptr(p, ""));
495
496 p = path_startswith("/foo/bar/barfoo/", "/foo/bar/barfoo////");
497 assert_se(streq_ptr(p, ""));
498
499 p = path_startswith("/foo/bar/barfoo/", "/foo/bar///barfoo/");
500 assert_se(streq_ptr(p, ""));
501
502 p = path_startswith("/foo/bar/barfoo/", "/foo////bar/barfoo/");
503 assert_se(streq_ptr(p, ""));
504
505 p = path_startswith("/foo/bar/barfoo/", "////foo/bar/barfoo/");
506 assert_se(streq_ptr(p, ""));
507
508 p = path_startswith("/foo/bar/barfoo/", "/foo/bar/barfoo");
509 assert_se(streq_ptr(p, ""));
5895b62f
RC
510
511 assert_se(!path_startswith("/foo/bar/barfoo/", "/foo/bar/barfooa/"));
512 assert_se(!path_startswith("/foo/bar/barfoo/", "/foo/bar/barfooa"));
513 assert_se(!path_startswith("/foo/bar/barfoo/", ""));
514 assert_se(!path_startswith("/foo/bar/barfoo/", "/bar/foo"));
515 assert_se(!path_startswith("/foo/bar/barfoo/", "/f/b/b/"));
1dff135b
YW
516 assert_se(!path_startswith("/foo/bar/barfoo/", "/foo/bar/barfo"));
517 assert_se(!path_startswith("/foo/bar/barfoo/", "/foo/bar/bar"));
518 assert_se(!path_startswith("/foo/bar/barfoo/", "/fo"));
5895b62f
RC
519}
520
1d13f648
LP
521static void test_prefix_root_one(const char *r, const char *p, const char *expected) {
522 _cleanup_free_ char *s = NULL;
523 const char *t;
524
c6134d3e
LP
525 assert_se(s = path_join(r, p));
526 assert_se(path_equal_ptr(s, expected));
1d13f648
LP
527
528 t = prefix_roota(r, p);
529 assert_se(t);
c6134d3e 530 assert_se(path_equal_ptr(t, expected));
1d13f648
LP
531}
532
533static void test_prefix_root(void) {
771fded3
ZJS
534 log_info("/* %s */", __func__);
535
1d13f648
LP
536 test_prefix_root_one("/", "/foo", "/foo");
537 test_prefix_root_one(NULL, "/foo", "/foo");
538 test_prefix_root_one("", "/foo", "/foo");
539 test_prefix_root_one("///", "/foo", "/foo");
540 test_prefix_root_one("/", "////foo", "/foo");
541 test_prefix_root_one(NULL, "////foo", "/foo");
f9421dd8
YW
542 test_prefix_root_one("/", "foo", "/foo");
543 test_prefix_root_one("", "foo", "foo");
544 test_prefix_root_one(NULL, "foo", "foo");
1d13f648
LP
545
546 test_prefix_root_one("/foo", "/bar", "/foo/bar");
547 test_prefix_root_one("/foo", "bar", "/foo/bar");
548 test_prefix_root_one("foo", "bar", "foo/bar");
549 test_prefix_root_one("/foo/", "/bar", "/foo/bar");
550 test_prefix_root_one("/foo/", "//bar", "/foo/bar");
551 test_prefix_root_one("/foo///", "//bar", "/foo/bar");
552}
553
63292663
RC
554static void test_file_in_same_dir(void) {
555 char *t;
556
771fded3
ZJS
557 log_info("/* %s */", __func__);
558
63292663
RC
559 t = file_in_same_dir("/", "a");
560 assert_se(streq(t, "/a"));
561 free(t);
562
563 t = file_in_same_dir("/", "/a");
564 assert_se(streq(t, "/a"));
565 free(t);
566
567 t = file_in_same_dir("", "a");
568 assert_se(streq(t, "a"));
569 free(t);
570
571 t = file_in_same_dir("a/", "a");
572 assert_se(streq(t, "a/a"));
573 free(t);
574
575 t = file_in_same_dir("bar/foo", "bar");
576 assert_se(streq(t, "bar/bar"));
577 free(t);
578}
579
0ee54dd4
YW
580static void test_path_find_first_component_one(
581 const char *path,
582 bool accept_dot_dot,
583 char **expected,
584 int ret) {
585
586 log_debug("/* %s(\"%s\", accept_dot_dot=%s) */", __func__, strnull(path), yes_no(accept_dot_dot));
587
588 for (const char *p = path;;) {
589 const char *e;
590 int r;
591
592 r = path_find_first_component(&p, accept_dot_dot, &e);
593 if (r <= 0) {
594 if (r == 0) {
595 if (path)
596 assert_se(p == path + strlen_ptr(path));
597 else
598 assert_se(!p);
599 assert_se(!e);
600 }
601 assert_se(r == ret);
602 assert_se(strv_isempty(expected));
603 return;
604 }
605
606 assert_se(e);
607 assert_se(strcspn(e, "/") == (size_t) r);
608 assert_se(strlen_ptr(*expected) == (size_t) r);
609 assert_se(strneq(e, *expected++, r));
610 }
611}
612
613static void test_path_find_first_component(void) {
614 _cleanup_free_ char *hoge = NULL;
615 char foo[NAME_MAX * 2];
616
617 log_info("/* %s */", __func__);
618
619 test_path_find_first_component_one(NULL, false, NULL, 0);
620 test_path_find_first_component_one("", false, NULL, 0);
621 test_path_find_first_component_one("/", false, NULL, 0);
622 test_path_find_first_component_one(".", false, NULL, 0);
623 test_path_find_first_component_one("./", false, NULL, 0);
624 test_path_find_first_component_one("./.", false, NULL, 0);
625 test_path_find_first_component_one("..", false, NULL, -EINVAL);
626 test_path_find_first_component_one("/..", false, NULL, -EINVAL);
627 test_path_find_first_component_one("./..", false, NULL, -EINVAL);
628 test_path_find_first_component_one("////./././//.", false, NULL, 0);
629 test_path_find_first_component_one("a/b/c", false, STRV_MAKE("a", "b", "c"), 0);
630 test_path_find_first_component_one("././//.///aa/bbb//./ccc", false, STRV_MAKE("aa", "bbb", "ccc"), 0);
631 test_path_find_first_component_one("././//.///aa/.../../bbb//./ccc/.", false, STRV_MAKE("aa", "..."), -EINVAL);
632 test_path_find_first_component_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.", false, STRV_MAKE("aaa", ".bbb"), -EINVAL);
633 test_path_find_first_component_one("a/foo./b", false, STRV_MAKE("a", "foo.", "b"), 0);
634
635 test_path_find_first_component_one(NULL, true, NULL, 0);
636 test_path_find_first_component_one("", true, NULL, 0);
637 test_path_find_first_component_one("/", true, NULL, 0);
638 test_path_find_first_component_one(".", true, NULL, 0);
639 test_path_find_first_component_one("./", true, NULL, 0);
640 test_path_find_first_component_one("./.", true, NULL, 0);
641 test_path_find_first_component_one("..", true, STRV_MAKE(".."), 0);
642 test_path_find_first_component_one("/..", true, STRV_MAKE(".."), 0);
643 test_path_find_first_component_one("./..", true, STRV_MAKE(".."), 0);
644 test_path_find_first_component_one("////./././//.", true, NULL, 0);
645 test_path_find_first_component_one("a/b/c", true, STRV_MAKE("a", "b", "c"), 0);
646 test_path_find_first_component_one("././//.///aa/bbb//./ccc", true, STRV_MAKE("aa", "bbb", "ccc"), 0);
647 test_path_find_first_component_one("././//.///aa/.../../bbb//./ccc/.", true, STRV_MAKE("aa", "...", "..", "bbb", "ccc"), 0);
648 test_path_find_first_component_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.", true, STRV_MAKE("aaa", ".bbb", "..", "c.", "d.dd", "..eeee"), 0);
649 test_path_find_first_component_one("a/foo./b", true, STRV_MAKE("a", "foo.", "b"), 0);
650
651 memset(foo, 'a', sizeof(foo) -1);
652 char_array_0(foo);
653
654 test_path_find_first_component_one(foo, false, NULL, -EINVAL);
655 test_path_find_first_component_one(foo, true, NULL, -EINVAL);
656
657 hoge = strjoin("a/b/c/", foo, "//d/e/.//f/");
658 assert_se(hoge);
659
660 test_path_find_first_component_one(hoge, false, STRV_MAKE("a", "b", "c"), -EINVAL);
661 test_path_find_first_component_one(hoge, true, STRV_MAKE("a", "b", "c"), -EINVAL);
662}
663
b12d25a8 664static void test_last_path_component(void) {
77e0a1b5 665 assert_se(last_path_component(NULL) == NULL);
b12d25a8
ZJS
666 assert_se(streq(last_path_component("a/b/c"), "c"));
667 assert_se(streq(last_path_component("a/b/c/"), "c/"));
668 assert_se(streq(last_path_component("/"), "/"));
669 assert_se(streq(last_path_component("//"), "/"));
670 assert_se(streq(last_path_component("///"), "/"));
671 assert_se(streq(last_path_component("."), "."));
672 assert_se(streq(last_path_component("./."), "."));
673 assert_se(streq(last_path_component("././"), "./"));
674 assert_se(streq(last_path_component("././/"), ".//"));
675 assert_se(streq(last_path_component("/foo/a"), "a"));
676 assert_se(streq(last_path_component("/foo/a/"), "a/"));
69f9ccf1 677 assert_se(streq(last_path_component(""), ""));
8460289f
LP
678 assert_se(streq(last_path_component("a"), "a"));
679 assert_se(streq(last_path_component("a/"), "a/"));
680 assert_se(streq(last_path_component("/a"), "a"));
681 assert_se(streq(last_path_component("/a/"), "a/"));
b12d25a8
ZJS
682}
683
a60c8eee
LP
684static void test_path_extract_filename_one(const char *input, const char *output, int ret) {
685 _cleanup_free_ char *k = NULL;
686 int r;
687
688 r = path_extract_filename(input, &k);
ee277c6b
LP
689 log_info_errno(r, "%s → %s/%m [expected: %s/%s]",
690 strnull(input),
691 strnull(k), /* strerror(r) is printed via %m, to avoid that the two strerror()'s overwrite each other's buffers */
692 strnull(output), ret < 0 ? strerror_safe(ret) : "-");
a60c8eee
LP
693 assert_se(streq_ptr(k, output));
694 assert_se(r == ret);
695}
696
697static void test_path_extract_filename(void) {
771fded3
ZJS
698 log_info("/* %s */", __func__);
699
a60c8eee
LP
700 test_path_extract_filename_one(NULL, NULL, -EINVAL);
701 test_path_extract_filename_one("a/b/c", "c", 0);
ee277c6b 702 test_path_extract_filename_one("a/b/c/", "c", O_DIRECTORY);
3cdcbdd3
LP
703 test_path_extract_filename_one("/", NULL, -EADDRNOTAVAIL);
704 test_path_extract_filename_one("//", NULL, -EADDRNOTAVAIL);
705 test_path_extract_filename_one("///", NULL, -EADDRNOTAVAIL);
a60c8eee
LP
706 test_path_extract_filename_one(".", NULL, -EINVAL);
707 test_path_extract_filename_one("./.", NULL, -EINVAL);
708 test_path_extract_filename_one("././", NULL, -EINVAL);
709 test_path_extract_filename_one("././/", NULL, -EINVAL);
710 test_path_extract_filename_one("/foo/a", "a", 0);
ee277c6b 711 test_path_extract_filename_one("/foo/a/", "a", O_DIRECTORY);
a60c8eee
LP
712 test_path_extract_filename_one("", NULL, -EINVAL);
713 test_path_extract_filename_one("a", "a", 0);
ee277c6b 714 test_path_extract_filename_one("a/", "a", O_DIRECTORY);
a60c8eee 715 test_path_extract_filename_one("/a", "a", 0);
ee277c6b
LP
716 test_path_extract_filename_one("/a/", "a", O_DIRECTORY);
717 test_path_extract_filename_one("/////////////a/////////////", "a", O_DIRECTORY);
a60c8eee
LP
718 test_path_extract_filename_one("xx/.", NULL, -EINVAL);
719 test_path_extract_filename_one("xx/..", NULL, -EINVAL);
720 test_path_extract_filename_one("..", NULL, -EINVAL);
721 test_path_extract_filename_one("/..", NULL, -EINVAL);
722 test_path_extract_filename_one("../", NULL, -EINVAL);
723 test_path_extract_filename_one(".", NULL, -EINVAL);
724 test_path_extract_filename_one("/.", NULL, -EINVAL);
725 test_path_extract_filename_one("./", NULL, -EINVAL);
726}
727
8dcb891c
LP
728static void test_path_extract_directory_one(const char *input, const char *output, int ret) {
729 _cleanup_free_ char *k = NULL;
730 int r;
731
732 r = path_extract_directory(input, &k);
733 log_info_errno(r, "%s → %s/%m [expected: %s/%s]",
734 strnull(input),
735 strnull(k), /* we output strerror_safe(r) via %m here, since otherwise the error buffer might be overwritten twice */
736 strnull(output), strerror_safe(ret));
737 assert_se(streq_ptr(k, output));
738 assert_se(r == ret);
739
740 /* Extra safety check: let's make sure that if we split out the filename too (and it works) the
741 * joined parts are identical to the original again */
742 if (r >= 0) {
743 _cleanup_free_ char *f = NULL;
744
745 r = path_extract_filename(input, &f);
746 if (r >= 0) {
747 _cleanup_free_ char *j = NULL;
748
749 assert_se(j = path_join(k, f));
750 assert_se(path_equal(input, j));
751 }
752 }
753}
754
755static void test_path_extract_directory(void) {
756 log_info("/* %s */", __func__);
757
758 test_path_extract_directory_one(NULL, NULL, -EINVAL);
759 test_path_extract_directory_one("a/b/c", "a/b", 0);
760 test_path_extract_directory_one("a/b/c/", "a/b", 0);
761 test_path_extract_directory_one("/", NULL, -EADDRNOTAVAIL);
762 test_path_extract_directory_one("//", NULL, -EADDRNOTAVAIL);
763 test_path_extract_directory_one("///", NULL, -EADDRNOTAVAIL);
764 test_path_extract_directory_one(".", NULL, -EDESTADDRREQ);
765 test_path_extract_directory_one("./.", ".", 0);
766 test_path_extract_directory_one("././", ".", 0);
767 test_path_extract_directory_one("././/", ".", 0);
768 test_path_extract_directory_one("/foo/a", "/foo", 0);
769 test_path_extract_directory_one("/foo/a/", "/foo", 0);
770 test_path_extract_directory_one("", NULL, -EINVAL);
771 test_path_extract_directory_one("a", NULL, -EDESTADDRREQ);
772 test_path_extract_directory_one("a/", NULL, -EDESTADDRREQ);
773 test_path_extract_directory_one("/a", "/", 0);
774 test_path_extract_directory_one("/a/", "/", 0);
775 test_path_extract_directory_one("/////////////a/////////////", "/", 0);
776 test_path_extract_directory_one("xx/.", "xx", 0);
777 test_path_extract_directory_one("xx/..", "xx", 0);
778 test_path_extract_directory_one("..", NULL, -EDESTADDRREQ);
779 test_path_extract_directory_one("/..", "/", 0);
780 test_path_extract_directory_one("../", NULL, -EDESTADDRREQ);
781 test_path_extract_directory_one(".", NULL, -EDESTADDRREQ);
782 test_path_extract_directory_one("/.", "/", 0);
783 test_path_extract_directory_one("./", NULL, -EDESTADDRREQ);
784}
785
63292663 786static void test_filename_is_valid(void) {
2ef2376d 787 char foo[NAME_MAX+2];
63292663 788
771fded3
ZJS
789 log_info("/* %s */", __func__);
790
63292663
RC
791 assert_se(!filename_is_valid(""));
792 assert_se(!filename_is_valid("/bar/foo"));
793 assert_se(!filename_is_valid("/"));
794 assert_se(!filename_is_valid("."));
795 assert_se(!filename_is_valid(".."));
1c322571
ZJS
796 assert_se(!filename_is_valid("bar/foo"));
797 assert_se(!filename_is_valid("bar/foo/"));
798 assert_se(!filename_is_valid("bar//"));
63292663 799
2ef2376d
LP
800 memset(foo, 'a', sizeof(foo) - 1);
801 char_array_0(foo);
63292663
RC
802
803 assert_se(!filename_is_valid(foo));
804
805 assert_se(filename_is_valid("foo_bar-333"));
806 assert_se(filename_is_valid("o.o"));
807}
808
32df2e14
YW
809static void test_path_is_valid_and_safe_one(const char *p, bool ret) {
810 log_debug("/* %s(\"%s\")*/", __func__, strnull(p));
811
812 assert_se(path_is_valid(p) == ret);
813 if (ret)
814 ret = !streq(p, "..") &&
815 !startswith(p, "../") &&
816 !endswith(p, "/..") &&
817 !strstr(p, "/../");
818 assert_se(path_is_safe(p) == ret);
819}
820
821static void test_path_is_valid_and_safe(void) {
2ef2376d
LP
822 char foo[PATH_MAX+2];
823 const char *c;
824
825 log_info("/* %s */", __func__);
826
32df2e14
YW
827 test_path_is_valid_and_safe_one("", false);
828 test_path_is_valid_and_safe_one("/bar/foo", true);
829 test_path_is_valid_and_safe_one("/bar/foo/", true);
830 test_path_is_valid_and_safe_one("/bar/foo/", true);
831 test_path_is_valid_and_safe_one("//bar//foo//", true);
832 test_path_is_valid_and_safe_one("/", true);
833 test_path_is_valid_and_safe_one("/////", true);
834 test_path_is_valid_and_safe_one("/////.///.////...///..//.", true);
835 test_path_is_valid_and_safe_one(".", true);
836 test_path_is_valid_and_safe_one("..", true);
837 test_path_is_valid_and_safe_one("bar/foo", true);
838 test_path_is_valid_and_safe_one("bar/foo/", true);
839 test_path_is_valid_and_safe_one("bar//", true);
2ef2376d
LP
840
841 memset(foo, 'a', sizeof(foo) -1);
842 char_array_0(foo);
843
32df2e14 844 test_path_is_valid_and_safe_one(foo, false);
2ef2376d
LP
845
846 c = strjoina("/xxx/", foo, "/yyy");
32df2e14 847 test_path_is_valid_and_safe_one(c, false);
2ef2376d 848
32df2e14
YW
849 test_path_is_valid_and_safe_one("foo_bar-333", true);
850 test_path_is_valid_and_safe_one("o.o", true);
2ef2376d
LP
851}
852
b05b9cde 853static void test_hidden_or_backup_file(void) {
771fded3
ZJS
854 log_info("/* %s */", __func__);
855
b05b9cde
ZJS
856 assert_se(hidden_or_backup_file(".hidden"));
857 assert_se(hidden_or_backup_file("..hidden"));
858 assert_se(!hidden_or_backup_file("hidden."));
859
860 assert_se(hidden_or_backup_file("backup~"));
861 assert_se(hidden_or_backup_file(".backup~"));
862
863 assert_se(hidden_or_backup_file("lost+found"));
864 assert_se(hidden_or_backup_file("aquota.user"));
865 assert_se(hidden_or_backup_file("aquota.group"));
866
867 assert_se(hidden_or_backup_file("test.rpmnew"));
868 assert_se(hidden_or_backup_file("test.dpkg-old"));
869 assert_se(hidden_or_backup_file("test.dpkg-remove"));
870 assert_se(hidden_or_backup_file("test.swp"));
871
872 assert_se(!hidden_or_backup_file("test.rpmnew."));
873 assert_se(!hidden_or_backup_file("test.dpkg-old.foo"));
874}
875
5a46d55f
ZJS
876static void test_systemd_installation_has_version(const char *path) {
877 int r;
a67c318d 878 const unsigned versions[] = {0, 231, PROJECT_VERSION, 999};
5a46d55f
ZJS
879 unsigned i;
880
771fded3
ZJS
881 log_info("/* %s */", __func__);
882
5a46d55f
ZJS
883 for (i = 0; i < ELEMENTSOF(versions); i++) {
884 r = systemd_installation_has_version(path, versions[i]);
885 assert_se(r >= 0);
886 log_info("%s has systemd >= %u: %s",
887 path ?: "Current installation", versions[i], yes_no(r));
888 }
889}
890
a119ec7c 891static void test_skip_dev_prefix(void) {
771fded3 892 log_info("/* %s */", __func__);
a119ec7c
LP
893
894 assert_se(streq(skip_dev_prefix("/"), "/"));
895 assert_se(streq(skip_dev_prefix("/dev"), ""));
896 assert_se(streq(skip_dev_prefix("/dev/"), ""));
897 assert_se(streq(skip_dev_prefix("/dev/foo"), "foo"));
898 assert_se(streq(skip_dev_prefix("/dev/foo/bar"), "foo/bar"));
899 assert_se(streq(skip_dev_prefix("//dev"), ""));
900 assert_se(streq(skip_dev_prefix("//dev//"), ""));
901 assert_se(streq(skip_dev_prefix("/dev///foo"), "foo"));
902 assert_se(streq(skip_dev_prefix("///dev///foo///bar"), "foo///bar"));
903 assert_se(streq(skip_dev_prefix("//foo"), "//foo"));
904 assert_se(streq(skip_dev_prefix("foo"), "foo"));
905}
906
57ea45e1 907static void test_empty_or_root(void) {
771fded3
ZJS
908 log_info("/* %s */", __func__);
909
57ea45e1
LP
910 assert_se(empty_or_root(NULL));
911 assert_se(empty_or_root(""));
912 assert_se(empty_or_root("/"));
913 assert_se(empty_or_root("//"));
914 assert_se(empty_or_root("///"));
915 assert_se(empty_or_root("/////////////////"));
916 assert_se(!empty_or_root("xxx"));
917 assert_se(!empty_or_root("/xxx"));
918 assert_se(!empty_or_root("/xxx/"));
919 assert_se(!empty_or_root("//yy//"));
920}
921
d898ed65 922static void test_path_startswith_set(void) {
771fded3 923 log_info("/* %s */", __func__);
d898ed65
LP
924
925 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/foo/bar", "/zzz"), ""));
926 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/foo/", "/zzz"), "bar"));
927 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/foo", "/zzz"), "bar"));
928 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/", "/zzz"), "foo/bar"));
929 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "", "/zzz"), NULL));
930
931 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/foo/bar", "/zzz"), NULL));
932 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/foo/", "/zzz"), "bar2"));
933 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/foo", "/zzz"), "bar2"));
934 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/", "/zzz"), "foo/bar2"));
935 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "", "/zzz"), NULL));
936
937 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/foo/bar", "/zzz"), NULL));
938 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/foo/", "/zzz"), NULL));
939 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/foo", "/zzz"), NULL));
940 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/", "/zzz"), "foo2/bar"));
941 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "", "/zzz"), NULL));
942}
943
cc4d7d81
ZJS
944static void test_path_startswith_strv(void) {
945 log_info("/* %s */", __func__);
946
947 assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/foo/bar", "/zzz")), ""));
948 assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/foo/", "/zzz")), "bar"));
949 assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/foo", "/zzz")), "bar"));
950 assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/", "/zzz")), "foo/bar"));
951 assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "", "/zzz")), NULL));
952
953 assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "/foo/bar", "/zzz")), NULL));
954 assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "/foo/", "/zzz")), "bar2"));
955 assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "/foo", "/zzz")), "bar2"));
956 assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "/", "/zzz")), "foo/bar2"));
957 assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "", "/zzz")), NULL));
958
959 assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "/foo/bar", "/zzz")), NULL));
960 assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "/foo/", "/zzz")), NULL));
961 assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "/foo", "/zzz")), NULL));
962 assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "/", "/zzz")), "foo2/bar"));
963 assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "", "/zzz")), NULL));
964}
965
7f076504 966int main(int argc, char **argv) {
6d7c4033 967 test_setup_logging(LOG_DEBUG);
5a46d55f 968
69866062
LP
969 log_info("PATH_MAX=%zu\n"
970 "FILENAME_MAX=%zu\n"
971 "NAME_MAX=%zu",
972 (size_t) PATH_MAX,
973 (size_t) FILENAME_MAX,
974 (size_t) NAME_MAX);
975
976 assert_cc(FILENAME_MAX == PATH_MAX);
977
3602ca6f 978 test_print_paths();
76877b46 979 test_path();
84e72b5e 980 test_path_equal_root();
92673045 981 test_find_executable_full();
f7bc0c32 982 test_find_executable(argv[0]);
5ca9139a 983 test_find_executable_exec();
fecffe5d 984 test_prefixes();
0c6ea3a4 985 test_path_join();
7ae27680 986 test_path_extend();
eb66db55 987 test_fsck_exists();
6b56a651 988 test_make_relative();
3e8a78c8 989 test_strv_resolve();
5895b62f 990 test_path_startswith();
1d13f648 991 test_prefix_root();
63292663 992 test_file_in_same_dir();
0ee54dd4 993 test_path_find_first_component();
b12d25a8 994 test_last_path_component();
a60c8eee 995 test_path_extract_filename();
8dcb891c 996 test_path_extract_directory();
63292663 997 test_filename_is_valid();
32df2e14 998 test_path_is_valid_and_safe();
b05b9cde 999 test_hidden_or_backup_file();
a119ec7c 1000 test_skip_dev_prefix();
57ea45e1 1001 test_empty_or_root();
d898ed65 1002 test_path_startswith_set();
cc4d7d81 1003 test_path_startswith_strv();
5895b62f 1004
5a46d55f
ZJS
1005 test_systemd_installation_has_version(argv[1]); /* NULL is OK */
1006
76877b46
ZJS
1007 return 0;
1008}