]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tests/sys/fs/fusefs/mknod.cc
fusefs: adapt the tests to the fuse => fusefs rename
[FreeBSD/FreeBSD.git] / tests / sys / fs / fusefs / 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         FuseTest::SetUp();
46
47         if (geteuid() != 0) {
48                 GTEST_SKIP() << "Only root may use most mknod(2) variations";
49         }
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 __unused, auto out) {
71                 SET_OUT_HEADER_LEN(out, create);
72                 out->body.create.entry.attr.mode = mode;
73                 out->body.create.entry.nodeid = ino;
74                 out->body.create.entry.entry_valid = UINT64_MAX;
75                 out->body.create.entry.attr_valid = UINT64_MAX;
76                 out->body.create.entry.attr.rdev = dev;
77         })));
78         EXPECT_EQ(0, mknod(FULLPATH, mode, dev)) << strerror(errno);
79 }
80
81 };
82
83 /* 
84  * mknod(2) should be able to create block devices on a FUSE filesystem.  Even
85  * though FreeBSD doesn't use block devices, this is useful when copying media
86  * from or preparing media for other operating systems.
87  */
88 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236236 */
89 TEST_F(Mknod, DISABLED_blk)
90 {
91         test_ok(S_IFBLK | 0755, 0xfe00); /* /dev/vda's device number on Linux */
92 }
93
94 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236236 */
95 TEST_F(Mknod, DISABLED_chr)
96 {
97         test_ok(S_IFCHR | 0755, 0x64);  /* /dev/fuse's device number */
98 }
99
100 /* 
101  * The daemon is responsible for checking file permissions (unless the
102  * default_permissions mount option was used)
103  */
104 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236236 */
105 TEST_F(Mknod, DISABLED_eperm)
106 {
107         const char FULLPATH[] = "mountpoint/some_file.txt";
108         const char RELPATH[] = "some_file.txt";
109         mode_t mode = S_IFIFO | 0755;
110
111         EXPECT_LOOKUP(1, RELPATH).WillOnce(Invoke(ReturnErrno(ENOENT)));
112
113         EXPECT_CALL(*m_mock, process(
114                 ResultOf([=](auto in) {
115                         const char *name = (const char*)in->body.bytes +
116                                 sizeof(fuse_mknod_in);
117                         return (in->header.opcode == FUSE_MKNOD &&
118                                 in->body.mknod.mode == mode &&
119                                 (0 == strcmp(RELPATH, name)));
120                 }, Eq(true)),
121                 _)
122         ).WillOnce(Invoke(ReturnErrno(EPERM)));
123         EXPECT_NE(0, mknod(FULLPATH, mode, 0));
124         EXPECT_EQ(EPERM, errno);
125 }
126
127
128 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236236 */
129 TEST_F(Mknod, DISABLED_fifo)
130 {
131         test_ok(S_IFIFO | 0755, 0);
132 }
133
134 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236236 */
135 TEST_F(Mknod, DISABLED_whiteout)
136 {
137         test_ok(S_IFWHT | 0755, 0);
138 }