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>
40 #include <semaphore.h>
44 #include <gtest/gtest.h>
49 using namespace testing;
51 /* Check that fusefs(4) is accessible and the current user can mount(2) */
52 void check_environment()
54 const char *devnode = "/dev/fuse";
55 const char *usermount_node = "vfs.usermount";
56 int usermount_val = 0;
57 size_t usermount_size = sizeof(usermount_val);
58 if (eaccess(devnode, R_OK | W_OK)) {
59 if (errno == ENOENT) {
60 GTEST_SKIP() << devnode << " does not exist";
61 } else if (errno == EACCES) {
62 GTEST_SKIP() << devnode <<
63 " is not accessible by the current user";
65 GTEST_SKIP() << strerror(errno);
68 sysctlbyname(usermount_node, &usermount_val, &usermount_size,
70 if (geteuid() != 0 && !usermount_val)
71 GTEST_SKIP() << "current user is not allowed to mount";
74 class FuseEnv: public Environment {
75 virtual void SetUp() {
79 void FuseTest::SetUp() {
80 const char *node = "vfs.maxbcachebuf";
82 size_t size = sizeof(val);
85 * XXX check_environment should be called from FuseEnv::SetUp, but
86 * can't due to https://github.com/google/googletest/issues/2189
92 ASSERT_EQ(0, sysctlbyname(node, &val, &size, NULL, 0))
97 m_mock = new MockFS(m_maxreadahead, m_allow_other,
98 m_default_permissions, m_push_symlinks_in, m_ro,
99 m_pm, m_init_flags, m_kernel_minor_version);
101 * FUSE_ACCESS is called almost universally. Expecting it in
102 * each test case would be super-annoying. Instead, set a
103 * default expectation for FUSE_ACCESS and return ENOSYS.
105 * Individual test cases can override this expectation since
106 * googlemock evaluates expectations in LIFO order.
108 EXPECT_CALL(*m_mock, process(
109 ResultOf([=](auto in) {
110 return (in->header.opcode == FUSE_ACCESS);
114 .WillRepeatedly(Invoke(ReturnErrno(ENOSYS)));
115 } catch (std::system_error err) {
116 FAIL() << err.what();
121 FuseTest::expect_access(uint64_t ino, mode_t access_mode, int error)
123 EXPECT_CALL(*m_mock, process(
124 ResultOf([=](auto in) {
125 return (in->header.opcode == FUSE_ACCESS &&
126 in->header.nodeid == ino &&
127 in->body.access.mask == access_mode);
130 ).WillOnce(Invoke(ReturnErrno(error)));
134 FuseTest::expect_destroy(int error)
136 EXPECT_CALL(*m_mock, process(
137 ResultOf([=](auto in) {
138 return (in->header.opcode == FUSE_DESTROY);
141 ).WillOnce(Invoke( ReturnImmediate([&](auto in, auto out) {
142 m_mock->m_quit = true;
143 out->header.len = sizeof(out->header);
144 out->header.unique = in->header.unique;
145 out->header.error = -error;
150 FuseTest::expect_flush(uint64_t ino, int times, ProcessMockerT r)
152 EXPECT_CALL(*m_mock, process(
153 ResultOf([=](auto in) {
154 return (in->header.opcode == FUSE_FLUSH &&
155 in->header.nodeid == ino);
159 .WillRepeatedly(Invoke(r));
163 FuseTest::expect_forget(uint64_t ino, uint64_t nlookup)
165 EXPECT_CALL(*m_mock, process(
166 ResultOf([=](auto in) {
167 return (in->header.opcode == FUSE_FORGET &&
168 in->header.nodeid == ino &&
169 in->body.forget.nlookup == nlookup);
172 ).WillOnce(Invoke([](auto in __unused, auto &out __unused) {
173 /* FUSE_FORGET has no response! */
177 void FuseTest::expect_getattr(uint64_t ino, uint64_t size)
179 EXPECT_CALL(*m_mock, process(
180 ResultOf([=](auto in) {
181 return (in->header.opcode == FUSE_GETATTR &&
182 in->header.nodeid == ino);
185 ).WillOnce(Invoke(ReturnImmediate([=](auto i __unused, auto out) {
186 SET_OUT_HEADER_LEN(out, attr);
187 out->body.attr.attr.ino = ino; // Must match nodeid
188 out->body.attr.attr.mode = S_IFREG | 0644;
189 out->body.attr.attr.size = size;
190 out->body.attr.attr_valid = UINT64_MAX;
194 void FuseTest::expect_lookup(const char *relpath, uint64_t ino, mode_t mode,
195 uint64_t size, int times, uint64_t attr_valid, uid_t uid, gid_t gid)
197 EXPECT_LOOKUP(1, relpath)
199 .WillRepeatedly(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
200 SET_OUT_HEADER_LEN(out, entry);
201 out->body.entry.attr.mode = mode;
202 out->body.entry.nodeid = ino;
203 out->body.entry.attr.nlink = 1;
204 out->body.entry.attr_valid = attr_valid;
205 out->body.entry.attr.size = size;
206 out->body.entry.attr.uid = uid;
207 out->body.entry.attr.gid = gid;
211 void FuseTest::expect_lookup_7_8(const char *relpath, uint64_t ino, mode_t mode,
212 uint64_t size, int times, uint64_t attr_valid, uid_t uid, gid_t gid)
214 EXPECT_LOOKUP(1, relpath)
216 .WillRepeatedly(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
217 SET_OUT_HEADER_LEN(out, entry_7_8);
218 out->body.entry.attr.mode = mode;
219 out->body.entry.nodeid = ino;
220 out->body.entry.attr.nlink = 1;
221 out->body.entry.attr_valid = attr_valid;
222 out->body.entry.attr.size = size;
223 out->body.entry.attr.uid = uid;
224 out->body.entry.attr.gid = gid;
228 void FuseTest::expect_open(uint64_t ino, uint32_t flags, int times)
230 EXPECT_CALL(*m_mock, process(
231 ResultOf([=](auto in) {
232 return (in->header.opcode == FUSE_OPEN &&
233 in->header.nodeid == ino);
237 .WillRepeatedly(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
238 out->header.len = sizeof(out->header);
239 SET_OUT_HEADER_LEN(out, open);
240 out->body.open.fh = FH;
241 out->body.open.open_flags = flags;
245 void FuseTest::expect_opendir(uint64_t ino)
247 /* opendir(3) calls fstatfs */
248 EXPECT_CALL(*m_mock, process(
249 ResultOf([](auto in) {
250 return (in->header.opcode == FUSE_STATFS);
253 ).WillRepeatedly(Invoke(ReturnImmediate([=](auto i __unused, auto out) {
254 SET_OUT_HEADER_LEN(out, statfs);
257 EXPECT_CALL(*m_mock, process(
258 ResultOf([=](auto in) {
259 return (in->header.opcode == FUSE_OPENDIR &&
260 in->header.nodeid == ino);
263 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
264 out->header.len = sizeof(out->header);
265 SET_OUT_HEADER_LEN(out, open);
266 out->body.open.fh = FH;
270 void FuseTest::expect_read(uint64_t ino, uint64_t offset, uint64_t isize,
271 uint64_t osize, const void *contents)
273 EXPECT_CALL(*m_mock, process(
274 ResultOf([=](auto in) {
275 return (in->header.opcode == FUSE_READ &&
276 in->header.nodeid == ino &&
277 in->body.read.fh == FH &&
278 in->body.read.offset == offset &&
279 in->body.read.size == isize);
282 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
283 out->header.len = sizeof(struct fuse_out_header) + osize;
284 memmove(out->body.bytes, contents, osize);
285 }))).RetiresOnSaturation();
288 void FuseTest::expect_release(uint64_t ino, uint64_t fh)
290 EXPECT_CALL(*m_mock, process(
291 ResultOf([=](auto in) {
292 return (in->header.opcode == FUSE_RELEASE &&
293 in->header.nodeid == ino &&
294 in->body.release.fh == fh);
297 ).WillOnce(Invoke(ReturnErrno(0)));
300 void FuseTest::expect_releasedir(uint64_t ino, ProcessMockerT r)
302 EXPECT_CALL(*m_mock, process(
303 ResultOf([=](auto in) {
304 return (in->header.opcode == FUSE_RELEASEDIR &&
305 in->header.nodeid == ino &&
306 in->body.release.fh == FH);
309 ).WillOnce(Invoke(r));
312 void FuseTest::expect_unlink(uint64_t parent, const char *path, int error)
314 EXPECT_CALL(*m_mock, process(
315 ResultOf([=](auto in) {
316 return (in->header.opcode == FUSE_UNLINK &&
317 0 == strcmp(path, in->body.unlink) &&
318 in->header.nodeid == parent);
321 ).WillOnce(Invoke(ReturnErrno(error)));
324 void FuseTest::expect_write(uint64_t ino, uint64_t offset, uint64_t isize,
325 uint64_t osize, uint32_t flags, const void *contents)
327 EXPECT_CALL(*m_mock, process(
328 ResultOf([=](auto in) {
329 const char *buf = (const char*)in->body.bytes +
330 sizeof(struct fuse_write_in);
333 if (in->body.write.write_flags & FUSE_WRITE_CACHE)
336 pid_ok = (pid_t)in->header.pid == getpid();
338 return (in->header.opcode == FUSE_WRITE &&
339 in->header.nodeid == ino &&
340 in->body.write.fh == FH &&
341 in->body.write.offset == offset &&
342 in->body.write.size == isize &&
344 in->body.write.write_flags == flags &&
345 0 == bcmp(buf, contents, isize));
348 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
349 SET_OUT_HEADER_LEN(out, write);
350 out->body.write.size = osize;
354 void FuseTest::expect_write_7_8(uint64_t ino, uint64_t offset, uint64_t isize,
355 uint64_t osize, uint32_t flags, const void *contents)
357 EXPECT_CALL(*m_mock, process(
358 ResultOf([=](auto in) {
359 const char *buf = (const char*)in->body.bytes +
360 FUSE_COMPAT_WRITE_IN_SIZE;
361 bool pid_ok = (pid_t)in->header.pid == getpid();
362 return (in->header.opcode == FUSE_WRITE &&
363 in->header.nodeid == ino &&
364 in->body.write.fh == FH &&
365 in->body.write.offset == offset &&
366 in->body.write.size == isize &&
368 in->body.write.write_flags == flags &&
369 0 == bcmp(buf, contents, isize));
372 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
373 SET_OUT_HEADER_LEN(out, write);
374 out->body.write.size = osize;
379 get_unprivileged_id(uid_t *uid, gid_t *gid)
385 * First try "tests", Kyua's default unprivileged user. XXX after
386 * GoogleTest gains a proper Kyua wrapper, get this with the Kyua API
388 pw = getpwnam("tests");
390 /* Fall back to "nobody" */
391 pw = getpwnam("nobody");
394 GTEST_SKIP() << "Test requires an unprivileged user";
395 /* Use group "nobody", which is Kyua's default unprivileged group */
396 gr = getgrnam("nobody");
398 GTEST_SKIP() << "Test requires an unprivileged group";
404 FuseTest::fork(bool drop_privs, int *child_status,
405 std::function<void()> parent_func,
406 std::function<int()> child_func)
409 int mprot = PROT_READ | PROT_WRITE;
410 int mflags = MAP_ANON | MAP_SHARED;
416 get_unprivileged_id(&uid, &gid);
421 sem = (sem_t*)mmap(NULL, sizeof(*sem), mprot, mflags, -1, 0);
422 ASSERT_NE(MAP_FAILED, sem) << strerror(errno);
423 ASSERT_EQ(0, sem_init(sem, 1, 0)) << strerror(errno);
425 if ((child = ::fork()) == 0) {
435 if (drop_privs && 0 != setegid(gid)) {
440 if (drop_privs && 0 != setreuid(-1, uid)) {
450 } else if (child > 0) {
452 * In parent. Cleanup must happen here, because it's still
455 m_mock->m_child_pid = child;
456 ASSERT_NO_FATAL_FAILURE(parent_func());
458 /* Signal the child process to go */
459 ASSERT_EQ(0, sem_post(sem)) << strerror(errno);
461 ASSERT_LE(0, wait(child_status)) << strerror(errno);
463 FAIL() << strerror(errno);
465 munmap(sem, sizeof(*sem));
469 static void usage(char* progname) {
470 fprintf(stderr, "Usage: %s [-v]\n\t-v increase verbosity\n", progname);
474 int main(int argc, char **argv) {
476 FuseEnv *fuse_env = new FuseEnv;
478 InitGoogleTest(&argc, argv);
479 AddGlobalTestEnvironment(fuse_env);
481 while ((ch = getopt(argc, argv, "v")) != -1) {
492 return (RUN_ALL_TESTS());