1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2016 Fujitsu Ltd.
4 * Author: Xiao Yang <yangx.jy@cn.fujitsu.com>
5 */
6
7 /*
8 * Test Name: llistxattr01
9 *
10 * Description:
11 * The testcase checks the basic functionality of the llistxattr(2).
12 * llistxattr(2) retrieves the list of extended attribute names
13 * associated with the link itself in the filesystem.
14 *
15 */
16
17 #include "config.h"
18 #include <errno.h>
19 #include <sys/types.h>
20 #include <string.h>
21
22 #ifdef HAVE_SYS_XATTR_H
23 # include <sys/xattr.h>
24 #endif
25
26 #include "tst_test.h"
27
28 #ifdef HAVE_SYS_XATTR_H
29
30 #define SECURITY_KEY1 "security.ltptest1"
31 #define SECURITY_KEY2 "security.ltptest2"
32 #define VALUE "test"
33 #define VALUE_SIZE (sizeof(VALUE) - 1)
34 #define KEY_SIZE (sizeof(SECURITY_KEY1) - 1)
35 #define TESTFILE "testfile"
36 #define SYMLINK "symlink"
37
38
has_attribute(const char * list,int llen,const char * attr)39 static int has_attribute(const char *list, int llen, const char *attr)
40 {
41 int i;
42
43 for (i = 0; i < llen; i += strlen(list + i) + 1) {
44 if (!strcmp(list + i, attr))
45 return 1;
46 }
47 return 0;
48 }
49
verify_llistxattr(void)50 static void verify_llistxattr(void)
51 {
52 char buf[64];
53
54 TEST(llistxattr(SYMLINK, buf, sizeof(buf)));
55 if (TST_RET == -1) {
56 tst_res(TFAIL | TTERRNO, "llistxattr() failed");
57 return;
58 }
59
60 if (has_attribute(buf, sizeof(buf), SECURITY_KEY1)) {
61 tst_res(TFAIL, "get file attribute %s unexpectlly",
62 SECURITY_KEY1);
63 return;
64 }
65
66 if (!has_attribute(buf, sizeof(buf), SECURITY_KEY2)) {
67 tst_res(TFAIL, "missing attribute %s", SECURITY_KEY2);
68 return;
69 }
70
71 tst_res(TPASS, "llistxattr() succeeded");
72 }
73
setup(void)74 static void setup(void)
75 {
76 SAFE_TOUCH(TESTFILE, 0644, NULL);
77
78 SAFE_SYMLINK(TESTFILE, SYMLINK);
79
80 SAFE_LSETXATTR(TESTFILE, SECURITY_KEY1, VALUE, VALUE_SIZE, XATTR_CREATE);
81
82 SAFE_LSETXATTR(SYMLINK, SECURITY_KEY2, VALUE, VALUE_SIZE, XATTR_CREATE);
83 }
84
85 static struct tst_test test = {
86 .needs_tmpdir = 1,
87 .needs_root = 1,
88 .test_all = verify_llistxattr,
89 .setup = setup,
90 };
91
92 #else
93 TST_TEST_TCONF("<sys/xattr.h> does not exist.");
94 #endif /* HAVE_SYS_XATTR_H */
95
96