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;
53 /* Check that fusefs(4) is accessible and the current user can mount(2) */
54 void check_environment()
56 const char *devnode = "/dev/fuse";
57 const char *usermount_node = "vfs.usermount";
58 int usermount_val = 0;
59 size_t usermount_size = sizeof(usermount_val);
60 if (eaccess(devnode, R_OK | W_OK)) {
61 if (errno == ENOENT) {
62 GTEST_SKIP() << devnode << " does not exist";
63 } else if (errno == EACCES) {
64 GTEST_SKIP() << devnode <<
65 " is not accessible by the current user";
67 GTEST_SKIP() << strerror(errno);
70 sysctlbyname(usermount_node, &usermount_val, &usermount_size,
72 if (geteuid() != 0 && !usermount_val)
73 GTEST_SKIP() << "current user is not allowed to mount";
76 class FuseEnv: public Environment {
77 virtual void SetUp() {
81 void FuseTest::SetUp() {
82 const char *node = "vfs.maxbcachebuf";
84 size_t size = sizeof(val);
87 * XXX check_environment should be called from FuseEnv::SetUp, but
88 * can't due to https://github.com/google/googletest/issues/2189
94 ASSERT_EQ(0, sysctlbyname(node, &val, &size, NULL, 0))
99 m_mock = new MockFS(m_maxreadahead, m_allow_other,
100 m_default_permissions, m_push_symlinks_in, m_ro,
101 m_pm, m_init_flags, m_kernel_minor_version);
103 * FUSE_ACCESS is called almost universally. Expecting it in
104 * each test case would be super-annoying. Instead, set a
105 * default expectation for FUSE_ACCESS and return ENOSYS.
107 * Individual test cases can override this expectation since
108 * googlemock evaluates expectations in LIFO order.
110 EXPECT_CALL(*m_mock, process(
111 ResultOf([=](auto in) {
112 return (in.header.opcode == FUSE_ACCESS);
116 .WillRepeatedly(Invoke(ReturnErrno(ENOSYS)));
117 } catch (std::system_error err) {
118 FAIL() << err.what();
123 FuseTest::expect_access(uint64_t ino, mode_t access_mode, int error)
125 EXPECT_CALL(*m_mock, process(
126 ResultOf([=](auto in) {
127 return (in.header.opcode == FUSE_ACCESS &&
128 in.header.nodeid == ino &&
129 in.body.access.mask == access_mode);
132 ).WillOnce(Invoke(ReturnErrno(error)));
136 FuseTest::expect_destroy(int error)
138 EXPECT_CALL(*m_mock, process(
139 ResultOf([=](auto in) {
140 return (in.header.opcode == FUSE_DESTROY);
143 ).WillOnce(Invoke( ReturnImmediate([&](auto in, auto& out) {
144 m_mock->m_quit = true;
145 out.header.len = sizeof(out.header);
146 out.header.unique = in.header.unique;
147 out.header.error = -error;
152 FuseTest::expect_flush(uint64_t ino, int times, ProcessMockerT r)
154 EXPECT_CALL(*m_mock, process(
155 ResultOf([=](auto in) {
156 return (in.header.opcode == FUSE_FLUSH &&
157 in.header.nodeid == ino);
161 .WillRepeatedly(Invoke(r));
165 FuseTest::expect_forget(uint64_t ino, uint64_t nlookup)
167 EXPECT_CALL(*m_mock, process(
168 ResultOf([=](auto in) {
169 return (in.header.opcode == FUSE_FORGET &&
170 in.header.nodeid == ino &&
171 in.body.forget.nlookup == nlookup);
174 ).WillOnce(Invoke([](auto in __unused, auto &out __unused) {
175 /* FUSE_FORGET has no response! */
179 void FuseTest::expect_getattr(uint64_t ino, uint64_t size)
181 EXPECT_CALL(*m_mock, process(
182 ResultOf([=](auto in) {
183 return (in.header.opcode == FUSE_GETATTR &&
184 in.header.nodeid == ino);
187 ).WillOnce(Invoke(ReturnImmediate([=](auto i __unused, auto& out) {
188 SET_OUT_HEADER_LEN(out, attr);
189 out.body.attr.attr.ino = ino; // Must match nodeid
190 out.body.attr.attr.mode = S_IFREG | 0644;
191 out.body.attr.attr.size = size;
192 out.body.attr.attr_valid = UINT64_MAX;
196 void FuseTest::expect_lookup(const char *relpath, uint64_t ino, mode_t mode,
197 uint64_t size, int times, uint64_t attr_valid, uid_t uid, gid_t gid)
199 EXPECT_LOOKUP(1, relpath)
201 .WillRepeatedly(Invoke(
202 ReturnImmediate([=](auto in __unused, auto& out) {
203 SET_OUT_HEADER_LEN(out, entry);
204 out.body.entry.attr.mode = mode;
205 out.body.entry.nodeid = ino;
206 out.body.entry.attr.nlink = 1;
207 out.body.entry.attr_valid = attr_valid;
208 out.body.entry.attr.size = size;
209 out.body.entry.attr.uid = uid;
210 out.body.entry.attr.gid = gid;
214 void FuseTest::expect_lookup_7_8(const char *relpath, uint64_t ino, mode_t mode,
215 uint64_t size, int times, uint64_t attr_valid, uid_t uid, gid_t gid)
217 EXPECT_LOOKUP(1, relpath)
219 .WillRepeatedly(Invoke(
220 ReturnImmediate([=](auto in __unused, auto& out) {
221 SET_OUT_HEADER_LEN(out, entry_7_8);
222 out.body.entry.attr.mode = mode;
223 out.body.entry.nodeid = ino;
224 out.body.entry.attr.nlink = 1;
225 out.body.entry.attr_valid = attr_valid;
226 out.body.entry.attr.size = size;
227 out.body.entry.attr.uid = uid;
228 out.body.entry.attr.gid = gid;
232 void FuseTest::expect_open(uint64_t ino, uint32_t flags, int times)
234 EXPECT_CALL(*m_mock, process(
235 ResultOf([=](auto in) {
236 return (in.header.opcode == FUSE_OPEN &&
237 in.header.nodeid == ino);
241 .WillRepeatedly(Invoke(
242 ReturnImmediate([=](auto in __unused, auto& out) {
243 out.header.len = sizeof(out.header);
244 SET_OUT_HEADER_LEN(out, open);
245 out.body.open.fh = FH;
246 out.body.open.open_flags = flags;
250 void FuseTest::expect_opendir(uint64_t ino)
252 /* opendir(3) calls fstatfs */
253 EXPECT_CALL(*m_mock, process(
254 ResultOf([](auto in) {
255 return (in.header.opcode == FUSE_STATFS);
258 ).WillRepeatedly(Invoke(
259 ReturnImmediate([=](auto i __unused, auto& out) {
260 SET_OUT_HEADER_LEN(out, statfs);
263 EXPECT_CALL(*m_mock, process(
264 ResultOf([=](auto in) {
265 return (in.header.opcode == FUSE_OPENDIR &&
266 in.header.nodeid == ino);
269 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
270 out.header.len = sizeof(out.header);
271 SET_OUT_HEADER_LEN(out, open);
272 out.body.open.fh = FH;
276 void FuseTest::expect_read(uint64_t ino, uint64_t offset, uint64_t isize,
277 uint64_t osize, const void *contents, int flags)
279 EXPECT_CALL(*m_mock, process(
280 ResultOf([=](auto in) {
281 return (in.header.opcode == FUSE_READ &&
282 in.header.nodeid == ino &&
283 in.body.read.fh == FH &&
284 in.body.read.offset == offset &&
285 in.body.read.size == isize &&
287 (in.body.read.flags == O_RDONLY ||
288 in.body.read.flags == O_RDWR)
289 : in.body.read.flags == (uint32_t)flags);
292 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
293 out.header.len = sizeof(struct fuse_out_header) + osize;
294 memmove(out.body.bytes, contents, osize);
295 }))).RetiresOnSaturation();
298 void FuseTest::expect_readdir(uint64_t ino, uint64_t off,
299 std::vector<struct dirent> &ents)
301 EXPECT_CALL(*m_mock, process(
302 ResultOf([=](auto in) {
303 return (in.header.opcode == FUSE_READDIR &&
304 in.header.nodeid == ino &&
305 in.body.readdir.fh == FH &&
306 in.body.readdir.offset == off);
309 ).WillRepeatedly(Invoke(ReturnImmediate([=](auto in, auto& out) {
310 struct fuse_dirent *fde = (struct fuse_dirent*)&(out.body);
313 out.header.error = 0;
316 for (const auto& it: ents) {
317 size_t entlen, entsize;
319 fde->ino = it.d_fileno;
321 fde->type = it.d_type;
322 fde->namelen = it.d_namlen;
323 strncpy(fde->name, it.d_name, it.d_namlen);
324 entlen = FUSE_NAME_OFFSET + fde->namelen;
325 entsize = FUSE_DIRENT_SIZE(fde);
327 * The FUSE protocol does not require zeroing out the
328 * unused portion of the name. But it's a good
329 * practice to prevent information disclosure to the
330 * FUSE client, even though the client is usually the
333 memset(fde->name + fde->namelen, 0, entsize - entlen);
334 if (out.header.len + entsize > in.body.read.size) {
335 printf("Overflow in readdir expectation: i=%d\n"
339 out.header.len += entsize;
340 fde = (struct fuse_dirent*)
341 ((intmax_t*)fde + entsize / sizeof(intmax_t));
344 out.header.len += sizeof(out.header);
348 void FuseTest::expect_release(uint64_t ino, uint64_t fh)
350 EXPECT_CALL(*m_mock, process(
351 ResultOf([=](auto in) {
352 return (in.header.opcode == FUSE_RELEASE &&
353 in.header.nodeid == ino &&
354 in.body.release.fh == fh);
357 ).WillOnce(Invoke(ReturnErrno(0)));
360 void FuseTest::expect_releasedir(uint64_t ino, ProcessMockerT r)
362 EXPECT_CALL(*m_mock, process(
363 ResultOf([=](auto in) {
364 return (in.header.opcode == FUSE_RELEASEDIR &&
365 in.header.nodeid == ino &&
366 in.body.release.fh == FH);
369 ).WillOnce(Invoke(r));
372 void FuseTest::expect_unlink(uint64_t parent, const char *path, int error)
374 EXPECT_CALL(*m_mock, process(
375 ResultOf([=](auto in) {
376 return (in.header.opcode == FUSE_UNLINK &&
377 0 == strcmp(path, in.body.unlink) &&
378 in.header.nodeid == parent);
381 ).WillOnce(Invoke(ReturnErrno(error)));
384 void FuseTest::expect_write(uint64_t ino, uint64_t offset, uint64_t isize,
385 uint64_t osize, uint32_t flags_set, uint32_t flags_unset,
386 const void *contents)
388 EXPECT_CALL(*m_mock, process(
389 ResultOf([=](auto in) {
390 const char *buf = (const char*)in.body.bytes +
391 sizeof(struct fuse_write_in);
393 uint32_t wf = in.body.write.write_flags;
395 if (wf & FUSE_WRITE_CACHE)
398 pid_ok = (pid_t)in.header.pid == getpid();
400 return (in.header.opcode == FUSE_WRITE &&
401 in.header.nodeid == ino &&
402 in.body.write.fh == FH &&
403 in.body.write.offset == offset &&
404 in.body.write.size == isize &&
406 (wf & flags_set) == flags_set &&
407 (wf & flags_unset) == 0 &&
408 (in.body.write.flags == O_WRONLY ||
409 in.body.write.flags == O_RDWR) &&
410 0 == bcmp(buf, contents, isize));
413 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
414 SET_OUT_HEADER_LEN(out, write);
415 out.body.write.size = osize;
419 void FuseTest::expect_write_7_8(uint64_t ino, uint64_t offset, uint64_t isize,
420 uint64_t osize, const void *contents)
422 EXPECT_CALL(*m_mock, process(
423 ResultOf([=](auto in) {
424 const char *buf = (const char*)in.body.bytes +
425 FUSE_COMPAT_WRITE_IN_SIZE;
426 bool pid_ok = (pid_t)in.header.pid == getpid();
427 return (in.header.opcode == FUSE_WRITE &&
428 in.header.nodeid == ino &&
429 in.body.write.fh == FH &&
430 in.body.write.offset == offset &&
431 in.body.write.size == isize &&
433 0 == bcmp(buf, contents, isize));
436 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
437 SET_OUT_HEADER_LEN(out, write);
438 out.body.write.size = osize;
443 get_unprivileged_id(uid_t *uid, gid_t *gid)
449 * First try "tests", Kyua's default unprivileged user. XXX after
450 * GoogleTest gains a proper Kyua wrapper, get this with the Kyua API
452 pw = getpwnam("tests");
454 /* Fall back to "nobody" */
455 pw = getpwnam("nobody");
458 GTEST_SKIP() << "Test requires an unprivileged user";
459 /* Use group "nobody", which is Kyua's default unprivileged group */
460 gr = getgrnam("nobody");
462 GTEST_SKIP() << "Test requires an unprivileged group";
468 FuseTest::fork(bool drop_privs, int *child_status,
469 std::function<void()> parent_func,
470 std::function<int()> child_func)
473 int mprot = PROT_READ | PROT_WRITE;
474 int mflags = MAP_ANON | MAP_SHARED;
480 get_unprivileged_id(&uid, &gid);
485 sem = (sem_t*)mmap(NULL, sizeof(*sem), mprot, mflags, -1, 0);
486 ASSERT_NE(MAP_FAILED, sem) << strerror(errno);
487 ASSERT_EQ(0, sem_init(sem, 1, 0)) << strerror(errno);
489 if ((child = ::fork()) == 0) {
499 if (drop_privs && 0 != setegid(gid)) {
504 if (drop_privs && 0 != setreuid(-1, uid)) {
514 } else if (child > 0) {
516 * In parent. Cleanup must happen here, because it's still
519 m_mock->m_child_pid = child;
520 ASSERT_NO_FATAL_FAILURE(parent_func());
522 /* Signal the child process to go */
523 ASSERT_EQ(0, sem_post(sem)) << strerror(errno);
525 ASSERT_LE(0, wait(child_status)) << strerror(errno);
527 FAIL() << strerror(errno);
529 munmap(sem, sizeof(*sem));
533 static void usage(char* progname) {
534 fprintf(stderr, "Usage: %s [-v]\n\t-v increase verbosity\n", progname);
538 int main(int argc, char **argv) {
540 FuseEnv *fuse_env = new FuseEnv;
542 InitGoogleTest(&argc, argv);
543 AddGlobalTestEnvironment(fuse_env);
545 while ((ch = getopt(argc, argv, "v")) != -1) {
556 return (RUN_ALL_TESTS());