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
32 #include <sys/param.h>
33 #include <sys/mount.h>
36 #include "mntopts.h" // for build_iovec
42 using namespace testing;
44 class UpdateOk: public FuseTest, public WithParamInterface<const char*> {};
45 class UpdateErr: public FuseTest, public WithParamInterface<const char*> {};
47 int mntflag_from_string(const char *s)
49 if (0 == strcmp("MNT_RDONLY", s))
51 else if (0 == strcmp("MNT_NOEXEC", s))
53 else if (0 == strcmp("MNT_NOSUID", s))
55 else if (0 == strcmp("MNT_NOATIME", s))
57 else if (0 == strcmp("MNT_SUIDDIR", s))
59 else if (0 == strcmp("MNT_USER", s))
65 /* Some mount options can be changed by mount -u */
66 TEST_P(UpdateOk, update)
68 struct statfs statbuf;
69 struct iovec *iov = NULL;
72 int newflags = MNT_UPDATE | MNT_SYNCHRONOUS;
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";
80 EXPECT_CALL(*m_mock, process(
81 ResultOf([](auto in) {
82 return (in.header.opcode == FUSE_STATFS);
85 ).WillRepeatedly(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
87 * All of the fields except f_flags are don't care, and f_flags is set by
90 SET_OUT_HEADER_LEN(out, statfs);
93 ASSERT_EQ(0, statfs("mountpoint", &statbuf)) << strerror(errno);
94 newflags = (statbuf.f_flags | MNT_UPDATE) ^ flag;
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);
101 ASSERT_EQ(0, statfs("mountpoint", &statbuf)) << strerror(errno);
102 EXPECT_FALSE((newflags ^ statbuf.f_flags) & flag);
105 /* Some mount options cannnot be changed by mount -u */
106 TEST_P(UpdateErr, update)
108 struct statfs statbuf;
109 struct iovec *iov = NULL;
112 int newflags = MNT_UPDATE | MNT_SYNCHRONOUS;
114 flag = mntflag_from_string(GetParam());
115 EXPECT_CALL(*m_mock, process(
116 ResultOf([](auto in) {
117 return (in.header.opcode == FUSE_STATFS);
120 ).WillRepeatedly(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
122 * All of the fields except f_flags are don't care, and f_flags is set by
125 SET_OUT_HEADER_LEN(out, statfs);
128 ASSERT_EQ(0, statfs("mountpoint", &statbuf)) << strerror(errno);
129 newflags = (statbuf.f_flags | MNT_UPDATE) ^ flag;
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);
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.
139 (void)nmount(iov, iovlen, newflags);
141 ASSERT_EQ(0, statfs("mountpoint", &statbuf)) << strerror(errno);
142 EXPECT_TRUE((newflags ^ statbuf.f_flags) & flag);
145 INSTANTIATE_TEST_CASE_P(Mount, UpdateOk,
146 ::testing::Values("MNT_RDONLY", "MNT_NOEXEC", "MNT_NOSUID", "MNT_NOATIME",
150 INSTANTIATE_TEST_CASE_P(Mount, UpdateErr,
151 ::testing::Values( "MNT_USER");