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: flistxattr03
9 *
10 * Description:
11 * flistxattr is identical to listxattr. an empty buffer of size zero
12 * can return the current size of the list of extended attribute names,
13 * which can be used to estimate a suitable buffer.
14 */
15
16 #include "config.h"
17 #include <errno.h>
18 #include <sys/types.h>
19
20 #ifdef HAVE_SYS_XATTR_H
21 # include <sys/xattr.h>
22 #endif
23
24 #include "tst_test.h"
25
26 #ifdef HAVE_SYS_XATTR_H
27
28 #define SECURITY_KEY "security.ltptest"
29 #define VALUE "test"
30 #define VALUE_SIZE (sizeof(VALUE) - 1)
31
32 static int fd[] = {0, 0};
33
check_suitable_buf(const int file,long size)34 static int check_suitable_buf(const int file, long size)
35 {
36 int n;
37 char buf[size];
38
39 n = flistxattr(file, buf, sizeof(buf));
40
41 return n != -1;
42 }
43
verify_flistxattr(unsigned int n)44 static void verify_flistxattr(unsigned int n)
45 {
46 TEST(flistxattr(fd[n], NULL, 0));
47 if (TST_RET == -1) {
48 tst_res(TFAIL | TTERRNO, "flistxattr() failed");
49 return;
50 }
51
52 if (check_suitable_buf(fd[n], TST_RET))
53 tst_res(TPASS, "flistxattr() succeed with suitable buffer");
54 else
55 tst_res(TFAIL, "flistxattr() failed with small buffer");
56 }
57
setup(void)58 static void setup(void)
59 {
60 fd[0] = SAFE_OPEN("testfile1", O_RDWR | O_CREAT, 0644);
61
62 fd[1] = SAFE_OPEN("testfile2", O_RDWR | O_CREAT, 0644);
63
64 SAFE_FSETXATTR(fd[1], SECURITY_KEY, VALUE, VALUE_SIZE, XATTR_CREATE);
65 }
66
cleanup(void)67 static void cleanup(void)
68 {
69 SAFE_CLOSE(fd[1]);
70 SAFE_CLOSE(fd[0]);
71 }
72
73 static struct tst_test test = {
74 .needs_tmpdir = 1,
75 .needs_root = 1,
76 .test = verify_flistxattr,
77 .tcnt = ARRAY_SIZE(fd),
78 .setup = setup,
79 .cleanup = cleanup,
80 };
81
82 #else /* HAVE_SYS_XATTR_H */
83 TST_TEST_TCONF("<sys/xattr.h> does not exist.");
84 #endif
85