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
34 #include <sys/param.h>
36 #include <sys/module.h>
37 #include <sys/sysctl.h>
44 #include <semaphore.h>
48 #include <gtest/gtest.h>
53 using namespace testing;
56 * The default max_write is set to this formula in libfuse, though
57 * individual filesystems can lower it. The "- 4096" was added in
58 * commit 154ffe2, with the commit message "fix".
60 const uint32_t libfuse_max_write = 32 * getpagesize() + 0x1000 - 4096;
63 * Set the default max_write to a distinct value from MAXPHYS to catch bugs
64 * that confuse the two.
66 const uint32_t default_max_write = MIN(libfuse_max_write, MAXPHYS / 2);
69 /* Check that fusefs(4) is accessible and the current user can mount(2) */
70 void check_environment()
72 const char *devnode = "/dev/fuse";
73 const char *bsdextended_node = "security.mac.bsdextended.enabled";
74 int bsdextended_val = 0;
75 size_t bsdextended_size = sizeof(bsdextended_val);
76 int bsdextended_found;
77 const char *usermount_node = "vfs.usermount";
78 int usermount_val = 0;
79 size_t usermount_size = sizeof(usermount_val);
80 if (eaccess(devnode, R_OK | W_OK)) {
81 if (errno == ENOENT) {
82 GTEST_SKIP() << devnode << " does not exist";
83 } else if (errno == EACCES) {
84 GTEST_SKIP() << devnode <<
85 " is not accessible by the current user";
87 GTEST_SKIP() << strerror(errno);
90 // mac_bsdextended(4), when enabled, generates many more GETATTR
91 // operations. The fusefs tests' expectations don't account for those,
92 // and adding extra code to handle them obfuscates the real purpose of
93 // the tests. Better just to skip the fusefs tests if mac_bsdextended
95 bsdextended_found = sysctlbyname(bsdextended_node, &bsdextended_val,
96 &bsdextended_size, NULL, 0);
97 if (bsdextended_found == 0 && bsdextended_val != 0)
99 "The fusefs tests are incompatible with mac_bsdextended.";
100 ASSERT_EQ(sysctlbyname(usermount_node, &usermount_val, &usermount_size,
103 if (geteuid() != 0 && !usermount_val)
104 GTEST_SKIP() << "current user is not allowed to mount";
107 const char *cache_mode_to_s(enum cache_mode cm) {
112 return "Writethrough";
116 return "WritebackAsync";
122 bool is_unsafe_aio_enabled(void) {
123 const char *node = "vfs.aio.enable_unsafe";
125 size_t size = sizeof(val);
127 if (sysctlbyname(node, &val, &size, NULL, 0)) {
128 perror("sysctlbyname");
134 class FuseEnv: public Environment {
135 virtual void SetUp() {
139 void FuseTest::SetUp() {
140 const char *maxbcachebuf_node = "vfs.maxbcachebuf";
141 const char *maxphys_node = "kern.maxphys";
143 size_t size = sizeof(val);
146 * XXX check_environment should be called from FuseEnv::SetUp, but
147 * can't due to https://github.com/google/googletest/issues/2189
153 ASSERT_EQ(0, sysctlbyname(maxbcachebuf_node, &val, &size, NULL, 0))
155 m_maxbcachebuf = val;
156 ASSERT_EQ(0, sysctlbyname(maxphys_node, &val, &size, NULL, 0))
161 m_mock = new MockFS(m_maxreadahead, m_allow_other,
162 m_default_permissions, m_push_symlinks_in, m_ro,
163 m_pm, m_init_flags, m_kernel_minor_version,
164 m_maxwrite, m_async, m_noclusterr, m_time_gran,
167 * FUSE_ACCESS is called almost universally. Expecting it in
168 * each test case would be super-annoying. Instead, set a
169 * default expectation for FUSE_ACCESS and return ENOSYS.
171 * Individual test cases can override this expectation since
172 * googlemock evaluates expectations in LIFO order.
174 EXPECT_CALL(*m_mock, process(
175 ResultOf([=](auto in) {
176 return (in.header.opcode == FUSE_ACCESS);
180 .WillRepeatedly(Invoke(ReturnErrno(ENOSYS)));
182 * FUSE_BMAP is called for most test cases that read data. Set
183 * a default expectation and return ENOSYS.
185 * Individual test cases can override this expectation since
186 * googlemock evaluates expectations in LIFO order.
188 EXPECT_CALL(*m_mock, process(
189 ResultOf([=](auto in) {
190 return (in.header.opcode == FUSE_BMAP);
194 .WillRepeatedly(Invoke(ReturnErrno(ENOSYS)));
195 } catch (std::system_error err) {
196 FAIL() << err.what();
201 FuseTest::expect_access(uint64_t ino, mode_t access_mode, int error)
203 EXPECT_CALL(*m_mock, process(
204 ResultOf([=](auto in) {
205 return (in.header.opcode == FUSE_ACCESS &&
206 in.header.nodeid == ino &&
207 in.body.access.mask == access_mode);
210 ).WillOnce(Invoke(ReturnErrno(error)));
214 FuseTest::expect_destroy(int error)
216 EXPECT_CALL(*m_mock, process(
217 ResultOf([=](auto in) {
218 return (in.header.opcode == FUSE_DESTROY);
221 ).WillOnce(Invoke( ReturnImmediate([&](auto in, auto& out) {
222 m_mock->m_quit = true;
223 out.header.len = sizeof(out.header);
224 out.header.unique = in.header.unique;
225 out.header.error = -error;
230 FuseTest::expect_flush(uint64_t ino, int times, ProcessMockerT r)
232 EXPECT_CALL(*m_mock, process(
233 ResultOf([=](auto in) {
234 return (in.header.opcode == FUSE_FLUSH &&
235 in.header.nodeid == ino);
239 .WillRepeatedly(Invoke(r));
243 FuseTest::expect_forget(uint64_t ino, uint64_t nlookup, sem_t *sem)
245 EXPECT_CALL(*m_mock, process(
246 ResultOf([=](auto in) {
247 return (in.header.opcode == FUSE_FORGET &&
248 in.header.nodeid == ino &&
249 in.body.forget.nlookup == nlookup);
252 ).WillOnce(Invoke([=](auto in __unused, auto &out __unused) {
255 /* FUSE_FORGET has no response! */
259 void FuseTest::expect_getattr(uint64_t ino, uint64_t size)
261 EXPECT_CALL(*m_mock, process(
262 ResultOf([=](auto in) {
263 return (in.header.opcode == FUSE_GETATTR &&
264 in.header.nodeid == ino);
267 ).WillOnce(Invoke(ReturnImmediate([=](auto i __unused, auto& out) {
268 SET_OUT_HEADER_LEN(out, attr);
269 out.body.attr.attr.ino = ino; // Must match nodeid
270 out.body.attr.attr.mode = S_IFREG | 0644;
271 out.body.attr.attr.size = size;
272 out.body.attr.attr_valid = UINT64_MAX;
276 void FuseTest::expect_lookup(const char *relpath, uint64_t ino, mode_t mode,
277 uint64_t size, int times, uint64_t attr_valid, uid_t uid, gid_t gid)
279 EXPECT_LOOKUP(FUSE_ROOT_ID, relpath)
281 .WillRepeatedly(Invoke(
282 ReturnImmediate([=](auto in __unused, auto& out) {
283 SET_OUT_HEADER_LEN(out, entry);
284 out.body.entry.attr.mode = mode;
285 out.body.entry.nodeid = ino;
286 out.body.entry.attr.nlink = 1;
287 out.body.entry.attr_valid = attr_valid;
288 out.body.entry.attr.size = size;
289 out.body.entry.attr.uid = uid;
290 out.body.entry.attr.gid = gid;
294 void FuseTest::expect_lookup_7_8(const char *relpath, uint64_t ino, mode_t mode,
295 uint64_t size, int times, uint64_t attr_valid, uid_t uid, gid_t gid)
297 EXPECT_LOOKUP(FUSE_ROOT_ID, relpath)
299 .WillRepeatedly(Invoke(
300 ReturnImmediate([=](auto in __unused, auto& out) {
301 SET_OUT_HEADER_LEN(out, entry_7_8);
302 out.body.entry.attr.mode = mode;
303 out.body.entry.nodeid = ino;
304 out.body.entry.attr.nlink = 1;
305 out.body.entry.attr_valid = attr_valid;
306 out.body.entry.attr.size = size;
307 out.body.entry.attr.uid = uid;
308 out.body.entry.attr.gid = gid;
312 void FuseTest::expect_open(uint64_t ino, uint32_t flags, int times)
314 EXPECT_CALL(*m_mock, process(
315 ResultOf([=](auto in) {
316 return (in.header.opcode == FUSE_OPEN &&
317 in.header.nodeid == ino);
321 .WillRepeatedly(Invoke(
322 ReturnImmediate([=](auto in __unused, auto& out) {
323 out.header.len = sizeof(out.header);
324 SET_OUT_HEADER_LEN(out, open);
325 out.body.open.fh = FH;
326 out.body.open.open_flags = flags;
330 void FuseTest::expect_opendir(uint64_t ino)
332 /* opendir(3) calls fstatfs */
333 EXPECT_CALL(*m_mock, process(
334 ResultOf([](auto in) {
335 return (in.header.opcode == FUSE_STATFS);
338 ).WillRepeatedly(Invoke(
339 ReturnImmediate([=](auto i __unused, auto& out) {
340 SET_OUT_HEADER_LEN(out, statfs);
343 EXPECT_CALL(*m_mock, process(
344 ResultOf([=](auto in) {
345 return (in.header.opcode == FUSE_OPENDIR &&
346 in.header.nodeid == ino);
349 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
350 out.header.len = sizeof(out.header);
351 SET_OUT_HEADER_LEN(out, open);
352 out.body.open.fh = FH;
356 void FuseTest::expect_read(uint64_t ino, uint64_t offset, uint64_t isize,
357 uint64_t osize, const void *contents, int flags)
359 EXPECT_CALL(*m_mock, process(
360 ResultOf([=](auto in) {
361 return (in.header.opcode == FUSE_READ &&
362 in.header.nodeid == ino &&
363 in.body.read.fh == FH &&
364 in.body.read.offset == offset &&
365 in.body.read.size == isize &&
367 (in.body.read.flags == O_RDONLY ||
368 in.body.read.flags == O_RDWR)
369 : in.body.read.flags == (uint32_t)flags);
372 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
373 out.header.len = sizeof(struct fuse_out_header) + osize;
374 memmove(out.body.bytes, contents, osize);
375 }))).RetiresOnSaturation();
378 void FuseTest::expect_readdir(uint64_t ino, uint64_t off,
379 std::vector<struct dirent> &ents)
381 EXPECT_CALL(*m_mock, process(
382 ResultOf([=](auto in) {
383 return (in.header.opcode == FUSE_READDIR &&
384 in.header.nodeid == ino &&
385 in.body.readdir.fh == FH &&
386 in.body.readdir.offset == off);
389 ).WillRepeatedly(Invoke(ReturnImmediate([=](auto in, auto& out) {
390 struct fuse_dirent *fde = (struct fuse_dirent*)&(out.body);
393 out.header.error = 0;
396 for (const auto& it: ents) {
397 size_t entlen, entsize;
399 fde->ino = it.d_fileno;
401 fde->type = it.d_type;
402 fde->namelen = it.d_namlen;
403 strncpy(fde->name, it.d_name, it.d_namlen);
404 entlen = FUSE_NAME_OFFSET + fde->namelen;
405 entsize = FUSE_DIRENT_SIZE(fde);
407 * The FUSE protocol does not require zeroing out the
408 * unused portion of the name. But it's a good
409 * practice to prevent information disclosure to the
410 * FUSE client, even though the client is usually the
413 memset(fde->name + fde->namelen, 0, entsize - entlen);
414 if (out.header.len + entsize > in.body.read.size) {
415 printf("Overflow in readdir expectation: i=%d\n"
419 out.header.len += entsize;
420 fde = (struct fuse_dirent*)
421 ((intmax_t*)fde + entsize / sizeof(intmax_t));
424 out.header.len += sizeof(out.header);
428 void FuseTest::expect_release(uint64_t ino, uint64_t fh)
430 EXPECT_CALL(*m_mock, process(
431 ResultOf([=](auto in) {
432 return (in.header.opcode == FUSE_RELEASE &&
433 in.header.nodeid == ino &&
434 in.body.release.fh == fh);
437 ).WillOnce(Invoke(ReturnErrno(0)));
440 void FuseTest::expect_releasedir(uint64_t ino, ProcessMockerT r)
442 EXPECT_CALL(*m_mock, process(
443 ResultOf([=](auto in) {
444 return (in.header.opcode == FUSE_RELEASEDIR &&
445 in.header.nodeid == ino &&
446 in.body.release.fh == FH);
449 ).WillOnce(Invoke(r));
452 void FuseTest::expect_unlink(uint64_t parent, const char *path, int error)
454 EXPECT_CALL(*m_mock, process(
455 ResultOf([=](auto in) {
456 return (in.header.opcode == FUSE_UNLINK &&
457 0 == strcmp(path, in.body.unlink) &&
458 in.header.nodeid == parent);
461 ).WillOnce(Invoke(ReturnErrno(error)));
464 void FuseTest::expect_write(uint64_t ino, uint64_t offset, uint64_t isize,
465 uint64_t osize, uint32_t flags_set, uint32_t flags_unset,
466 const void *contents)
468 EXPECT_CALL(*m_mock, process(
469 ResultOf([=](auto in) {
470 const char *buf = (const char*)in.body.bytes +
471 sizeof(struct fuse_write_in);
473 uint32_t wf = in.body.write.write_flags;
475 if (wf & FUSE_WRITE_CACHE)
478 pid_ok = (pid_t)in.header.pid == getpid();
480 return (in.header.opcode == FUSE_WRITE &&
481 in.header.nodeid == ino &&
482 in.body.write.fh == FH &&
483 in.body.write.offset == offset &&
484 in.body.write.size == isize &&
486 (wf & flags_set) == flags_set &&
487 (wf & flags_unset) == 0 &&
488 (in.body.write.flags == O_WRONLY ||
489 in.body.write.flags == O_RDWR) &&
490 0 == bcmp(buf, contents, isize));
493 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
494 SET_OUT_HEADER_LEN(out, write);
495 out.body.write.size = osize;
499 void FuseTest::expect_write_7_8(uint64_t ino, uint64_t offset, uint64_t isize,
500 uint64_t osize, const void *contents)
502 EXPECT_CALL(*m_mock, process(
503 ResultOf([=](auto in) {
504 const char *buf = (const char*)in.body.bytes +
505 FUSE_COMPAT_WRITE_IN_SIZE;
506 bool pid_ok = (pid_t)in.header.pid == getpid();
507 return (in.header.opcode == FUSE_WRITE &&
508 in.header.nodeid == ino &&
509 in.body.write.fh == FH &&
510 in.body.write.offset == offset &&
511 in.body.write.size == isize &&
513 0 == bcmp(buf, contents, isize));
516 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
517 SET_OUT_HEADER_LEN(out, write);
518 out.body.write.size = osize;
523 get_unprivileged_id(uid_t *uid, gid_t *gid)
529 * First try "tests", Kyua's default unprivileged user. XXX after
530 * GoogleTest gains a proper Kyua wrapper, get this with the Kyua API
532 pw = getpwnam("tests");
534 /* Fall back to "nobody" */
535 pw = getpwnam("nobody");
538 GTEST_SKIP() << "Test requires an unprivileged user";
539 /* Use group "nobody", which is Kyua's default unprivileged group */
540 gr = getgrnam("nobody");
542 GTEST_SKIP() << "Test requires an unprivileged group";
548 FuseTest::fork(bool drop_privs, int *child_status,
549 std::function<void()> parent_func,
550 std::function<int()> child_func)
553 int mprot = PROT_READ | PROT_WRITE;
554 int mflags = MAP_ANON | MAP_SHARED;
560 get_unprivileged_id(&uid, &gid);
565 sem = (sem_t*)mmap(NULL, sizeof(*sem), mprot, mflags, -1, 0);
566 ASSERT_NE(MAP_FAILED, sem) << strerror(errno);
567 ASSERT_EQ(0, sem_init(sem, 1, 0)) << strerror(errno);
569 if ((child = ::fork()) == 0) {
579 if (drop_privs && 0 != setegid(gid)) {
584 if (drop_privs && 0 != setreuid(-1, uid)) {
594 } else if (child > 0) {
596 * In parent. Cleanup must happen here, because it's still
599 m_mock->m_child_pid = child;
600 ASSERT_NO_FATAL_FAILURE(parent_func());
602 /* Signal the child process to go */
603 ASSERT_EQ(0, sem_post(sem)) << strerror(errno);
605 ASSERT_LE(0, wait(child_status)) << strerror(errno);
607 FAIL() << strerror(errno);
609 munmap(sem, sizeof(*sem));
613 static void usage(char* progname) {
614 fprintf(stderr, "Usage: %s [-v]\n\t-v increase verbosity\n", progname);
618 int main(int argc, char **argv) {
620 FuseEnv *fuse_env = new FuseEnv;
622 InitGoogleTest(&argc, argv);
623 AddGlobalTestEnvironment(fuse_env);
625 while ((ch = getopt(argc, argv, "v")) != -1) {
636 return (RUN_ALL_TESTS());