]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-path-util.c
sysctl: split out code for applying glob option
[thirdparty/systemd.git] / src / test / test-path-util.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
76877b46 2
a696dbef 3#include <stdio.h>
07630cea 4#include <unistd.h>
a696dbef 5
b5efdb8a 6#include "alloc-util.h"
a6d9111c 7#include "exec-util.h"
3ffd4af2 8#include "fd-util.h"
8c35c10d 9#include "fs-util.h"
76877b46 10#include "macro.h"
07630cea 11#include "path-util.h"
5ca9139a 12#include "process-util.h"
c6878637 13#include "rm-rf.h"
84e72b5e 14#include "stat-util.h"
07630cea
LP
15#include "string-util.h"
16#include "strv.h"
6d7c4033 17#include "tests.h"
8c35c10d 18#include "tmpfile-util.h"
07630cea 19#include "util.h"
76877b46 20
4f7452a8 21TEST(print_paths) {
3602ca6f
ZJS
22 log_info("DEFAULT_PATH=%s", DEFAULT_PATH);
23 log_info("DEFAULT_USER_PATH=%s", DEFAULT_USER_PATH);
24}
25
4f7452a8 26TEST(path) {
76877b46
ZJS
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
2b6bf07d
ZJS
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.../"), ""));
76877b46 38
3ae5990c
ZJS
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"));
24737c29
ZJS
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"));
727e63e3
LB
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"));
76877b46
ZJS
55}
56
cb71ed91
YW
57static void test_path_simplify_one(const char *in, const char *out) {
58 char *p;
59
2f82562b 60 p = strdupa_safe(in);
cb71ed91
YW
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
4f7452a8 66TEST(path_simplify) {
cb71ed91
YW
67 _cleanup_free_ char *hoge = NULL, *hoge_out = NULL;
68 char foo[NAME_MAX * 2];
69
cb71ed91
YW
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
353df443
YW
109static 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
4f7452a8 126TEST(path_compare) {
353df443
YW
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);
6d216bdd
ZJS
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);
353df443
YW
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
4f7452a8 154TEST(path_equal_root) {
84e72b5e
ZJS
155 /* Nail down the details of how path_equal("/", ...) works. */
156
157 assert_se(path_equal("/", "/"));
158 assert_se(path_equal("/", "//"));
159
353df443 160 assert_se(path_equal("/", "/./"));
84e72b5e
ZJS
161 assert_se(!path_equal("/", "/../"));
162
163 assert_se(!path_equal("/", "/.../"));
164
165 /* Make sure that files_same works as expected. */
166
e3f791a2
ZJS
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);
84e72b5e 171
e3f791a2
ZJS
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);
84e72b5e 176
e3f791a2
ZJS
177 assert_se(files_same("/", "/.../", 0) == -ENOENT);
178 assert_se(files_same("/", "/.../", AT_SYMLINK_NOFOLLOW) == -ENOENT);
84e72b5e
ZJS
179
180 /* The same for path_equal_or_files_same. */
181
e3f791a2
ZJS
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));
84e72b5e 186
e3f791a2
ZJS
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));
84e72b5e 191
e3f791a2
ZJS
192 assert_se(!path_equal_or_files_same("/", "/.../", 0));
193 assert_se(!path_equal_or_files_same("/", "/.../", AT_SYMLINK_NOFOLLOW));
84e72b5e
ZJS
194}
195
4f7452a8 196TEST(find_executable_full) {
92673045 197 char *p;
8c35c10d 198 char* test_file_name;
199 _cleanup_close_ int fd = -1;
200 char fn[] = "/tmp/test-XXXXXX";
92673045 201
8c35c10d 202 assert_se(find_executable_full("sh", NULL, NULL, true, &p, NULL) == 0);
92673045
ZJS
203 puts(p);
204 assert_se(streq(basename(p), "sh"));
205 free(p);
206
8c35c10d 207 assert_se(find_executable_full("sh", NULL, NULL, false, &p, NULL) == 0);
92673045
ZJS
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
44ee03d1 217 assert_se(unsetenv("PATH") == 0);
92673045 218
8c35c10d 219 assert_se(find_executable_full("sh", NULL, NULL, true, &p, NULL) == 0);
92673045
ZJS
220 puts(p);
221 assert_se(streq(basename(p), "sh"));
222 free(p);
223
8c35c10d 224 assert_se(find_executable_full("sh", NULL, NULL, false, &p, NULL) == 0);
92673045
ZJS
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);
8c35c10d 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);
92673045
ZJS
244}
245
4f7452a8 246TEST(find_executable) {
c9d954b2
ZJS
247 char *p;
248
f7bc0c32 249 assert_se(find_executable("/bin/sh", &p) == 0);
c9d954b2 250 puts(p);
85eca92e 251 assert_se(path_equal(p, "/bin/sh"));
c9d954b2
ZJS
252 free(p);
253
4f7452a8 254 assert_se(find_executable(saved_argv[0], &p) == 0);
c9d954b2 255 puts(p);
92673045 256 assert_se(endswith(p, "/test-path-util"));
8d95631e 257 assert_se(path_is_absolute(p));
c9d954b2
ZJS
258 free(p);
259
f7bc0c32 260 assert_se(find_executable("sh", &p) == 0);
c9d954b2 261 puts(p);
8d95631e
FB
262 assert_se(endswith(p, "/sh"));
263 assert_se(path_is_absolute(p));
c9d954b2
ZJS
264 free(p);
265
92673045
ZJS
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
f7bc0c32
ZJS
275 assert_se(find_executable("xxxx-xxxx", &p) == -ENOENT);
276 assert_se(find_executable("/some/dir/xxxx-xxxx", &p) == -ENOENT);
92673045 277 assert_se(find_executable("/proc/filesystems", &p) == -EACCES);
c9d954b2
ZJS
278}
279
5ca9139a
ZJS
280static 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
8c35c10d 286 r = find_executable_full(path, NULL, NULL, false, &t, &fd);
5ca9139a
ZJS
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) {
a6d9111c
ZJS
298 r = fexecve_or_execve(fd, t, STRV_MAKE(t, "--version"), STRV_MAKE(NULL));
299 log_error_errno(r, "[f]execve: %m");
5ca9139a
ZJS
300 _exit(EXIT_FAILURE);
301 }
302
303 assert_se(wait_for_terminate_and_check(t, pid, WAIT_LOG) == 0);
304}
305
4f7452a8 306TEST(find_executable_exec) {
5ca9139a
ZJS
307 test_find_executable_exec_one("touch");
308 test_find_executable_exec_one("/bin/touch");
a6d9111c
ZJS
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);
5ca9139a
ZJS
313}
314
4f7452a8 315TEST(prefixes) {
b82f71c7
LP
316 static const char* const values[] = {
317 "/a/b/c/d",
318 "/a/b/c",
319 "/a/b",
320 "/a",
321 "",
322 NULL
323 };
e203f7c3 324 unsigned i;
fecffe5d 325 char s[PATH_MAX];
e203f7c3 326 bool b;
fecffe5d 327
e203f7c3
LP
328 i = 0;
329 PATH_FOREACH_PREFIX_MORE(s, "/a/b/c/d") {
fecffe5d
LP
330 log_error("---%s---", s);
331 assert_se(streq(s, values[i++]));
332 }
e203f7c3 333 assert_se(values[i] == NULL);
fecffe5d 334
e203f7c3
LP
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 }
fecffe5d
LP
340 assert_se(values[i] == NULL);
341
342 i = 0;
e203f7c3 343 PATH_FOREACH_PREFIX_MORE(s, "////a////b////c///d///////")
fecffe5d 344 assert_se(streq(s, values[i++]));
e203f7c3 345 assert_se(values[i] == NULL);
fecffe5d 346
e203f7c3
LP
347 i = 1;
348 PATH_FOREACH_PREFIX(s, "////a////b////c///d///////")
349 assert_se(streq(s, values[i++]));
fecffe5d
LP
350 assert_se(values[i] == NULL);
351
352 PATH_FOREACH_PREFIX(s, "////")
04499a70 353 assert_not_reached();
e203f7c3
LP
354
355 b = false;
356 PATH_FOREACH_PREFIX_MORE(s, "////") {
357 assert_se(!b);
fecffe5d 358 assert_se(streq(s, ""));
e203f7c3
LP
359 b = true;
360 }
361 assert_se(b);
fecffe5d
LP
362
363 PATH_FOREACH_PREFIX(s, "")
04499a70 364 assert_not_reached();
fecffe5d 365
e203f7c3
LP
366 b = false;
367 PATH_FOREACH_PREFIX_MORE(s, "") {
8d95631e
FB
368 assert_se(!b);
369 assert_se(streq(s, ""));
e203f7c3
LP
370 b = true;
371 }
fecffe5d
LP
372}
373
4f7452a8 374TEST(path_join) {
62a85ee0 375#define test_join(expected, ...) { \
59ae3a95 376 _cleanup_free_ char *z = NULL; \
62a85ee0
ZJS
377 z = path_join(__VA_ARGS__); \
378 log_debug("got \"%s\", expected \"%s\"", z, expected); \
59ae3a95
TA
379 assert_se(streq(z, expected)); \
380 }
381
62a85ee0
ZJS
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
652ef298
ZJS
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
62a85ee0 400 test_join("", "", NULL);
652ef298
ZJS
401 test_join("", NULL, "");
402 test_join("", NULL, NULL);
62a85ee0
ZJS
403
404 test_join("foo/bar", "foo", "bar");
405 test_join("foo/bar", "", "foo", "bar");
652ef298 406 test_join("foo/bar", NULL, "foo", NULL, "bar");
62a85ee0
ZJS
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//");
0c6ea3a4
ZJS
416}
417
4f7452a8 418TEST(path_extend) {
7ae27680
LP
419 _cleanup_free_ char *p = NULL;
420
7ae27680
LP
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"));
340cd6b6
YW
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"));
7ae27680
LP
441}
442
4f7452a8 443TEST(fsck_exists) {
eb66db55 444 /* Ensure we use a sane default for PATH. */
44ee03d1 445 assert_se(unsetenv("PATH") == 0);
eb66db55
MG
446
447 /* fsck.minix is provided by util-linux and will probably exist. */
85eca92e 448 assert_se(fsck_exists("minix") == 1);
eb66db55 449
85eca92e
LP
450 assert_se(fsck_exists("AbCdE") == 0);
451 assert_se(fsck_exists("/../bin/") == 0);
eb66db55
MG
452}
453
fe69c41e
YW
454static void test_path_make_relative_one(const char *from, const char *to, const char *expected) {
455 _cleanup_free_ char *z = NULL;
456 int r;
6b56a651 457
fe69c41e 458 log_info("/* %s(%s, %s) */", __func__, from, to);
771fded3 459
fe69c41e
YW
460 r = path_make_relative(from, to, &z);
461 assert_se((r >= 0) == !!expected);
462 assert_se(streq_ptr(z, expected));
463}
6b56a651 464
4f7452a8 465TEST(path_make_relative) {
fe69c41e
YW
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");
6b56a651
TK
478}
479
4f7452a8 480TEST(path_strv_resolve) {
3e8a78c8
MM
481 char tmp_dir[] = "/tmp/test-path-util-XXXXXX";
482 _cleanup_strv_free_ char **search_dirs = NULL;
483 _cleanup_strv_free_ char **absolute_dirs = NULL;
3e8a78c8
MM
484
485 assert_se(mkdtemp(tmp_dir) != NULL);
486
bea1a013 487 search_dirs = strv_new("/dir1", "/dir2", "/dir3");
3e8a78c8
MM
488 assert_se(search_dirs);
489 STRV_FOREACH(d, search_dirs) {
b910cc72 490 char *p = path_join(tmp_dir, *d);
3e8a78c8
MM
491 assert_se(p);
492 assert_se(strv_push(&absolute_dirs, p) == 0);
493 }
494
495 assert_se(mkdir(absolute_dirs[0], 0700) == 0);
496 assert_se(mkdir(absolute_dirs[1], 0700) == 0);
497 assert_se(symlink("dir2", absolute_dirs[2]) == 0);
498
499 path_strv_resolve(search_dirs, tmp_dir);
500 assert_se(streq(search_dirs[0], "/dir1"));
501 assert_se(streq(search_dirs[1], "/dir2"));
502 assert_se(streq(search_dirs[2], "/dir2"));
503
c6878637 504 assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
3e8a78c8
MM
505}
506
63f11e35
YW
507static void test_path_startswith_one(const char *path, const char *prefix, const char *skipped, const char *expected) {
508 const char *p, *q;
0470289b 509
63f11e35 510 log_debug("/* %s(%s, %s) */", __func__, path, prefix);
0470289b 511
63f11e35
YW
512 p = path_startswith(path, prefix);
513 assert_se(streq_ptr(p, expected));
514 if (p) {
515 q = strjoina(skipped, p);
516 assert_se(streq(q, path));
517 assert_se(p == path + strlen(skipped));
518 }
519}
0470289b 520
4f7452a8 521TEST(path_startswith) {
63f11e35
YW
522 test_path_startswith_one("/foo/bar/barfoo/", "/foo", "/foo/", "bar/barfoo/");
523 test_path_startswith_one("/foo/bar/barfoo/", "/foo/", "/foo/", "bar/barfoo/");
524 test_path_startswith_one("/foo/bar/barfoo/", "/", "/", "foo/bar/barfoo/");
525 test_path_startswith_one("/foo/bar/barfoo/", "////", "/", "foo/bar/barfoo/");
526 test_path_startswith_one("/foo/bar/barfoo/", "/foo//bar/////barfoo///", "/foo/bar/barfoo/", "");
527 test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/barfoo////", "/foo/bar/barfoo/", "");
528 test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar///barfoo/", "/foo/bar/barfoo/", "");
529 test_path_startswith_one("/foo/bar/barfoo/", "/foo////bar/barfoo/", "/foo/bar/barfoo/", "");
530 test_path_startswith_one("/foo/bar/barfoo/", "////foo/bar/barfoo/", "/foo/bar/barfoo/", "");
531 test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/barfoo", "/foo/bar/barfoo/", "");
532
533 test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/barfooa/", NULL, NULL);
534 test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/barfooa", NULL, NULL);
535 test_path_startswith_one("/foo/bar/barfoo/", "", NULL, NULL);
536 test_path_startswith_one("/foo/bar/barfoo/", "/bar/foo", NULL, NULL);
537 test_path_startswith_one("/foo/bar/barfoo/", "/f/b/b/", NULL, NULL);
538 test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/barfo", NULL, NULL);
539 test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/bar", NULL, NULL);
540 test_path_startswith_one("/foo/bar/barfoo/", "/fo", NULL, NULL);
5895b62f
RC
541}
542
1d13f648
LP
543static void test_prefix_root_one(const char *r, const char *p, const char *expected) {
544 _cleanup_free_ char *s = NULL;
545 const char *t;
546
c6134d3e
LP
547 assert_se(s = path_join(r, p));
548 assert_se(path_equal_ptr(s, expected));
1d13f648
LP
549
550 t = prefix_roota(r, p);
551 assert_se(t);
c6134d3e 552 assert_se(path_equal_ptr(t, expected));
1d13f648
LP
553}
554
4f7452a8 555TEST(prefix_root) {
1d13f648
LP
556 test_prefix_root_one("/", "/foo", "/foo");
557 test_prefix_root_one(NULL, "/foo", "/foo");
558 test_prefix_root_one("", "/foo", "/foo");
559 test_prefix_root_one("///", "/foo", "/foo");
560 test_prefix_root_one("/", "////foo", "/foo");
561 test_prefix_root_one(NULL, "////foo", "/foo");
f9421dd8
YW
562 test_prefix_root_one("/", "foo", "/foo");
563 test_prefix_root_one("", "foo", "foo");
564 test_prefix_root_one(NULL, "foo", "foo");
1d13f648
LP
565
566 test_prefix_root_one("/foo", "/bar", "/foo/bar");
567 test_prefix_root_one("/foo", "bar", "/foo/bar");
568 test_prefix_root_one("foo", "bar", "foo/bar");
569 test_prefix_root_one("/foo/", "/bar", "/foo/bar");
570 test_prefix_root_one("/foo/", "//bar", "/foo/bar");
571 test_prefix_root_one("/foo///", "//bar", "/foo/bar");
572}
573
4f7452a8 574TEST(file_in_same_dir) {
63292663
RC
575 char *t;
576
577 t = file_in_same_dir("/", "a");
578 assert_se(streq(t, "/a"));
579 free(t);
580
581 t = file_in_same_dir("/", "/a");
582 assert_se(streq(t, "/a"));
583 free(t);
584
585 t = file_in_same_dir("", "a");
586 assert_se(streq(t, "a"));
587 free(t);
588
589 t = file_in_same_dir("a/", "a");
590 assert_se(streq(t, "a/a"));
591 free(t);
592
593 t = file_in_same_dir("bar/foo", "bar");
594 assert_se(streq(t, "bar/bar"));
595 free(t);
596}
597
0ee54dd4
YW
598static void test_path_find_first_component_one(
599 const char *path,
600 bool accept_dot_dot,
601 char **expected,
602 int ret) {
603
604 log_debug("/* %s(\"%s\", accept_dot_dot=%s) */", __func__, strnull(path), yes_no(accept_dot_dot));
605
606 for (const char *p = path;;) {
607 const char *e;
608 int r;
609
610 r = path_find_first_component(&p, accept_dot_dot, &e);
611 if (r <= 0) {
612 if (r == 0) {
613 if (path)
614 assert_se(p == path + strlen_ptr(path));
615 else
616 assert_se(!p);
617 assert_se(!e);
618 }
619 assert_se(r == ret);
620 assert_se(strv_isempty(expected));
621 return;
622 }
623
624 assert_se(e);
625 assert_se(strcspn(e, "/") == (size_t) r);
626 assert_se(strlen_ptr(*expected) == (size_t) r);
627 assert_se(strneq(e, *expected++, r));
628 }
629}
630
4f7452a8 631TEST(path_find_first_component) {
0ee54dd4
YW
632 _cleanup_free_ char *hoge = NULL;
633 char foo[NAME_MAX * 2];
634
0ee54dd4
YW
635 test_path_find_first_component_one(NULL, false, NULL, 0);
636 test_path_find_first_component_one("", false, NULL, 0);
637 test_path_find_first_component_one("/", false, NULL, 0);
638 test_path_find_first_component_one(".", false, NULL, 0);
639 test_path_find_first_component_one("./", false, NULL, 0);
640 test_path_find_first_component_one("./.", false, NULL, 0);
641 test_path_find_first_component_one("..", false, NULL, -EINVAL);
642 test_path_find_first_component_one("/..", false, NULL, -EINVAL);
643 test_path_find_first_component_one("./..", false, NULL, -EINVAL);
644 test_path_find_first_component_one("////./././//.", false, NULL, 0);
645 test_path_find_first_component_one("a/b/c", false, STRV_MAKE("a", "b", "c"), 0);
646 test_path_find_first_component_one("././//.///aa/bbb//./ccc", false, STRV_MAKE("aa", "bbb", "ccc"), 0);
647 test_path_find_first_component_one("././//.///aa/.../../bbb//./ccc/.", false, STRV_MAKE("aa", "..."), -EINVAL);
648 test_path_find_first_component_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.", false, STRV_MAKE("aaa", ".bbb"), -EINVAL);
649 test_path_find_first_component_one("a/foo./b", false, STRV_MAKE("a", "foo.", "b"), 0);
650
651 test_path_find_first_component_one(NULL, true, NULL, 0);
652 test_path_find_first_component_one("", true, NULL, 0);
653 test_path_find_first_component_one("/", true, NULL, 0);
654 test_path_find_first_component_one(".", true, NULL, 0);
655 test_path_find_first_component_one("./", true, NULL, 0);
656 test_path_find_first_component_one("./.", true, NULL, 0);
657 test_path_find_first_component_one("..", true, STRV_MAKE(".."), 0);
658 test_path_find_first_component_one("/..", true, STRV_MAKE(".."), 0);
659 test_path_find_first_component_one("./..", true, STRV_MAKE(".."), 0);
660 test_path_find_first_component_one("////./././//.", true, NULL, 0);
661 test_path_find_first_component_one("a/b/c", true, STRV_MAKE("a", "b", "c"), 0);
662 test_path_find_first_component_one("././//.///aa/bbb//./ccc", true, STRV_MAKE("aa", "bbb", "ccc"), 0);
663 test_path_find_first_component_one("././//.///aa/.../../bbb//./ccc/.", true, STRV_MAKE("aa", "...", "..", "bbb", "ccc"), 0);
664 test_path_find_first_component_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.", true, STRV_MAKE("aaa", ".bbb", "..", "c.", "d.dd", "..eeee"), 0);
665 test_path_find_first_component_one("a/foo./b", true, STRV_MAKE("a", "foo.", "b"), 0);
666
667 memset(foo, 'a', sizeof(foo) -1);
668 char_array_0(foo);
669
670 test_path_find_first_component_one(foo, false, NULL, -EINVAL);
671 test_path_find_first_component_one(foo, true, NULL, -EINVAL);
672
673 hoge = strjoin("a/b/c/", foo, "//d/e/.//f/");
674 assert_se(hoge);
675
676 test_path_find_first_component_one(hoge, false, STRV_MAKE("a", "b", "c"), -EINVAL);
677 test_path_find_first_component_one(hoge, true, STRV_MAKE("a", "b", "c"), -EINVAL);
678}
679
484cd43c
YW
680static void test_path_find_last_component_one(
681 const char *path,
682 bool accept_dot_dot,
683 char **expected,
684 int ret) {
685
686 log_debug("/* %s(\"%s\", accept_dot_dot=%s) */", __func__, strnull(path), yes_no(accept_dot_dot));
687
688 for (const char *next = NULL;;) {
689 const char *e;
690 int r;
691
692 r = path_find_last_component(path, accept_dot_dot, &next, &e);
693 if (r <= 0) {
694 if (r == 0) {
695 assert_se(next == path);
696 assert_se(!e);
697 }
698 assert_se(r == ret);
699 assert_se(strv_isempty(expected));
700 return;
701 }
702
703 assert_se(e);
704 assert_se(strcspn(e, "/") == (size_t) r);
705 assert_se(strlen_ptr(*expected) == (size_t) r);
706 assert_se(strneq(e, *expected++, r));
707 }
708}
709
4f7452a8 710TEST(path_find_last_component) {
484cd43c
YW
711 _cleanup_free_ char *hoge = NULL;
712 char foo[NAME_MAX * 2];
713
484cd43c
YW
714 test_path_find_last_component_one(NULL, false, NULL, 0);
715 test_path_find_last_component_one("", false, NULL, 0);
716 test_path_find_last_component_one("/", false, NULL, 0);
717 test_path_find_last_component_one(".", false, NULL, 0);
718 test_path_find_last_component_one("./", false, NULL, 0);
719 test_path_find_last_component_one("./.", false, NULL, 0);
720 test_path_find_last_component_one("..", false, NULL, -EINVAL);
721 test_path_find_last_component_one("/..", false, NULL, -EINVAL);
722 test_path_find_last_component_one("./..", false, NULL, -EINVAL);
723 test_path_find_last_component_one("////./././//.", false, NULL, 0);
724 test_path_find_last_component_one("a/b/c", false, STRV_MAKE("c", "b", "a"), 0);
725 test_path_find_last_component_one("././//.///aa./.bbb//./ccc/././/", false, STRV_MAKE("ccc", ".bbb", "aa."), 0);
726 test_path_find_last_component_one("././//.///aa/../.../bbb//./ccc/.", false, STRV_MAKE("ccc", "bbb", "..."), -EINVAL);
727 test_path_find_last_component_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.", false, STRV_MAKE("..eeee", "d.dd", "c."), -EINVAL);
728
729 test_path_find_last_component_one(NULL, true, NULL, 0);
730 test_path_find_last_component_one("", true, NULL, 0);
731 test_path_find_last_component_one("/", true, NULL, 0);
732 test_path_find_last_component_one(".", true, NULL, 0);
733 test_path_find_last_component_one("./", true, NULL, 0);
734 test_path_find_last_component_one("./.", true, NULL, 0);
735 test_path_find_last_component_one("..", true, STRV_MAKE(".."), 0);
736 test_path_find_last_component_one("/..", true, STRV_MAKE(".."), 0);
737 test_path_find_last_component_one("./..", true, STRV_MAKE(".."), 0);
738 test_path_find_last_component_one("////./././//.", true, NULL, 0);
739 test_path_find_last_component_one("a/b/c", true, STRV_MAKE("c", "b", "a"), 0);
740 test_path_find_last_component_one("././//.///aa./.bbb//./ccc/././/", true, STRV_MAKE("ccc", ".bbb", "aa."), 0);
741 test_path_find_last_component_one("././//.///aa/../.../bbb//./ccc/.", true, STRV_MAKE("ccc", "bbb", "...", "..", "aa"), 0);
742 test_path_find_last_component_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.", true, STRV_MAKE("..eeee", "d.dd", "c.", "..", ".bbb", "aaa"), 0);
743
744 memset(foo, 'a', sizeof(foo) -1);
745 char_array_0(foo);
746
747 test_path_find_last_component_one(foo, false, NULL, -EINVAL);
748 test_path_find_last_component_one(foo, true, NULL, -EINVAL);
749
750 hoge = strjoin(foo, "/a/b/c/");
751 assert_se(hoge);
752
753 test_path_find_last_component_one(hoge, false, STRV_MAKE("c", "b", "a"), -EINVAL);
754 test_path_find_last_component_one(hoge, true, STRV_MAKE("c", "b", "a"), -EINVAL);
755}
756
4f7452a8 757TEST(last_path_component) {
77e0a1b5 758 assert_se(last_path_component(NULL) == NULL);
b12d25a8
ZJS
759 assert_se(streq(last_path_component("a/b/c"), "c"));
760 assert_se(streq(last_path_component("a/b/c/"), "c/"));
761 assert_se(streq(last_path_component("/"), "/"));
762 assert_se(streq(last_path_component("//"), "/"));
763 assert_se(streq(last_path_component("///"), "/"));
764 assert_se(streq(last_path_component("."), "."));
765 assert_se(streq(last_path_component("./."), "."));
766 assert_se(streq(last_path_component("././"), "./"));
767 assert_se(streq(last_path_component("././/"), ".//"));
768 assert_se(streq(last_path_component("/foo/a"), "a"));
769 assert_se(streq(last_path_component("/foo/a/"), "a/"));
69f9ccf1 770 assert_se(streq(last_path_component(""), ""));
8460289f
LP
771 assert_se(streq(last_path_component("a"), "a"));
772 assert_se(streq(last_path_component("a/"), "a/"));
773 assert_se(streq(last_path_component("/a"), "a"));
774 assert_se(streq(last_path_component("/a/"), "a/"));
b12d25a8
ZJS
775}
776
a60c8eee
LP
777static void test_path_extract_filename_one(const char *input, const char *output, int ret) {
778 _cleanup_free_ char *k = NULL;
779 int r;
780
781 r = path_extract_filename(input, &k);
ee277c6b
LP
782 log_info_errno(r, "%s → %s/%m [expected: %s/%s]",
783 strnull(input),
784 strnull(k), /* strerror(r) is printed via %m, to avoid that the two strerror()'s overwrite each other's buffers */
785 strnull(output), ret < 0 ? strerror_safe(ret) : "-");
a60c8eee
LP
786 assert_se(streq_ptr(k, output));
787 assert_se(r == ret);
788}
789
4f7452a8 790TEST(path_extract_filename) {
a60c8eee
LP
791 test_path_extract_filename_one(NULL, NULL, -EINVAL);
792 test_path_extract_filename_one("a/b/c", "c", 0);
ee277c6b 793 test_path_extract_filename_one("a/b/c/", "c", O_DIRECTORY);
3cdcbdd3
LP
794 test_path_extract_filename_one("/", NULL, -EADDRNOTAVAIL);
795 test_path_extract_filename_one("//", NULL, -EADDRNOTAVAIL);
796 test_path_extract_filename_one("///", NULL, -EADDRNOTAVAIL);
01950464
YW
797 test_path_extract_filename_one("/.", NULL, -EADDRNOTAVAIL);
798 test_path_extract_filename_one(".", NULL, -EADDRNOTAVAIL);
799 test_path_extract_filename_one("./", NULL, -EADDRNOTAVAIL);
800 test_path_extract_filename_one("./.", NULL, -EADDRNOTAVAIL);
801 test_path_extract_filename_one("././", NULL, -EADDRNOTAVAIL);
802 test_path_extract_filename_one("././/", NULL, -EADDRNOTAVAIL);
a60c8eee 803 test_path_extract_filename_one("/foo/a", "a", 0);
ee277c6b 804 test_path_extract_filename_one("/foo/a/", "a", O_DIRECTORY);
a60c8eee
LP
805 test_path_extract_filename_one("", NULL, -EINVAL);
806 test_path_extract_filename_one("a", "a", 0);
ee277c6b 807 test_path_extract_filename_one("a/", "a", O_DIRECTORY);
01950464 808 test_path_extract_filename_one("a/././//.", "a", O_DIRECTORY);
a60c8eee 809 test_path_extract_filename_one("/a", "a", 0);
ee277c6b 810 test_path_extract_filename_one("/a/", "a", O_DIRECTORY);
01950464 811 test_path_extract_filename_one("/a//./.", "a", O_DIRECTORY);
ee277c6b 812 test_path_extract_filename_one("/////////////a/////////////", "a", O_DIRECTORY);
01950464
YW
813 test_path_extract_filename_one("//./a/.///b./././.c//./d//.", "d", O_DIRECTORY);
814 test_path_extract_filename_one("xx/.", "xx", O_DIRECTORY);
a60c8eee
LP
815 test_path_extract_filename_one("xx/..", NULL, -EINVAL);
816 test_path_extract_filename_one("..", NULL, -EINVAL);
817 test_path_extract_filename_one("/..", NULL, -EINVAL);
818 test_path_extract_filename_one("../", NULL, -EINVAL);
a60c8eee
LP
819}
820
8dcb891c
LP
821static void test_path_extract_directory_one(const char *input, const char *output, int ret) {
822 _cleanup_free_ char *k = NULL;
823 int r;
824
825 r = path_extract_directory(input, &k);
826 log_info_errno(r, "%s → %s/%m [expected: %s/%s]",
827 strnull(input),
828 strnull(k), /* we output strerror_safe(r) via %m here, since otherwise the error buffer might be overwritten twice */
829 strnull(output), strerror_safe(ret));
830 assert_se(streq_ptr(k, output));
831 assert_se(r == ret);
832
833 /* Extra safety check: let's make sure that if we split out the filename too (and it works) the
834 * joined parts are identical to the original again */
835 if (r >= 0) {
836 _cleanup_free_ char *f = NULL;
837
838 r = path_extract_filename(input, &f);
839 if (r >= 0) {
840 _cleanup_free_ char *j = NULL;
841
842 assert_se(j = path_join(k, f));
843 assert_se(path_equal(input, j));
844 }
845 }
846}
847
4f7452a8 848TEST(path_extract_directory) {
8dcb891c
LP
849 test_path_extract_directory_one(NULL, NULL, -EINVAL);
850 test_path_extract_directory_one("a/b/c", "a/b", 0);
851 test_path_extract_directory_one("a/b/c/", "a/b", 0);
852 test_path_extract_directory_one("/", NULL, -EADDRNOTAVAIL);
853 test_path_extract_directory_one("//", NULL, -EADDRNOTAVAIL);
854 test_path_extract_directory_one("///", NULL, -EADDRNOTAVAIL);
01950464
YW
855 test_path_extract_directory_one("/.", NULL, -EADDRNOTAVAIL);
856 test_path_extract_directory_one(".", NULL, -EADDRNOTAVAIL);
857 test_path_extract_directory_one("./", NULL, -EADDRNOTAVAIL);
858 test_path_extract_directory_one("./.", NULL, -EADDRNOTAVAIL);
859 test_path_extract_directory_one("././", NULL, -EADDRNOTAVAIL);
860 test_path_extract_directory_one("././/", NULL, -EADDRNOTAVAIL);
8dcb891c
LP
861 test_path_extract_directory_one("/foo/a", "/foo", 0);
862 test_path_extract_directory_one("/foo/a/", "/foo", 0);
863 test_path_extract_directory_one("", NULL, -EINVAL);
864 test_path_extract_directory_one("a", NULL, -EDESTADDRREQ);
865 test_path_extract_directory_one("a/", NULL, -EDESTADDRREQ);
01950464 866 test_path_extract_directory_one("a/././//.", NULL, -EDESTADDRREQ);
8dcb891c
LP
867 test_path_extract_directory_one("/a", "/", 0);
868 test_path_extract_directory_one("/a/", "/", 0);
01950464 869 test_path_extract_directory_one("/a//./.", "/", 0);
8dcb891c 870 test_path_extract_directory_one("/////////////a/////////////", "/", 0);
01950464
YW
871 test_path_extract_directory_one("//./a/.///b./././.c//./d//.", "/a/b./.c", 0);
872 test_path_extract_directory_one("xx/.", NULL, -EDESTADDRREQ);
873 test_path_extract_directory_one("xx/..", NULL, -EINVAL);
874 test_path_extract_directory_one("..", NULL, -EINVAL);
875 test_path_extract_directory_one("/..", NULL, -EINVAL);
876 test_path_extract_directory_one("../", NULL, -EINVAL);
8dcb891c
LP
877}
878
4f7452a8 879TEST(filename_is_valid) {
2ef2376d 880 char foo[NAME_MAX+2];
63292663
RC
881
882 assert_se(!filename_is_valid(""));
883 assert_se(!filename_is_valid("/bar/foo"));
884 assert_se(!filename_is_valid("/"));
885 assert_se(!filename_is_valid("."));
886 assert_se(!filename_is_valid(".."));
1c322571
ZJS
887 assert_se(!filename_is_valid("bar/foo"));
888 assert_se(!filename_is_valid("bar/foo/"));
889 assert_se(!filename_is_valid("bar//"));
63292663 890
2ef2376d
LP
891 memset(foo, 'a', sizeof(foo) - 1);
892 char_array_0(foo);
63292663
RC
893
894 assert_se(!filename_is_valid(foo));
895
896 assert_se(filename_is_valid("foo_bar-333"));
897 assert_se(filename_is_valid("o.o"));
898}
899
32df2e14 900static void test_path_is_valid_and_safe_one(const char *p, bool ret) {
7802194a 901 log_debug("/* %s(\"%s\") */", __func__, strnull(p));
32df2e14
YW
902
903 assert_se(path_is_valid(p) == ret);
904 if (ret)
905 ret = !streq(p, "..") &&
906 !startswith(p, "../") &&
907 !endswith(p, "/..") &&
908 !strstr(p, "/../");
909 assert_se(path_is_safe(p) == ret);
910}
911
4f7452a8 912TEST(path_is_valid_and_safe) {
2ef2376d
LP
913 char foo[PATH_MAX+2];
914 const char *c;
915
32df2e14
YW
916 test_path_is_valid_and_safe_one("", false);
917 test_path_is_valid_and_safe_one("/bar/foo", true);
918 test_path_is_valid_and_safe_one("/bar/foo/", true);
919 test_path_is_valid_and_safe_one("/bar/foo/", true);
920 test_path_is_valid_and_safe_one("//bar//foo//", true);
921 test_path_is_valid_and_safe_one("/", true);
922 test_path_is_valid_and_safe_one("/////", true);
923 test_path_is_valid_and_safe_one("/////.///.////...///..//.", true);
924 test_path_is_valid_and_safe_one(".", true);
925 test_path_is_valid_and_safe_one("..", true);
926 test_path_is_valid_and_safe_one("bar/foo", true);
927 test_path_is_valid_and_safe_one("bar/foo/", true);
928 test_path_is_valid_and_safe_one("bar//", true);
2ef2376d
LP
929
930 memset(foo, 'a', sizeof(foo) -1);
931 char_array_0(foo);
932
32df2e14 933 test_path_is_valid_and_safe_one(foo, false);
2ef2376d
LP
934
935 c = strjoina("/xxx/", foo, "/yyy");
32df2e14 936 test_path_is_valid_and_safe_one(c, false);
2ef2376d 937
32df2e14
YW
938 test_path_is_valid_and_safe_one("foo_bar-333", true);
939 test_path_is_valid_and_safe_one("o.o", true);
2ef2376d
LP
940}
941
4f7452a8 942TEST(hidden_or_backup_file) {
b05b9cde
ZJS
943 assert_se(hidden_or_backup_file(".hidden"));
944 assert_se(hidden_or_backup_file("..hidden"));
945 assert_se(!hidden_or_backup_file("hidden."));
946
947 assert_se(hidden_or_backup_file("backup~"));
948 assert_se(hidden_or_backup_file(".backup~"));
949
950 assert_se(hidden_or_backup_file("lost+found"));
951 assert_se(hidden_or_backup_file("aquota.user"));
952 assert_se(hidden_or_backup_file("aquota.group"));
953
954 assert_se(hidden_or_backup_file("test.rpmnew"));
955 assert_se(hidden_or_backup_file("test.dpkg-old"));
956 assert_se(hidden_or_backup_file("test.dpkg-remove"));
957 assert_se(hidden_or_backup_file("test.swp"));
958
959 assert_se(!hidden_or_backup_file("test.rpmnew."));
960 assert_se(!hidden_or_backup_file("test.dpkg-old.foo"));
961}
962
4f7452a8 963TEST(skip_dev_prefix) {
a119ec7c
LP
964 assert_se(streq(skip_dev_prefix("/"), "/"));
965 assert_se(streq(skip_dev_prefix("/dev"), ""));
966 assert_se(streq(skip_dev_prefix("/dev/"), ""));
967 assert_se(streq(skip_dev_prefix("/dev/foo"), "foo"));
968 assert_se(streq(skip_dev_prefix("/dev/foo/bar"), "foo/bar"));
969 assert_se(streq(skip_dev_prefix("//dev"), ""));
970 assert_se(streq(skip_dev_prefix("//dev//"), ""));
971 assert_se(streq(skip_dev_prefix("/dev///foo"), "foo"));
972 assert_se(streq(skip_dev_prefix("///dev///foo///bar"), "foo///bar"));
973 assert_se(streq(skip_dev_prefix("//foo"), "//foo"));
974 assert_se(streq(skip_dev_prefix("foo"), "foo"));
975}
976
4f7452a8 977TEST(empty_or_root) {
57ea45e1
LP
978 assert_se(empty_or_root(NULL));
979 assert_se(empty_or_root(""));
980 assert_se(empty_or_root("/"));
981 assert_se(empty_or_root("//"));
982 assert_se(empty_or_root("///"));
983 assert_se(empty_or_root("/////////////////"));
984 assert_se(!empty_or_root("xxx"));
985 assert_se(!empty_or_root("/xxx"));
986 assert_se(!empty_or_root("/xxx/"));
987 assert_se(!empty_or_root("//yy//"));
988}
989
4f7452a8 990TEST(path_startswith_set) {
d898ed65
LP
991 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/foo/bar", "/zzz"), ""));
992 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/foo/", "/zzz"), "bar"));
993 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/foo", "/zzz"), "bar"));
994 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/", "/zzz"), "foo/bar"));
995 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "", "/zzz"), NULL));
996
997 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/foo/bar", "/zzz"), NULL));
998 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/foo/", "/zzz"), "bar2"));
999 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/foo", "/zzz"), "bar2"));
1000 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/", "/zzz"), "foo/bar2"));
1001 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "", "/zzz"), NULL));
1002
1003 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/foo/bar", "/zzz"), NULL));
1004 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/foo/", "/zzz"), NULL));
1005 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/foo", "/zzz"), NULL));
1006 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/", "/zzz"), "foo2/bar"));
1007 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "", "/zzz"), NULL));
1008}
1009
4f7452a8 1010TEST(path_startswith_strv) {
cc4d7d81
ZJS
1011 assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/foo/bar", "/zzz")), ""));
1012 assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/foo/", "/zzz")), "bar"));
1013 assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/foo", "/zzz")), "bar"));
1014 assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/", "/zzz")), "foo/bar"));
1015 assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "", "/zzz")), NULL));
1016
1017 assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "/foo/bar", "/zzz")), NULL));
1018 assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "/foo/", "/zzz")), "bar2"));
1019 assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "/foo", "/zzz")), "bar2"));
1020 assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "/", "/zzz")), "foo/bar2"));
1021 assert_se(streq_ptr(path_startswith_strv("/foo/bar2", STRV_MAKE("/foo/quux", "", "/zzz")), NULL));
1022
1023 assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "/foo/bar", "/zzz")), NULL));
1024 assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "/foo/", "/zzz")), NULL));
1025 assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "/foo", "/zzz")), NULL));
1026 assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "/", "/zzz")), "foo2/bar"));
1027 assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "", "/zzz")), NULL));
1028}
1029
4f7452a8 1030TEST(print_MAX) {
69866062
LP
1031 log_info("PATH_MAX=%zu\n"
1032 "FILENAME_MAX=%zu\n"
1033 "NAME_MAX=%zu",
1034 (size_t) PATH_MAX,
1035 (size_t) FILENAME_MAX,
1036 (size_t) NAME_MAX);
1037
1038 assert_cc(FILENAME_MAX == PATH_MAX);
76877b46 1039}
4f7452a8
JJ
1040
1041DEFINE_TEST_MAIN(LOG_DEBUG);