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: llistxattr03
19 *
20 * Description:
21 * llistxattr is identical to listxattr. an empty buffer of size zero
22 * can return the current size of the list of extended attribute names,
23 * which can be used to estimate a suitable buffer.
24 */
25 
26 #include "config.h"
27 #include <errno.h>
28 #include <sys/types.h>
29 
30 #ifdef HAVE_ATTR_XATTR_H
31 # include <attr/xattr.h>
32 #endif
33 
34 #include "tst_test.h"
35 
36 #ifdef HAVE_ATTR_XATTR_H
37 
38 #define SECURITY_KEY	"security.ltptest"
39 #define VALUE	"test"
40 #define VALUE_SIZE	4
41 
42 static const char *filename[] = {"testfile1", "testfile2"};
43 
check_suitable_buf(const char * name,long size)44 static int check_suitable_buf(const char *name, long size)
45 {
46 	int n;
47 	char buf[size];
48 
49 	n = llistxattr(name, buf, size);
50 	if (n == -1)
51 		return 0;
52 	else
53 		return 1;
54 }
55 
verify_llistxattr(unsigned int n)56 static void verify_llistxattr(unsigned int n)
57 {
58 	const char *name = filename[n];
59 
60 	TEST(llistxattr(name, NULL, 0));
61 	if (TEST_RETURN == -1) {
62 		tst_res(TFAIL | TERRNO, "llistxattr() failed");
63 		return;
64 	}
65 
66 	if (check_suitable_buf(name, TEST_RETURN))
67 		tst_res(TPASS, "llistxattr() succeed with suitable buffer");
68 	else
69 		tst_res(TFAIL, "llistxattr() failed with small buffer");
70 }
71 
setup(void)72 static void setup(void)
73 {
74 	int ret;
75 
76 	SAFE_TOUCH(filename[0], 0644, NULL);
77 
78 	SAFE_TOUCH(filename[1], 0644, NULL);
79 
80 	ret = lsetxattr(filename[1], SECURITY_KEY, VALUE, VALUE_SIZE, XATTR_CREATE);
81 	if (ret == -1) {
82 		if (errno == ENOTSUP) {
83 			tst_brk(TCONF, "no xattr support in fs or "
84 				 "mounted without user_xattr option");
85 		} else {
86 			tst_brk(TBROK | TERRNO, "lsetxattr() failed");
87 		}
88 	}
89 }
90 
91 static struct tst_test test = {
92 	.tid = "llistxattr02",
93 	.needs_tmpdir = 1,
94 	.needs_root = 1,
95 	.test = verify_llistxattr,
96 	.tcnt = ARRAY_SIZE(filename),
97 	.setup = setup,
98 };
99 
100 #else /* HAVE_ATTR_XATTR_H */
101 	TST_TEST_TCONF("<attr/xattr.h> does not exist.");
102 #endif
103