xref: /freebsd/tests/sys/fs/fusefs/utils.hh (revision 9f31c47460412ab6ccae36a70ca019b47423ccec)
19821f1d3SAlan Somers /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
39821f1d3SAlan Somers  *
49821f1d3SAlan Somers  * Copyright (c) 2019 The FreeBSD Foundation
59821f1d3SAlan Somers  *
69821f1d3SAlan Somers  * This software was developed by BFF Storage Systems, LLC under sponsorship
79821f1d3SAlan Somers  * from the FreeBSD Foundation.
89821f1d3SAlan Somers  *
99821f1d3SAlan Somers  * Redistribution and use in source and binary forms, with or without
109821f1d3SAlan Somers  * modification, are permitted provided that the following conditions
119821f1d3SAlan Somers  * are met:
129821f1d3SAlan Somers  * 1. Redistributions of source code must retain the above copyright
139821f1d3SAlan Somers  *    notice, this list of conditions and the following disclaimer.
149821f1d3SAlan Somers  * 2. Redistributions in binary form must reproduce the above copyright
159821f1d3SAlan Somers  *    notice, this list of conditions and the following disclaimer in the
169821f1d3SAlan Somers  *    documentation and/or other materials provided with the distribution.
179821f1d3SAlan Somers  *
189821f1d3SAlan Somers  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
199821f1d3SAlan Somers  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
209821f1d3SAlan Somers  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
219821f1d3SAlan Somers  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
229821f1d3SAlan Somers  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
239821f1d3SAlan Somers  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
249821f1d3SAlan Somers  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
259821f1d3SAlan Somers  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
269821f1d3SAlan Somers  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
279821f1d3SAlan Somers  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
289821f1d3SAlan Somers  * SUCH DAMAGE.
299821f1d3SAlan Somers  */
309821f1d3SAlan Somers 
312d6bf515SAlan Somers struct _sem;
322d6bf515SAlan Somers typedef struct _sem sem_t;
337fc0921dSAlan Somers struct _dirdesc;
347fc0921dSAlan Somers typedef struct _dirdesc DIR;
352d6bf515SAlan Somers 
36a87257acSAlan Somers /* Nanoseconds to sleep, for tests that must */
37a87257acSAlan Somers #define NAP_NS	(100'000'000)
38a87257acSAlan Somers 
3961b0a927SAlan Somers void get_unprivileged_id(uid_t *uid, gid_t *gid);
nap()40a87257acSAlan Somers inline void nap()
41a87257acSAlan Somers {
42a87257acSAlan Somers 	usleep(NAP_NS / 1000);
43a87257acSAlan Somers }
4461b0a927SAlan Somers 
45b0ecfb42SAlan Somers enum cache_mode {
46b0ecfb42SAlan Somers 	Uncached,
47b0ecfb42SAlan Somers 	Writethrough,
48b0ecfb42SAlan Somers 	Writeback,
49b0ecfb42SAlan Somers 	WritebackAsync
50b0ecfb42SAlan Somers };
51b0ecfb42SAlan Somers 
52b0ecfb42SAlan Somers const char *cache_mode_to_s(enum cache_mode cm);
53c2265ae7SAlan Somers bool is_unsafe_aio_enabled(void);
54c2265ae7SAlan Somers 
558eecd9ceSAlan Somers extern const uint32_t libfuse_max_write;
569821f1d3SAlan Somers class FuseTest : public ::testing::Test {
579821f1d3SAlan Somers 	protected:
58*9f31c474SAlan Somers 	uint32_t m_maxread;
599821f1d3SAlan Somers 	uint32_t m_maxreadahead;
608eecd9ceSAlan Somers 	uint32_t m_maxwrite;
619821f1d3SAlan Somers 	uint32_t m_init_flags;
6291ff3a0dSAlan Somers 	bool m_allow_other;
639821f1d3SAlan Somers 	bool m_default_permissions;
6416bd2d47SAlan Somers 	uint32_t m_kernel_minor_version;
653429092cSAlan Somers 	enum poll_method m_pm;
6691972cfcSAlan Somers 	bool m_noatime;
679821f1d3SAlan Somers 	bool m_push_symlinks_in;
68140bb492SAlan Somers 	bool m_ro;
698eecd9ceSAlan Somers 	bool m_async;
70402b609cSAlan Somers 	bool m_noclusterr;
71ed74f781SAlan Somers 	bool m_nointr;
72fef46454SAlan Somers 	unsigned m_time_gran;
739821f1d3SAlan Somers 	MockFS *m_mock = NULL;
749821f1d3SAlan Somers 	const static uint64_t FH = 0xdeadbeef1a7ebabe;
758d99a6b9SAlan Somers 	const char *reclaim_mib = "debug.try_reclaim_vnode";
762f636248SAlan Somers 	const char *m_fsname;
77616eaa66SAlan Somers 	const char *m_subtype;
789821f1d3SAlan Somers 
799821f1d3SAlan Somers 	public:
809821f1d3SAlan Somers 	int m_maxbcachebuf;
81e9b411d2SGleb Smirnoff 	unsigned long m_maxphys;
829821f1d3SAlan Somers 
FuseTest()839821f1d3SAlan Somers 	FuseTest():
84*9f31c474SAlan Somers 		m_maxread(0),
85d569012fSAlan Somers 		m_maxreadahead(0),
86f928dbcbSAlan Somers 		m_maxwrite(0),
879821f1d3SAlan Somers 		m_init_flags(0),
8891ff3a0dSAlan Somers 		m_allow_other(false),
899821f1d3SAlan Somers 		m_default_permissions(false),
9016bd2d47SAlan Somers 		m_kernel_minor_version(FUSE_KERNEL_MINOR_VERSION),
913429092cSAlan Somers 		m_pm(BLOCKING),
9291972cfcSAlan Somers 		m_noatime(false),
93140bb492SAlan Somers 		m_push_symlinks_in(false),
948eecd9ceSAlan Somers 		m_ro(false),
95402b609cSAlan Somers 		m_async(false),
96fef46454SAlan Somers 		m_noclusterr(false),
97ed74f781SAlan Somers 		m_nointr(false),
988e765737SAlan Somers 		m_time_gran(1),
992f636248SAlan Somers 		m_fsname(""),
100616eaa66SAlan Somers 		m_subtype(""),
1018e765737SAlan Somers 		m_maxbcachebuf(0),
1028e765737SAlan Somers 		m_maxphys(0)
1039821f1d3SAlan Somers 	{}
1049821f1d3SAlan Somers 
1059821f1d3SAlan Somers 	virtual void SetUp();
1069821f1d3SAlan Somers 
TearDown()1079821f1d3SAlan Somers 	virtual void TearDown() {
1089821f1d3SAlan Somers 		if (m_mock)
1099821f1d3SAlan Somers 			delete m_mock;
1109821f1d3SAlan Somers 	}
1119821f1d3SAlan Somers 
1129821f1d3SAlan Somers 	/*
1139f10f423SAlan Somers 	 * Create an expectation that FUSE_ACCESS will be called once for the
11491ff3a0dSAlan Somers 	 * given inode with the given access_mode, returning the given errno
11591ff3a0dSAlan Somers 	 */
11691ff3a0dSAlan Somers 	void expect_access(uint64_t ino, mode_t access_mode, int error);
11791ff3a0dSAlan Somers 
1183429092cSAlan Somers 	/* Expect FUSE_DESTROY and shutdown the daemon */
1193429092cSAlan Somers 	void expect_destroy(int error);
1203429092cSAlan Somers 
12191ff3a0dSAlan Somers 	/*
122398c88c7SAlan Somers 	 * Create an expectation that FUSE_FALLOCATE will be called with the
123398c88c7SAlan Somers 	 * given inode, offset, length, and mode, exactly times times and
124398c88c7SAlan Somers 	 * returning error
125398c88c7SAlan Somers 	 */
126398c88c7SAlan Somers 	void expect_fallocate(uint64_t ino, uint64_t offset, uint64_t length,
127398c88c7SAlan Somers 		uint32_t mode, int error, int times=1);
128398c88c7SAlan Somers 
129398c88c7SAlan Somers 	/*
1309f10f423SAlan Somers 	 * Create an expectation that FUSE_FLUSH will be called times times for
1319f10f423SAlan Somers 	 * the given inode
1329f10f423SAlan Somers 	 */
1339f10f423SAlan Somers 	void expect_flush(uint64_t ino, int times, ProcessMockerT r);
1349f10f423SAlan Somers 
1359f10f423SAlan Somers 	/*
136ff4fbdf5SAlan Somers 	 * Create an expectation that FUSE_FORGET will be called for the given
1372d6bf515SAlan Somers 	 * inode.  There will be no response.  If sem is provided, it will be
1382d6bf515SAlan Somers 	 * posted after the operation is received by the daemon.
139ff4fbdf5SAlan Somers 	 */
1402d6bf515SAlan Somers 	void expect_forget(uint64_t ino, uint64_t nlookup, sem_t *sem = NULL);
141ff4fbdf5SAlan Somers 
142ff4fbdf5SAlan Somers 	/*
1439821f1d3SAlan Somers 	 * Create an expectation that FUSE_GETATTR will be called for the given
1449821f1d3SAlan Somers 	 * inode any number of times.  It will respond with a few basic
1459821f1d3SAlan Somers 	 * attributes, like the given size and the mode S_IFREG | 0644
1469821f1d3SAlan Somers 	 */
1479821f1d3SAlan Somers 	void expect_getattr(uint64_t ino, uint64_t size);
1489821f1d3SAlan Somers 
1499821f1d3SAlan Somers 	/*
150bfcb817bSAlan Somers 	 * Create an expectation that FUSE_GETXATTR will be called once for the
151bfcb817bSAlan Somers 	 * given inode.
152bfcb817bSAlan Somers 	 */
153bfcb817bSAlan Somers 	void expect_getxattr(uint64_t ino, const char *attr, ProcessMockerT r);
154bfcb817bSAlan Somers 
155bfcb817bSAlan Somers 	/*
1569821f1d3SAlan Somers 	 * Create an expectation that FUSE_LOOKUP will be called for the given
157ff4fbdf5SAlan Somers 	 * path exactly times times and cache validity period.  It will respond
158ff4fbdf5SAlan Somers 	 * with inode ino, mode mode, filesize size.
1599821f1d3SAlan Somers 	 */
1609821f1d3SAlan Somers 	void expect_lookup(const char *relpath, uint64_t ino, mode_t mode,
161ff4fbdf5SAlan Somers 		uint64_t size, int times, uint64_t attr_valid = UINT64_MAX,
162474ba6faSAlan Somers 		uid_t uid = 0, gid_t gid = 0);
1639821f1d3SAlan Somers 
16416bd2d47SAlan Somers 	/* The protocol 7.8 version of expect_lookup */
16516bd2d47SAlan Somers 	void expect_lookup_7_8(const char *relpath, uint64_t ino, mode_t mode,
16616bd2d47SAlan Somers 		uint64_t size, int times, uint64_t attr_valid = UINT64_MAX,
16716bd2d47SAlan Somers 		uid_t uid = 0, gid_t gid = 0);
16816bd2d47SAlan Somers 
1699821f1d3SAlan Somers 	/*
170a1542146SAlan Somers 	 * Create an expectation that FUSE_OPEN will be called for the given
1719821f1d3SAlan Somers 	 * inode exactly times times.  It will return with open_flags flags and
1729821f1d3SAlan Somers 	 * file handle FH.
1739821f1d3SAlan Somers 	 */
1749821f1d3SAlan Somers 	void expect_open(uint64_t ino, uint32_t flags, int times);
1759821f1d3SAlan Somers 
1769821f1d3SAlan Somers 	/*
1779821f1d3SAlan Somers 	 * Create an expectation that FUSE_OPENDIR will be called exactly once
1789821f1d3SAlan Somers 	 * for inode ino.
1799821f1d3SAlan Somers 	 */
1809821f1d3SAlan Somers 	void expect_opendir(uint64_t ino);
1819821f1d3SAlan Somers 
1829821f1d3SAlan Somers 	/*
1839821f1d3SAlan Somers 	 * Create an expectation that FUSE_READ will be called exactly once for
1849821f1d3SAlan Somers 	 * the given inode, at offset offset and with size isize.  It will
1859821f1d3SAlan Somers 	 * return the first osize bytes from contents
18616bd2d47SAlan Somers 	 *
18716bd2d47SAlan Somers 	 * Protocol 7.8 tests can use this same expectation method because
18816bd2d47SAlan Somers 	 * nothing currently validates the size of the fuse_read_in struct.
1899821f1d3SAlan Somers 	 */
1909821f1d3SAlan Somers 	void expect_read(uint64_t ino, uint64_t offset, uint64_t isize,
19141ae9f9eSAlan Somers 		uint64_t osize, const void *contents, int flags = -1,
19241ae9f9eSAlan Somers 		uint64_t fh = FH);
1939821f1d3SAlan Somers 
1949821f1d3SAlan Somers 	/*
195e5b50fe7SAlan Somers 	 * Create an expectation that FUSE_READIR will be called any number of
196e5b50fe7SAlan Somers 	 * times on the given ino with the given offset, returning (by copy)
197e5b50fe7SAlan Somers 	 * the provided entries
198e5b50fe7SAlan Somers 	 */
199e5b50fe7SAlan Somers 	void expect_readdir(uint64_t ino, uint64_t off,
200e5b50fe7SAlan Somers 		std::vector<struct dirent> &ents);
201e5b50fe7SAlan Somers 
202e5b50fe7SAlan Somers 	/*
203e0bec057SAlan Somers 	 * Create an expectation that FUSE_RELEASE will be called exactly once
20442d50d16SAlan Somers 	 * for the given inode and filehandle, returning success
2059821f1d3SAlan Somers 	 */
20642d50d16SAlan Somers 	void expect_release(uint64_t ino, uint64_t fh);
2079821f1d3SAlan Somers 
2089821f1d3SAlan Somers 	/*
20935cf0e7eSAlan Somers 	 * Create an expectation that FUSE_RELEASEDIR will be called exactly
21035cf0e7eSAlan Somers 	 * once for the given inode
21135cf0e7eSAlan Somers 	 */
21235cf0e7eSAlan Somers 	void expect_releasedir(uint64_t ino, ProcessMockerT r);
21335cf0e7eSAlan Somers 
21435cf0e7eSAlan Somers 	/*
215ff4fbdf5SAlan Somers 	 * Create an expectation that FUSE_UNLINK will be called exactly once
216ff4fbdf5SAlan Somers 	 * for the given path, returning an errno
217ff4fbdf5SAlan Somers 	 */
218ff4fbdf5SAlan Somers 	void expect_unlink(uint64_t parent, const char *path, int error);
219ff4fbdf5SAlan Somers 
220ff4fbdf5SAlan Somers 	/*
2219821f1d3SAlan Somers 	 * Create an expectation that FUSE_WRITE will be called exactly once
222bda39894SAlan Somers 	 * for the given inode, at offset offset, with  size isize and buffer
223bda39894SAlan Somers 	 * contents.  Any flags present in flags_set must be set, and any
224bda39894SAlan Somers 	 * present in flags_unset must not be set.  Other flags are don't care.
225bda39894SAlan Somers 	 * It will return osize.
2269821f1d3SAlan Somers 	 */
2279821f1d3SAlan Somers 	void expect_write(uint64_t ino, uint64_t offset, uint64_t isize,
228bda39894SAlan Somers 		uint64_t osize, uint32_t flags_set, uint32_t flags_unset,
229bda39894SAlan Somers 		const void *contents);
23009c01e67SAlan Somers 
23116bd2d47SAlan Somers 	/* Protocol 7.8 version of expect_write */
23216bd2d47SAlan Somers 	void expect_write_7_8(uint64_t ino, uint64_t offset, uint64_t isize,
233bda39894SAlan Somers 		uint64_t osize, const void *contents);
23416bd2d47SAlan Somers 
23509c01e67SAlan Somers 	/*
23609c01e67SAlan Somers 	 * Helper that runs code in a child process.
23709c01e67SAlan Somers 	 *
23809c01e67SAlan Somers 	 * First, parent_func runs in the parent process.
23909c01e67SAlan Somers 	 * Then, child_func runs in the child process, dropping privileges if
24009c01e67SAlan Somers 	 * desired.
24109c01e67SAlan Somers 	 * Finally, fusetest_fork returns.
24209c01e67SAlan Somers 	 *
24309c01e67SAlan Somers 	 * # Returns
24409c01e67SAlan Somers 	 *
245a1542146SAlan Somers 	 * fusetest_fork may SKIP the test, which the caller should detect with
246a1542146SAlan Somers 	 * the IsSkipped() method.  If not, then the child's exit status will
247a1542146SAlan Somers 	 * be returned in status.
24809c01e67SAlan Somers 	 */
249a1542146SAlan Somers 	void fork(bool drop_privs, int *status,
25009c01e67SAlan Somers 		std::function<void()> parent_func,
25109c01e67SAlan Somers 		std::function<int()> child_func);
2527fc0921dSAlan Somers 
2537fc0921dSAlan Somers 	/*
2547fc0921dSAlan Somers 	 * Deliberately leak a file descriptor.
2557fc0921dSAlan Somers 	 *
2567fc0921dSAlan Somers 	 * Closing a file descriptor on fusefs would cause the server to
2577fc0921dSAlan Somers 	 * receive FUSE_CLOSE and possibly FUSE_INACTIVE.  Handling those
2587fc0921dSAlan Somers 	 * operations would needlessly complicate most tests.  So most tests
2597fc0921dSAlan Somers 	 * deliberately leak the file descriptors instead.  This method serves
2607fc0921dSAlan Somers 	 * to document the leakage, and provide a single point of suppression
2617fc0921dSAlan Somers 	 * for static analyzers.
2627fc0921dSAlan Somers 	 */
263dd21a916SAlan Somers 	/* coverity[+close: arg-0] */
leak(int fd __unused)2647fc0921dSAlan Somers 	static void leak(int fd __unused) {}
2657fc0921dSAlan Somers 
2667fc0921dSAlan Somers 	/*
2677fc0921dSAlan Somers 	 * Deliberately leak a DIR* pointer
2687fc0921dSAlan Somers 	 *
2697fc0921dSAlan Somers 	 * See comments for FuseTest::leak
2707fc0921dSAlan Somers 	 */
leakdir(DIR * dirp __unused)2717fc0921dSAlan Somers 	static void leakdir(DIR* dirp __unused) {}
2728d99a6b9SAlan Somers 
2738d99a6b9SAlan Somers 	/* Manually reclaim a vnode.  Requires root privileges. */
2748d99a6b9SAlan Somers 	void reclaim_vnode(const char *fullpath);
2759821f1d3SAlan Somers };
276