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/mount.h>
37 #include <sys/select.h>
50 #include "mntopts.h" // for build_iovec
55 #include <gtest/gtest.h>
59 using namespace testing;
63 const char* opcode2opname(uint32_t opcode)
65 const char* table[] = {
85 "Unknown (opcode 19)",
115 if (opcode >= nitems(table))
116 return ("Unknown (opcode > max)");
118 return (table[opcode]);
122 ReturnErrno(int error)
124 return([=](auto in, auto &out) {
125 std::unique_ptr<mockfs_buf_out> out0(new mockfs_buf_out);
126 out0->header.unique = in.header.unique;
127 out0->header.error = -error;
128 out0->header.len = sizeof(out0->header);
129 out.push_back(std::move(out0));
133 /* Helper function used for returning negative cache entries for LOOKUP */
135 ReturnNegativeCache(const struct timespec *entry_valid)
137 return([=](auto in, auto &out) {
138 /* nodeid means ENOENT and cache it */
139 std::unique_ptr<mockfs_buf_out> out0(new mockfs_buf_out);
140 out0->body.entry.nodeid = 0;
141 out0->header.unique = in.header.unique;
142 out0->header.error = 0;
143 out0->body.entry.entry_valid = entry_valid->tv_sec;
144 out0->body.entry.entry_valid_nsec = entry_valid->tv_nsec;
145 SET_OUT_HEADER_LEN(*out0, entry);
146 out.push_back(std::move(out0));
151 ReturnImmediate(std::function<void(const mockfs_buf_in& in,
152 struct mockfs_buf_out &out)> f)
154 return([=](auto& in, auto &out) {
155 std::unique_ptr<mockfs_buf_out> out0(new mockfs_buf_out);
156 out0->header.unique = in.header.unique;
158 out.push_back(std::move(out0));
162 void sigint_handler(int __unused sig) {
163 // Don't do anything except interrupt the daemon's read(2) call
166 void MockFS::debug_request(const mockfs_buf_in &in, ssize_t buflen)
168 printf("%-11s ino=%2" PRIu64, opcode2opname(in.header.opcode),
171 printf(" uid=%5u gid=%5u pid=%5u unique=%" PRIu64 " len=%u"
173 in.header.uid, in.header.gid, in.header.pid,
174 in.header.unique, in.header.len, buflen);
176 switch (in.header.opcode) {
177 const char *name, *value;
180 printf(" mask=%#x", in.body.access.mask);
183 printf(" block=%" PRIx64 " blocksize=%#x",
184 in.body.bmap.block, in.body.bmap.blocksize);
186 case FUSE_COPY_FILE_RANGE:
187 printf(" off_in=%" PRIu64 " ino_out=%" PRIu64
188 " off_out=%" PRIu64 " size=%" PRIu64,
189 in.body.copy_file_range.off_in,
190 in.body.copy_file_range.nodeid_out,
191 in.body.copy_file_range.off_out,
192 in.body.copy_file_range.len);
194 printf(" fh_in=%" PRIu64 " fh_out=%" PRIu64
196 in.body.copy_file_range.fh_in,
197 in.body.copy_file_range.fh_out,
198 in.body.copy_file_range.flags);
201 if (m_kernel_minor_version >= 12)
202 name = (const char*)in.body.bytes +
203 sizeof(fuse_create_in);
205 name = (const char*)in.body.bytes +
206 sizeof(fuse_open_in);
207 printf(" flags=%#x name=%s",
208 in.body.open.flags, name);
211 printf(" fh=%#" PRIx64 " offset=%" PRIu64
212 " length=%" PRIx64 " mode=%#x",
213 in.body.fallocate.fh,
214 in.body.fallocate.offset,
215 in.body.fallocate.length,
216 in.body.fallocate.mode);
219 printf(" fh=%#" PRIx64 " lock_owner=%" PRIu64,
221 in.body.flush.lock_owner);
224 printf(" nlookup=%" PRIu64, in.body.forget.nlookup);
227 printf(" flags=%#x", in.body.fsync.fsync_flags);
230 printf(" flags=%#x", in.body.fsyncdir.fsync_flags);
233 printf(" fh=%#" PRIx64
236 in.body.getlk.lk.type,
237 in.body.getlk.lk.pid);
238 if (verbosity >= 2) {
239 printf(" range=[%" PRIi64 ":%" PRIi64 "]",
240 in.body.getlk.lk.start,
241 in.body.getlk.lk.end);
245 printf(" unique=%" PRIu64, in.body.interrupt.unique);
248 printf(" oldnodeid=%" PRIu64, in.body.link.oldnodeid);
251 printf(" size=%" PRIu32, in.body.listxattr.size);
254 printf(" %s", in.body.lookup);
257 switch (in.body.lseek.whence) {
259 printf(" SEEK_HOLE offset=%jd",
260 in.body.lseek.offset);
263 printf(" SEEK_DATA offset=%jd",
264 in.body.lseek.offset);
267 printf(" whence=%u offset=%jd",
268 in.body.lseek.whence, in.body.lseek.offset);
273 name = (const char*)in.body.bytes +
274 sizeof(fuse_mkdir_in);
275 printf(" name=%s mode=%#o umask=%#o", name,
276 in.body.mkdir.mode, in.body.mkdir.umask);
279 if (m_kernel_minor_version >= 12)
280 name = (const char*)in.body.bytes +
281 sizeof(fuse_mknod_in);
283 name = (const char*)in.body.bytes +
284 FUSE_COMPAT_MKNOD_IN_SIZE;
285 printf(" mode=%#o rdev=%x umask=%#o name=%s",
286 in.body.mknod.mode, in.body.mknod.rdev,
287 in.body.mknod.umask, name);
290 printf(" flags=%#x", in.body.open.flags);
293 printf(" flags=%#x", in.body.opendir.flags);
296 printf(" offset=%" PRIu64 " size=%u",
300 printf(" flags=%#x", in.body.read.flags);
303 printf(" fh=%#" PRIx64 " offset=%" PRIu64 " size=%u",
304 in.body.readdir.fh, in.body.readdir.offset,
305 in.body.readdir.size);
308 printf(" fh=%#" PRIx64 " flags=%#x lock_owner=%" PRIu64,
310 in.body.release.flags,
311 in.body.release.lock_owner);
315 const char *src = (const char*)in.body.bytes +
316 sizeof(fuse_rename_in);
317 const char *dst = src + strlen(src) + 1;
318 printf(" src=%s newdir=%" PRIu64 " dst=%s",
319 src, in.body.rename.newdir, dst);
323 if (verbosity <= 1) {
324 printf(" valid=%#x", in.body.setattr.valid);
327 if (in.body.setattr.valid & FATTR_MODE)
328 printf(" mode=%#o", in.body.setattr.mode);
329 if (in.body.setattr.valid & FATTR_UID)
330 printf(" uid=%u", in.body.setattr.uid);
331 if (in.body.setattr.valid & FATTR_GID)
332 printf(" gid=%u", in.body.setattr.gid);
333 if (in.body.setattr.valid & FATTR_SIZE)
334 printf(" size=%" PRIu64, in.body.setattr.size);
335 if (in.body.setattr.valid & FATTR_ATIME)
336 printf(" atime=%" PRIu64 ".%u",
337 in.body.setattr.atime,
338 in.body.setattr.atimensec);
339 if (in.body.setattr.valid & FATTR_MTIME)
340 printf(" mtime=%" PRIu64 ".%u",
341 in.body.setattr.mtime,
342 in.body.setattr.mtimensec);
343 if (in.body.setattr.valid & FATTR_FH)
344 printf(" fh=%" PRIu64 "", in.body.setattr.fh);
347 printf(" fh=%#" PRIx64 " owner=%" PRIu64
349 in.body.setlk.fh, in.body.setlk.owner,
350 in.body.setlk.lk.type,
351 in.body.setlk.lk.pid);
352 if (verbosity >= 2) {
353 printf(" range=[%" PRIi64 ":%" PRIi64 "]",
354 in.body.setlk.lk.start,
355 in.body.setlk.lk.end);
360 * In theory neither the xattr name and value need be
361 * ASCII, but in this test suite they always are.
363 name = (const char*)in.body.bytes +
364 sizeof(fuse_setxattr_in);
365 value = name + strlen(name) + 1;
366 printf(" %s=%s", name, value);
369 printf(" fh=%#" PRIx64 " offset=%" PRIu64
370 " size=%u write_flags=%u",
372 in.body.write.offset, in.body.write.size,
373 in.body.write.write_flags);
375 printf(" flags=%#x", in.body.write.flags);
384 * Debug a FUSE response.
386 * This is mostly useful for asynchronous notifications, which don't correspond
389 void MockFS::debug_response(const mockfs_buf_out &out) {
395 switch (out.header.error) {
396 case FUSE_NOTIFY_INVAL_ENTRY:
397 name = (const char*)out.body.bytes +
398 sizeof(fuse_notify_inval_entry_out);
399 printf("<- INVAL_ENTRY parent=%" PRIu64 " %s\n",
400 out.body.inval_entry.parent, name);
402 case FUSE_NOTIFY_INVAL_INODE:
403 printf("<- INVAL_INODE ino=%" PRIu64 " off=%" PRIi64
404 " len=%" PRIi64 "\n",
405 out.body.inval_inode.ino,
406 out.body.inval_inode.off,
407 out.body.inval_inode.len);
409 case FUSE_NOTIFY_STORE:
410 printf("<- STORE ino=%" PRIu64 " off=%" PRIu64
411 " size=%" PRIu32 "\n",
412 out.body.store.nodeid,
413 out.body.store.offset,
414 out.body.store.size);
421 MockFS::MockFS(int max_readahead, bool allow_other, bool default_permissions,
422 bool push_symlinks_in, bool ro, enum poll_method pm, uint32_t flags,
423 uint32_t kernel_minor_version, uint32_t max_write, bool async,
424 bool noclusterr, unsigned time_gran, bool nointr, bool noatime,
425 const char *fsname, const char *subtype)
428 struct iovec *iov = NULL;
431 const bool trueval = true;
434 m_kernel_minor_version = kernel_minor_version;
435 m_maxreadahead = max_readahead;
436 m_maxwrite = MIN(max_write, max_max_write);
439 m_time_gran = time_gran;
448 * Kyua sets pwd to a testcase-unique tempdir; no need to use
452 * googletest doesn't allow ASSERT_ in constructors, so we must throw
455 if (mkdir("mountpoint" , 0755) && errno != EEXIST)
456 throw(std::system_error(errno, std::system_category(),
457 "Couldn't make mountpoint directory"));
461 m_fuse_fd = open("/dev/fuse", O_CLOEXEC | O_RDWR);
464 m_fuse_fd = open("/dev/fuse", O_CLOEXEC | O_RDWR | O_NONBLOCK);
468 throw(std::system_error(errno, std::system_category(),
469 "Couldn't open /dev/fuse"));
474 build_iovec(&iov, &iovlen, "fstype", __DECONST(void *, "fusefs"), -1);
475 build_iovec(&iov, &iovlen, "fspath",
476 __DECONST(void *, "mountpoint"), -1);
477 build_iovec(&iov, &iovlen, "from", __DECONST(void *, "/dev/fuse"), -1);
478 sprintf(fdstr, "%d", m_fuse_fd);
479 build_iovec(&iov, &iovlen, "fd", fdstr, -1);
481 build_iovec(&iov, &iovlen, "allow_other",
482 __DECONST(void*, &trueval), sizeof(bool));
484 if (default_permissions) {
485 build_iovec(&iov, &iovlen, "default_permissions",
486 __DECONST(void*, &trueval), sizeof(bool));
488 if (push_symlinks_in) {
489 build_iovec(&iov, &iovlen, "push_symlinks_in",
490 __DECONST(void*, &trueval), sizeof(bool));
493 build_iovec(&iov, &iovlen, "ro",
494 __DECONST(void*, &trueval), sizeof(bool));
497 build_iovec(&iov, &iovlen, "async", __DECONST(void*, &trueval),
501 build_iovec(&iov, &iovlen, "noatime",
502 __DECONST(void*, &trueval), sizeof(bool));
505 build_iovec(&iov, &iovlen, "noclusterr",
506 __DECONST(void*, &trueval), sizeof(bool));
509 build_iovec(&iov, &iovlen, "nointr",
510 __DECONST(void*, &trueval), sizeof(bool));
512 build_iovec(&iov, &iovlen, "intr",
513 __DECONST(void*, &trueval), sizeof(bool));
516 build_iovec(&iov, &iovlen, "fsname=",
517 __DECONST(void*, fsname), -1);
520 build_iovec(&iov, &iovlen, "subtype=",
521 __DECONST(void*, subtype), -1);
523 if (nmount(iov, iovlen, 0))
524 throw(std::system_error(errno, std::system_category(),
525 "Couldn't mount filesystem"));
527 // Setup default handler
528 ON_CALL(*this, process(_, _))
529 .WillByDefault(Invoke(this, &MockFS::process_default));
532 bzero(&sa, sizeof(sa));
533 sa.sa_handler = sigint_handler;
534 sa.sa_flags = 0; /* Don't set SA_RESTART! */
535 if (0 != sigaction(SIGUSR1, &sa, NULL))
536 throw(std::system_error(errno, std::system_category(),
537 "Couldn't handle SIGUSR1"));
538 if (pthread_create(&m_daemon_id, NULL, service, (void*)this))
539 throw(std::system_error(errno, std::system_category(),
540 "Couldn't Couldn't start fuse thread"));
545 if (m_daemon_id != NULL) {
546 pthread_join(m_daemon_id, NULL);
549 ::unmount("mountpoint", MNT_FORCE);
555 void MockFS::audit_request(const mockfs_buf_in &in, ssize_t buflen) {
556 uint32_t inlen = in.header.len;
557 size_t fih = sizeof(in.header);
558 switch (in.header.opcode) {
563 EXPECT_GT(inlen, fih) << "Missing request filename";
564 // No redundant information for checking buflen
567 EXPECT_EQ(inlen, fih + sizeof(in.body.forget));
568 EXPECT_EQ((size_t)buflen, inlen);
571 EXPECT_EQ(inlen, fih + sizeof(in.body.getattr));
572 EXPECT_EQ((size_t)buflen, inlen);
575 EXPECT_EQ(inlen, fih + sizeof(in.body.setattr));
576 EXPECT_EQ((size_t)buflen, inlen);
579 EXPECT_EQ(inlen, fih) << "Unexpected request body";
580 EXPECT_EQ((size_t)buflen, inlen);
585 if (m_kernel_minor_version >= 12)
586 s = sizeof(in.body.mknod);
588 s = FUSE_COMPAT_MKNOD_IN_SIZE;
589 EXPECT_GE(inlen, fih + s) << "Missing request body";
590 EXPECT_GT(inlen, fih + s) << "Missing request filename";
591 // No redundant information for checking buflen
595 EXPECT_GE(inlen, fih + sizeof(in.body.mkdir)) <<
596 "Missing request body";
597 EXPECT_GT(inlen, fih + sizeof(in.body.mkdir)) <<
598 "Missing request filename";
599 // No redundant information for checking buflen
602 EXPECT_GE(inlen, fih + sizeof(in.body.rename)) <<
603 "Missing request body";
604 EXPECT_GT(inlen, fih + sizeof(in.body.rename)) <<
605 "Missing request filename";
606 // No redundant information for checking buflen
609 EXPECT_GE(inlen, fih + sizeof(in.body.link)) <<
610 "Missing request body";
611 EXPECT_GT(inlen, fih + sizeof(in.body.link)) <<
612 "Missing request filename";
613 // No redundant information for checking buflen
616 EXPECT_EQ(inlen, fih + sizeof(in.body.open));
617 EXPECT_EQ((size_t)buflen, inlen);
620 EXPECT_EQ(inlen, fih + sizeof(in.body.read));
621 EXPECT_EQ((size_t)buflen, inlen);
627 if (m_kernel_minor_version >= 9)
628 s = sizeof(in.body.write);
630 s = FUSE_COMPAT_WRITE_IN_SIZE;
631 // I suppose a 0-byte write should be allowed
632 EXPECT_GE(inlen, fih + s) << "Missing request body";
633 EXPECT_EQ((size_t)buflen, fih + s + in.body.write.size);
638 EXPECT_EQ(inlen, fih);
639 EXPECT_EQ((size_t)buflen, inlen);
642 EXPECT_EQ(inlen, fih + sizeof(in.body.release));
643 EXPECT_EQ((size_t)buflen, inlen);
647 EXPECT_EQ(inlen, fih + sizeof(in.body.fsync));
648 EXPECT_EQ((size_t)buflen, inlen);
651 EXPECT_GE(inlen, fih + sizeof(in.body.setxattr)) <<
652 "Missing request body";
653 EXPECT_GT(inlen, fih + sizeof(in.body.setxattr)) <<
654 "Missing request attribute name";
655 // No redundant information for checking buflen
658 EXPECT_GE(inlen, fih + sizeof(in.body.getxattr)) <<
659 "Missing request body";
660 EXPECT_GT(inlen, fih + sizeof(in.body.getxattr)) <<
661 "Missing request attribute name";
662 // No redundant information for checking buflen
665 EXPECT_EQ(inlen, fih + sizeof(in.body.listxattr));
666 EXPECT_EQ((size_t)buflen, inlen);
668 case FUSE_REMOVEXATTR:
669 EXPECT_GT(inlen, fih) << "Missing request attribute name";
670 // No redundant information for checking buflen
673 EXPECT_EQ(inlen, fih + sizeof(in.body.flush));
674 EXPECT_EQ((size_t)buflen, inlen);
677 EXPECT_EQ(inlen, fih + sizeof(in.body.init));
678 EXPECT_EQ((size_t)buflen, inlen);
681 EXPECT_EQ(inlen, fih + sizeof(in.body.opendir));
682 EXPECT_EQ((size_t)buflen, inlen);
685 EXPECT_EQ(inlen, fih + sizeof(in.body.readdir));
686 EXPECT_EQ((size_t)buflen, inlen);
688 case FUSE_RELEASEDIR:
689 EXPECT_EQ(inlen, fih + sizeof(in.body.releasedir));
690 EXPECT_EQ((size_t)buflen, inlen);
693 EXPECT_EQ(inlen, fih + sizeof(in.body.getlk));
694 EXPECT_EQ((size_t)buflen, inlen);
698 EXPECT_EQ(inlen, fih + sizeof(in.body.setlk));
699 EXPECT_EQ((size_t)buflen, inlen);
702 EXPECT_EQ(inlen, fih + sizeof(in.body.access));
703 EXPECT_EQ((size_t)buflen, inlen);
706 EXPECT_GE(inlen, fih + sizeof(in.body.create)) <<
707 "Missing request body";
708 EXPECT_GT(inlen, fih + sizeof(in.body.create)) <<
709 "Missing request filename";
710 // No redundant information for checking buflen
713 EXPECT_EQ(inlen, fih + sizeof(in.body.interrupt));
714 EXPECT_EQ((size_t)buflen, inlen);
717 EXPECT_EQ(inlen, fih + sizeof(in.body.fallocate));
718 EXPECT_EQ((size_t)buflen, inlen);
721 EXPECT_EQ(inlen, fih + sizeof(in.body.bmap));
722 EXPECT_EQ((size_t)buflen, inlen);
725 EXPECT_EQ(inlen, fih + sizeof(in.body.lseek));
726 EXPECT_EQ((size_t)buflen, inlen);
728 case FUSE_COPY_FILE_RANGE:
729 EXPECT_EQ(inlen, fih + sizeof(in.body.copy_file_range));
730 EXPECT_EQ(0ul, in.body.copy_file_range.flags);
731 EXPECT_EQ((size_t)buflen, inlen);
733 case FUSE_NOTIFY_REPLY:
734 case FUSE_BATCH_FORGET:
737 case FUSE_READDIRPLUS:
738 FAIL() << "Unsupported opcode?";
740 FAIL() << "Unknown opcode " << in.header.opcode;
743 * Check that the ticket's unique value is sequential. Technically it
744 * doesn't need to be sequential, merely unique. But the current
745 * fusefs driver _does_ make it sequential, and that's easy to check
748 if (in.header.unique != ++m_last_unique)
749 FAIL() << "Non-sequential unique value";
752 void MockFS::init(uint32_t flags) {
755 std::unique_ptr<mockfs_buf_in> in(new mockfs_buf_in);
756 std::unique_ptr<mockfs_buf_out> out(new mockfs_buf_out);
758 read_request(*in, buflen);
760 debug_request(*in, buflen);
761 audit_request(*in, buflen);
762 ASSERT_EQ(FUSE_INIT, in->header.opcode);
764 out->header.unique = in->header.unique;
765 out->header.error = 0;
766 out->body.init.major = FUSE_KERNEL_VERSION;
767 out->body.init.minor = m_kernel_minor_version;;
768 out->body.init.flags = in->body.init.flags & flags;
769 out->body.init.max_write = m_maxwrite;
770 out->body.init.max_readahead = m_maxreadahead;
772 if (m_kernel_minor_version < 23) {
773 SET_OUT_HEADER_LEN(*out, init_7_22);
775 out->body.init.time_gran = m_time_gran;
776 SET_OUT_HEADER_LEN(*out, init);
779 write(m_fuse_fd, out.get(), out->header.len);
782 void MockFS::kill_daemon() {
784 if (m_daemon_id != NULL)
785 pthread_kill(m_daemon_id, SIGUSR1);
786 // Closing the /dev/fuse file descriptor first allows unmount to
787 // succeed even if the daemon doesn't correctly respond to commands
788 // during the unmount sequence.
793 void MockFS::loop() {
794 std::vector<std::unique_ptr<mockfs_buf_out>> out;
796 std::unique_ptr<mockfs_buf_in> in(new mockfs_buf_in);
797 ASSERT_TRUE(in != NULL);
801 bzero(in.get(), sizeof(*in));
802 read_request(*in, buflen);
806 debug_request(*in, buflen);
807 audit_request(*in, buflen);
808 if (pid_ok((pid_t)in->header.pid)) {
812 * Reject any requests from unknown processes. Because
813 * we actually do mount a filesystem, plenty of
814 * unrelated system daemons may try to access it.
817 printf("\tREJECTED (wrong pid %d)\n",
819 process_default(*in, out);
827 int MockFS::notify_inval_entry(ino_t parent, const char *name, size_t namelen)
829 std::unique_ptr<mockfs_buf_out> out(new mockfs_buf_out);
831 out->header.unique = 0; /* 0 means asynchronous notification */
832 out->header.error = FUSE_NOTIFY_INVAL_ENTRY;
833 out->body.inval_entry.parent = parent;
834 out->body.inval_entry.namelen = namelen;
835 strlcpy((char*)&out->body.bytes + sizeof(out->body.inval_entry),
836 name, sizeof(out->body.bytes) - sizeof(out->body.inval_entry));
837 out->header.len = sizeof(out->header) + sizeof(out->body.inval_entry) +
839 debug_response(*out);
840 write_response(*out);
844 int MockFS::notify_inval_inode(ino_t ino, off_t off, ssize_t len)
846 std::unique_ptr<mockfs_buf_out> out(new mockfs_buf_out);
848 out->header.unique = 0; /* 0 means asynchronous notification */
849 out->header.error = FUSE_NOTIFY_INVAL_INODE;
850 out->body.inval_inode.ino = ino;
851 out->body.inval_inode.off = off;
852 out->body.inval_inode.len = len;
853 out->header.len = sizeof(out->header) + sizeof(out->body.inval_inode);
854 debug_response(*out);
855 write_response(*out);
859 int MockFS::notify_store(ino_t ino, off_t off, const void* data, ssize_t size)
861 std::unique_ptr<mockfs_buf_out> out(new mockfs_buf_out);
863 out->header.unique = 0; /* 0 means asynchronous notification */
864 out->header.error = FUSE_NOTIFY_STORE;
865 out->body.store.nodeid = ino;
866 out->body.store.offset = off;
867 out->body.store.size = size;
868 bcopy(data, (char*)&out->body.bytes + sizeof(out->body.store), size);
869 out->header.len = sizeof(out->header) + sizeof(out->body.store) + size;
870 debug_response(*out);
871 write_response(*out);
875 bool MockFS::pid_ok(pid_t pid) {
878 } else if (pid == m_child_pid) {
881 struct kinfo_proc *ki;
884 ki = kinfo_getproc(pid);
888 * Allow access by the aio daemon processes so that our tests
889 * can use aio functions
891 if (0 == strncmp("aiod", ki->ki_comm, 4))
898 void MockFS::process_default(const mockfs_buf_in& in,
899 std::vector<std::unique_ptr<mockfs_buf_out>> &out)
901 std::unique_ptr<mockfs_buf_out> out0(new mockfs_buf_out);
902 out0->header.unique = in.header.unique;
903 out0->header.error = -EOPNOTSUPP;
904 out0->header.len = sizeof(out0->header);
905 out.push_back(std::move(out0));
908 void MockFS::read_request(mockfs_buf_in &in, ssize_t &res) {
912 struct kevent changes[1];
913 struct kevent events[1];
914 struct timespec timeout_ts;
915 struct timeval timeout_tv;
916 const int timeout_ms = 999;
917 int timeout_int, nfds;
924 timeout_ts.tv_sec = 0;
925 timeout_ts.tv_nsec = timeout_ms * 1'000'000;
926 while (nready == 0) {
927 EV_SET(&changes[0], m_fuse_fd, EVFILT_READ,
928 EV_ADD | EV_ONESHOT, 0, 0, 0);
929 nready = kevent(m_kq, &changes[0], 1, &events[0], 1,
934 ASSERT_LE(0, nready) << strerror(errno);
935 ASSERT_EQ(events[0].ident, (uintptr_t)m_fuse_fd);
936 if (events[0].flags & EV_ERROR)
937 FAIL() << strerror(events[0].data);
938 else if (events[0].flags & EV_EOF)
939 FAIL() << strerror(events[0].fflags);
940 m_nready = events[0].data;
943 timeout_int = timeout_ms;
944 fds[0].fd = m_fuse_fd;
945 fds[0].events = POLLIN;
946 while (nready == 0) {
947 nready = poll(fds, 1, timeout_int);
951 ASSERT_LE(0, nready) << strerror(errno);
952 ASSERT_TRUE(fds[0].revents & POLLIN);
958 timeout_tv.tv_sec = 0;
959 timeout_tv.tv_usec = timeout_ms * 1'000;
961 while (nready == 0) {
963 FD_SET(fuse_fd, &readfds);
964 nready = select(nfds, &readfds, NULL, NULL,
969 ASSERT_LE(0, nready) << strerror(errno);
970 ASSERT_TRUE(FD_ISSET(fuse_fd, &readfds));
973 FAIL() << "not yet implemented";
975 res = read(m_fuse_fd, &in, sizeof(in));
977 if (res < 0 && !m_quit) {
979 FAIL() << "read: " << strerror(errno);
981 ASSERT_TRUE(res >= static_cast<ssize_t>(sizeof(in.header)) || m_quit);
983 * Inconsistently, fuse_in_header.len is the size of the entire
984 * request,including header, even though fuse_out_header.len excludes
985 * the size of the header.
987 ASSERT_TRUE(res == static_cast<ssize_t>(in.header.len) || m_quit);
990 void MockFS::write_response(const mockfs_buf_out &out) {
993 struct kevent changes[1];
994 struct kevent events[1];
1002 EV_SET(&changes[0], m_fuse_fd, EVFILT_WRITE,
1003 EV_ADD | EV_ONESHOT, 0, 0, 0);
1004 nready = kevent(m_kq, &changes[0], 1, &events[0], 1,
1006 ASSERT_LE(0, nready) << strerror(errno);
1007 ASSERT_EQ(events[0].ident, (uintptr_t)m_fuse_fd);
1008 if (events[0].flags & EV_ERROR)
1009 FAIL() << strerror(events[0].data);
1010 else if (events[0].flags & EV_EOF)
1011 FAIL() << strerror(events[0].fflags);
1012 m_nready = events[0].data;
1015 fds[0].fd = m_fuse_fd;
1016 fds[0].events = POLLOUT;
1017 nready = poll(fds, 1, INFTIM);
1018 ASSERT_LE(0, nready) << strerror(errno);
1019 ASSERT_EQ(1, nready) << "NULL timeout expired?";
1020 ASSERT_TRUE(fds[0].revents & POLLOUT);
1024 FD_SET(m_fuse_fd, &writefds);
1025 nfds = m_fuse_fd + 1;
1026 nready = select(nfds, NULL, &writefds, NULL, NULL);
1027 ASSERT_LE(0, nready) << strerror(errno);
1028 ASSERT_EQ(1, nready) << "NULL timeout expired?";
1029 ASSERT_TRUE(FD_ISSET(m_fuse_fd, &writefds));
1032 FAIL() << "not yet implemented";
1034 r = write(m_fuse_fd, &out, out.header.len);
1035 if (out.expected_errno) {
1037 ASSERT_EQ(out.expected_errno, errno) << strerror(errno);
1039 ASSERT_TRUE(r > 0 || errno == EAGAIN) << strerror(errno);
1043 void* MockFS::service(void *pthr_data) {
1044 MockFS *mock_fs = (MockFS*)pthr_data;
1051 void MockFS::unmount() {
1052 ::unmount("mountpoint", 0);