chiark / gitweb /
52142a53f30e0a0de7ff1321d1043512257f3660
[elogind.git] / src / test / test-stat-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3   This file is part of systemd.
4
5   Copyright 2010 Lennart Poettering
6 ***/
7
8 #include <fcntl.h>
9 #include <linux/magic.h>
10 #include <unistd.h>
11
12 #include "alloc-util.h"
13 #include "fd-util.h"
14 #include "fileio.h"
15 #include "macro.h"
16 #include "missing.h"
17 #include "mount-util.h"
18 #include "stat-util.h"
19
20 static void test_files_same(void) {
21         _cleanup_close_ int fd = -1;
22         char name[] = "/tmp/test-files_same.XXXXXX";
23         char name_alias[] = "/tmp/test-files_same.alias";
24
25         fd = mkostemp_safe(name);
26         assert_se(fd >= 0);
27         assert_se(symlink(name, name_alias) >= 0);
28
29         assert_se(files_same(name, name, 0));
30         assert_se(files_same(name, name, AT_SYMLINK_NOFOLLOW));
31         assert_se(files_same(name, name_alias, 0));
32         assert_se(!files_same(name, name_alias, AT_SYMLINK_NOFOLLOW));
33
34         unlink(name);
35         unlink(name_alias);
36 }
37
38 #if 0 /// UNNEEDED by elogind
39 static void test_is_symlink(void) {
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 static void test_path_is_fs_type(void) {
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         assert_se(path_is_fs_type("/proc", PROC_SUPER_MAGIC) > 0);
63         assert_se(path_is_fs_type("/proc", BTRFS_SUPER_MAGIC) == 0);
64         assert_se(path_is_fs_type("/proc", BTRFS_SUPER_MAGIC) == 0);
65         assert_se(path_is_fs_type("/i-dont-exist", BTRFS_SUPER_MAGIC) == -ENOENT);
66 }
67
68 static void test_path_is_temporary_fs(void) {
69         /* run might not be a mount point in build chroots */
70         if (path_is_mount_point("/run", NULL, AT_SYMLINK_FOLLOW) > 0)
71                 assert_se(path_is_temporary_fs("/run") > 0);
72         assert_se(path_is_temporary_fs("/proc") == 0);
73         assert_se(path_is_temporary_fs("/i-dont-exist") == -ENOENT);
74 }
75 #endif // 0
76
77 int main(int argc, char *argv[]) {
78         test_files_same();
79 #if 0 /// UNNEEDED by elogind
80         test_is_symlink();
81         test_path_is_fs_type();
82         test_path_is_temporary_fs();
83 #endif // 0
84
85         return 0;
86 }