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
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));
78 * The fuse daemon fails the request with enoent. This usually indicates a
79 * race condition: some other FUSE client removed the file in between when the
80 * kernel checked for it with lookup and tried to open it
82 TEST_F(Opendir, enoent)
84 const char FULLPATH[] = "mountpoint/some_dir";
85 const char RELPATH[] = "some_dir";
89 ASSERT_EQ(0, sem_init(&sem, 0, 0)) << strerror(errno);
91 expect_lookup(RELPATH, ino);
92 expect_opendir(ino, O_RDONLY, ReturnErrno(ENOENT));
93 // Since FUSE_OPENDIR returns ENOENT, the kernel will reclaim the vnode
94 // and send a FUSE_FORGET
95 expect_forget(ino, 1, &sem);
97 ASSERT_EQ(-1, open(FULLPATH, O_DIRECTORY));
98 EXPECT_EQ(ENOENT, errno);
105 * The daemon is responsible for checking file permissions (unless the
106 * default_permissions mount option was used)
108 TEST_F(Opendir, eperm)
110 const char FULLPATH[] = "mountpoint/some_dir";
111 const char RELPATH[] = "some_dir";
114 expect_lookup(RELPATH, ino);
115 expect_opendir(ino, O_RDONLY, ReturnErrno(EPERM));
117 EXPECT_EQ(-1, open(FULLPATH, O_DIRECTORY));
118 EXPECT_EQ(EPERM, errno);
121 TEST_F(Opendir, open)
123 const char FULLPATH[] = "mountpoint/some_dir";
124 const char RELPATH[] = "some_dir";
128 expect_lookup(RELPATH, ino);
129 expect_opendir(ino, O_RDONLY,
130 ReturnImmediate([=](auto in __unused, auto& out) {
131 SET_OUT_HEADER_LEN(out, open);
134 fd = open(FULLPATH, O_DIRECTORY);
135 ASSERT_LE(0, fd) << strerror(errno);
140 /* Directories can be opened O_EXEC for stuff like fchdir(2) */
141 TEST_F(Opendir, open_exec)
143 const char FULLPATH[] = "mountpoint/some_dir";
144 const char RELPATH[] = "some_dir";
148 expect_lookup(RELPATH, ino);
149 expect_opendir(ino, O_EXEC,
150 ReturnImmediate([=](auto in __unused, auto& out) {
151 SET_OUT_HEADER_LEN(out, open);
154 fd = open(FULLPATH, O_EXEC | O_DIRECTORY);
155 ASSERT_LE(0, fd) << strerror(errno);
160 TEST_F(Opendir, opendir)
162 const char FULLPATH[] = "mountpoint/some_dir";
163 const char RELPATH[] = "some_dir";
166 expect_lookup(RELPATH, ino);
167 expect_opendir(ino, O_RDONLY,
168 ReturnImmediate([=](auto in __unused, auto& out) {
169 SET_OUT_HEADER_LEN(out, open);
173 EXPECT_NE(nullptr, opendir(FULLPATH)) << strerror(errno);