]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tests/sys/fs/fusefs/release.cc
fusefs: make the tests more cplusplusy
[FreeBSD/FreeBSD.git] / tests / sys / fs / fusefs / release.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 Release: public FuseTest {
42
43 public:
44 void expect_lookup(const char *relpath, uint64_t ino, int times)
45 {
46         FuseTest::expect_lookup(relpath, ino, S_IFREG | 0644, 0, times);
47 }
48
49 void expect_release(uint64_t ino, uint64_t lock_owner,
50         uint32_t flags, int error)
51 {
52         EXPECT_CALL(*m_mock, process(
53                 ResultOf([=](auto in) {
54                         return (in.header.opcode == FUSE_RELEASE &&
55                                 in.header.nodeid == ino &&
56                                 in.body.release.lock_owner == lock_owner &&
57                                 in.body.release.fh == FH &&
58                                 in.body.release.flags == flags);
59                 }, Eq(true)),
60                 _)
61         ).WillOnce(Invoke(ReturnErrno(error)))
62         .RetiresOnSaturation();
63 }
64 };
65
66 class ReleaseWithLocks: public Release {
67         virtual void SetUp() {
68                 m_init_flags = FUSE_POSIX_LOCKS;
69                 Release::SetUp();
70         }
71 };
72
73
74 /* If a file descriptor is duplicated, only the last close causes RELEASE */
75 TEST_F(Release, dup)
76 {
77         const char FULLPATH[] = "mountpoint/some_file.txt";
78         const char RELPATH[] = "some_file.txt";
79         uint64_t ino = 42;
80         int fd, fd2;
81
82         expect_lookup(RELPATH, ino, 1);
83         expect_open(ino, 0, 1);
84         expect_flush(ino, 1, ReturnErrno(0));
85         expect_release(ino, getpid(), O_RDONLY, 0);
86         
87         fd = open(FULLPATH, O_RDONLY);
88         EXPECT_LE(0, fd) << strerror(errno);
89
90         fd2 = dup(fd);
91
92         ASSERT_EQ(0, close(fd2)) << strerror(errno);
93         ASSERT_EQ(0, close(fd)) << strerror(errno);
94 }
95
96 /* 
97  * Some FUSE filesystem cache data internally and flush it on release.  Such
98  * filesystems may generate errors during release.  On Linux, these get
99  * returned by close(2).  However, POSIX does not require close(2) to return
100  * this error.  FreeBSD's fuse(4) should return EIO if it returns an error at
101  * all.
102  */
103 /* http://pubs.opengroup.org/onlinepubs/9699919799/functions/close.html */
104 TEST_F(Release, eio)
105 {
106         const char FULLPATH[] = "mountpoint/some_file.txt";
107         const char RELPATH[] = "some_file.txt";
108         uint64_t ino = 42;
109         int fd;
110
111         expect_lookup(RELPATH, ino, 1);
112         expect_open(ino, 0, 1);
113         expect_flush(ino, 1, ReturnErrno(0));
114         expect_release(ino, getpid(), O_WRONLY, EIO);
115         
116         fd = open(FULLPATH, O_WRONLY);
117         EXPECT_LE(0, fd) << strerror(errno);
118
119         ASSERT_TRUE(0 == close(fd) || errno == EIO) << strerror(errno);
120 }
121
122 /*
123  * FUSE_RELEASE should contain the same flags used for FUSE_OPEN
124  */
125 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236340 */
126 TEST_F(Release, DISABLED_flags)
127 {
128         const char FULLPATH[] = "mountpoint/some_file.txt";
129         const char RELPATH[] = "some_file.txt";
130         uint64_t ino = 42;
131         int fd;
132
133         expect_lookup(RELPATH, ino, 1);
134         expect_open(ino, 0, 1);
135         expect_flush(ino, 1, ReturnErrno(0));
136         expect_release(ino, getpid(), O_RDWR | O_APPEND, 0);
137         
138         fd = open(FULLPATH, O_RDWR | O_APPEND);
139         EXPECT_LE(0, fd) << strerror(errno);
140
141         ASSERT_EQ(0, close(fd)) << strerror(errno);
142 }
143
144 /*
145  * fuse(4) will issue multiple FUSE_OPEN operations for the same file if it's
146  * opened with different modes.  Each FUSE_OPEN should get its own
147  * FUSE_RELEASE.
148  */
149 TEST_F(Release, multiple_opens)
150 {
151         const char FULLPATH[] = "mountpoint/some_file.txt";
152         const char RELPATH[] = "some_file.txt";
153         uint64_t ino = 42;
154         int fd, fd2;
155
156         expect_lookup(RELPATH, ino, 2);
157         expect_open(ino, 0, 2);
158         expect_flush(ino, 2, ReturnErrno(0));
159         expect_release(ino, getpid(), O_RDONLY, 0);
160         
161         fd = open(FULLPATH, O_RDONLY);
162         EXPECT_LE(0, fd) << strerror(errno);
163
164         expect_release(ino, getpid(), O_WRONLY, 0);
165         fd2 = open(FULLPATH, O_WRONLY);
166         EXPECT_LE(0, fd2) << strerror(errno);
167
168         ASSERT_EQ(0, close(fd2)) << strerror(errno);
169         ASSERT_EQ(0, close(fd)) << strerror(errno);
170 }
171
172 TEST_F(Release, ok)
173 {
174         const char FULLPATH[] = "mountpoint/some_file.txt";
175         const char RELPATH[] = "some_file.txt";
176         uint64_t ino = 42;
177         int fd;
178
179         expect_lookup(RELPATH, ino, 1);
180         expect_open(ino, 0, 1);
181         expect_flush(ino, 1, ReturnErrno(0));
182         expect_release(ino, getpid(), O_RDONLY, 0);
183         
184         fd = open(FULLPATH, O_RDONLY);
185         EXPECT_LE(0, fd) << strerror(errno);
186
187         ASSERT_EQ(0, close(fd)) << strerror(errno);
188 }
189
190 /* When closing a file with a POSIX file lock, release should release the lock*/
191 TEST_F(ReleaseWithLocks, unlock_on_close)
192 {
193         const char FULLPATH[] = "mountpoint/some_file.txt";
194         const char RELPATH[] = "some_file.txt";
195         uint64_t ino = 42;
196         int fd;
197         struct flock fl;
198         pid_t pid = getpid();
199
200         expect_lookup(RELPATH, ino, 1);
201         expect_open(ino, 0, 1);
202         EXPECT_CALL(*m_mock, process(
203                 ResultOf([=](auto in) {
204                         return (in.header.opcode == FUSE_SETLK &&
205                                 in.header.nodeid == ino &&
206                                 in.body.setlk.fh == FH);
207                 }, Eq(true)),
208                 _)
209         ).WillOnce(Invoke(ReturnErrno(0)));
210         expect_flush(ino, 1, ReturnErrno(0));
211         expect_release(ino, static_cast<uint64_t>(pid), O_RDWR, 0);
212
213         fd = open(FULLPATH, O_RDWR);
214         ASSERT_LE(0, fd) << strerror(errno);
215         fl.l_start = 0;
216         fl.l_len = 0;
217         fl.l_pid = pid;
218         fl.l_type = F_RDLCK;
219         fl.l_whence = SEEK_SET;
220         fl.l_sysid = 0;
221         ASSERT_NE(-1, fcntl(fd, F_SETLKW, &fl)) << strerror(errno);
222
223         ASSERT_EQ(0, close(fd)) << strerror(errno);
224 }