2 * Copyright (c) 2019 The FreeBSD Foundation
5 * This software was developed by BFF Storage Systems, LLC under sponsorship
6 * from the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 using namespace testing;
40 class Flush: public FuseTest {
42 const static uint64_t FH = 0xdeadbeef1a7ebabe;
45 void expect_flush(uint64_t ino, int times, ProcessMockerT r)
47 EXPECT_CALL(*m_mock, process(
48 ResultOf([=](auto in) {
49 return (in->header.opcode == FUSE_FLUSH &&
50 in->header.nodeid == ino &&
51 in->body.flush.fh == Flush::FH);
55 .WillRepeatedly(Invoke(r));
58 void expect_getattr(uint64_t ino)
60 /* Until the attr cache is working, we may send an additional GETATTR */
61 EXPECT_CALL(*m_mock, process(
62 ResultOf([=](auto in) {
63 return (in->header.opcode == FUSE_GETATTR &&
64 in->header.nodeid == ino);
67 ).WillRepeatedly(Invoke([=](auto in, auto out) {
68 out->header.unique = in->header.unique;
69 SET_OUT_HEADER_LEN(out, attr);
70 out->body.attr.attr.ino = ino; // Must match nodeid
71 out->body.attr.attr.mode = S_IFREG | 0644;
76 void expect_lookup(const char *relpath, uint64_t ino)
78 EXPECT_LOOKUP(1, relpath).WillRepeatedly(Invoke([=](auto in, auto out) {
79 out->header.unique = in->header.unique;
80 SET_OUT_HEADER_LEN(out, entry);
81 out->body.entry.attr.mode = S_IFREG | 0644;
82 out->body.entry.nodeid = ino;
83 out->body.entry.attr_valid = UINT64_MAX;
87 void expect_open(uint64_t ino, int times)
89 EXPECT_CALL(*m_mock, process(
90 ResultOf([=](auto in) {
91 return (in->header.opcode == FUSE_OPEN &&
92 in->header.nodeid == ino);
96 .WillRepeatedly(Invoke([](auto in, auto out) {
97 out->header.unique = in->header.unique;
98 out->header.len = sizeof(out->header);
99 SET_OUT_HEADER_LEN(out, open);
100 out->body.open.fh = Flush::FH;
106 * When testing FUSE_FLUSH, the FUSE_RELEASE calls are uninteresting. This
107 * expectation will silence googlemock warnings
109 void expect_release()
111 EXPECT_CALL(*m_mock, process(
112 ResultOf([=](auto in) {
113 return (in->header.opcode == FUSE_RELEASE);
116 ).WillRepeatedly(Invoke(ReturnErrno(0)));
120 // TODO: lock_owner stuff
122 /* If a file descriptor is duplicated, every close causes FLUSH */
123 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236405 */
124 TEST_F(Flush, DISABLED_dup)
126 const char FULLPATH[] = "mountpoint/some_file.txt";
127 const char RELPATH[] = "some_file.txt";
131 expect_lookup(RELPATH, ino);
134 expect_flush(ino, 2, ReturnErrno(0));
137 fd = open(FULLPATH, O_WRONLY);
138 EXPECT_LE(0, fd) << strerror(errno);
142 ASSERT_EQ(0, close(fd2)) << strerror(errno);
143 ASSERT_EQ(0, close(fd)) << strerror(errno);
147 * Some FUSE filesystem cache data internally and flush it on release. Such
148 * filesystems may generate errors during release. On Linux, these get
149 * returned by close(2). However, POSIX does not require close(2) to return
150 * this error. FreeBSD's fuse(4) should return EIO if it returns an error at
153 /* http://pubs.opengroup.org/onlinepubs/9699919799/functions/close.html */
154 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236405 */
155 TEST_F(Flush, DISABLED_eio)
157 const char FULLPATH[] = "mountpoint/some_file.txt";
158 const char RELPATH[] = "some_file.txt";
162 expect_lookup(RELPATH, ino);
165 expect_flush(ino, 1, ReturnErrno(EIO));
168 fd = open(FULLPATH, O_WRONLY);
169 EXPECT_LE(0, fd) << strerror(errno);
171 ASSERT_TRUE(0 == close(fd) || errno == EIO) << strerror(errno);
174 /* A FUSE_FLUSH should be sent on close(2) */
175 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236405 */
176 TEST_F(Flush, DISABLED_flush)
178 const char FULLPATH[] = "mountpoint/some_file.txt";
179 const char RELPATH[] = "some_file.txt";
183 expect_lookup(RELPATH, ino);
186 expect_flush(ino, 1, ReturnErrno(0));
189 fd = open(FULLPATH, O_WRONLY);
190 EXPECT_LE(0, fd) << strerror(errno);
192 ASSERT_TRUE(0 == close(fd)) << strerror(errno);