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 {
43 const static uint64_t FH = 0xdeadbeef1a7ebabe;
45 void expect_lookup(const char *relpath, uint64_t ino)
47 EXPECT_LOOKUP(1, relpath).WillRepeatedly(Invoke([=](auto in, auto out) {
48 out->header.unique = in->header.unique;
49 SET_OUT_HEADER_LEN(out, entry);
50 out->body.entry.attr.mode = S_IFDIR | 0755;
51 out->body.entry.nodeid = ino;
52 out->body.entry.attr_valid = UINT64_MAX;
56 void expect_opendir(uint64_t ino)
58 EXPECT_CALL(*m_mock, process(
59 ResultOf([](auto in) {
60 return (in->header.opcode == FUSE_STATFS);
63 ).WillOnce(Invoke([=](auto in, auto out) {
64 out->header.unique = in->header.unique;
65 SET_OUT_HEADER_LEN(out, statfs);
68 EXPECT_CALL(*m_mock, process(
69 ResultOf([=](auto in) {
70 return (in->header.opcode == FUSE_OPENDIR &&
71 in->header.nodeid == ino);
74 ).WillOnce(Invoke([=](auto in, auto out) {
75 out->header.unique = in->header.unique;
76 out->header.len = sizeof(out->header);
77 SET_OUT_HEADER_LEN(out, open);
78 out->body.open.fh = FH;
82 void expect_readdir(uint64_t ino, uint64_t off, vector<struct dirent> &ents)
84 EXPECT_CALL(*m_mock, process(
85 ResultOf([=](auto in) {
86 return (in->header.opcode == FUSE_READDIR &&
87 in->header.nodeid == ino &&
88 in->body.readdir.offset == off);
91 ).WillRepeatedly(Invoke([=](auto in, auto out) {
92 struct fuse_dirent *fde = (struct fuse_dirent*)out->body.bytes;
95 out->header.unique = in->header.unique;
96 out->header.error = 0;
99 for (const auto& it: ents) {
100 size_t entlen, entsize;
102 fde->ino = it.d_fileno;
104 fde->type = it.d_type;
105 fde->namelen = it.d_namlen;
106 strncpy(fde->name, it.d_name, it.d_namlen);
107 entlen = FUSE_NAME_OFFSET + fde->namelen;
108 entsize = FUSE_DIRENT_SIZE(fde);
110 * The FUSE protocol does not require zeroing out the
111 * unused portion of the name. But it's a good
112 * practice to prevent information disclosure to the
113 * FUSE client, even though the client is usually the
116 memset(fde->name + fde->namelen, 0, entsize - entlen);
117 if (out->header.len + entsize > in->body.read.size) {
118 printf("Overflow in readdir expectation: i=%d\n"
122 out->header.len += entsize;
123 fde = (struct fuse_dirent*)
124 ((long*)fde + entsize / sizeof(long));
127 out->header.len += sizeof(out->header);
133 /* FUSE_READDIR returns nothing but "." and ".." */
134 TEST_F(Readdir, dots)
136 const char FULLPATH[] = "mountpoint/some_dir";
137 const char RELPATH[] = "some_dir";
141 vector<struct dirent> ents(2);
142 vector<struct dirent> empty_ents(0);
143 const char *dot = ".";
144 const char *dotdot = "..";
146 expect_lookup(RELPATH, ino);
148 ents[0].d_fileno = 2;
149 ents[0].d_off = 2000;
150 ents[0].d_namlen = strlen(dotdot);
151 ents[0].d_type = DT_DIR;
152 strncpy(ents[0].d_name, dotdot, ents[0].d_namlen);
153 ents[1].d_fileno = 3;
154 ents[1].d_off = 3000;
155 ents[1].d_namlen = strlen(dot);
156 ents[1].d_type = DT_DIR;
157 strncpy(ents[1].d_name, dot, ents[1].d_namlen);
158 expect_readdir(ino, 0, ents);
159 expect_readdir(ino, 3000, empty_ents);
162 dir = opendir(FULLPATH);
163 ASSERT_NE(NULL, dir) << strerror(errno);
167 ASSERT_NE(NULL, de) << strerror(errno);
168 EXPECT_EQ(2ul, de->d_fileno);
170 * fuse(4) doesn't actually set d_off, which is ok for now because
173 //EXPECT_EQ(2000, de->d_off);
174 EXPECT_EQ(DT_DIR, de->d_type);
175 EXPECT_EQ(2, de->d_namlen);
176 EXPECT_EQ(0, strcmp("..", de->d_name));
180 ASSERT_NE(NULL, de) << strerror(errno);
181 EXPECT_EQ(3ul, de->d_fileno);
182 //EXPECT_EQ(3000, de->d_off);
183 EXPECT_EQ(DT_DIR, de->d_type);
184 EXPECT_EQ(1, de->d_namlen);
185 EXPECT_EQ(0, strcmp(".", de->d_name));
187 ASSERT_EQ(NULL, readdir(dir));
190 /* Deliberately leak dir. RELEASEDIR will be tested separately */
195 const char FULLPATH[] = "mountpoint/some_dir";
196 const char RELPATH[] = "some_dir";
201 expect_lookup(RELPATH, ino);
203 EXPECT_CALL(*m_mock, process(
204 ResultOf([=](auto in) {
205 return (in->header.opcode == FUSE_READDIR &&
206 in->header.nodeid == ino &&
207 in->body.readdir.offset == 0);
210 ).WillOnce(Invoke(ReturnErrno(EIO)));
213 dir = opendir(FULLPATH);
214 ASSERT_NE(NULL, dir) << strerror(errno);
219 ASSERT_EQ(EIO, errno);
221 /* Deliberately leak dir. RELEASEDIR will be tested separately */
225 * FUSE_READDIR returns nothing, not even "." and "..". This is legal, though
226 * the filesystem obviously won't be fully functional.
228 TEST_F(Readdir, nodots)
230 const char FULLPATH[] = "mountpoint/some_dir";
231 const char RELPATH[] = "some_dir";
235 expect_lookup(RELPATH, ino);
238 EXPECT_CALL(*m_mock, process(
239 ResultOf([=](auto in) {
240 return (in->header.opcode == FUSE_READDIR &&
241 in->header.nodeid == ino);
244 ).WillOnce(Invoke([=](auto in, auto out) {
245 out->header.unique = in->header.unique;
246 out->header.error = 0;
247 out->header.len = sizeof(out->header);
251 dir = opendir(FULLPATH);
252 ASSERT_NE(NULL, dir) << strerror(errno);
254 ASSERT_EQ(NULL, readdir(dir));
257 /* Deliberately leak dir. RELEASEDIR will be tested separately */
260 /* telldir(3) and seekdir(3) should work with fuse */
261 TEST_F(Readdir, seekdir)
263 const char FULLPATH[] = "mountpoint/some_dir";
264 const char RELPATH[] = "some_dir";
269 * use enough entries to be > 4096 bytes, so getdirentries must be
273 vector<struct dirent> ents0(122), ents1(102), ents2(30);
277 for (auto& it: ents0) {
278 snprintf(it.d_name, MAXNAMLEN, "file.%d", i);
280 it.d_off = (2 + i) * 1000;
281 it.d_namlen = strlen(it.d_name);
285 for (auto& it: ents1) {
286 snprintf(it.d_name, MAXNAMLEN, "file.%d", i);
288 it.d_off = (2 + i) * 1000;
289 it.d_namlen = strlen(it.d_name);
293 for (auto& it: ents2) {
294 snprintf(it.d_name, MAXNAMLEN, "file.%d", i);
296 it.d_off = (2 + i) * 1000;
297 it.d_namlen = strlen(it.d_name);
302 expect_lookup(RELPATH, ino);
305 expect_readdir(ino, 0, ents0);
306 expect_readdir(ino, 123000, ents1);
307 expect_readdir(ino, 225000, ents2);
310 dir = opendir(FULLPATH);
311 ASSERT_NE(NULL, dir) << strerror(errno);
313 for (i=0; i < 128; i++) {
316 ASSERT_NE(NULL, de) << strerror(errno);
317 EXPECT_EQ(2 + (ino_t)i, de->d_fileno);
319 bookmark = telldir(dir);
321 for (; i < 232; i++) {
324 ASSERT_NE(NULL, de) << strerror(errno);
325 EXPECT_EQ(2 + (ino_t)i, de->d_fileno);
328 seekdir(dir, bookmark);
330 ASSERT_NE(NULL, de) << strerror(errno);
331 EXPECT_EQ(130ul, de->d_fileno);
333 /* Deliberately leak dir. RELEASEDIR will be tested separately */