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