2019-03-01 23:53:05 +00:00
|
|
|
/*-
|
2019-03-11 19:10:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
|
|
|
|
*
|
2019-03-01 23:53:05 +00:00
|
|
|
* Copyright (c) 2019 The FreeBSD Foundation
|
|
|
|
*
|
|
|
|
* This software was developed by BFF Storage Systems, LLC under sponsorship
|
|
|
|
* from the FreeBSD Foundation.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <pthread.h>
|
|
|
|
|
|
|
|
#include "fuse_kernel.h"
|
|
|
|
}
|
|
|
|
|
|
|
|
#include <gmock/gmock.h>
|
|
|
|
|
2019-03-06 00:38:10 +00:00
|
|
|
#define TIME_T_MAX (std::numeric_limits<time_t>::max())
|
|
|
|
|
2019-03-01 23:53:05 +00:00
|
|
|
#define SET_OUT_HEADER_LEN(out, variant) { \
|
|
|
|
(out)->header.len = (sizeof((out)->header) + \
|
|
|
|
sizeof((out)->body.variant)); \
|
|
|
|
}
|
|
|
|
|
2019-03-05 03:27:32 +00:00
|
|
|
/*
|
|
|
|
* Create an expectation on FUSE_LOOKUP and return it so the caller can set
|
|
|
|
* actions.
|
|
|
|
*
|
|
|
|
* This must be a macro instead of a method because EXPECT_CALL returns a type
|
|
|
|
* with a deleted constructor.
|
|
|
|
*/
|
2019-03-05 21:40:08 +00:00
|
|
|
#define EXPECT_LOOKUP(parent, path) \
|
2019-03-05 03:27:32 +00:00
|
|
|
EXPECT_CALL(*m_mock, process( \
|
|
|
|
ResultOf([=](auto in) { \
|
|
|
|
return (in->header.opcode == FUSE_LOOKUP && \
|
2019-03-05 21:40:08 +00:00
|
|
|
in->header.nodeid == (parent) && \
|
2019-03-05 03:27:32 +00:00
|
|
|
strcmp(in->body.lookup, (path)) == 0); \
|
|
|
|
}, Eq(true)), \
|
|
|
|
_) \
|
|
|
|
)
|
|
|
|
|
2019-03-01 23:53:05 +00:00
|
|
|
extern int verbosity;
|
|
|
|
|
2019-03-04 22:07:33 +00:00
|
|
|
/* This struct isn't defined by fuse_kernel.h or libfuse, but it should be */
|
|
|
|
struct fuse_create_out {
|
|
|
|
struct fuse_entry_out entry;
|
|
|
|
struct fuse_open_out open;
|
|
|
|
};
|
|
|
|
|
2019-03-01 23:53:05 +00:00
|
|
|
union fuse_payloads_in {
|
2019-03-05 18:53:28 +00:00
|
|
|
fuse_access_in access;
|
2019-03-02 15:32:20 +00:00
|
|
|
/* value is from fuse_kern_chan.c in fusefs-libs */
|
|
|
|
uint8_t bytes[0x21000 - sizeof(struct fuse_in_header)];
|
2019-03-08 23:07:51 +00:00
|
|
|
fuse_flush_in flush;
|
2019-03-11 22:29:56 +00:00
|
|
|
fuse_fsync_in fsync;
|
|
|
|
fuse_fsync_in fsyncdir;
|
2019-03-01 23:53:05 +00:00
|
|
|
fuse_forget_in forget;
|
|
|
|
fuse_init_in init;
|
2019-03-05 21:40:08 +00:00
|
|
|
fuse_link_in link;
|
2019-03-01 23:53:05 +00:00
|
|
|
char lookup[0];
|
2019-03-05 18:53:28 +00:00
|
|
|
fuse_mkdir_in mkdir;
|
2019-03-05 00:27:54 +00:00
|
|
|
fuse_mknod_in mknod;
|
2019-03-04 22:07:33 +00:00
|
|
|
fuse_open_in open;
|
2019-03-12 22:25:59 +00:00
|
|
|
fuse_open_in opendir;
|
2019-03-08 19:01:31 +00:00
|
|
|
fuse_read_in read;
|
2019-03-12 22:25:59 +00:00
|
|
|
fuse_read_in readdir;
|
2019-03-08 19:01:31 +00:00
|
|
|
fuse_release_in release;
|
2019-03-13 13:41:05 +00:00
|
|
|
fuse_release_in releasedir;
|
2019-03-05 21:40:08 +00:00
|
|
|
fuse_rename_in rename;
|
2019-03-06 00:38:10 +00:00
|
|
|
char rmdir[0];
|
2019-03-02 15:32:20 +00:00
|
|
|
fuse_setattr_in setattr;
|
2019-03-06 00:38:10 +00:00
|
|
|
char unlink[0];
|
2019-03-08 19:01:31 +00:00
|
|
|
fuse_write_in write;
|
2019-03-01 23:53:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct mockfs_buf_in {
|
|
|
|
fuse_in_header header;
|
|
|
|
union fuse_payloads_in body;
|
|
|
|
};
|
|
|
|
|
|
|
|
union fuse_payloads_out {
|
|
|
|
fuse_attr_out attr;
|
2019-03-04 22:07:33 +00:00
|
|
|
fuse_create_out create;
|
2019-03-08 19:01:31 +00:00
|
|
|
/* The protocol places no limits on the size of bytes */
|
2019-03-11 18:28:20 +00:00
|
|
|
uint8_t bytes[0x20000];
|
2019-03-04 22:07:33 +00:00
|
|
|
fuse_entry_out entry;
|
|
|
|
fuse_init_out init;
|
|
|
|
fuse_open_out open;
|
2019-03-06 00:38:10 +00:00
|
|
|
fuse_statfs_out statfs;
|
2019-03-04 22:07:33 +00:00
|
|
|
/*
|
|
|
|
* The protocol places no limits on the length of the string. This is
|
|
|
|
* merely convenient for testing.
|
|
|
|
*/
|
|
|
|
char str[80];
|
2019-03-08 19:01:31 +00:00
|
|
|
fuse_write_out write;
|
2019-03-01 23:53:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct mockfs_buf_out {
|
|
|
|
fuse_out_header header;
|
|
|
|
union fuse_payloads_out body;
|
|
|
|
};
|
|
|
|
|
2019-03-08 19:01:31 +00:00
|
|
|
/* A function that can be invoked in place of MockFS::process */
|
|
|
|
typedef std::function<void (const struct mockfs_buf_in *in,
|
|
|
|
struct mockfs_buf_out *out)>
|
|
|
|
ProcessMockerT;
|
|
|
|
|
2019-03-05 03:27:32 +00:00
|
|
|
/*
|
|
|
|
* Helper function used for setting an error expectation for any fuse operation.
|
|
|
|
* The operation will return the supplied error
|
|
|
|
*/
|
2019-03-08 19:01:31 +00:00
|
|
|
ProcessMockerT ReturnErrno(int error);
|
2019-03-05 03:27:32 +00:00
|
|
|
|
2019-03-06 00:38:10 +00:00
|
|
|
/* Helper function used for returning negative cache entries for LOOKUP */
|
|
|
|
std::function<void (const struct mockfs_buf_in *in, struct mockfs_buf_out *out)>
|
|
|
|
ReturnNegativeCache(const struct timespec *entry_valid);
|
|
|
|
|
2019-03-01 23:53:05 +00:00
|
|
|
/*
|
|
|
|
* Fake FUSE filesystem
|
|
|
|
*
|
|
|
|
* "Mounts" a filesystem to a temporary directory and services requests
|
|
|
|
* according to the programmed expectations.
|
|
|
|
*
|
|
|
|
* Operates directly on the fuse(4) kernel API, not the libfuse(3) user api.
|
|
|
|
*/
|
|
|
|
class MockFS {
|
2019-03-06 00:38:10 +00:00
|
|
|
/*
|
|
|
|
* thread id of the fuse daemon thread
|
|
|
|
*
|
|
|
|
* It must run in a separate thread so it doesn't deadlock with the
|
|
|
|
* client test code.
|
|
|
|
*/
|
2019-03-01 23:53:05 +00:00
|
|
|
pthread_t m_daemon_id;
|
|
|
|
|
|
|
|
/* file descriptor of /dev/fuse control device */
|
|
|
|
int m_fuse_fd;
|
|
|
|
|
2019-03-11 18:28:20 +00:00
|
|
|
/* The max_readahead filesystem option */
|
|
|
|
uint32_t m_maxreadahead;
|
|
|
|
|
2019-03-01 23:53:05 +00:00
|
|
|
/* pid of the test process */
|
|
|
|
pid_t m_pid;
|
|
|
|
|
|
|
|
/* Initialize a session after mounting */
|
|
|
|
void init();
|
|
|
|
|
2019-03-08 19:01:31 +00:00
|
|
|
/* Is pid from a process that might be involved in the test? */
|
|
|
|
bool pid_ok(pid_t pid);
|
|
|
|
|
2019-03-01 23:53:05 +00:00
|
|
|
/* Default request handler */
|
|
|
|
void process_default(const mockfs_buf_in*, mockfs_buf_out*);
|
|
|
|
|
|
|
|
/* Entry point for the daemon thread */
|
|
|
|
static void* service(void*);
|
|
|
|
|
|
|
|
/* Read, but do not process, a single request from the kernel */
|
|
|
|
void read_request(mockfs_buf_in*);
|
|
|
|
|
|
|
|
public:
|
2019-03-08 19:01:31 +00:00
|
|
|
/* Maximum size of a FUSE_WRITE write */
|
|
|
|
uint32_t m_max_write;
|
|
|
|
|
2019-03-01 23:53:05 +00:00
|
|
|
/* Create a new mockfs and mount it to a tempdir */
|
2019-03-11 18:28:20 +00:00
|
|
|
MockFS(int max_readahead);
|
2019-03-01 23:53:05 +00:00
|
|
|
virtual ~MockFS();
|
|
|
|
|
2019-03-06 00:38:10 +00:00
|
|
|
/* Kill the filesystem daemon without unmounting the filesystem */
|
|
|
|
void kill_daemon();
|
|
|
|
|
2019-03-01 23:53:05 +00:00
|
|
|
/* Process FUSE requests endlessly */
|
|
|
|
void loop();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Request handler
|
|
|
|
*
|
|
|
|
* This method is expected to provide the response to each FUSE
|
|
|
|
* operation. Responses must be immediate (so this method can't be used
|
|
|
|
* for testing a daemon with queue depth > 1). Test cases must define
|
|
|
|
* each response using Googlemock expectations
|
|
|
|
*/
|
|
|
|
MOCK_METHOD2(process, void(const mockfs_buf_in*, mockfs_buf_out*));
|
|
|
|
|
|
|
|
/* Gracefully unmount */
|
|
|
|
void unmount();
|
|
|
|
};
|