1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
4 #include <linux/magic.h>
6 #include <sys/eventfd.h>
9 #include "alloc-util.h"
10 #include "errno-list.h"
14 #include "mountpoint-util.h"
15 #include "namespace-util.h"
16 #include "path-util.h"
18 #include "stat-util.h"
20 #include "tmpfile-util.h"
22 TEST(null_or_empty_path
) {
23 assert_se(null_or_empty_path("/dev/null") == 1);
24 assert_se(null_or_empty_path("/dev/tty") == 1); /* We assume that any character device is "empty", bleh. */
25 assert_se(null_or_empty_path("../../../../../../../../../../../../../../../../../../../../dev/null") == 1);
26 assert_se(null_or_empty_path("/proc/self/exe") == 0);
27 assert_se(null_or_empty_path("/nosuchfileordir") == -ENOENT
);
30 TEST(null_or_empty_path_with_root
) {
31 assert_se(null_or_empty_path_with_root("/dev/null", NULL
) == 1);
32 assert_se(null_or_empty_path_with_root("/dev/null", "/") == 1);
33 assert_se(null_or_empty_path_with_root("/dev/null", "/.././../") == 1);
34 assert_se(null_or_empty_path_with_root("/dev/null", "/.././..") == 1);
35 assert_se(null_or_empty_path_with_root("../../../../../../../../../../../../../../../../../../../../dev/null", NULL
) == 1);
36 assert_se(null_or_empty_path_with_root("../../../../../../../../../../../../../../../../../../../../dev/null", "/") == 1);
37 assert_se(null_or_empty_path_with_root("/proc/self/exe", NULL
) == 0);
38 assert_se(null_or_empty_path_with_root("/proc/self/exe", "/") == 0);
39 assert_se(null_or_empty_path_with_root("/nosuchfileordir", NULL
) == -ENOENT
);
40 assert_se(null_or_empty_path_with_root("/nosuchfileordir", "/.././../") == -ENOENT
);
41 assert_se(null_or_empty_path_with_root("/nosuchfileordir", "/.././..") == -ENOENT
);
42 assert_se(null_or_empty_path_with_root("/foobar/barbar/dev/null", "/foobar/barbar") == 1);
43 assert_se(null_or_empty_path_with_root("/foobar/barbar/dev/null", "/foobar/barbar/") == 1);
47 _cleanup_close_
int fd
= -EBADF
;
48 _cleanup_(unlink_tempfilep
) char name
[] = "/tmp/test-files_same.XXXXXX";
49 _cleanup_(unlink_tempfilep
) char name_alias
[] = "/tmp/test-files_same.alias";
51 fd
= mkostemp_safe(name
);
53 assert_se(symlink(name
, name_alias
) >= 0);
55 assert_se(inode_same(name
, name
, 0));
56 assert_se(inode_same(name
, name
, AT_SYMLINK_NOFOLLOW
));
57 assert_se(inode_same(name
, name_alias
, 0));
58 assert_se(!inode_same(name
, name_alias
, AT_SYMLINK_NOFOLLOW
));
62 _cleanup_(unlink_tempfilep
) char name
[] = "/tmp/test-is_symlink.XXXXXX";
63 _cleanup_(unlink_tempfilep
) char name_link
[] = "/tmp/test-is_symlink.link";
64 _cleanup_close_
int fd
= -EBADF
;
66 fd
= mkostemp_safe(name
);
68 assert_se(symlink(name
, name_link
) >= 0);
70 assert_se(is_symlink(name
) == 0);
71 assert_se(is_symlink(name_link
) == 1);
72 assert_se(is_symlink("/a/file/which/does/not/exist/i/guess") < 0);
75 TEST(path_is_fs_type
) {
76 /* run might not be a mount point in build chroots */
77 if (path_is_mount_point_full("/run", NULL
, AT_SYMLINK_FOLLOW
) > 0) {
78 assert_se(path_is_fs_type("/run", TMPFS_MAGIC
) > 0);
79 assert_se(path_is_fs_type("/run", BTRFS_SUPER_MAGIC
) == 0);
81 if (path_is_mount_point_full("/proc", NULL
, AT_SYMLINK_FOLLOW
) > 0) {
82 assert_se(path_is_fs_type("/proc", PROC_SUPER_MAGIC
) > 0);
83 assert_se(path_is_fs_type("/proc", BTRFS_SUPER_MAGIC
) == 0);
85 assert_se(path_is_fs_type("/i-dont-exist", BTRFS_SUPER_MAGIC
) == -ENOENT
);
88 TEST(path_is_temporary_fs
) {
91 FOREACH_STRING(s
, "/", "/run", "/sys", "/sys/", "/proc", "/i-dont-exist", "/var", "/var/lib") {
92 r
= path_is_temporary_fs(s
);
94 log_info_errno(r
, "path_is_temporary_fs(\"%s\"): %d, %s",
95 s
, r
, r
< 0 ? errno_to_name(r
) : yes_no(r
));
98 /* run might not be a mount point in build chroots */
99 if (path_is_mount_point_full("/run", NULL
, AT_SYMLINK_FOLLOW
) > 0)
100 assert_se(path_is_temporary_fs("/run") > 0);
101 assert_se(path_is_temporary_fs("/proc") == 0);
102 assert_se(path_is_temporary_fs("/i-dont-exist") == -ENOENT
);
105 TEST(path_is_read_only_fs
) {
108 FOREACH_STRING(s
, "/", "/run", "/sys", "/sys/", "/proc", "/i-dont-exist", "/var", "/var/lib") {
109 r
= path_is_read_only_fs(s
);
111 log_info_errno(r
, "path_is_read_only_fs(\"%s\"): %d, %s",
112 s
, r
, r
< 0 ? errno_to_name(r
) : yes_no(r
));
115 if (path_is_mount_point_full("/sys", NULL
, AT_SYMLINK_FOLLOW
) > 0)
116 assert_se(IN_SET(path_is_read_only_fs("/sys"), 0, 1));
118 assert_se(path_is_read_only_fs("/proc") == 0);
119 assert_se(path_is_read_only_fs("/i-dont-exist") == -ENOENT
);
123 _cleanup_close_
int fd
= -EBADF
;
125 assert_se(fd_is_ns(STDIN_FILENO
, CLONE_NEWNET
) == 0);
126 assert_se(fd_is_ns(STDERR_FILENO
, CLONE_NEWNET
) == 0);
127 assert_se(fd_is_ns(STDOUT_FILENO
, CLONE_NEWNET
) == 0);
129 fd
= open("/proc/self/ns/mnt", O_CLOEXEC
|O_RDONLY
);
131 assert_se(errno
== ENOENT
);
132 log_notice("Path %s not found, skipping test", "/proc/self/ns/mnt");
136 assert_se(IN_SET(fd_is_ns(fd
, CLONE_NEWNET
), 0, -EUCLEAN
));
139 assert_se((fd
= open("/proc/self/ns/ipc", O_CLOEXEC
|O_RDONLY
)) >= 0);
140 assert_se(IN_SET(fd_is_ns(fd
, CLONE_NEWIPC
), 1, -EUCLEAN
));
143 assert_se((fd
= open("/proc/self/ns/net", O_CLOEXEC
|O_RDONLY
)) >= 0);
144 assert_se(IN_SET(fd_is_ns(fd
, CLONE_NEWNET
), 1, -EUCLEAN
));
148 _cleanup_(rm_rf_physical_and_freep
) char *empty_dir
= NULL
;
149 _cleanup_free_
char *j
= NULL
, *jj
= NULL
, *jjj
= NULL
;
151 assert_se(dir_is_empty_at(AT_FDCWD
, "/proc", /* ignore_hidden_or_backup= */ true) == 0);
152 assert_se(dir_is_empty_at(AT_FDCWD
, "/icertainlydontexistdoi", /* ignore_hidden_or_backup= */ true) == -ENOENT
);
154 assert_se(mkdtemp_malloc("/tmp/emptyXXXXXX", &empty_dir
) >= 0);
155 assert_se(dir_is_empty_at(AT_FDCWD
, empty_dir
, /* ignore_hidden_or_backup= */ true) > 0);
157 j
= path_join(empty_dir
, "zzz");
159 assert_se(touch(j
) >= 0);
161 assert_se(dir_is_empty_at(AT_FDCWD
, empty_dir
, /* ignore_hidden_or_backup= */ true) == 0);
163 jj
= path_join(empty_dir
, "ppp");
165 assert_se(touch(jj
) >= 0);
167 jjj
= path_join(empty_dir
, ".qqq");
169 assert_se(touch(jjj
) >= 0);
171 assert_se(dir_is_empty_at(AT_FDCWD
, empty_dir
, /* ignore_hidden_or_backup= */ true) == 0);
172 assert_se(dir_is_empty_at(AT_FDCWD
, empty_dir
, /* ignore_hidden_or_backup= */ false) == 0);
173 assert_se(unlink(j
) >= 0);
174 assert_se(dir_is_empty_at(AT_FDCWD
, empty_dir
, /* ignore_hidden_or_backup= */ true) == 0);
175 assert_se(dir_is_empty_at(AT_FDCWD
, empty_dir
, /* ignore_hidden_or_backup= */ false) == 0);
176 assert_se(unlink(jj
) >= 0);
177 assert_se(dir_is_empty_at(AT_FDCWD
, empty_dir
, /* ignore_hidden_or_backup= */ true) > 0);
178 assert_se(dir_is_empty_at(AT_FDCWD
, empty_dir
, /* ignore_hidden_or_backup= */ false) == 0);
179 assert_se(unlink(jjj
) >= 0);
180 assert_se(dir_is_empty_at(AT_FDCWD
, empty_dir
, /* ignore_hidden_or_backup= */ true) > 0);
181 assert_se(dir_is_empty_at(AT_FDCWD
, empty_dir
, /* ignore_hidden_or_backup= */ false) > 0);
184 TEST(inode_type_from_string
) {
185 static const mode_t types
[] = {
195 FOREACH_ELEMENT(m
, types
)
196 assert_se(inode_type_from_string(inode_type_to_string(*m
)) == *m
);
199 TEST(anonymous_inode
) {
200 _cleanup_close_
int fd
= -EBADF
;
202 fd
= eventfd(0, EFD_CLOEXEC
);
205 /* Verify that we handle anonymous inodes correctly, i.e. those which have no file type */
208 ASSERT_OK_ERRNO(fstat(fd
, &st
));
209 assert_se((st
.st_mode
& S_IFMT
) == 0);
211 assert_se(!inode_type_to_string(st
.st_mode
));
214 TEST(fd_verify_linked
) {
215 _cleanup_(rm_rf_physical_and_freep
) char *t
= NULL
;
216 _cleanup_close_
int tfd
= -EBADF
, fd
= -EBADF
;
217 _cleanup_free_
char *p
= NULL
;
219 tfd
= mkdtemp_open(NULL
, O_PATH
, &t
);
222 assert_se(p
= path_join(t
, "hoge"));
223 assert_se(touch(p
) >= 0);
225 fd
= open(p
, O_CLOEXEC
| O_PATH
);
228 assert_se(fd_verify_linked(fd
) >= 0);
229 assert_se(unlinkat(tfd
, "hoge", 0) >= 0);
230 assert_se(fd_verify_linked(fd
) == -EIDRM
);
233 static int intro(void) {
234 log_show_color(true);
238 DEFINE_TEST_MAIN_WITH_INTRO(LOG_INFO
, intro
);