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 typedef struct _sem sem_t;
36 typedef struct _dirdesc DIR;
38 /* Nanoseconds to sleep, for tests that must */
39 #define NAP_NS (100'000'000)
41 void get_unprivileged_id(uid_t *uid, gid_t *gid);
44 usleep(NAP_NS / 1000);
47 extern const uint32_t libfuse_max_write;
48 extern const uint32_t default_max_write;
49 class FuseTest : public ::testing::Test {
51 uint32_t m_maxreadahead;
53 uint32_t m_init_flags;
55 bool m_default_permissions;
56 uint32_t m_kernel_minor_version;
57 enum poll_method m_pm;
58 bool m_push_symlinks_in;
64 MockFS *m_mock = NULL;
65 const static uint64_t FH = 0xdeadbeef1a7ebabe;
73 m_maxwrite(default_max_write),
76 m_default_permissions(false),
77 m_kernel_minor_version(FUSE_KERNEL_MINOR_VERSION),
79 m_push_symlinks_in(false),
89 virtual void TearDown() {
95 * Create an expectation that FUSE_ACCESS will be called once for the
96 * given inode with the given access_mode, returning the given errno
98 void expect_access(uint64_t ino, mode_t access_mode, int error);
100 /* Expect FUSE_DESTROY and shutdown the daemon */
101 void expect_destroy(int error);
104 * Create an expectation that FUSE_FLUSH will be called times times for
107 void expect_flush(uint64_t ino, int times, ProcessMockerT r);
110 * Create an expectation that FUSE_FORGET will be called for the given
111 * inode. There will be no response. If sem is provided, it will be
112 * posted after the operation is received by the daemon.
114 void expect_forget(uint64_t ino, uint64_t nlookup, sem_t *sem = NULL);
117 * Create an expectation that FUSE_GETATTR will be called for the given
118 * inode any number of times. It will respond with a few basic
119 * attributes, like the given size and the mode S_IFREG | 0644
121 void expect_getattr(uint64_t ino, uint64_t size);
124 * Create an expectation that FUSE_LOOKUP will be called for the given
125 * path exactly times times and cache validity period. It will respond
126 * with inode ino, mode mode, filesize size.
128 void expect_lookup(const char *relpath, uint64_t ino, mode_t mode,
129 uint64_t size, int times, uint64_t attr_valid = UINT64_MAX,
130 uid_t uid = 0, gid_t gid = 0);
132 /* The protocol 7.8 version of expect_lookup */
133 void expect_lookup_7_8(const char *relpath, uint64_t ino, mode_t mode,
134 uint64_t size, int times, uint64_t attr_valid = UINT64_MAX,
135 uid_t uid = 0, gid_t gid = 0);
138 * Create an expectation that FUSE_OPEN will be called for the given
139 * inode exactly times times. It will return with open_flags flags and
142 void expect_open(uint64_t ino, uint32_t flags, int times);
145 * Create an expectation that FUSE_OPENDIR will be called exactly once
148 void expect_opendir(uint64_t ino);
151 * Create an expectation that FUSE_READ will be called exactly once for
152 * the given inode, at offset offset and with size isize. It will
153 * return the first osize bytes from contents
155 * Protocol 7.8 tests can use this same expectation method because
156 * nothing currently validates the size of the fuse_read_in struct.
158 void expect_read(uint64_t ino, uint64_t offset, uint64_t isize,
159 uint64_t osize, const void *contents, int flags = -1);
162 * Create an expectation that FUSE_READIR will be called any number of
163 * times on the given ino with the given offset, returning (by copy)
164 * the provided entries
166 void expect_readdir(uint64_t ino, uint64_t off,
167 std::vector<struct dirent> &ents);
170 * Create an expectation that FUSE_RELEASE will be called exactly once
171 * for the given inode and filehandle, returning success
173 void expect_release(uint64_t ino, uint64_t fh);
176 * Create an expectation that FUSE_RELEASEDIR will be called exactly
177 * once for the given inode
179 void expect_releasedir(uint64_t ino, ProcessMockerT r);
182 * Create an expectation that FUSE_UNLINK will be called exactly once
183 * for the given path, returning an errno
185 void expect_unlink(uint64_t parent, const char *path, int error);
188 * Create an expectation that FUSE_WRITE will be called exactly once
189 * for the given inode, at offset offset, with size isize and buffer
190 * contents. Any flags present in flags_set must be set, and any
191 * present in flags_unset must not be set. Other flags are don't care.
192 * It will return osize.
194 void expect_write(uint64_t ino, uint64_t offset, uint64_t isize,
195 uint64_t osize, uint32_t flags_set, uint32_t flags_unset,
196 const void *contents);
198 /* Protocol 7.8 version of expect_write */
199 void expect_write_7_8(uint64_t ino, uint64_t offset, uint64_t isize,
200 uint64_t osize, const void *contents);
203 * Helper that runs code in a child process.
205 * First, parent_func runs in the parent process.
206 * Then, child_func runs in the child process, dropping privileges if
208 * Finally, fusetest_fork returns.
212 * fusetest_fork may SKIP the test, which the caller should detect with
213 * the IsSkipped() method. If not, then the child's exit status will
214 * be returned in status.
216 void fork(bool drop_privs, int *status,
217 std::function<void()> parent_func,
218 std::function<int()> child_func);
221 * Deliberately leak a file descriptor.
223 * Closing a file descriptor on fusefs would cause the server to
224 * receive FUSE_CLOSE and possibly FUSE_INACTIVE. Handling those
225 * operations would needlessly complicate most tests. So most tests
226 * deliberately leak the file descriptors instead. This method serves
227 * to document the leakage, and provide a single point of suppression
228 * for static analyzers.
230 static void leak(int fd __unused) {}
233 * Deliberately leak a DIR* pointer
235 * See comments for FuseTest::leak
237 static void leakdir(DIR* dirp __unused) {}