]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-stat-util.c
1697b2d777f99e57c4e5469a8649a7166261db6d
[thirdparty/systemd.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 static void test_is_symlink(void) {
39 char name[] = "/tmp/test-is_symlink.XXXXXX";
40 char name_link[] = "/tmp/test-is_symlink.link";
41 _cleanup_close_ int fd = -1;
42
43 fd = mkostemp_safe(name);
44 assert_se(fd >= 0);
45 assert_se(symlink(name, name_link) >= 0);
46
47 assert_se(is_symlink(name) == 0);
48 assert_se(is_symlink(name_link) == 1);
49 assert_se(is_symlink("/a/file/which/does/not/exist/i/guess") < 0);
50
51 unlink(name);
52 unlink(name_link);
53 }
54
55 static void test_path_is_fs_type(void) {
56 /* run might not be a mount point in build chroots */
57 if (path_is_mount_point("/run", NULL, AT_SYMLINK_FOLLOW) > 0) {
58 assert_se(path_is_fs_type("/run", TMPFS_MAGIC) > 0);
59 assert_se(path_is_fs_type("/run", BTRFS_SUPER_MAGIC) == 0);
60 }
61 assert_se(path_is_fs_type("/proc", PROC_SUPER_MAGIC) > 0);
62 assert_se(path_is_fs_type("/proc", BTRFS_SUPER_MAGIC) == 0);
63 assert_se(path_is_fs_type("/proc", BTRFS_SUPER_MAGIC) == 0);
64 assert_se(path_is_fs_type("/i-dont-exist", BTRFS_SUPER_MAGIC) == -ENOENT);
65 }
66
67 static void test_path_is_temporary_fs(void) {
68 /* run might not be a mount point in build chroots */
69 if (path_is_mount_point("/run", NULL, AT_SYMLINK_FOLLOW) > 0)
70 assert_se(path_is_temporary_fs("/run") > 0);
71 assert_se(path_is_temporary_fs("/proc") == 0);
72 assert_se(path_is_temporary_fs("/i-dont-exist") == -ENOENT);
73 }
74
75 int main(int argc, char *argv[]) {
76 test_files_same();
77 test_is_symlink();
78 test_path_is_fs_type();
79 test_path_is_temporary_fs();
80
81 return 0;
82 }