]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-stat-util.c
use FOREACH_ELEMENT
[thirdparty/systemd.git] / src / test / test-stat-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <fcntl.h>
4 #include <linux/magic.h>
5 #include <sched.h>
6 #include <sys/eventfd.h>
7 #include <unistd.h>
8
9 #include "alloc-util.h"
10 #include "errno-list.h"
11 #include "fd-util.h"
12 #include "fs-util.h"
13 #include "macro.h"
14 #include "mountpoint-util.h"
15 #include "namespace-util.h"
16 #include "path-util.h"
17 #include "rm-rf.h"
18 #include "stat-util.h"
19 #include "tests.h"
20 #include "tmpfile-util.h"
21
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);
28 }
29
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);
44 }
45
46 TEST(inode_same) {
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";
50
51 fd = mkostemp_safe(name);
52 assert_se(fd >= 0);
53 assert_se(symlink(name, name_alias) >= 0);
54
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));
59 }
60
61 TEST(is_symlink) {
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;
65
66 fd = mkostemp_safe(name);
67 assert_se(fd >= 0);
68 assert_se(symlink(name, name_link) >= 0);
69
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);
73 }
74
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);
80 }
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);
84 }
85 assert_se(path_is_fs_type("/i-dont-exist", BTRFS_SUPER_MAGIC) == -ENOENT);
86 }
87
88 TEST(path_is_temporary_fs) {
89 int r;
90
91 FOREACH_STRING(s, "/", "/run", "/sys", "/sys/", "/proc", "/i-dont-exist", "/var", "/var/lib") {
92 r = path_is_temporary_fs(s);
93
94 log_info_errno(r, "path_is_temporary_fs(\"%s\"): %d, %s",
95 s, r, r < 0 ? errno_to_name(r) : yes_no(r));
96 }
97
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);
103 }
104
105 TEST(path_is_read_only_fs) {
106 int r;
107
108 FOREACH_STRING(s, "/", "/run", "/sys", "/sys/", "/proc", "/i-dont-exist", "/var", "/var/lib") {
109 r = path_is_read_only_fs(s);
110
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));
113 }
114
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));
117
118 assert_se(path_is_read_only_fs("/proc") == 0);
119 assert_se(path_is_read_only_fs("/i-dont-exist") == -ENOENT);
120 }
121
122 TEST(fd_is_ns) {
123 _cleanup_close_ int fd = -EBADF;
124
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);
128
129 fd = open("/proc/self/ns/mnt", O_CLOEXEC|O_RDONLY);
130 if (fd < 0) {
131 assert_se(errno == ENOENT);
132 log_notice("Path %s not found, skipping test", "/proc/self/ns/mnt");
133 return;
134 }
135 assert_se(fd >= 0);
136 assert_se(IN_SET(fd_is_ns(fd, CLONE_NEWNET), 0, -EUCLEAN));
137 fd = safe_close(fd);
138
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));
141 fd = safe_close(fd);
142
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));
145 }
146
147 TEST(dir_is_empty) {
148 _cleanup_(rm_rf_physical_and_freep) char *empty_dir = NULL;
149 _cleanup_free_ char *j = NULL, *jj = NULL, *jjj = NULL;
150
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);
153
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);
156
157 j = path_join(empty_dir, "zzz");
158 assert_se(j);
159 assert_se(touch(j) >= 0);
160
161 assert_se(dir_is_empty_at(AT_FDCWD, empty_dir, /* ignore_hidden_or_backup= */ true) == 0);
162
163 jj = path_join(empty_dir, "ppp");
164 assert_se(jj);
165 assert_se(touch(jj) >= 0);
166
167 jjj = path_join(empty_dir, ".qqq");
168 assert_se(jjj);
169 assert_se(touch(jjj) >= 0);
170
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);
182 }
183
184 TEST(inode_type_from_string) {
185 static const mode_t types[] = {
186 S_IFREG,
187 S_IFDIR,
188 S_IFLNK,
189 S_IFCHR,
190 S_IFBLK,
191 S_IFIFO,
192 S_IFSOCK,
193 };
194
195 FOREACH_ELEMENT(m, types)
196 assert_se(inode_type_from_string(inode_type_to_string(*m)) == *m);
197 }
198
199 TEST(anonymous_inode) {
200 _cleanup_close_ int fd = -EBADF;
201
202 fd = eventfd(0, EFD_CLOEXEC);
203 assert_se(fd >= 0);
204
205 /* Verify that we handle anonymous inodes correctly, i.e. those which have no file type */
206
207 struct stat st;
208 ASSERT_OK_ERRNO(fstat(fd, &st));
209 assert_se((st.st_mode & S_IFMT) == 0);
210
211 assert_se(!inode_type_to_string(st.st_mode));
212 }
213
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;
218
219 tfd = mkdtemp_open(NULL, O_PATH, &t);
220 assert_se(tfd >= 0);
221
222 assert_se(p = path_join(t, "hoge"));
223 assert_se(touch(p) >= 0);
224
225 fd = open(p, O_CLOEXEC | O_PATH);
226 assert_se(fd >= 0);
227
228 assert_se(fd_verify_linked(fd) >= 0);
229 assert_se(unlinkat(tfd, "hoge", 0) >= 0);
230 assert_se(fd_verify_linked(fd) == -EIDRM);
231 }
232
233 static int intro(void) {
234 log_show_color(true);
235 return EXIT_SUCCESS;
236 }
237
238 DEFINE_TEST_MAIN_WITH_INTRO(LOG_INFO, intro);