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 *node = "vfs.maxbcachebuf";
98 size_t size = sizeof(val);
101 * XXX check_environment should be called from FuseEnv::SetUp, but
102 * can't due to https://github.com/google/googletest/issues/2189
108 ASSERT_EQ(0, sysctlbyname(node, &val, &size, NULL, 0))
110 m_maxbcachebuf = val;
113 m_mock = new MockFS(m_maxreadahead, m_allow_other,
114 m_default_permissions, m_push_symlinks_in, m_ro,
115 m_pm, m_init_flags, m_kernel_minor_version,
116 m_maxwrite, m_async, m_noclusterr);
118 * FUSE_ACCESS is called almost universally. Expecting it in
119 * each test case would be super-annoying. Instead, set a
120 * default expectation for FUSE_ACCESS and return ENOSYS.
122 * Individual test cases can override this expectation since
123 * googlemock evaluates expectations in LIFO order.
125 EXPECT_CALL(*m_mock, process(
126 ResultOf([=](auto in) {
127 return (in.header.opcode == FUSE_ACCESS);
131 .WillRepeatedly(Invoke(ReturnErrno(ENOSYS)));
132 } catch (std::system_error err) {
133 FAIL() << err.what();
138 FuseTest::expect_access(uint64_t ino, mode_t access_mode, int error)
140 EXPECT_CALL(*m_mock, process(
141 ResultOf([=](auto in) {
142 return (in.header.opcode == FUSE_ACCESS &&
143 in.header.nodeid == ino &&
144 in.body.access.mask == access_mode);
147 ).WillOnce(Invoke(ReturnErrno(error)));
151 FuseTest::expect_destroy(int error)
153 EXPECT_CALL(*m_mock, process(
154 ResultOf([=](auto in) {
155 return (in.header.opcode == FUSE_DESTROY);
158 ).WillOnce(Invoke( ReturnImmediate([&](auto in, auto& out) {
159 m_mock->m_quit = true;
160 out.header.len = sizeof(out.header);
161 out.header.unique = in.header.unique;
162 out.header.error = -error;
167 FuseTest::expect_flush(uint64_t ino, int times, ProcessMockerT r)
169 EXPECT_CALL(*m_mock, process(
170 ResultOf([=](auto in) {
171 return (in.header.opcode == FUSE_FLUSH &&
172 in.header.nodeid == ino);
176 .WillRepeatedly(Invoke(r));
180 FuseTest::expect_forget(uint64_t ino, uint64_t nlookup, sem_t *sem)
182 EXPECT_CALL(*m_mock, process(
183 ResultOf([=](auto in) {
184 return (in.header.opcode == FUSE_FORGET &&
185 in.header.nodeid == ino &&
186 in.body.forget.nlookup == nlookup);
189 ).WillOnce(Invoke([=](auto in __unused, auto &out __unused) {
192 /* FUSE_FORGET has no response! */
196 void FuseTest::expect_getattr(uint64_t ino, uint64_t size)
198 EXPECT_CALL(*m_mock, process(
199 ResultOf([=](auto in) {
200 return (in.header.opcode == FUSE_GETATTR &&
201 in.header.nodeid == ino);
204 ).WillOnce(Invoke(ReturnImmediate([=](auto i __unused, auto& out) {
205 SET_OUT_HEADER_LEN(out, attr);
206 out.body.attr.attr.ino = ino; // Must match nodeid
207 out.body.attr.attr.mode = S_IFREG | 0644;
208 out.body.attr.attr.size = size;
209 out.body.attr.attr_valid = UINT64_MAX;
213 void FuseTest::expect_lookup(const char *relpath, uint64_t ino, mode_t mode,
214 uint64_t size, int times, uint64_t attr_valid, uid_t uid, gid_t gid)
216 EXPECT_LOOKUP(FUSE_ROOT_ID, relpath)
218 .WillRepeatedly(Invoke(
219 ReturnImmediate([=](auto in __unused, auto& out) {
220 SET_OUT_HEADER_LEN(out, entry);
221 out.body.entry.attr.mode = mode;
222 out.body.entry.nodeid = ino;
223 out.body.entry.attr.nlink = 1;
224 out.body.entry.attr_valid = attr_valid;
225 out.body.entry.attr.size = size;
226 out.body.entry.attr.uid = uid;
227 out.body.entry.attr.gid = gid;
231 void FuseTest::expect_lookup_7_8(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_7_8);
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_open(uint64_t ino, uint32_t flags, int times)
251 EXPECT_CALL(*m_mock, process(
252 ResultOf([=](auto in) {
253 return (in.header.opcode == FUSE_OPEN &&
254 in.header.nodeid == ino);
258 .WillRepeatedly(Invoke(
259 ReturnImmediate([=](auto in __unused, auto& out) {
260 out.header.len = sizeof(out.header);
261 SET_OUT_HEADER_LEN(out, open);
262 out.body.open.fh = FH;
263 out.body.open.open_flags = flags;
267 void FuseTest::expect_opendir(uint64_t ino)
269 /* opendir(3) calls fstatfs */
270 EXPECT_CALL(*m_mock, process(
271 ResultOf([](auto in) {
272 return (in.header.opcode == FUSE_STATFS);
275 ).WillRepeatedly(Invoke(
276 ReturnImmediate([=](auto i __unused, auto& out) {
277 SET_OUT_HEADER_LEN(out, statfs);
280 EXPECT_CALL(*m_mock, process(
281 ResultOf([=](auto in) {
282 return (in.header.opcode == FUSE_OPENDIR &&
283 in.header.nodeid == ino);
286 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
287 out.header.len = sizeof(out.header);
288 SET_OUT_HEADER_LEN(out, open);
289 out.body.open.fh = FH;
293 void FuseTest::expect_read(uint64_t ino, uint64_t offset, uint64_t isize,
294 uint64_t osize, const void *contents, int flags)
296 EXPECT_CALL(*m_mock, process(
297 ResultOf([=](auto in) {
298 return (in.header.opcode == FUSE_READ &&
299 in.header.nodeid == ino &&
300 in.body.read.fh == FH &&
301 in.body.read.offset == offset &&
302 in.body.read.size == isize &&
304 (in.body.read.flags == O_RDONLY ||
305 in.body.read.flags == O_RDWR)
306 : in.body.read.flags == (uint32_t)flags);
309 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
310 out.header.len = sizeof(struct fuse_out_header) + osize;
311 memmove(out.body.bytes, contents, osize);
312 }))).RetiresOnSaturation();
315 void FuseTest::expect_readdir(uint64_t ino, uint64_t off,
316 std::vector<struct dirent> &ents)
318 EXPECT_CALL(*m_mock, process(
319 ResultOf([=](auto in) {
320 return (in.header.opcode == FUSE_READDIR &&
321 in.header.nodeid == ino &&
322 in.body.readdir.fh == FH &&
323 in.body.readdir.offset == off);
326 ).WillRepeatedly(Invoke(ReturnImmediate([=](auto in, auto& out) {
327 struct fuse_dirent *fde = (struct fuse_dirent*)&(out.body);
330 out.header.error = 0;
333 for (const auto& it: ents) {
334 size_t entlen, entsize;
336 fde->ino = it.d_fileno;
338 fde->type = it.d_type;
339 fde->namelen = it.d_namlen;
340 strncpy(fde->name, it.d_name, it.d_namlen);
341 entlen = FUSE_NAME_OFFSET + fde->namelen;
342 entsize = FUSE_DIRENT_SIZE(fde);
344 * The FUSE protocol does not require zeroing out the
345 * unused portion of the name. But it's a good
346 * practice to prevent information disclosure to the
347 * FUSE client, even though the client is usually the
350 memset(fde->name + fde->namelen, 0, entsize - entlen);
351 if (out.header.len + entsize > in.body.read.size) {
352 printf("Overflow in readdir expectation: i=%d\n"
356 out.header.len += entsize;
357 fde = (struct fuse_dirent*)
358 ((intmax_t*)fde + entsize / sizeof(intmax_t));
361 out.header.len += sizeof(out.header);
365 void FuseTest::expect_release(uint64_t ino, uint64_t fh)
367 EXPECT_CALL(*m_mock, process(
368 ResultOf([=](auto in) {
369 return (in.header.opcode == FUSE_RELEASE &&
370 in.header.nodeid == ino &&
371 in.body.release.fh == fh);
374 ).WillOnce(Invoke(ReturnErrno(0)));
377 void FuseTest::expect_releasedir(uint64_t ino, ProcessMockerT r)
379 EXPECT_CALL(*m_mock, process(
380 ResultOf([=](auto in) {
381 return (in.header.opcode == FUSE_RELEASEDIR &&
382 in.header.nodeid == ino &&
383 in.body.release.fh == FH);
386 ).WillOnce(Invoke(r));
389 void FuseTest::expect_unlink(uint64_t parent, const char *path, int error)
391 EXPECT_CALL(*m_mock, process(
392 ResultOf([=](auto in) {
393 return (in.header.opcode == FUSE_UNLINK &&
394 0 == strcmp(path, in.body.unlink) &&
395 in.header.nodeid == parent);
398 ).WillOnce(Invoke(ReturnErrno(error)));
401 void FuseTest::expect_write(uint64_t ino, uint64_t offset, uint64_t isize,
402 uint64_t osize, uint32_t flags_set, uint32_t flags_unset,
403 const void *contents)
405 EXPECT_CALL(*m_mock, process(
406 ResultOf([=](auto in) {
407 const char *buf = (const char*)in.body.bytes +
408 sizeof(struct fuse_write_in);
410 uint32_t wf = in.body.write.write_flags;
412 if (wf & FUSE_WRITE_CACHE)
415 pid_ok = (pid_t)in.header.pid == getpid();
417 return (in.header.opcode == FUSE_WRITE &&
418 in.header.nodeid == ino &&
419 in.body.write.fh == FH &&
420 in.body.write.offset == offset &&
421 in.body.write.size == isize &&
423 (wf & flags_set) == flags_set &&
424 (wf & flags_unset) == 0 &&
425 (in.body.write.flags == O_WRONLY ||
426 in.body.write.flags == O_RDWR) &&
427 0 == bcmp(buf, contents, isize));
430 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
431 SET_OUT_HEADER_LEN(out, write);
432 out.body.write.size = osize;
436 void FuseTest::expect_write_7_8(uint64_t ino, uint64_t offset, uint64_t isize,
437 uint64_t osize, const void *contents)
439 EXPECT_CALL(*m_mock, process(
440 ResultOf([=](auto in) {
441 const char *buf = (const char*)in.body.bytes +
442 FUSE_COMPAT_WRITE_IN_SIZE;
443 bool pid_ok = (pid_t)in.header.pid == getpid();
444 return (in.header.opcode == FUSE_WRITE &&
445 in.header.nodeid == ino &&
446 in.body.write.fh == FH &&
447 in.body.write.offset == offset &&
448 in.body.write.size == isize &&
450 0 == bcmp(buf, contents, isize));
453 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
454 SET_OUT_HEADER_LEN(out, write);
455 out.body.write.size = osize;
460 get_unprivileged_id(uid_t *uid, gid_t *gid)
466 * First try "tests", Kyua's default unprivileged user. XXX after
467 * GoogleTest gains a proper Kyua wrapper, get this with the Kyua API
469 pw = getpwnam("tests");
471 /* Fall back to "nobody" */
472 pw = getpwnam("nobody");
475 GTEST_SKIP() << "Test requires an unprivileged user";
476 /* Use group "nobody", which is Kyua's default unprivileged group */
477 gr = getgrnam("nobody");
479 GTEST_SKIP() << "Test requires an unprivileged group";
485 FuseTest::fork(bool drop_privs, int *child_status,
486 std::function<void()> parent_func,
487 std::function<int()> child_func)
490 int mprot = PROT_READ | PROT_WRITE;
491 int mflags = MAP_ANON | MAP_SHARED;
497 get_unprivileged_id(&uid, &gid);
502 sem = (sem_t*)mmap(NULL, sizeof(*sem), mprot, mflags, -1, 0);
503 ASSERT_NE(MAP_FAILED, sem) << strerror(errno);
504 ASSERT_EQ(0, sem_init(sem, 1, 0)) << strerror(errno);
506 if ((child = ::fork()) == 0) {
516 if (drop_privs && 0 != setegid(gid)) {
521 if (drop_privs && 0 != setreuid(-1, uid)) {
531 } else if (child > 0) {
533 * In parent. Cleanup must happen here, because it's still
536 m_mock->m_child_pid = child;
537 ASSERT_NO_FATAL_FAILURE(parent_func());
539 /* Signal the child process to go */
540 ASSERT_EQ(0, sem_post(sem)) << strerror(errno);
542 ASSERT_LE(0, wait(child_status)) << strerror(errno);
544 FAIL() << strerror(errno);
546 munmap(sem, sizeof(*sem));
550 static void usage(char* progname) {
551 fprintf(stderr, "Usage: %s [-v]\n\t-v increase verbosity\n", progname);
555 int main(int argc, char **argv) {
557 FuseEnv *fuse_env = new FuseEnv;
559 InitGoogleTest(&argc, argv);
560 AddGlobalTestEnvironment(fuse_env);
562 while ((ch = getopt(argc, argv, "v")) != -1) {
573 return (RUN_ALL_TESTS());