• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * [Description]
9  *
10  * Verify that llistxattr(2) call with zero size returns the current size of the
11  * list of extended attribute names, which can be used to determine the size of
12  * the buffer that should be supplied in a subsequent llistxattr(2) call.
13  */
14 
15 #include "config.h"
16 #include <errno.h>
17 #include <sys/types.h>
18 
19 #ifdef HAVE_SYS_XATTR_H
20 #include <sys/xattr.h>
21 #endif
22 
23 #include "tst_test.h"
24 
25 #ifdef HAVE_SYS_XATTR_H
26 
27 #define SECURITY_KEY	"security.ltptest"
28 #define VALUE           "test"
29 #define VALUE_SIZE      (sizeof(VALUE) - 1)
30 
31 static const char *filename[] = {"testfile1", "testfile2"};
32 
check_suitable_buf(const char * name,long size)33 static int check_suitable_buf(const char *name, long size)
34 {
35 	int n;
36 	char buf[size];
37 
38 	n = llistxattr(name, buf, sizeof(buf));
39 
40 	return n != -1;
41 }
42 
verify_llistxattr(unsigned int n)43 static void verify_llistxattr(unsigned int n)
44 {
45 	const char *name = filename[n];
46 
47 	TEST(llistxattr(name, NULL, 0));
48 	if (TST_RET == -1) {
49 		tst_res(TFAIL | TTERRNO, "llistxattr() failed");
50 		return;
51 	}
52 
53 	if (check_suitable_buf(name, TST_RET))
54 		tst_res(TPASS, "llistxattr() succeed with suitable buffer");
55 	else
56 		tst_res(TFAIL, "llistxattr() failed with small buffer");
57 }
58 
setup(void)59 static void setup(void)
60 {
61 	SAFE_TOUCH(filename[0], 0644, NULL);
62 
63 	SAFE_TOUCH(filename[1], 0644, NULL);
64 
65 	SAFE_LSETXATTR(filename[1], SECURITY_KEY, VALUE, VALUE_SIZE, XATTR_CREATE);
66 }
67 
68 static struct tst_test test = {
69 	.needs_tmpdir = 1,
70 	.needs_root = 1,
71 	.test = verify_llistxattr,
72 	.tcnt = ARRAY_SIZE(filename),
73 	.setup = setup,
74 };
75 
76 #else /* HAVE_SYS_XATTR_H */
77 	TST_TEST_TCONF("<sys/xattr.h> does not exist.");
78 #endif
79