xref: /freebsd/tests/sys/fs/fusefs/utils.hh (revision 0d66206fff44f864ea8a4b220c3a53b4caa959a0)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2019 The FreeBSD Foundation
5  *
6  * This software was developed by BFF Storage Systems, LLC under sponsorship
7  * from the FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
17  *
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
28  * SUCH DAMAGE.
29  *
30  * $FreeBSD$
31  */
32 
33 struct _sem;
34 typedef struct _sem sem_t;
35 struct _dirdesc;
36 typedef struct _dirdesc DIR;
37 
38 /* Nanoseconds to sleep, for tests that must */
39 #define NAP_NS	(100'000'000)
40 
41 void get_unprivileged_id(uid_t *uid, gid_t *gid);
42 inline void nap()
43 {
44 	usleep(NAP_NS / 1000);
45 }
46 
47 enum cache_mode {
48 	Uncached,
49 	Writethrough,
50 	Writeback,
51 	WritebackAsync
52 };
53 
54 const char *cache_mode_to_s(enum cache_mode cm);
55 bool is_unsafe_aio_enabled(void);
56 
57 extern const uint32_t libfuse_max_write;
58 class FuseTest : public ::testing::Test {
59 	protected:
60 	uint32_t m_maxreadahead;
61 	uint32_t m_maxwrite;
62 	uint32_t m_init_flags;
63 	bool m_allow_other;
64 	bool m_default_permissions;
65 	uint32_t m_kernel_minor_version;
66 	enum poll_method m_pm;
67 	bool m_noatime;
68 	bool m_push_symlinks_in;
69 	bool m_ro;
70 	bool m_async;
71 	bool m_noclusterr;
72 	bool m_nointr;
73 	unsigned m_time_gran;
74 	MockFS *m_mock = NULL;
75 	const static uint64_t FH = 0xdeadbeef1a7ebabe;
76 	const char *reclaim_mib = "debug.try_reclaim_vnode";
77 	const char *m_fsname;
78 	const char *m_subtype;
79 
80 	public:
81 	int m_maxbcachebuf;
82 	int m_maxphys;
83 
84 	FuseTest():
85 		m_maxreadahead(0),
86 		m_maxwrite(0),
87 		m_init_flags(0),
88 		m_allow_other(false),
89 		m_default_permissions(false),
90 		m_kernel_minor_version(FUSE_KERNEL_MINOR_VERSION),
91 		m_pm(BLOCKING),
92 		m_noatime(false),
93 		m_push_symlinks_in(false),
94 		m_ro(false),
95 		m_async(false),
96 		m_noclusterr(false),
97 		m_nointr(false),
98 		m_time_gran(1),
99 		m_fsname(""),
100 		m_subtype(""),
101 		m_maxbcachebuf(0),
102 		m_maxphys(0)
103 	{}
104 
105 	virtual void SetUp();
106 
107 	virtual void TearDown() {
108 		if (m_mock)
109 			delete m_mock;
110 	}
111 
112 	/*
113 	 * Create an expectation that FUSE_ACCESS will be called once for the
114 	 * given inode with the given access_mode, returning the given errno
115 	 */
116 	void expect_access(uint64_t ino, mode_t access_mode, int error);
117 
118 	/* Expect FUSE_DESTROY and shutdown the daemon */
119 	void expect_destroy(int error);
120 
121 	/*
122 	 * Create an expectation that FUSE_FALLOCATE will be called with the
123 	 * given inode, offset, length, and mode, exactly times times and
124 	 * returning error
125 	 */
126 	void expect_fallocate(uint64_t ino, uint64_t offset, uint64_t length,
127 		uint32_t mode, int error, int times=1);
128 
129 	/*
130 	 * Create an expectation that FUSE_FLUSH will be called times times for
131 	 * the given inode
132 	 */
133 	void expect_flush(uint64_t ino, int times, ProcessMockerT r);
134 
135 	/*
136 	 * Create an expectation that FUSE_FORGET will be called for the given
137 	 * inode.  There will be no response.  If sem is provided, it will be
138 	 * posted after the operation is received by the daemon.
139 	 */
140 	void expect_forget(uint64_t ino, uint64_t nlookup, sem_t *sem = NULL);
141 
142 	/*
143 	 * Create an expectation that FUSE_GETATTR will be called for the given
144 	 * inode any number of times.  It will respond with a few basic
145 	 * attributes, like the given size and the mode S_IFREG | 0644
146 	 */
147 	void expect_getattr(uint64_t ino, uint64_t size);
148 
149 	/*
150 	 * Create an expectation that FUSE_GETXATTR will be called once for the
151 	 * given inode.
152 	 */
153 	void expect_getxattr(uint64_t ino, const char *attr, ProcessMockerT r);
154 
155 	/*
156 	 * Create an expectation that FUSE_LOOKUP will be called for the given
157 	 * path exactly times times and cache validity period.  It will respond
158 	 * with inode ino, mode mode, filesize size.
159 	 */
160 	void expect_lookup(const char *relpath, uint64_t ino, mode_t mode,
161 		uint64_t size, int times, uint64_t attr_valid = UINT64_MAX,
162 		uid_t uid = 0, gid_t gid = 0);
163 
164 	/* The protocol 7.8 version of expect_lookup */
165 	void expect_lookup_7_8(const char *relpath, uint64_t ino, mode_t mode,
166 		uint64_t size, int times, uint64_t attr_valid = UINT64_MAX,
167 		uid_t uid = 0, gid_t gid = 0);
168 
169 	/*
170 	 * Create an expectation that FUSE_OPEN will be called for the given
171 	 * inode exactly times times.  It will return with open_flags flags and
172 	 * file handle FH.
173 	 */
174 	void expect_open(uint64_t ino, uint32_t flags, int times);
175 
176 	/*
177 	 * Create an expectation that FUSE_OPENDIR will be called exactly once
178 	 * for inode ino.
179 	 */
180 	void expect_opendir(uint64_t ino);
181 
182 	/*
183 	 * Create an expectation that FUSE_READ will be called exactly once for
184 	 * the given inode, at offset offset and with size isize.  It will
185 	 * return the first osize bytes from contents
186 	 *
187 	 * Protocol 7.8 tests can use this same expectation method because
188 	 * nothing currently validates the size of the fuse_read_in struct.
189 	 */
190 	void expect_read(uint64_t ino, uint64_t offset, uint64_t isize,
191 		uint64_t osize, const void *contents, int flags = -1,
192 		uint64_t fh = FH);
193 
194 	/*
195 	 * Create an expectation that FUSE_READIR will be called any number of
196 	 * times on the given ino with the given offset, returning (by copy)
197 	 * the provided entries
198 	 */
199 	void expect_readdir(uint64_t ino, uint64_t off,
200 		std::vector<struct dirent> &ents);
201 
202 	/*
203 	 * Create an expectation that FUSE_RELEASE will be called exactly once
204 	 * for the given inode and filehandle, returning success
205 	 */
206 	void expect_release(uint64_t ino, uint64_t fh);
207 
208 	/*
209 	 * Create an expectation that FUSE_RELEASEDIR will be called exactly
210 	 * once for the given inode
211 	 */
212 	void expect_releasedir(uint64_t ino, ProcessMockerT r);
213 
214 	/*
215 	 * Create an expectation that FUSE_UNLINK will be called exactly once
216 	 * for the given path, returning an errno
217 	 */
218 	void expect_unlink(uint64_t parent, const char *path, int error);
219 
220 	/*
221 	 * Create an expectation that FUSE_WRITE will be called exactly once
222 	 * for the given inode, at offset offset, with  size isize and buffer
223 	 * contents.  Any flags present in flags_set must be set, and any
224 	 * present in flags_unset must not be set.  Other flags are don't care.
225 	 * It will return osize.
226 	 */
227 	void expect_write(uint64_t ino, uint64_t offset, uint64_t isize,
228 		uint64_t osize, uint32_t flags_set, uint32_t flags_unset,
229 		const void *contents);
230 
231 	/* Protocol 7.8 version of expect_write */
232 	void expect_write_7_8(uint64_t ino, uint64_t offset, uint64_t isize,
233 		uint64_t osize, const void *contents);
234 
235 	/*
236 	 * Helper that runs code in a child process.
237 	 *
238 	 * First, parent_func runs in the parent process.
239 	 * Then, child_func runs in the child process, dropping privileges if
240 	 * desired.
241 	 * Finally, fusetest_fork returns.
242 	 *
243 	 * # Returns
244 	 *
245 	 * fusetest_fork may SKIP the test, which the caller should detect with
246 	 * the IsSkipped() method.  If not, then the child's exit status will
247 	 * be returned in status.
248 	 */
249 	void fork(bool drop_privs, int *status,
250 		std::function<void()> parent_func,
251 		std::function<int()> child_func);
252 
253 	/*
254 	 * Deliberately leak a file descriptor.
255 	 *
256 	 * Closing a file descriptor on fusefs would cause the server to
257 	 * receive FUSE_CLOSE and possibly FUSE_INACTIVE.  Handling those
258 	 * operations would needlessly complicate most tests.  So most tests
259 	 * deliberately leak the file descriptors instead.  This method serves
260 	 * to document the leakage, and provide a single point of suppression
261 	 * for static analyzers.
262 	 */
263 	/* coverity[+close: arg-0] */
264 	static void leak(int fd __unused) {}
265 
266 	/*
267 	 * Deliberately leak a DIR* pointer
268 	 *
269 	 * See comments for FuseTest::leak
270 	 */
271 	static void leakdir(DIR* dirp __unused) {}
272 
273 	/* Manually reclaim a vnode.  Requires root privileges. */
274 	void reclaim_vnode(const char *fullpath);
275 };
276