]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-mount-util.c
test: fix test-mount-util when handling duplicate mounts on the same location
[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"
9b1573ef 30#include "path-util.h"
83555251
LP
31#include "string-util.h"
32
c7383828
ZJS
33static void test_mount_propagation_flags(const char *name, int ret, unsigned long expected) {
34 long unsigned flags;
83555251 35
85e55d14 36 assert_se(mount_propagation_flags_from_string(name, &flags) == ret);
c7383828
ZJS
37
38 if (ret >= 0) {
39 const char *c;
40
41 assert_se(flags == expected);
42
43 c = mount_propagation_flags_to_string(flags);
44 if (isempty(name))
45 assert_se(isempty(c));
46 else
47 assert_se(streq(c, name));
48 }
83555251
LP
49}
50
c2a986d5
LP
51static void test_mnt_id(void) {
52 _cleanup_fclose_ FILE *f = NULL;
53 Hashmap *h;
54 Iterator i;
9b1573ef
LP
55 char *p;
56 void *k;
c2a986d5
LP
57 int r;
58
59 assert_se(f = fopen("/proc/self/mountinfo", "re"));
9b1573ef 60 assert_se(h = hashmap_new(&trivial_hash_ops));
c2a986d5
LP
61
62 for (;;) {
63 _cleanup_free_ char *line = NULL, *path = NULL;
c2a986d5
LP
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
9b1573ef 73 assert_se(hashmap_put(h, INT_TO_PTR(mnt_id), path) >= 0);
c2a986d5
LP
74 path = NULL;
75 }
76
77 HASHMAP_FOREACH_KEY(p, k, h, i) {
9b1573ef 78 int mnt_id = PTR_TO_INT(k), mnt_id2;
c2a986d5 79
9b1573ef 80 r = path_get_mnt_id(p, &mnt_id2);
c2a986d5 81 if (r == -EOPNOTSUPP) { /* kernel or file system too old? */
9b1573ef 82 log_debug("%s doesn't support mount IDs\n", p);
c2a986d5
LP
83 continue;
84 }
85 if (IN_SET(r, -EACCES, -EPERM)) {
9b1573ef 86 log_debug("Can't access %s\n", p);
c2a986d5
LP
87 continue;
88 }
89
9b1573ef
LP
90 log_debug("mnt id of %s is %i\n", p, mnt_id2);
91
92 if (mnt_id == mnt_id2)
93 continue;
c2a986d5 94
9b1573ef
LP
95 /* The ids don't match? If so, then there are two mounts on the same path, let's check if that's really
96 * the case */
97 assert_se(path_equal_ptr(hashmap_get(h, INT_TO_PTR(mnt_id2)), p));
c2a986d5
LP
98 }
99
9b1573ef
LP
100 while ((p = hashmap_steal_first(h)))
101 free(p);
c2a986d5
LP
102
103 hashmap_free(h);
104}
105
83555251
LP
106int main(int argc, char *argv[]) {
107
108 log_set_max_level(LOG_DEBUG);
109
c7383828
ZJS
110 test_mount_propagation_flags("shared", 0, MS_SHARED);
111 test_mount_propagation_flags("slave", 0, MS_SLAVE);
112 test_mount_propagation_flags("private", 0, MS_PRIVATE);
113 test_mount_propagation_flags(NULL, 0, 0);
114 test_mount_propagation_flags("", 0, 0);
115 test_mount_propagation_flags("xxxx", -EINVAL, 0);
116 test_mount_propagation_flags(" ", -EINVAL, 0);
83555251 117
c2a986d5
LP
118 test_mnt_id();
119
83555251
LP
120 return 0;
121}