]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tests/sys/fs/fuse/mkdir.cc
fuse(4): combine common code in the tests
[FreeBSD/FreeBSD.git] / tests / sys / fs / fuse / mkdir.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 <fcntl.h>
33 }
34
35 #include "mockfs.hh"
36 #include "utils.hh"
37
38 using namespace testing;
39
40 class Mkdir: public FuseTest {};
41
42 /* 
43  * EMLINK is possible on filesystems that limit the number of hard links to a
44  * single file, like early versions of BtrFS
45  */
46 TEST_F(Mkdir, emlink)
47 {
48         const char FULLPATH[] = "mountpoint/some_dir";
49         const char RELPATH[] = "some_dir";
50         mode_t mode = 0755;
51
52         EXPECT_LOOKUP(1, RELPATH).WillOnce(Invoke(ReturnErrno(ENOENT)));
53
54         EXPECT_CALL(*m_mock, process(
55                 ResultOf([=](auto in) {
56                         const char *name = (const char*)in->body.bytes +
57                                 sizeof(fuse_mkdir_in);
58                         return (in->header.opcode == FUSE_MKDIR &&
59                                 in->body.mkdir.mode == (S_IFDIR | mode) &&
60                                 (0 == strcmp(RELPATH, name)));
61                 }, Eq(true)),
62                 _)
63         ).WillOnce(Invoke(ReturnErrno(EMLINK)));
64
65         ASSERT_NE(1, mkdir(FULLPATH, mode));
66         ASSERT_EQ(EMLINK, errno);
67 }
68
69 /*
70  * Creating a new directory after FUSE_LOOKUP returned a negative cache entry
71  */
72 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236231 */
73 TEST_F(Mkdir, DISABLED_entry_cache_negative)
74 {
75         const char FULLPATH[] = "mountpoint/some_file.txt";
76         const char RELPATH[] = "some_file.txt";
77         mode_t mode = 0755;
78         uint64_t ino = 42;
79         /* 
80          * Set entry_valid = 0 because this test isn't concerned with whether
81          * or not we actually cache negative entries, only with whether we
82          * interpret negative cache responses correctly.
83          */
84         struct timespec entry_valid = {.tv_sec = 0, .tv_nsec = 0};
85
86         /* mkdir will first do a LOOKUP, adding a negative cache entry */
87         EXPECT_LOOKUP(1, RELPATH).WillOnce(ReturnNegativeCache(&entry_valid));
88
89         EXPECT_CALL(*m_mock, process(
90                 ResultOf([=](auto in) {
91                         const char *name = (const char*)in->body.bytes +
92                                 sizeof(fuse_open_in);
93                         return (in->header.opcode == FUSE_MKDIR &&
94                                 in->body.mkdir.mode == (S_IFDIR | mode) &&
95                                 (0 == strcmp(RELPATH, name)));
96                 }, Eq(true)),
97                 _)
98         ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
99                 SET_OUT_HEADER_LEN(out, entry);
100                 out->body.create.entry.attr.mode = S_IFDIR | mode;
101                 out->body.create.entry.nodeid = ino;
102                 out->body.create.entry.entry_valid = UINT64_MAX;
103                 out->body.create.entry.attr_valid = UINT64_MAX;
104         })));
105
106         ASSERT_EQ(0, mkdir(FULLPATH, mode)) << strerror(errno);
107 }
108
109 /*
110  * Creating a new directory should purge any negative namecache entries
111  */
112 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236231 */
113 TEST_F(Mkdir, DISABLED_entry_cache_negative_purge)
114 {
115         const char FULLPATH[] = "mountpoint/some_file.txt";
116         const char RELPATH[] = "some_file.txt";
117         mode_t mode = 0755;
118         uint64_t ino = 42;
119         struct timespec entry_valid = {.tv_sec = TIME_T_MAX, .tv_nsec = 0};
120
121         /* mkdir will first do a LOOKUP, adding a negative cache entry */
122         EXPECT_LOOKUP(1, RELPATH).Times(1)
123         .WillOnce(Invoke(ReturnNegativeCache(&entry_valid)))
124         .RetiresOnSaturation();
125
126         /* Then the MKDIR should purge the negative cache entry */
127         EXPECT_CALL(*m_mock, process(
128                 ResultOf([=](auto in) {
129                         const char *name = (const char*)in->body.bytes +
130                                 sizeof(fuse_open_in);
131                         return (in->header.opcode == FUSE_MKDIR &&
132                                 in->body.mkdir.mode == (S_IFDIR | mode) &&
133                                 (0 == strcmp(RELPATH, name)));
134                 }, Eq(true)),
135                 _)
136         ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
137                 SET_OUT_HEADER_LEN(out, entry);
138                 out->body.entry.attr.mode = S_IFDIR | mode;
139                 out->body.entry.nodeid = ino;
140                 out->body.entry.attr_valid = UINT64_MAX;
141         })));
142
143         ASSERT_EQ(0, mkdir(FULLPATH, mode)) << strerror(errno);
144
145         /* Finally, a subsequent lookup should query the daemon */
146         expect_lookup(RELPATH, ino, S_IFDIR | mode, 1);
147
148         ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno);
149 }
150
151 TEST_F(Mkdir, ok)
152 {
153         const char FULLPATH[] = "mountpoint/some_dir";
154         const char RELPATH[] = "some_dir";
155         mode_t mode = 0755;
156         uint64_t ino = 42;
157
158         EXPECT_LOOKUP(1, RELPATH).WillOnce(Invoke(ReturnErrno(ENOENT)));
159
160         EXPECT_CALL(*m_mock, process(
161                 ResultOf([=](auto in) {
162                         const char *name = (const char*)in->body.bytes +
163                                 sizeof(fuse_mkdir_in);
164                         return (in->header.opcode == FUSE_MKDIR &&
165                                 in->body.mkdir.mode == (S_IFDIR | mode) &&
166                                 (0 == strcmp(RELPATH, name)));
167                 }, Eq(true)),
168                 _)
169         ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
170                 SET_OUT_HEADER_LEN(out, entry);
171                 out->body.create.entry.attr.mode = S_IFDIR | mode;
172                 out->body.create.entry.nodeid = ino;
173                 out->body.create.entry.entry_valid = UINT64_MAX;
174                 out->body.create.entry.attr_valid = UINT64_MAX;
175         })));
176
177         ASSERT_EQ(0, mkdir(FULLPATH, mode)) << strerror(errno);
178 }