]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-mount-util.c
mount-util: fix bad indenting
[thirdparty/systemd.git] / src / test / test-mount-util.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
83555251
LP
2/***
3 This file is part of systemd.
4
5 Copyright 2016 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 <sys/mount.h>
22
c2a986d5
LP
23#include "alloc-util.h"
24#include "def.h"
25#include "fd-util.h"
26#include "fileio.h"
27#include "hashmap.h"
83555251
LP
28#include "log.h"
29#include "mount-util.h"
30#include "string-util.h"
31
c7383828
ZJS
32static void test_mount_propagation_flags(const char *name, int ret, unsigned long expected) {
33 long unsigned flags;
83555251 34
85e55d14 35 assert_se(mount_propagation_flags_from_string(name, &flags) == ret);
c7383828
ZJS
36
37 if (ret >= 0) {
38 const char *c;
39
40 assert_se(flags == expected);
41
42 c = mount_propagation_flags_to_string(flags);
43 if (isempty(name))
44 assert_se(isempty(c));
45 else
46 assert_se(streq(c, name));
47 }
83555251
LP
48}
49
c2a986d5
LP
50static void test_mnt_id(void) {
51 _cleanup_fclose_ FILE *f = NULL;
52 Hashmap *h;
53 Iterator i;
54 char *k;
55 void *p;
56 int r;
57
58 assert_se(f = fopen("/proc/self/mountinfo", "re"));
59 assert_se(h = hashmap_new(&string_hash_ops));
60
61 for (;;) {
62 _cleanup_free_ char *line = NULL, *path = NULL;
63 void *old_key;
64 int mnt_id;
65
66 r = read_line(f, LONG_LINE_MAX, &line);
67 if (r == 0)
68 break;
69 assert_se(r > 0);
70
71 assert_se(sscanf(line, "%i %*s %*s %*s %ms", &mnt_id, &path) == 2);
72
73 /* Add all mount points and their ids to a hashtable, so that we filter out mount points that are
74 * overmounted. For those we only care for the "upper" mount, since that's the only one
75 * path_get_mnt_id() can determine. */
76
77 if (hashmap_remove2(h, path, &old_key))
78 free(old_key);
79
80 assert_se(hashmap_put(h, path, INT_TO_PTR(mnt_id)) >= 0);
81 path = NULL;
82 }
83
84 HASHMAP_FOREACH_KEY(p, k, h, i) {
85 int mnt_id = PTR_TO_INT(p), mnt_id2;
86
87 r = path_get_mnt_id(k, &mnt_id2);
88 if (r == -EOPNOTSUPP) { /* kernel or file system too old? */
89 log_debug("%s doesn't support mount IDs\n", k);
90 continue;
91 }
92 if (IN_SET(r, -EACCES, -EPERM)) {
93 log_debug("Can't access %s\n", k);
94 continue;
95 }
96
97 log_debug("mnt id of %s is %i\n", k, mnt_id2);
98
99 assert_se(r >= 0);
100 assert_se(mnt_id == mnt_id2);
101 }
102
103 while ((k = hashmap_steal_first_key(h)))
104 free(k);
105
106 hashmap_free(h);
107}
108
83555251
LP
109int main(int argc, char *argv[]) {
110
111 log_set_max_level(LOG_DEBUG);
112
c7383828
ZJS
113 test_mount_propagation_flags("shared", 0, MS_SHARED);
114 test_mount_propagation_flags("slave", 0, MS_SLAVE);
115 test_mount_propagation_flags("private", 0, MS_PRIVATE);
116 test_mount_propagation_flags(NULL, 0, 0);
117 test_mount_propagation_flags("", 0, 0);
118 test_mount_propagation_flags("xxxx", -EINVAL, 0);
119 test_mount_propagation_flags(" ", -EINVAL, 0);
83555251 120
c2a986d5
LP
121 test_mnt_id();
122
83555251
LP
123 return 0;
124}