xref: /freebsd/tests/sys/fs/fusefs/rmdir.cc (revision 535af610)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 <fcntl.h>
35 #include <semaphore.h>
36 }
37 
38 #include "mockfs.hh"
39 #include "utils.hh"
40 
41 using namespace testing;
42 
43 class Rmdir: public FuseTest {
44 public:
45 void expect_lookup(const char *relpath, uint64_t ino, int times=1)
46 {
47 	EXPECT_LOOKUP(FUSE_ROOT_ID, relpath)
48 	.Times(times)
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;
55 	})));
56 }
57 
58 void expect_rmdir(uint64_t parent, const char *relpath, int error)
59 {
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);
65 		}, Eq(true)),
66 		_)
67 	).WillOnce(Invoke(ReturnErrno(error)));
68 }
69 };
70 
71 /*
72  * A successful rmdir should clear the parent directory's attribute cache,
73  * because the fuse daemon should update its mtime and ctime
74  */
75 TEST_F(Rmdir, parent_attr_cache)
76 {
77 	const char FULLPATH[] = "mountpoint/some_dir";
78 	const char RELPATH[] = "some_dir";
79 	struct stat sb;
80 	sem_t sem;
81 	uint64_t ino = 42;
82 	Sequence seq;
83 
84 	ASSERT_EQ(0, sem_init(&sem, 0, 0)) << strerror(errno);
85 
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);
92 		}, Eq(true)),
93 		_)
94 	).InSequence(seq)
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);
101 		}, Eq(true)),
102 		_)
103 	).InSequence(seq)
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;
109 	})));
110 
111 	ASSERT_EQ(0, rmdir(FULLPATH)) << strerror(errno);
112 	EXPECT_EQ(0, stat("mountpoint", &sb)) << strerror(errno);
113 	sem_wait(&sem);
114 	sem_destroy(&sem);
115 }
116 
117 TEST_F(Rmdir, enotempty)
118 {
119 	const char FULLPATH[] = "mountpoint/some_dir";
120 	const char RELPATH[] = "some_dir";
121 	uint64_t ino = 42;
122 
123 	expect_lookup(RELPATH, ino);
124 	expect_rmdir(FUSE_ROOT_ID, RELPATH, ENOTEMPTY);
125 
126 	ASSERT_NE(0, rmdir(FULLPATH));
127 	ASSERT_EQ(ENOTEMPTY, errno);
128 }
129 
130 /* Removing a directory should expire its entry cache */
131 TEST_F(Rmdir, entry_cache)
132 {
133 	const char FULLPATH[] = "mountpoint/some_dir";
134 	const char RELPATH[] = "some_dir";
135 	sem_t sem;
136 	uint64_t ino = 42;
137 
138 	expect_lookup(RELPATH, ino, 2);
139 	expect_rmdir(FUSE_ROOT_ID, RELPATH, 0);
140 	expect_forget(ino, 1, &sem);
141 
142 	ASSERT_EQ(0, rmdir(FULLPATH)) << strerror(errno);
143 	ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno);
144 	sem_wait(&sem);
145 	sem_destroy(&sem);
146 }
147 
148 TEST_F(Rmdir, ok)
149 {
150 	const char FULLPATH[] = "mountpoint/some_dir";
151 	const char RELPATH[] = "some_dir";
152 	sem_t sem;
153 	uint64_t ino = 42;
154 
155 	ASSERT_EQ(0, sem_init(&sem, 0, 0)) << strerror(errno);
156 
157 	expect_lookup(RELPATH, ino);
158 	expect_rmdir(FUSE_ROOT_ID, RELPATH, 0);
159 	expect_forget(ino, 1, &sem);
160 
161 	ASSERT_EQ(0, rmdir(FULLPATH)) << strerror(errno);
162 	sem_wait(&sem);
163 	sem_destroy(&sem);
164 }
165