]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-stat-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
4 #include <linux/magic.h>
7 #include "alloc-util.h"
11 #include "mountpoint-util.h"
12 #include "namespace-util.h"
13 #include "path-util.h"
14 #include "stat-util.h"
15 #include "tmpfile-util.h"
17 static void test_files_same(void) {
18 _cleanup_close_
int fd
= -1;
19 char name
[] = "/tmp/test-files_same.XXXXXX";
20 char name_alias
[] = "/tmp/test-files_same.alias";
22 fd
= mkostemp_safe(name
);
24 assert_se(symlink(name
, name_alias
) >= 0);
26 assert_se(files_same(name
, name
, 0));
27 assert_se(files_same(name
, name
, AT_SYMLINK_NOFOLLOW
));
28 assert_se(files_same(name
, name_alias
, 0));
29 assert_se(!files_same(name
, name_alias
, AT_SYMLINK_NOFOLLOW
));
35 static void test_is_symlink(void) {
36 char name
[] = "/tmp/test-is_symlink.XXXXXX";
37 char name_link
[] = "/tmp/test-is_symlink.link";
38 _cleanup_close_
int fd
= -1;
40 fd
= mkostemp_safe(name
);
42 assert_se(symlink(name
, name_link
) >= 0);
44 assert_se(is_symlink(name
) == 0);
45 assert_se(is_symlink(name_link
) == 1);
46 assert_se(is_symlink("/a/file/which/does/not/exist/i/guess") < 0);
52 static void test_path_is_fs_type(void) {
53 /* run might not be a mount point in build chroots */
54 if (path_is_mount_point("/run", NULL
, AT_SYMLINK_FOLLOW
) > 0) {
55 assert_se(path_is_fs_type("/run", TMPFS_MAGIC
) > 0);
56 assert_se(path_is_fs_type("/run", BTRFS_SUPER_MAGIC
) == 0);
58 assert_se(path_is_fs_type("/proc", PROC_SUPER_MAGIC
) > 0);
59 assert_se(path_is_fs_type("/proc", BTRFS_SUPER_MAGIC
) == 0);
60 assert_se(path_is_fs_type("/i-dont-exist", BTRFS_SUPER_MAGIC
) == -ENOENT
);
63 static void test_path_is_temporary_fs(void) {
64 /* run might not be a mount point in build chroots */
65 if (path_is_mount_point("/run", NULL
, AT_SYMLINK_FOLLOW
) > 0)
66 assert_se(path_is_temporary_fs("/run") > 0);
67 assert_se(path_is_temporary_fs("/proc") == 0);
68 assert_se(path_is_temporary_fs("/i-dont-exist") == -ENOENT
);
71 static void test_fd_is_network_ns(void) {
72 _cleanup_close_
int fd
= -1;
73 assert_se(fd_is_network_ns(STDIN_FILENO
) == 0);
74 assert_se(fd_is_network_ns(STDERR_FILENO
) == 0);
75 assert_se(fd_is_network_ns(STDOUT_FILENO
) == 0);
77 assert_se((fd
= open("/proc/self/ns/mnt", O_CLOEXEC
|O_RDONLY
)) >= 0);
78 assert_se(IN_SET(fd_is_network_ns(fd
), 0, -EUCLEAN
));
81 assert_se((fd
= open("/proc/self/ns/net", O_CLOEXEC
|O_RDONLY
)) >= 0);
82 assert_se(IN_SET(fd_is_network_ns(fd
), 1, -EUCLEAN
));
85 static void test_device_major_minor_valid(void) {
86 /* on glibc dev_t is 64bit, even though in the kernel it is only 32bit */
87 assert_cc(sizeof(dev_t
) == sizeof(uint64_t));
89 assert_se(DEVICE_MAJOR_VALID(0U));
90 assert_se(DEVICE_MINOR_VALID(0U));
92 assert_se(DEVICE_MAJOR_VALID(1U));
93 assert_se(DEVICE_MINOR_VALID(1U));
95 assert_se(!DEVICE_MAJOR_VALID(-1U));
96 assert_se(!DEVICE_MINOR_VALID(-1U));
98 assert_se(DEVICE_MAJOR_VALID(1U << 10));
99 assert_se(DEVICE_MINOR_VALID(1U << 10));
101 assert_se(DEVICE_MAJOR_VALID((1U << 12) - 1));
102 assert_se(DEVICE_MINOR_VALID((1U << 20) - 1));
104 assert_se(!DEVICE_MAJOR_VALID((1U << 12)));
105 assert_se(!DEVICE_MINOR_VALID((1U << 20)));
107 assert_se(!DEVICE_MAJOR_VALID(1U << 25));
108 assert_se(!DEVICE_MINOR_VALID(1U << 25));
110 assert_se(!DEVICE_MAJOR_VALID(UINT32_MAX
));
111 assert_se(!DEVICE_MINOR_VALID(UINT32_MAX
));
113 assert_se(!DEVICE_MAJOR_VALID(UINT64_MAX
));
114 assert_se(!DEVICE_MINOR_VALID(UINT64_MAX
));
116 assert_se(DEVICE_MAJOR_VALID(major(0)));
117 assert_se(DEVICE_MINOR_VALID(minor(0)));
120 static void test_device_path_make_canonical_one(const char *path
) {
121 _cleanup_free_
char *resolved
= NULL
, *raw
= NULL
;
127 assert_se(stat(path
, &st
) >= 0);
128 r
= device_path_make_canonical(st
.st_mode
, st
.st_rdev
, &resolved
);
129 if (r
== -ENOENT
) /* maybe /dev/char/x:y and /dev/block/x:y are missing in this test environment, because we
130 * run in a container or so? */
134 assert_se(path_equal(path
, resolved
));
136 assert_se(device_path_make_major_minor(st
.st_mode
, st
.st_rdev
, &raw
) >= 0);
137 assert_se(device_path_parse_major_minor(raw
, &mode
, &devno
) >= 0);
139 assert_se(st
.st_rdev
== devno
);
140 assert_se((st
.st_mode
& S_IFMT
) == (mode
& S_IFMT
));
143 static void test_device_path_make_canonical(void) {
145 test_device_path_make_canonical_one("/dev/null");
146 test_device_path_make_canonical_one("/dev/zero");
147 test_device_path_make_canonical_one("/dev/full");
148 test_device_path_make_canonical_one("/dev/random");
149 test_device_path_make_canonical_one("/dev/urandom");
150 test_device_path_make_canonical_one("/dev/tty");
152 if (is_device_node("/run/systemd/inaccessible/chr") > 0) {
153 test_device_path_make_canonical_one("/run/systemd/inaccessible/chr");
154 test_device_path_make_canonical_one("/run/systemd/inaccessible/blk");
158 int main(int argc
, char *argv
[]) {
161 test_path_is_fs_type();
162 test_path_is_temporary_fs();
163 test_fd_is_network_ns();
164 test_device_major_minor_valid();
165 test_device_path_make_canonical();