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