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