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 ReleaseDir: public FuseTest { 42 43 public: 44 void expect_lookup(const char *relpath, uint64_t ino) 45 { 46 FuseTest::expect_lookup(relpath, ino, S_IFDIR | 0755, 0, 1); 47 } 48 49 void expect_releasedir(uint64_t ino, ProcessMockerT r) 50 { 51 EXPECT_CALL(*m_mock, process( 52 ResultOf([=](auto in) { 53 return (in->header.opcode == FUSE_RELEASEDIR && 54 in->header.nodeid == ino && 55 in->body.release.fh == FH); 56 }, Eq(true)), 57 _) 58 ).WillOnce(Invoke(r)); 59 } 60 }; 61 62 /* If a file descriptor is duplicated, only the last close causes RELEASE */ 63 TEST_F(ReleaseDir, dup) 64 { 65 const char FULLPATH[] = "mountpoint/some_file.txt"; 66 const char RELPATH[] = "some_file.txt"; 67 uint64_t ino = 42; 68 DIR *dir, *dir2; 69 70 expect_lookup(RELPATH, ino); 71 expect_opendir(ino); 72 EXPECT_CALL(*m_mock, process( 73 ResultOf([=](auto in) { 74 return (in->header.opcode == FUSE_READDIR && 75 in->header.nodeid == ino && 76 in->body.readdir.offset == 0); 77 }, Eq(true)), 78 _) 79 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto out) { 80 out->header.error = 0; 81 out->header.len = sizeof(out->header); 82 }))); 83 expect_releasedir(ino, ReturnErrno(0)); 84 85 dir = opendir(FULLPATH); 86 ASSERT_NE(NULL, dir) << strerror(errno); 87 88 dir2 = fdopendir(dup(dirfd(dir))); 89 ASSERT_NE(NULL, dir2) << strerror(errno); 90 91 ASSERT_EQ(0, closedir(dir)) << strerror(errno); 92 ASSERT_EQ(0, closedir(dir2)) << strerror(errno); 93 } 94 95 TEST_F(ReleaseDir, ok) 96 { 97 const char FULLPATH[] = "mountpoint/some_dir"; 98 const char RELPATH[] = "some_dir"; 99 uint64_t ino = 42; 100 DIR *dir; 101 102 expect_lookup(RELPATH, ino); 103 expect_opendir(ino); 104 expect_releasedir(ino, ReturnErrno(0)); 105 106 dir = opendir(FULLPATH); 107 ASSERT_NE(NULL, dir) << strerror(errno); 108 109 ASSERT_EQ(0, closedir(dir)) << strerror(errno); 110 } 111 112 /* Directories opened O_EXEC should be properly released, too */ 113 TEST_F(ReleaseDir, o_exec) 114 { 115 const char FULLPATH[] = "mountpoint/some_dir"; 116 const char RELPATH[] = "some_dir"; 117 uint64_t ino = 42; 118 int fd; 119 120 expect_lookup(RELPATH, ino); 121 expect_opendir(ino); 122 expect_releasedir(ino, ReturnErrno(0)); 123 124 fd = open(FULLPATH, O_EXEC | O_DIRECTORY); 125 EXPECT_LE(0, fd) << strerror(errno); 126 127 ASSERT_EQ(0, close(fd)) << strerror(errno); 128 } 129