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 UpdateOk: public FuseTest, public WithParamInterface<const char*> {};
47 class UpdateErr: public FuseTest, public WithParamInterface<const char*> {};
49 int mntflag_from_string(const char *s)
51 if (0 == strcmp("MNT_RDONLY", s))
53 else if (0 == strcmp("MNT_NOEXEC", s))
55 else if (0 == strcmp("MNT_NOSUID", s))
57 else if (0 == strcmp("MNT_NOATIME", s))
59 else if (0 == strcmp("MNT_SUIDDIR", s))
61 else if (0 == strcmp("MNT_USER", s))
67 /* Some mount options can be changed by mount -u */
68 TEST_P(UpdateOk, update)
70 struct statfs statbuf;
71 struct iovec *iov = NULL;
74 int newflags = MNT_UPDATE | MNT_SYNCHRONOUS;
76 flag = mntflag_from_string(GetParam());
77 if (flag == MNT_NOSUID && 0 != geteuid())
78 GTEST_SKIP() << "Only root may clear MNT_NOSUID";
79 if (flag == MNT_SUIDDIR && 0 != geteuid())
80 GTEST_SKIP() << "Only root may set MNT_SUIDDIR";
82 EXPECT_CALL(*m_mock, process(
83 ResultOf([](auto in) {
84 return (in.header.opcode == FUSE_STATFS);
87 ).WillRepeatedly(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
89 * All of the fields except f_flags are don't care, and f_flags is set by
92 SET_OUT_HEADER_LEN(out, statfs);
95 ASSERT_EQ(0, statfs("mountpoint", &statbuf)) << strerror(errno);
96 newflags = (statbuf.f_flags | MNT_UPDATE) ^ flag;
98 build_iovec(&iov, &iovlen, "fstype", (void*)statbuf.f_fstypename, -1);
99 build_iovec(&iov, &iovlen, "fspath", (void*)statbuf.f_mntonname, -1);
100 build_iovec(&iov, &iovlen, "from", __DECONST(void *, "/dev/fuse"), -1);
101 ASSERT_EQ(0, nmount(iov, iovlen, newflags)) << strerror(errno);
103 ASSERT_EQ(0, statfs("mountpoint", &statbuf)) << strerror(errno);
104 EXPECT_FALSE((newflags ^ statbuf.f_flags) & flag);
107 /* Some mount options cannnot be changed by mount -u */
108 TEST_P(UpdateErr, update)
110 struct statfs statbuf;
111 struct iovec *iov = NULL;
114 int newflags = MNT_UPDATE | MNT_SYNCHRONOUS;
116 flag = mntflag_from_string(GetParam());
117 EXPECT_CALL(*m_mock, process(
118 ResultOf([](auto in) {
119 return (in.header.opcode == FUSE_STATFS);
122 ).WillRepeatedly(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
124 * All of the fields except f_flags are don't care, and f_flags is set by
127 SET_OUT_HEADER_LEN(out, statfs);
130 ASSERT_EQ(0, statfs("mountpoint", &statbuf)) << strerror(errno);
131 newflags = (statbuf.f_flags | MNT_UPDATE) ^ flag;
133 build_iovec(&iov, &iovlen, "fstype", (void*)statbuf.f_fstypename, -1);
134 build_iovec(&iov, &iovlen, "fspath", (void*)statbuf.f_mntonname, -1);
135 build_iovec(&iov, &iovlen, "from", __DECONST(void *, "/dev/fuse"), -1);
137 * Don't check nmount's return value, because vfs_domount may "fix" the
138 * options for us. The important thing is to check the final value of
139 * statbuf.f_flags below.
141 (void)nmount(iov, iovlen, newflags);
143 ASSERT_EQ(0, statfs("mountpoint", &statbuf)) << strerror(errno);
144 EXPECT_TRUE((newflags ^ statbuf.f_flags) & flag);
147 INSTANTIATE_TEST_CASE_P(Mount, UpdateOk,
148 ::testing::Values("MNT_RDONLY", "MNT_NOEXEC", "MNT_NOSUID", "MNT_NOATIME",
152 INSTANTIATE_TEST_CASE_P(Mount, UpdateErr,
153 ::testing::Values( "MNT_USER");