]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tests/sys/fs/fuse/flush.cc
Update copyright info in fuse tests
[FreeBSD/FreeBSD.git] / tests / sys / fs / fuse / flush.cc
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2019 The FreeBSD Foundation
5  *
6  * This software was developed by BFF Storage Systems, LLC under sponsorship
7  * from the FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
17  *
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
28  * SUCH DAMAGE.
29  */
30
31 extern "C" {
32 #include <fcntl.h>
33 #include <unistd.h>
34 }
35
36 #include "mockfs.hh"
37 #include "utils.hh"
38
39 using namespace testing;
40
41 class Flush: public FuseTest {
42
43 const static uint64_t FH = 0xdeadbeef1a7ebabe;
44
45 public:
46 void expect_flush(uint64_t ino, int times, ProcessMockerT r)
47 {
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);
53                 }, Eq(true)),
54                 _)
55         ).Times(times)
56         .WillRepeatedly(Invoke(r));
57 }
58
59 void expect_getattr(uint64_t ino)
60 {
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);
66                 }, Eq(true)),
67                 _)
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;
73         }));
74
75 }
76
77 void expect_lookup(const char *relpath, uint64_t ino)
78 {
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;
85         }));
86 }
87
88 void expect_open(uint64_t ino, int times)
89 {
90         EXPECT_CALL(*m_mock, process(
91                 ResultOf([=](auto in) {
92                         return (in->header.opcode == FUSE_OPEN &&
93                                 in->header.nodeid == ino);
94                 }, Eq(true)),
95                 _)
96         ).Times(times)
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;
102         }));
103
104 }
105
106 /*
107  * When testing FUSE_FLUSH, the FUSE_RELEASE calls are uninteresting.  This
108  * expectation will silence googlemock warnings
109  */
110 void expect_release()
111 {
112         EXPECT_CALL(*m_mock, process(
113                 ResultOf([=](auto in) {
114                         return (in->header.opcode == FUSE_RELEASE);
115                 }, Eq(true)),
116                 _)
117         ).WillRepeatedly(Invoke(ReturnErrno(0)));
118 }
119 };
120
121 // TODO: lock_owner stuff
122
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)
126 {
127         const char FULLPATH[] = "mountpoint/some_file.txt";
128         const char RELPATH[] = "some_file.txt";
129         uint64_t ino = 42;
130         int fd, fd2;
131
132         expect_lookup(RELPATH, ino);
133         expect_open(ino, 1);
134         expect_getattr(ino);
135         expect_flush(ino, 2, ReturnErrno(0));
136         expect_release();
137
138         fd = open(FULLPATH, O_WRONLY);
139         EXPECT_LE(0, fd) << strerror(errno);
140
141         fd2 = dup(fd);
142
143         ASSERT_EQ(0, close(fd2)) << strerror(errno);
144         ASSERT_EQ(0, close(fd)) << strerror(errno);
145 }
146
147 /*
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
152  * all.
153  */
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)
157 {
158         const char FULLPATH[] = "mountpoint/some_file.txt";
159         const char RELPATH[] = "some_file.txt";
160         uint64_t ino = 42;
161         int fd;
162
163         expect_lookup(RELPATH, ino);
164         expect_open(ino, 1);
165         expect_getattr(ino);
166         expect_flush(ino, 1, ReturnErrno(EIO));
167         expect_release();
168
169         fd = open(FULLPATH, O_WRONLY);
170         EXPECT_LE(0, fd) << strerror(errno);
171
172         ASSERT_TRUE(0 == close(fd) || errno == EIO) << strerror(errno);
173 }
174
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)
178 {
179         const char FULLPATH[] = "mountpoint/some_file.txt";
180         const char RELPATH[] = "some_file.txt";
181         uint64_t ino = 42;
182         int fd;
183
184         expect_lookup(RELPATH, ino);
185         expect_open(ino, 1);
186         expect_getattr(ino);
187         expect_flush(ino, 1, ReturnErrno(0));
188         expect_release();
189
190         fd = open(FULLPATH, O_WRONLY);
191         EXPECT_LE(0, fd) << strerror(errno);
192
193         ASSERT_TRUE(0 == close(fd)) << strerror(errno);
194 }