]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-path-util.c
tree-wide: introduce strerror_safe()
[thirdparty/systemd.git] / src / test / test-path-util.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
76877b46 2
a696dbef 3#include <stdio.h>
07630cea 4#include <unistd.h>
a696dbef 5
b5efdb8a 6#include "alloc-util.h"
3ffd4af2 7#include "fd-util.h"
76877b46 8#include "macro.h"
049af8ad 9#include "mountpoint-util.h"
07630cea 10#include "path-util.h"
c6878637 11#include "rm-rf.h"
84e72b5e 12#include "stat-util.h"
07630cea
LP
13#include "string-util.h"
14#include "strv.h"
6d7c4033 15#include "tests.h"
07630cea 16#include "util.h"
76877b46 17
2230852b
MS
18#define test_path_compare(a, b, result) { \
19 assert_se(path_compare(a, b) == result); \
20 assert_se(path_compare(b, a) == -result); \
21 assert_se(path_equal(a, b) == !result); \
22 assert_se(path_equal(b, a) == !result); \
23 }
76877b46 24
858d36c1
YW
25static void test_path_simplify(const char *in, const char *out, const char *out_dot) {
26 char *p;
27
28 p = strdupa(in);
29 assert_se(streq(path_simplify(p, false), out));
30
31 p = strdupa(in);
32 assert_se(streq(path_simplify(p, true), out_dot));
33}
34
76877b46 35static void test_path(void) {
3f72b427
LP
36 _cleanup_close_ int fd = -1;
37
2230852b
MS
38 test_path_compare("/goo", "/goo", 0);
39 test_path_compare("/goo", "/goo", 0);
40 test_path_compare("//goo", "/goo", 0);
41 test_path_compare("//goo/////", "/goo", 0);
42 test_path_compare("goo/////", "goo", 0);
76877b46 43
2230852b
MS
44 test_path_compare("/goo/boo", "/goo//boo", 0);
45 test_path_compare("//goo/boo", "/goo/boo//", 0);
76877b46 46
2230852b 47 test_path_compare("/", "///", 0);
76877b46 48
2230852b
MS
49 test_path_compare("/x", "x/", 1);
50 test_path_compare("x/", "/", -1);
76877b46 51
2230852b
MS
52 test_path_compare("/x/./y", "x/y", 1);
53 test_path_compare("x/.y", "x/y", -1);
54
55 test_path_compare("foo", "/foo", -1);
56 test_path_compare("/foo", "/foo/bar", -1);
57 test_path_compare("/foo/aaa", "/foo/b", -1);
58 test_path_compare("/foo/aaa", "/foo/b/a", -1);
59 test_path_compare("/foo/a", "/foo/aaa", -1);
60 test_path_compare("/foo/a/b", "/foo/aaa", -1);
76877b46
ZJS
61
62 assert_se(path_is_absolute("/"));
63 assert_se(!path_is_absolute("./"));
64
65 assert_se(is_path("/dir"));
66 assert_se(is_path("a/b"));
67 assert_se(!is_path("."));
68
2b6bf07d
ZJS
69 assert_se(streq(basename("./aa/bb/../file.da."), "file.da."));
70 assert_se(streq(basename("/aa///.file"), ".file"));
71 assert_se(streq(basename("/aa///file..."), "file..."));
72 assert_se(streq(basename("file.../"), ""));
76877b46 73
3f72b427
LP
74 fd = open("/", O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOCTTY);
75 assert_se(fd >= 0);
5d409034 76 assert_se(fd_is_mount_point(fd, "/", 0) > 0);
76877b46 77
858d36c1
YW
78 test_path_simplify("aaa/bbb////ccc", "aaa/bbb/ccc", "aaa/bbb/ccc");
79 test_path_simplify("//aaa/.////ccc", "/aaa/./ccc", "/aaa/ccc");
80 test_path_simplify("///", "/", "/");
81 test_path_simplify("///.//", "/.", "/");
82 test_path_simplify("///.//.///", "/./.", "/");
83 test_path_simplify("////.././///../.", "/.././../.", "/../..");
afbae3e9
TH
84 test_path_simplify(".", ".", ".");
85 test_path_simplify("./", ".", ".");
86 test_path_simplify(".///.//./.", "./././.", ".");
87 test_path_simplify(".///.//././/", "./././.", ".");
858d36c1
YW
88 test_path_simplify("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.",
89 "/./aaa/././.bbb/../c./d.dd/..eeee/.",
90 "/aaa/.bbb/../c./d.dd/..eeee");
91 test_path_simplify("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/..",
92 "/./aaa/././.bbb/../c./d.dd/..eeee/..",
93 "/aaa/.bbb/../c./d.dd/..eeee/..");
94 test_path_simplify(".//./aaa///.//./.bbb/..///c.//d.dd///..eeee/..",
95 "././aaa/././.bbb/../c./d.dd/..eeee/..",
96 "aaa/.bbb/../c./d.dd/..eeee/..");
97 test_path_simplify("..//./aaa///.//./.bbb/..///c.//d.dd///..eeee/..",
98 ".././aaa/././.bbb/../c./d.dd/..eeee/..",
99 "../aaa/.bbb/../c./d.dd/..eeee/..");
3ae5990c
ZJS
100
101 assert_se(PATH_IN_SET("/bin", "/", "/bin", "/foo"));
102 assert_se(PATH_IN_SET("/bin", "/bin"));
103 assert_se(PATH_IN_SET("/bin", "/foo/bar", "/bin"));
104 assert_se(PATH_IN_SET("/", "/", "/", "/foo/bar"));
105 assert_se(!PATH_IN_SET("/", "/abc", "/def"));
24737c29
ZJS
106
107 assert_se(path_equal_ptr(NULL, NULL));
108 assert_se(path_equal_ptr("/a", "/a"));
109 assert_se(!path_equal_ptr("/a", "/b"));
110 assert_se(!path_equal_ptr("/a", NULL));
111 assert_se(!path_equal_ptr(NULL, "/a"));
76877b46
ZJS
112}
113
84e72b5e
ZJS
114static void test_path_equal_root(void) {
115 /* Nail down the details of how path_equal("/", ...) works. */
116
117 assert_se(path_equal("/", "/"));
118 assert_se(path_equal("/", "//"));
119
120 assert_se(!path_equal("/", "/./"));
121 assert_se(!path_equal("/", "/../"));
122
123 assert_se(!path_equal("/", "/.../"));
124
125 /* Make sure that files_same works as expected. */
126
e3f791a2
ZJS
127 assert_se(files_same("/", "/", 0) > 0);
128 assert_se(files_same("/", "/", AT_SYMLINK_NOFOLLOW) > 0);
129 assert_se(files_same("/", "//", 0) > 0);
130 assert_se(files_same("/", "//", AT_SYMLINK_NOFOLLOW) > 0);
84e72b5e 131
e3f791a2
ZJS
132 assert_se(files_same("/", "/./", 0) > 0);
133 assert_se(files_same("/", "/./", AT_SYMLINK_NOFOLLOW) > 0);
134 assert_se(files_same("/", "/../", 0) > 0);
135 assert_se(files_same("/", "/../", AT_SYMLINK_NOFOLLOW) > 0);
84e72b5e 136
e3f791a2
ZJS
137 assert_se(files_same("/", "/.../", 0) == -ENOENT);
138 assert_se(files_same("/", "/.../", AT_SYMLINK_NOFOLLOW) == -ENOENT);
84e72b5e
ZJS
139
140 /* The same for path_equal_or_files_same. */
141
e3f791a2
ZJS
142 assert_se(path_equal_or_files_same("/", "/", 0));
143 assert_se(path_equal_or_files_same("/", "/", AT_SYMLINK_NOFOLLOW));
144 assert_se(path_equal_or_files_same("/", "//", 0));
145 assert_se(path_equal_or_files_same("/", "//", AT_SYMLINK_NOFOLLOW));
84e72b5e 146
e3f791a2
ZJS
147 assert_se(path_equal_or_files_same("/", "/./", 0));
148 assert_se(path_equal_or_files_same("/", "/./", AT_SYMLINK_NOFOLLOW));
149 assert_se(path_equal_or_files_same("/", "/../", 0));
150 assert_se(path_equal_or_files_same("/", "/../", AT_SYMLINK_NOFOLLOW));
84e72b5e 151
e3f791a2
ZJS
152 assert_se(!path_equal_or_files_same("/", "/.../", 0));
153 assert_se(!path_equal_or_files_same("/", "/.../", AT_SYMLINK_NOFOLLOW));
84e72b5e
ZJS
154}
155
85eca92e 156static void test_find_binary(const char *self) {
c9d954b2
ZJS
157 char *p;
158
85eca92e 159 assert_se(find_binary("/bin/sh", &p) == 0);
c9d954b2 160 puts(p);
85eca92e 161 assert_se(path_equal(p, "/bin/sh"));
c9d954b2
ZJS
162 free(p);
163
85eca92e 164 assert_se(find_binary(self, &p) == 0);
c9d954b2 165 puts(p);
6d1e2ddd
MG
166 /* libtool might prefix the binary name with "lt-" */
167 assert_se(endswith(p, "/lt-test-path-util") || endswith(p, "/test-path-util"));
8d95631e 168 assert_se(path_is_absolute(p));
c9d954b2
ZJS
169 free(p);
170
85eca92e 171 assert_se(find_binary("sh", &p) == 0);
c9d954b2 172 puts(p);
8d95631e
FB
173 assert_se(endswith(p, "/sh"));
174 assert_se(path_is_absolute(p));
c9d954b2
ZJS
175 free(p);
176
85eca92e
LP
177 assert_se(find_binary("xxxx-xxxx", &p) == -ENOENT);
178 assert_se(find_binary("/some/dir/xxxx-xxxx", &p) == -ENOENT);
c9d954b2
ZJS
179}
180
fecffe5d 181static void test_prefixes(void) {
b82f71c7
LP
182 static const char* const values[] = {
183 "/a/b/c/d",
184 "/a/b/c",
185 "/a/b",
186 "/a",
187 "",
188 NULL
189 };
e203f7c3 190 unsigned i;
fecffe5d 191 char s[PATH_MAX];
e203f7c3 192 bool b;
fecffe5d 193
e203f7c3
LP
194 i = 0;
195 PATH_FOREACH_PREFIX_MORE(s, "/a/b/c/d") {
fecffe5d
LP
196 log_error("---%s---", s);
197 assert_se(streq(s, values[i++]));
198 }
e203f7c3 199 assert_se(values[i] == NULL);
fecffe5d 200
e203f7c3
LP
201 i = 1;
202 PATH_FOREACH_PREFIX(s, "/a/b/c/d") {
203 log_error("---%s---", s);
204 assert_se(streq(s, values[i++]));
205 }
fecffe5d
LP
206 assert_se(values[i] == NULL);
207
208 i = 0;
e203f7c3 209 PATH_FOREACH_PREFIX_MORE(s, "////a////b////c///d///////")
fecffe5d 210 assert_se(streq(s, values[i++]));
e203f7c3 211 assert_se(values[i] == NULL);
fecffe5d 212
e203f7c3
LP
213 i = 1;
214 PATH_FOREACH_PREFIX(s, "////a////b////c///d///////")
215 assert_se(streq(s, values[i++]));
fecffe5d
LP
216 assert_se(values[i] == NULL);
217
218 PATH_FOREACH_PREFIX(s, "////")
e203f7c3
LP
219 assert_not_reached("Wut?");
220
221 b = false;
222 PATH_FOREACH_PREFIX_MORE(s, "////") {
223 assert_se(!b);
fecffe5d 224 assert_se(streq(s, ""));
e203f7c3
LP
225 b = true;
226 }
227 assert_se(b);
fecffe5d
LP
228
229 PATH_FOREACH_PREFIX(s, "")
230 assert_not_reached("wut?");
231
e203f7c3
LP
232 b = false;
233 PATH_FOREACH_PREFIX_MORE(s, "") {
8d95631e
FB
234 assert_se(!b);
235 assert_se(streq(s, ""));
e203f7c3
LP
236 b = true;
237 }
fecffe5d
LP
238}
239
0c6ea3a4 240static void test_path_join(void) {
59ae3a95 241
62a85ee0 242#define test_join(expected, ...) { \
59ae3a95 243 _cleanup_free_ char *z = NULL; \
62a85ee0
ZJS
244 z = path_join(__VA_ARGS__); \
245 log_debug("got \"%s\", expected \"%s\"", z, expected); \
59ae3a95
TA
246 assert_se(streq(z, expected)); \
247 }
248
62a85ee0
ZJS
249 test_join("/root/a/b/c", "/root", "/a/b", "/c");
250 test_join("/root/a/b/c", "/root", "a/b", "c");
251 test_join("/root/a/b/c", "/root", "/a/b", "c");
252 test_join("/root/c", "/root", "/", "c");
253 test_join("/root/", "/root", "/", NULL);
254
255 test_join("/a/b/c", "", "/a/b", "/c");
256 test_join("a/b/c", "", "a/b", "c");
257 test_join("/a/b/c", "", "/a/b", "c");
258 test_join("/c", "", "/", "c");
259 test_join("/", "", "/", NULL);
260
652ef298
ZJS
261 test_join("/a/b/c", NULL, "/a/b", "/c");
262 test_join("a/b/c", NULL, "a/b", "c");
263 test_join("/a/b/c", NULL, "/a/b", "c");
264 test_join("/c", NULL, "/", "c");
265 test_join("/", NULL, "/", NULL);
266
62a85ee0 267 test_join("", "", NULL);
652ef298
ZJS
268 test_join("", NULL, "");
269 test_join("", NULL, NULL);
62a85ee0
ZJS
270
271 test_join("foo/bar", "foo", "bar");
272 test_join("foo/bar", "", "foo", "bar");
652ef298 273 test_join("foo/bar", NULL, "foo", NULL, "bar");
62a85ee0
ZJS
274 test_join("foo/bar", "", "foo", "", "bar", "");
275 test_join("foo/bar", "", "", "", "", "foo", "", "", "", "bar", "", "", "");
276
277 test_join("//foo///bar//", "", "/", "", "/foo/", "", "/", "", "/bar/", "", "/", "");
278 test_join("/foo/bar/", "/", "foo", "/", "bar", "/");
279 test_join("foo/bar/baz", "foo", "bar", "baz");
280 test_join("foo/bar/baz", "foo/", "bar", "/baz");
281 test_join("foo//bar//baz", "foo/", "/bar/", "/baz");
282 test_join("//foo////bar////baz//", "//foo/", "///bar/", "///baz//");
0c6ea3a4
ZJS
283}
284
eb66db55
MG
285static void test_fsck_exists(void) {
286 /* Ensure we use a sane default for PATH. */
287 unsetenv("PATH");
288
289 /* fsck.minix is provided by util-linux and will probably exist. */
85eca92e 290 assert_se(fsck_exists("minix") == 1);
eb66db55 291
85eca92e
LP
292 assert_se(fsck_exists("AbCdE") == 0);
293 assert_se(fsck_exists("/../bin/") == 0);
eb66db55
MG
294}
295
6b56a651
TK
296static void test_make_relative(void) {
297 char *result;
298
299 assert_se(path_make_relative("some/relative/path", "/some/path", &result) < 0);
300 assert_se(path_make_relative("/some/path", "some/relative/path", &result) < 0);
2a5beb66 301 assert_se(path_make_relative("/some/dotdot/../path", "/some/path", &result) < 0);
6b56a651 302
59ae3a95
TA
303#define test(from_dir, to_path, expected) { \
304 _cleanup_free_ char *z = NULL; \
305 path_make_relative(from_dir, to_path, &z); \
306 assert_se(streq(z, expected)); \
6b56a651
TK
307 }
308
309 test("/", "/", ".");
310 test("/", "/some/path", "some/path");
311 test("/some/path", "/some/path", ".");
312 test("/some/path", "/some/path/in/subdir", "in/subdir");
313 test("/some/path", "/", "../..");
314 test("/some/path", "/some/other/path", "../other/path");
2a5beb66 315 test("/some/path/./dot", "/some/further/path", "../../further/path");
48054262 316 test("//extra.//.//./.slashes//./won't////fo.ol///anybody//", "/././/extra././/.slashes////ar.e/.just/././.fine///", "../../../ar.e/.just/.fine");
6b56a651
TK
317}
318
3e8a78c8
MM
319static void test_strv_resolve(void) {
320 char tmp_dir[] = "/tmp/test-path-util-XXXXXX";
321 _cleanup_strv_free_ char **search_dirs = NULL;
322 _cleanup_strv_free_ char **absolute_dirs = NULL;
323 char **d;
324
325 assert_se(mkdtemp(tmp_dir) != NULL);
326
bea1a013 327 search_dirs = strv_new("/dir1", "/dir2", "/dir3");
3e8a78c8
MM
328 assert_se(search_dirs);
329 STRV_FOREACH(d, search_dirs) {
330 char *p = strappend(tmp_dir, *d);
331 assert_se(p);
332 assert_se(strv_push(&absolute_dirs, p) == 0);
333 }
334
335 assert_se(mkdir(absolute_dirs[0], 0700) == 0);
336 assert_se(mkdir(absolute_dirs[1], 0700) == 0);
337 assert_se(symlink("dir2", absolute_dirs[2]) == 0);
338
339 path_strv_resolve(search_dirs, tmp_dir);
340 assert_se(streq(search_dirs[0], "/dir1"));
341 assert_se(streq(search_dirs[1], "/dir2"));
342 assert_se(streq(search_dirs[2], "/dir2"));
343
c6878637 344 assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
3e8a78c8
MM
345}
346
5895b62f 347static void test_path_startswith(void) {
0470289b
ZJS
348 const char *p;
349
350 p = path_startswith("/foo/bar/barfoo/", "/foo");
351 assert_se(streq_ptr(p, "bar/barfoo/"));
352
353 p = path_startswith("/foo/bar/barfoo/", "/foo/");
354 assert_se(streq_ptr(p, "bar/barfoo/"));
355
356 p = path_startswith("/foo/bar/barfoo/", "/");
357 assert_se(streq_ptr(p, "foo/bar/barfoo/"));
358
359 p = path_startswith("/foo/bar/barfoo/", "////");
360 assert_se(streq_ptr(p, "foo/bar/barfoo/"));
361
362 p = path_startswith("/foo/bar/barfoo/", "/foo//bar/////barfoo///");
363 assert_se(streq_ptr(p, ""));
364
365 p = path_startswith("/foo/bar/barfoo/", "/foo/bar/barfoo////");
366 assert_se(streq_ptr(p, ""));
367
368 p = path_startswith("/foo/bar/barfoo/", "/foo/bar///barfoo/");
369 assert_se(streq_ptr(p, ""));
370
371 p = path_startswith("/foo/bar/barfoo/", "/foo////bar/barfoo/");
372 assert_se(streq_ptr(p, ""));
373
374 p = path_startswith("/foo/bar/barfoo/", "////foo/bar/barfoo/");
375 assert_se(streq_ptr(p, ""));
376
377 p = path_startswith("/foo/bar/barfoo/", "/foo/bar/barfoo");
378 assert_se(streq_ptr(p, ""));
5895b62f
RC
379
380 assert_se(!path_startswith("/foo/bar/barfoo/", "/foo/bar/barfooa/"));
381 assert_se(!path_startswith("/foo/bar/barfoo/", "/foo/bar/barfooa"));
382 assert_se(!path_startswith("/foo/bar/barfoo/", ""));
383 assert_se(!path_startswith("/foo/bar/barfoo/", "/bar/foo"));
384 assert_se(!path_startswith("/foo/bar/barfoo/", "/f/b/b/"));
385}
386
1d13f648
LP
387static void test_prefix_root_one(const char *r, const char *p, const char *expected) {
388 _cleanup_free_ char *s = NULL;
389 const char *t;
390
c6134d3e
LP
391 assert_se(s = path_join(r, p));
392 assert_se(path_equal_ptr(s, expected));
1d13f648
LP
393
394 t = prefix_roota(r, p);
395 assert_se(t);
c6134d3e 396 assert_se(path_equal_ptr(t, expected));
1d13f648
LP
397}
398
399static void test_prefix_root(void) {
400 test_prefix_root_one("/", "/foo", "/foo");
401 test_prefix_root_one(NULL, "/foo", "/foo");
402 test_prefix_root_one("", "/foo", "/foo");
403 test_prefix_root_one("///", "/foo", "/foo");
404 test_prefix_root_one("/", "////foo", "/foo");
405 test_prefix_root_one(NULL, "////foo", "/foo");
f9421dd8
YW
406 test_prefix_root_one("/", "foo", "/foo");
407 test_prefix_root_one("", "foo", "foo");
408 test_prefix_root_one(NULL, "foo", "foo");
1d13f648
LP
409
410 test_prefix_root_one("/foo", "/bar", "/foo/bar");
411 test_prefix_root_one("/foo", "bar", "/foo/bar");
412 test_prefix_root_one("foo", "bar", "foo/bar");
413 test_prefix_root_one("/foo/", "/bar", "/foo/bar");
414 test_prefix_root_one("/foo/", "//bar", "/foo/bar");
415 test_prefix_root_one("/foo///", "//bar", "/foo/bar");
416}
417
63292663
RC
418static void test_file_in_same_dir(void) {
419 char *t;
420
421 t = file_in_same_dir("/", "a");
422 assert_se(streq(t, "/a"));
423 free(t);
424
425 t = file_in_same_dir("/", "/a");
426 assert_se(streq(t, "/a"));
427 free(t);
428
429 t = file_in_same_dir("", "a");
430 assert_se(streq(t, "a"));
431 free(t);
432
433 t = file_in_same_dir("a/", "a");
434 assert_se(streq(t, "a/a"));
435 free(t);
436
437 t = file_in_same_dir("bar/foo", "bar");
438 assert_se(streq(t, "bar/bar"));
439 free(t);
440}
441
b12d25a8 442static void test_last_path_component(void) {
77e0a1b5 443 assert_se(last_path_component(NULL) == NULL);
b12d25a8
ZJS
444 assert_se(streq(last_path_component("a/b/c"), "c"));
445 assert_se(streq(last_path_component("a/b/c/"), "c/"));
446 assert_se(streq(last_path_component("/"), "/"));
447 assert_se(streq(last_path_component("//"), "/"));
448 assert_se(streq(last_path_component("///"), "/"));
449 assert_se(streq(last_path_component("."), "."));
450 assert_se(streq(last_path_component("./."), "."));
451 assert_se(streq(last_path_component("././"), "./"));
452 assert_se(streq(last_path_component("././/"), ".//"));
453 assert_se(streq(last_path_component("/foo/a"), "a"));
454 assert_se(streq(last_path_component("/foo/a/"), "a/"));
69f9ccf1 455 assert_se(streq(last_path_component(""), ""));
8460289f
LP
456 assert_se(streq(last_path_component("a"), "a"));
457 assert_se(streq(last_path_component("a/"), "a/"));
458 assert_se(streq(last_path_component("/a"), "a"));
459 assert_se(streq(last_path_component("/a/"), "a/"));
b12d25a8
ZJS
460}
461
a60c8eee
LP
462static void test_path_extract_filename_one(const char *input, const char *output, int ret) {
463 _cleanup_free_ char *k = NULL;
464 int r;
465
466 r = path_extract_filename(input, &k);
4bbccb02 467 log_info("%s → %s/%s [expected: %s/%s]", strnull(input), strnull(k), strerror_safe(r), strnull(output), strerror_safe(ret));
a60c8eee
LP
468 assert_se(streq_ptr(k, output));
469 assert_se(r == ret);
470}
471
472static void test_path_extract_filename(void) {
473 test_path_extract_filename_one(NULL, NULL, -EINVAL);
474 test_path_extract_filename_one("a/b/c", "c", 0);
475 test_path_extract_filename_one("a/b/c/", "c", 0);
476 test_path_extract_filename_one("/", NULL, -EINVAL);
477 test_path_extract_filename_one("//", NULL, -EINVAL);
478 test_path_extract_filename_one("///", NULL, -EINVAL);
479 test_path_extract_filename_one(".", NULL, -EINVAL);
480 test_path_extract_filename_one("./.", NULL, -EINVAL);
481 test_path_extract_filename_one("././", NULL, -EINVAL);
482 test_path_extract_filename_one("././/", NULL, -EINVAL);
483 test_path_extract_filename_one("/foo/a", "a", 0);
484 test_path_extract_filename_one("/foo/a/", "a", 0);
485 test_path_extract_filename_one("", NULL, -EINVAL);
486 test_path_extract_filename_one("a", "a", 0);
487 test_path_extract_filename_one("a/", "a", 0);
488 test_path_extract_filename_one("/a", "a", 0);
489 test_path_extract_filename_one("/a/", "a", 0);
490 test_path_extract_filename_one("/////////////a/////////////", "a", 0);
491 test_path_extract_filename_one("xx/.", NULL, -EINVAL);
492 test_path_extract_filename_one("xx/..", NULL, -EINVAL);
493 test_path_extract_filename_one("..", NULL, -EINVAL);
494 test_path_extract_filename_one("/..", NULL, -EINVAL);
495 test_path_extract_filename_one("../", NULL, -EINVAL);
496 test_path_extract_filename_one(".", NULL, -EINVAL);
497 test_path_extract_filename_one("/.", NULL, -EINVAL);
498 test_path_extract_filename_one("./", NULL, -EINVAL);
499}
500
63292663
RC
501static void test_filename_is_valid(void) {
502 char foo[FILENAME_MAX+2];
503 int i;
504
505 assert_se(!filename_is_valid(""));
506 assert_se(!filename_is_valid("/bar/foo"));
507 assert_se(!filename_is_valid("/"));
508 assert_se(!filename_is_valid("."));
509 assert_se(!filename_is_valid(".."));
510
511 for (i=0; i<FILENAME_MAX+1; i++)
512 foo[i] = 'a';
513 foo[FILENAME_MAX+1] = '\0';
514
515 assert_se(!filename_is_valid(foo));
516
517 assert_se(filename_is_valid("foo_bar-333"));
518 assert_se(filename_is_valid("o.o"));
519}
520
b05b9cde
ZJS
521static void test_hidden_or_backup_file(void) {
522 assert_se(hidden_or_backup_file(".hidden"));
523 assert_se(hidden_or_backup_file("..hidden"));
524 assert_se(!hidden_or_backup_file("hidden."));
525
526 assert_se(hidden_or_backup_file("backup~"));
527 assert_se(hidden_or_backup_file(".backup~"));
528
529 assert_se(hidden_or_backup_file("lost+found"));
530 assert_se(hidden_or_backup_file("aquota.user"));
531 assert_se(hidden_or_backup_file("aquota.group"));
532
533 assert_se(hidden_or_backup_file("test.rpmnew"));
534 assert_se(hidden_or_backup_file("test.dpkg-old"));
535 assert_se(hidden_or_backup_file("test.dpkg-remove"));
536 assert_se(hidden_or_backup_file("test.swp"));
537
538 assert_se(!hidden_or_backup_file("test.rpmnew."));
539 assert_se(!hidden_or_backup_file("test.dpkg-old.foo"));
540}
541
5a46d55f
ZJS
542static void test_systemd_installation_has_version(const char *path) {
543 int r;
a67c318d 544 const unsigned versions[] = {0, 231, PROJECT_VERSION, 999};
5a46d55f
ZJS
545 unsigned i;
546
547 for (i = 0; i < ELEMENTSOF(versions); i++) {
548 r = systemd_installation_has_version(path, versions[i]);
549 assert_se(r >= 0);
550 log_info("%s has systemd >= %u: %s",
551 path ?: "Current installation", versions[i], yes_no(r));
552 }
553}
554
a119ec7c
LP
555static void test_skip_dev_prefix(void) {
556
557 assert_se(streq(skip_dev_prefix("/"), "/"));
558 assert_se(streq(skip_dev_prefix("/dev"), ""));
559 assert_se(streq(skip_dev_prefix("/dev/"), ""));
560 assert_se(streq(skip_dev_prefix("/dev/foo"), "foo"));
561 assert_se(streq(skip_dev_prefix("/dev/foo/bar"), "foo/bar"));
562 assert_se(streq(skip_dev_prefix("//dev"), ""));
563 assert_se(streq(skip_dev_prefix("//dev//"), ""));
564 assert_se(streq(skip_dev_prefix("/dev///foo"), "foo"));
565 assert_se(streq(skip_dev_prefix("///dev///foo///bar"), "foo///bar"));
566 assert_se(streq(skip_dev_prefix("//foo"), "//foo"));
567 assert_se(streq(skip_dev_prefix("foo"), "foo"));
568}
569
57ea45e1
LP
570static void test_empty_or_root(void) {
571 assert_se(empty_or_root(NULL));
572 assert_se(empty_or_root(""));
573 assert_se(empty_or_root("/"));
574 assert_se(empty_or_root("//"));
575 assert_se(empty_or_root("///"));
576 assert_se(empty_or_root("/////////////////"));
577 assert_se(!empty_or_root("xxx"));
578 assert_se(!empty_or_root("/xxx"));
579 assert_se(!empty_or_root("/xxx/"));
580 assert_se(!empty_or_root("//yy//"));
581}
582
d898ed65
LP
583static void test_path_startswith_set(void) {
584
585 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/foo/bar", "/zzz"), ""));
586 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/foo/", "/zzz"), "bar"));
587 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/foo", "/zzz"), "bar"));
588 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/", "/zzz"), "foo/bar"));
589 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "", "/zzz"), NULL));
590
591 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/foo/bar", "/zzz"), NULL));
592 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/foo/", "/zzz"), "bar2"));
593 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/foo", "/zzz"), "bar2"));
594 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "/", "/zzz"), "foo/bar2"));
595 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar2", "/foo/quux", "", "/zzz"), NULL));
596
597 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/foo/bar", "/zzz"), NULL));
598 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/foo/", "/zzz"), NULL));
599 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/foo", "/zzz"), NULL));
600 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "/", "/zzz"), "foo2/bar"));
601 assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "", "/zzz"), NULL));
602}
603
7f076504 604int main(int argc, char **argv) {
6d7c4033 605 test_setup_logging(LOG_DEBUG);
5a46d55f 606
76877b46 607 test_path();
84e72b5e 608 test_path_equal_root();
85eca92e 609 test_find_binary(argv[0]);
fecffe5d 610 test_prefixes();
0c6ea3a4 611 test_path_join();
eb66db55 612 test_fsck_exists();
6b56a651 613 test_make_relative();
3e8a78c8 614 test_strv_resolve();
5895b62f 615 test_path_startswith();
1d13f648 616 test_prefix_root();
63292663 617 test_file_in_same_dir();
b12d25a8 618 test_last_path_component();
a60c8eee 619 test_path_extract_filename();
63292663 620 test_filename_is_valid();
b05b9cde 621 test_hidden_or_backup_file();
a119ec7c 622 test_skip_dev_prefix();
57ea45e1 623 test_empty_or_root();
d898ed65 624 test_path_startswith_set();
5895b62f 625
5a46d55f
ZJS
626 test_systemd_installation_has_version(argv[1]); /* NULL is OK */
627
76877b46
ZJS
628 return 0;
629}