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