]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tests/sys/fs/fuse/rename.cc
fuse(4): add tests for the FOPEN_KEEP_CACHE option
[FreeBSD/FreeBSD.git] / tests / sys / fs / fuse / rename.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 <stdlib.h>
33 #include <unistd.h>
34 }
35
36 #include "mockfs.hh"
37 #include "utils.hh"
38
39 using namespace testing;
40
41 class Rename: public FuseTest {
42         public:
43         int tmpfd = -1;
44         char tmpfile[80] = "/tmp/fuse.rename.XXXXXX";
45
46         virtual void TearDown() {
47                 if (tmpfd >= 0) {
48                         close(tmpfd);
49                         unlink(tmpfile);
50                 }
51
52                 FuseTest::TearDown();
53         }
54 };
55
56 // EINVAL, dst is subdir of src
57 TEST_F(Rename, einval)
58 {
59         const char FULLDST[] = "mountpoint/src/dst";
60         const char RELDST[] = "dst";
61         const char FULLSRC[] = "mountpoint/src";
62         const char RELSRC[] = "src";
63         uint64_t src_ino = 42;
64
65         expect_lookup(RELSRC, src_ino, S_IFDIR | 0755, 0, 2);
66         EXPECT_LOOKUP(src_ino, RELDST).WillOnce(Invoke(ReturnErrno(ENOENT)));
67
68         ASSERT_NE(0, rename(FULLSRC, FULLDST));
69         ASSERT_EQ(EINVAL, errno);
70 }
71
72 // source does not exist
73 TEST_F(Rename, enoent)
74 {
75         const char FULLDST[] = "mountpoint/dst";
76         const char FULLSRC[] = "mountpoint/src";
77         const char RELSRC[] = "src";
78         // FUSE hardcodes the mountpoint to inode 1
79
80         EXPECT_LOOKUP(1, RELSRC).WillOnce(Invoke(ReturnErrno(ENOENT)));
81
82         ASSERT_NE(0, rename(FULLSRC, FULLDST));
83         ASSERT_EQ(ENOENT, errno);
84 }
85
86 /*
87  * Renaming a file after FUSE_LOOKUP returned a negative cache entry for dst
88  */
89 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236231 */
90 TEST_F(Rename, DISABLED_entry_cache_negative)
91 {
92         const char FULLDST[] = "mountpoint/dst";
93         const char RELDST[] = "dst";
94         const char FULLSRC[] = "mountpoint/src";
95         const char RELSRC[] = "src";
96         // FUSE hardcodes the mountpoint to inode 1
97         uint64_t dst_dir_ino = 1;
98         uint64_t ino = 42;
99         /* 
100          * Set entry_valid = 0 because this test isn't concerned with whether
101          * or not we actually cache negative entries, only with whether we
102          * interpret negative cache responses correctly.
103          */
104         struct timespec entry_valid = {.tv_sec = 0, .tv_nsec = 0};
105
106         expect_lookup(RELSRC, ino, S_IFREG | 0644, 0, 1);
107         /* LOOKUP returns a negative cache entry for dst */
108         EXPECT_LOOKUP(1, RELDST).WillOnce(ReturnNegativeCache(&entry_valid));
109
110         EXPECT_CALL(*m_mock, process(
111                 ResultOf([=](auto in) {
112                         const char *src = (const char*)in->body.bytes +
113                                 sizeof(fuse_rename_in);
114                         const char *dst = src + strlen(src) + 1;
115                         return (in->header.opcode == FUSE_RENAME &&
116                                 in->body.rename.newdir == dst_dir_ino &&
117                                 (0 == strcmp(RELDST, dst)) &&
118                                 (0 == strcmp(RELSRC, src)));
119                 }, Eq(true)),
120                 _)
121         ).WillOnce(Invoke(ReturnErrno(0)));
122
123         ASSERT_EQ(0, rename(FULLSRC, FULLDST)) << strerror(errno);
124 }
125
126 /*
127  * Renaming a file should purge any negative namecache entries for the dst
128  */
129 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236231 */
130 TEST_F(Rename, DISABLED_entry_cache_negative_purge)
131 {
132         const char FULLDST[] = "mountpoint/dst";
133         const char RELDST[] = "dst";
134         const char FULLSRC[] = "mountpoint/src";
135         const char RELSRC[] = "src";
136         // FUSE hardcodes the mountpoint to inode 1
137         uint64_t dst_dir_ino = 1;
138         uint64_t ino = 42;
139         /* 
140          * Set entry_valid = 0 because this test isn't concerned with whether
141          * or not we actually cache negative entries, only with whether we
142          * interpret negative cache responses correctly.
143          */
144         struct timespec entry_valid = {.tv_sec = 0, .tv_nsec = 0};
145
146         expect_lookup(RELSRC, ino, S_IFREG | 0644, 0, 1);
147         /* LOOKUP returns a negative cache entry for dst */
148         EXPECT_LOOKUP(1, RELDST).WillOnce(ReturnNegativeCache(&entry_valid))
149         .RetiresOnSaturation();
150
151         EXPECT_CALL(*m_mock, process(
152                 ResultOf([=](auto in) {
153                         const char *src = (const char*)in->body.bytes +
154                                 sizeof(fuse_rename_in);
155                         const char *dst = src + strlen(src) + 1;
156                         return (in->header.opcode == FUSE_RENAME &&
157                                 in->body.rename.newdir == dst_dir_ino &&
158                                 (0 == strcmp(RELDST, dst)) &&
159                                 (0 == strcmp(RELSRC, src)));
160                 }, Eq(true)),
161                 _)
162         ).WillOnce(Invoke(ReturnErrno(0)));
163
164         ASSERT_EQ(0, rename(FULLSRC, FULLDST)) << strerror(errno);
165
166         /* Finally, a subsequent lookup should query the daemon */
167         expect_lookup(RELSRC, ino, S_IFREG | 0644, 0, 1);
168
169         ASSERT_EQ(0, access(FULLDST, F_OK)) << strerror(errno);
170 }
171
172 TEST_F(Rename, exdev)
173 {
174         const char FULLB[] = "mountpoint/src";
175         const char RELB[] = "src";
176         // FUSE hardcodes the mountpoint to inode 1
177         uint64_t b_ino = 42;
178
179         tmpfd = mkstemp(tmpfile);
180         ASSERT_LE(0, tmpfd) << strerror(errno);
181
182         expect_lookup(RELB, b_ino, S_IFREG | 0644, 0, 2);
183
184         ASSERT_NE(0, rename(tmpfile, FULLB));
185         ASSERT_EQ(EXDEV, errno);
186
187         ASSERT_NE(0, rename(FULLB, tmpfile));
188         ASSERT_EQ(EXDEV, errno);
189 }
190
191 TEST_F(Rename, ok)
192 {
193         const char FULLDST[] = "mountpoint/dst";
194         const char RELDST[] = "dst";
195         const char FULLSRC[] = "mountpoint/src";
196         const char RELSRC[] = "src";
197         // FUSE hardcodes the mountpoint to inode 1
198         uint64_t dst_dir_ino = 1;
199         uint64_t ino = 42;
200
201         expect_lookup(RELSRC, ino, S_IFREG | 0644, 0, 1);
202         EXPECT_LOOKUP(1, RELDST).WillOnce(Invoke(ReturnErrno(ENOENT)));
203
204         EXPECT_CALL(*m_mock, process(
205                 ResultOf([=](auto in) {
206                         const char *src = (const char*)in->body.bytes +
207                                 sizeof(fuse_rename_in);
208                         const char *dst = src + strlen(src) + 1;
209                         return (in->header.opcode == FUSE_RENAME &&
210                                 in->body.rename.newdir == dst_dir_ino &&
211                                 (0 == strcmp(RELDST, dst)) &&
212                                 (0 == strcmp(RELSRC, src)));
213                 }, Eq(true)),
214                 _)
215         ).WillOnce(Invoke(ReturnErrno(0)));
216
217         ASSERT_EQ(0, rename(FULLSRC, FULLDST)) << strerror(errno);
218 }
219
220 // Rename overwrites an existing destination file
221 TEST_F(Rename, overwrite)
222 {
223         const char FULLDST[] = "mountpoint/dst";
224         const char RELDST[] = "dst";
225         const char FULLSRC[] = "mountpoint/src";
226         const char RELSRC[] = "src";
227         // The inode of the already-existing destination file
228         uint64_t dst_ino = 2;
229         // FUSE hardcodes the mountpoint to inode 1
230         uint64_t dst_dir_ino = 1;
231         uint64_t ino = 42;
232
233         expect_lookup(RELSRC, ino, S_IFREG | 0644, 0, 1);
234         expect_lookup(RELDST, dst_ino, S_IFREG | 0644, 0, 1);
235         EXPECT_CALL(*m_mock, process(
236                 ResultOf([=](auto in) {
237                         const char *src = (const char*)in->body.bytes +
238                                 sizeof(fuse_rename_in);
239                         const char *dst = src + strlen(src) + 1;
240                         return (in->header.opcode == FUSE_RENAME &&
241                                 in->body.rename.newdir == dst_dir_ino &&
242                                 (0 == strcmp(RELDST, dst)) &&
243                                 (0 == strcmp(RELSRC, src)));
244                 }, Eq(true)),
245                 _)
246         ).WillOnce(Invoke(ReturnErrno(0)));
247
248         ASSERT_EQ(0, rename(FULLSRC, FULLDST)) << strerror(errno);
249 }