1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2013 Zbigniew Jędrzejewski-Szmek
11 #include "alloc-util.h"
14 #include "mount-util.h"
15 #include "path-util.h"
17 #include "stat-util.h"
18 #include "string-util.h"
22 #define test_path_compare(a, b, result) { \
23 assert_se(path_compare(a, b) == result); \
24 assert_se(path_compare(b, a) == -result); \
25 assert_se(path_equal(a, b) == !result); \
26 assert_se(path_equal(b, a) == !result); \
29 static void test_path_simplify(const char *in, const char *out, const char *out_dot) {
33 assert_se(streq(path_simplify(p, false), out));
36 assert_se(streq(path_simplify(p, true), out_dot));
39 static void test_path(void) {
40 _cleanup_close_ int fd = -1;
42 test_path_compare("/goo", "/goo", 0);
43 test_path_compare("/goo", "/goo", 0);
44 test_path_compare("//goo", "/goo", 0);
45 test_path_compare("//goo/////", "/goo", 0);
46 test_path_compare("goo/////", "goo", 0);
48 test_path_compare("/goo/boo", "/goo//boo", 0);
49 test_path_compare("//goo/boo", "/goo/boo//", 0);
51 test_path_compare("/", "///", 0);
53 test_path_compare("/x", "x/", 1);
54 test_path_compare("x/", "/", -1);
56 test_path_compare("/x/./y", "x/y", 1);
57 test_path_compare("x/.y", "x/y", -1);
59 test_path_compare("foo", "/foo", -1);
60 test_path_compare("/foo", "/foo/bar", -1);
61 test_path_compare("/foo/aaa", "/foo/b", -1);
62 test_path_compare("/foo/aaa", "/foo/b/a", -1);
63 test_path_compare("/foo/a", "/foo/aaa", -1);
64 test_path_compare("/foo/a/b", "/foo/aaa", -1);
66 assert_se(path_is_absolute("/"));
67 assert_se(!path_is_absolute("./"));
69 assert_se(is_path("/dir"));
70 assert_se(is_path("a/b"));
71 assert_se(!is_path("."));
73 assert_se(streq(basename("./aa/bb/../file.da."), "file.da."));
74 assert_se(streq(basename("/aa///.file"), ".file"));
75 assert_se(streq(basename("/aa///file..."), "file..."));
76 assert_se(streq(basename("file.../"), ""));
78 fd = open("/", O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOCTTY);
80 assert_se(fd_is_mount_point(fd, "/", 0) > 0);
82 test_path_simplify("aaa/bbb////ccc", "aaa/bbb/ccc", "aaa/bbb/ccc");
83 test_path_simplify("//aaa/.////ccc", "/aaa/./ccc", "/aaa/ccc");
84 test_path_simplify("///", "/", "/");
85 test_path_simplify("///.//", "/.", "/");
86 test_path_simplify("///.//.///", "/./.", "/");
87 test_path_simplify("////.././///../.", "/.././../.", "/../..");
88 test_path_simplify(".", ".", "");
89 test_path_simplify("./", ".", "");
90 test_path_simplify(".///.//./.", "./././.", "");
91 test_path_simplify(".///.//././/", "./././.", "");
92 test_path_simplify("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.",
93 "/./aaa/././.bbb/../c./d.dd/..eeee/.",
94 "/aaa/.bbb/../c./d.dd/..eeee");
95 test_path_simplify("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/..",
96 "/./aaa/././.bbb/../c./d.dd/..eeee/..",
97 "/aaa/.bbb/../c./d.dd/..eeee/..");
98 test_path_simplify(".//./aaa///.//./.bbb/..///c.//d.dd///..eeee/..",
99 "././aaa/././.bbb/../c./d.dd/..eeee/..",
100 "aaa/.bbb/../c./d.dd/..eeee/..");
101 test_path_simplify("..//./aaa///.//./.bbb/..///c.//d.dd///..eeee/..",
102 ".././aaa/././.bbb/../c./d.dd/..eeee/..",
103 "../aaa/.bbb/../c./d.dd/..eeee/..");
105 assert_se(PATH_IN_SET("/bin", "/", "/bin", "/foo"));
106 assert_se(PATH_IN_SET("/bin", "/bin"));
107 assert_se(PATH_IN_SET("/bin", "/foo/bar", "/bin"));
108 assert_se(PATH_IN_SET("/", "/", "/", "/foo/bar"));
109 assert_se(!PATH_IN_SET("/", "/abc", "/def"));
111 assert_se(path_equal_ptr(NULL, NULL));
112 assert_se(path_equal_ptr("/a", "/a"));
113 assert_se(!path_equal_ptr("/a", "/b"));
114 assert_se(!path_equal_ptr("/a", NULL));
115 assert_se(!path_equal_ptr(NULL, "/a"));
118 static void test_path_equal_root(void) {
119 /* Nail down the details of how path_equal("/", ...) works. */
121 assert_se(path_equal("/", "/"));
122 assert_se(path_equal("/", "//"));
124 assert_se(!path_equal("/", "/./"));
125 assert_se(!path_equal("/", "/../"));
127 assert_se(!path_equal("/", "/.../"));
129 /* Make sure that files_same works as expected. */
131 assert_se(files_same("/", "/", 0) > 0);
132 assert_se(files_same("/", "/", AT_SYMLINK_NOFOLLOW) > 0);
133 assert_se(files_same("/", "//", 0) > 0);
134 assert_se(files_same("/", "//", AT_SYMLINK_NOFOLLOW) > 0);
136 assert_se(files_same("/", "/./", 0) > 0);
137 assert_se(files_same("/", "/./", AT_SYMLINK_NOFOLLOW) > 0);
138 assert_se(files_same("/", "/../", 0) > 0);
139 assert_se(files_same("/", "/../", AT_SYMLINK_NOFOLLOW) > 0);
141 assert_se(files_same("/", "/.../", 0) == -ENOENT);
142 assert_se(files_same("/", "/.../", AT_SYMLINK_NOFOLLOW) == -ENOENT);
144 /* The same for path_equal_or_files_same. */
146 assert_se(path_equal_or_files_same("/", "/", 0));
147 assert_se(path_equal_or_files_same("/", "/", AT_SYMLINK_NOFOLLOW));
148 assert_se(path_equal_or_files_same("/", "//", 0));
149 assert_se(path_equal_or_files_same("/", "//", AT_SYMLINK_NOFOLLOW));
151 assert_se(path_equal_or_files_same("/", "/./", 0));
152 assert_se(path_equal_or_files_same("/", "/./", AT_SYMLINK_NOFOLLOW));
153 assert_se(path_equal_or_files_same("/", "/../", 0));
154 assert_se(path_equal_or_files_same("/", "/../", AT_SYMLINK_NOFOLLOW));
156 assert_se(!path_equal_or_files_same("/", "/.../", 0));
157 assert_se(!path_equal_or_files_same("/", "/.../", AT_SYMLINK_NOFOLLOW));
160 static void test_find_binary(const char *self) {
163 assert_se(find_binary("/bin/sh", &p) == 0);
165 assert_se(path_equal(p, "/bin/sh"));
168 assert_se(find_binary(self, &p) == 0);
170 /* libtool might prefix the binary name with "lt-" */
171 assert_se(endswith(p, "/lt-test-path-util") || endswith(p, "/test-path-util"));
172 assert_se(path_is_absolute(p));
175 assert_se(find_binary("sh", &p) == 0);
177 assert_se(endswith(p, "/sh"));
178 assert_se(path_is_absolute(p));
181 assert_se(find_binary("xxxx-xxxx", &p) == -ENOENT);
182 assert_se(find_binary("/some/dir/xxxx-xxxx", &p) == -ENOENT);
185 static void test_prefixes(void) {
186 static const char* values[] = { "/a/b/c/d", "/a/b/c", "/a/b", "/a", "", NULL};
192 PATH_FOREACH_PREFIX_MORE(s, "/a/b/c/d") {
193 log_error("---%s---", s);
194 assert_se(streq(s, values[i++]));
196 assert_se(values[i] == NULL);
199 PATH_FOREACH_PREFIX(s, "/a/b/c/d") {
200 log_error("---%s---", s);
201 assert_se(streq(s, values[i++]));
203 assert_se(values[i] == NULL);
206 PATH_FOREACH_PREFIX_MORE(s, "////a////b////c///d///////")
207 assert_se(streq(s, values[i++]));
208 assert_se(values[i] == NULL);
211 PATH_FOREACH_PREFIX(s, "////a////b////c///d///////")
212 assert_se(streq(s, values[i++]));
213 assert_se(values[i] == NULL);
215 PATH_FOREACH_PREFIX(s, "////")
216 assert_not_reached("Wut?");
219 PATH_FOREACH_PREFIX_MORE(s, "////") {
221 assert_se(streq(s, ""));
226 PATH_FOREACH_PREFIX(s, "")
227 assert_not_reached("wut?");
230 PATH_FOREACH_PREFIX_MORE(s, "") {
232 assert_se(streq(s, ""));
237 static void test_path_join(void) {
239 #define test_join(root, path, rest, expected) { \
240 _cleanup_free_ char *z = NULL; \
241 z = path_join(root, path, rest); \
242 assert_se(streq(z, expected)); \
245 test_join("/root", "/a/b", "/c", "/root/a/b/c");
246 test_join("/root", "a/b", "c", "/root/a/b/c");
247 test_join("/root", "/a/b", "c", "/root/a/b/c");
248 test_join("/root", "/", "c", "/root/c");
249 test_join("/root", "/", NULL, "/root/");
251 test_join(NULL, "/a/b", "/c", "/a/b/c");
252 test_join(NULL, "a/b", "c", "a/b/c");
253 test_join(NULL, "/a/b", "c", "/a/b/c");
254 test_join(NULL, "/", "c", "/c");
255 test_join(NULL, "/", NULL, "/");
258 #if 0 /// UNNEEDED by elogind
259 static void test_fsck_exists(void) {
260 /* Ensure we use a sane default for PATH. */
263 /* fsck.minix is provided by util-linux and will probably exist. */
264 assert_se(fsck_exists("minix") == 1);
266 assert_se(fsck_exists("AbCdE") == 0);
267 assert_se(fsck_exists("/../bin/") == 0);
270 static void test_make_relative(void) {
273 assert_se(path_make_relative("some/relative/path", "/some/path", &result) < 0);
274 assert_se(path_make_relative("/some/path", "some/relative/path", &result) < 0);
275 assert_se(path_make_relative("/some/dotdot/../path", "/some/path", &result) < 0);
277 #define test(from_dir, to_path, expected) { \
278 _cleanup_free_ char *z = NULL; \
279 path_make_relative(from_dir, to_path, &z); \
280 assert_se(streq(z, expected)); \
284 test("/", "/some/path", "some/path");
285 test("/some/path", "/some/path", ".");
286 test("/some/path", "/some/path/in/subdir", "in/subdir");
287 test("/some/path", "/", "../..");
288 test("/some/path", "/some/other/path", "../other/path");
289 test("/some/path/./dot", "/some/further/path", "../../further/path");
290 test("//extra.//.//./.slashes//./won't////fo.ol///anybody//", "/././/extra././/.slashes////ar.e/.just/././.fine///", "../../../ar.e/.just/.fine");
294 static void test_strv_resolve(void) {
295 char tmp_dir[] = "/tmp/test-path-util-XXXXXX";
296 _cleanup_strv_free_ char **search_dirs = NULL;
297 _cleanup_strv_free_ char **absolute_dirs = NULL;
300 assert_se(mkdtemp(tmp_dir) != NULL);
302 search_dirs = strv_new("/dir1", "/dir2", "/dir3", NULL);
303 assert_se(search_dirs);
304 STRV_FOREACH(d, search_dirs) {
305 char *p = strappend(tmp_dir, *d);
307 assert_se(strv_push(&absolute_dirs, p) == 0);
310 assert_se(mkdir(absolute_dirs[0], 0700) == 0);
311 assert_se(mkdir(absolute_dirs[1], 0700) == 0);
312 assert_se(symlink("dir2", absolute_dirs[2]) == 0);
314 path_strv_resolve(search_dirs, tmp_dir);
315 assert_se(streq(search_dirs[0], "/dir1"));
316 assert_se(streq(search_dirs[1], "/dir2"));
317 assert_se(streq(search_dirs[2], "/dir2"));
319 assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
322 static void test_path_startswith(void) {
325 p = path_startswith("/foo/bar/barfoo/", "/foo");
326 assert_se(streq_ptr(p, "bar/barfoo/"));
328 p = path_startswith("/foo/bar/barfoo/", "/foo/");
329 assert_se(streq_ptr(p, "bar/barfoo/"));
331 p = path_startswith("/foo/bar/barfoo/", "/");
332 assert_se(streq_ptr(p, "foo/bar/barfoo/"));
334 p = path_startswith("/foo/bar/barfoo/", "////");
335 assert_se(streq_ptr(p, "foo/bar/barfoo/"));
337 p = path_startswith("/foo/bar/barfoo/", "/foo//bar/////barfoo///");
338 assert_se(streq_ptr(p, ""));
340 p = path_startswith("/foo/bar/barfoo/", "/foo/bar/barfoo////");
341 assert_se(streq_ptr(p, ""));
343 p = path_startswith("/foo/bar/barfoo/", "/foo/bar///barfoo/");
344 assert_se(streq_ptr(p, ""));
346 p = path_startswith("/foo/bar/barfoo/", "/foo////bar/barfoo/");
347 assert_se(streq_ptr(p, ""));
349 p = path_startswith("/foo/bar/barfoo/", "////foo/bar/barfoo/");
350 assert_se(streq_ptr(p, ""));
352 p = path_startswith("/foo/bar/barfoo/", "/foo/bar/barfoo");
353 assert_se(streq_ptr(p, ""));
355 assert_se(!path_startswith("/foo/bar/barfoo/", "/foo/bar/barfooa/"));
356 assert_se(!path_startswith("/foo/bar/barfoo/", "/foo/bar/barfooa"));
357 assert_se(!path_startswith("/foo/bar/barfoo/", ""));
358 assert_se(!path_startswith("/foo/bar/barfoo/", "/bar/foo"));
359 assert_se(!path_startswith("/foo/bar/barfoo/", "/f/b/b/"));
362 static void test_prefix_root_one(const char *r, const char *p, const char *expected) {
363 _cleanup_free_ char *s = NULL;
366 assert_se(s = prefix_root(r, p));
367 assert_se(streq_ptr(s, expected));
369 t = prefix_roota(r, p);
371 assert_se(streq_ptr(t, expected));
374 static void test_prefix_root(void) {
375 test_prefix_root_one("/", "/foo", "/foo");
376 test_prefix_root_one(NULL, "/foo", "/foo");
377 test_prefix_root_one("", "/foo", "/foo");
378 test_prefix_root_one("///", "/foo", "/foo");
379 test_prefix_root_one("/", "////foo", "/foo");
380 test_prefix_root_one(NULL, "////foo", "/foo");
382 test_prefix_root_one("/foo", "/bar", "/foo/bar");
383 test_prefix_root_one("/foo", "bar", "/foo/bar");
384 test_prefix_root_one("foo", "bar", "foo/bar");
385 test_prefix_root_one("/foo/", "/bar", "/foo/bar");
386 test_prefix_root_one("/foo/", "//bar", "/foo/bar");
387 test_prefix_root_one("/foo///", "//bar", "/foo/bar");
390 static void test_file_in_same_dir(void) {
393 t = file_in_same_dir("/", "a");
394 assert_se(streq(t, "/a"));
397 t = file_in_same_dir("/", "/a");
398 assert_se(streq(t, "/a"));
401 t = file_in_same_dir("", "a");
402 assert_se(streq(t, "a"));
405 t = file_in_same_dir("a/", "a");
406 assert_se(streq(t, "a/a"));
409 t = file_in_same_dir("bar/foo", "bar");
410 assert_se(streq(t, "bar/bar"));
414 static void test_last_path_component(void) {
415 assert_se(streq(last_path_component("a/b/c"), "c"));
416 assert_se(streq(last_path_component("a/b/c/"), "c/"));
417 assert_se(streq(last_path_component("/"), "/"));
418 assert_se(streq(last_path_component("//"), "/"));
419 assert_se(streq(last_path_component("///"), "/"));
420 assert_se(streq(last_path_component("."), "."));
421 assert_se(streq(last_path_component("./."), "."));
422 assert_se(streq(last_path_component("././"), "./"));
423 assert_se(streq(last_path_component("././/"), ".//"));
424 assert_se(streq(last_path_component("/foo/a"), "a"));
425 assert_se(streq(last_path_component("/foo/a/"), "a/"));
426 assert_se(streq(last_path_component(""), ""));
427 assert_se(streq(last_path_component("a"), "a"));
428 assert_se(streq(last_path_component("a/"), "a/"));
429 assert_se(streq(last_path_component("/a"), "a"));
430 assert_se(streq(last_path_component("/a/"), "a/"));
433 static void test_filename_is_valid(void) {
434 char foo[FILENAME_MAX+2];
437 assert_se(!filename_is_valid(""));
438 assert_se(!filename_is_valid("/bar/foo"));
439 assert_se(!filename_is_valid("/"));
440 assert_se(!filename_is_valid("."));
441 assert_se(!filename_is_valid(".."));
443 for (i=0; i<FILENAME_MAX+1; i++)
445 foo[FILENAME_MAX+1] = '\0';
447 assert_se(!filename_is_valid(foo));
449 assert_se(filename_is_valid("foo_bar-333"));
450 assert_se(filename_is_valid("o.o"));
453 static void test_hidden_or_backup_file(void) {
454 assert_se(hidden_or_backup_file(".hidden"));
455 assert_se(hidden_or_backup_file("..hidden"));
456 assert_se(!hidden_or_backup_file("hidden."));
458 assert_se(hidden_or_backup_file("backup~"));
459 assert_se(hidden_or_backup_file(".backup~"));
461 assert_se(hidden_or_backup_file("lost+found"));
462 assert_se(hidden_or_backup_file("aquota.user"));
463 assert_se(hidden_or_backup_file("aquota.group"));
465 assert_se(hidden_or_backup_file("test.rpmnew"));
466 assert_se(hidden_or_backup_file("test.dpkg-old"));
467 assert_se(hidden_or_backup_file("test.dpkg-remove"));
468 assert_se(hidden_or_backup_file("test.swp"));
470 assert_se(!hidden_or_backup_file("test.rpmnew."));
471 assert_se(!hidden_or_backup_file("test.dpkg-old.foo"));
474 #if 0 /// UNNEEDED by elogind
475 static void test_systemd_installation_has_version(const char *path) {
477 const unsigned versions[] = {0, 231, atoi(PACKAGE_VERSION), 999};
480 for (i = 0; i < ELEMENTSOF(versions); i++) {
481 r = systemd_installation_has_version(path, versions[i]);
483 log_info("%s has systemd >= %u: %s",
484 path ?: "Current installation", versions[i], yes_no(r));
489 static void test_skip_dev_prefix(void) {
491 assert_se(streq(skip_dev_prefix("/"), "/"));
492 assert_se(streq(skip_dev_prefix("/dev"), ""));
493 assert_se(streq(skip_dev_prefix("/dev/"), ""));
494 assert_se(streq(skip_dev_prefix("/dev/foo"), "foo"));
495 assert_se(streq(skip_dev_prefix("/dev/foo/bar"), "foo/bar"));
496 assert_se(streq(skip_dev_prefix("//dev"), ""));
497 assert_se(streq(skip_dev_prefix("//dev//"), ""));
498 assert_se(streq(skip_dev_prefix("/dev///foo"), "foo"));
499 assert_se(streq(skip_dev_prefix("///dev///foo///bar"), "foo///bar"));
500 assert_se(streq(skip_dev_prefix("//foo"), "//foo"));
501 assert_se(streq(skip_dev_prefix("foo"), "foo"));
504 static void test_empty_or_root(void) {
505 assert_se(empty_or_root(NULL));
506 assert_se(empty_or_root(""));
507 assert_se(empty_or_root("/"));
508 assert_se(empty_or_root("//"));
509 assert_se(empty_or_root("///"));
510 assert_se(empty_or_root("/////////////////"));
511 assert_se(!empty_or_root("xxx"));
512 assert_se(!empty_or_root("/xxx"));
513 assert_se(!empty_or_root("/xxx/"));
514 assert_se(!empty_or_root("//yy//"));
517 int main(int argc, char **argv) {
518 log_set_max_level(LOG_DEBUG);
519 log_parse_environment();
523 test_path_equal_root();
524 test_find_binary(argv[0]);
527 #if 0 /// UNNEEDED by elogind
529 test_make_relative();
532 test_path_startswith();
534 test_file_in_same_dir();
535 test_last_path_component();
536 test_filename_is_valid();
537 test_hidden_or_backup_file();
538 test_skip_dev_prefix();
539 test_empty_or_root();
541 #if 0 /// UNNEEDED by elogind
542 test_systemd_installation_has_version(argv[1]); /* NULL is OK */