2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2019 The FreeBSD Foundation
6 * This software was developed by BFF Storage Systems, LLC under sponsorship
7 * from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/param.h>
34 #include <sys/module.h>
35 #include <sys/sysctl.h>
42 #include <semaphore.h>
46 #include <gtest/gtest.h>
51 using namespace testing;
54 * The default max_write is set to this formula in libfuse, though
55 * individual filesystems can lower it. The "- 4096" was added in
56 * commit 154ffe2, with the commit message "fix".
58 const uint32_t libfuse_max_write = 32 * getpagesize() + 0x1000 - 4096;
61 * Set the default max_write to a distinct value from MAXPHYS to catch bugs
62 * that confuse the two.
64 const uint32_t default_max_write = MIN(libfuse_max_write, MAXPHYS / 2);
67 /* Check that fusefs(4) is accessible and the current user can mount(2) */
68 void check_environment()
70 const char *devnode = "/dev/fuse";
71 const char *usermount_node = "vfs.usermount";
72 int usermount_val = 0;
73 size_t usermount_size = sizeof(usermount_val);
74 if (eaccess(devnode, R_OK | W_OK)) {
75 if (errno == ENOENT) {
76 GTEST_SKIP() << devnode << " does not exist";
77 } else if (errno == EACCES) {
78 GTEST_SKIP() << devnode <<
79 " is not accessible by the current user";
81 GTEST_SKIP() << strerror(errno);
84 sysctlbyname(usermount_node, &usermount_val, &usermount_size,
86 if (geteuid() != 0 && !usermount_val)
87 GTEST_SKIP() << "current user is not allowed to mount";
90 class FuseEnv: public Environment {
91 virtual void SetUp() {
95 void FuseTest::SetUp() {
96 const char *maxbcachebuf_node = "vfs.maxbcachebuf";
97 const char *maxphys_node = "kern.maxphys";
99 size_t size = sizeof(val);
102 * XXX check_environment should be called from FuseEnv::SetUp, but
103 * can't due to https://github.com/google/googletest/issues/2189
109 ASSERT_EQ(0, sysctlbyname(maxbcachebuf_node, &val, &size, NULL, 0))
111 m_maxbcachebuf = val;
112 ASSERT_EQ(0, sysctlbyname(maxphys_node, &val, &size, NULL, 0))
117 m_mock = new MockFS(m_maxreadahead, m_allow_other,
118 m_default_permissions, m_push_symlinks_in, m_ro,
119 m_pm, m_init_flags, m_kernel_minor_version,
120 m_maxwrite, m_async, m_noclusterr, m_time_gran,
123 * FUSE_ACCESS is called almost universally. Expecting it in
124 * each test case would be super-annoying. Instead, set a
125 * default expectation for FUSE_ACCESS and return ENOSYS.
127 * Individual test cases can override this expectation since
128 * googlemock evaluates expectations in LIFO order.
130 EXPECT_CALL(*m_mock, process(
131 ResultOf([=](auto in) {
132 return (in.header.opcode == FUSE_ACCESS);
136 .WillRepeatedly(Invoke(ReturnErrno(ENOSYS)));
138 * FUSE_BMAP is called for most test cases that read data. Set
139 * a default expectation and return ENOSYS.
141 * Individual test cases can override this expectation since
142 * googlemock evaluates expectations in LIFO order.
144 EXPECT_CALL(*m_mock, process(
145 ResultOf([=](auto in) {
146 return (in.header.opcode == FUSE_BMAP);
150 .WillRepeatedly(Invoke(ReturnErrno(ENOSYS)));
151 } catch (std::system_error err) {
152 FAIL() << err.what();
157 FuseTest::expect_access(uint64_t ino, mode_t access_mode, int error)
159 EXPECT_CALL(*m_mock, process(
160 ResultOf([=](auto in) {
161 return (in.header.opcode == FUSE_ACCESS &&
162 in.header.nodeid == ino &&
163 in.body.access.mask == access_mode);
166 ).WillOnce(Invoke(ReturnErrno(error)));
170 FuseTest::expect_destroy(int error)
172 EXPECT_CALL(*m_mock, process(
173 ResultOf([=](auto in) {
174 return (in.header.opcode == FUSE_DESTROY);
177 ).WillOnce(Invoke( ReturnImmediate([&](auto in, auto& out) {
178 m_mock->m_quit = true;
179 out.header.len = sizeof(out.header);
180 out.header.unique = in.header.unique;
181 out.header.error = -error;
186 FuseTest::expect_flush(uint64_t ino, int times, ProcessMockerT r)
188 EXPECT_CALL(*m_mock, process(
189 ResultOf([=](auto in) {
190 return (in.header.opcode == FUSE_FLUSH &&
191 in.header.nodeid == ino);
195 .WillRepeatedly(Invoke(r));
199 FuseTest::expect_forget(uint64_t ino, uint64_t nlookup, sem_t *sem)
201 EXPECT_CALL(*m_mock, process(
202 ResultOf([=](auto in) {
203 return (in.header.opcode == FUSE_FORGET &&
204 in.header.nodeid == ino &&
205 in.body.forget.nlookup == nlookup);
208 ).WillOnce(Invoke([=](auto in __unused, auto &out __unused) {
211 /* FUSE_FORGET has no response! */
215 void FuseTest::expect_getattr(uint64_t ino, uint64_t size)
217 EXPECT_CALL(*m_mock, process(
218 ResultOf([=](auto in) {
219 return (in.header.opcode == FUSE_GETATTR &&
220 in.header.nodeid == ino);
223 ).WillOnce(Invoke(ReturnImmediate([=](auto i __unused, auto& out) {
224 SET_OUT_HEADER_LEN(out, attr);
225 out.body.attr.attr.ino = ino; // Must match nodeid
226 out.body.attr.attr.mode = S_IFREG | 0644;
227 out.body.attr.attr.size = size;
228 out.body.attr.attr_valid = UINT64_MAX;
232 void FuseTest::expect_lookup(const char *relpath, uint64_t ino, mode_t mode,
233 uint64_t size, int times, uint64_t attr_valid, uid_t uid, gid_t gid)
235 EXPECT_LOOKUP(FUSE_ROOT_ID, relpath)
237 .WillRepeatedly(Invoke(
238 ReturnImmediate([=](auto in __unused, auto& out) {
239 SET_OUT_HEADER_LEN(out, entry);
240 out.body.entry.attr.mode = mode;
241 out.body.entry.nodeid = ino;
242 out.body.entry.attr.nlink = 1;
243 out.body.entry.attr_valid = attr_valid;
244 out.body.entry.attr.size = size;
245 out.body.entry.attr.uid = uid;
246 out.body.entry.attr.gid = gid;
250 void FuseTest::expect_lookup_7_8(const char *relpath, uint64_t ino, mode_t mode,
251 uint64_t size, int times, uint64_t attr_valid, uid_t uid, gid_t gid)
253 EXPECT_LOOKUP(FUSE_ROOT_ID, relpath)
255 .WillRepeatedly(Invoke(
256 ReturnImmediate([=](auto in __unused, auto& out) {
257 SET_OUT_HEADER_LEN(out, entry_7_8);
258 out.body.entry.attr.mode = mode;
259 out.body.entry.nodeid = ino;
260 out.body.entry.attr.nlink = 1;
261 out.body.entry.attr_valid = attr_valid;
262 out.body.entry.attr.size = size;
263 out.body.entry.attr.uid = uid;
264 out.body.entry.attr.gid = gid;
268 void FuseTest::expect_open(uint64_t ino, uint32_t flags, int times)
270 EXPECT_CALL(*m_mock, process(
271 ResultOf([=](auto in) {
272 return (in.header.opcode == FUSE_OPEN &&
273 in.header.nodeid == ino);
277 .WillRepeatedly(Invoke(
278 ReturnImmediate([=](auto in __unused, auto& out) {
279 out.header.len = sizeof(out.header);
280 SET_OUT_HEADER_LEN(out, open);
281 out.body.open.fh = FH;
282 out.body.open.open_flags = flags;
286 void FuseTest::expect_opendir(uint64_t ino)
288 /* opendir(3) calls fstatfs */
289 EXPECT_CALL(*m_mock, process(
290 ResultOf([](auto in) {
291 return (in.header.opcode == FUSE_STATFS);
294 ).WillRepeatedly(Invoke(
295 ReturnImmediate([=](auto i __unused, auto& out) {
296 SET_OUT_HEADER_LEN(out, statfs);
299 EXPECT_CALL(*m_mock, process(
300 ResultOf([=](auto in) {
301 return (in.header.opcode == FUSE_OPENDIR &&
302 in.header.nodeid == ino);
305 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
306 out.header.len = sizeof(out.header);
307 SET_OUT_HEADER_LEN(out, open);
308 out.body.open.fh = FH;
312 void FuseTest::expect_read(uint64_t ino, uint64_t offset, uint64_t isize,
313 uint64_t osize, const void *contents, int flags)
315 EXPECT_CALL(*m_mock, process(
316 ResultOf([=](auto in) {
317 return (in.header.opcode == FUSE_READ &&
318 in.header.nodeid == ino &&
319 in.body.read.fh == FH &&
320 in.body.read.offset == offset &&
321 in.body.read.size == isize &&
323 (in.body.read.flags == O_RDONLY ||
324 in.body.read.flags == O_RDWR)
325 : in.body.read.flags == (uint32_t)flags);
328 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
329 out.header.len = sizeof(struct fuse_out_header) + osize;
330 memmove(out.body.bytes, contents, osize);
331 }))).RetiresOnSaturation();
334 void FuseTest::expect_readdir(uint64_t ino, uint64_t off,
335 std::vector<struct dirent> &ents)
337 EXPECT_CALL(*m_mock, process(
338 ResultOf([=](auto in) {
339 return (in.header.opcode == FUSE_READDIR &&
340 in.header.nodeid == ino &&
341 in.body.readdir.fh == FH &&
342 in.body.readdir.offset == off);
345 ).WillRepeatedly(Invoke(ReturnImmediate([=](auto in, auto& out) {
346 struct fuse_dirent *fde = (struct fuse_dirent*)&(out.body);
349 out.header.error = 0;
352 for (const auto& it: ents) {
353 size_t entlen, entsize;
355 fde->ino = it.d_fileno;
357 fde->type = it.d_type;
358 fde->namelen = it.d_namlen;
359 strncpy(fde->name, it.d_name, it.d_namlen);
360 entlen = FUSE_NAME_OFFSET + fde->namelen;
361 entsize = FUSE_DIRENT_SIZE(fde);
363 * The FUSE protocol does not require zeroing out the
364 * unused portion of the name. But it's a good
365 * practice to prevent information disclosure to the
366 * FUSE client, even though the client is usually the
369 memset(fde->name + fde->namelen, 0, entsize - entlen);
370 if (out.header.len + entsize > in.body.read.size) {
371 printf("Overflow in readdir expectation: i=%d\n"
375 out.header.len += entsize;
376 fde = (struct fuse_dirent*)
377 ((intmax_t*)fde + entsize / sizeof(intmax_t));
380 out.header.len += sizeof(out.header);
384 void FuseTest::expect_release(uint64_t ino, uint64_t fh)
386 EXPECT_CALL(*m_mock, process(
387 ResultOf([=](auto in) {
388 return (in.header.opcode == FUSE_RELEASE &&
389 in.header.nodeid == ino &&
390 in.body.release.fh == fh);
393 ).WillOnce(Invoke(ReturnErrno(0)));
396 void FuseTest::expect_releasedir(uint64_t ino, ProcessMockerT r)
398 EXPECT_CALL(*m_mock, process(
399 ResultOf([=](auto in) {
400 return (in.header.opcode == FUSE_RELEASEDIR &&
401 in.header.nodeid == ino &&
402 in.body.release.fh == FH);
405 ).WillOnce(Invoke(r));
408 void FuseTest::expect_unlink(uint64_t parent, const char *path, int error)
410 EXPECT_CALL(*m_mock, process(
411 ResultOf([=](auto in) {
412 return (in.header.opcode == FUSE_UNLINK &&
413 0 == strcmp(path, in.body.unlink) &&
414 in.header.nodeid == parent);
417 ).WillOnce(Invoke(ReturnErrno(error)));
420 void FuseTest::expect_write(uint64_t ino, uint64_t offset, uint64_t isize,
421 uint64_t osize, uint32_t flags_set, uint32_t flags_unset,
422 const void *contents)
424 EXPECT_CALL(*m_mock, process(
425 ResultOf([=](auto in) {
426 const char *buf = (const char*)in.body.bytes +
427 sizeof(struct fuse_write_in);
429 uint32_t wf = in.body.write.write_flags;
431 if (wf & FUSE_WRITE_CACHE)
434 pid_ok = (pid_t)in.header.pid == getpid();
436 return (in.header.opcode == FUSE_WRITE &&
437 in.header.nodeid == ino &&
438 in.body.write.fh == FH &&
439 in.body.write.offset == offset &&
440 in.body.write.size == isize &&
442 (wf & flags_set) == flags_set &&
443 (wf & flags_unset) == 0 &&
444 (in.body.write.flags == O_WRONLY ||
445 in.body.write.flags == O_RDWR) &&
446 0 == bcmp(buf, contents, isize));
449 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
450 SET_OUT_HEADER_LEN(out, write);
451 out.body.write.size = osize;
455 void FuseTest::expect_write_7_8(uint64_t ino, uint64_t offset, uint64_t isize,
456 uint64_t osize, const void *contents)
458 EXPECT_CALL(*m_mock, process(
459 ResultOf([=](auto in) {
460 const char *buf = (const char*)in.body.bytes +
461 FUSE_COMPAT_WRITE_IN_SIZE;
462 bool pid_ok = (pid_t)in.header.pid == getpid();
463 return (in.header.opcode == FUSE_WRITE &&
464 in.header.nodeid == ino &&
465 in.body.write.fh == FH &&
466 in.body.write.offset == offset &&
467 in.body.write.size == isize &&
469 0 == bcmp(buf, contents, isize));
472 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
473 SET_OUT_HEADER_LEN(out, write);
474 out.body.write.size = osize;
479 get_unprivileged_id(uid_t *uid, gid_t *gid)
485 * First try "tests", Kyua's default unprivileged user. XXX after
486 * GoogleTest gains a proper Kyua wrapper, get this with the Kyua API
488 pw = getpwnam("tests");
490 /* Fall back to "nobody" */
491 pw = getpwnam("nobody");
494 GTEST_SKIP() << "Test requires an unprivileged user";
495 /* Use group "nobody", which is Kyua's default unprivileged group */
496 gr = getgrnam("nobody");
498 GTEST_SKIP() << "Test requires an unprivileged group";
504 FuseTest::fork(bool drop_privs, int *child_status,
505 std::function<void()> parent_func,
506 std::function<int()> child_func)
509 int mprot = PROT_READ | PROT_WRITE;
510 int mflags = MAP_ANON | MAP_SHARED;
516 get_unprivileged_id(&uid, &gid);
521 sem = (sem_t*)mmap(NULL, sizeof(*sem), mprot, mflags, -1, 0);
522 ASSERT_NE(MAP_FAILED, sem) << strerror(errno);
523 ASSERT_EQ(0, sem_init(sem, 1, 0)) << strerror(errno);
525 if ((child = ::fork()) == 0) {
535 if (drop_privs && 0 != setegid(gid)) {
540 if (drop_privs && 0 != setreuid(-1, uid)) {
550 } else if (child > 0) {
552 * In parent. Cleanup must happen here, because it's still
555 m_mock->m_child_pid = child;
556 ASSERT_NO_FATAL_FAILURE(parent_func());
558 /* Signal the child process to go */
559 ASSERT_EQ(0, sem_post(sem)) << strerror(errno);
561 ASSERT_LE(0, wait(child_status)) << strerror(errno);
563 FAIL() << strerror(errno);
565 munmap(sem, sizeof(*sem));
569 static void usage(char* progname) {
570 fprintf(stderr, "Usage: %s [-v]\n\t-v increase verbosity\n", progname);
574 int main(int argc, char **argv) {
576 FuseEnv *fuse_env = new FuseEnv;
578 InitGoogleTest(&argc, argv);
579 AddGlobalTestEnvironment(fuse_env);
581 while ((ch = getopt(argc, argv, "v")) != -1) {
592 return (RUN_ALL_TESTS());