2 * SPDX-License-Identifier: BSD-2-Clause
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
37 #include <semaphore.h>
43 using namespace testing;
45 class Opendir: public FuseTest {
47 void expect_lookup(const char *relpath, uint64_t ino)
49 FuseTest::expect_lookup(relpath, ino, S_IFDIR | 0755, 0, 1);
52 void expect_opendir(uint64_t ino, uint32_t flags, ProcessMockerT r)
54 /* opendir(3) calls fstatfs */
55 EXPECT_CALL(*m_mock, process(
56 ResultOf([](auto in) {
57 return (in.header.opcode == FUSE_STATFS);
60 ).WillRepeatedly(Invoke(ReturnImmediate([=](auto i __unused, auto& out) {
61 SET_OUT_HEADER_LEN(out, statfs);
64 EXPECT_CALL(*m_mock, process(
65 ResultOf([=](auto in) {
66 return (in.header.opcode == FUSE_OPENDIR &&
67 in.header.nodeid == ino &&
68 in.body.opendir.flags == flags);
71 ).WillOnce(Invoke(r));
76 class OpendirNoOpendirSupport: public Opendir {
77 virtual void SetUp() {
78 m_init_flags = FUSE_NO_OPENDIR_SUPPORT;
85 * The fuse daemon fails the request with enoent. This usually indicates a
86 * race condition: some other FUSE client removed the file in between when the
87 * kernel checked for it with lookup and tried to open it
89 TEST_F(Opendir, enoent)
91 const char FULLPATH[] = "mountpoint/some_dir";
92 const char RELPATH[] = "some_dir";
96 ASSERT_EQ(0, sem_init(&sem, 0, 0)) << strerror(errno);
98 expect_lookup(RELPATH, ino);
99 expect_opendir(ino, O_RDONLY, ReturnErrno(ENOENT));
100 // Since FUSE_OPENDIR returns ENOENT, the kernel will reclaim the vnode
101 // and send a FUSE_FORGET
102 expect_forget(ino, 1, &sem);
104 ASSERT_EQ(-1, open(FULLPATH, O_DIRECTORY));
105 EXPECT_EQ(ENOENT, errno);
112 * The daemon is responsible for checking file permissions (unless the
113 * default_permissions mount option was used)
115 TEST_F(Opendir, eperm)
117 const char FULLPATH[] = "mountpoint/some_dir";
118 const char RELPATH[] = "some_dir";
121 expect_lookup(RELPATH, ino);
122 expect_opendir(ino, O_RDONLY, ReturnErrno(EPERM));
124 EXPECT_EQ(-1, open(FULLPATH, O_DIRECTORY));
125 EXPECT_EQ(EPERM, errno);
128 TEST_F(Opendir, open)
130 const char FULLPATH[] = "mountpoint/some_dir";
131 const char RELPATH[] = "some_dir";
135 expect_lookup(RELPATH, ino);
136 expect_opendir(ino, O_RDONLY,
137 ReturnImmediate([=](auto in __unused, auto& out) {
138 SET_OUT_HEADER_LEN(out, open);
141 fd = open(FULLPATH, O_DIRECTORY);
142 ASSERT_LE(0, fd) << strerror(errno);
147 /* Directories can be opened O_EXEC for stuff like fchdir(2) */
148 TEST_F(Opendir, open_exec)
150 const char FULLPATH[] = "mountpoint/some_dir";
151 const char RELPATH[] = "some_dir";
155 expect_lookup(RELPATH, ino);
156 expect_opendir(ino, O_EXEC,
157 ReturnImmediate([=](auto in __unused, auto& out) {
158 SET_OUT_HEADER_LEN(out, open);
161 fd = open(FULLPATH, O_EXEC | O_DIRECTORY);
162 ASSERT_LE(0, fd) << strerror(errno);
167 TEST_F(Opendir, opendir)
169 const char FULLPATH[] = "mountpoint/some_dir";
170 const char RELPATH[] = "some_dir";
173 expect_lookup(RELPATH, ino);
174 expect_opendir(ino, O_RDONLY,
175 ReturnImmediate([=](auto in __unused, auto& out) {
176 SET_OUT_HEADER_LEN(out, open);
180 EXPECT_NE(nullptr, opendir(FULLPATH)) << strerror(errno);
184 * Without FUSE_NO_OPENDIR_SUPPORT, returning ENOSYS is an error
186 TEST_F(Opendir, enosys)
188 const char FULLPATH[] = "mountpoint/some_file.txt";
189 const char RELPATH[] = "some_file.txt";
192 expect_lookup(RELPATH, ino);
193 expect_opendir(ino, O_RDONLY, ReturnErrno(ENOSYS));
195 EXPECT_EQ(-1, open(FULLPATH, O_DIRECTORY));
196 EXPECT_EQ(ENOSYS, errno);
200 * If a fuse server sets FUSE_NO_OPENDIR_SUPPORT and returns ENOSYS to a
201 * FUSE_OPENDIR, then it and subsequent FUSE_OPENDIR and FUSE_RELEASEDIR
202 * operations will also succeed automatically without being sent to the server.
204 TEST_F(OpendirNoOpendirSupport, enosys)
206 const char FULLPATH[] = "mountpoint/some_file.txt";
207 const char RELPATH[] = "some_file.txt";
211 FuseTest::expect_lookup(RELPATH, ino, S_IFDIR | 0755, 0, 2);
212 expect_opendir(ino, O_RDONLY, ReturnErrno(ENOSYS));
214 fd = open(FULLPATH, O_DIRECTORY);
215 ASSERT_LE(0, fd) << strerror(errno);
218 fd = open(FULLPATH, O_DIRECTORY);
219 ASSERT_LE(0, fd) << strerror(errno);