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);
122 * FUSE_ACCESS is called almost universally. Expecting it in
123 * each test case would be super-annoying. Instead, set a
124 * default expectation for FUSE_ACCESS and return ENOSYS.
126 * Individual test cases can override this expectation since
127 * googlemock evaluates expectations in LIFO order.
129 EXPECT_CALL(*m_mock, process(
130 ResultOf([=](auto in) {
131 return (in.header.opcode == FUSE_ACCESS);
135 .WillRepeatedly(Invoke(ReturnErrno(ENOSYS)));
137 * FUSE_BMAP is called for most test cases that read data. Set
138 * a default expectation and return ENOSYS.
140 * Individual test cases can override this expectation since
141 * googlemock evaluates expectations in LIFO order.
143 EXPECT_CALL(*m_mock, process(
144 ResultOf([=](auto in) {
145 return (in.header.opcode == FUSE_BMAP);
149 .WillRepeatedly(Invoke(ReturnErrno(ENOSYS)));
150 } catch (std::system_error err) {
151 FAIL() << err.what();
156 FuseTest::expect_access(uint64_t ino, mode_t access_mode, int error)
158 EXPECT_CALL(*m_mock, process(
159 ResultOf([=](auto in) {
160 return (in.header.opcode == FUSE_ACCESS &&
161 in.header.nodeid == ino &&
162 in.body.access.mask == access_mode);
165 ).WillOnce(Invoke(ReturnErrno(error)));
169 FuseTest::expect_destroy(int error)
171 EXPECT_CALL(*m_mock, process(
172 ResultOf([=](auto in) {
173 return (in.header.opcode == FUSE_DESTROY);
176 ).WillOnce(Invoke( ReturnImmediate([&](auto in, auto& out) {
177 m_mock->m_quit = true;
178 out.header.len = sizeof(out.header);
179 out.header.unique = in.header.unique;
180 out.header.error = -error;
185 FuseTest::expect_flush(uint64_t ino, int times, ProcessMockerT r)
187 EXPECT_CALL(*m_mock, process(
188 ResultOf([=](auto in) {
189 return (in.header.opcode == FUSE_FLUSH &&
190 in.header.nodeid == ino);
194 .WillRepeatedly(Invoke(r));
198 FuseTest::expect_forget(uint64_t ino, uint64_t nlookup, sem_t *sem)
200 EXPECT_CALL(*m_mock, process(
201 ResultOf([=](auto in) {
202 return (in.header.opcode == FUSE_FORGET &&
203 in.header.nodeid == ino &&
204 in.body.forget.nlookup == nlookup);
207 ).WillOnce(Invoke([=](auto in __unused, auto &out __unused) {
210 /* FUSE_FORGET has no response! */
214 void FuseTest::expect_getattr(uint64_t ino, uint64_t size)
216 EXPECT_CALL(*m_mock, process(
217 ResultOf([=](auto in) {
218 return (in.header.opcode == FUSE_GETATTR &&
219 in.header.nodeid == ino);
222 ).WillOnce(Invoke(ReturnImmediate([=](auto i __unused, auto& out) {
223 SET_OUT_HEADER_LEN(out, attr);
224 out.body.attr.attr.ino = ino; // Must match nodeid
225 out.body.attr.attr.mode = S_IFREG | 0644;
226 out.body.attr.attr.size = size;
227 out.body.attr.attr_valid = UINT64_MAX;
231 void FuseTest::expect_lookup(const char *relpath, uint64_t ino, mode_t mode,
232 uint64_t size, int times, uint64_t attr_valid, uid_t uid, gid_t gid)
234 EXPECT_LOOKUP(FUSE_ROOT_ID, relpath)
236 .WillRepeatedly(Invoke(
237 ReturnImmediate([=](auto in __unused, auto& out) {
238 SET_OUT_HEADER_LEN(out, entry);
239 out.body.entry.attr.mode = mode;
240 out.body.entry.nodeid = ino;
241 out.body.entry.attr.nlink = 1;
242 out.body.entry.attr_valid = attr_valid;
243 out.body.entry.attr.size = size;
244 out.body.entry.attr.uid = uid;
245 out.body.entry.attr.gid = gid;
249 void FuseTest::expect_lookup_7_8(const char *relpath, uint64_t ino, mode_t mode,
250 uint64_t size, int times, uint64_t attr_valid, uid_t uid, gid_t gid)
252 EXPECT_LOOKUP(FUSE_ROOT_ID, relpath)
254 .WillRepeatedly(Invoke(
255 ReturnImmediate([=](auto in __unused, auto& out) {
256 SET_OUT_HEADER_LEN(out, entry_7_8);
257 out.body.entry.attr.mode = mode;
258 out.body.entry.nodeid = ino;
259 out.body.entry.attr.nlink = 1;
260 out.body.entry.attr_valid = attr_valid;
261 out.body.entry.attr.size = size;
262 out.body.entry.attr.uid = uid;
263 out.body.entry.attr.gid = gid;
267 void FuseTest::expect_open(uint64_t ino, uint32_t flags, int times)
269 EXPECT_CALL(*m_mock, process(
270 ResultOf([=](auto in) {
271 return (in.header.opcode == FUSE_OPEN &&
272 in.header.nodeid == ino);
276 .WillRepeatedly(Invoke(
277 ReturnImmediate([=](auto in __unused, auto& out) {
278 out.header.len = sizeof(out.header);
279 SET_OUT_HEADER_LEN(out, open);
280 out.body.open.fh = FH;
281 out.body.open.open_flags = flags;
285 void FuseTest::expect_opendir(uint64_t ino)
287 /* opendir(3) calls fstatfs */
288 EXPECT_CALL(*m_mock, process(
289 ResultOf([](auto in) {
290 return (in.header.opcode == FUSE_STATFS);
293 ).WillRepeatedly(Invoke(
294 ReturnImmediate([=](auto i __unused, auto& out) {
295 SET_OUT_HEADER_LEN(out, statfs);
298 EXPECT_CALL(*m_mock, process(
299 ResultOf([=](auto in) {
300 return (in.header.opcode == FUSE_OPENDIR &&
301 in.header.nodeid == ino);
304 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
305 out.header.len = sizeof(out.header);
306 SET_OUT_HEADER_LEN(out, open);
307 out.body.open.fh = FH;
311 void FuseTest::expect_read(uint64_t ino, uint64_t offset, uint64_t isize,
312 uint64_t osize, const void *contents, int flags)
314 EXPECT_CALL(*m_mock, process(
315 ResultOf([=](auto in) {
316 return (in.header.opcode == FUSE_READ &&
317 in.header.nodeid == ino &&
318 in.body.read.fh == FH &&
319 in.body.read.offset == offset &&
320 in.body.read.size == isize &&
322 (in.body.read.flags == O_RDONLY ||
323 in.body.read.flags == O_RDWR)
324 : in.body.read.flags == (uint32_t)flags);
327 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
328 out.header.len = sizeof(struct fuse_out_header) + osize;
329 memmove(out.body.bytes, contents, osize);
330 }))).RetiresOnSaturation();
333 void FuseTest::expect_readdir(uint64_t ino, uint64_t off,
334 std::vector<struct dirent> &ents)
336 EXPECT_CALL(*m_mock, process(
337 ResultOf([=](auto in) {
338 return (in.header.opcode == FUSE_READDIR &&
339 in.header.nodeid == ino &&
340 in.body.readdir.fh == FH &&
341 in.body.readdir.offset == off);
344 ).WillRepeatedly(Invoke(ReturnImmediate([=](auto in, auto& out) {
345 struct fuse_dirent *fde = (struct fuse_dirent*)&(out.body);
348 out.header.error = 0;
351 for (const auto& it: ents) {
352 size_t entlen, entsize;
354 fde->ino = it.d_fileno;
356 fde->type = it.d_type;
357 fde->namelen = it.d_namlen;
358 strncpy(fde->name, it.d_name, it.d_namlen);
359 entlen = FUSE_NAME_OFFSET + fde->namelen;
360 entsize = FUSE_DIRENT_SIZE(fde);
362 * The FUSE protocol does not require zeroing out the
363 * unused portion of the name. But it's a good
364 * practice to prevent information disclosure to the
365 * FUSE client, even though the client is usually the
368 memset(fde->name + fde->namelen, 0, entsize - entlen);
369 if (out.header.len + entsize > in.body.read.size) {
370 printf("Overflow in readdir expectation: i=%d\n"
374 out.header.len += entsize;
375 fde = (struct fuse_dirent*)
376 ((intmax_t*)fde + entsize / sizeof(intmax_t));
379 out.header.len += sizeof(out.header);
383 void FuseTest::expect_release(uint64_t ino, uint64_t fh)
385 EXPECT_CALL(*m_mock, process(
386 ResultOf([=](auto in) {
387 return (in.header.opcode == FUSE_RELEASE &&
388 in.header.nodeid == ino &&
389 in.body.release.fh == fh);
392 ).WillOnce(Invoke(ReturnErrno(0)));
395 void FuseTest::expect_releasedir(uint64_t ino, ProcessMockerT r)
397 EXPECT_CALL(*m_mock, process(
398 ResultOf([=](auto in) {
399 return (in.header.opcode == FUSE_RELEASEDIR &&
400 in.header.nodeid == ino &&
401 in.body.release.fh == FH);
404 ).WillOnce(Invoke(r));
407 void FuseTest::expect_unlink(uint64_t parent, const char *path, int error)
409 EXPECT_CALL(*m_mock, process(
410 ResultOf([=](auto in) {
411 return (in.header.opcode == FUSE_UNLINK &&
412 0 == strcmp(path, in.body.unlink) &&
413 in.header.nodeid == parent);
416 ).WillOnce(Invoke(ReturnErrno(error)));
419 void FuseTest::expect_write(uint64_t ino, uint64_t offset, uint64_t isize,
420 uint64_t osize, uint32_t flags_set, uint32_t flags_unset,
421 const void *contents)
423 EXPECT_CALL(*m_mock, process(
424 ResultOf([=](auto in) {
425 const char *buf = (const char*)in.body.bytes +
426 sizeof(struct fuse_write_in);
428 uint32_t wf = in.body.write.write_flags;
430 if (wf & FUSE_WRITE_CACHE)
433 pid_ok = (pid_t)in.header.pid == getpid();
435 return (in.header.opcode == FUSE_WRITE &&
436 in.header.nodeid == ino &&
437 in.body.write.fh == FH &&
438 in.body.write.offset == offset &&
439 in.body.write.size == isize &&
441 (wf & flags_set) == flags_set &&
442 (wf & flags_unset) == 0 &&
443 (in.body.write.flags == O_WRONLY ||
444 in.body.write.flags == O_RDWR) &&
445 0 == bcmp(buf, contents, isize));
448 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
449 SET_OUT_HEADER_LEN(out, write);
450 out.body.write.size = osize;
454 void FuseTest::expect_write_7_8(uint64_t ino, uint64_t offset, uint64_t isize,
455 uint64_t osize, const void *contents)
457 EXPECT_CALL(*m_mock, process(
458 ResultOf([=](auto in) {
459 const char *buf = (const char*)in.body.bytes +
460 FUSE_COMPAT_WRITE_IN_SIZE;
461 bool pid_ok = (pid_t)in.header.pid == getpid();
462 return (in.header.opcode == FUSE_WRITE &&
463 in.header.nodeid == ino &&
464 in.body.write.fh == FH &&
465 in.body.write.offset == offset &&
466 in.body.write.size == isize &&
468 0 == bcmp(buf, contents, isize));
471 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
472 SET_OUT_HEADER_LEN(out, write);
473 out.body.write.size = osize;
478 get_unprivileged_id(uid_t *uid, gid_t *gid)
484 * First try "tests", Kyua's default unprivileged user. XXX after
485 * GoogleTest gains a proper Kyua wrapper, get this with the Kyua API
487 pw = getpwnam("tests");
489 /* Fall back to "nobody" */
490 pw = getpwnam("nobody");
493 GTEST_SKIP() << "Test requires an unprivileged user";
494 /* Use group "nobody", which is Kyua's default unprivileged group */
495 gr = getgrnam("nobody");
497 GTEST_SKIP() << "Test requires an unprivileged group";
503 FuseTest::fork(bool drop_privs, int *child_status,
504 std::function<void()> parent_func,
505 std::function<int()> child_func)
508 int mprot = PROT_READ | PROT_WRITE;
509 int mflags = MAP_ANON | MAP_SHARED;
515 get_unprivileged_id(&uid, &gid);
520 sem = (sem_t*)mmap(NULL, sizeof(*sem), mprot, mflags, -1, 0);
521 ASSERT_NE(MAP_FAILED, sem) << strerror(errno);
522 ASSERT_EQ(0, sem_init(sem, 1, 0)) << strerror(errno);
524 if ((child = ::fork()) == 0) {
534 if (drop_privs && 0 != setegid(gid)) {
539 if (drop_privs && 0 != setreuid(-1, uid)) {
549 } else if (child > 0) {
551 * In parent. Cleanup must happen here, because it's still
554 m_mock->m_child_pid = child;
555 ASSERT_NO_FATAL_FAILURE(parent_func());
557 /* Signal the child process to go */
558 ASSERT_EQ(0, sem_post(sem)) << strerror(errno);
560 ASSERT_LE(0, wait(child_status)) << strerror(errno);
562 FAIL() << strerror(errno);
564 munmap(sem, sizeof(*sem));
568 static void usage(char* progname) {
569 fprintf(stderr, "Usage: %s [-v]\n\t-v increase verbosity\n", progname);
573 int main(int argc, char **argv) {
575 FuseEnv *fuse_env = new FuseEnv;
577 InitGoogleTest(&argc, argv);
578 AddGlobalTestEnvironment(fuse_env);
580 while ((ch = getopt(argc, argv, "v")) != -1) {
591 return (RUN_ALL_TESTS());