2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2019 The FreeBSD Foundation
6 * This software was developed by BFF Storage Systems, LLC under sponsorship
7 * from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
38 using namespace testing;
40 class Mknod: public FuseTest {
44 virtual void SetUp() {
46 // TODO: With GoogleTest 1.8.2, use SKIP instead
47 FAIL() << "Only root may use most mknod(2) variations";
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";
58 EXPECT_LOOKUP(1, RELPATH).WillOnce(Invoke(ReturnErrno(ENOENT)));
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)));
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;
78 EXPECT_EQ(0, mknod(FULLPATH, mode, dev)) << strerror(errno);
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.
88 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236236 */
89 TEST_F(Mknod, DISABLED_blk)
91 test_ok(S_IFBLK | 0755, 0xfe00); /* /dev/vda's device number on Linux */
94 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236236 */
95 TEST_F(Mknod, DISABLED_chr)
97 test_ok(S_IFCHR | 0755, 0x64); /* /dev/fuse's device number */
101 * The daemon is responsible for checking file permissions (unless the
102 * default_permissions mount option was used)
104 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236236 */
105 TEST_F(Mknod, DISABLED_eperm)
107 const char FULLPATH[] = "mountpoint/some_file.txt";
108 const char RELPATH[] = "some_file.txt";
109 mode_t mode = S_IFIFO | 0755;
111 EXPECT_LOOKUP(1, RELPATH).WillOnce(Invoke(ReturnErrno(ENOENT)));
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)));
122 ).WillOnce(Invoke(ReturnErrno(EPERM)));
123 EXPECT_NE(0, mknod(FULLPATH, mode, 0));
124 EXPECT_EQ(EPERM, errno);
128 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236236 */
129 TEST_F(Mknod, DISABLED_fifo)
131 test_ok(S_IFIFO | 0755, 0);
134 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236236 */
135 TEST_F(Mknod, DISABLED_whiteout)
137 test_ok(S_IFWHT | 0755, 0);