chiark / gitweb /
tree-wide: remove Lennart's copyright lines
[elogind.git] / src / test / test-stat-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <fcntl.h>
4 #include <linux/magic.h>
5 #include <unistd.h>
6
7 #include "alloc-util.h"
8 #include "fd-util.h"
9 #include "fileio.h"
10 #include "macro.h"
11 #include "missing.h"
12 #include "mount-util.h"
13 #include "stat-util.h"
14
15 static void test_files_same(void) {
16         _cleanup_close_ int fd = -1;
17         char name[] = "/tmp/test-files_same.XXXXXX";
18         char name_alias[] = "/tmp/test-files_same.alias";
19
20         fd = mkostemp_safe(name);
21         assert_se(fd >= 0);
22         assert_se(symlink(name, name_alias) >= 0);
23
24         assert_se(files_same(name, name, 0));
25         assert_se(files_same(name, name, AT_SYMLINK_NOFOLLOW));
26         assert_se(files_same(name, name_alias, 0));
27         assert_se(!files_same(name, name_alias, AT_SYMLINK_NOFOLLOW));
28
29         unlink(name);
30         unlink(name_alias);
31 }
32
33 #if 0 /// UNNEEDED by elogind
34 static void test_is_symlink(void) {
35         char name[] = "/tmp/test-is_symlink.XXXXXX";
36         char name_link[] = "/tmp/test-is_symlink.link";
37         _cleanup_close_ int fd = -1;
38
39         fd = mkostemp_safe(name);
40         assert_se(fd >= 0);
41         assert_se(symlink(name, name_link) >= 0);
42
43         assert_se(is_symlink(name) == 0);
44         assert_se(is_symlink(name_link) == 1);
45         assert_se(is_symlink("/a/file/which/does/not/exist/i/guess") < 0);
46
47         unlink(name);
48         unlink(name_link);
49 }
50
51 static void test_path_is_fs_type(void) {
52         /* run might not be a mount point in build chroots */
53         if (path_is_mount_point("/run", NULL, AT_SYMLINK_FOLLOW) > 0) {
54                 assert_se(path_is_fs_type("/run", TMPFS_MAGIC) > 0);
55                 assert_se(path_is_fs_type("/run", BTRFS_SUPER_MAGIC) == 0);
56         }
57         assert_se(path_is_fs_type("/proc", PROC_SUPER_MAGIC) > 0);
58         assert_se(path_is_fs_type("/proc", BTRFS_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);
61 }
62
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);
69 }
70 #endif // 0
71
72 int main(int argc, char *argv[]) {
73         test_files_same();
74 #if 0 /// UNNEEDED by elogind
75         test_is_symlink();
76         test_path_is_fs_type();
77         test_path_is_temporary_fs();
78 #endif // 0
79
80         return 0;
81 }