]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tests/sys/fs/fusefs/utils.cc
fusefs: improvements to interruptibility
[FreeBSD/FreeBSD.git] / tests / sys / fs / fusefs / utils.cc
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 extern "C" {
32 #include <sys/param.h>
33 #include <sys/mman.h>
34 #include <sys/module.h>
35 #include <sys/sysctl.h>
36 #include <sys/wait.h>
37
38 #include <pwd.h>
39 #include <semaphore.h>
40 #include <unistd.h>
41 }
42
43 #include <gtest/gtest.h>
44
45 #include "mockfs.hh"
46 #include "utils.hh"
47
48 using namespace testing;
49
50 /* Check that fusefs(4) is accessible and the current user can mount(2) */
51 void check_environment()
52 {
53         const char *devnode = "/dev/fuse";
54         const char *usermount_node = "vfs.usermount";
55         int usermount_val = 0;
56         size_t usermount_size = sizeof(usermount_val);
57         if (eaccess(devnode, R_OK | W_OK)) {
58                 if (errno == ENOENT) {
59                         GTEST_SKIP() << devnode << " does not exist";
60                 } else if (errno == EACCES) {
61                         GTEST_SKIP() << devnode <<
62                             " is not accessible by the current user";
63                 } else {
64                         GTEST_SKIP() << strerror(errno);
65                 }
66         }
67         sysctlbyname(usermount_node, &usermount_val, &usermount_size,
68                      NULL, 0);
69         if (geteuid() != 0 && !usermount_val)
70                 GTEST_SKIP() << "current user is not allowed to mount";
71 }
72
73 class FuseEnv: public Environment {
74         virtual void SetUp() {
75         }
76 };
77
78 void FuseTest::SetUp() {
79         const char *node = "vfs.maxbcachebuf";
80         int val = 0;
81         size_t size = sizeof(val);
82
83         /*
84          * XXX check_environment should be called from FuseEnv::SetUp, but
85          * can't due to https://github.com/google/googletest/issues/2189
86          */
87         check_environment();
88         if (IsSkipped())
89                 return;
90
91         ASSERT_EQ(0, sysctlbyname(node, &val, &size, NULL, 0))
92                 << strerror(errno);
93         m_maxbcachebuf = val;
94
95         try {
96                 m_mock = new MockFS(m_maxreadahead, m_allow_other,
97                         m_default_permissions, m_push_symlinks_in, m_ro,
98                         m_init_flags);
99                 /* 
100                  * FUSE_ACCESS is called almost universally.  Expecting it in
101                  * each test case would be super-annoying.  Instead, set a
102                  * default expectation for FUSE_ACCESS and return ENOSYS.
103                  *
104                  * Individual test cases can override this expectation since
105                  * googlemock evaluates expectations in LIFO order.
106                  */
107                 EXPECT_CALL(*m_mock, process(
108                         ResultOf([=](auto in) {
109                                 return (in->header.opcode == FUSE_ACCESS);
110                         }, Eq(true)),
111                         _)
112                 ).Times(AnyNumber())
113                 .WillRepeatedly(Invoke(ReturnErrno(ENOSYS)));
114         } catch (std::system_error err) {
115                 FAIL() << err.what();
116         }
117 }
118
119 void
120 FuseTest::expect_access(uint64_t ino, mode_t access_mode, int error)
121 {
122         EXPECT_CALL(*m_mock, process(
123                 ResultOf([=](auto in) {
124                         return (in->header.opcode == FUSE_ACCESS &&
125                                 in->header.nodeid == ino &&
126                                 in->body.access.mask == access_mode);
127                 }, Eq(true)),
128                 _)
129         ).WillOnce(Invoke(ReturnErrno(error)));
130 }
131
132 void
133 FuseTest::expect_flush(uint64_t ino, int times, ProcessMockerT r)
134 {
135         EXPECT_CALL(*m_mock, process(
136                 ResultOf([=](auto in) {
137                         return (in->header.opcode == FUSE_FLUSH &&
138                                 in->header.nodeid == ino);
139                 }, Eq(true)),
140                 _)
141         ).Times(times)
142         .WillRepeatedly(Invoke(r));
143 }
144
145 void
146 FuseTest::expect_forget(uint64_t ino, uint64_t nlookup)
147 {
148         EXPECT_CALL(*m_mock, process(
149                 ResultOf([=](auto in) {
150                         return (in->header.opcode == FUSE_FORGET &&
151                                 in->header.nodeid == ino &&
152                                 in->body.forget.nlookup == nlookup);
153                 }, Eq(true)),
154                 _)
155         ).WillOnce(Invoke([](auto in __unused, auto &out __unused) {
156                 /* FUSE_FORGET has no response! */
157         }));
158 }
159
160 void FuseTest::expect_getattr(uint64_t ino, uint64_t size)
161 {
162         EXPECT_CALL(*m_mock, process(
163                 ResultOf([=](auto in) {
164                         return (in->header.opcode == FUSE_GETATTR &&
165                                 in->header.nodeid == ino);
166                 }, Eq(true)),
167                 _)
168         ).WillOnce(Invoke(ReturnImmediate([=](auto i __unused, auto out) {
169                 SET_OUT_HEADER_LEN(out, attr);
170                 out->body.attr.attr.ino = ino;  // Must match nodeid
171                 out->body.attr.attr.mode = S_IFREG | 0644;
172                 out->body.attr.attr.size = size;
173                 out->body.attr.attr_valid = UINT64_MAX;
174         })));
175 }
176
177 void FuseTest::expect_lookup(const char *relpath, uint64_t ino, mode_t mode,
178         uint64_t size, int times, uint64_t attr_valid, uid_t uid)
179 {
180         EXPECT_LOOKUP(1, relpath)
181         .Times(times)
182         .WillRepeatedly(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
183                 SET_OUT_HEADER_LEN(out, entry);
184                 out->body.entry.attr.mode = mode;
185                 out->body.entry.nodeid = ino;
186                 out->body.entry.attr.nlink = 1;
187                 out->body.entry.attr_valid = attr_valid;
188                 out->body.entry.attr.size = size;
189                 out->body.entry.attr.uid = uid;
190         })));
191 }
192
193 void FuseTest::expect_open(uint64_t ino, uint32_t flags, int times)
194 {
195         EXPECT_CALL(*m_mock, process(
196                 ResultOf([=](auto in) {
197                         return (in->header.opcode == FUSE_OPEN &&
198                                 in->header.nodeid == ino);
199                 }, Eq(true)),
200                 _)
201         ).Times(times)
202         .WillRepeatedly(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
203                 out->header.len = sizeof(out->header);
204                 SET_OUT_HEADER_LEN(out, open);
205                 out->body.open.fh = FH;
206                 out->body.open.open_flags = flags;
207         })));
208 }
209
210 void FuseTest::expect_opendir(uint64_t ino)
211 {
212         /* opendir(3) calls fstatfs */
213         EXPECT_CALL(*m_mock, process(
214                 ResultOf([](auto in) {
215                         return (in->header.opcode == FUSE_STATFS);
216                 }, Eq(true)),
217                 _)
218         ).WillRepeatedly(Invoke(ReturnImmediate([=](auto i __unused, auto out) {
219                 SET_OUT_HEADER_LEN(out, statfs);
220         })));
221
222         EXPECT_CALL(*m_mock, process(
223                 ResultOf([=](auto in) {
224                         return (in->header.opcode == FUSE_OPENDIR &&
225                                 in->header.nodeid == ino);
226                 }, Eq(true)),
227                 _)
228         ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
229                 out->header.len = sizeof(out->header);
230                 SET_OUT_HEADER_LEN(out, open);
231                 out->body.open.fh = FH;
232         })));
233 }
234
235 void FuseTest::expect_read(uint64_t ino, uint64_t offset, uint64_t isize,
236         uint64_t osize, const void *contents)
237 {
238         EXPECT_CALL(*m_mock, process(
239                 ResultOf([=](auto in) {
240                         return (in->header.opcode == FUSE_READ &&
241                                 in->header.nodeid == ino &&
242                                 in->body.read.fh == FH &&
243                                 in->body.read.offset == offset &&
244                                 in->body.read.size == isize);
245                 }, Eq(true)),
246                 _)
247         ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
248                 out->header.len = sizeof(struct fuse_out_header) + osize;
249                 memmove(out->body.bytes, contents, osize);
250         }))).RetiresOnSaturation();
251 }
252
253 void FuseTest::expect_release(uint64_t ino, uint64_t fh)
254 {
255         EXPECT_CALL(*m_mock, process(
256                 ResultOf([=](auto in) {
257                         return (in->header.opcode == FUSE_RELEASE &&
258                                 in->header.nodeid == ino &&
259                                 in->body.release.fh == fh);
260                 }, Eq(true)),
261                 _)
262         ).WillOnce(Invoke(ReturnErrno(0)));
263 }
264
265 void FuseTest::expect_releasedir(uint64_t ino, ProcessMockerT r)
266 {
267         EXPECT_CALL(*m_mock, process(
268                 ResultOf([=](auto in) {
269                         return (in->header.opcode == FUSE_RELEASEDIR &&
270                                 in->header.nodeid == ino &&
271                                 in->body.release.fh == FH);
272                 }, Eq(true)),
273                 _)
274         ).WillOnce(Invoke(r));
275 }
276
277 void FuseTest::expect_unlink(uint64_t parent, const char *path, int error)
278 {
279         EXPECT_CALL(*m_mock, process(
280                 ResultOf([=](auto in) {
281                         return (in->header.opcode == FUSE_UNLINK &&
282                                 0 == strcmp(path, in->body.unlink) &&
283                                 in->header.nodeid == parent);
284                 }, Eq(true)),
285                 _)
286         ).WillOnce(Invoke(ReturnErrno(error)));
287 }
288
289 void FuseTest::expect_write(uint64_t ino, uint64_t offset, uint64_t isize,
290         uint64_t osize, uint32_t flags, const void *contents)
291 {
292         EXPECT_CALL(*m_mock, process(
293                 ResultOf([=](auto in) {
294                         const char *buf = (const char*)in->body.bytes +
295                                 sizeof(struct fuse_write_in);
296                         bool pid_ok;
297
298                         if (in->body.write.write_flags & FUSE_WRITE_CACHE)
299                                 pid_ok = true;
300                         else
301                                 pid_ok = (pid_t)in->header.pid == getpid();
302
303                         return (in->header.opcode == FUSE_WRITE &&
304                                 in->header.nodeid == ino &&
305                                 in->body.write.fh == FH &&
306                                 in->body.write.offset == offset  &&
307                                 in->body.write.size == isize &&
308                                 pid_ok &&
309                                 in->body.write.write_flags == flags &&
310                                 0 == bcmp(buf, contents, isize));
311                 }, Eq(true)),
312                 _)
313         ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
314                 SET_OUT_HEADER_LEN(out, write);
315                 out->body.write.size = osize;
316         })));
317 }
318
319 static void
320 get_unprivileged_uid(uid_t *uid)
321 {
322         struct passwd *pw;
323
324         /* 
325          * First try "tests", Kyua's default unprivileged user.  XXX after
326          * GoogleTest gains a proper Kyua wrapper, get this with the Kyua API
327          */
328         pw = getpwnam("tests");
329         if (pw == NULL) {
330                 /* Fall back to "nobody" */
331                 pw = getpwnam("nobody");
332         }
333         if (pw == NULL)
334                 GTEST_SKIP() << "Test requires an unprivileged user";
335         *uid = pw->pw_uid;
336 }
337
338 void
339 FuseTest::fork(bool drop_privs, int *child_status,
340         std::function<void()> parent_func,
341         std::function<int()> child_func)
342 {
343         sem_t *sem;
344         int mprot = PROT_READ | PROT_WRITE;
345         int mflags = MAP_ANON | MAP_SHARED;
346         pid_t child;
347         uid_t uid;
348         
349         if (drop_privs) {
350                 get_unprivileged_uid(&uid);
351                 if (IsSkipped())
352                         return;
353         }
354
355         sem = (sem_t*)mmap(NULL, sizeof(*sem), mprot, mflags, -1, 0);
356         ASSERT_NE(MAP_FAILED, sem) << strerror(errno);
357         ASSERT_EQ(0, sem_init(sem, 1, 0)) << strerror(errno);
358
359         if ((child = ::fork()) == 0) {
360                 /* In child */
361                 int err = 0;
362
363                 if (sem_wait(sem)) {
364                         perror("sem_wait");
365                         err = 1;
366                         goto out;
367                 }
368
369                 if (drop_privs && 0 != setreuid(-1, uid)) {
370                         perror("setreuid");
371                         err = 1;
372                         goto out;
373                 }
374                 err = child_func();
375
376 out:
377                 sem_destroy(sem);
378                 _exit(err);
379         } else if (child > 0) {
380                 /* 
381                  * In parent.  Cleanup must happen here, because it's still
382                  * privileged.
383                  */
384                 m_mock->m_child_pid = child;
385                 ASSERT_NO_FATAL_FAILURE(parent_func());
386
387                 /* Signal the child process to go */
388                 ASSERT_EQ(0, sem_post(sem)) << strerror(errno);
389
390                 ASSERT_LE(0, wait(child_status)) << strerror(errno);
391         } else {
392                 FAIL() << strerror(errno);
393         }
394         munmap(sem, sizeof(*sem));
395         return;
396 }
397
398 static void usage(char* progname) {
399         fprintf(stderr, "Usage: %s [-v]\n\t-v increase verbosity\n", progname);
400         exit(2);
401 }
402
403 int main(int argc, char **argv) {
404         int ch;
405         FuseEnv *fuse_env = new FuseEnv;
406
407         InitGoogleTest(&argc, argv);
408         AddGlobalTestEnvironment(fuse_env);
409
410         while ((ch = getopt(argc, argv, "v")) != -1) {
411                 switch (ch) {
412                         case 'v':
413                                 verbosity++;
414                                 break;
415                         default:
416                                 usage(argv[0]);
417                                 break;
418                 }
419         }
420
421         return (RUN_ALL_TESTS());
422 }