]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tests/sys/fs/fusefs/utils.hh
MFHead @347527
[FreeBSD/FreeBSD.git] / tests / sys / fs / fusefs / utils.hh
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
31 /*
32  * TODO: remove FUSE_WRITE_CACHE definition when upgrading to protocol 7.9.
33  * This bit was actually part of kernel protocol version 7.2, but never
34  * documented until 7.9
35  */
36 #ifndef FUSE_WRITE_CACHE
37 #define FUSE_WRITE_CACHE 1
38 #endif
39
40 /* Nanoseconds to sleep, for tests that must */
41 #define NAP_NS  (100'000'000)
42
43 void get_unprivileged_id(uid_t *uid, gid_t *gid);
44 inline void nap()
45 {
46         usleep(NAP_NS / 1000);
47 }
48
49 class FuseTest : public ::testing::Test {
50         protected:
51         uint32_t m_maxreadahead;
52         uint32_t m_init_flags;
53         bool m_allow_other;
54         bool m_default_permissions;
55         enum poll_method m_pm;
56         bool m_push_symlinks_in;
57         bool m_ro;
58         MockFS *m_mock = NULL;
59         const static uint64_t FH = 0xdeadbeef1a7ebabe;
60
61         public:
62         int m_maxbcachebuf;
63
64         FuseTest():
65                 /*
66                  * libfuse's default max_readahead is UINT_MAX, though it can
67                  * be lowered
68                  */
69                 m_maxreadahead(UINT_MAX),
70                 m_init_flags(0),
71                 m_allow_other(false),
72                 m_default_permissions(false),
73                 m_pm(BLOCKING),
74                 m_push_symlinks_in(false),
75                 m_ro(false)
76         {}
77
78         virtual void SetUp();
79
80         virtual void TearDown() {
81                 if (m_mock)
82                         delete m_mock;
83         }
84
85         /*
86          * Create an expectation that FUSE_ACCESS will be called once for the
87          * given inode with the given access_mode, returning the given errno
88          */
89         void expect_access(uint64_t ino, mode_t access_mode, int error);
90
91         /* Expect FUSE_DESTROY and shutdown the daemon */
92         void expect_destroy(int error);
93
94         /*
95          * Create an expectation that FUSE_FLUSH will be called times times for
96          * the given inode
97          */
98         void expect_flush(uint64_t ino, int times, ProcessMockerT r);
99
100         /*
101          * Create an expectation that FUSE_FORGET will be called for the given
102          * inode.  There will be no response
103          */
104         void expect_forget(uint64_t ino, uint64_t nlookup);
105
106         /*
107          * Create an expectation that FUSE_GETATTR will be called for the given
108          * inode any number of times.  It will respond with a few basic
109          * attributes, like the given size and the mode S_IFREG | 0644
110          */
111         void expect_getattr(uint64_t ino, uint64_t size);
112
113         /*
114          * Create an expectation that FUSE_LOOKUP will be called for the given
115          * path exactly times times and cache validity period.  It will respond
116          * with inode ino, mode mode, filesize size.
117          */
118         void expect_lookup(const char *relpath, uint64_t ino, mode_t mode,
119                 uint64_t size, int times, uint64_t attr_valid = UINT64_MAX,
120                 uid_t uid = 0, gid_t gid = 0);
121
122         /*
123          * Create an expectation that FUSE_OPEN will be called for the given
124          * inode exactly times times.  It will return with open_flags flags and
125          * file handle FH.
126          */
127         void expect_open(uint64_t ino, uint32_t flags, int times);
128
129         /*
130          * Create an expectation that FUSE_OPENDIR will be called exactly once
131          * for inode ino.
132          */
133         void expect_opendir(uint64_t ino);
134
135         /*
136          * Create an expectation that FUSE_READ will be called exactly once for
137          * the given inode, at offset offset and with size isize.  It will
138          * return the first osize bytes from contents
139          */
140         void expect_read(uint64_t ino, uint64_t offset, uint64_t isize,
141                 uint64_t osize, const void *contents);
142
143         /* 
144          * Create an expectation that FUSE_RELEASE will be called exactly once
145          * for the given inode and filehandle, returning success
146          */
147         void expect_release(uint64_t ino, uint64_t fh);
148
149         /*
150          * Create an expectation that FUSE_RELEASEDIR will be called exactly
151          * once for the given inode
152          */
153         void expect_releasedir(uint64_t ino, ProcessMockerT r);
154
155         /*
156          * Create an expectation that FUSE_UNLINK will be called exactly once
157          * for the given path, returning an errno
158          */
159         void expect_unlink(uint64_t parent, const char *path, int error);
160
161         /*
162          * Create an expectation that FUSE_WRITE will be called exactly once
163          * for the given inode, at offset offset, with write_flags flags, 
164          * size isize and buffer contents.  It will return osize
165          */
166         void expect_write(uint64_t ino, uint64_t offset, uint64_t isize,
167                 uint64_t osize, uint32_t flags, const void *contents);
168
169         /*
170          * Helper that runs code in a child process.
171          *
172          * First, parent_func runs in the parent process.
173          * Then, child_func runs in the child process, dropping privileges if
174          * desired.
175          * Finally, fusetest_fork returns.
176          *
177          * # Returns
178          *
179          * fusetest_fork may SKIP the test, which the caller should detect with
180          * the IsSkipped() method.  If not, then the child's exit status will
181          * be returned in status.
182          */
183         void fork(bool drop_privs, int *status,
184                 std::function<void()> parent_func,
185                 std::function<int()> child_func);
186 };