]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tests/sys/fs/fusefs/link.cc
MFC the new fusefs driver
[FreeBSD/FreeBSD.git] / tests / sys / fs / fusefs / link.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  * $FreeBSD$
31  */
32
33 extern "C" {
34 #include <unistd.h>
35 }
36
37 #include "mockfs.hh"
38 #include "utils.hh"
39
40 using namespace testing;
41
42 class Link: public FuseTest {
43 public:
44 void expect_link(uint64_t ino, const char *relpath, mode_t mode, uint32_t nlink)
45 {
46         EXPECT_CALL(*m_mock, process(
47                 ResultOf([=](auto in) {
48                         const char *name = (const char*)in.body.bytes
49                                 + sizeof(struct fuse_link_in);
50                         return (in.header.opcode == FUSE_LINK &&
51                                 in.body.link.oldnodeid == ino &&
52                                 (0 == strcmp(name, relpath)));
53                 }, Eq(true)),
54                 _)
55         ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
56                 SET_OUT_HEADER_LEN(out, entry);
57                 out.body.entry.nodeid = ino;
58                 out.body.entry.attr.mode = mode;
59                 out.body.entry.attr.nlink = nlink;
60                 out.body.entry.attr_valid = UINT64_MAX;
61                 out.body.entry.entry_valid = UINT64_MAX;
62         })));
63 }
64
65 void expect_lookup(const char *relpath, uint64_t ino)
66 {
67         FuseTest::expect_lookup(relpath, ino, S_IFREG | 0644, 0, 1);
68 }
69 };
70
71 class Link_7_8: public FuseTest {
72 public:
73 virtual void SetUp() {
74         m_kernel_minor_version = 8;
75         FuseTest::SetUp();
76 }
77
78 void expect_link(uint64_t ino, const char *relpath, mode_t mode, uint32_t nlink)
79 {
80         EXPECT_CALL(*m_mock, process(
81                 ResultOf([=](auto in) {
82                         const char *name = (const char*)in.body.bytes
83                                 + sizeof(struct fuse_link_in);
84                         return (in.header.opcode == FUSE_LINK &&
85                                 in.body.link.oldnodeid == ino &&
86                                 (0 == strcmp(name, relpath)));
87                 }, Eq(true)),
88                 _)
89         ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
90                 SET_OUT_HEADER_LEN(out, entry_7_8);
91                 out.body.entry.nodeid = ino;
92                 out.body.entry.attr.mode = mode;
93                 out.body.entry.attr.nlink = nlink;
94                 out.body.entry.attr_valid = UINT64_MAX;
95                 out.body.entry.entry_valid = UINT64_MAX;
96         })));
97 }
98
99 void expect_lookup(const char *relpath, uint64_t ino)
100 {
101         FuseTest::expect_lookup_7_8(relpath, ino, S_IFREG | 0644, 0, 1);
102 }
103 };
104
105 /*
106  * A successful link should clear the parent directory's attribute cache,
107  * because the fuse daemon should update its mtime and ctime
108  */
109 TEST_F(Link, clear_attr_cache)
110 {
111         const char FULLPATH[] = "mountpoint/src";
112         const char RELPATH[] = "src";
113         const char FULLDST[] = "mountpoint/dst";
114         const char RELDST[] = "dst";
115         const uint64_t ino = 42;
116         mode_t mode = S_IFREG | 0644;
117         struct stat sb;
118
119         EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH)
120         .WillOnce(Invoke(ReturnErrno(ENOENT)));
121         EXPECT_CALL(*m_mock, process(
122                 ResultOf([=](auto in) {
123                         return (in.header.opcode == FUSE_GETATTR &&
124                                 in.header.nodeid == FUSE_ROOT_ID);
125                 }, Eq(true)),
126                 _)
127         ).Times(2)
128         .WillRepeatedly(Invoke(ReturnImmediate([=](auto i __unused, auto& out) {
129                 SET_OUT_HEADER_LEN(out, attr);
130                 out.body.attr.attr.ino = FUSE_ROOT_ID;
131                 out.body.attr.attr.mode = S_IFDIR | 0755;
132                 out.body.attr.attr_valid = UINT64_MAX;
133         })));
134
135         EXPECT_LOOKUP(FUSE_ROOT_ID, RELDST)
136         
137         .WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
138                 SET_OUT_HEADER_LEN(out, entry);
139                 out.body.entry.attr.mode = mode;
140                 out.body.entry.nodeid = ino;
141                 out.body.entry.attr.nlink = 1;
142                 out.body.entry.attr_valid = UINT64_MAX;
143                 out.body.entry.entry_valid = UINT64_MAX;
144         })));
145         expect_link(ino, RELPATH, mode, 2);
146
147         EXPECT_EQ(0, stat("mountpoint", &sb)) << strerror(errno);
148         EXPECT_EQ(0, link(FULLDST, FULLPATH)) << strerror(errno);
149         EXPECT_EQ(0, stat("mountpoint", &sb)) << strerror(errno);
150 }
151
152 TEST_F(Link, emlink)
153 {
154         const char FULLPATH[] = "mountpoint/lnk";
155         const char RELPATH[] = "lnk";
156         const char FULLDST[] = "mountpoint/dst";
157         const char RELDST[] = "dst";
158         uint64_t dst_ino = 42;
159
160         EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH)
161         .WillOnce(Invoke(ReturnErrno(ENOENT)));
162         expect_lookup(RELDST, dst_ino);
163
164         EXPECT_CALL(*m_mock, process(
165                 ResultOf([=](auto in) {
166                         const char *name = (const char*)in.body.bytes
167                                 + sizeof(struct fuse_link_in);
168                         return (in.header.opcode == FUSE_LINK &&
169                                 in.body.link.oldnodeid == dst_ino &&
170                                 (0 == strcmp(name, RELPATH)));
171                 }, Eq(true)),
172                 _)
173         ).WillOnce(Invoke(ReturnErrno(EMLINK)));
174
175         EXPECT_EQ(-1, link(FULLDST, FULLPATH));
176         EXPECT_EQ(EMLINK, errno);
177 }
178
179 TEST_F(Link, ok)
180 {
181         const char FULLPATH[] = "mountpoint/src";
182         const char RELPATH[] = "src";
183         const char FULLDST[] = "mountpoint/dst";
184         const char RELDST[] = "dst";
185         const uint64_t ino = 42;
186         mode_t mode = S_IFREG | 0644;
187         struct stat sb;
188
189         EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH)
190         .WillOnce(Invoke(ReturnErrno(ENOENT)));
191         EXPECT_LOOKUP(FUSE_ROOT_ID, RELDST)
192         .WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
193                 SET_OUT_HEADER_LEN(out, entry);
194                 out.body.entry.attr.mode = mode;
195                 out.body.entry.nodeid = ino;
196                 out.body.entry.attr.nlink = 1;
197                 out.body.entry.attr_valid = UINT64_MAX;
198                 out.body.entry.entry_valid = UINT64_MAX;
199         })));
200         expect_link(ino, RELPATH, mode, 2);
201
202         ASSERT_EQ(0, link(FULLDST, FULLPATH)) << strerror(errno);
203         // Check that the original file's nlink count has increased.
204         ASSERT_EQ(0, stat(FULLDST, &sb)) << strerror(errno);
205         EXPECT_EQ(2ul, sb.st_nlink);
206 }
207
208 TEST_F(Link_7_8, ok)
209 {
210         const char FULLPATH[] = "mountpoint/src";
211         const char RELPATH[] = "src";
212         const char FULLDST[] = "mountpoint/dst";
213         const char RELDST[] = "dst";
214         const uint64_t ino = 42;
215         mode_t mode = S_IFREG | 0644;
216         struct stat sb;
217
218         EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH)
219         .WillOnce(Invoke(ReturnErrno(ENOENT)));
220         EXPECT_LOOKUP(FUSE_ROOT_ID, RELDST)
221         .WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
222                 SET_OUT_HEADER_LEN(out, entry_7_8);
223                 out.body.entry.attr.mode = mode;
224                 out.body.entry.nodeid = ino;
225                 out.body.entry.attr.nlink = 1;
226                 out.body.entry.attr_valid = UINT64_MAX;
227                 out.body.entry.entry_valid = UINT64_MAX;
228         })));
229         expect_link(ino, RELPATH, mode, 2);
230
231         ASSERT_EQ(0, link(FULLDST, FULLPATH)) << strerror(errno);
232         // Check that the original file's nlink count has increased.
233         ASSERT_EQ(0, stat(FULLDST, &sb)) << strerror(errno);
234         EXPECT_EQ(2ul, sb.st_nlink);
235 }