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