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