]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tests/sys/fs/fuse/mknod.cc
fuse(4): add tests for FUSE_INTERRUPT
[FreeBSD/FreeBSD.git] / tests / sys / fs / fuse / mknod.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 <fcntl.h>
33 }
34
35 #include "mockfs.hh"
36 #include "utils.hh"
37
38 using namespace testing;
39
40 class Mknod: public FuseTest {
41
42 public:
43
44 virtual void SetUp() {
45         if (geteuid() != 0) {
46                 // TODO: With GoogleTest 1.8.2, use SKIP instead
47                 FAIL() << "Only root may use most mknod(2) variations";
48         }
49         FuseTest::SetUp();
50 }
51
52 /* Test an OK creation of a file with the given mode and device number */
53 void test_ok(mode_t mode, dev_t dev) {
54         const char FULLPATH[] = "mountpoint/some_file.txt";
55         const char RELPATH[] = "some_file.txt";
56         uint64_t ino = 42;
57
58         EXPECT_LOOKUP(1, RELPATH).WillOnce(Invoke(ReturnErrno(ENOENT)));
59
60         EXPECT_CALL(*m_mock, process(
61                 ResultOf([=](auto in) {
62                         const char *name = (const char*)in->body.bytes +
63                                 sizeof(fuse_mknod_in);
64                         return (in->header.opcode == FUSE_MKNOD &&
65                                 in->body.mknod.mode == mode &&
66                                 in->body.mknod.rdev == dev &&
67                                 (0 == strcmp(RELPATH, name)));
68                 }, Eq(true)),
69                 _)
70         ).WillOnce(Invoke(ReturnImmediate([=](auto in, auto out) {
71                 out->header.unique = in->header.unique;
72                 SET_OUT_HEADER_LEN(out, create);
73                 out->body.create.entry.attr.mode = mode;
74                 out->body.create.entry.nodeid = ino;
75                 out->body.create.entry.entry_valid = UINT64_MAX;
76                 out->body.create.entry.attr_valid = UINT64_MAX;
77                 out->body.create.entry.attr.rdev = dev;
78         })));
79         EXPECT_EQ(0, mknod(FULLPATH, mode, dev)) << strerror(errno);
80 }
81
82 };
83
84 /* 
85  * mknod(2) should be able to create block devices on a FUSE filesystem.  Even
86  * though FreeBSD doesn't use block devices, this is useful when copying media
87  * from or preparing media for other operating systems.
88  */
89 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236236 */
90 TEST_F(Mknod, DISABLED_blk)
91 {
92         test_ok(S_IFBLK | 0755, 0xfe00); /* /dev/vda's device number on Linux */
93 }
94
95 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236236 */
96 TEST_F(Mknod, DISABLED_chr)
97 {
98         test_ok(S_IFCHR | 0755, 0x64);  /* /dev/fuse's device number */
99 }
100
101 /* 
102  * The daemon is responsible for checking file permissions (unless the
103  * default_permissions mount option was used)
104  */
105 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236236 */
106 TEST_F(Mknod, DISABLED_eperm)
107 {
108         const char FULLPATH[] = "mountpoint/some_file.txt";
109         const char RELPATH[] = "some_file.txt";
110         mode_t mode = S_IFIFO | 0755;
111
112         EXPECT_LOOKUP(1, RELPATH).WillOnce(Invoke(ReturnErrno(ENOENT)));
113
114         EXPECT_CALL(*m_mock, process(
115                 ResultOf([=](auto in) {
116                         const char *name = (const char*)in->body.bytes +
117                                 sizeof(fuse_mknod_in);
118                         return (in->header.opcode == FUSE_MKNOD &&
119                                 in->body.mknod.mode == mode &&
120                                 (0 == strcmp(RELPATH, name)));
121                 }, Eq(true)),
122                 _)
123         ).WillOnce(Invoke(ReturnErrno(EPERM)));
124         EXPECT_NE(0, mknod(FULLPATH, mode, 0));
125         EXPECT_EQ(EPERM, errno);
126 }
127
128
129 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236236 */
130 TEST_F(Mknod, DISABLED_fifo)
131 {
132         test_ok(S_IFIFO | 0755, 0);
133 }
134
135 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236236 */
136 TEST_F(Mknod, DISABLED_whiteout)
137 {
138         test_ok(S_IFWHT | 0755, 0);
139 }