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 <dirent.h> 33 #include <fcntl.h> 34 } 35 36 #include "mockfs.hh" 37 #include "utils.hh" 38 39 using namespace testing; 40 41 class Opendir: public FuseTest { 42 public: 43 void expect_lookup(const char *relpath, uint64_t ino) 44 { 45 FuseTest::expect_lookup(relpath, ino, S_IFDIR | 0755, 0, 1); 46 } 47 48 void expect_opendir(uint64_t ino, uint32_t flags, ProcessMockerT r) 49 { 50 /* opendir(3) calls fstatfs */ 51 EXPECT_CALL(*m_mock, process( 52 ResultOf([](auto in) { 53 return (in.header.opcode == FUSE_STATFS); 54 }, Eq(true)), 55 _) 56 ).WillRepeatedly(Invoke(ReturnImmediate([=](auto i __unused, auto& out) { 57 SET_OUT_HEADER_LEN(out, statfs); 58 }))); 59 60 EXPECT_CALL(*m_mock, process( 61 ResultOf([=](auto in) { 62 return (in.header.opcode == FUSE_OPENDIR && 63 in.header.nodeid == ino && 64 in.body.opendir.flags == flags); 65 }, Eq(true)), 66 _) 67 ).WillOnce(Invoke(r)); 68 } 69 70 }; 71 72 73 /* 74 * The fuse daemon fails the request with enoent. This usually indicates a 75 * race condition: some other FUSE client removed the file in between when the 76 * kernel checked for it with lookup and tried to open it 77 */ 78 TEST_F(Opendir, enoent) 79 { 80 const char FULLPATH[] = "mountpoint/some_dir"; 81 const char RELPATH[] = "some_dir"; 82 uint64_t ino = 42; 83 84 expect_lookup(RELPATH, ino); 85 expect_opendir(ino, O_RDONLY, ReturnErrno(ENOENT)); 86 87 EXPECT_NE(0, open(FULLPATH, O_DIRECTORY)); 88 EXPECT_EQ(ENOENT, errno); 89 } 90 91 /* 92 * The daemon is responsible for checking file permissions (unless the 93 * default_permissions mount option was used) 94 */ 95 TEST_F(Opendir, eperm) 96 { 97 const char FULLPATH[] = "mountpoint/some_dir"; 98 const char RELPATH[] = "some_dir"; 99 uint64_t ino = 42; 100 101 expect_lookup(RELPATH, ino); 102 expect_opendir(ino, O_RDONLY, ReturnErrno(EPERM)); 103 104 EXPECT_NE(0, open(FULLPATH, O_DIRECTORY)); 105 EXPECT_EQ(EPERM, errno); 106 } 107 108 TEST_F(Opendir, open) 109 { 110 const char FULLPATH[] = "mountpoint/some_dir"; 111 const char RELPATH[] = "some_dir"; 112 uint64_t ino = 42; 113 114 expect_lookup(RELPATH, ino); 115 expect_opendir(ino, O_RDONLY, 116 ReturnImmediate([=](auto in __unused, auto& out) { 117 SET_OUT_HEADER_LEN(out, open); 118 })); 119 120 EXPECT_LE(0, open(FULLPATH, O_DIRECTORY)) << strerror(errno); 121 } 122 123 /* Directories can be opened O_EXEC for stuff like fchdir(2) */ 124 TEST_F(Opendir, open_exec) 125 { 126 const char FULLPATH[] = "mountpoint/some_dir"; 127 const char RELPATH[] = "some_dir"; 128 uint64_t ino = 42; 129 int fd; 130 131 expect_lookup(RELPATH, ino); 132 expect_opendir(ino, O_EXEC, 133 ReturnImmediate([=](auto in __unused, auto& out) { 134 SET_OUT_HEADER_LEN(out, open); 135 })); 136 137 fd = open(FULLPATH, O_EXEC | O_DIRECTORY); 138 ASSERT_LE(0, fd) << strerror(errno); 139 } 140 141 TEST_F(Opendir, opendir) 142 { 143 const char FULLPATH[] = "mountpoint/some_dir"; 144 const char RELPATH[] = "some_dir"; 145 uint64_t ino = 42; 146 147 expect_lookup(RELPATH, ino); 148 expect_opendir(ino, O_RDONLY, 149 ReturnImmediate([=](auto in __unused, auto& out) { 150 SET_OUT_HEADER_LEN(out, open); 151 })); 152 153 errno = 0; 154 EXPECT_NE(nullptr, opendir(FULLPATH)) << strerror(errno); 155 } 156