2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2019 The FreeBSD Foundation
6 * This software was developed by BFF Storage Systems, LLC under sponsorship
7 * from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/types.h>
33 #include <sys/extattr.h>
40 using namespace testing;
42 const char FULLPATH[] = "mountpoint/some_file.txt";
43 const char RELPATH[] = "some_file.txt";
45 /* For testing filesystems without posix locking support */
46 class Xattr: public FuseTest {
48 void expect_getxattr(uint64_t ino, const char *attr, ProcessMockerT r)
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));
59 ).WillOnce(Invoke(r));
62 void expect_listxattr(uint64_t ino, uint32_t size, ProcessMockerT r)
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);
72 .RetiresOnSaturation();
75 void expect_removexattr(uint64_t ino, const char *attr, int error)
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));
85 ).WillOnce(Invoke(ReturnErrno(error)));
88 void expect_setxattr(uint64_t ino, const char *attr, const char *value,
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));
102 ).WillOnce(Invoke(r));
107 class Getxattr: public Xattr {};
108 class Listxattr: public Xattr {};
109 class Removexattr: public Xattr {};
110 class Setxattr: public Xattr {};
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)
117 TEST_F(Getxattr, enoattr)
121 int ns = EXTATTR_NAMESPACE_USER;
124 expect_lookup(RELPATH, ino, S_IFREG | 0644, 0, 1);
125 expect_getxattr(ino, "user.foo", ReturnErrno(ENOATTR));
127 r = extattr_get_file(FULLPATH, ns, "foo", data, sizeof(data));
129 ASSERT_EQ(ENOATTR, errno);
133 * If the filesystem returns ENOSYS, then it will be treated as a permanent
134 * failure and all future VOP_GETEXTATTR calls will fail with EOPNOTSUPP
135 * without querying the filesystem daemon
137 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236557 */
138 TEST_F(Getxattr, DISABLED_enosys)
142 int ns = EXTATTR_NAMESPACE_USER;
145 expect_lookup(RELPATH, ino, S_IFREG | 0644, 0, 1);
146 expect_getxattr(ino, "user.foo", ReturnErrno(ENOSYS));
148 r = extattr_get_file(FULLPATH, ns, "foo", data, sizeof(data));
150 EXPECT_EQ(EOPNOTSUPP, errno);
152 /* Subsequent attempts should not query the filesystem at all */
153 r = extattr_get_file(FULLPATH, ns, "foo", data, sizeof(data));
155 EXPECT_EQ(EOPNOTSUPP, errno);
159 * On FreeBSD, if the user passes an insufficiently large buffer then the
160 * filesystem is supposed to copy as much of the attribute's value as will fit.
162 * On Linux, however, the filesystem is supposed to return ERANGE.
164 * libfuse specifies the Linux behavior. However, that's probably an error.
165 * It would probably be correct for the filesystem to use platform-dependent
168 * This test case covers a filesystem that uses the Linux behavior
170 TEST_F(Getxattr, erange)
174 int ns = EXTATTR_NAMESPACE_USER;
177 expect_lookup(RELPATH, ino, S_IFREG | 0644, 0, 1);
178 expect_getxattr(ino, "user.foo", ReturnErrno(ERANGE));
180 r = extattr_get_file(FULLPATH, ns, "foo", data, sizeof(data));
182 ASSERT_EQ(ERANGE, errno);
186 * If the user passes a 0-length buffer, then the daemon should just return the
187 * size of the attribute
189 TEST_F(Getxattr, size_only)
192 int ns = EXTATTR_NAMESPACE_USER;
194 expect_lookup(RELPATH, ino, S_IFREG | 0644, 0, 1);
195 expect_getxattr(ino, "user.foo",
196 ReturnImmediate([](auto in __unused, auto out) {
197 SET_OUT_HEADER_LEN(out, getxattr);
198 out->body.getxattr.size = 99;
202 ASSERT_EQ(99, extattr_get_file(FULLPATH, ns, "foo", NULL, 0))
207 * Successfully get an attribute from the system namespace
209 TEST_F(Getxattr, system)
213 const char value[] = "whatever";
214 ssize_t value_len = strlen(value) + 1;
215 int ns = EXTATTR_NAMESPACE_SYSTEM;
218 expect_lookup(RELPATH, ino, S_IFREG | 0644, 0, 1);
219 expect_getxattr(ino, "system.foo",
220 ReturnImmediate([&](auto in __unused, auto out) {
221 memcpy((void*)out->body.bytes, value, value_len);
222 out->header.len = sizeof(out->header) + value_len;
226 r = extattr_get_file(FULLPATH, ns, "foo", data, sizeof(data));
227 ASSERT_EQ(value_len, r) << strerror(errno);
228 EXPECT_STREQ(value, data);
232 * Successfully get an attribute from the user namespace
234 TEST_F(Getxattr, user)
238 const char value[] = "whatever";
239 ssize_t value_len = strlen(value) + 1;
240 int ns = EXTATTR_NAMESPACE_USER;
243 expect_lookup(RELPATH, ino, S_IFREG | 0644, 0, 1);
244 expect_getxattr(ino, "user.foo",
245 ReturnImmediate([&](auto in __unused, auto out) {
246 memcpy((void*)out->body.bytes, value, value_len);
247 out->header.len = sizeof(out->header) + value_len;
251 r = extattr_get_file(FULLPATH, ns, "foo", data, sizeof(data));
252 ASSERT_EQ(value_len, r) << strerror(errno);
253 EXPECT_STREQ(value, data);
257 * If the filesystem returns ENOSYS, then it will be treated as a permanent
258 * failure and all future VOP_LISTEXTATTR calls will fail with EOPNOTSUPP
259 * without querying the filesystem daemon
261 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236557 */
262 TEST_F(Listxattr, DISABLED_enosys)
265 int ns = EXTATTR_NAMESPACE_USER;
267 expect_lookup(RELPATH, ino, S_IFREG | 0644, 0, 1);
268 expect_listxattr(ino, 0, ReturnErrno(ENOSYS));
270 ASSERT_EQ(-1, extattr_list_file(FULLPATH, ns, NULL, 0));
271 EXPECT_EQ(EOPNOTSUPP, errno);
273 /* Subsequent attempts should not query the filesystem at all */
274 ASSERT_EQ(-1, extattr_list_file(FULLPATH, ns, NULL, 0));
275 EXPECT_EQ(EOPNOTSUPP, errno);
279 * Listing extended attributes failed because they aren't configured on this
282 TEST_F(Listxattr, enotsup)
285 int ns = EXTATTR_NAMESPACE_USER;
287 expect_lookup(RELPATH, ino, S_IFREG | 0644, 0, 1);
288 expect_listxattr(ino, 0, ReturnErrno(ENOTSUP));
290 ASSERT_EQ(-1, extattr_list_file(FULLPATH, ns, NULL, 0));
291 ASSERT_EQ(ENOTSUP, errno);
295 * On FreeBSD, if the user passes an insufficiently large buffer then the
296 * filesystem is supposed to copy as much of the attribute's value as will fit.
298 * On Linux, however, the filesystem is supposed to return ERANGE.
300 * libfuse specifies the Linux behavior. However, that's probably an error.
301 * It would probably be correct for the filesystem to use platform-dependent
304 * This test case covers a filesystem that uses the Linux behavior
306 TEST_F(Listxattr, erange)
309 int ns = EXTATTR_NAMESPACE_USER;
311 expect_lookup(RELPATH, ino, S_IFREG | 0644, 0, 1);
312 expect_listxattr(ino, 0, ReturnErrno(ERANGE));
314 ASSERT_EQ(-1, extattr_list_file(FULLPATH, ns, NULL, 0));
315 ASSERT_EQ(ERANGE, errno);
319 * Get the size of the list that it would take to list no extended attributes
321 TEST_F(Listxattr, size_only_empty)
324 int ns = EXTATTR_NAMESPACE_USER;
326 expect_lookup(RELPATH, ino, S_IFREG | 0644, 0, 1);
327 expect_listxattr(ino, 0, ReturnImmediate([](auto i __unused, auto out) {
328 out->body.listxattr.size = 0;
329 SET_OUT_HEADER_LEN(out, listxattr);
332 ASSERT_EQ(0, extattr_list_file(FULLPATH, ns, NULL, 0))
337 * Get the size of the list that it would take to list some extended
338 * attributes. Due to the format differences between a FreeBSD and a
339 * Linux/FUSE extended attribute list, fuse(4) will actually allocate a buffer
340 * and get the whole list, then convert it, just to figure out its size.
342 TEST_F(Listxattr, size_only_nonempty)
345 int ns = EXTATTR_NAMESPACE_USER;
347 expect_lookup(RELPATH, ino, S_IFREG | 0644, 0, 1);
348 expect_listxattr(ino, 0, ReturnImmediate([](auto i __unused, auto out) {
349 out->body.listxattr.size = 45;
350 SET_OUT_HEADER_LEN(out, listxattr);
353 // TODO: fix the expected size after fixing the size calculation bug in
354 // fuse_vnop_listextattr. It should be exactly 45.
355 expect_listxattr(ino, 53,
356 ReturnImmediate([](auto in __unused, auto out) {
357 const char l[] = "user.foo";
358 strlcpy((char*)out->body.bytes, l,
359 sizeof(out->body.bytes));
360 out->header.len = sizeof(fuse_out_header) + sizeof(l);
364 ASSERT_EQ(4, extattr_list_file(FULLPATH, ns, NULL, 0))
368 TEST_F(Listxattr, size_only_really_big)
371 int ns = EXTATTR_NAMESPACE_USER;
373 expect_lookup(RELPATH, ino, S_IFREG | 0644, 0, 1);
374 expect_listxattr(ino, 0, ReturnImmediate([](auto i __unused, auto out) {
375 out->body.listxattr.size = 16000;
376 SET_OUT_HEADER_LEN(out, listxattr);
379 // TODO: fix the expected size after fixing the size calculation bug in
380 // fuse_vnop_listextattr. It should be exactly 16000.
381 expect_listxattr(ino, 16008,
382 ReturnImmediate([](auto in __unused, auto out) {
383 const char l[16] = "user.foobarbang";
384 for (int i=0; i < 1000; i++) {
385 memcpy(&out->body.bytes[16 * i], l, 16);
387 out->header.len = sizeof(fuse_out_header) + 16000;
391 ASSERT_EQ(11000, extattr_list_file(FULLPATH, ns, NULL, 0))
396 * List all of the user attributes of a file which has both user and system
399 TEST_F(Listxattr, user)
402 int ns = EXTATTR_NAMESPACE_USER;
404 char expected[9] = {3, 'f', 'o', 'o', 4, 'b', 'a', 'n', 'g'};
405 char attrs[28] = "user.foo\0system.x\0user.bang";
407 expect_lookup(RELPATH, ino, S_IFREG | 0644, 0, 1);
408 expect_listxattr(ino, 0,
409 ReturnImmediate([&](auto in __unused, auto out) {
410 out->body.listxattr.size = sizeof(attrs);
411 SET_OUT_HEADER_LEN(out, listxattr);
415 // TODO: fix the expected size after fixing the size calculation bug in
416 // fuse_vnop_listextattr.
417 expect_listxattr(ino, sizeof(attrs) + 8,
418 ReturnImmediate([&](auto in __unused, auto out) {
419 memcpy((void*)out->body.bytes, attrs, sizeof(attrs));
420 out->header.len = sizeof(fuse_out_header) + sizeof(attrs);
423 ASSERT_EQ((ssize_t)sizeof(expected),
424 extattr_list_file(FULLPATH, ns, data, sizeof(data)))
426 ASSERT_EQ(0, memcmp(expected, data, sizeof(expected)));
430 * List all of the system attributes of a file which has both user and system
433 TEST_F(Listxattr, system)
436 int ns = EXTATTR_NAMESPACE_SYSTEM;
438 char expected[2] = {1, 'x'};
439 char attrs[28] = "user.foo\0system.x\0user.bang";
441 expect_lookup(RELPATH, ino, S_IFREG | 0644, 0, 1);
442 expect_listxattr(ino, 0,
443 ReturnImmediate([&](auto in __unused, auto out) {
444 out->body.listxattr.size = sizeof(attrs);
445 SET_OUT_HEADER_LEN(out, listxattr);
449 // TODO: fix the expected size after fixing the size calculation bug in
450 // fuse_vnop_listextattr.
451 expect_listxattr(ino, sizeof(attrs) + 8,
452 ReturnImmediate([&](auto in __unused, auto out) {
453 memcpy((void*)out->body.bytes, attrs, sizeof(attrs));
454 out->header.len = sizeof(fuse_out_header) + sizeof(attrs);
457 ASSERT_EQ((ssize_t)sizeof(expected),
458 extattr_list_file(FULLPATH, ns, data, sizeof(data)))
460 ASSERT_EQ(0, memcmp(expected, data, sizeof(expected)));
463 /* Fail to remove a nonexistent attribute */
464 TEST_F(Removexattr, enoattr)
467 int ns = EXTATTR_NAMESPACE_USER;
469 expect_lookup(RELPATH, ino, S_IFREG | 0644, 0, 1);
470 expect_removexattr(ino, "user.foo", ENOATTR);
472 ASSERT_EQ(-1, extattr_delete_file(FULLPATH, ns, "foo"));
473 ASSERT_EQ(ENOATTR, errno);
477 * If the filesystem returns ENOSYS, then it will be treated as a permanent
478 * failure and all future VOP_DELETEEXTATTR calls will fail with EOPNOTSUPP
479 * without querying the filesystem daemon
481 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236557 */
482 TEST_F(Removexattr, DISABLED_enosys)
485 int ns = EXTATTR_NAMESPACE_USER;
487 expect_lookup(RELPATH, ino, S_IFREG | 0644, 0, 1);
488 expect_removexattr(ino, "user.foo", ENOSYS);
490 ASSERT_EQ(-1, extattr_delete_file(FULLPATH, ns, "foo"));
491 EXPECT_EQ(EOPNOTSUPP, errno);
493 /* Subsequent attempts should not query the filesystem at all */
494 ASSERT_EQ(-1, extattr_delete_file(FULLPATH, ns, "foo"));
495 EXPECT_EQ(EOPNOTSUPP, errno);
498 /* Successfully remove a user xattr */
499 TEST_F(Removexattr, user)
502 int ns = EXTATTR_NAMESPACE_USER;
504 expect_lookup(RELPATH, ino, S_IFREG | 0644, 0, 1);
505 expect_removexattr(ino, "user.foo", 0);
507 ASSERT_EQ(0, extattr_delete_file(FULLPATH, ns, "foo"))
511 /* Successfully remove a system xattr */
512 TEST_F(Removexattr, system)
515 int ns = EXTATTR_NAMESPACE_SYSTEM;
517 expect_lookup(RELPATH, ino, S_IFREG | 0644, 0, 1);
518 expect_removexattr(ino, "system.foo", 0);
520 ASSERT_EQ(0, extattr_delete_file(FULLPATH, ns, "foo"))
525 * If the filesystem returns ENOSYS, then it will be treated as a permanent
526 * failure and all future VOP_SETEXTATTR calls will fail with EOPNOTSUPP
527 * without querying the filesystem daemon
529 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236557 */
530 TEST_F(Setxattr, DISABLED_enosys)
533 const char value[] = "whatever";
534 ssize_t value_len = strlen(value) + 1;
535 int ns = EXTATTR_NAMESPACE_USER;
538 expect_lookup(RELPATH, ino, S_IFREG | 0644, 0, 1);
539 expect_setxattr(ino, "user.foo", value, ReturnErrno(ENOSYS));
541 r = extattr_set_file(FULLPATH, ns, "foo", (void*)value, value_len);
543 EXPECT_EQ(EOPNOTSUPP, errno);
545 /* Subsequent attempts should not query the filesystem at all */
546 r = extattr_set_file(FULLPATH, ns, "foo", (void*)value, value_len);
548 EXPECT_EQ(EOPNOTSUPP, errno);
552 * SETXATTR will return ENOTSUP if the namespace is invalid or the filesystem
553 * as currently configured doesn't support extended attributes.
555 TEST_F(Setxattr, enotsup)
558 const char value[] = "whatever";
559 ssize_t value_len = strlen(value) + 1;
560 int ns = EXTATTR_NAMESPACE_USER;
563 expect_lookup(RELPATH, ino, S_IFREG | 0644, 0, 1);
564 expect_setxattr(ino, "user.foo", value, ReturnErrno(ENOTSUP));
566 r = extattr_set_file(FULLPATH, ns, "foo", (void*)value, value_len);
568 EXPECT_EQ(ENOTSUP, errno);
572 * Successfully set a user attribute.
574 TEST_F(Setxattr, user)
577 const char value[] = "whatever";
578 ssize_t value_len = strlen(value) + 1;
579 int ns = EXTATTR_NAMESPACE_USER;
582 expect_lookup(RELPATH, ino, S_IFREG | 0644, 0, 1);
583 expect_setxattr(ino, "user.foo", value, ReturnErrno(0));
585 r = extattr_set_file(FULLPATH, ns, "foo", (void*)value, value_len);
586 ASSERT_EQ(value_len, r) << strerror(errno);
590 * Successfully set a system attribute.
592 TEST_F(Setxattr, system)
595 const char value[] = "whatever";
596 ssize_t value_len = strlen(value) + 1;
597 int ns = EXTATTR_NAMESPACE_SYSTEM;
600 expect_lookup(RELPATH, ino, S_IFREG | 0644, 0, 1);
601 expect_setxattr(ino, "system.foo", value, ReturnErrno(0));
603 r = extattr_set_file(FULLPATH, ns, "foo", (void*)value, value_len);
604 ASSERT_EQ(value_len, r) << strerror(errno);