]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tests/sys/fs/fuse/opendir.cc
fuse(4): add tests for the FOPEN_KEEP_CACHE option
[FreeBSD/FreeBSD.git] / tests / sys / fs / fuse / opendir.cc
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 Opendir: public FuseTest {
42 public:
43 void expect_lookup(const char *relpath, uint64_t ino)
44 {
45         FuseTest::expect_lookup(relpath, ino, S_IFDIR | 0755, 0, 1);
46 }
47 };
48
49
50 /* 
51  * The fuse daemon fails the request with enoent.  This usually indicates a
52  * race condition: some other FUSE client removed the file in between when the
53  * kernel checked for it with lookup and tried to open it
54  */
55 TEST_F(Opendir, enoent)
56 {
57         const char FULLPATH[] = "mountpoint/some_dir";
58         const char RELPATH[] = "some_dir";
59         uint64_t ino = 42;
60
61         expect_lookup(RELPATH, ino);
62
63         EXPECT_CALL(*m_mock, process(
64                 ResultOf([=](auto in) {
65                         return (in->header.opcode == FUSE_OPENDIR &&
66                                 in->header.nodeid == ino);
67                 }, Eq(true)),
68                 _)
69         ).WillOnce(Invoke(ReturnErrno(ENOENT)));
70         EXPECT_NE(0, open(FULLPATH, O_DIRECTORY));
71         EXPECT_EQ(ENOENT, errno);
72 }
73
74 /* 
75  * The daemon is responsible for checking file permissions (unless the
76  * default_permissions mount option was used)
77  */
78 TEST_F(Opendir, eperm)
79 {
80         const char FULLPATH[] = "mountpoint/some_dir";
81         const char RELPATH[] = "some_dir";
82         uint64_t ino = 42;
83
84         expect_lookup(RELPATH, ino);
85
86         EXPECT_CALL(*m_mock, process(
87                 ResultOf([=](auto in) {
88                         return (in->header.opcode == FUSE_OPENDIR &&
89                                 in->header.nodeid == ino);
90                 }, Eq(true)),
91                 _)
92         ).WillOnce(Invoke(ReturnErrno(EPERM)));
93
94         EXPECT_NE(0, open(FULLPATH, O_DIRECTORY));
95         EXPECT_EQ(EPERM, errno);
96 }
97
98 TEST_F(Opendir, open)
99 {
100         const char FULLPATH[] = "mountpoint/some_dir";
101         const char RELPATH[] = "some_dir";
102         uint64_t ino = 42;
103
104         expect_lookup(RELPATH, ino);
105
106         EXPECT_CALL(*m_mock, process(
107                 ResultOf([=](auto in) {
108                         return (in->header.opcode == FUSE_OPENDIR &&
109                                 in->header.nodeid == ino);
110                 }, Eq(true)),
111                 _)
112         ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
113                 SET_OUT_HEADER_LEN(out, open);
114         })));
115
116         EXPECT_LE(0, open(FULLPATH, O_DIRECTORY)) << strerror(errno);
117 }
118
119 TEST_F(Opendir, opendir)
120 {
121         const char FULLPATH[] = "mountpoint/some_dir";
122         const char RELPATH[] = "some_dir";
123         uint64_t ino = 42;
124
125         expect_lookup(RELPATH, ino);
126         EXPECT_CALL(*m_mock, process(
127                 ResultOf([](auto in) {
128                         return (in->header.opcode == FUSE_STATFS);
129                 }, Eq(true)),
130                 _)
131         ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
132                 SET_OUT_HEADER_LEN(out, statfs);
133         })));
134
135         EXPECT_CALL(*m_mock, process(
136                 ResultOf([=](auto in) {
137                         return (in->header.opcode == FUSE_OPENDIR &&
138                                 in->header.nodeid == ino);
139                 }, Eq(true)),
140                 _)
141         ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
142                 SET_OUT_HEADER_LEN(out, open);
143         })));
144
145         errno = 0;
146         EXPECT_NE(NULL, opendir(FULLPATH)) << strerror(errno);
147 }