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