]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-path-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 #include "alloc-util.h"
9 #include "mount-util.h"
10 #include "path-util.h"
12 #include "stat-util.h"
13 #include "string-util.h"
18 #define test_path_compare(a, b, result) { \
19 assert_se(path_compare(a, b) == result); \
20 assert_se(path_compare(b, a) == -result); \
21 assert_se(path_equal(a, b) == !result); \
22 assert_se(path_equal(b, a) == !result); \
25 static void test_path_simplify(const char *in
, const char *out
, const char *out_dot
) {
29 assert_se(streq(path_simplify(p
, false), out
));
32 assert_se(streq(path_simplify(p
, true), out_dot
));
35 static void test_path(void) {
36 _cleanup_close_
int fd
= -1;
38 test_path_compare("/goo", "/goo", 0);
39 test_path_compare("/goo", "/goo", 0);
40 test_path_compare("//goo", "/goo", 0);
41 test_path_compare("//goo/////", "/goo", 0);
42 test_path_compare("goo/////", "goo", 0);
44 test_path_compare("/goo/boo", "/goo//boo", 0);
45 test_path_compare("//goo/boo", "/goo/boo//", 0);
47 test_path_compare("/", "///", 0);
49 test_path_compare("/x", "x/", 1);
50 test_path_compare("x/", "/", -1);
52 test_path_compare("/x/./y", "x/y", 1);
53 test_path_compare("x/.y", "x/y", -1);
55 test_path_compare("foo", "/foo", -1);
56 test_path_compare("/foo", "/foo/bar", -1);
57 test_path_compare("/foo/aaa", "/foo/b", -1);
58 test_path_compare("/foo/aaa", "/foo/b/a", -1);
59 test_path_compare("/foo/a", "/foo/aaa", -1);
60 test_path_compare("/foo/a/b", "/foo/aaa", -1);
62 assert_se(path_is_absolute("/"));
63 assert_se(!path_is_absolute("./"));
65 assert_se(is_path("/dir"));
66 assert_se(is_path("a/b"));
67 assert_se(!is_path("."));
69 assert_se(streq(basename("./aa/bb/../file.da."), "file.da."));
70 assert_se(streq(basename("/aa///.file"), ".file"));
71 assert_se(streq(basename("/aa///file..."), "file..."));
72 assert_se(streq(basename("file.../"), ""));
74 fd
= open("/", O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
|O_NOCTTY
);
76 assert_se(fd_is_mount_point(fd
, "/", 0) > 0);
78 test_path_simplify("aaa/bbb////ccc", "aaa/bbb/ccc", "aaa/bbb/ccc");
79 test_path_simplify("//aaa/.////ccc", "/aaa/./ccc", "/aaa/ccc");
80 test_path_simplify("///", "/", "/");
81 test_path_simplify("///.//", "/.", "/");
82 test_path_simplify("///.//.///", "/./.", "/");
83 test_path_simplify("////.././///../.", "/.././../.", "/../..");
84 test_path_simplify(".", ".", ".");
85 test_path_simplify("./", ".", ".");
86 test_path_simplify(".///.//./.", "./././.", ".");
87 test_path_simplify(".///.//././/", "./././.", ".");
88 test_path_simplify("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.",
89 "/./aaa/././.bbb/../c./d.dd/..eeee/.",
90 "/aaa/.bbb/../c./d.dd/..eeee");
91 test_path_simplify("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/..",
92 "/./aaa/././.bbb/../c./d.dd/..eeee/..",
93 "/aaa/.bbb/../c./d.dd/..eeee/..");
94 test_path_simplify(".//./aaa///.//./.bbb/..///c.//d.dd///..eeee/..",
95 "././aaa/././.bbb/../c./d.dd/..eeee/..",
96 "aaa/.bbb/../c./d.dd/..eeee/..");
97 test_path_simplify("..//./aaa///.//./.bbb/..///c.//d.dd///..eeee/..",
98 ".././aaa/././.bbb/../c./d.dd/..eeee/..",
99 "../aaa/.bbb/../c./d.dd/..eeee/..");
101 assert_se(PATH_IN_SET("/bin", "/", "/bin", "/foo"));
102 assert_se(PATH_IN_SET("/bin", "/bin"));
103 assert_se(PATH_IN_SET("/bin", "/foo/bar", "/bin"));
104 assert_se(PATH_IN_SET("/", "/", "/", "/foo/bar"));
105 assert_se(!PATH_IN_SET("/", "/abc", "/def"));
107 assert_se(path_equal_ptr(NULL
, NULL
));
108 assert_se(path_equal_ptr("/a", "/a"));
109 assert_se(!path_equal_ptr("/a", "/b"));
110 assert_se(!path_equal_ptr("/a", NULL
));
111 assert_se(!path_equal_ptr(NULL
, "/a"));
114 static void test_path_equal_root(void) {
115 /* Nail down the details of how path_equal("/", ...) works. */
117 assert_se(path_equal("/", "/"));
118 assert_se(path_equal("/", "//"));
120 assert_se(!path_equal("/", "/./"));
121 assert_se(!path_equal("/", "/../"));
123 assert_se(!path_equal("/", "/.../"));
125 /* Make sure that files_same works as expected. */
127 assert_se(files_same("/", "/", 0) > 0);
128 assert_se(files_same("/", "/", AT_SYMLINK_NOFOLLOW
) > 0);
129 assert_se(files_same("/", "//", 0) > 0);
130 assert_se(files_same("/", "//", AT_SYMLINK_NOFOLLOW
) > 0);
132 assert_se(files_same("/", "/./", 0) > 0);
133 assert_se(files_same("/", "/./", AT_SYMLINK_NOFOLLOW
) > 0);
134 assert_se(files_same("/", "/../", 0) > 0);
135 assert_se(files_same("/", "/../", AT_SYMLINK_NOFOLLOW
) > 0);
137 assert_se(files_same("/", "/.../", 0) == -ENOENT
);
138 assert_se(files_same("/", "/.../", AT_SYMLINK_NOFOLLOW
) == -ENOENT
);
140 /* The same for path_equal_or_files_same. */
142 assert_se(path_equal_or_files_same("/", "/", 0));
143 assert_se(path_equal_or_files_same("/", "/", AT_SYMLINK_NOFOLLOW
));
144 assert_se(path_equal_or_files_same("/", "//", 0));
145 assert_se(path_equal_or_files_same("/", "//", AT_SYMLINK_NOFOLLOW
));
147 assert_se(path_equal_or_files_same("/", "/./", 0));
148 assert_se(path_equal_or_files_same("/", "/./", AT_SYMLINK_NOFOLLOW
));
149 assert_se(path_equal_or_files_same("/", "/../", 0));
150 assert_se(path_equal_or_files_same("/", "/../", AT_SYMLINK_NOFOLLOW
));
152 assert_se(!path_equal_or_files_same("/", "/.../", 0));
153 assert_se(!path_equal_or_files_same("/", "/.../", AT_SYMLINK_NOFOLLOW
));
156 static void test_find_binary(const char *self
) {
159 assert_se(find_binary("/bin/sh", &p
) == 0);
161 assert_se(path_equal(p
, "/bin/sh"));
164 assert_se(find_binary(self
, &p
) == 0);
166 /* libtool might prefix the binary name with "lt-" */
167 assert_se(endswith(p
, "/lt-test-path-util") || endswith(p
, "/test-path-util"));
168 assert_se(path_is_absolute(p
));
171 assert_se(find_binary("sh", &p
) == 0);
173 assert_se(endswith(p
, "/sh"));
174 assert_se(path_is_absolute(p
));
177 assert_se(find_binary("xxxx-xxxx", &p
) == -ENOENT
);
178 assert_se(find_binary("/some/dir/xxxx-xxxx", &p
) == -ENOENT
);
181 static void test_prefixes(void) {
182 static const char* values
[] = { "/a/b/c/d", "/a/b/c", "/a/b", "/a", "", NULL
};
188 PATH_FOREACH_PREFIX_MORE(s
, "/a/b/c/d") {
189 log_error("---%s---", s
);
190 assert_se(streq(s
, values
[i
++]));
192 assert_se(values
[i
] == NULL
);
195 PATH_FOREACH_PREFIX(s
, "/a/b/c/d") {
196 log_error("---%s---", s
);
197 assert_se(streq(s
, values
[i
++]));
199 assert_se(values
[i
] == NULL
);
202 PATH_FOREACH_PREFIX_MORE(s
, "////a////b////c///d///////")
203 assert_se(streq(s
, values
[i
++]));
204 assert_se(values
[i
] == NULL
);
207 PATH_FOREACH_PREFIX(s
, "////a////b////c///d///////")
208 assert_se(streq(s
, values
[i
++]));
209 assert_se(values
[i
] == NULL
);
211 PATH_FOREACH_PREFIX(s
, "////")
212 assert_not_reached("Wut?");
215 PATH_FOREACH_PREFIX_MORE(s
, "////") {
217 assert_se(streq(s
, ""));
222 PATH_FOREACH_PREFIX(s
, "")
223 assert_not_reached("wut?");
226 PATH_FOREACH_PREFIX_MORE(s
, "") {
228 assert_se(streq(s
, ""));
233 static void test_path_join(void) {
235 #define test_join(root, path, rest, expected) { \
236 _cleanup_free_ char *z = NULL; \
237 z = path_join(root, path, rest); \
238 assert_se(streq(z, expected)); \
241 test_join("/root", "/a/b", "/c", "/root/a/b/c");
242 test_join("/root", "a/b", "c", "/root/a/b/c");
243 test_join("/root", "/a/b", "c", "/root/a/b/c");
244 test_join("/root", "/", "c", "/root/c");
245 test_join("/root", "/", NULL
, "/root/");
247 test_join(NULL
, "/a/b", "/c", "/a/b/c");
248 test_join(NULL
, "a/b", "c", "a/b/c");
249 test_join(NULL
, "/a/b", "c", "/a/b/c");
250 test_join(NULL
, "/", "c", "/c");
251 test_join(NULL
, "/", NULL
, "/");
254 static void test_fsck_exists(void) {
255 /* Ensure we use a sane default for PATH. */
258 /* fsck.minix is provided by util-linux and will probably exist. */
259 assert_se(fsck_exists("minix") == 1);
261 assert_se(fsck_exists("AbCdE") == 0);
262 assert_se(fsck_exists("/../bin/") == 0);
265 static void test_make_relative(void) {
268 assert_se(path_make_relative("some/relative/path", "/some/path", &result
) < 0);
269 assert_se(path_make_relative("/some/path", "some/relative/path", &result
) < 0);
270 assert_se(path_make_relative("/some/dotdot/../path", "/some/path", &result
) < 0);
272 #define test(from_dir, to_path, expected) { \
273 _cleanup_free_ char *z = NULL; \
274 path_make_relative(from_dir, to_path, &z); \
275 assert_se(streq(z, expected)); \
279 test("/", "/some/path", "some/path");
280 test("/some/path", "/some/path", ".");
281 test("/some/path", "/some/path/in/subdir", "in/subdir");
282 test("/some/path", "/", "../..");
283 test("/some/path", "/some/other/path", "../other/path");
284 test("/some/path/./dot", "/some/further/path", "../../further/path");
285 test("//extra.//.//./.slashes//./won't////fo.ol///anybody//", "/././/extra././/.slashes////ar.e/.just/././.fine///", "../../../ar.e/.just/.fine");
288 static void test_strv_resolve(void) {
289 char tmp_dir
[] = "/tmp/test-path-util-XXXXXX";
290 _cleanup_strv_free_
char **search_dirs
= NULL
;
291 _cleanup_strv_free_
char **absolute_dirs
= NULL
;
294 assert_se(mkdtemp(tmp_dir
) != NULL
);
296 search_dirs
= strv_new("/dir1", "/dir2", "/dir3");
297 assert_se(search_dirs
);
298 STRV_FOREACH(d
, search_dirs
) {
299 char *p
= strappend(tmp_dir
, *d
);
301 assert_se(strv_push(&absolute_dirs
, p
) == 0);
304 assert_se(mkdir(absolute_dirs
[0], 0700) == 0);
305 assert_se(mkdir(absolute_dirs
[1], 0700) == 0);
306 assert_se(symlink("dir2", absolute_dirs
[2]) == 0);
308 path_strv_resolve(search_dirs
, tmp_dir
);
309 assert_se(streq(search_dirs
[0], "/dir1"));
310 assert_se(streq(search_dirs
[1], "/dir2"));
311 assert_se(streq(search_dirs
[2], "/dir2"));
313 assert_se(rm_rf(tmp_dir
, REMOVE_ROOT
|REMOVE_PHYSICAL
) == 0);
316 static void test_path_startswith(void) {
319 p
= path_startswith("/foo/bar/barfoo/", "/foo");
320 assert_se(streq_ptr(p
, "bar/barfoo/"));
322 p
= path_startswith("/foo/bar/barfoo/", "/foo/");
323 assert_se(streq_ptr(p
, "bar/barfoo/"));
325 p
= path_startswith("/foo/bar/barfoo/", "/");
326 assert_se(streq_ptr(p
, "foo/bar/barfoo/"));
328 p
= path_startswith("/foo/bar/barfoo/", "////");
329 assert_se(streq_ptr(p
, "foo/bar/barfoo/"));
331 p
= path_startswith("/foo/bar/barfoo/", "/foo//bar/////barfoo///");
332 assert_se(streq_ptr(p
, ""));
334 p
= path_startswith("/foo/bar/barfoo/", "/foo/bar/barfoo////");
335 assert_se(streq_ptr(p
, ""));
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 assert_se(!path_startswith("/foo/bar/barfoo/", "/foo/bar/barfooa/"));
350 assert_se(!path_startswith("/foo/bar/barfoo/", "/foo/bar/barfooa"));
351 assert_se(!path_startswith("/foo/bar/barfoo/", ""));
352 assert_se(!path_startswith("/foo/bar/barfoo/", "/bar/foo"));
353 assert_se(!path_startswith("/foo/bar/barfoo/", "/f/b/b/"));
356 static void test_prefix_root_one(const char *r
, const char *p
, const char *expected
) {
357 _cleanup_free_
char *s
= NULL
;
360 assert_se(s
= prefix_root(r
, p
));
361 assert_se(streq_ptr(s
, expected
));
363 t
= prefix_roota(r
, p
);
365 assert_se(streq_ptr(t
, expected
));
368 static void test_prefix_root(void) {
369 test_prefix_root_one("/", "/foo", "/foo");
370 test_prefix_root_one(NULL
, "/foo", "/foo");
371 test_prefix_root_one("", "/foo", "/foo");
372 test_prefix_root_one("///", "/foo", "/foo");
373 test_prefix_root_one("/", "////foo", "/foo");
374 test_prefix_root_one(NULL
, "////foo", "/foo");
376 test_prefix_root_one("/foo", "/bar", "/foo/bar");
377 test_prefix_root_one("/foo", "bar", "/foo/bar");
378 test_prefix_root_one("foo", "bar", "foo/bar");
379 test_prefix_root_one("/foo/", "/bar", "/foo/bar");
380 test_prefix_root_one("/foo/", "//bar", "/foo/bar");
381 test_prefix_root_one("/foo///", "//bar", "/foo/bar");
384 static void test_file_in_same_dir(void) {
387 t
= file_in_same_dir("/", "a");
388 assert_se(streq(t
, "/a"));
391 t
= file_in_same_dir("/", "/a");
392 assert_se(streq(t
, "/a"));
395 t
= file_in_same_dir("", "a");
396 assert_se(streq(t
, "a"));
399 t
= file_in_same_dir("a/", "a");
400 assert_se(streq(t
, "a/a"));
403 t
= file_in_same_dir("bar/foo", "bar");
404 assert_se(streq(t
, "bar/bar"));
408 static void test_last_path_component(void) {
409 assert_se(streq(last_path_component("a/b/c"), "c"));
410 assert_se(streq(last_path_component("a/b/c/"), "c/"));
411 assert_se(streq(last_path_component("/"), "/"));
412 assert_se(streq(last_path_component("//"), "/"));
413 assert_se(streq(last_path_component("///"), "/"));
414 assert_se(streq(last_path_component("."), "."));
415 assert_se(streq(last_path_component("./."), "."));
416 assert_se(streq(last_path_component("././"), "./"));
417 assert_se(streq(last_path_component("././/"), ".//"));
418 assert_se(streq(last_path_component("/foo/a"), "a"));
419 assert_se(streq(last_path_component("/foo/a/"), "a/"));
420 assert_se(streq(last_path_component(""), ""));
421 assert_se(streq(last_path_component("a"), "a"));
422 assert_se(streq(last_path_component("a/"), "a/"));
423 assert_se(streq(last_path_component("/a"), "a"));
424 assert_se(streq(last_path_component("/a/"), "a/"));
427 static void test_filename_is_valid(void) {
428 char foo
[FILENAME_MAX
+2];
431 assert_se(!filename_is_valid(""));
432 assert_se(!filename_is_valid("/bar/foo"));
433 assert_se(!filename_is_valid("/"));
434 assert_se(!filename_is_valid("."));
435 assert_se(!filename_is_valid(".."));
437 for (i
=0; i
<FILENAME_MAX
+1; i
++)
439 foo
[FILENAME_MAX
+1] = '\0';
441 assert_se(!filename_is_valid(foo
));
443 assert_se(filename_is_valid("foo_bar-333"));
444 assert_se(filename_is_valid("o.o"));
447 static void test_hidden_or_backup_file(void) {
448 assert_se(hidden_or_backup_file(".hidden"));
449 assert_se(hidden_or_backup_file("..hidden"));
450 assert_se(!hidden_or_backup_file("hidden."));
452 assert_se(hidden_or_backup_file("backup~"));
453 assert_se(hidden_or_backup_file(".backup~"));
455 assert_se(hidden_or_backup_file("lost+found"));
456 assert_se(hidden_or_backup_file("aquota.user"));
457 assert_se(hidden_or_backup_file("aquota.group"));
459 assert_se(hidden_or_backup_file("test.rpmnew"));
460 assert_se(hidden_or_backup_file("test.dpkg-old"));
461 assert_se(hidden_or_backup_file("test.dpkg-remove"));
462 assert_se(hidden_or_backup_file("test.swp"));
464 assert_se(!hidden_or_backup_file("test.rpmnew."));
465 assert_se(!hidden_or_backup_file("test.dpkg-old.foo"));
468 static void test_systemd_installation_has_version(const char *path
) {
470 const unsigned versions
[] = {0, 231, atoi(PACKAGE_VERSION
), 999};
473 for (i
= 0; i
< ELEMENTSOF(versions
); i
++) {
474 r
= systemd_installation_has_version(path
, versions
[i
]);
476 log_info("%s has systemd >= %u: %s",
477 path
?: "Current installation", versions
[i
], yes_no(r
));
481 static void test_skip_dev_prefix(void) {
483 assert_se(streq(skip_dev_prefix("/"), "/"));
484 assert_se(streq(skip_dev_prefix("/dev"), ""));
485 assert_se(streq(skip_dev_prefix("/dev/"), ""));
486 assert_se(streq(skip_dev_prefix("/dev/foo"), "foo"));
487 assert_se(streq(skip_dev_prefix("/dev/foo/bar"), "foo/bar"));
488 assert_se(streq(skip_dev_prefix("//dev"), ""));
489 assert_se(streq(skip_dev_prefix("//dev//"), ""));
490 assert_se(streq(skip_dev_prefix("/dev///foo"), "foo"));
491 assert_se(streq(skip_dev_prefix("///dev///foo///bar"), "foo///bar"));
492 assert_se(streq(skip_dev_prefix("//foo"), "//foo"));
493 assert_se(streq(skip_dev_prefix("foo"), "foo"));
496 static void test_empty_or_root(void) {
497 assert_se(empty_or_root(NULL
));
498 assert_se(empty_or_root(""));
499 assert_se(empty_or_root("/"));
500 assert_se(empty_or_root("//"));
501 assert_se(empty_or_root("///"));
502 assert_se(empty_or_root("/////////////////"));
503 assert_se(!empty_or_root("xxx"));
504 assert_se(!empty_or_root("/xxx"));
505 assert_se(!empty_or_root("/xxx/"));
506 assert_se(!empty_or_root("//yy//"));
509 int main(int argc
, char **argv
) {
510 test_setup_logging(LOG_DEBUG
);
513 test_path_equal_root();
514 test_find_binary(argv
[0]);
518 test_make_relative();
520 test_path_startswith();
522 test_file_in_same_dir();
523 test_last_path_component();
524 test_filename_is_valid();
525 test_hidden_or_backup_file();
526 test_skip_dev_prefix();
527 test_empty_or_root();
529 test_systemd_installation_has_version(argv
[1]); /* NULL is OK */