]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tests/sys/fs/fuse/xattr.cc
fuse(4): combine common code in the tests
[FreeBSD/FreeBSD.git] / tests / sys / fs / fuse / xattr.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 <sys/types.h>
33 #include <sys/extattr.h>
34 #include <string.h>
35 }
36
37 #include "mockfs.hh"
38 #include "utils.hh"
39
40 using namespace testing;
41
42 const char FULLPATH[] = "mountpoint/some_file.txt";
43 const char RELPATH[] = "some_file.txt";
44
45 /* For testing filesystems without posix locking support */
46 class Xattr: public FuseTest {
47 public:
48 void expect_getxattr(uint64_t ino, const char *attr, ProcessMockerT r)
49 {
50         EXPECT_CALL(*m_mock, process(
51                 ResultOf([=](auto in) {
52                         const char *a = (const char*)in->body.bytes +
53                                 sizeof(fuse_getxattr_in);
54                         return (in->header.opcode == FUSE_GETXATTR &&
55                                 in->header.nodeid == ino &&
56                                 0 == strcmp(attr, a));
57                 }, Eq(true)),
58                 _)
59         ).WillOnce(Invoke(r));
60 }
61
62 void expect_listxattr(uint64_t ino, uint32_t size, ProcessMockerT r)
63 {
64         EXPECT_CALL(*m_mock, process(
65                 ResultOf([=](auto in) {
66                         return (in->header.opcode == FUSE_LISTXATTR &&
67                                 in->header.nodeid == ino &&
68                                 in->body.listxattr.size == size);
69                 }, Eq(true)),
70                 _)
71         ).WillOnce(Invoke(r))
72         .RetiresOnSaturation();
73 }
74
75 void expect_removexattr(uint64_t ino, const char *attr, int error)
76 {
77         EXPECT_CALL(*m_mock, process(
78                 ResultOf([=](auto in) {
79                         const char *a = (const char*)in->body.bytes;
80                         return (in->header.opcode == FUSE_REMOVEXATTR &&
81                                 in->header.nodeid == ino &&
82                                 0 == strcmp(attr, a));
83                 }, Eq(true)),
84                 _)
85         ).WillOnce(Invoke(ReturnErrno(error)));
86 }
87
88 void expect_setxattr(uint64_t ino, const char *attr, const char *value,
89         ProcessMockerT r)
90 {
91         EXPECT_CALL(*m_mock, process(
92                 ResultOf([=](auto in) {
93                         const char *a = (const char*)in->body.bytes +
94                                 sizeof(fuse_setxattr_in);
95                         const char *v = a + strlen(a) + 1;
96                         return (in->header.opcode == FUSE_SETXATTR &&
97                                 in->header.nodeid == ino &&
98                                 0 == strcmp(attr, a) &&
99                                 0 == strcmp(value, v));
100                 }, Eq(true)),
101                 _)
102         ).WillOnce(Invoke(r));
103 }
104
105 };
106
107 class Getxattr: public Xattr {};
108 class Listxattr: public Xattr {};
109 class Removexattr: public Xattr {};
110 class Setxattr: public Xattr {};
111
112 /* 
113  * If the extended attribute does not exist on this file, the daemon should
114  * return ENOATTR (ENODATA on Linux, but it's up to the daemon to choose the
115  * correct errror code)
116  */
117 TEST_F(Getxattr, enoattr)
118 {
119         char data[80];
120         uint64_t ino = 42;
121         int ns = EXTATTR_NAMESPACE_USER;
122         ssize_t r;
123
124         expect_lookup(RELPATH, ino, S_IFREG | 0644, 1);
125         expect_getxattr(ino, "user.foo", ReturnErrno(ENOATTR));
126
127         r = extattr_get_file(FULLPATH, ns, "foo", data, sizeof(data));
128         ASSERT_EQ(-1, r);
129         ASSERT_EQ(ENOATTR, errno);
130 }
131
132 /*
133  * On FreeBSD, if the user passes an insufficiently large buffer then the
134  * filesystem is supposed to copy as much of the attribute's value as will fit.
135  *
136  * On Linux, however, the filesystem is supposed to return ERANGE.
137  *
138  * libfuse specifies the Linux behavior.  However, that's probably an error.
139  * It would probably be correct for the filesystem to use platform-dependent
140  * behavior.
141  *
142  * This test case covers a filesystem that uses the Linux behavior
143  */
144 TEST_F(Getxattr, erange)
145 {
146         char data[10];
147         uint64_t ino = 42;
148         int ns = EXTATTR_NAMESPACE_USER;
149         ssize_t r;
150
151         expect_lookup(RELPATH, ino, S_IFREG | 0644, 1);
152         expect_getxattr(ino, "user.foo", ReturnErrno(ERANGE));
153
154         r = extattr_get_file(FULLPATH, ns, "foo", data, sizeof(data));
155         ASSERT_EQ(-1, r);
156         ASSERT_EQ(ERANGE, errno);
157 }
158
159 /*
160  * If the user passes a 0-length buffer, then the daemon should just return the
161  * size of the attribute
162  */
163 TEST_F(Getxattr, size_only)
164 {
165         uint64_t ino = 42;
166         int ns = EXTATTR_NAMESPACE_USER;
167
168         expect_lookup(RELPATH, ino, S_IFREG | 0644, 1);
169         expect_getxattr(ino, "user.foo",
170                 ReturnImmediate([](auto in __unused, auto out) {
171                         SET_OUT_HEADER_LEN(out, getxattr);
172                         out->body.getxattr.size = 99;
173                 })
174         );
175
176         ASSERT_EQ(99, extattr_get_file(FULLPATH, ns, "foo", NULL, 0))
177                 << strerror(errno);;
178 }
179
180 /*
181  * Successfully get an attribute from the system namespace
182  */
183 TEST_F(Getxattr, system)
184 {
185         uint64_t ino = 42;
186         char data[80];
187         const char value[] = "whatever";
188         ssize_t value_len = strlen(value) + 1;
189         int ns = EXTATTR_NAMESPACE_SYSTEM;
190         ssize_t r;
191
192         expect_lookup(RELPATH, ino, S_IFREG | 0644, 1);
193         expect_getxattr(ino, "system.foo",
194                 ReturnImmediate([&](auto in __unused, auto out) {
195                         memcpy((void*)out->body.bytes, value, value_len);
196                         out->header.len = sizeof(out->header) + value_len;
197                 })
198         );
199
200         r = extattr_get_file(FULLPATH, ns, "foo", data, sizeof(data));
201         ASSERT_EQ(value_len, r)  << strerror(errno);
202         EXPECT_STREQ(value, data);
203 }
204
205 /*
206  * Successfully get an attribute from the user namespace
207  */
208 TEST_F(Getxattr, user)
209 {
210         uint64_t ino = 42;
211         char data[80];
212         const char value[] = "whatever";
213         ssize_t value_len = strlen(value) + 1;
214         int ns = EXTATTR_NAMESPACE_USER;
215         ssize_t r;
216
217         expect_lookup(RELPATH, ino, S_IFREG | 0644, 1);
218         expect_getxattr(ino, "user.foo",
219                 ReturnImmediate([&](auto in __unused, auto out) {
220                         memcpy((void*)out->body.bytes, value, value_len);
221                         out->header.len = sizeof(out->header) + value_len;
222                 })
223         );
224
225         r = extattr_get_file(FULLPATH, ns, "foo", data, sizeof(data));
226         ASSERT_EQ(value_len, r)  << strerror(errno);
227         EXPECT_STREQ(value, data);
228 }
229
230 /*
231  * Listing extended attributes failed because they aren't configured on this
232  * filesystem
233  */
234 TEST_F(Listxattr, enotsup)
235 {
236         uint64_t ino = 42;
237         int ns = EXTATTR_NAMESPACE_USER;
238
239         expect_lookup(RELPATH, ino, S_IFREG | 0644, 1);
240         expect_listxattr(ino, 0, ReturnErrno(ENOTSUP));
241
242         ASSERT_EQ(-1, extattr_list_file(FULLPATH, ns, NULL, 0));
243         ASSERT_EQ(ENOTSUP, errno);
244 }
245
246 /*
247  * On FreeBSD, if the user passes an insufficiently large buffer then the
248  * filesystem is supposed to copy as much of the attribute's value as will fit.
249  *
250  * On Linux, however, the filesystem is supposed to return ERANGE.
251  *
252  * libfuse specifies the Linux behavior.  However, that's probably an error.
253  * It would probably be correct for the filesystem to use platform-dependent
254  * behavior.
255  *
256  * This test case covers a filesystem that uses the Linux behavior
257  */
258 TEST_F(Listxattr, erange)
259 {
260         uint64_t ino = 42;
261         int ns = EXTATTR_NAMESPACE_USER;
262
263         expect_lookup(RELPATH, ino, S_IFREG | 0644, 1);
264         expect_listxattr(ino, 0, ReturnErrno(ERANGE));
265
266         ASSERT_EQ(-1, extattr_list_file(FULLPATH, ns, NULL, 0));
267         ASSERT_EQ(ERANGE, errno);
268 }
269
270 /*
271  * Get the size of the list that it would take to list no extended attributes
272  */
273 TEST_F(Listxattr, size_only_empty)
274 {
275         uint64_t ino = 42;
276         int ns = EXTATTR_NAMESPACE_USER;
277
278         expect_lookup(RELPATH, ino, S_IFREG | 0644, 1);
279         expect_listxattr(ino, 0, ReturnImmediate([](auto i __unused, auto out) {
280                 out->body.listxattr.size = 0;
281                 SET_OUT_HEADER_LEN(out, listxattr);
282         }));
283
284         ASSERT_EQ(0, extattr_list_file(FULLPATH, ns, NULL, 0))
285                 << strerror(errno);
286 }
287
288 /*
289  * Get the size of the list that it would take to list some extended
290  * attributes.  Due to the format differences between a FreeBSD and a
291  * Linux/FUSE extended attribute list, fuse(4) will actually allocate a buffer
292  * and get the whole list, then convert it, just to figure out its size.
293  */
294 TEST_F(Listxattr, size_only_nonempty)
295 {
296         uint64_t ino = 42;
297         int ns = EXTATTR_NAMESPACE_USER;
298
299         expect_lookup(RELPATH, ino, S_IFREG | 0644, 1);
300         expect_listxattr(ino, 0, ReturnImmediate([](auto i __unused, auto out) {
301                 out->body.listxattr.size = 45;
302                 SET_OUT_HEADER_LEN(out, listxattr);
303         }));
304
305         // TODO: fix the expected size after fixing the size calculation bug in
306         // fuse_vnop_listextattr.  It should be exactly 45.
307         expect_listxattr(ino, 53,
308                 ReturnImmediate([](auto in __unused, auto out) {
309                         const char l[] = "user.foo";
310                         strlcpy((char*)out->body.bytes, l,
311                                 sizeof(out->body.bytes));
312                         out->header.len = sizeof(fuse_out_header) + sizeof(l);
313                 })
314         );
315
316         ASSERT_EQ(4, extattr_list_file(FULLPATH, ns, NULL, 0))
317                 << strerror(errno);
318 }
319
320 TEST_F(Listxattr, size_only_really_big)
321 {
322         uint64_t ino = 42;
323         int ns = EXTATTR_NAMESPACE_USER;
324
325         expect_lookup(RELPATH, ino, S_IFREG | 0644, 1);
326         expect_listxattr(ino, 0, ReturnImmediate([](auto i __unused, auto out) {
327                 out->body.listxattr.size = 16000;
328                 SET_OUT_HEADER_LEN(out, listxattr);
329         }));
330
331         // TODO: fix the expected size after fixing the size calculation bug in
332         // fuse_vnop_listextattr.  It should be exactly 16000.
333         expect_listxattr(ino, 16008,
334                 ReturnImmediate([](auto in __unused, auto out) {
335                         const char l[16] = "user.foobarbang";
336                         for (int i=0; i < 1000; i++) {
337                                 memcpy(&out->body.bytes[16 * i], l, 16);
338                         }
339                         out->header.len = sizeof(fuse_out_header) + 16000;
340                 })
341         );
342
343         ASSERT_EQ(11000, extattr_list_file(FULLPATH, ns, NULL, 0))
344                 << strerror(errno);
345 }
346
347 /* 
348  * List all of the user attributes of a file which has both user and system
349  * attributes
350  */
351 TEST_F(Listxattr, user)
352 {
353         uint64_t ino = 42;
354         int ns = EXTATTR_NAMESPACE_USER;
355         char data[80];
356         char expected[9] = {3, 'f', 'o', 'o', 4, 'b', 'a', 'n', 'g'};
357         char attrs[28] = "user.foo\0system.x\0user.bang";
358
359         expect_lookup(RELPATH, ino, S_IFREG | 0644, 1);
360         expect_listxattr(ino, 0,
361                 ReturnImmediate([&](auto in __unused, auto out) {
362                         out->body.listxattr.size = sizeof(attrs);
363                         SET_OUT_HEADER_LEN(out, listxattr);
364                 })
365         );
366
367         // TODO: fix the expected size after fixing the size calculation bug in
368         // fuse_vnop_listextattr.
369         expect_listxattr(ino, sizeof(attrs) + 8,
370         ReturnImmediate([&](auto in __unused, auto out) {
371                 memcpy((void*)out->body.bytes, attrs, sizeof(attrs));
372                 out->header.len = sizeof(fuse_out_header) + sizeof(attrs);
373         }));
374
375         ASSERT_EQ((ssize_t)sizeof(expected),
376                 extattr_list_file(FULLPATH, ns, data, sizeof(data)))
377                 << strerror(errno);
378         ASSERT_EQ(0, memcmp(expected, data, sizeof(expected)));
379 }
380
381 /* 
382  * List all of the system attributes of a file which has both user and system
383  * attributes
384  */
385 TEST_F(Listxattr, system)
386 {
387         uint64_t ino = 42;
388         int ns = EXTATTR_NAMESPACE_SYSTEM;
389         char data[80];
390         char expected[2] = {1, 'x'};
391         char attrs[28] = "user.foo\0system.x\0user.bang";
392
393         expect_lookup(RELPATH, ino, S_IFREG | 0644, 1);
394         expect_listxattr(ino, 0,
395                 ReturnImmediate([&](auto in __unused, auto out) {
396                         out->body.listxattr.size = sizeof(attrs);
397                         SET_OUT_HEADER_LEN(out, listxattr);
398                 })
399         );
400
401         // TODO: fix the expected size after fixing the size calculation bug in
402         // fuse_vnop_listextattr.
403         expect_listxattr(ino, sizeof(attrs) + 8,
404         ReturnImmediate([&](auto in __unused, auto out) {
405                 memcpy((void*)out->body.bytes, attrs, sizeof(attrs));
406                 out->header.len = sizeof(fuse_out_header) + sizeof(attrs);
407         }));
408
409         ASSERT_EQ((ssize_t)sizeof(expected),
410                 extattr_list_file(FULLPATH, ns, data, sizeof(data)))
411                 << strerror(errno);
412         ASSERT_EQ(0, memcmp(expected, data, sizeof(expected)));
413 }
414
415 /* Fail to remove a nonexistent attribute */
416 TEST_F(Removexattr, enoattr)
417 {
418         uint64_t ino = 42;
419         int ns = EXTATTR_NAMESPACE_USER;
420
421         expect_lookup(RELPATH, ino, S_IFREG | 0644, 1);
422         expect_removexattr(ino, "user.foo", ENOATTR);
423
424         ASSERT_EQ(-1, extattr_delete_file(FULLPATH, ns, "foo"));
425         ASSERT_EQ(ENOATTR, errno);
426 }
427
428 /* Successfully remove a user xattr */
429 TEST_F(Removexattr, user)
430 {
431         uint64_t ino = 42;
432         int ns = EXTATTR_NAMESPACE_USER;
433
434         expect_lookup(RELPATH, ino, S_IFREG | 0644, 1);
435         expect_removexattr(ino, "user.foo", 0);
436
437         ASSERT_EQ(0, extattr_delete_file(FULLPATH, ns, "foo"))
438                 << strerror(errno);
439 }
440
441 /* Successfully remove a system xattr */
442 TEST_F(Removexattr, system)
443 {
444         uint64_t ino = 42;
445         int ns = EXTATTR_NAMESPACE_SYSTEM;
446
447         expect_lookup(RELPATH, ino, S_IFREG | 0644, 1);
448         expect_removexattr(ino, "system.foo", 0);
449
450         ASSERT_EQ(0, extattr_delete_file(FULLPATH, ns, "foo"))
451                 << strerror(errno);
452 }
453
454 /*
455  * SETXATTR will return ENOTSUP if the namespace is invalid or the filesystem
456  * as currently configured doesn't support extended attributes.
457  */
458 TEST_F(Setxattr, enotsup)
459 {
460         uint64_t ino = 42;
461         const char value[] = "whatever";
462         ssize_t value_len = strlen(value) + 1;
463         int ns = EXTATTR_NAMESPACE_USER;
464         ssize_t r;
465
466         expect_lookup(RELPATH, ino, S_IFREG | 0644, 1);
467         expect_setxattr(ino, "user.foo", value, ReturnErrno(ENOTSUP));
468
469         r = extattr_set_file(FULLPATH, ns, "foo", (void*)value, value_len);
470         ASSERT_EQ(-1, r);
471         EXPECT_EQ(ENOTSUP, errno);
472 }
473
474 /*
475  * Successfully set a user attribute.
476  */
477 TEST_F(Setxattr, user)
478 {
479         uint64_t ino = 42;
480         const char value[] = "whatever";
481         ssize_t value_len = strlen(value) + 1;
482         int ns = EXTATTR_NAMESPACE_USER;
483         ssize_t r;
484
485         expect_lookup(RELPATH, ino, S_IFREG | 0644, 1);
486         expect_setxattr(ino, "user.foo", value, ReturnErrno(0));
487
488         r = extattr_set_file(FULLPATH, ns, "foo", (void*)value, value_len);
489         ASSERT_EQ(value_len, r) << strerror(errno);
490 }
491
492 /*
493  * Successfully set a system attribute.
494  */
495 TEST_F(Setxattr, system)
496 {
497         uint64_t ino = 42;
498         const char value[] = "whatever";
499         ssize_t value_len = strlen(value) + 1;
500         int ns = EXTATTR_NAMESPACE_SYSTEM;
501         ssize_t r;
502
503         expect_lookup(RELPATH, ino, S_IFREG | 0644, 1);
504         expect_setxattr(ino, "system.foo", value, ReturnErrno(0));
505
506         r = extattr_set_file(FULLPATH, ns, "foo", (void*)value, value_len);
507         ASSERT_EQ(value_len, r) << strerror(errno);
508 }