]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-path-util.c
udev-builtin-blkid: Use _cleanup_blkid_free_probe_ to free probe (#6108)
[thirdparty/systemd.git] / src / test / test-path-util.c
CommitLineData
76877b46
ZJS
1/***
2 This file is part of systemd.
3
4 Copyright 2013 Zbigniew Jędrzejewski-Szmek
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
a696dbef 20#include <stdio.h>
5d409034 21#include <sys/mount.h>
07630cea 22#include <unistd.h>
a696dbef 23
b5efdb8a 24#include "alloc-util.h"
3ffd4af2 25#include "fd-util.h"
76877b46 26#include "macro.h"
4349cd7c 27#include "mount-util.h"
07630cea 28#include "path-util.h"
c6878637 29#include "rm-rf.h"
84e72b5e 30#include "stat-util.h"
07630cea
LP
31#include "string-util.h"
32#include "strv.h"
33#include "util.h"
76877b46 34
2230852b
MS
35#define test_path_compare(a, b, result) { \
36 assert_se(path_compare(a, b) == result); \
37 assert_se(path_compare(b, a) == -result); \
38 assert_se(path_equal(a, b) == !result); \
39 assert_se(path_equal(b, a) == !result); \
40 }
76877b46
ZJS
41
42static void test_path(void) {
3f72b427
LP
43 _cleanup_close_ int fd = -1;
44
2230852b
MS
45 test_path_compare("/goo", "/goo", 0);
46 test_path_compare("/goo", "/goo", 0);
47 test_path_compare("//goo", "/goo", 0);
48 test_path_compare("//goo/////", "/goo", 0);
49 test_path_compare("goo/////", "goo", 0);
76877b46 50
2230852b
MS
51 test_path_compare("/goo/boo", "/goo//boo", 0);
52 test_path_compare("//goo/boo", "/goo/boo//", 0);
76877b46 53
2230852b 54 test_path_compare("/", "///", 0);
76877b46 55
2230852b
MS
56 test_path_compare("/x", "x/", 1);
57 test_path_compare("x/", "/", -1);
76877b46 58
2230852b
MS
59 test_path_compare("/x/./y", "x/y", 1);
60 test_path_compare("x/.y", "x/y", -1);
61
62 test_path_compare("foo", "/foo", -1);
63 test_path_compare("/foo", "/foo/bar", -1);
64 test_path_compare("/foo/aaa", "/foo/b", -1);
65 test_path_compare("/foo/aaa", "/foo/b/a", -1);
66 test_path_compare("/foo/a", "/foo/aaa", -1);
67 test_path_compare("/foo/a/b", "/foo/aaa", -1);
76877b46
ZJS
68
69 assert_se(path_is_absolute("/"));
70 assert_se(!path_is_absolute("./"));
71
72 assert_se(is_path("/dir"));
73 assert_se(is_path("a/b"));
74 assert_se(!is_path("."));
75
2b6bf07d
ZJS
76 assert_se(streq(basename("./aa/bb/../file.da."), "file.da."));
77 assert_se(streq(basename("/aa///.file"), ".file"));
78 assert_se(streq(basename("/aa///file..."), "file..."));
79 assert_se(streq(basename("file.../"), ""));
76877b46 80
3f72b427
LP
81 fd = open("/", O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOCTTY);
82 assert_se(fd >= 0);
5d409034 83 assert_se(fd_is_mount_point(fd, "/", 0) > 0);
76877b46
ZJS
84
85 {
86 char p1[] = "aaa/bbb////ccc";
87 char p2[] = "//aaa/.////ccc";
88 char p3[] = "/./";
89
8d95631e
FB
90 assert_se(path_equal(path_kill_slashes(p1), "aaa/bbb/ccc"));
91 assert_se(path_equal(path_kill_slashes(p2), "/aaa/./ccc"));
92 assert_se(path_equal(path_kill_slashes(p3), "/./"));
76877b46 93 }
3ae5990c
ZJS
94
95 assert_se(PATH_IN_SET("/bin", "/", "/bin", "/foo"));
96 assert_se(PATH_IN_SET("/bin", "/bin"));
97 assert_se(PATH_IN_SET("/bin", "/foo/bar", "/bin"));
98 assert_se(PATH_IN_SET("/", "/", "/", "/foo/bar"));
99 assert_se(!PATH_IN_SET("/", "/abc", "/def"));
24737c29
ZJS
100
101 assert_se(path_equal_ptr(NULL, NULL));
102 assert_se(path_equal_ptr("/a", "/a"));
103 assert_se(!path_equal_ptr("/a", "/b"));
104 assert_se(!path_equal_ptr("/a", NULL));
105 assert_se(!path_equal_ptr(NULL, "/a"));
76877b46
ZJS
106}
107
84e72b5e
ZJS
108static void test_path_equal_root(void) {
109 /* Nail down the details of how path_equal("/", ...) works. */
110
111 assert_se(path_equal("/", "/"));
112 assert_se(path_equal("/", "//"));
113
114 assert_se(!path_equal("/", "/./"));
115 assert_se(!path_equal("/", "/../"));
116
117 assert_se(!path_equal("/", "/.../"));
118
119 /* Make sure that files_same works as expected. */
120
121 assert_se(files_same("/", "/") > 0);
122 assert_se(files_same("/", "//") > 0);
123
124 assert_se(files_same("/", "/./") > 0);
125 assert_se(files_same("/", "/../") > 0);
126
127 assert_se(files_same("/", "/.../") == -ENOENT);
128
129 /* The same for path_equal_or_files_same. */
130
131 assert_se(path_equal_or_files_same("/", "/"));
132 assert_se(path_equal_or_files_same("/", "//"));
133
134 assert_se(path_equal_or_files_same("/", "/./"));
135 assert_se(path_equal_or_files_same("/", "/../"));
136
137 assert_se(!path_equal_or_files_same("/", "/.../"));
138}
139
85eca92e 140static void test_find_binary(const char *self) {
c9d954b2
ZJS
141 char *p;
142
85eca92e 143 assert_se(find_binary("/bin/sh", &p) == 0);
c9d954b2 144 puts(p);
85eca92e 145 assert_se(path_equal(p, "/bin/sh"));
c9d954b2
ZJS
146 free(p);
147
85eca92e 148 assert_se(find_binary(self, &p) == 0);
c9d954b2 149 puts(p);
6d1e2ddd
MG
150 /* libtool might prefix the binary name with "lt-" */
151 assert_se(endswith(p, "/lt-test-path-util") || endswith(p, "/test-path-util"));
8d95631e 152 assert_se(path_is_absolute(p));
c9d954b2
ZJS
153 free(p);
154
85eca92e 155 assert_se(find_binary("sh", &p) == 0);
c9d954b2 156 puts(p);
8d95631e
FB
157 assert_se(endswith(p, "/sh"));
158 assert_se(path_is_absolute(p));
c9d954b2
ZJS
159 free(p);
160
85eca92e
LP
161 assert_se(find_binary("xxxx-xxxx", &p) == -ENOENT);
162 assert_se(find_binary("/some/dir/xxxx-xxxx", &p) == -ENOENT);
c9d954b2
ZJS
163}
164
fecffe5d 165static void test_prefixes(void) {
e203f7c3
LP
166 static const char* values[] = { "/a/b/c/d", "/a/b/c", "/a/b", "/a", "", NULL};
167 unsigned i;
fecffe5d 168 char s[PATH_MAX];
e203f7c3 169 bool b;
fecffe5d 170
e203f7c3
LP
171 i = 0;
172 PATH_FOREACH_PREFIX_MORE(s, "/a/b/c/d") {
fecffe5d
LP
173 log_error("---%s---", s);
174 assert_se(streq(s, values[i++]));
175 }
e203f7c3 176 assert_se(values[i] == NULL);
fecffe5d 177
e203f7c3
LP
178 i = 1;
179 PATH_FOREACH_PREFIX(s, "/a/b/c/d") {
180 log_error("---%s---", s);
181 assert_se(streq(s, values[i++]));
182 }
fecffe5d
LP
183 assert_se(values[i] == NULL);
184
185 i = 0;
e203f7c3 186 PATH_FOREACH_PREFIX_MORE(s, "////a////b////c///d///////")
fecffe5d 187 assert_se(streq(s, values[i++]));
e203f7c3 188 assert_se(values[i] == NULL);
fecffe5d 189
e203f7c3
LP
190 i = 1;
191 PATH_FOREACH_PREFIX(s, "////a////b////c///d///////")
192 assert_se(streq(s, values[i++]));
fecffe5d
LP
193 assert_se(values[i] == NULL);
194
195 PATH_FOREACH_PREFIX(s, "////")
e203f7c3
LP
196 assert_not_reached("Wut?");
197
198 b = false;
199 PATH_FOREACH_PREFIX_MORE(s, "////") {
200 assert_se(!b);
fecffe5d 201 assert_se(streq(s, ""));
e203f7c3
LP
202 b = true;
203 }
204 assert_se(b);
fecffe5d
LP
205
206 PATH_FOREACH_PREFIX(s, "")
207 assert_not_reached("wut?");
208
e203f7c3
LP
209 b = false;
210 PATH_FOREACH_PREFIX_MORE(s, "") {
8d95631e
FB
211 assert_se(!b);
212 assert_se(streq(s, ""));
e203f7c3
LP
213 b = true;
214 }
fecffe5d
LP
215}
216
0c6ea3a4 217static void test_path_join(void) {
59ae3a95
TA
218
219#define test_join(root, path, rest, expected) { \
220 _cleanup_free_ char *z = NULL; \
221 z = path_join(root, path, rest); \
222 assert_se(streq(z, expected)); \
223 }
224
225 test_join("/root", "/a/b", "/c", "/root/a/b/c");
226 test_join("/root", "a/b", "c", "/root/a/b/c");
227 test_join("/root", "/a/b", "c", "/root/a/b/c");
bc854dc7 228 test_join("/root", "/", "c", "/root/c");
59ae3a95
TA
229 test_join("/root", "/", NULL, "/root/");
230
231 test_join(NULL, "/a/b", "/c", "/a/b/c");
232 test_join(NULL, "a/b", "c", "a/b/c");
233 test_join(NULL, "/a/b", "c", "/a/b/c");
bc854dc7 234 test_join(NULL, "/", "c", "/c");
59ae3a95 235 test_join(NULL, "/", NULL, "/");
0c6ea3a4
ZJS
236}
237
eb66db55
MG
238static void test_fsck_exists(void) {
239 /* Ensure we use a sane default for PATH. */
240 unsetenv("PATH");
241
242 /* fsck.minix is provided by util-linux and will probably exist. */
85eca92e 243 assert_se(fsck_exists("minix") == 1);
eb66db55 244
85eca92e
LP
245 assert_se(fsck_exists("AbCdE") == 0);
246 assert_se(fsck_exists("/../bin/") == 0);
eb66db55
MG
247}
248
6b56a651
TK
249static void test_make_relative(void) {
250 char *result;
251
252 assert_se(path_make_relative("some/relative/path", "/some/path", &result) < 0);
253 assert_se(path_make_relative("/some/path", "some/relative/path", &result) < 0);
254
59ae3a95
TA
255#define test(from_dir, to_path, expected) { \
256 _cleanup_free_ char *z = NULL; \
257 path_make_relative(from_dir, to_path, &z); \
258 assert_se(streq(z, expected)); \
6b56a651
TK
259 }
260
261 test("/", "/", ".");
262 test("/", "/some/path", "some/path");
263 test("/some/path", "/some/path", ".");
264 test("/some/path", "/some/path/in/subdir", "in/subdir");
265 test("/some/path", "/", "../..");
266 test("/some/path", "/some/other/path", "../other/path");
267 test("//extra/////slashes///won't////fool///anybody//", "////extra///slashes////are/just///fine///", "../../../are/just/fine");
268}
269
3e8a78c8
MM
270static void test_strv_resolve(void) {
271 char tmp_dir[] = "/tmp/test-path-util-XXXXXX";
272 _cleanup_strv_free_ char **search_dirs = NULL;
273 _cleanup_strv_free_ char **absolute_dirs = NULL;
274 char **d;
275
276 assert_se(mkdtemp(tmp_dir) != NULL);
277
278 search_dirs = strv_new("/dir1", "/dir2", "/dir3", NULL);
279 assert_se(search_dirs);
280 STRV_FOREACH(d, search_dirs) {
281 char *p = strappend(tmp_dir, *d);
282 assert_se(p);
283 assert_se(strv_push(&absolute_dirs, p) == 0);
284 }
285
286 assert_se(mkdir(absolute_dirs[0], 0700) == 0);
287 assert_se(mkdir(absolute_dirs[1], 0700) == 0);
288 assert_se(symlink("dir2", absolute_dirs[2]) == 0);
289
290 path_strv_resolve(search_dirs, tmp_dir);
291 assert_se(streq(search_dirs[0], "/dir1"));
292 assert_se(streq(search_dirs[1], "/dir2"));
293 assert_se(streq(search_dirs[2], "/dir2"));
294
c6878637 295 assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
3e8a78c8
MM
296}
297
5895b62f 298static void test_path_startswith(void) {
0470289b
ZJS
299 const char *p;
300
301 p = path_startswith("/foo/bar/barfoo/", "/foo");
302 assert_se(streq_ptr(p, "bar/barfoo/"));
303
304 p = path_startswith("/foo/bar/barfoo/", "/foo/");
305 assert_se(streq_ptr(p, "bar/barfoo/"));
306
307 p = path_startswith("/foo/bar/barfoo/", "/");
308 assert_se(streq_ptr(p, "foo/bar/barfoo/"));
309
310 p = path_startswith("/foo/bar/barfoo/", "////");
311 assert_se(streq_ptr(p, "foo/bar/barfoo/"));
312
313 p = path_startswith("/foo/bar/barfoo/", "/foo//bar/////barfoo///");
314 assert_se(streq_ptr(p, ""));
315
316 p = path_startswith("/foo/bar/barfoo/", "/foo/bar/barfoo////");
317 assert_se(streq_ptr(p, ""));
318
319 p = path_startswith("/foo/bar/barfoo/", "/foo/bar///barfoo/");
320 assert_se(streq_ptr(p, ""));
321
322 p = path_startswith("/foo/bar/barfoo/", "/foo////bar/barfoo/");
323 assert_se(streq_ptr(p, ""));
324
325 p = path_startswith("/foo/bar/barfoo/", "////foo/bar/barfoo/");
326 assert_se(streq_ptr(p, ""));
327
328 p = path_startswith("/foo/bar/barfoo/", "/foo/bar/barfoo");
329 assert_se(streq_ptr(p, ""));
5895b62f
RC
330
331 assert_se(!path_startswith("/foo/bar/barfoo/", "/foo/bar/barfooa/"));
332 assert_se(!path_startswith("/foo/bar/barfoo/", "/foo/bar/barfooa"));
333 assert_se(!path_startswith("/foo/bar/barfoo/", ""));
334 assert_se(!path_startswith("/foo/bar/barfoo/", "/bar/foo"));
335 assert_se(!path_startswith("/foo/bar/barfoo/", "/f/b/b/"));
336}
337
1d13f648
LP
338static void test_prefix_root_one(const char *r, const char *p, const char *expected) {
339 _cleanup_free_ char *s = NULL;
340 const char *t;
341
342 assert_se(s = prefix_root(r, p));
343 assert_se(streq_ptr(s, expected));
344
345 t = prefix_roota(r, p);
346 assert_se(t);
347 assert_se(streq_ptr(t, expected));
348}
349
350static void test_prefix_root(void) {
351 test_prefix_root_one("/", "/foo", "/foo");
352 test_prefix_root_one(NULL, "/foo", "/foo");
353 test_prefix_root_one("", "/foo", "/foo");
354 test_prefix_root_one("///", "/foo", "/foo");
355 test_prefix_root_one("/", "////foo", "/foo");
356 test_prefix_root_one(NULL, "////foo", "/foo");
357
358 test_prefix_root_one("/foo", "/bar", "/foo/bar");
359 test_prefix_root_one("/foo", "bar", "/foo/bar");
360 test_prefix_root_one("foo", "bar", "foo/bar");
361 test_prefix_root_one("/foo/", "/bar", "/foo/bar");
362 test_prefix_root_one("/foo/", "//bar", "/foo/bar");
363 test_prefix_root_one("/foo///", "//bar", "/foo/bar");
364}
365
5d409034 366static void test_path_is_mount_point(void) {
36908eb8 367 int fd;
5d409034
MP
368 char tmp_dir[] = "/tmp/test-path-is-mount-point-XXXXXX";
369 _cleanup_free_ char *file1 = NULL, *file2 = NULL, *link1 = NULL, *link2 = NULL;
36908eb8
MP
370 _cleanup_free_ char *dir1 = NULL, *dir1file = NULL, *dirlink1 = NULL, *dirlink1file = NULL;
371 _cleanup_free_ char *dir2 = NULL, *dir2file = NULL;
5d409034 372
e1873695
LP
373 assert_se(path_is_mount_point("/", NULL, AT_SYMLINK_FOLLOW) > 0);
374 assert_se(path_is_mount_point("/", NULL, 0) > 0);
5d409034 375
e1873695
LP
376 assert_se(path_is_mount_point("/proc", NULL, AT_SYMLINK_FOLLOW) > 0);
377 assert_se(path_is_mount_point("/proc", NULL, 0) > 0);
5d409034 378
e1873695
LP
379 assert_se(path_is_mount_point("/proc/1", NULL, AT_SYMLINK_FOLLOW) == 0);
380 assert_se(path_is_mount_point("/proc/1", NULL, 0) == 0);
5d409034 381
e1873695
LP
382 assert_se(path_is_mount_point("/sys", NULL, AT_SYMLINK_FOLLOW) > 0);
383 assert_se(path_is_mount_point("/sys", NULL, 0) > 0);
5d409034 384
36908eb8
MP
385 /* we'll create a hierarchy of different kinds of dir/file/link
386 * layouts:
387 *
388 * <tmp>/file1, <tmp>/file2
389 * <tmp>/link1 -> file1, <tmp>/link2 -> file2
390 * <tmp>/dir1/
391 * <tmp>/dir1/file
392 * <tmp>/dirlink1 -> dir1
393 * <tmp>/dirlink1file -> dirlink1/file
394 * <tmp>/dir2/
395 * <tmp>/dir2/file
396 */
397
5d409034
MP
398 /* file mountpoints */
399 assert_se(mkdtemp(tmp_dir) != NULL);
400 file1 = path_join(NULL, tmp_dir, "file1");
401 assert_se(file1);
402 file2 = path_join(NULL, tmp_dir, "file2");
403 assert_se(file2);
404 fd = open(file1, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0664);
405 assert_se(fd > 0);
406 close(fd);
407 fd = open(file2, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0664);
408 assert_se(fd > 0);
409 close(fd);
410 link1 = path_join(NULL, tmp_dir, "link1");
411 assert_se(link1);
412 assert_se(symlink("file1", link1) == 0);
413 link2 = path_join(NULL, tmp_dir, "link2");
414 assert_se(link1);
415 assert_se(symlink("file2", link2) == 0);
416
e1873695
LP
417 assert_se(path_is_mount_point(file1, NULL, AT_SYMLINK_FOLLOW) == 0);
418 assert_se(path_is_mount_point(file1, NULL, 0) == 0);
419 assert_se(path_is_mount_point(link1, NULL, AT_SYMLINK_FOLLOW) == 0);
420 assert_se(path_is_mount_point(link1, NULL, 0) == 0);
5d409034 421
36908eb8
MP
422 /* directory mountpoints */
423 dir1 = path_join(NULL, tmp_dir, "dir1");
424 assert_se(dir1);
425 assert_se(mkdir(dir1, 0755) == 0);
426 dirlink1 = path_join(NULL, tmp_dir, "dirlink1");
427 assert_se(dirlink1);
428 assert_se(symlink("dir1", dirlink1) == 0);
429 dirlink1file = path_join(NULL, tmp_dir, "dirlink1file");
430 assert_se(dirlink1file);
431 assert_se(symlink("dirlink1/file", dirlink1file) == 0);
432 dir2 = path_join(NULL, tmp_dir, "dir2");
433 assert_se(dir2);
434 assert_se(mkdir(dir2, 0755) == 0);
435
e1873695
LP
436 assert_se(path_is_mount_point(dir1, NULL, AT_SYMLINK_FOLLOW) == 0);
437 assert_se(path_is_mount_point(dir1, NULL, 0) == 0);
438 assert_se(path_is_mount_point(dirlink1, NULL, AT_SYMLINK_FOLLOW) == 0);
439 assert_se(path_is_mount_point(dirlink1, NULL, 0) == 0);
36908eb8
MP
440
441 /* file in subdirectory mountpoints */
442 dir1file = path_join(NULL, dir1, "file");
443 assert_se(dir1file);
444 fd = open(dir1file, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0664);
445 assert_se(fd > 0);
446 close(fd);
447
e1873695
LP
448 assert_se(path_is_mount_point(dir1file, NULL, AT_SYMLINK_FOLLOW) == 0);
449 assert_se(path_is_mount_point(dir1file, NULL, 0) == 0);
450 assert_se(path_is_mount_point(dirlink1file, NULL, AT_SYMLINK_FOLLOW) == 0);
451 assert_se(path_is_mount_point(dirlink1file, NULL, 0) == 0);
36908eb8
MP
452
453 /* these tests will only work as root */
5d409034 454 if (mount(file1, file2, NULL, MS_BIND, NULL) >= 0) {
36908eb8
MP
455 int rt, rf, rlt, rlf, rl1t, rl1f;
456
457 /* files */
458 /* capture results in vars, to avoid dangling mounts on failure */
e1873695
LP
459 rf = path_is_mount_point(file2, NULL, 0);
460 rt = path_is_mount_point(file2, NULL, AT_SYMLINK_FOLLOW);
461 rlf = path_is_mount_point(link2, NULL, 0);
462 rlt = path_is_mount_point(link2, NULL, AT_SYMLINK_FOLLOW);
5d409034
MP
463
464 assert_se(umount(file2) == 0);
465
466 assert_se(rf == 1);
467 assert_se(rt == 1);
468 assert_se(rlf == 0);
469 assert_se(rlt == 1);
36908eb8
MP
470
471 /* dirs */
472 dir2file = path_join(NULL, dir2, "file");
473 assert_se(dir2file);
474 fd = open(dir2file, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0664);
475 assert_se(fd > 0);
476 close(fd);
477
478 assert_se(mount(dir2, dir1, NULL, MS_BIND, NULL) >= 0);
479
e1873695
LP
480 rf = path_is_mount_point(dir1, NULL, 0);
481 rt = path_is_mount_point(dir1, NULL, AT_SYMLINK_FOLLOW);
482 rlf = path_is_mount_point(dirlink1, NULL, 0);
483 rlt = path_is_mount_point(dirlink1, NULL, AT_SYMLINK_FOLLOW);
36908eb8 484 /* its parent is a mount point, but not /file itself */
e1873695
LP
485 rl1f = path_is_mount_point(dirlink1file, NULL, 0);
486 rl1t = path_is_mount_point(dirlink1file, NULL, AT_SYMLINK_FOLLOW);
36908eb8
MP
487
488 assert_se(umount(dir1) == 0);
489
490 assert_se(rf == 1);
491 assert_se(rt == 1);
492 assert_se(rlf == 0);
493 assert_se(rlt == 1);
494 assert_se(rl1f == 0);
495 assert_se(rl1t == 0);
496
5d409034
MP
497 } else
498 printf("Skipping bind mount file test: %m\n");
499
500 assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
501}
502
63292663
RC
503static void test_file_in_same_dir(void) {
504 char *t;
505
506 t = file_in_same_dir("/", "a");
507 assert_se(streq(t, "/a"));
508 free(t);
509
510 t = file_in_same_dir("/", "/a");
511 assert_se(streq(t, "/a"));
512 free(t);
513
514 t = file_in_same_dir("", "a");
515 assert_se(streq(t, "a"));
516 free(t);
517
518 t = file_in_same_dir("a/", "a");
519 assert_se(streq(t, "a/a"));
520 free(t);
521
522 t = file_in_same_dir("bar/foo", "bar");
523 assert_se(streq(t, "bar/bar"));
524 free(t);
525}
526
527static void test_filename_is_valid(void) {
528 char foo[FILENAME_MAX+2];
529 int i;
530
531 assert_se(!filename_is_valid(""));
532 assert_se(!filename_is_valid("/bar/foo"));
533 assert_se(!filename_is_valid("/"));
534 assert_se(!filename_is_valid("."));
535 assert_se(!filename_is_valid(".."));
536
537 for (i=0; i<FILENAME_MAX+1; i++)
538 foo[i] = 'a';
539 foo[FILENAME_MAX+1] = '\0';
540
541 assert_se(!filename_is_valid(foo));
542
543 assert_se(filename_is_valid("foo_bar-333"));
544 assert_se(filename_is_valid("o.o"));
545}
546
b05b9cde
ZJS
547static void test_hidden_or_backup_file(void) {
548 assert_se(hidden_or_backup_file(".hidden"));
549 assert_se(hidden_or_backup_file("..hidden"));
550 assert_se(!hidden_or_backup_file("hidden."));
551
552 assert_se(hidden_or_backup_file("backup~"));
553 assert_se(hidden_or_backup_file(".backup~"));
554
555 assert_se(hidden_or_backup_file("lost+found"));
556 assert_se(hidden_or_backup_file("aquota.user"));
557 assert_se(hidden_or_backup_file("aquota.group"));
558
559 assert_se(hidden_or_backup_file("test.rpmnew"));
560 assert_se(hidden_or_backup_file("test.dpkg-old"));
561 assert_se(hidden_or_backup_file("test.dpkg-remove"));
562 assert_se(hidden_or_backup_file("test.swp"));
563
564 assert_se(!hidden_or_backup_file("test.rpmnew."));
565 assert_se(!hidden_or_backup_file("test.dpkg-old.foo"));
566}
567
5a46d55f
ZJS
568static void test_systemd_installation_has_version(const char *path) {
569 int r;
570 const unsigned versions[] = {0, 231, atoi(PACKAGE_VERSION), 999};
571 unsigned i;
572
573 for (i = 0; i < ELEMENTSOF(versions); i++) {
574 r = systemd_installation_has_version(path, versions[i]);
575 assert_se(r >= 0);
576 log_info("%s has systemd >= %u: %s",
577 path ?: "Current installation", versions[i], yes_no(r));
578 }
579}
580
7f076504 581int main(int argc, char **argv) {
5a46d55f
ZJS
582 log_set_max_level(LOG_DEBUG);
583 log_parse_environment();
584 log_open();
585
76877b46 586 test_path();
84e72b5e 587 test_path_equal_root();
85eca92e 588 test_find_binary(argv[0]);
fecffe5d 589 test_prefixes();
0c6ea3a4 590 test_path_join();
eb66db55 591 test_fsck_exists();
6b56a651 592 test_make_relative();
3e8a78c8 593 test_strv_resolve();
5895b62f 594 test_path_startswith();
1d13f648 595 test_prefix_root();
5d409034 596 test_path_is_mount_point();
63292663
RC
597 test_file_in_same_dir();
598 test_filename_is_valid();
b05b9cde 599 test_hidden_or_backup_file();
5895b62f 600
5a46d55f
ZJS
601 test_systemd_installation_has_version(argv[1]); /* NULL is OK */
602
76877b46
ZJS
603 return 0;
604}