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: llistxattr03
9 *
10 * Description:
11 * llistxattr 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 const char *filename[] = {"testfile1", "testfile2"};
33
check_suitable_buf(const char * name,long size)34 static int check_suitable_buf(const char *name, long size)
35 {
36 int n;
37 char buf[size];
38
39 n = llistxattr(name, buf, sizeof(buf));
40
41 return n != -1;
42 }
43
verify_llistxattr(unsigned int n)44 static void verify_llistxattr(unsigned int n)
45 {
46 const char *name = filename[n];
47
48 TEST(llistxattr(name, NULL, 0));
49 if (TST_RET == -1) {
50 tst_res(TFAIL | TTERRNO, "llistxattr() failed");
51 return;
52 }
53
54 if (check_suitable_buf(name, TST_RET))
55 tst_res(TPASS, "llistxattr() succeed with suitable buffer");
56 else
57 tst_res(TFAIL, "llistxattr() failed with small buffer");
58 }
59
setup(void)60 static void setup(void)
61 {
62 SAFE_TOUCH(filename[0], 0644, NULL);
63
64 SAFE_TOUCH(filename[1], 0644, NULL);
65
66 SAFE_LSETXATTR(filename[1], SECURITY_KEY, VALUE, VALUE_SIZE, XATTR_CREATE);
67 }
68
69 static struct tst_test test = {
70 .needs_tmpdir = 1,
71 .needs_root = 1,
72 .test = verify_llistxattr,
73 .tcnt = ARRAY_SIZE(filename),
74 .setup = setup,
75 };
76
77 #else /* HAVE_SYS_XATTR_H */
78 TST_TEST_TCONF("<sys/xattr.h> does not exist.");
79 #endif
80