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