]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tests/sys/fs/fusefs/utils.cc
fusefs: correctly return EROFS from VOP_ACCESS
[FreeBSD/FreeBSD.git] / tests / sys / fs / fusefs / utils.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 <sys/param.h>
33 #include <sys/mman.h>
34 #include <sys/module.h>
35 #include <sys/sysctl.h>
36 #include <sys/wait.h>
37
38 #include <pwd.h>
39 #include <semaphore.h>
40 #include <unistd.h>
41 }
42
43 #include <gtest/gtest.h>
44
45 #include "mockfs.hh"
46 #include "utils.hh"
47
48 using namespace testing;
49
50 /* Check that fusefs(4) is accessible and the current user can mount(2) */
51 void check_environment()
52 {
53         const char *devnode = "/dev/fuse";
54         const char *usermount_node = "vfs.usermount";
55         int usermount_val = 0;
56         size_t usermount_size = sizeof(usermount_val);
57         if (eaccess(devnode, R_OK | W_OK)) {
58                 if (errno == ENOENT) {
59                         GTEST_SKIP() << devnode << " does not exist";
60                 } else if (errno == EACCES) {
61                         GTEST_SKIP() << devnode <<
62                             " is not accessible by the current user";
63                 } else {
64                         GTEST_SKIP() << strerror(errno);
65                 }
66         }
67         sysctlbyname(usermount_node, &usermount_val, &usermount_size,
68                      NULL, 0);
69         if (geteuid() != 0 && !usermount_val)
70                 GTEST_SKIP() << "current user is not allowed to mount";
71 }
72
73 class FuseEnv: public Environment {
74         virtual void SetUp() {
75         }
76 };
77
78 void FuseTest::SetUp() {
79         const char *node = "vfs.maxbcachebuf";
80         int val = 0;
81         size_t size = sizeof(val);
82
83         /*
84          * XXX check_environment should be called from FuseEnv::SetUp, but
85          * can't due to https://github.com/google/googletest/issues/2189
86          */
87         check_environment();
88         if (IsSkipped())
89                 return;
90
91         ASSERT_EQ(0, sysctlbyname(node, &val, &size, NULL, 0))
92                 << strerror(errno);
93         m_maxbcachebuf = val;
94
95         try {
96                 m_mock = new MockFS(m_maxreadahead, m_allow_other,
97                         m_default_permissions, m_push_symlinks_in, m_ro,
98                         m_init_flags);
99         } catch (std::system_error err) {
100                 FAIL() << err.what();
101         }
102 }
103
104 void
105 FuseTest::expect_access(uint64_t ino, mode_t access_mode, int error)
106 {
107         EXPECT_CALL(*m_mock, process(
108                 ResultOf([=](auto in) {
109                         return (in->header.opcode == FUSE_ACCESS &&
110                                 in->header.nodeid == ino &&
111                                 in->body.access.mask == access_mode);
112                 }, Eq(true)),
113                 _)
114         ).WillOnce(Invoke(ReturnErrno(error)));
115 }
116
117 void
118 FuseTest::expect_flush(uint64_t ino, int times, ProcessMockerT r)
119 {
120         EXPECT_CALL(*m_mock, process(
121                 ResultOf([=](auto in) {
122                         return (in->header.opcode == FUSE_FLUSH &&
123                                 in->header.nodeid == ino);
124                 }, Eq(true)),
125                 _)
126         ).Times(times)
127         .WillRepeatedly(Invoke(r));
128 }
129
130 void FuseTest::expect_getattr(uint64_t ino, uint64_t size)
131 {
132         /* Until the attr cache is working, we may send an additional GETATTR */
133         EXPECT_CALL(*m_mock, process(
134                 ResultOf([=](auto in) {
135                         return (in->header.opcode == FUSE_GETATTR &&
136                                 in->header.nodeid == ino);
137                 }, Eq(true)),
138                 _)
139         ).WillRepeatedly(Invoke(ReturnImmediate([=](auto i __unused, auto out) {
140                 SET_OUT_HEADER_LEN(out, attr);
141                 out->body.attr.attr.ino = ino;  // Must match nodeid
142                 out->body.attr.attr.mode = S_IFREG | 0644;
143                 out->body.attr.attr.size = size;
144                 out->body.attr.attr_valid = UINT64_MAX;
145         })));
146 }
147
148 void FuseTest::expect_lookup(const char *relpath, uint64_t ino, mode_t mode,
149         uint64_t size, int times)
150 {
151         EXPECT_LOOKUP(1, relpath)
152         .Times(times)
153         .WillRepeatedly(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
154                 SET_OUT_HEADER_LEN(out, entry);
155                 out->body.entry.attr.mode = mode;
156                 out->body.entry.nodeid = ino;
157                 out->body.entry.attr.nlink = 1;
158                 out->body.entry.attr_valid = UINT64_MAX;
159                 out->body.entry.attr.size = size;
160         })));
161 }
162
163 void FuseTest::expect_open(uint64_t ino, uint32_t flags, int times)
164 {
165         EXPECT_CALL(*m_mock, process(
166                 ResultOf([=](auto in) {
167                         return (in->header.opcode == FUSE_OPEN &&
168                                 in->header.nodeid == ino);
169                 }, Eq(true)),
170                 _)
171         ).Times(times)
172         .WillRepeatedly(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
173                 out->header.len = sizeof(out->header);
174                 SET_OUT_HEADER_LEN(out, open);
175                 out->body.open.fh = FH;
176                 out->body.open.open_flags = flags;
177         })));
178 }
179
180 void FuseTest::expect_opendir(uint64_t ino)
181 {
182         /* opendir(3) calls fstatfs */
183         EXPECT_CALL(*m_mock, process(
184                 ResultOf([](auto in) {
185                         return (in->header.opcode == FUSE_STATFS);
186                 }, Eq(true)),
187                 _)
188         ).WillRepeatedly(Invoke(ReturnImmediate([=](auto i __unused, auto out) {
189                 SET_OUT_HEADER_LEN(out, statfs);
190         })));
191
192         EXPECT_CALL(*m_mock, process(
193                 ResultOf([=](auto in) {
194                         return (in->header.opcode == FUSE_OPENDIR &&
195                                 in->header.nodeid == ino);
196                 }, Eq(true)),
197                 _)
198         ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
199                 out->header.len = sizeof(out->header);
200                 SET_OUT_HEADER_LEN(out, open);
201                 out->body.open.fh = FH;
202         })));
203 }
204
205 void FuseTest::expect_read(uint64_t ino, uint64_t offset, uint64_t isize,
206         uint64_t osize, const void *contents)
207 {
208         EXPECT_CALL(*m_mock, process(
209                 ResultOf([=](auto in) {
210                         return (in->header.opcode == FUSE_READ &&
211                                 in->header.nodeid == ino &&
212                                 in->body.read.fh == FH &&
213                                 in->body.read.offset == offset &&
214                                 in->body.read.size == isize);
215                 }, Eq(true)),
216                 _)
217         ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
218                 out->header.len = sizeof(struct fuse_out_header) + osize;
219                 memmove(out->body.bytes, contents, osize);
220         }))).RetiresOnSaturation();
221 }
222
223 void FuseTest::expect_release(uint64_t ino, uint64_t fh)
224 {
225         EXPECT_CALL(*m_mock, process(
226                 ResultOf([=](auto in) {
227                         return (in->header.opcode == FUSE_RELEASE &&
228                                 in->header.nodeid == ino &&
229                                 in->body.release.fh == fh);
230                 }, Eq(true)),
231                 _)
232         ).WillOnce(Invoke(ReturnErrno(0)));
233 }
234
235 void FuseTest::expect_releasedir(uint64_t ino, ProcessMockerT r)
236 {
237         EXPECT_CALL(*m_mock, process(
238                 ResultOf([=](auto in) {
239                         return (in->header.opcode == FUSE_RELEASEDIR &&
240                                 in->header.nodeid == ino &&
241                                 in->body.release.fh == FH);
242                 }, Eq(true)),
243                 _)
244         ).WillOnce(Invoke(r));
245 }
246
247 void FuseTest::expect_write(uint64_t ino, uint64_t offset, uint64_t isize,
248         uint64_t osize, uint32_t flags, const void *contents)
249 {
250         EXPECT_CALL(*m_mock, process(
251                 ResultOf([=](auto in) {
252                         const char *buf = (const char*)in->body.bytes +
253                                 sizeof(struct fuse_write_in);
254                         bool pid_ok;
255
256                         if (in->body.write.write_flags & FUSE_WRITE_CACHE)
257                                 pid_ok = true;
258                         else
259                                 pid_ok = (pid_t)in->header.pid == getpid();
260
261                         return (in->header.opcode == FUSE_WRITE &&
262                                 in->header.nodeid == ino &&
263                                 in->body.write.fh == FH &&
264                                 in->body.write.offset == offset  &&
265                                 in->body.write.size == isize &&
266                                 pid_ok &&
267                                 in->body.write.write_flags == flags &&
268                                 0 == bcmp(buf, contents, isize));
269                 }, Eq(true)),
270                 _)
271         ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
272                 SET_OUT_HEADER_LEN(out, write);
273                 out->body.write.size = osize;
274         })));
275 }
276
277 static void
278 get_unprivileged_uid(uid_t *uid)
279 {
280         struct passwd *pw;
281
282         /* 
283          * First try "tests", Kyua's default unprivileged user.  XXX after
284          * GoogleTest gains a proper Kyua wrapper, get this with the Kyua API
285          */
286         pw = getpwnam("tests");
287         if (pw == NULL) {
288                 /* Fall back to "nobody" */
289                 pw = getpwnam("nobody");
290         }
291         if (pw == NULL)
292                 GTEST_SKIP() << "Test requires an unprivileged user";
293         *uid = pw->pw_uid;
294 }
295
296 void
297 FuseTest::fork(bool drop_privs, std::function<void()> parent_func,
298         std::function<int()> child_func)
299 {
300         sem_t *sem;
301         int mprot = PROT_READ | PROT_WRITE;
302         int mflags = MAP_ANON | MAP_SHARED;
303         pid_t child;
304         uid_t uid;
305         
306         if (drop_privs) {
307                 get_unprivileged_uid(&uid);
308                 if (IsSkipped())
309                         return;
310         }
311
312         sem = (sem_t*)mmap(NULL, sizeof(*sem), mprot, mflags, -1, 0);
313         ASSERT_NE(MAP_FAILED, sem) << strerror(errno);
314         ASSERT_EQ(0, sem_init(sem, 1, 0)) << strerror(errno);
315
316         if ((child = ::fork()) == 0) {
317                 /* In child */
318                 int err = 0;
319
320                 if (sem_wait(sem)) {
321                         perror("sem_wait");
322                         err = 1;
323                         goto out;
324                 }
325
326                 if (drop_privs && 0 != setreuid(-1, uid)) {
327                         perror("setreuid");
328                         err = 1;
329                         goto out;
330                 }
331                 err = child_func();
332
333 out:
334                 sem_destroy(sem);
335                 _exit(err);
336         } else if (child > 0) {
337                 int child_status;
338
339                 /* 
340                  * In parent.  Cleanup must happen here, because it's still
341                  * privileged.
342                  */
343                 m_mock->m_child_pid = child;
344                 ASSERT_NO_FATAL_FAILURE(parent_func());
345
346                 /* Signal the child process to go */
347                 ASSERT_EQ(0, sem_post(sem)) << strerror(errno);
348
349                 ASSERT_LE(0, wait(&child_status)) << strerror(errno);
350                 ASSERT_EQ(0, WEXITSTATUS(child_status));
351         } else {
352                 FAIL() << strerror(errno);
353         }
354         munmap(sem, sizeof(*sem));
355 }
356
357 static void usage(char* progname) {
358         fprintf(stderr, "Usage: %s [-v]\n\t-v increase verbosity\n", progname);
359         exit(2);
360 }
361
362 int main(int argc, char **argv) {
363         int ch;
364         FuseEnv *fuse_env = new FuseEnv;
365
366         InitGoogleTest(&argc, argv);
367         AddGlobalTestEnvironment(fuse_env);
368
369         while ((ch = getopt(argc, argv, "v")) != -1) {
370                 switch (ch) {
371                         case 'v':
372                                 verbosity++;
373                                 break;
374                         default:
375                                 usage(argv[0]);
376                                 break;
377                 }
378         }
379
380         return (RUN_ALL_TESTS());
381 }