xref: /freebsd/tests/sys/fs/fusefs/opendir.cc (revision 5a0b9a2776715c31a8c0364674b34f13c75f3179)
19821f1d3SAlan Somers /*-
29821f1d3SAlan Somers  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 <dirent.h>
339821f1d3SAlan Somers #include <fcntl.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 Opendir: public FuseTest {
429821f1d3SAlan Somers public:
439821f1d3SAlan Somers void expect_lookup(const char *relpath, uint64_t ino)
449821f1d3SAlan Somers {
459821f1d3SAlan Somers 	FuseTest::expect_lookup(relpath, ino, S_IFDIR | 0755, 0, 1);
469821f1d3SAlan Somers }
47363a7416SAlan Somers 
48363a7416SAlan Somers void expect_opendir(uint64_t ino, uint32_t flags, ProcessMockerT r)
49363a7416SAlan Somers {
50363a7416SAlan Somers 	/* opendir(3) calls fstatfs */
51363a7416SAlan Somers 	EXPECT_CALL(*m_mock, process(
52363a7416SAlan Somers 		ResultOf([](auto in) {
5329edc611SAlan Somers 			return (in.header.opcode == FUSE_STATFS);
54363a7416SAlan Somers 		}, Eq(true)),
55363a7416SAlan Somers 		_)
5629edc611SAlan Somers 	).WillRepeatedly(Invoke(ReturnImmediate([=](auto i __unused, auto& out) {
57363a7416SAlan Somers 		SET_OUT_HEADER_LEN(out, statfs);
58363a7416SAlan Somers 	})));
59363a7416SAlan Somers 
60363a7416SAlan Somers 	EXPECT_CALL(*m_mock, process(
61363a7416SAlan Somers 		ResultOf([=](auto in) {
6229edc611SAlan Somers 			return (in.header.opcode == FUSE_OPENDIR &&
6329edc611SAlan Somers 				in.header.nodeid == ino &&
6429edc611SAlan Somers 				in.body.opendir.flags == flags);
65363a7416SAlan Somers 		}, Eq(true)),
66363a7416SAlan Somers 		_)
67363a7416SAlan Somers 	).WillOnce(Invoke(r));
68363a7416SAlan Somers }
69363a7416SAlan Somers 
709821f1d3SAlan Somers };
719821f1d3SAlan Somers 
729821f1d3SAlan Somers 
739821f1d3SAlan Somers /*
749821f1d3SAlan Somers  * The fuse daemon fails the request with enoent.  This usually indicates a
759821f1d3SAlan Somers  * race condition: some other FUSE client removed the file in between when the
769821f1d3SAlan Somers  * kernel checked for it with lookup and tried to open it
779821f1d3SAlan Somers  */
789821f1d3SAlan Somers TEST_F(Opendir, enoent)
799821f1d3SAlan Somers {
809821f1d3SAlan Somers 	const char FULLPATH[] = "mountpoint/some_dir";
819821f1d3SAlan Somers 	const char RELPATH[] = "some_dir";
829821f1d3SAlan Somers 	uint64_t ino = 42;
839821f1d3SAlan Somers 
849821f1d3SAlan Somers 	expect_lookup(RELPATH, ino);
85363a7416SAlan Somers 	expect_opendir(ino, O_RDONLY, ReturnErrno(ENOENT));
869821f1d3SAlan Somers 
879821f1d3SAlan Somers 	EXPECT_NE(0, open(FULLPATH, O_DIRECTORY));
889821f1d3SAlan Somers 	EXPECT_EQ(ENOENT, errno);
899821f1d3SAlan Somers }
909821f1d3SAlan Somers 
919821f1d3SAlan Somers /*
929821f1d3SAlan Somers  * The daemon is responsible for checking file permissions (unless the
939821f1d3SAlan Somers  * default_permissions mount option was used)
949821f1d3SAlan Somers  */
959821f1d3SAlan Somers TEST_F(Opendir, eperm)
969821f1d3SAlan Somers {
979821f1d3SAlan Somers 	const char FULLPATH[] = "mountpoint/some_dir";
989821f1d3SAlan Somers 	const char RELPATH[] = "some_dir";
999821f1d3SAlan Somers 	uint64_t ino = 42;
1009821f1d3SAlan Somers 
1019821f1d3SAlan Somers 	expect_lookup(RELPATH, ino);
102363a7416SAlan Somers 	expect_opendir(ino, O_RDONLY, ReturnErrno(EPERM));
1039821f1d3SAlan Somers 
1049821f1d3SAlan Somers 	EXPECT_NE(0, open(FULLPATH, O_DIRECTORY));
1059821f1d3SAlan Somers 	EXPECT_EQ(EPERM, errno);
1069821f1d3SAlan Somers }
1079821f1d3SAlan Somers 
1089821f1d3SAlan Somers TEST_F(Opendir, open)
1099821f1d3SAlan Somers {
1109821f1d3SAlan Somers 	const char FULLPATH[] = "mountpoint/some_dir";
1119821f1d3SAlan Somers 	const char RELPATH[] = "some_dir";
1129821f1d3SAlan Somers 	uint64_t ino = 42;
1139821f1d3SAlan Somers 
1149821f1d3SAlan Somers 	expect_lookup(RELPATH, ino);
115363a7416SAlan Somers 	expect_opendir(ino, O_RDONLY,
11629edc611SAlan Somers 	ReturnImmediate([=](auto in __unused, auto& out) {
1179821f1d3SAlan Somers 		SET_OUT_HEADER_LEN(out, open);
118363a7416SAlan Somers 	}));
1199821f1d3SAlan Somers 
1209821f1d3SAlan Somers 	EXPECT_LE(0, open(FULLPATH, O_DIRECTORY)) << strerror(errno);
1219821f1d3SAlan Somers }
1229821f1d3SAlan Somers 
123363a7416SAlan Somers /* Directories can be opened O_EXEC for stuff like fchdir(2) */
124363a7416SAlan Somers TEST_F(Opendir, open_exec)
125363a7416SAlan Somers {
126363a7416SAlan Somers 	const char FULLPATH[] = "mountpoint/some_dir";
127363a7416SAlan Somers 	const char RELPATH[] = "some_dir";
128363a7416SAlan Somers 	uint64_t ino = 42;
129363a7416SAlan Somers 	int fd;
130363a7416SAlan Somers 
131363a7416SAlan Somers 	expect_lookup(RELPATH, ino);
132363a7416SAlan Somers 	expect_opendir(ino, O_EXEC,
13329edc611SAlan Somers 	ReturnImmediate([=](auto in __unused, auto& out) {
134363a7416SAlan Somers 		SET_OUT_HEADER_LEN(out, open);
135363a7416SAlan Somers 	}));
136363a7416SAlan Somers 
137363a7416SAlan Somers 	fd = open(FULLPATH, O_EXEC | O_DIRECTORY);
138363a7416SAlan Somers 	ASSERT_LE(0, fd) << strerror(errno);
139363a7416SAlan Somers }
140363a7416SAlan Somers 
1419821f1d3SAlan Somers TEST_F(Opendir, opendir)
1429821f1d3SAlan Somers {
1439821f1d3SAlan Somers 	const char FULLPATH[] = "mountpoint/some_dir";
1449821f1d3SAlan Somers 	const char RELPATH[] = "some_dir";
1459821f1d3SAlan Somers 	uint64_t ino = 42;
1469821f1d3SAlan Somers 
1479821f1d3SAlan Somers 	expect_lookup(RELPATH, ino);
148363a7416SAlan Somers 	expect_opendir(ino, O_RDONLY,
14929edc611SAlan Somers 	ReturnImmediate([=](auto in __unused, auto& out) {
1509821f1d3SAlan Somers 		SET_OUT_HEADER_LEN(out, open);
151363a7416SAlan Somers 	}));
1529821f1d3SAlan Somers 
1539821f1d3SAlan Somers 	errno = 0;
154*5a0b9a27SAlan Somers 	EXPECT_NE(nullptr, opendir(FULLPATH)) << strerror(errno);
1559821f1d3SAlan Somers }
156