xref: /freebsd/tests/sys/fs/fusefs/mount.cc (revision 0bf48626aaa33768078f5872b922b1487b3a9296)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 UpdateOk: public FuseTest, public WithParamInterface<const char*> {};
45 class UpdateErr: public FuseTest, public WithParamInterface<const char*> {};
46 
47 int mntflag_from_string(const char *s)
48 {
49 	if (0 == strcmp("MNT_RDONLY", s))
50 		return MNT_RDONLY;
51 	else if (0 == strcmp("MNT_NOEXEC", s))
52 		return MNT_NOEXEC;
53 	else if (0 == strcmp("MNT_NOSUID", s))
54 		return MNT_NOSUID;
55 	else if (0 == strcmp("MNT_NOATIME", s))
56 		return MNT_NOATIME;
57 	else if (0 == strcmp("MNT_SUIDDIR", s))
58 		return MNT_SUIDDIR;
59 	else if (0 == strcmp("MNT_USER", s))
60 		return MNT_USER;
61 	else
62 		return 0;
63 }
64 
65 /* Some mount options can be changed by mount -u */
66 TEST_P(UpdateOk, update)
67 {
68 	struct statfs statbuf;
69 	struct iovec *iov = NULL;
70 	int iovlen = 0;
71 	int flag;
72 	int newflags = MNT_UPDATE | MNT_SYNCHRONOUS;
73 
74 	flag = mntflag_from_string(GetParam());
75 	if (flag == MNT_NOSUID && 0 != geteuid())
76 		GTEST_SKIP() << "Only root may clear MNT_NOSUID";
77 	if (flag == MNT_SUIDDIR && 0 != geteuid())
78 		GTEST_SKIP() << "Only root may set MNT_SUIDDIR";
79 
80 	EXPECT_CALL(*m_mock, process(
81 		ResultOf([](auto in) {
82 			return (in.header.opcode == FUSE_STATFS);
83 		}, Eq(true)),
84 		_)
85 	).WillRepeatedly(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
86 		/*
87 		 * All of the fields except f_flags are don't care, and f_flags is set by
88 		 * the VFS
89 		 */
90 		SET_OUT_HEADER_LEN(out, statfs);
91 	})));
92 
93 	ASSERT_EQ(0, statfs("mountpoint", &statbuf)) << strerror(errno);
94 	newflags = (statbuf.f_flags | MNT_UPDATE) ^ flag;
95 
96 	build_iovec(&iov, &iovlen, "fstype", (void*)statbuf.f_fstypename, -1);
97 	build_iovec(&iov, &iovlen, "fspath", (void*)statbuf.f_mntonname, -1);
98 	build_iovec(&iov, &iovlen, "from", __DECONST(void *, "/dev/fuse"), -1);
99 	ASSERT_EQ(0, nmount(iov, iovlen, newflags)) << strerror(errno);
100 
101 	ASSERT_EQ(0, statfs("mountpoint", &statbuf)) << strerror(errno);
102 	EXPECT_FALSE((newflags ^ statbuf.f_flags) & flag);
103 }
104 
105 /* Some mount options cannnot be changed by mount -u */
106 TEST_P(UpdateErr, update)
107 {
108 	struct statfs statbuf;
109 	struct iovec *iov = NULL;
110 	int iovlen = 0;
111 	int flag;
112 	int newflags = MNT_UPDATE | MNT_SYNCHRONOUS;
113 
114 	flag = mntflag_from_string(GetParam());
115 	EXPECT_CALL(*m_mock, process(
116 		ResultOf([](auto in) {
117 			return (in.header.opcode == FUSE_STATFS);
118 		}, Eq(true)),
119 		_)
120 	).WillRepeatedly(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
121 		/*
122 		 * All of the fields except f_flags are don't care, and f_flags is set by
123 		 * the VFS
124 		 */
125 		SET_OUT_HEADER_LEN(out, statfs);
126 	})));
127 
128 	ASSERT_EQ(0, statfs("mountpoint", &statbuf)) << strerror(errno);
129 	newflags = (statbuf.f_flags | MNT_UPDATE) ^ flag;
130 
131 	build_iovec(&iov, &iovlen, "fstype", (void*)statbuf.f_fstypename, -1);
132 	build_iovec(&iov, &iovlen, "fspath", (void*)statbuf.f_mntonname, -1);
133 	build_iovec(&iov, &iovlen, "from", __DECONST(void *, "/dev/fuse"), -1);
134 	/*
135 	 * Don't check nmount's return value, because vfs_domount may "fix" the
136 	 * options for us.  The important thing is to check the final value of
137 	 * statbuf.f_flags below.
138 	 */
139 	(void)nmount(iov, iovlen, newflags);
140 
141 	ASSERT_EQ(0, statfs("mountpoint", &statbuf)) << strerror(errno);
142 	EXPECT_TRUE((newflags ^ statbuf.f_flags) & flag);
143 }
144 
145 INSTANTIATE_TEST_CASE_P(Mount, UpdateOk,
146 		::testing::Values("MNT_RDONLY", "MNT_NOEXEC", "MNT_NOSUID", "MNT_NOATIME",
147 		"MNT_SUIDDIR")
148 );
149 
150 INSTANTIATE_TEST_CASE_P(Mount, UpdateErr,
151 		::testing::Values( "MNT_USER");
152 );
153