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 Rename: public FuseTest {
44 char tmpfile[80] = "/tmp/fuse.rename.XXXXXX";
46 virtual void TearDown() {
56 // EINVAL, dst is subdir of src
57 TEST_F(Rename, einval)
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;
65 expect_lookup(RELSRC, src_ino, S_IFDIR | 0755, 0, 2);
66 EXPECT_LOOKUP(src_ino, RELDST).WillOnce(Invoke(ReturnErrno(ENOENT)));
68 ASSERT_NE(0, rename(FULLSRC, FULLDST));
69 ASSERT_EQ(EINVAL, errno);
72 // source does not exist
73 TEST_F(Rename, enoent)
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
80 EXPECT_LOOKUP(1, RELSRC).WillOnce(Invoke(ReturnErrno(ENOENT)));
82 ASSERT_NE(0, rename(FULLSRC, FULLDST));
83 ASSERT_EQ(ENOENT, errno);
87 * Renaming a file after FUSE_LOOKUP returned a negative cache entry for dst
89 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236231 */
90 TEST_F(Rename, DISABLED_entry_cache_negative)
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;
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.
104 struct timespec entry_valid = {.tv_sec = 0, .tv_nsec = 0};
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));
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)));
121 ).WillOnce(Invoke(ReturnErrno(0)));
123 ASSERT_EQ(0, rename(FULLSRC, FULLDST)) << strerror(errno);
127 * Renaming a file should purge any negative namecache entries for the dst
129 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236231 */
130 TEST_F(Rename, DISABLED_entry_cache_negative_purge)
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;
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.
144 struct timespec entry_valid = {.tv_sec = 0, .tv_nsec = 0};
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();
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)));
162 ).WillOnce(Invoke(ReturnErrno(0)));
164 ASSERT_EQ(0, rename(FULLSRC, FULLDST)) << strerror(errno);
166 /* Finally, a subsequent lookup should query the daemon */
167 expect_lookup(RELSRC, ino, S_IFREG | 0644, 0, 1);
169 ASSERT_EQ(0, access(FULLDST, F_OK)) << strerror(errno);
172 TEST_F(Rename, exdev)
174 const char FULLB[] = "mountpoint/src";
175 const char RELB[] = "src";
176 // FUSE hardcodes the mountpoint to inode 1
179 tmpfd = mkstemp(tmpfile);
180 ASSERT_LE(0, tmpfd) << strerror(errno);
182 expect_lookup(RELB, b_ino, S_IFREG | 0644, 0, 2);
184 ASSERT_NE(0, rename(tmpfile, FULLB));
185 ASSERT_EQ(EXDEV, errno);
187 ASSERT_NE(0, rename(FULLB, tmpfile));
188 ASSERT_EQ(EXDEV, errno);
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;
201 expect_lookup(RELSRC, ino, S_IFREG | 0644, 0, 1);
202 EXPECT_LOOKUP(1, RELDST).WillOnce(Invoke(ReturnErrno(ENOENT)));
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)));
215 ).WillOnce(Invoke(ReturnErrno(0)));
217 ASSERT_EQ(0, rename(FULLSRC, FULLDST)) << strerror(errno);
220 // Rename overwrites an existing destination file
221 TEST_F(Rename, overwrite)
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;
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)));
246 ).WillOnce(Invoke(ReturnErrno(0)));
248 ASSERT_EQ(0, rename(FULLSRC, FULLDST)) << strerror(errno);