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