]>
Commit | Line | Data |
---|---|---|
f4c13ad7 RC |
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> | |
436e916e | 21 | #include <linux/magic.h> |
f4c13ad7 RC |
22 | #include <unistd.h> |
23 | ||
24 | #include "alloc-util.h" | |
25 | #include "fd-util.h" | |
26 | #include "fileio.h" | |
27 | #include "macro.h" | |
436e916e | 28 | #include "missing.h" |
cc390161 | 29 | #include "mount-util.h" |
f4c13ad7 RC |
30 | #include "stat-util.h" |
31 | ||
32 | static void test_files_same(void) { | |
33 | _cleanup_close_ int fd = -1; | |
34 | char name[] = "/tmp/test-files_same.XXXXXX"; | |
35 | char name_alias[] = "/tmp/test-files_same.alias"; | |
36 | ||
646853bd | 37 | fd = mkostemp_safe(name); |
f4c13ad7 RC |
38 | assert_se(fd >= 0); |
39 | assert_se(symlink(name, name_alias) >= 0); | |
40 | ||
41 | assert_se(files_same(name, name)); | |
42 | assert_se(files_same(name, name_alias)); | |
43 | ||
44 | unlink(name); | |
45 | unlink(name_alias); | |
46 | } | |
47 | ||
48 | static void test_is_symlink(void) { | |
49 | char name[] = "/tmp/test-is_symlink.XXXXXX"; | |
50 | char name_link[] = "/tmp/test-is_symlink.link"; | |
51 | _cleanup_close_ int fd = -1; | |
52 | ||
646853bd | 53 | fd = mkostemp_safe(name); |
f4c13ad7 RC |
54 | assert_se(fd >= 0); |
55 | assert_se(symlink(name, name_link) >= 0); | |
56 | ||
57 | assert_se(is_symlink(name) == 0); | |
58 | assert_se(is_symlink(name_link) == 1); | |
59 | assert_se(is_symlink("/a/file/which/does/not/exist/i/guess") < 0); | |
60 | ||
61 | ||
62 | unlink(name); | |
63 | unlink(name_link); | |
64 | } | |
65 | ||
7dcdb24e LP |
66 | static void test_path_is_os_tree(void) { |
67 | assert_se(path_is_os_tree("/") > 0); | |
68 | assert_se(path_is_os_tree("/etc") == 0); | |
69 | assert_se(path_is_os_tree("/idontexist") == -ENOENT); | |
70 | } | |
71 | ||
436e916e | 72 | static void test_path_check_fstype(void) { |
cc390161 MP |
73 | /* run might not be a mount point in build chroots */ |
74 | if (path_is_mount_point("/run", NULL, AT_SYMLINK_FOLLOW) > 0) { | |
75 | assert_se(path_check_fstype("/run", TMPFS_MAGIC) > 0); | |
76 | assert_se(path_check_fstype("/run", BTRFS_SUPER_MAGIC) == 0); | |
77 | } | |
436e916e LP |
78 | assert_se(path_check_fstype("/proc", PROC_SUPER_MAGIC) > 0); |
79 | assert_se(path_check_fstype("/proc", BTRFS_SUPER_MAGIC) == 0); | |
80 | assert_se(path_check_fstype("/proc", BTRFS_SUPER_MAGIC) == 0); | |
81 | assert_se(path_check_fstype("/i-dont-exist", BTRFS_SUPER_MAGIC) == -ENOENT); | |
82 | } | |
83 | ||
84 | static void test_path_is_temporary_fs(void) { | |
8dfc2f40 MP |
85 | /* run might not be a mount point in build chroots */ |
86 | if (path_is_mount_point("/run", NULL, AT_SYMLINK_FOLLOW) > 0) | |
87 | assert_se(path_is_temporary_fs("/run") > 0); | |
436e916e LP |
88 | assert_se(path_is_temporary_fs("/proc") == 0); |
89 | assert_se(path_is_temporary_fs("/i-dont-exist") == -ENOENT); | |
90 | } | |
91 | ||
f4c13ad7 RC |
92 | int main(int argc, char *argv[]) { |
93 | test_files_same(); | |
94 | test_is_symlink(); | |
7dcdb24e | 95 | test_path_is_os_tree(); |
436e916e LP |
96 | test_path_check_fstype(); |
97 | test_path_is_temporary_fs(); | |
f4c13ad7 RC |
98 | |
99 | return 0; | |
100 | } |