]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-stat-util.c
Merge pull request #4991 from poettering/seccomp-fix
[thirdparty/systemd.git] / src / test / test-stat-util.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2010 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <fcntl.h>
21 #include <linux/magic.h>
22 #include <unistd.h>
23
24 #include "alloc-util.h"
25 #include "fd-util.h"
26 #include "fileio.h"
27 #include "macro.h"
28 #include "missing.h"
29 #include "stat-util.h"
30
31 static void test_files_same(void) {
32 _cleanup_close_ int fd = -1;
33 char name[] = "/tmp/test-files_same.XXXXXX";
34 char name_alias[] = "/tmp/test-files_same.alias";
35
36 fd = mkostemp_safe(name);
37 assert_se(fd >= 0);
38 assert_se(symlink(name, name_alias) >= 0);
39
40 assert_se(files_same(name, name));
41 assert_se(files_same(name, name_alias));
42
43 unlink(name);
44 unlink(name_alias);
45 }
46
47 static void test_is_symlink(void) {
48 char name[] = "/tmp/test-is_symlink.XXXXXX";
49 char name_link[] = "/tmp/test-is_symlink.link";
50 _cleanup_close_ int fd = -1;
51
52 fd = mkostemp_safe(name);
53 assert_se(fd >= 0);
54 assert_se(symlink(name, name_link) >= 0);
55
56 assert_se(is_symlink(name) == 0);
57 assert_se(is_symlink(name_link) == 1);
58 assert_se(is_symlink("/a/file/which/does/not/exist/i/guess") < 0);
59
60
61 unlink(name);
62 unlink(name_link);
63 }
64
65 static void test_path_is_os_tree(void) {
66 assert_se(path_is_os_tree("/") > 0);
67 assert_se(path_is_os_tree("/etc") == 0);
68 assert_se(path_is_os_tree("/idontexist") == -ENOENT);
69 }
70
71 static void test_path_check_fstype(void) {
72 assert_se(path_check_fstype("/run", TMPFS_MAGIC) > 0);
73 assert_se(path_check_fstype("/run", BTRFS_SUPER_MAGIC) == 0);
74 assert_se(path_check_fstype("/proc", PROC_SUPER_MAGIC) > 0);
75 assert_se(path_check_fstype("/proc", BTRFS_SUPER_MAGIC) == 0);
76 assert_se(path_check_fstype("/proc", BTRFS_SUPER_MAGIC) == 0);
77 assert_se(path_check_fstype("/i-dont-exist", BTRFS_SUPER_MAGIC) == -ENOENT);
78 }
79
80 static void test_path_is_temporary_fs(void) {
81 assert_se(path_is_temporary_fs("/run") > 0);
82 assert_se(path_is_temporary_fs("/proc") == 0);
83 assert_se(path_is_temporary_fs("/i-dont-exist") == -ENOENT);
84 }
85
86 int main(int argc, char *argv[]) {
87 test_files_same();
88 test_is_symlink();
89 test_path_is_os_tree();
90 test_path_check_fstype();
91 test_path_is_temporary_fs();
92
93 return 0;
94 }