]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-stat-util.c
test: Use TEST macro
[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 <unistd.h>
7
8 #include "alloc-util.h"
9 #include "errno-list.h"
10 #include "fd-util.h"
11 #include "fs-util.h"
12 #include "macro.h"
13 #include "mountpoint-util.h"
14 #include "namespace-util.h"
15 #include "path-util.h"
16 #include "rm-rf.h"
17 #include "stat-util.h"
18 #include "tests.h"
19 #include "tmpfile-util.h"
20
21 TEST(files_same) {
22 _cleanup_close_ int fd = -1;
23 char name[] = "/tmp/test-files_same.XXXXXX";
24 char name_alias[] = "/tmp/test-files_same.alias";
25
26 fd = mkostemp_safe(name);
27 assert_se(fd >= 0);
28 assert_se(symlink(name, name_alias) >= 0);
29
30 assert_se(files_same(name, name, 0));
31 assert_se(files_same(name, name, AT_SYMLINK_NOFOLLOW));
32 assert_se(files_same(name, name_alias, 0));
33 assert_se(!files_same(name, name_alias, AT_SYMLINK_NOFOLLOW));
34
35 unlink(name);
36 unlink(name_alias);
37 }
38
39 TEST(is_symlink) {
40 char name[] = "/tmp/test-is_symlink.XXXXXX";
41 char name_link[] = "/tmp/test-is_symlink.link";
42 _cleanup_close_ int fd = -1;
43
44 fd = mkostemp_safe(name);
45 assert_se(fd >= 0);
46 assert_se(symlink(name, name_link) >= 0);
47
48 assert_se(is_symlink(name) == 0);
49 assert_se(is_symlink(name_link) == 1);
50 assert_se(is_symlink("/a/file/which/does/not/exist/i/guess") < 0);
51
52 unlink(name);
53 unlink(name_link);
54 }
55
56 TEST(path_is_fs_type) {
57 /* run might not be a mount point in build chroots */
58 if (path_is_mount_point("/run", NULL, AT_SYMLINK_FOLLOW) > 0) {
59 assert_se(path_is_fs_type("/run", TMPFS_MAGIC) > 0);
60 assert_se(path_is_fs_type("/run", BTRFS_SUPER_MAGIC) == 0);
61 }
62 if (path_is_mount_point("/proc", NULL, AT_SYMLINK_FOLLOW) > 0) {
63 assert_se(path_is_fs_type("/proc", PROC_SUPER_MAGIC) > 0);
64 assert_se(path_is_fs_type("/proc", BTRFS_SUPER_MAGIC) == 0);
65 }
66 assert_se(path_is_fs_type("/i-dont-exist", BTRFS_SUPER_MAGIC) == -ENOENT);
67 }
68
69 TEST(path_is_temporary_fs) {
70 const char *s;
71 int r;
72
73 FOREACH_STRING(s, "/", "/run", "/sys", "/sys/", "/proc", "/i-dont-exist", "/var", "/var/lib") {
74 r = path_is_temporary_fs(s);
75
76 log_info_errno(r, "path_is_temporary_fs(\"%s\"): %d, %s",
77 s, r, r < 0 ? errno_to_name(r) : yes_no(r));
78 }
79
80 /* run might not be a mount point in build chroots */
81 if (path_is_mount_point("/run", NULL, AT_SYMLINK_FOLLOW) > 0)
82 assert_se(path_is_temporary_fs("/run") > 0);
83 assert_se(path_is_temporary_fs("/proc") == 0);
84 assert_se(path_is_temporary_fs("/i-dont-exist") == -ENOENT);
85 }
86
87 TEST(path_is_read_only_fs) {
88 const char *s;
89 int r;
90
91 FOREACH_STRING(s, "/", "/run", "/sys", "/sys/", "/proc", "/i-dont-exist", "/var", "/var/lib") {
92 r = path_is_read_only_fs(s);
93
94 log_info_errno(r, "path_is_read_only_fs(\"%s\"): %d, %s",
95 s, r, r < 0 ? errno_to_name(r) : yes_no(r));
96 }
97
98 if (path_is_mount_point("/sys", NULL, AT_SYMLINK_FOLLOW) > 0)
99 assert_se(IN_SET(path_is_read_only_fs("/sys"), 0, 1));
100
101 assert_se(path_is_read_only_fs("/proc") == 0);
102 assert_se(path_is_read_only_fs("/i-dont-exist") == -ENOENT);
103 }
104
105 TEST(fd_is_ns) {
106 _cleanup_close_ int fd = -1;
107
108 assert_se(fd_is_ns(STDIN_FILENO, CLONE_NEWNET) == 0);
109 assert_se(fd_is_ns(STDERR_FILENO, CLONE_NEWNET) == 0);
110 assert_se(fd_is_ns(STDOUT_FILENO, CLONE_NEWNET) == 0);
111
112 fd = open("/proc/self/ns/mnt", O_CLOEXEC|O_RDONLY);
113 if (fd < 0) {
114 assert_se(errno == ENOENT);
115 log_notice("Path %s not found, skipping test", "/proc/self/ns/mnt");
116 return;
117 }
118 assert_se(fd >= 0);
119 assert_se(IN_SET(fd_is_ns(fd, CLONE_NEWNET), 0, -EUCLEAN));
120 fd = safe_close(fd);
121
122 assert_se((fd = open("/proc/self/ns/ipc", O_CLOEXEC|O_RDONLY)) >= 0);
123 assert_se(IN_SET(fd_is_ns(fd, CLONE_NEWIPC), 1, -EUCLEAN));
124 fd = safe_close(fd);
125
126 assert_se((fd = open("/proc/self/ns/net", O_CLOEXEC|O_RDONLY)) >= 0);
127 assert_se(IN_SET(fd_is_ns(fd, CLONE_NEWNET), 1, -EUCLEAN));
128 }
129
130 TEST(device_major_minor_valid) {
131 /* on glibc dev_t is 64bit, even though in the kernel it is only 32bit */
132 assert_cc(sizeof(dev_t) == sizeof(uint64_t));
133
134 assert_se(DEVICE_MAJOR_VALID(0U));
135 assert_se(DEVICE_MINOR_VALID(0U));
136
137 assert_se(DEVICE_MAJOR_VALID(1U));
138 assert_se(DEVICE_MINOR_VALID(1U));
139
140 assert_se(!DEVICE_MAJOR_VALID(-1U));
141 assert_se(!DEVICE_MINOR_VALID(-1U));
142
143 assert_se(DEVICE_MAJOR_VALID(1U << 10));
144 assert_se(DEVICE_MINOR_VALID(1U << 10));
145
146 assert_se(DEVICE_MAJOR_VALID((1U << 12) - 1));
147 assert_se(DEVICE_MINOR_VALID((1U << 20) - 1));
148
149 assert_se(!DEVICE_MAJOR_VALID((1U << 12)));
150 assert_se(!DEVICE_MINOR_VALID((1U << 20)));
151
152 assert_se(!DEVICE_MAJOR_VALID(1U << 25));
153 assert_se(!DEVICE_MINOR_VALID(1U << 25));
154
155 assert_se(!DEVICE_MAJOR_VALID(UINT32_MAX));
156 assert_se(!DEVICE_MINOR_VALID(UINT32_MAX));
157
158 assert_se(!DEVICE_MAJOR_VALID(UINT64_MAX));
159 assert_se(!DEVICE_MINOR_VALID(UINT64_MAX));
160
161 assert_se(DEVICE_MAJOR_VALID(major(0)));
162 assert_se(DEVICE_MINOR_VALID(minor(0)));
163 }
164
165 static void test_device_path_make_canonical_one(const char *path) {
166 _cleanup_free_ char *resolved = NULL, *raw = NULL;
167 struct stat st;
168 dev_t devno;
169 mode_t mode;
170 int r;
171
172 log_debug("> %s", path);
173
174 if (stat(path, &st) < 0) {
175 assert_se(errno == ENOENT);
176 log_notice("Path %s not found, skipping test", path);
177 return;
178 }
179
180 r = device_path_make_canonical(st.st_mode, st.st_rdev, &resolved);
181 if (r == -ENOENT) {
182 /* maybe /dev/char/x:y and /dev/block/x:y are missing in this test environment, because we
183 * run in a container or so? */
184 log_notice("Device %s cannot be resolved, skipping test", path);
185 return;
186 }
187
188 assert_se(r >= 0);
189 assert_se(path_equal(path, resolved));
190
191 assert_se(device_path_make_major_minor(st.st_mode, st.st_rdev, &raw) >= 0);
192 assert_se(device_path_parse_major_minor(raw, &mode, &devno) >= 0);
193
194 assert_se(st.st_rdev == devno);
195 assert_se((st.st_mode & S_IFMT) == (mode & S_IFMT));
196 }
197
198 TEST(device_path_make_canonical) {
199 test_device_path_make_canonical_one("/dev/null");
200 test_device_path_make_canonical_one("/dev/zero");
201 test_device_path_make_canonical_one("/dev/full");
202 test_device_path_make_canonical_one("/dev/random");
203 test_device_path_make_canonical_one("/dev/urandom");
204 test_device_path_make_canonical_one("/dev/tty");
205
206 if (is_device_node("/run/systemd/inaccessible/blk") > 0) {
207 test_device_path_make_canonical_one("/run/systemd/inaccessible/chr");
208 test_device_path_make_canonical_one("/run/systemd/inaccessible/blk");
209 }
210 }
211
212 TEST(dir_is_empty) {
213 _cleanup_(rm_rf_physical_and_freep) char *empty_dir = NULL;
214 _cleanup_free_ char *j = NULL, *jj = NULL;
215
216 assert_se(dir_is_empty_at(AT_FDCWD, "/proc") == 0);
217 assert_se(dir_is_empty_at(AT_FDCWD, "/icertainlydontexistdoi") == -ENOENT);
218
219 assert_se(mkdtemp_malloc("/tmp/emptyXXXXXX", &empty_dir) >= 0);
220 assert_se(dir_is_empty_at(AT_FDCWD, empty_dir) > 0);
221
222 j = path_join(empty_dir, "zzz");
223 assert_se(j);
224 assert_se(touch(j) >= 0);
225
226 assert_se(dir_is_empty_at(AT_FDCWD, empty_dir) == 0);
227
228 jj = path_join(empty_dir, "ppp");
229 assert_se(jj);
230 assert_se(touch(jj) >= 0);
231
232 assert_se(dir_is_empty_at(AT_FDCWD, empty_dir) == 0);
233 assert_se(unlink(j) >= 0);
234 assert_se(dir_is_empty_at(AT_FDCWD, empty_dir) == 0);
235 assert_se(unlink(jj) >= 0);
236 assert_se(dir_is_empty_at(AT_FDCWD, empty_dir) > 0);
237 }
238
239 DEFINE_CUSTOM_TEST_MAIN(LOG_INFO, log_show_color(true), /* no outro */);