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