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