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
39 using namespace testing;
41 class Flush: public FuseTest {
43 const static uint64_t FH = 0xdeadbeef1a7ebabe;
46 void expect_flush(uint64_t ino, int times, ProcessMockerT r)
48 EXPECT_CALL(*m_mock, process(
49 ResultOf([=](auto in) {
50 return (in->header.opcode == FUSE_FLUSH &&
51 in->header.nodeid == ino &&
52 in->body.flush.fh == Flush::FH);
56 .WillRepeatedly(Invoke(r));
59 void expect_getattr(uint64_t ino)
61 /* Until the attr cache is working, we may send an additional GETATTR */
62 EXPECT_CALL(*m_mock, process(
63 ResultOf([=](auto in) {
64 return (in->header.opcode == FUSE_GETATTR &&
65 in->header.nodeid == ino);
68 ).WillRepeatedly(Invoke([=](auto in, auto out) {
69 out->header.unique = in->header.unique;
70 SET_OUT_HEADER_LEN(out, attr);
71 out->body.attr.attr.ino = ino; // Must match nodeid
72 out->body.attr.attr.mode = S_IFREG | 0644;
77 void expect_lookup(const char *relpath, uint64_t ino)
79 EXPECT_LOOKUP(1, relpath).WillRepeatedly(Invoke([=](auto in, auto out) {
80 out->header.unique = in->header.unique;
81 SET_OUT_HEADER_LEN(out, entry);
82 out->body.entry.attr.mode = S_IFREG | 0644;
83 out->body.entry.nodeid = ino;
84 out->body.entry.attr_valid = UINT64_MAX;
88 void expect_open(uint64_t ino, int times)
90 EXPECT_CALL(*m_mock, process(
91 ResultOf([=](auto in) {
92 return (in->header.opcode == FUSE_OPEN &&
93 in->header.nodeid == ino);
97 .WillRepeatedly(Invoke([](auto in, auto out) {
98 out->header.unique = in->header.unique;
99 out->header.len = sizeof(out->header);
100 SET_OUT_HEADER_LEN(out, open);
101 out->body.open.fh = Flush::FH;
107 * When testing FUSE_FLUSH, the FUSE_RELEASE calls are uninteresting. This
108 * expectation will silence googlemock warnings
110 void expect_release()
112 EXPECT_CALL(*m_mock, process(
113 ResultOf([=](auto in) {
114 return (in->header.opcode == FUSE_RELEASE);
117 ).WillRepeatedly(Invoke(ReturnErrno(0)));
121 // TODO: lock_owner stuff
123 /* If a file descriptor is duplicated, every close causes FLUSH */
124 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236405 */
125 TEST_F(Flush, DISABLED_dup)
127 const char FULLPATH[] = "mountpoint/some_file.txt";
128 const char RELPATH[] = "some_file.txt";
132 expect_lookup(RELPATH, ino);
135 expect_flush(ino, 2, ReturnErrno(0));
138 fd = open(FULLPATH, O_WRONLY);
139 EXPECT_LE(0, fd) << strerror(errno);
143 ASSERT_EQ(0, close(fd2)) << strerror(errno);
144 ASSERT_EQ(0, close(fd)) << strerror(errno);
148 * Some FUSE filesystem cache data internally and flush it on release. Such
149 * filesystems may generate errors during release. On Linux, these get
150 * returned by close(2). However, POSIX does not require close(2) to return
151 * this error. FreeBSD's fuse(4) should return EIO if it returns an error at
154 /* http://pubs.opengroup.org/onlinepubs/9699919799/functions/close.html */
155 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236405 */
156 TEST_F(Flush, DISABLED_eio)
158 const char FULLPATH[] = "mountpoint/some_file.txt";
159 const char RELPATH[] = "some_file.txt";
163 expect_lookup(RELPATH, ino);
166 expect_flush(ino, 1, ReturnErrno(EIO));
169 fd = open(FULLPATH, O_WRONLY);
170 EXPECT_LE(0, fd) << strerror(errno);
172 ASSERT_TRUE(0 == close(fd) || errno == EIO) << strerror(errno);
175 /* A FUSE_FLUSH should be sent on close(2) */
176 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236405 */
177 TEST_F(Flush, DISABLED_flush)
179 const char FULLPATH[] = "mountpoint/some_file.txt";
180 const char RELPATH[] = "some_file.txt";
184 expect_lookup(RELPATH, ino);
187 expect_flush(ino, 1, ReturnErrno(0));
190 fd = open(FULLPATH, O_WRONLY);
191 EXPECT_LE(0, fd) << strerror(errno);
193 ASSERT_TRUE(0 == close(fd)) << strerror(errno);