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
41 using namespace testing;
43 class Opendir: public FuseTest {
45 void expect_lookup(const char *relpath, uint64_t ino)
47 FuseTest::expect_lookup(relpath, ino, S_IFDIR | 0755, 0, 1);
50 void expect_opendir(uint64_t ino, uint32_t flags, ProcessMockerT r)
52 /* opendir(3) calls fstatfs */
53 EXPECT_CALL(*m_mock, process(
54 ResultOf([](auto in) {
55 return (in.header.opcode == FUSE_STATFS);
58 ).WillRepeatedly(Invoke(ReturnImmediate([=](auto i __unused, auto& out) {
59 SET_OUT_HEADER_LEN(out, statfs);
62 EXPECT_CALL(*m_mock, process(
63 ResultOf([=](auto in) {
64 return (in.header.opcode == FUSE_OPENDIR &&
65 in.header.nodeid == ino &&
66 in.body.opendir.flags == flags);
69 ).WillOnce(Invoke(r));
76 * The fuse daemon fails the request with enoent. This usually indicates a
77 * race condition: some other FUSE client removed the file in between when the
78 * kernel checked for it with lookup and tried to open it
80 TEST_F(Opendir, enoent)
82 const char FULLPATH[] = "mountpoint/some_dir";
83 const char RELPATH[] = "some_dir";
86 expect_lookup(RELPATH, ino);
87 expect_opendir(ino, O_RDONLY, ReturnErrno(ENOENT));
89 EXPECT_NE(0, open(FULLPATH, O_DIRECTORY));
90 EXPECT_EQ(ENOENT, errno);
94 * The daemon is responsible for checking file permissions (unless the
95 * default_permissions mount option was used)
97 TEST_F(Opendir, eperm)
99 const char FULLPATH[] = "mountpoint/some_dir";
100 const char RELPATH[] = "some_dir";
103 expect_lookup(RELPATH, ino);
104 expect_opendir(ino, O_RDONLY, ReturnErrno(EPERM));
106 EXPECT_NE(0, open(FULLPATH, O_DIRECTORY));
107 EXPECT_EQ(EPERM, errno);
110 TEST_F(Opendir, open)
112 const char FULLPATH[] = "mountpoint/some_dir";
113 const char RELPATH[] = "some_dir";
116 expect_lookup(RELPATH, ino);
117 expect_opendir(ino, O_RDONLY,
118 ReturnImmediate([=](auto in __unused, auto& out) {
119 SET_OUT_HEADER_LEN(out, open);
122 EXPECT_LE(0, open(FULLPATH, O_DIRECTORY)) << strerror(errno);
125 /* Directories can be opened O_EXEC for stuff like fchdir(2) */
126 TEST_F(Opendir, open_exec)
128 const char FULLPATH[] = "mountpoint/some_dir";
129 const char RELPATH[] = "some_dir";
133 expect_lookup(RELPATH, ino);
134 expect_opendir(ino, O_EXEC,
135 ReturnImmediate([=](auto in __unused, auto& out) {
136 SET_OUT_HEADER_LEN(out, open);
139 fd = open(FULLPATH, O_EXEC | O_DIRECTORY);
140 ASSERT_LE(0, fd) << strerror(errno);
143 TEST_F(Opendir, opendir)
145 const char FULLPATH[] = "mountpoint/some_dir";
146 const char RELPATH[] = "some_dir";
149 expect_lookup(RELPATH, ino);
150 expect_opendir(ino, O_RDONLY,
151 ReturnImmediate([=](auto in __unused, auto& out) {
152 SET_OUT_HEADER_LEN(out, open);
156 EXPECT_NE(nullptr, opendir(FULLPATH)) << strerror(errno);