xref: /freebsd/tests/sys/fs/fusefs/mount.cc (revision d0b2dbfa)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2019 The FreeBSD Foundation
5  *
6  * This software was developed by BFF Storage Systems, LLC under sponsorship
7  * from the FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 extern "C" {
32 #include <sys/param.h>
33 #include <sys/mount.h>
34 #include <sys/uio.h>
35 
36 #include "mntopts.h"	// for build_iovec
37 }
38 
39 #include "mockfs.hh"
40 #include "utils.hh"
41 
42 using namespace testing;
43 
44 class Mount: public FuseTest {
45 public:
46 void expect_statfs() {
47 	EXPECT_CALL(*m_mock, process(
48 		ResultOf([](auto in) {
49 			return (in.header.opcode == FUSE_STATFS);
50 		}, Eq(true)),
51 		_)
52 	).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
53 		SET_OUT_HEADER_LEN(out, statfs);
54 	})));
55 }
56 };
57 
58 class Fsname: public Mount {
59 	void SetUp() {
60 		m_fsname = "http://something";
61 		Mount::SetUp();
62 	}
63 };
64 
65 class Subtype: public Mount {
66 	void SetUp() {
67 		m_subtype = "myfs";
68 		Mount::SetUp();
69 	}
70 };
71 
72 class UpdateOk: public Mount, public WithParamInterface<const char*> {};
73 class UpdateErr: public Mount, public WithParamInterface<const char*> {};
74 
75 int mntflag_from_string(const char *s)
76 {
77 	if (0 == strcmp("MNT_RDONLY", s))
78 		return MNT_RDONLY;
79 	else if (0 == strcmp("MNT_NOEXEC", s))
80 		return MNT_NOEXEC;
81 	else if (0 == strcmp("MNT_NOSUID", s))
82 		return MNT_NOSUID;
83 	else if (0 == strcmp("MNT_NOATIME", s))
84 		return MNT_NOATIME;
85 	else if (0 == strcmp("MNT_SUIDDIR", s))
86 		return MNT_SUIDDIR;
87 	else if (0 == strcmp("MNT_USER", s))
88 		return MNT_USER;
89 	else
90 		return 0;
91 }
92 
93 TEST_F(Fsname, fsname)
94 {
95 	struct statfs statbuf;
96 
97 	expect_statfs();
98 
99 	ASSERT_EQ(0, statfs("mountpoint", &statbuf)) << strerror(errno);
100 	ASSERT_STREQ("http://something", statbuf.f_mntfromname);
101 }
102 
103 TEST_F(Subtype, subtype)
104 {
105 	struct statfs statbuf;
106 
107 	expect_statfs();
108 
109 	ASSERT_EQ(0, statfs("mountpoint", &statbuf)) << strerror(errno);
110 	ASSERT_STREQ("fusefs.myfs", statbuf.f_fstypename);
111 }
112 
113 /* Some mount options can be changed by mount -u */
114 TEST_P(UpdateOk, update)
115 {
116 	struct statfs statbuf;
117 	struct iovec *iov = NULL;
118 	int iovlen = 0;
119 	int flag;
120 	int newflags = MNT_UPDATE | MNT_SYNCHRONOUS;
121 
122 	flag = mntflag_from_string(GetParam());
123 	if (flag == MNT_NOSUID && 0 != geteuid())
124 		GTEST_SKIP() << "Only root may clear MNT_NOSUID";
125 	if (flag == MNT_SUIDDIR && 0 != geteuid())
126 		GTEST_SKIP() << "Only root may set MNT_SUIDDIR";
127 
128 	EXPECT_CALL(*m_mock, process(
129 		ResultOf([](auto in) {
130 			return (in.header.opcode == FUSE_STATFS);
131 		}, Eq(true)),
132 		_)
133 	).WillRepeatedly(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
134 		/*
135 		 * All of the fields except f_flags are don't care, and f_flags is set by
136 		 * the VFS
137 		 */
138 		SET_OUT_HEADER_LEN(out, statfs);
139 	})));
140 
141 	ASSERT_EQ(0, statfs("mountpoint", &statbuf)) << strerror(errno);
142 	newflags = (statbuf.f_flags | MNT_UPDATE) ^ flag;
143 
144 	build_iovec(&iov, &iovlen, "fstype", (void*)statbuf.f_fstypename, -1);
145 	build_iovec(&iov, &iovlen, "fspath", (void*)statbuf.f_mntonname, -1);
146 	build_iovec(&iov, &iovlen, "from", __DECONST(void *, "/dev/fuse"), -1);
147 	ASSERT_EQ(0, nmount(iov, iovlen, newflags)) << strerror(errno);
148 
149 	ASSERT_EQ(0, statfs("mountpoint", &statbuf)) << strerror(errno);
150 	EXPECT_FALSE((newflags ^ statbuf.f_flags) & flag);
151 }
152 
153 /* Some mount options cannnot be changed by mount -u */
154 TEST_P(UpdateErr, update)
155 {
156 	struct statfs statbuf;
157 	struct iovec *iov = NULL;
158 	int iovlen = 0;
159 	int flag;
160 	int newflags = MNT_UPDATE | MNT_SYNCHRONOUS;
161 
162 	flag = mntflag_from_string(GetParam());
163 	EXPECT_CALL(*m_mock, process(
164 		ResultOf([](auto in) {
165 			return (in.header.opcode == FUSE_STATFS);
166 		}, Eq(true)),
167 		_)
168 	).WillRepeatedly(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
169 		/*
170 		 * All of the fields except f_flags are don't care, and f_flags is set by
171 		 * the VFS
172 		 */
173 		SET_OUT_HEADER_LEN(out, statfs);
174 	})));
175 
176 	ASSERT_EQ(0, statfs("mountpoint", &statbuf)) << strerror(errno);
177 	newflags = (statbuf.f_flags | MNT_UPDATE) ^ flag;
178 
179 	build_iovec(&iov, &iovlen, "fstype", (void*)statbuf.f_fstypename, -1);
180 	build_iovec(&iov, &iovlen, "fspath", (void*)statbuf.f_mntonname, -1);
181 	build_iovec(&iov, &iovlen, "from", __DECONST(void *, "/dev/fuse"), -1);
182 	/*
183 	 * Don't check nmount's return value, because vfs_domount may "fix" the
184 	 * options for us.  The important thing is to check the final value of
185 	 * statbuf.f_flags below.
186 	 */
187 	(void)nmount(iov, iovlen, newflags);
188 
189 	ASSERT_EQ(0, statfs("mountpoint", &statbuf)) << strerror(errno);
190 	EXPECT_TRUE((newflags ^ statbuf.f_flags) & flag);
191 }
192 
193 INSTANTIATE_TEST_SUITE_P(Mount, UpdateOk,
194 		::testing::Values("MNT_RDONLY", "MNT_NOEXEC", "MNT_NOSUID", "MNT_NOATIME",
195 		"MNT_SUIDDIR")
196 );
197 
198 INSTANTIATE_TEST_SUITE_P(Mount, UpdateErr,
199 		::testing::Values( "MNT_USER")
200 );
201