xref: /freebsd/tests/sys/fs/fusefs/rmdir.cc (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
19821f1d3SAlan Somers /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
39821f1d3SAlan Somers  *
49821f1d3SAlan Somers  * Copyright (c) 2019 The FreeBSD Foundation
59821f1d3SAlan Somers  *
69821f1d3SAlan Somers  * This software was developed by BFF Storage Systems, LLC under sponsorship
79821f1d3SAlan Somers  * from the FreeBSD Foundation.
89821f1d3SAlan Somers  *
99821f1d3SAlan Somers  * Redistribution and use in source and binary forms, with or without
109821f1d3SAlan Somers  * modification, are permitted provided that the following conditions
119821f1d3SAlan Somers  * are met:
129821f1d3SAlan Somers  * 1. Redistributions of source code must retain the above copyright
139821f1d3SAlan Somers  *    notice, this list of conditions and the following disclaimer.
149821f1d3SAlan Somers  * 2. Redistributions in binary form must reproduce the above copyright
159821f1d3SAlan Somers  *    notice, this list of conditions and the following disclaimer in the
169821f1d3SAlan Somers  *    documentation and/or other materials provided with the distribution.
179821f1d3SAlan Somers  *
189821f1d3SAlan Somers  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
199821f1d3SAlan Somers  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
209821f1d3SAlan Somers  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
219821f1d3SAlan Somers  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
229821f1d3SAlan Somers  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
239821f1d3SAlan Somers  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
249821f1d3SAlan Somers  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
259821f1d3SAlan Somers  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
269821f1d3SAlan Somers  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
279821f1d3SAlan Somers  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
289821f1d3SAlan Somers  * SUCH DAMAGE.
299821f1d3SAlan Somers  */
309821f1d3SAlan Somers 
319821f1d3SAlan Somers extern "C" {
329821f1d3SAlan Somers #include <fcntl.h>
33435ecf40SAlan Somers #include <semaphore.h>
349821f1d3SAlan Somers }
359821f1d3SAlan Somers 
369821f1d3SAlan Somers #include "mockfs.hh"
379821f1d3SAlan Somers #include "utils.hh"
389821f1d3SAlan Somers 
399821f1d3SAlan Somers using namespace testing;
409821f1d3SAlan Somers 
419821f1d3SAlan Somers class Rmdir: public FuseTest {
429821f1d3SAlan Somers public:
expect_lookup(const char * relpath,uint64_t ino,int times=1)43435ecf40SAlan Somers void expect_lookup(const char *relpath, uint64_t ino, int times=1)
449821f1d3SAlan Somers {
45a34cdd26SAlan Somers 	EXPECT_LOOKUP(FUSE_ROOT_ID, relpath)
46435ecf40SAlan Somers 	.Times(times)
47435ecf40SAlan Somers 	.WillRepeatedly(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
489821f1d3SAlan Somers 		SET_OUT_HEADER_LEN(out, entry);
49435ecf40SAlan Somers 		out.body.entry.attr_valid = UINT64_MAX;
5029edc611SAlan Somers 		out.body.entry.attr.mode = S_IFDIR | 0755;
5129edc611SAlan Somers 		out.body.entry.nodeid = ino;
5229edc611SAlan Somers 		out.body.entry.attr.nlink = 2;
539821f1d3SAlan Somers 	})));
549821f1d3SAlan Somers }
55002e54b0SAlan Somers 
expect_rmdir(uint64_t parent,const char * relpath,int error)56002e54b0SAlan Somers void expect_rmdir(uint64_t parent, const char *relpath, int error)
57002e54b0SAlan Somers {
58002e54b0SAlan Somers 	EXPECT_CALL(*m_mock, process(
59002e54b0SAlan Somers 		ResultOf([=](auto in) {
6029edc611SAlan Somers 			return (in.header.opcode == FUSE_RMDIR &&
6129edc611SAlan Somers 				0 == strcmp(relpath, in.body.rmdir) &&
6229edc611SAlan Somers 				in.header.nodeid == parent);
63002e54b0SAlan Somers 		}, Eq(true)),
64002e54b0SAlan Somers 		_)
65002e54b0SAlan Somers 	).WillOnce(Invoke(ReturnErrno(error)));
66002e54b0SAlan Somers }
679821f1d3SAlan Somers };
689821f1d3SAlan Somers 
69002e54b0SAlan Somers /*
70002e54b0SAlan Somers  * A successful rmdir should clear the parent directory's attribute cache,
71002e54b0SAlan Somers  * because the fuse daemon should update its mtime and ctime
72002e54b0SAlan Somers  */
TEST_F(Rmdir,parent_attr_cache)73435ecf40SAlan Somers TEST_F(Rmdir, parent_attr_cache)
74002e54b0SAlan Somers {
75002e54b0SAlan Somers 	const char FULLPATH[] = "mountpoint/some_dir";
76002e54b0SAlan Somers 	const char RELPATH[] = "some_dir";
77002e54b0SAlan Somers 	struct stat sb;
78435ecf40SAlan Somers 	sem_t sem;
79002e54b0SAlan Somers 	uint64_t ino = 42;
80bfcb817bSAlan Somers 	Sequence seq;
81002e54b0SAlan Somers 
82435ecf40SAlan Somers 	ASSERT_EQ(0, sem_init(&sem, 0, 0)) << strerror(errno);
83435ecf40SAlan Somers 
84bfcb817bSAlan Somers 	expect_lookup(RELPATH, ino);
85bfcb817bSAlan Somers 	EXPECT_CALL(*m_mock, process(
86bfcb817bSAlan Somers 		ResultOf([=](auto in) {
87bfcb817bSAlan Somers 			return (in.header.opcode == FUSE_RMDIR &&
88bfcb817bSAlan Somers 				0 == strcmp(RELPATH, in.body.rmdir) &&
89bfcb817bSAlan Somers 				in.header.nodeid == FUSE_ROOT_ID);
90bfcb817bSAlan Somers 		}, Eq(true)),
91bfcb817bSAlan Somers 		_)
92bfcb817bSAlan Somers 	).InSequence(seq)
93bfcb817bSAlan Somers 	.WillOnce(Invoke(ReturnErrno(0)));
94bfcb817bSAlan Somers 	expect_forget(ino, 1, &sem);
95002e54b0SAlan Somers 	EXPECT_CALL(*m_mock, process(
96002e54b0SAlan Somers 		ResultOf([=](auto in) {
9729edc611SAlan Somers 			return (in.header.opcode == FUSE_GETATTR &&
98a34cdd26SAlan Somers 				in.header.nodeid == FUSE_ROOT_ID);
99002e54b0SAlan Somers 		}, Eq(true)),
100002e54b0SAlan Somers 		_)
101bfcb817bSAlan Somers 	).InSequence(seq)
10229edc611SAlan Somers 	.WillRepeatedly(Invoke(ReturnImmediate([=](auto i __unused, auto& out) {
103002e54b0SAlan Somers 		SET_OUT_HEADER_LEN(out, attr);
104bfcb817bSAlan Somers 		out.body.attr.attr.ino = FUSE_ROOT_ID;
10529edc611SAlan Somers 		out.body.attr.attr.mode = S_IFDIR | 0755;
10629edc611SAlan Somers 		out.body.attr.attr_valid = UINT64_MAX;
107002e54b0SAlan Somers 	})));
108002e54b0SAlan Somers 
109002e54b0SAlan Somers 	ASSERT_EQ(0, rmdir(FULLPATH)) << strerror(errno);
110002e54b0SAlan Somers 	EXPECT_EQ(0, stat("mountpoint", &sb)) << strerror(errno);
111435ecf40SAlan Somers 	sem_wait(&sem);
112435ecf40SAlan Somers 	sem_destroy(&sem);
113002e54b0SAlan Somers }
114002e54b0SAlan Somers 
TEST_F(Rmdir,enotempty)1159821f1d3SAlan Somers TEST_F(Rmdir, enotempty)
1169821f1d3SAlan Somers {
1179821f1d3SAlan Somers 	const char FULLPATH[] = "mountpoint/some_dir";
1189821f1d3SAlan Somers 	const char RELPATH[] = "some_dir";
1199821f1d3SAlan Somers 	uint64_t ino = 42;
1209821f1d3SAlan Somers 
1219821f1d3SAlan Somers 	expect_lookup(RELPATH, ino);
122a34cdd26SAlan Somers 	expect_rmdir(FUSE_ROOT_ID, RELPATH, ENOTEMPTY);
1239821f1d3SAlan Somers 
1249821f1d3SAlan Somers 	ASSERT_NE(0, rmdir(FULLPATH));
1259821f1d3SAlan Somers 	ASSERT_EQ(ENOTEMPTY, errno);
1269821f1d3SAlan Somers }
1279821f1d3SAlan Somers 
128435ecf40SAlan Somers /* Removing a directory should expire its entry cache */
TEST_F(Rmdir,entry_cache)129435ecf40SAlan Somers TEST_F(Rmdir, entry_cache)
130435ecf40SAlan Somers {
131435ecf40SAlan Somers 	const char FULLPATH[] = "mountpoint/some_dir";
132435ecf40SAlan Somers 	const char RELPATH[] = "some_dir";
133435ecf40SAlan Somers 	sem_t sem;
134435ecf40SAlan Somers 	uint64_t ino = 42;
135435ecf40SAlan Somers 
136435ecf40SAlan Somers 	expect_lookup(RELPATH, ino, 2);
137435ecf40SAlan Somers 	expect_rmdir(FUSE_ROOT_ID, RELPATH, 0);
138435ecf40SAlan Somers 	expect_forget(ino, 1, &sem);
139435ecf40SAlan Somers 
140435ecf40SAlan Somers 	ASSERT_EQ(0, rmdir(FULLPATH)) << strerror(errno);
141435ecf40SAlan Somers 	ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno);
142435ecf40SAlan Somers 	sem_wait(&sem);
143435ecf40SAlan Somers 	sem_destroy(&sem);
144435ecf40SAlan Somers }
145435ecf40SAlan Somers 
TEST_F(Rmdir,ok)1469821f1d3SAlan Somers TEST_F(Rmdir, ok)
1479821f1d3SAlan Somers {
1489821f1d3SAlan Somers 	const char FULLPATH[] = "mountpoint/some_dir";
1499821f1d3SAlan Somers 	const char RELPATH[] = "some_dir";
150435ecf40SAlan Somers 	sem_t sem;
1519821f1d3SAlan Somers 	uint64_t ino = 42;
1529821f1d3SAlan Somers 
153435ecf40SAlan Somers 	ASSERT_EQ(0, sem_init(&sem, 0, 0)) << strerror(errno);
154435ecf40SAlan Somers 
1559821f1d3SAlan Somers 	expect_lookup(RELPATH, ino);
156a34cdd26SAlan Somers 	expect_rmdir(FUSE_ROOT_ID, RELPATH, 0);
157435ecf40SAlan Somers 	expect_forget(ino, 1, &sem);
1589821f1d3SAlan Somers 
1599821f1d3SAlan Somers 	ASSERT_EQ(0, rmdir(FULLPATH)) << strerror(errno);
160435ecf40SAlan Somers 	sem_wait(&sem);
161435ecf40SAlan Somers 	sem_destroy(&sem);
1629821f1d3SAlan Somers }
163