]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tests/sys/fs/fusefs/utils.cc
fusefs: Allow update mounts
[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 <grp.h>
39 #include <pwd.h>
40 #include <semaphore.h>
41 #include <unistd.h>
42 }
43
44 #include <gtest/gtest.h>
45
46 #include "mockfs.hh"
47 #include "utils.hh"
48
49 using namespace testing;
50
51 /* Check that fusefs(4) is accessible and the current user can mount(2) */
52 void check_environment()
53 {
54         const char *devnode = "/dev/fuse";
55         const char *usermount_node = "vfs.usermount";
56         int usermount_val = 0;
57         size_t usermount_size = sizeof(usermount_val);
58         if (eaccess(devnode, R_OK | W_OK)) {
59                 if (errno == ENOENT) {
60                         GTEST_SKIP() << devnode << " does not exist";
61                 } else if (errno == EACCES) {
62                         GTEST_SKIP() << devnode <<
63                             " is not accessible by the current user";
64                 } else {
65                         GTEST_SKIP() << strerror(errno);
66                 }
67         }
68         sysctlbyname(usermount_node, &usermount_val, &usermount_size,
69                      NULL, 0);
70         if (geteuid() != 0 && !usermount_val)
71                 GTEST_SKIP() << "current user is not allowed to mount";
72 }
73
74 class FuseEnv: public Environment {
75         virtual void SetUp() {
76         }
77 };
78
79 void FuseTest::SetUp() {
80         const char *node = "vfs.maxbcachebuf";
81         int val = 0;
82         size_t size = sizeof(val);
83
84         /*
85          * XXX check_environment should be called from FuseEnv::SetUp, but
86          * can't due to https://github.com/google/googletest/issues/2189
87          */
88         check_environment();
89         if (IsSkipped())
90                 return;
91
92         ASSERT_EQ(0, sysctlbyname(node, &val, &size, NULL, 0))
93                 << strerror(errno);
94         m_maxbcachebuf = val;
95
96         try {
97                 m_mock = new MockFS(m_maxreadahead, m_allow_other,
98                         m_default_permissions, m_push_symlinks_in, m_ro,
99                         m_pm, m_init_flags, m_kernel_minor_version);
100                 /* 
101                  * FUSE_ACCESS is called almost universally.  Expecting it in
102                  * each test case would be super-annoying.  Instead, set a
103                  * default expectation for FUSE_ACCESS and return ENOSYS.
104                  *
105                  * Individual test cases can override this expectation since
106                  * googlemock evaluates expectations in LIFO order.
107                  */
108                 EXPECT_CALL(*m_mock, process(
109                         ResultOf([=](auto in) {
110                                 return (in->header.opcode == FUSE_ACCESS);
111                         }, Eq(true)),
112                         _)
113                 ).Times(AnyNumber())
114                 .WillRepeatedly(Invoke(ReturnErrno(ENOSYS)));
115         } catch (std::system_error err) {
116                 FAIL() << err.what();
117         }
118 }
119
120 void
121 FuseTest::expect_access(uint64_t ino, mode_t access_mode, int error)
122 {
123         EXPECT_CALL(*m_mock, process(
124                 ResultOf([=](auto in) {
125                         return (in->header.opcode == FUSE_ACCESS &&
126                                 in->header.nodeid == ino &&
127                                 in->body.access.mask == access_mode);
128                 }, Eq(true)),
129                 _)
130         ).WillOnce(Invoke(ReturnErrno(error)));
131 }
132
133 void
134 FuseTest::expect_destroy(int error)
135 {
136         EXPECT_CALL(*m_mock, process(
137                 ResultOf([=](auto in) {
138                         return (in->header.opcode == FUSE_DESTROY);
139                 }, Eq(true)),
140                 _)
141         ).WillOnce(Invoke( ReturnImmediate([&](auto in, auto out) {
142                 m_mock->m_quit = true;
143                 out->header.len = sizeof(out->header);
144                 out->header.unique = in->header.unique;
145                 out->header.error = -error;
146         })));
147 }
148
149 void
150 FuseTest::expect_flush(uint64_t ino, int times, ProcessMockerT r)
151 {
152         EXPECT_CALL(*m_mock, process(
153                 ResultOf([=](auto in) {
154                         return (in->header.opcode == FUSE_FLUSH &&
155                                 in->header.nodeid == ino);
156                 }, Eq(true)),
157                 _)
158         ).Times(times)
159         .WillRepeatedly(Invoke(r));
160 }
161
162 void
163 FuseTest::expect_forget(uint64_t ino, uint64_t nlookup)
164 {
165         EXPECT_CALL(*m_mock, process(
166                 ResultOf([=](auto in) {
167                         return (in->header.opcode == FUSE_FORGET &&
168                                 in->header.nodeid == ino &&
169                                 in->body.forget.nlookup == nlookup);
170                 }, Eq(true)),
171                 _)
172         ).WillOnce(Invoke([](auto in __unused, auto &out __unused) {
173                 /* FUSE_FORGET has no response! */
174         }));
175 }
176
177 void FuseTest::expect_getattr(uint64_t ino, uint64_t size)
178 {
179         EXPECT_CALL(*m_mock, process(
180                 ResultOf([=](auto in) {
181                         return (in->header.opcode == FUSE_GETATTR &&
182                                 in->header.nodeid == ino);
183                 }, Eq(true)),
184                 _)
185         ).WillOnce(Invoke(ReturnImmediate([=](auto i __unused, auto out) {
186                 SET_OUT_HEADER_LEN(out, attr);
187                 out->body.attr.attr.ino = ino;  // Must match nodeid
188                 out->body.attr.attr.mode = S_IFREG | 0644;
189                 out->body.attr.attr.size = size;
190                 out->body.attr.attr_valid = UINT64_MAX;
191         })));
192 }
193
194 void FuseTest::expect_lookup(const char *relpath, uint64_t ino, mode_t mode,
195         uint64_t size, int times, uint64_t attr_valid, uid_t uid, gid_t gid)
196 {
197         EXPECT_LOOKUP(1, relpath)
198         .Times(times)
199         .WillRepeatedly(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
200                 SET_OUT_HEADER_LEN(out, entry);
201                 out->body.entry.attr.mode = mode;
202                 out->body.entry.nodeid = ino;
203                 out->body.entry.attr.nlink = 1;
204                 out->body.entry.attr_valid = attr_valid;
205                 out->body.entry.attr.size = size;
206                 out->body.entry.attr.uid = uid;
207                 out->body.entry.attr.gid = gid;
208         })));
209 }
210
211 void FuseTest::expect_lookup_7_8(const char *relpath, uint64_t ino, mode_t mode,
212         uint64_t size, int times, uint64_t attr_valid, uid_t uid, gid_t gid)
213 {
214         EXPECT_LOOKUP(1, relpath)
215         .Times(times)
216         .WillRepeatedly(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
217                 SET_OUT_HEADER_LEN(out, entry_7_8);
218                 out->body.entry.attr.mode = mode;
219                 out->body.entry.nodeid = ino;
220                 out->body.entry.attr.nlink = 1;
221                 out->body.entry.attr_valid = attr_valid;
222                 out->body.entry.attr.size = size;
223                 out->body.entry.attr.uid = uid;
224                 out->body.entry.attr.gid = gid;
225         })));
226 }
227
228 void FuseTest::expect_open(uint64_t ino, uint32_t flags, int times)
229 {
230         EXPECT_CALL(*m_mock, process(
231                 ResultOf([=](auto in) {
232                         return (in->header.opcode == FUSE_OPEN &&
233                                 in->header.nodeid == ino);
234                 }, Eq(true)),
235                 _)
236         ).Times(times)
237         .WillRepeatedly(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
238                 out->header.len = sizeof(out->header);
239                 SET_OUT_HEADER_LEN(out, open);
240                 out->body.open.fh = FH;
241                 out->body.open.open_flags = flags;
242         })));
243 }
244
245 void FuseTest::expect_opendir(uint64_t ino)
246 {
247         /* opendir(3) calls fstatfs */
248         EXPECT_CALL(*m_mock, process(
249                 ResultOf([](auto in) {
250                         return (in->header.opcode == FUSE_STATFS);
251                 }, Eq(true)),
252                 _)
253         ).WillRepeatedly(Invoke(ReturnImmediate([=](auto i __unused, auto out) {
254                 SET_OUT_HEADER_LEN(out, statfs);
255         })));
256
257         EXPECT_CALL(*m_mock, process(
258                 ResultOf([=](auto in) {
259                         return (in->header.opcode == FUSE_OPENDIR &&
260                                 in->header.nodeid == ino);
261                 }, Eq(true)),
262                 _)
263         ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
264                 out->header.len = sizeof(out->header);
265                 SET_OUT_HEADER_LEN(out, open);
266                 out->body.open.fh = FH;
267         })));
268 }
269
270 void FuseTest::expect_read(uint64_t ino, uint64_t offset, uint64_t isize,
271         uint64_t osize, const void *contents)
272 {
273         EXPECT_CALL(*m_mock, process(
274                 ResultOf([=](auto in) {
275                         return (in->header.opcode == FUSE_READ &&
276                                 in->header.nodeid == ino &&
277                                 in->body.read.fh == FH &&
278                                 in->body.read.offset == offset &&
279                                 in->body.read.size == isize);
280                 }, Eq(true)),
281                 _)
282         ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
283                 out->header.len = sizeof(struct fuse_out_header) + osize;
284                 memmove(out->body.bytes, contents, osize);
285         }))).RetiresOnSaturation();
286 }
287
288 void FuseTest::expect_release(uint64_t ino, uint64_t fh)
289 {
290         EXPECT_CALL(*m_mock, process(
291                 ResultOf([=](auto in) {
292                         return (in->header.opcode == FUSE_RELEASE &&
293                                 in->header.nodeid == ino &&
294                                 in->body.release.fh == fh);
295                 }, Eq(true)),
296                 _)
297         ).WillOnce(Invoke(ReturnErrno(0)));
298 }
299
300 void FuseTest::expect_releasedir(uint64_t ino, ProcessMockerT r)
301 {
302         EXPECT_CALL(*m_mock, process(
303                 ResultOf([=](auto in) {
304                         return (in->header.opcode == FUSE_RELEASEDIR &&
305                                 in->header.nodeid == ino &&
306                                 in->body.release.fh == FH);
307                 }, Eq(true)),
308                 _)
309         ).WillOnce(Invoke(r));
310 }
311
312 void FuseTest::expect_unlink(uint64_t parent, const char *path, int error)
313 {
314         EXPECT_CALL(*m_mock, process(
315                 ResultOf([=](auto in) {
316                         return (in->header.opcode == FUSE_UNLINK &&
317                                 0 == strcmp(path, in->body.unlink) &&
318                                 in->header.nodeid == parent);
319                 }, Eq(true)),
320                 _)
321         ).WillOnce(Invoke(ReturnErrno(error)));
322 }
323
324 void FuseTest::expect_write(uint64_t ino, uint64_t offset, uint64_t isize,
325         uint64_t osize, uint32_t flags, const void *contents)
326 {
327         EXPECT_CALL(*m_mock, process(
328                 ResultOf([=](auto in) {
329                         const char *buf = (const char*)in->body.bytes +
330                                 sizeof(struct fuse_write_in);
331                         bool pid_ok;
332
333                         if (in->body.write.write_flags & FUSE_WRITE_CACHE)
334                                 pid_ok = true;
335                         else
336                                 pid_ok = (pid_t)in->header.pid == getpid();
337
338                         return (in->header.opcode == FUSE_WRITE &&
339                                 in->header.nodeid == ino &&
340                                 in->body.write.fh == FH &&
341                                 in->body.write.offset == offset  &&
342                                 in->body.write.size == isize &&
343                                 pid_ok &&
344                                 in->body.write.write_flags == flags &&
345                                 0 == bcmp(buf, contents, isize));
346                 }, Eq(true)),
347                 _)
348         ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
349                 SET_OUT_HEADER_LEN(out, write);
350                 out->body.write.size = osize;
351         })));
352 }
353
354 void FuseTest::expect_write_7_8(uint64_t ino, uint64_t offset, uint64_t isize,
355         uint64_t osize, uint32_t flags, const void *contents)
356 {
357         EXPECT_CALL(*m_mock, process(
358                 ResultOf([=](auto in) {
359                         const char *buf = (const char*)in->body.bytes +
360                                 FUSE_COMPAT_WRITE_IN_SIZE;
361                         bool pid_ok = (pid_t)in->header.pid == getpid();
362                         return (in->header.opcode == FUSE_WRITE &&
363                                 in->header.nodeid == ino &&
364                                 in->body.write.fh == FH &&
365                                 in->body.write.offset == offset  &&
366                                 in->body.write.size == isize &&
367                                 pid_ok &&
368                                 in->body.write.write_flags == flags &&
369                                 0 == bcmp(buf, contents, isize));
370                 }, Eq(true)),
371                 _)
372         ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
373                 SET_OUT_HEADER_LEN(out, write);
374                 out->body.write.size = osize;
375         })));
376 }
377
378 void
379 get_unprivileged_id(uid_t *uid, gid_t *gid)
380 {
381         struct passwd *pw;
382         struct group *gr;
383
384         /* 
385          * First try "tests", Kyua's default unprivileged user.  XXX after
386          * GoogleTest gains a proper Kyua wrapper, get this with the Kyua API
387          */
388         pw = getpwnam("tests");
389         if (pw == NULL) {
390                 /* Fall back to "nobody" */
391                 pw = getpwnam("nobody");
392         }
393         if (pw == NULL)
394                 GTEST_SKIP() << "Test requires an unprivileged user";
395         /* Use group "nobody", which is Kyua's default unprivileged group */
396         gr = getgrnam("nobody");
397         if (gr == NULL)
398                 GTEST_SKIP() << "Test requires an unprivileged group";
399         *uid = pw->pw_uid;
400         *gid = gr->gr_gid;
401 }
402
403 void
404 FuseTest::fork(bool drop_privs, int *child_status,
405         std::function<void()> parent_func,
406         std::function<int()> child_func)
407 {
408         sem_t *sem;
409         int mprot = PROT_READ | PROT_WRITE;
410         int mflags = MAP_ANON | MAP_SHARED;
411         pid_t child;
412         uid_t uid;
413         gid_t gid;
414         
415         if (drop_privs) {
416                 get_unprivileged_id(&uid, &gid);
417                 if (IsSkipped())
418                         return;
419         }
420
421         sem = (sem_t*)mmap(NULL, sizeof(*sem), mprot, mflags, -1, 0);
422         ASSERT_NE(MAP_FAILED, sem) << strerror(errno);
423         ASSERT_EQ(0, sem_init(sem, 1, 0)) << strerror(errno);
424
425         if ((child = ::fork()) == 0) {
426                 /* In child */
427                 int err = 0;
428
429                 if (sem_wait(sem)) {
430                         perror("sem_wait");
431                         err = 1;
432                         goto out;
433                 }
434
435                 if (drop_privs && 0 != setegid(gid)) {
436                         perror("setegid");
437                         err = 1;
438                         goto out;
439                 }
440                 if (drop_privs && 0 != setreuid(-1, uid)) {
441                         perror("setreuid");
442                         err = 1;
443                         goto out;
444                 }
445                 err = child_func();
446
447 out:
448                 sem_destroy(sem);
449                 _exit(err);
450         } else if (child > 0) {
451                 /* 
452                  * In parent.  Cleanup must happen here, because it's still
453                  * privileged.
454                  */
455                 m_mock->m_child_pid = child;
456                 ASSERT_NO_FATAL_FAILURE(parent_func());
457
458                 /* Signal the child process to go */
459                 ASSERT_EQ(0, sem_post(sem)) << strerror(errno);
460
461                 ASSERT_LE(0, wait(child_status)) << strerror(errno);
462         } else {
463                 FAIL() << strerror(errno);
464         }
465         munmap(sem, sizeof(*sem));
466         return;
467 }
468
469 static void usage(char* progname) {
470         fprintf(stderr, "Usage: %s [-v]\n\t-v increase verbosity\n", progname);
471         exit(2);
472 }
473
474 int main(int argc, char **argv) {
475         int ch;
476         FuseEnv *fuse_env = new FuseEnv;
477
478         InitGoogleTest(&argc, argv);
479         AddGlobalTestEnvironment(fuse_env);
480
481         while ((ch = getopt(argc, argv, "v")) != -1) {
482                 switch (ch) {
483                         case 'v':
484                                 verbosity++;
485                                 break;
486                         default:
487                                 usage(argv[0]);
488                                 break;
489                 }
490         }
491
492         return (RUN_ALL_TESTS());
493 }