1 /*
2 * Copyright (c) 2016 Fujitsu Ltd.
3 * Author: Xiao Yang <yangx.jy@cn.fujitsu.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 *
13 * You should have received a copy of the GNU General Public License
14 * alone with this program.
15 */
16
17 /*
18 * Test Name: llistxattr01
19 *
20 * Description:
21 * The testcase checks the basic functionality of the llistxattr(2).
22 * llistxattr(2) retrieves the list of extended attribute names
23 * associated with the link itself in the filesystem.
24 *
25 */
26
27 #include "config.h"
28 #include <errno.h>
29 #include <sys/types.h>
30 #include <string.h>
31
32 #ifdef HAVE_SYS_XATTR_H
33 # include <sys/xattr.h>
34 #endif
35
36 #include "tst_test.h"
37
38 #ifdef HAVE_SYS_XATTR_H
39
40 #define SECURITY_KEY1 "security.ltptest1"
41 #define SECURITY_KEY2 "security.ltptest2"
42 #define VALUE "test"
43 #define VALUE_SIZE (sizeof(VALUE) - 1)
44 #define KEY_SIZE (sizeof(SECURITY_KEY1) - 1)
45 #define TESTFILE "testfile"
46 #define SYMLINK "symlink"
47
48
has_attribute(const char * list,int llen,const char * attr)49 static int has_attribute(const char *list, int llen, const char *attr)
50 {
51 int i;
52
53 for (i = 0; i < llen; i += strlen(list + i) + 1) {
54 if (!strcmp(list + i, attr))
55 return 1;
56 }
57 return 0;
58 }
59
verify_llistxattr(void)60 static void verify_llistxattr(void)
61 {
62 char buf[64];
63
64 TEST(llistxattr(SYMLINK, buf, sizeof(buf)));
65 if (TST_RET == -1) {
66 tst_res(TFAIL | TTERRNO, "llistxattr() failed");
67 return;
68 }
69
70 if (has_attribute(buf, sizeof(buf), SECURITY_KEY1)) {
71 tst_res(TFAIL, "get file attribute %s unexpectlly",
72 SECURITY_KEY1);
73 return;
74 }
75
76 if (!has_attribute(buf, sizeof(buf), SECURITY_KEY2)) {
77 tst_res(TFAIL, "missing attribute %s", SECURITY_KEY2);
78 return;
79 }
80
81 tst_res(TPASS, "llistxattr() succeeded");
82 }
83
setup(void)84 static void setup(void)
85 {
86 SAFE_TOUCH(TESTFILE, 0644, NULL);
87
88 SAFE_SYMLINK(TESTFILE, SYMLINK);
89
90 SAFE_LSETXATTR(TESTFILE, SECURITY_KEY1, VALUE, VALUE_SIZE, XATTR_CREATE);
91
92 SAFE_LSETXATTR(SYMLINK, SECURITY_KEY2, VALUE, VALUE_SIZE, XATTR_CREATE);
93 }
94
95 static struct tst_test test = {
96 .needs_tmpdir = 1,
97 .needs_root = 1,
98 .test_all = verify_llistxattr,
99 .setup = setup,
100 };
101
102 #else
103 TST_TEST_TCONF("<sys/xattr.h> does not exist.");
104 #endif /* HAVE_SYS_XATTR_H */
105
106