Skip to content

Commit 70619d4

Browse files
committed
selftests/kernfs: test xattr retrieval
Make sure that listxattr() returns zero and that getxattr() returns ENODATA when no extended attributs are set. Use /sys/kernel/warn_count as that always exists and is a read-only file. Link: https://lore.kernel.org/20250702-hochmoderne-abklatsch-af9c605b57b2@brauner Signed-off-by: Christian Brauner <[email protected]>
1 parent 05a0393 commit 70619d4

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

tools/testing/selftests/filesystems/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ dnotify_test
33
devpts_pts
44
file_stressor
55
anon_inode_test
6+
kernfs_test
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# SPDX-License-Identifier: GPL-2.0
22

33
CFLAGS += $(KHDR_INCLUDES)
4-
TEST_GEN_PROGS := devpts_pts file_stressor anon_inode_test
4+
TEST_GEN_PROGS := devpts_pts file_stressor anon_inode_test kernfs_test
55
TEST_GEN_PROGS_EXTENDED := dnotify_test
66

77
include ../lib.mk
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#define _GNU_SOURCE
3+
#define __SANE_USERSPACE_TYPES__
4+
5+
#include <fcntl.h>
6+
#include <stdio.h>
7+
#include <sys/stat.h>
8+
#include <sys/xattr.h>
9+
10+
#include "../kselftest_harness.h"
11+
#include "wrappers.h"
12+
13+
TEST(kernfs_listxattr)
14+
{
15+
int fd;
16+
17+
/* Read-only file that can never have any extended attributes set. */
18+
fd = open("/sys/kernel/warn_count", O_RDONLY | O_CLOEXEC);
19+
ASSERT_GE(fd, 0);
20+
ASSERT_EQ(flistxattr(fd, NULL, 0), 0);
21+
EXPECT_EQ(close(fd), 0);
22+
}
23+
24+
TEST(kernfs_getxattr)
25+
{
26+
int fd;
27+
char buf[1];
28+
29+
/* Read-only file that can never have any extended attributes set. */
30+
fd = open("/sys/kernel/warn_count", O_RDONLY | O_CLOEXEC);
31+
ASSERT_GE(fd, 0);
32+
ASSERT_LT(fgetxattr(fd, "user.foo", buf, sizeof(buf)), 0);
33+
ASSERT_EQ(errno, ENODATA);
34+
EXPECT_EQ(close(fd), 0);
35+
}
36+
37+
TEST_HARNESS_MAIN
38+

0 commit comments

Comments
 (0)