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/types.h>
36 #include "fuse_kernel.h"
39 #include <gmock/gmock.h>
41 #define TIME_T_MAX (std::numeric_limits<time_t>::max())
43 #define SET_OUT_HEADER_LEN(out, variant) { \
44 (out)->header.len = (sizeof((out)->header) + \
45 sizeof((out)->body.variant)); \
49 * Create an expectation on FUSE_LOOKUP and return it so the caller can set
52 * This must be a macro instead of a method because EXPECT_CALL returns a type
53 * with a deleted constructor.
55 #define EXPECT_LOOKUP(parent, path) \
56 EXPECT_CALL(*m_mock, process( \
57 ResultOf([=](auto in) { \
58 return (in->header.opcode == FUSE_LOOKUP && \
59 in->header.nodeid == (parent) && \
60 strcmp(in->body.lookup, (path)) == 0); \
67 /* This struct isn't defined by fuse_kernel.h or libfuse, but it should be */
68 struct fuse_create_out {
69 struct fuse_entry_out entry;
70 struct fuse_open_out open;
73 union fuse_payloads_in {
74 fuse_access_in access;
75 /* value is from fuse_kern_chan.c in fusefs-libs */
76 uint8_t bytes[0x21000 - sizeof(struct fuse_in_header)];
79 fuse_fsync_in fsyncdir;
80 fuse_forget_in forget;
90 fuse_release_in release;
91 fuse_rename_in rename;
93 fuse_setattr_in setattr;
98 struct mockfs_buf_in {
99 fuse_in_header header;
100 union fuse_payloads_in body;
103 union fuse_payloads_out {
105 fuse_create_out create;
106 /* The protocol places no limits on the size of bytes */
107 uint8_t bytes[0x20000];
108 fuse_entry_out entry;
111 fuse_statfs_out statfs;
113 * The protocol places no limits on the length of the string. This is
114 * merely convenient for testing.
117 fuse_write_out write;
120 struct mockfs_buf_out {
121 fuse_out_header header;
122 union fuse_payloads_out body;
125 /* A function that can be invoked in place of MockFS::process */
126 typedef std::function<void (const struct mockfs_buf_in *in,
127 struct mockfs_buf_out *out)>
131 * Helper function used for setting an error expectation for any fuse operation.
132 * The operation will return the supplied error
134 ProcessMockerT ReturnErrno(int error);
136 /* Helper function used for returning negative cache entries for LOOKUP */
137 std::function<void (const struct mockfs_buf_in *in, struct mockfs_buf_out *out)>
138 ReturnNegativeCache(const struct timespec *entry_valid);
141 * Fake FUSE filesystem
143 * "Mounts" a filesystem to a temporary directory and services requests
144 * according to the programmed expectations.
146 * Operates directly on the fuse(4) kernel API, not the libfuse(3) user api.
150 * thread id of the fuse daemon thread
152 * It must run in a separate thread so it doesn't deadlock with the
155 pthread_t m_daemon_id;
157 /* file descriptor of /dev/fuse control device */
160 /* The max_readahead filesystem option */
161 uint32_t m_maxreadahead;
163 /* pid of the test process */
166 /* Initialize a session after mounting */
169 /* Is pid from a process that might be involved in the test? */
170 bool pid_ok(pid_t pid);
172 /* Default request handler */
173 void process_default(const mockfs_buf_in*, mockfs_buf_out*);
175 /* Entry point for the daemon thread */
176 static void* service(void*);
178 /* Read, but do not process, a single request from the kernel */
179 void read_request(mockfs_buf_in*);
182 /* Maximum size of a FUSE_WRITE write */
183 uint32_t m_max_write;
185 /* Create a new mockfs and mount it to a tempdir */
186 MockFS(int max_readahead);
189 /* Kill the filesystem daemon without unmounting the filesystem */
192 /* Process FUSE requests endlessly */
198 * This method is expected to provide the response to each FUSE
199 * operation. Responses must be immediate (so this method can't be used
200 * for testing a daemon with queue depth > 1). Test cases must define
201 * each response using Googlemock expectations
203 MOCK_METHOD2(process, void(const mockfs_buf_in*, mockfs_buf_out*));
205 /* Gracefully unmount */