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
35 #include <semaphore.h>
41 using namespace testing;
43 class Rmdir: public FuseTest {
45 void expect_lookup(const char *relpath, uint64_t ino, int times=1)
47 EXPECT_LOOKUP(FUSE_ROOT_ID, relpath)
49 .WillRepeatedly(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
50 SET_OUT_HEADER_LEN(out, entry);
51 out.body.entry.attr_valid = UINT64_MAX;
52 out.body.entry.attr.mode = S_IFDIR | 0755;
53 out.body.entry.nodeid = ino;
54 out.body.entry.attr.nlink = 2;
58 void expect_rmdir(uint64_t parent, const char *relpath, int error)
60 EXPECT_CALL(*m_mock, process(
61 ResultOf([=](auto in) {
62 return (in.header.opcode == FUSE_RMDIR &&
63 0 == strcmp(relpath, in.body.rmdir) &&
64 in.header.nodeid == parent);
67 ).WillOnce(Invoke(ReturnErrno(error)));
72 * A successful rmdir should clear the parent directory's attribute cache,
73 * because the fuse daemon should update its mtime and ctime
75 TEST_F(Rmdir, parent_attr_cache)
77 const char FULLPATH[] = "mountpoint/some_dir";
78 const char RELPATH[] = "some_dir";
84 ASSERT_EQ(0, sem_init(&sem, 0, 0)) << strerror(errno);
86 expect_lookup(RELPATH, ino);
87 EXPECT_CALL(*m_mock, process(
88 ResultOf([=](auto in) {
89 return (in.header.opcode == FUSE_RMDIR &&
90 0 == strcmp(RELPATH, in.body.rmdir) &&
91 in.header.nodeid == FUSE_ROOT_ID);
95 .WillOnce(Invoke(ReturnErrno(0)));
96 expect_forget(ino, 1, &sem);
97 EXPECT_CALL(*m_mock, process(
98 ResultOf([=](auto in) {
99 return (in.header.opcode == FUSE_GETATTR &&
100 in.header.nodeid == FUSE_ROOT_ID);
104 .WillRepeatedly(Invoke(ReturnImmediate([=](auto i __unused, auto& out) {
105 SET_OUT_HEADER_LEN(out, attr);
106 out.body.attr.attr.ino = FUSE_ROOT_ID;
107 out.body.attr.attr.mode = S_IFDIR | 0755;
108 out.body.attr.attr_valid = UINT64_MAX;
111 ASSERT_EQ(0, rmdir(FULLPATH)) << strerror(errno);
112 EXPECT_EQ(0, stat("mountpoint", &sb)) << strerror(errno);
117 TEST_F(Rmdir, enotempty)
119 const char FULLPATH[] = "mountpoint/some_dir";
120 const char RELPATH[] = "some_dir";
123 expect_lookup(RELPATH, ino);
124 expect_rmdir(FUSE_ROOT_ID, RELPATH, ENOTEMPTY);
126 ASSERT_NE(0, rmdir(FULLPATH));
127 ASSERT_EQ(ENOTEMPTY, errno);
130 /* Removing a directory should expire its entry cache */
131 TEST_F(Rmdir, entry_cache)
133 const char FULLPATH[] = "mountpoint/some_dir";
134 const char RELPATH[] = "some_dir";
138 expect_lookup(RELPATH, ino, 2);
139 expect_rmdir(FUSE_ROOT_ID, RELPATH, 0);
140 expect_forget(ino, 1, &sem);
142 ASSERT_EQ(0, rmdir(FULLPATH)) << strerror(errno);
143 ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno);
150 const char FULLPATH[] = "mountpoint/some_dir";
151 const char RELPATH[] = "some_dir";
155 ASSERT_EQ(0, sem_init(&sem, 0, 0)) << strerror(errno);
157 expect_lookup(RELPATH, ino);
158 expect_rmdir(FUSE_ROOT_ID, RELPATH, 0);
159 expect_forget(ino, 1, &sem);
161 ASSERT_EQ(0, rmdir(FULLPATH)) << strerror(errno);