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