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;
42 class Readdir: public FuseTest {
44 void expect_lookup(const char *relpath, uint64_t ino)
46 FuseTest::expect_lookup(relpath, ino, S_IFDIR | 0755, 0, 1);
50 class Readdir_7_8: public Readdir {
52 virtual void SetUp() {
53 m_kernel_minor_version = 8;
57 void expect_lookup(const char *relpath, uint64_t ino)
59 FuseTest::expect_lookup_7_8(relpath, ino, S_IFDIR | 0755, 0, 1);
63 /* FUSE_READDIR returns nothing but "." and ".." */
66 const char FULLPATH[] = "mountpoint/some_dir";
67 const char RELPATH[] = "some_dir";
71 vector<struct dirent> ents(2);
72 vector<struct dirent> empty_ents(0);
73 const char *dot = ".";
74 const char *dotdot = "..";
76 expect_lookup(RELPATH, ino);
80 ents[0].d_namlen = strlen(dotdot);
81 ents[0].d_type = DT_DIR;
82 strncpy(ents[0].d_name, dotdot, ents[0].d_namlen);
85 ents[1].d_namlen = strlen(dot);
86 ents[1].d_type = DT_DIR;
87 strncpy(ents[1].d_name, dot, ents[1].d_namlen);
88 expect_readdir(ino, 0, ents);
89 expect_readdir(ino, 3000, empty_ents);
92 dir = opendir(FULLPATH);
93 ASSERT_NE(NULL, dir) << strerror(errno);
97 ASSERT_NE(NULL, de) << strerror(errno);
98 EXPECT_EQ(2ul, de->d_fileno);
100 * fuse(4) doesn't actually set d_off, which is ok for now because
103 //EXPECT_EQ(2000, de->d_off);
104 EXPECT_EQ(DT_DIR, de->d_type);
105 EXPECT_EQ(2, de->d_namlen);
106 EXPECT_EQ(0, strcmp("..", de->d_name));
110 ASSERT_NE(NULL, de) << strerror(errno);
111 EXPECT_EQ(3ul, de->d_fileno);
112 //EXPECT_EQ(3000, de->d_off);
113 EXPECT_EQ(DT_DIR, de->d_type);
114 EXPECT_EQ(1, de->d_namlen);
115 EXPECT_EQ(0, strcmp(".", de->d_name));
117 ASSERT_EQ(NULL, readdir(dir));
120 /* Deliberately leak dir. RELEASEDIR will be tested separately */
125 const char FULLPATH[] = "mountpoint/some_dir";
126 const char RELPATH[] = "some_dir";
131 expect_lookup(RELPATH, ino);
133 EXPECT_CALL(*m_mock, process(
134 ResultOf([=](auto in) {
135 return (in->header.opcode == FUSE_READDIR &&
136 in->header.nodeid == ino &&
137 in->body.readdir.offset == 0);
140 ).WillOnce(Invoke(ReturnErrno(EIO)));
143 dir = opendir(FULLPATH);
144 ASSERT_NE(NULL, dir) << strerror(errno);
149 ASSERT_EQ(EIO, errno);
151 /* Deliberately leak dir. RELEASEDIR will be tested separately */
154 /* getdirentries(2) can use a larger buffer size than readdir(3) */
155 TEST_F(Readdir, getdirentries)
157 const char FULLPATH[] = "mountpoint/some_dir";
158 const char RELPATH[] = "some_dir";
164 expect_lookup(RELPATH, ino);
167 EXPECT_CALL(*m_mock, process(
168 ResultOf([=](auto in) {
169 return (in->header.opcode == FUSE_READDIR &&
170 in->header.nodeid == ino &&
171 in->body.readdir.size == 8192);
174 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
175 out->header.error = 0;
176 out->header.len = sizeof(out->header);
179 fd = open(FULLPATH, O_DIRECTORY);
180 ASSERT_LE(0, fd) << strerror(errno);
181 r = getdirentries(fd, buf, sizeof(buf), 0);
182 ASSERT_EQ(0, r) << strerror(errno);
184 /* Deliberately leak fd. RELEASEDIR will be tested separately */
188 * Nothing bad should happen if getdirentries is called on two file descriptors
189 * which were concurrently open, but one has already been closed.
190 * This is a regression test for a specific bug dating from r238402.
192 TEST_F(Readdir, getdirentries_concurrent)
194 const char FULLPATH[] = "mountpoint/some_dir";
195 const char RELPATH[] = "some_dir";
201 FuseTest::expect_lookup(RELPATH, ino, S_IFDIR | 0755, 0, 2);
204 EXPECT_CALL(*m_mock, process(
205 ResultOf([=](auto in) {
206 return (in->header.opcode == FUSE_READDIR &&
207 in->header.nodeid == ino &&
208 in->body.readdir.size == 8192);
212 .WillRepeatedly(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
213 out->header.error = 0;
214 out->header.len = sizeof(out->header);
217 fd0 = open(FULLPATH, O_DIRECTORY);
218 ASSERT_LE(0, fd0) << strerror(errno);
220 fd1 = open(FULLPATH, O_DIRECTORY);
221 ASSERT_LE(0, fd1) << strerror(errno);
223 r = getdirentries(fd0, buf, sizeof(buf), 0);
224 ASSERT_EQ(0, r) << strerror(errno);
226 EXPECT_EQ(0, close(fd0)) << strerror(errno);
228 r = getdirentries(fd1, buf, sizeof(buf), 0);
229 ASSERT_EQ(0, r) << strerror(errno);
231 /* Deliberately leak fd1. */
235 * FUSE_READDIR returns nothing, not even "." and "..". This is legal, though
236 * the filesystem obviously won't be fully functional.
238 TEST_F(Readdir, nodots)
240 const char FULLPATH[] = "mountpoint/some_dir";
241 const char RELPATH[] = "some_dir";
245 expect_lookup(RELPATH, ino);
248 EXPECT_CALL(*m_mock, process(
249 ResultOf([=](auto in) {
250 return (in->header.opcode == FUSE_READDIR &&
251 in->header.nodeid == ino);
254 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
255 out->header.error = 0;
256 out->header.len = sizeof(out->header);
260 dir = opendir(FULLPATH);
261 ASSERT_NE(NULL, dir) << strerror(errno);
263 ASSERT_EQ(NULL, readdir(dir));
266 /* Deliberately leak dir. RELEASEDIR will be tested separately */
269 /* telldir(3) and seekdir(3) should work with fuse */
270 TEST_F(Readdir, seekdir)
272 const char FULLPATH[] = "mountpoint/some_dir";
273 const char RELPATH[] = "some_dir";
278 * use enough entries to be > 4096 bytes, so getdirentries must be
282 vector<struct dirent> ents0(122), ents1(102), ents2(30);
286 for (auto& it: ents0) {
287 snprintf(it.d_name, MAXNAMLEN, "file.%d", i);
289 it.d_off = (2 + i) * 1000;
290 it.d_namlen = strlen(it.d_name);
294 for (auto& it: ents1) {
295 snprintf(it.d_name, MAXNAMLEN, "file.%d", i);
297 it.d_off = (2 + i) * 1000;
298 it.d_namlen = strlen(it.d_name);
302 for (auto& it: ents2) {
303 snprintf(it.d_name, MAXNAMLEN, "file.%d", i);
305 it.d_off = (2 + i) * 1000;
306 it.d_namlen = strlen(it.d_name);
311 expect_lookup(RELPATH, ino);
314 expect_readdir(ino, 0, ents0);
315 expect_readdir(ino, 123000, ents1);
316 expect_readdir(ino, 225000, ents2);
319 dir = opendir(FULLPATH);
320 ASSERT_NE(NULL, dir) << strerror(errno);
322 for (i=0; i < 128; i++) {
325 ASSERT_NE(NULL, de) << strerror(errno);
326 EXPECT_EQ(2 + (ino_t)i, de->d_fileno);
328 bookmark = telldir(dir);
330 for (; i < 232; i++) {
333 ASSERT_NE(NULL, de) << strerror(errno);
334 EXPECT_EQ(2 + (ino_t)i, de->d_fileno);
337 seekdir(dir, bookmark);
339 ASSERT_NE(NULL, de) << strerror(errno);
340 EXPECT_EQ(130ul, de->d_fileno);
342 /* Deliberately leak dir. RELEASEDIR will be tested separately */
345 TEST_F(Readdir_7_8, nodots)
347 const char FULLPATH[] = "mountpoint/some_dir";
348 const char RELPATH[] = "some_dir";
352 expect_lookup(RELPATH, ino);
355 EXPECT_CALL(*m_mock, process(
356 ResultOf([=](auto in) {
357 return (in->header.opcode == FUSE_READDIR &&
358 in->header.nodeid == ino);
361 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
362 out->header.error = 0;
363 out->header.len = sizeof(out->header);
367 dir = opendir(FULLPATH);
368 ASSERT_NE(NULL, dir) << strerror(errno);
370 ASSERT_EQ(NULL, readdir(dir));
373 /* Deliberately leak dir. RELEASEDIR will be tested separately */