1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2016 RT-RK Institute for Computer Based Systems
4 * Author: Dejan Jovicevic <dejan.jovicevic@rt-rk.com>
5 */
6
7 /*
8 * Test Name: verify_flistxattr01
9 *
10 * Description:
11 * The testcase checks the basic functionality of the flistxattr(2).
12 * flistxattr(2) retrieves the list of extended attribute names
13 * associated with the file 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 VALUE "test"
32 #define VALUE_SIZE (sizeof(VALUE) - 1)
33 #define KEY_SIZE (sizeof(SECURITY_KEY1) - 1)
34
35 static int fd;
36
has_attribute(const char * list,int llen,const char * attr)37 static int has_attribute(const char *list, int llen, const char *attr)
38 {
39 int i;
40
41 for (i = 0; i < llen; i += strlen(list + i) + 1) {
42 if (!strcmp(list + i, attr))
43 return 1;
44 }
45 return 0;
46 }
47
verify_flistxattr(void)48 static void verify_flistxattr(void)
49 {
50 char buf[64];
51
52 TEST(flistxattr(fd, buf, sizeof(buf)));
53 if (TST_RET == -1) {
54 tst_res(TFAIL | TTERRNO, "flistxattr() failed");
55 return;
56 }
57
58 if (!has_attribute(buf, sizeof(buf), SECURITY_KEY1)) {
59 tst_res(TFAIL, "missing attribute %s",
60 SECURITY_KEY1);
61 return;
62 }
63
64 tst_res(TPASS, "flistxattr() succeeded");
65 }
66
setup(void)67 static void setup(void)
68 {
69 fd = SAFE_OPEN("testfile", O_RDWR | O_CREAT, 0644);
70
71 SAFE_FSETXATTR(fd, SECURITY_KEY1, VALUE, VALUE_SIZE, XATTR_CREATE);
72 }
73
cleanup(void)74 static void cleanup(void)
75 {
76 if (fd > 0)
77 SAFE_CLOSE(fd);
78 }
79
80 static struct tst_test test = {
81 .needs_tmpdir = 1,
82 .needs_root = 1,
83 .test_all = verify_flistxattr,
84 .setup = setup,
85 .cleanup = cleanup,
86 };
87
88 #else
89 TST_TEST_TCONF("<sys/xattr.h> does not exist.");
90 #endif /* HAVE_SYS_XATTR_H */
91