xref: /freebsd/tests/sys/fs/fusefs/mount.cc (revision b61a573019428cbdc38ed03be9ecd2482011ea66)
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  * $FreeBSD$
31  */
32 
33 extern "C" {
34 #include <sys/param.h>
35 #include <sys/mount.h>
36 #include <sys/uio.h>
37 
38 #include "mntopts.h"	// for build_iovec
39 }
40 
41 #include "mockfs.hh"
42 #include "utils.hh"
43 
44 using namespace testing;
45 
46 class Mount: public FuseTest {
47 public:
48 void expect_statfs() {
49 	EXPECT_CALL(*m_mock, process(
50 		ResultOf([](auto in) {
51 			return (in.header.opcode == FUSE_STATFS);
52 		}, Eq(true)),
53 		_)
54 	).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
55 		SET_OUT_HEADER_LEN(out, statfs);
56 	})));
57 }
58 };
59 
60 class Fsname: public Mount {
61 	void SetUp() {
62 		m_fsname = "http://something";
63 		Mount::SetUp();
64 	}
65 };
66 
67 class Subtype: public Mount {
68 	void SetUp() {
69 		m_subtype = "myfs";
70 		Mount::SetUp();
71 	}
72 };
73 
74 class UpdateOk: public Mount, public WithParamInterface<const char*> {};
75 class UpdateErr: public Mount, public WithParamInterface<const char*> {};
76 
77 int mntflag_from_string(const char *s)
78 {
79 	if (0 == strcmp("MNT_RDONLY", s))
80 		return MNT_RDONLY;
81 	else if (0 == strcmp("MNT_NOEXEC", s))
82 		return MNT_NOEXEC;
83 	else if (0 == strcmp("MNT_NOSUID", s))
84 		return MNT_NOSUID;
85 	else if (0 == strcmp("MNT_NOATIME", s))
86 		return MNT_NOATIME;
87 	else if (0 == strcmp("MNT_SUIDDIR", s))
88 		return MNT_SUIDDIR;
89 	else if (0 == strcmp("MNT_USER", s))
90 		return MNT_USER;
91 	else
92 		return 0;
93 }
94 
95 TEST_F(Fsname, fsname)
96 {
97 	struct statfs statbuf;
98 
99 	expect_statfs();
100 
101 	ASSERT_EQ(0, statfs("mountpoint", &statbuf)) << strerror(errno);
102 	ASSERT_STREQ("http://something", statbuf.f_mntfromname);
103 }
104 
105 TEST_F(Subtype, subtype)
106 {
107 	struct statfs statbuf;
108 
109 	expect_statfs();
110 
111 	ASSERT_EQ(0, statfs("mountpoint", &statbuf)) << strerror(errno);
112 	ASSERT_STREQ("fusefs.myfs", statbuf.f_fstypename);
113 }
114 
115 /* Some mount options can be changed by mount -u */
116 TEST_P(UpdateOk, update)
117 {
118 	struct statfs statbuf;
119 	struct iovec *iov = NULL;
120 	int iovlen = 0;
121 	int flag;
122 	int newflags = MNT_UPDATE | MNT_SYNCHRONOUS;
123 
124 	flag = mntflag_from_string(GetParam());
125 	if (flag == MNT_NOSUID && 0 != geteuid())
126 		GTEST_SKIP() << "Only root may clear MNT_NOSUID";
127 	if (flag == MNT_SUIDDIR && 0 != geteuid())
128 		GTEST_SKIP() << "Only root may set MNT_SUIDDIR";
129 
130 	EXPECT_CALL(*m_mock, process(
131 		ResultOf([](auto in) {
132 			return (in.header.opcode == FUSE_STATFS);
133 		}, Eq(true)),
134 		_)
135 	).WillRepeatedly(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
136 		/*
137 		 * All of the fields except f_flags are don't care, and f_flags is set by
138 		 * the VFS
139 		 */
140 		SET_OUT_HEADER_LEN(out, statfs);
141 	})));
142 
143 	ASSERT_EQ(0, statfs("mountpoint", &statbuf)) << strerror(errno);
144 	newflags = (statbuf.f_flags | MNT_UPDATE) ^ flag;
145 
146 	build_iovec(&iov, &iovlen, "fstype", (void*)statbuf.f_fstypename, -1);
147 	build_iovec(&iov, &iovlen, "fspath", (void*)statbuf.f_mntonname, -1);
148 	build_iovec(&iov, &iovlen, "from", __DECONST(void *, "/dev/fuse"), -1);
149 	ASSERT_EQ(0, nmount(iov, iovlen, newflags)) << strerror(errno);
150 
151 	ASSERT_EQ(0, statfs("mountpoint", &statbuf)) << strerror(errno);
152 	EXPECT_FALSE((newflags ^ statbuf.f_flags) & flag);
153 }
154 
155 /* Some mount options cannnot be changed by mount -u */
156 TEST_P(UpdateErr, update)
157 {
158 	struct statfs statbuf;
159 	struct iovec *iov = NULL;
160 	int iovlen = 0;
161 	int flag;
162 	int newflags = MNT_UPDATE | MNT_SYNCHRONOUS;
163 
164 	flag = mntflag_from_string(GetParam());
165 	EXPECT_CALL(*m_mock, process(
166 		ResultOf([](auto in) {
167 			return (in.header.opcode == FUSE_STATFS);
168 		}, Eq(true)),
169 		_)
170 	).WillRepeatedly(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
171 		/*
172 		 * All of the fields except f_flags are don't care, and f_flags is set by
173 		 * the VFS
174 		 */
175 		SET_OUT_HEADER_LEN(out, statfs);
176 	})));
177 
178 	ASSERT_EQ(0, statfs("mountpoint", &statbuf)) << strerror(errno);
179 	newflags = (statbuf.f_flags | MNT_UPDATE) ^ flag;
180 
181 	build_iovec(&iov, &iovlen, "fstype", (void*)statbuf.f_fstypename, -1);
182 	build_iovec(&iov, &iovlen, "fspath", (void*)statbuf.f_mntonname, -1);
183 	build_iovec(&iov, &iovlen, "from", __DECONST(void *, "/dev/fuse"), -1);
184 	/*
185 	 * Don't check nmount's return value, because vfs_domount may "fix" the
186 	 * options for us.  The important thing is to check the final value of
187 	 * statbuf.f_flags below.
188 	 */
189 	(void)nmount(iov, iovlen, newflags);
190 
191 	ASSERT_EQ(0, statfs("mountpoint", &statbuf)) << strerror(errno);
192 	EXPECT_TRUE((newflags ^ statbuf.f_flags) & flag);
193 }
194 
195 INSTANTIATE_TEST_CASE_P(Mount, UpdateOk,
196 		::testing::Values("MNT_RDONLY", "MNT_NOEXEC", "MNT_NOSUID", "MNT_NOATIME",
197 		"MNT_SUIDDIR")
198 );
199 
200 INSTANTIATE_TEST_CASE_P(Mount, UpdateErr,
201 		::testing::Values( "MNT_USER");
202 );
203