Debian Codegraph @

src/test/test-stat-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <fcntl.h>
4 #include <linux/magic.h>
5 #include <sys/eventfd.h>
6 #include <sys/mount.h>
7 #include <unistd.h>
8
9 #include "alloc-util.h"
10 #include "errno-list.h"
11 #include "errno-util.h"
12 #include "fd-util.h"
13 #include "fs-util.h"
14 #include "mount-util.h"
15 #include "mountpoint-util.h"
16 #include "path-util.h"
17 #include "rm-rf.h"
18 #include "stat-util.h"
19 #include "strv.h"
20 #include "tests.h"
21 #include "tmpfile-util.h"
22
23 TEST(statx_definitions) {
24 /* Check if linux/stat.h is included from sys/stat.h. */
25 ASSERT_EQ(STATX_TYPE, 0x00000001U);
26 ASSERT_EQ(STATX_MODE, 0x00000002U);
27 ASSERT_EQ(STATX_NLINK, 0x00000004U);
28 ASSERT_EQ(STATX_UID, 0x00000008U);
29 ASSERT_EQ(STATX_GID, 0x00000010U);
30 ASSERT_EQ(STATX_ATIME, 0x00000020U);
31 ASSERT_EQ(STATX_MTIME, 0x00000040U);
32 ASSERT_EQ(STATX_CTIME, 0x00000080U);
33 ASSERT_EQ(STATX_INO, 0x00000100U);
34 ASSERT_EQ(STATX_SIZE, 0x00000200U);
35 ASSERT_EQ(STATX_BLOCKS, 0x00000400U);
36 ASSERT_EQ(STATX_BASIC_STATS, 0x000007ffU);
37 ASSERT_EQ(STATX_BTIME, 0x00000800U);
38 ASSERT_EQ(STATX_MNT_ID, 0x00001000U);
39 ASSERT_EQ(STATX_DIOALIGN, 0x00002000U);
40 ASSERT_EQ(STATX_MNT_ID_UNIQUE, 0x00004000U);
41 ASSERT_EQ(STATX_SUBVOL, 0x00008000U);
42 ASSERT_EQ(STATX_WRITE_ATOMIC, 0x00010000U);
43 ASSERT_EQ(STATX_DIO_READ_ALIGN, 0x00020000U);
44
45 ASSERT_EQ(STATX_ATTR_COMPRESSED, 0x00000004);
46 ASSERT_EQ(STATX_ATTR_IMMUTABLE, 0x00000010);
47 ASSERT_EQ(STATX_ATTR_APPEND, 0x00000020);
48 ASSERT_EQ(STATX_ATTR_NODUMP, 0x00000040);
49 ASSERT_EQ(STATX_ATTR_ENCRYPTED, 0x00000800);
50 ASSERT_EQ(STATX_ATTR_AUTOMOUNT, 0x00001000);
51 ASSERT_EQ(STATX_ATTR_MOUNT_ROOT, 0x00002000);
52 ASSERT_EQ(STATX_ATTR_VERITY, 0x00100000);
53 ASSERT_EQ(STATX_ATTR_DAX, 0x00200000);
54 ASSERT_EQ(STATX_ATTR_WRITE_ATOMIC, 0x00400000);
55 }
56
57 TEST(null_or_empty_path) {
58 assert_se(null_or_empty_path("/dev/null") == 1);
59 assert_se(null_or_empty_path("/dev/tty") == 1); /* We assume that any character device is "empty", bleh. */
60 assert_se(null_or_empty_path("../../../../../../../../../../../../../../../../../../../../dev/null") == 1);
61 assert_se(null_or_empty_path("/proc/self/exe") == 0);
62 assert_se(null_or_empty_path("/nosuchfileordir") == -ENOENT);
63 }
64
65 TEST(null_or_empty_path_with_root) {
66 assert_se(null_or_empty_path_with_root("/dev/null", NULL) == 1);
67 assert_se(null_or_empty_path_with_root("/dev/null", "/") == 1);
68 assert_se(null_or_empty_path_with_root("/dev/null", "/.././../") == 1);
69 assert_se(null_or_empty_path_with_root("/dev/null", "/.././..") == 1);
70 assert_se(null_or_empty_path_with_root("../../../../../../../../../../../../../../../../../../../../dev/null", NULL) == 1);
71 assert_se(null_or_empty_path_with_root("../../../../../../../../../../../../../../../../../../../../dev/null", "/") == 1);
72 assert_se(null_or_empty_path_with_root("/proc/self/exe", NULL) == 0);
73 assert_se(null_or_empty_path_with_root("/proc/self/exe", "/") == 0);
74 assert_se(null_or_empty_path_with_root("/nosuchfileordir", NULL) == -ENOENT);
75 assert_se(null_or_empty_path_with_root("/nosuchfileordir", "/.././../") == -ENOENT);
76 assert_se(null_or_empty_path_with_root("/nosuchfileordir", "/.././..") == -ENOENT);
77 assert_se(null_or_empty_path_with_root("/foobar/barbar/dev/null", "/foobar/barbar") == 1);
78 assert_se(null_or_empty_path_with_root("/foobar/barbar/dev/null", "/foobar/barbar/") == 1);
79 }
80
81 TEST(inode_same) {
82 _cleanup_close_ int fd = -EBADF;
83 _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-files_same.XXXXXX";
84 _cleanup_(unlink_tempfilep) char name_alias[] = "/tmp/test-files_same.alias";
85 int r;
86
87 fd = mkostemp_safe(name);
88 assert_se(fd >= 0);
89 assert_se(symlink(name, name_alias) >= 0);
90
91 assert_se(inode_same(name, name, 0) > 0);
92 assert_se(inode_same(name, name, AT_SYMLINK_NOFOLLOW) > 0);
93 assert_se(inode_same(name, name_alias, 0) > 0);
94 assert_se(inode_same(name, name_alias, AT_SYMLINK_NOFOLLOW) == 0);
95
96 assert_se(inode_same("/proc", "/proc", 0));
97 assert_se(inode_same("/proc", "/proc", AT_SYMLINK_NOFOLLOW));
98
99 _cleanup_close_ int fd1 = open("/dev/null", O_CLOEXEC|O_RDONLY),
100 fd2 = open("/dev/null", O_CLOEXEC|O_RDONLY);
101
102 assert_se(fd1 >= 0);
103 assert_se(fd2 >= 0);
104
105 assert_se(inode_same_at(fd1, NULL, fd2, NULL, AT_EMPTY_PATH) > 0);
106 assert_se(inode_same_at(fd2, NULL, fd1, NULL, AT_EMPTY_PATH) > 0);
107 assert_se(inode_same_at(fd1, NULL, fd2, NULL, AT_EMPTY_PATH|AT_SYMLINK_NOFOLLOW) > 0);
108 assert_se(inode_same_at(fd2, NULL, fd1, NULL, AT_EMPTY_PATH|AT_SYMLINK_NOFOLLOW) > 0);
109 assert_se(inode_same_at(fd1, NULL, fd1, NULL, AT_EMPTY_PATH) > 0);
110 assert_se(inode_same_at(fd2, NULL, fd2, NULL, AT_EMPTY_PATH|AT_SYMLINK_NOFOLLOW) > 0);
111
112 safe_close(fd2);
113 fd2 = open("/dev/urandom", O_CLOEXEC|O_RDONLY);
114 assert_se(fd2 >= 0);
115
116 assert_se(inode_same_at(fd1, NULL, fd2, NULL, AT_EMPTY_PATH) == 0);
117 assert_se(inode_same_at(fd2, NULL, fd1, NULL, AT_EMPTY_PATH) == 0);
118 assert_se(inode_same_at(fd1, NULL, fd2, NULL, AT_EMPTY_PATH|AT_SYMLINK_NOFOLLOW) == 0);
119 assert_se(inode_same_at(fd2, NULL, fd1, NULL, AT_EMPTY_PATH|AT_SYMLINK_NOFOLLOW) == 0);
120
121 assert_se(inode_same_at(AT_FDCWD, NULL, AT_FDCWD, NULL, AT_EMPTY_PATH) > 0);
122 assert_se(inode_same_at(AT_FDCWD, NULL, fd1, NULL, AT_EMPTY_PATH) == 0);
123 assert_se(inode_same_at(fd1, NULL, AT_FDCWD, NULL, AT_EMPTY_PATH) == 0);
124
125 _cleanup_(umount_and_unlink_and_freep) char *p = NULL;
126
127 assert_se(tempfn_random_child(NULL, NULL, &p) >= 0);
128 assert_se(touch(p) >= 0);
129
130 r = mount_nofollow_verbose(LOG_ERR, name, p, NULL, MS_BIND, NULL);
131 if (r < 0)
132 assert_se(ERRNO_IS_NEG_PRIVILEGE(r));
133 else {
134 assert_se(inode_same(name, p, 0) > 0);
135 assert_se(inode_same(name, p, AT_SYMLINK_NOFOLLOW) > 0);
136 }
137 }
138
139 TEST(is_symlink) {
140 _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-is_symlink.XXXXXX";
141 _cleanup_(unlink_tempfilep) char name_link[] = "/tmp/test-is_symlink.link";
142 _cleanup_close_ int fd = -EBADF;
143
144 fd = mkostemp_safe(name);
145 assert_se(fd >= 0);
146 assert_se(symlink(name, name_link) >= 0);
147
148 assert_se(is_symlink(name) == 0);
149 assert_se(is_symlink(name_link) == 1);
150 assert_se(is_symlink("/a/file/which/does/not/exist/i/guess") < 0);
151 }
152
153 TEST(path_is_fs_type) {
154 /* run might not be a mount point in build chroots */
155 if (path_is_mount_point_full("/run", NULL, AT_SYMLINK_FOLLOW) > 0) {
156 assert_se(path_is_fs_type("/run", TMPFS_MAGIC) > 0);
157 assert_se(path_is_fs_type("/run", BTRFS_SUPER_MAGIC) == 0);
158 }
159 if (path_is_mount_point_full("/proc", NULL, AT_SYMLINK_FOLLOW) > 0) {
160 assert_se(path_is_fs_type("/proc", PROC_SUPER_MAGIC) > 0);
161 assert_se(path_is_fs_type("/proc", BTRFS_SUPER_MAGIC) == 0);
162 }
163 assert_se(path_is_fs_type("/i-dont-exist", BTRFS_SUPER_MAGIC) == -ENOENT);
164 }
165
166 TEST(path_is_temporary_fs) {
167 int r;
168
169 FOREACH_STRING(s, "/", "/run", "/sys", "/sys/", "/proc", "/i-dont-exist", "/var", "/var/lib") {
170 r = path_is_temporary_fs(s);
171
172 log_info_errno(r, "path_is_temporary_fs(\"%s\"): %d, %s",
173 s, r, r < 0 ? ERRNO_NAME(r) : yes_no(r));
174 }
175
176 /* run might not be a mount point in build chroots */
177 if (path_is_mount_point_full("/run", NULL, AT_SYMLINK_FOLLOW) > 0)
178 assert_se(path_is_temporary_fs("/run") > 0);
179 assert_se(path_is_temporary_fs("/proc") == 0);
180 assert_se(path_is_temporary_fs("/i-dont-exist") == -ENOENT);
181 }
182
183 TEST(path_is_read_only_fs) {
184 int r;
185
186 FOREACH_STRING(s, "/", "/run", "/sys", "/sys/", "/proc", "/i-dont-exist", "/var", "/var/lib") {
187 r = path_is_read_only_fs(s);
188
189 log_info_errno(r, "path_is_read_only_fs(\"%s\"): %d, %s",
190 s, r, r < 0 ? ERRNO_NAME(r) : yes_no(r));
191 }
192
193 if (path_is_mount_point_full("/sys", NULL, AT_SYMLINK_FOLLOW) > 0)
194 assert_se(IN_SET(path_is_read_only_fs("/sys"), 0, 1));
195
196 assert_se(path_is_read_only_fs("/proc") == 0);
197 assert_se(path_is_read_only_fs("/i-dont-exist") == -ENOENT);
198 }
199
200 TEST(dir_is_empty) {
201 _cleanup_(rm_rf_physical_and_freep) char *empty_dir = NULL;
202 _cleanup_free_ char *j = NULL, *jj = NULL, *jjj = NULL;
203
204 assert_se(dir_is_empty_at(AT_FDCWD, "/proc", /* ignore_hidden_or_backup= */ true) == 0);
205 assert_se(dir_is_empty_at(AT_FDCWD, "/icertainlydontexistdoi", /* ignore_hidden_or_backup= */ true) == -ENOENT);
206
207 assert_se(mkdtemp_malloc("/tmp/emptyXXXXXX", &empty_dir) >= 0);
208 assert_se(dir_is_empty_at(AT_FDCWD, empty_dir, /* ignore_hidden_or_backup= */ true) > 0);
209
210 j = path_join(empty_dir, "zzz");
211 assert_se(j);
212 assert_se(touch(j) >= 0);
213
214 assert_se(dir_is_empty_at(AT_FDCWD, empty_dir, /* ignore_hidden_or_backup= */ true) == 0);
215
216 jj = path_join(empty_dir, "ppp");
217 assert_se(jj);
218 assert_se(touch(jj) >= 0);
219
220 jjj = path_join(empty_dir, ".qqq");
221 assert_se(jjj);
222 assert_se(touch(jjj) >= 0);
223
224 assert_se(dir_is_empty_at(AT_FDCWD, empty_dir, /* ignore_hidden_or_backup= */ true) == 0);
225 assert_se(dir_is_empty_at(AT_FDCWD, empty_dir, /* ignore_hidden_or_backup= */ false) == 0);
226 assert_se(unlink(j) >= 0);
227 assert_se(dir_is_empty_at(AT_FDCWD, empty_dir, /* ignore_hidden_or_backup= */ true) == 0);
228 assert_se(dir_is_empty_at(AT_FDCWD, empty_dir, /* ignore_hidden_or_backup= */ false) == 0);
229 assert_se(unlink(jj) >= 0);
230 assert_se(dir_is_empty_at(AT_FDCWD, empty_dir, /* ignore_hidden_or_backup= */ true) > 0);
231 assert_se(dir_is_empty_at(AT_FDCWD, empty_dir, /* ignore_hidden_or_backup= */ false) == 0);
232 assert_se(unlink(jjj) >= 0);
233 assert_se(dir_is_empty_at(AT_FDCWD, empty_dir, /* ignore_hidden_or_backup= */ true) > 0);
234 assert_se(dir_is_empty_at(AT_FDCWD, empty_dir, /* ignore_hidden_or_backup= */ false) > 0);
235 }
236
237 TEST(inode_type_from_string) {
238 static const mode_t types[] = {
239 S_IFREG,
240 S_IFDIR,
241 S_IFLNK,
242 S_IFCHR,
243 S_IFBLK,
244 S_IFIFO,
245 S_IFSOCK,
246 };
247
248 FOREACH_ELEMENT(m, types)
249 assert_se(inode_type_from_string(inode_type_to_string(*m)) == *m);
250 }
251
252 TEST(anonymous_inode) {
253 _cleanup_close_ int fd = -EBADF;
254
255 fd = eventfd(0, EFD_CLOEXEC);
256 assert_se(fd >= 0);
257
258 /* Verify that we handle anonymous inodes correctly, i.e. those which have no file type */
259
260 struct stat st;
261 ASSERT_OK_ERRNO(fstat(fd, &st));
262 assert_se((st.st_mode & S_IFMT) == 0);
263
264 assert_se(!inode_type_to_string(st.st_mode));
265 }
266
267 TEST(fd_verify_linked) {
268 _cleanup_(rm_rf_physical_and_freep) char *t = NULL;
269 _cleanup_close_ int tfd = -EBADF, fd = -EBADF;
270 _cleanup_free_ char *p = NULL;
271
272 tfd = mkdtemp_open(NULL, O_PATH, &t);
273 assert_se(tfd >= 0);
274
275 assert_se(p = path_join(t, "hoge"));
276 assert_se(touch(p) >= 0);
277
278 fd = open(p, O_CLOEXEC | O_PATH);
279 assert_se(fd >= 0);
280
281 assert_se(fd_verify_linked(fd) >= 0);
282 assert_se(unlinkat(tfd, "hoge", 0) >= 0);
283 assert_se(fd_verify_linked(fd) == -EIDRM);
284 }
285
286 static int intro(void) {
287 log_show_color(true);
288 return EXIT_SUCCESS;
289 }
290
291 DEFINE_TEST_MAIN_WITH_INTRO(LOG_INFO, intro);