2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2019 The FreeBSD Foundation
6 * This software was developed by BFF Storage Systems, LLC under sponsorship
7 * from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
34 #include <sys/param.h>
35 #include <sys/mount.h>
38 #include "mntopts.h" // for build_iovec
44 using namespace testing;
46 class Mount: public FuseTest {
48 void expect_statfs() {
49 EXPECT_CALL(*m_mock, process(
50 ResultOf([](auto in) {
51 return (in.header.opcode == FUSE_STATFS);
54 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
55 SET_OUT_HEADER_LEN(out, statfs);
60 class Fsname: public Mount {
62 m_fsname = "http://something";
67 class Subtype: public Mount {
74 class UpdateOk: public Mount, public WithParamInterface<const char*> {};
75 class UpdateErr: public Mount, public WithParamInterface<const char*> {};
77 int mntflag_from_string(const char *s)
79 if (0 == strcmp("MNT_RDONLY", s))
81 else if (0 == strcmp("MNT_NOEXEC", s))
83 else if (0 == strcmp("MNT_NOSUID", s))
85 else if (0 == strcmp("MNT_NOATIME", s))
87 else if (0 == strcmp("MNT_SUIDDIR", s))
89 else if (0 == strcmp("MNT_USER", s))
95 TEST_F(Fsname, fsname)
97 struct statfs statbuf;
101 ASSERT_EQ(0, statfs("mountpoint", &statbuf)) << strerror(errno);
102 ASSERT_STREQ("http://something", statbuf.f_mntfromname);
105 TEST_F(Subtype, subtype)
107 struct statfs statbuf;
111 ASSERT_EQ(0, statfs("mountpoint", &statbuf)) << strerror(errno);
112 ASSERT_STREQ("fusefs.myfs", statbuf.f_fstypename);
115 /* Some mount options can be changed by mount -u */
116 TEST_P(UpdateOk, update)
118 struct statfs statbuf;
119 struct iovec *iov = NULL;
122 int newflags = MNT_UPDATE | MNT_SYNCHRONOUS;
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";
130 EXPECT_CALL(*m_mock, process(
131 ResultOf([](auto in) {
132 return (in.header.opcode == FUSE_STATFS);
135 ).WillRepeatedly(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
137 * All of the fields except f_flags are don't care, and f_flags is set by
140 SET_OUT_HEADER_LEN(out, statfs);
143 ASSERT_EQ(0, statfs("mountpoint", &statbuf)) << strerror(errno);
144 newflags = (statbuf.f_flags | MNT_UPDATE) ^ flag;
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);
151 ASSERT_EQ(0, statfs("mountpoint", &statbuf)) << strerror(errno);
152 EXPECT_FALSE((newflags ^ statbuf.f_flags) & flag);
155 /* Some mount options cannnot be changed by mount -u */
156 TEST_P(UpdateErr, update)
158 struct statfs statbuf;
159 struct iovec *iov = NULL;
162 int newflags = MNT_UPDATE | MNT_SYNCHRONOUS;
164 flag = mntflag_from_string(GetParam());
165 EXPECT_CALL(*m_mock, process(
166 ResultOf([](auto in) {
167 return (in.header.opcode == FUSE_STATFS);
170 ).WillRepeatedly(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
172 * All of the fields except f_flags are don't care, and f_flags is set by
175 SET_OUT_HEADER_LEN(out, statfs);
178 ASSERT_EQ(0, statfs("mountpoint", &statbuf)) << strerror(errno);
179 newflags = (statbuf.f_flags | MNT_UPDATE) ^ flag;
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);
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.
189 (void)nmount(iov, iovlen, newflags);
191 ASSERT_EQ(0, statfs("mountpoint", &statbuf)) << strerror(errno);
192 EXPECT_TRUE((newflags ^ statbuf.f_flags) & flag);
195 INSTANTIATE_TEST_CASE_P(Mount, UpdateOk,
196 ::testing::Values("MNT_RDONLY", "MNT_NOEXEC", "MNT_NOSUID", "MNT_NOATIME",
200 INSTANTIATE_TEST_CASE_P(Mount, UpdateErr,
201 ::testing::Values( "MNT_USER");