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