]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tests/sys/fs/fusefs/nfs.cc
fusefs: make the tests more cplusplusy
[FreeBSD/FreeBSD.git] / tests / sys / fs / fusefs / nfs.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 /* This file tests functionality needed by NFS servers */
32 extern "C" {
33 #include <sys/param.h>
34 #include <sys/mount.h>
35
36 #include <dirent.h>
37 #include <fcntl.h>
38 #include <unistd.h>
39 }
40
41 #include "mockfs.hh"
42 #include "utils.hh"
43
44 using namespace std;
45 using namespace testing;
46
47
48 class Nfs: public FuseTest {
49 public:
50 virtual void SetUp() {
51         if (geteuid() != 0)
52                 GTEST_SKIP() << "This test requires a privileged user";
53         FuseTest::SetUp();
54 }
55 };
56
57 class Exportable: public Nfs {
58 public:
59 virtual void SetUp() {
60         m_init_flags = FUSE_EXPORT_SUPPORT;
61         Nfs::SetUp();
62 }
63 };
64
65 class Fhstat: public Exportable {};
66 class FhstatNotExportable: public Nfs {};
67 class Getfh: public Exportable {};
68 class Readdir: public Exportable {};
69
70 /* If the server returns a different generation number, then file is stale */
71 TEST_F(Fhstat, estale)
72 {
73         const char FULLPATH[] = "mountpoint/some_dir/.";
74         const char RELDIRPATH[] = "some_dir";
75         fhandle_t fhp;
76         struct stat sb;
77         const uint64_t ino = 42;
78         const mode_t mode = S_IFDIR | 0755;
79         Sequence seq;
80
81         EXPECT_LOOKUP(1, RELDIRPATH)
82         .InSequence(seq)
83         .WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
84                 SET_OUT_HEADER_LEN(out, entry);
85                 out.body.entry.attr.mode = mode;
86                 out.body.entry.nodeid = ino;
87                 out.body.entry.generation = 1;
88                 out.body.entry.attr_valid = UINT64_MAX;
89                 out.body.entry.entry_valid = 0;
90         })));
91
92         EXPECT_LOOKUP(ino, ".")
93         .InSequence(seq)
94         .WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
95                 SET_OUT_HEADER_LEN(out, entry);
96                 out.body.entry.attr.mode = mode;
97                 out.body.entry.nodeid = ino;
98                 out.body.entry.generation = 2;
99                 out.body.entry.attr_valid = UINT64_MAX;
100                 out.body.entry.entry_valid = 0;
101         })));
102
103         ASSERT_EQ(0, getfh(FULLPATH, &fhp)) << strerror(errno);
104         ASSERT_EQ(-1, fhstat(&fhp, &sb));
105         EXPECT_EQ(ESTALE, errno);
106 }
107
108 /* If we must lookup an entry from the server, send a LOOKUP request for "." */
109 TEST_F(Fhstat, lookup_dot)
110 {
111         const char FULLPATH[] = "mountpoint/some_dir/.";
112         const char RELDIRPATH[] = "some_dir";
113         fhandle_t fhp;
114         struct stat sb;
115         const uint64_t ino = 42;
116         const mode_t mode = S_IFDIR | 0755;
117         const uid_t uid = 12345;
118
119         EXPECT_LOOKUP(1, RELDIRPATH)
120         .WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
121                 SET_OUT_HEADER_LEN(out, entry);
122                 out.body.entry.attr.mode = mode;
123                 out.body.entry.nodeid = ino;
124                 out.body.entry.generation = 1;
125                 out.body.entry.attr.uid = uid;
126                 out.body.entry.attr_valid = UINT64_MAX;
127                 out.body.entry.entry_valid = 0;
128         })));
129
130         EXPECT_LOOKUP(ino, ".")
131         .WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
132                 SET_OUT_HEADER_LEN(out, entry);
133                 out.body.entry.attr.mode = mode;
134                 out.body.entry.nodeid = ino;
135                 out.body.entry.generation = 1;
136                 out.body.entry.attr.uid = uid;
137                 out.body.entry.attr_valid = UINT64_MAX;
138                 out.body.entry.entry_valid = 0;
139         })));
140
141         ASSERT_EQ(0, getfh(FULLPATH, &fhp)) << strerror(errno);
142         ASSERT_EQ(0, fhstat(&fhp, &sb)) << strerror(errno);
143         EXPECT_EQ(uid, sb.st_uid);
144         EXPECT_EQ(mode, sb.st_mode);
145 }
146
147 /* Use a file handle whose entry is still cached */
148 /* 
149  * Disabled because fuse_vfsop_vget doesn't yet check the entry cache.  No PR
150  * because that's a feature request, not a bug
151  */
152 TEST_F(Fhstat, DISABLED_cached)
153 {
154         const char FULLPATH[] = "mountpoint/some_dir/.";
155         const char RELDIRPATH[] = "some_dir";
156         fhandle_t fhp;
157         struct stat sb;
158         const uint64_t ino = 42;
159         const mode_t mode = S_IFDIR | 0755;
160         const uid_t uid = 12345;
161
162         EXPECT_LOOKUP(1, RELDIRPATH)
163         .WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
164                 SET_OUT_HEADER_LEN(out, entry);
165                 out.body.entry.attr.mode = mode;
166                 out.body.entry.nodeid = ino;
167                 out.body.entry.generation = 1;
168                 out.body.entry.attr.uid = uid;
169                 out.body.entry.attr_valid = UINT64_MAX;
170                 out.body.entry.entry_valid = UINT64_MAX;
171         })));
172
173         ASSERT_EQ(0, getfh(FULLPATH, &fhp)) << strerror(errno);
174         ASSERT_EQ(0, fhstat(&fhp, &sb)) << strerror(errno);
175         EXPECT_EQ(uid, sb.st_uid);
176         EXPECT_EQ(mode, sb.st_mode);
177 }
178
179 /* 
180  * If the server doesn't set FUSE_EXPORT_SUPPORT, then we can't do NFS-style
181  * lookups
182  */
183 TEST_F(FhstatNotExportable, lookup_dot)
184 {
185         const char FULLPATH[] = "mountpoint/some_dir/.";
186         const char RELDIRPATH[] = "some_dir";
187         fhandle_t fhp;
188         const uint64_t ino = 42;
189         const mode_t mode = S_IFDIR | 0755;
190
191         EXPECT_LOOKUP(1, RELDIRPATH)
192         .WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
193                 SET_OUT_HEADER_LEN(out, entry);
194                 out.body.entry.attr.mode = mode;
195                 out.body.entry.nodeid = ino;
196                 out.body.entry.generation = 1;
197                 out.body.entry.attr_valid = UINT64_MAX;
198                 out.body.entry.entry_valid = 0;
199         })));
200
201         ASSERT_EQ(-1, getfh(FULLPATH, &fhp));
202         ASSERT_EQ(EOPNOTSUPP, errno);
203 }
204
205 /* FreeBSD's fid struct doesn't have enough space for 64-bit generations */
206 TEST_F(Getfh, eoverflow)
207 {
208         const char FULLPATH[] = "mountpoint/some_dir/.";
209         const char RELDIRPATH[] = "some_dir";
210         fhandle_t fhp;
211         uint64_t ino = 42;
212
213         EXPECT_LOOKUP(1, RELDIRPATH)
214         .WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
215                 SET_OUT_HEADER_LEN(out, entry);
216                 out.body.entry.attr.mode = S_IFDIR | 0755;
217                 out.body.entry.nodeid = ino;
218                 out.body.entry.generation = (uint64_t)UINT32_MAX + 1;
219                 out.body.entry.attr_valid = UINT64_MAX;
220                 out.body.entry.entry_valid = UINT64_MAX;
221         })));
222
223         ASSERT_NE(0, getfh(FULLPATH, &fhp));
224         EXPECT_EQ(EOVERFLOW, errno);
225 }
226
227 /* Get an NFS file handle */
228 TEST_F(Getfh, ok)
229 {
230         const char FULLPATH[] = "mountpoint/some_dir/.";
231         const char RELDIRPATH[] = "some_dir";
232         fhandle_t fhp;
233         uint64_t ino = 42;
234
235         EXPECT_LOOKUP(1, RELDIRPATH)
236         .WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
237                 SET_OUT_HEADER_LEN(out, entry);
238                 out.body.entry.attr.mode = S_IFDIR | 0755;
239                 out.body.entry.nodeid = ino;
240                 out.body.entry.attr_valid = UINT64_MAX;
241                 out.body.entry.entry_valid = UINT64_MAX;
242         })));
243
244         ASSERT_EQ(0, getfh(FULLPATH, &fhp)) << strerror(errno);
245 }
246
247 /* 
248  * Call readdir via a file handle.
249  *
250  * This is how a userspace nfs server like nfs-ganesha or unfs3 would call
251  * readdir.  The in-kernel NFS server never does any equivalent of open.  I
252  * haven't discovered a way to mimic nfsd's behavior short of actually running
253  * nfsd.
254  */
255 TEST_F(Readdir, getdirentries)
256 {
257         const char FULLPATH[] = "mountpoint/some_dir";
258         const char RELPATH[] = "some_dir";
259         uint64_t ino = 42;
260         mode_t mode = S_IFDIR | 0755;
261         fhandle_t fhp;
262         int fd;
263         char buf[8192];
264         ssize_t r;
265
266         EXPECT_LOOKUP(1, RELPATH)
267         .WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
268                 SET_OUT_HEADER_LEN(out, entry);
269                 out.body.entry.attr.mode = mode;
270                 out.body.entry.nodeid = ino;
271                 out.body.entry.generation = 1;
272                 out.body.entry.attr_valid = UINT64_MAX;
273                 out.body.entry.entry_valid = 0;
274         })));
275
276         EXPECT_LOOKUP(ino, ".")
277         .WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
278                 SET_OUT_HEADER_LEN(out, entry);
279                 out.body.entry.attr.mode = mode;
280                 out.body.entry.nodeid = ino;
281                 out.body.entry.generation = 1;
282                 out.body.entry.attr_valid = UINT64_MAX;
283                 out.body.entry.entry_valid = 0;
284         })));
285
286         expect_opendir(ino);
287
288         EXPECT_CALL(*m_mock, process(
289                 ResultOf([=](auto in) {
290                         return (in.header.opcode == FUSE_READDIR &&
291                                 in.header.nodeid == ino &&
292                                 in.body.readdir.size == sizeof(buf));
293                 }, Eq(true)),
294                 _)
295         ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
296                 out.header.error = 0;
297                 out.header.len = sizeof(out.header);
298         })));
299
300         ASSERT_EQ(0, getfh(FULLPATH, &fhp)) << strerror(errno);
301         fd = fhopen(&fhp, O_DIRECTORY);
302         ASSERT_LE(0, fd) << strerror(errno);
303         r = getdirentries(fd, buf, sizeof(buf), 0);
304         ASSERT_EQ(0, r) << strerror(errno);
305
306         /* Deliberately leak fd.  RELEASEDIR will be tested separately */
307 }