1 /*
2 *  Copyright (c) 2016 RT-RK Institute for Computer Based Systems
3 *  Author: Dejan Jovicevic <dejan.jovicevic@rt-rk.com>
4 *
5 *  This program is free software;  you can redistribute it and/or modify
6 *  it under the terms of the GNU General Public License as published by
7 *  the Free Software Foundation; either version 2 of the License, or
8 *  (at your option) any later version.
9 *
10 *  This program is distributed in the hope that it will be useful,
11 *  but WITHOUT ANY WARRANTY;  without even the implied warranty of
12 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13 *  the GNU General Public License for more details.
14 *
15 *  You should have received a copy of the GNU General Public License
16 *  along with this program.
17 */
18 
19 /*
20 * Test Name: flistxattr03
21 *
22 * Description:
23 * flistxattr is identical to listxattr. an empty buffer of size zero
24 * can return the current size of the list of extended attribute names,
25 * which can be used to estimate a suitable buffer.
26 */
27 
28 #include "config.h"
29 #include <errno.h>
30 #include <sys/types.h>
31 
32 #ifdef HAVE_SYS_XATTR_H
33 # include <sys/xattr.h>
34 #endif
35 
36 #include "tst_test.h"
37 
38 #ifdef HAVE_SYS_XATTR_H
39 
40 #define SECURITY_KEY	"security.ltptest"
41 #define VALUE	"test"
42 #define VALUE_SIZE	(sizeof(VALUE) - 1)
43 
44 static int fd[] = {0, 0};
45 
46 static int check_suitable_buf(const int file, long size)
47 {
48 	int n;
49 	char buf[size];
50 
51 	n = flistxattr(file, buf, sizeof(buf));
52 
53 	return n != -1;
54 }
55 
56 static void verify_flistxattr(unsigned int n)
57 {
58 	TEST(flistxattr(fd[n], NULL, 0));
59 	if (TEST_RETURN == -1) {
60 		tst_res(TFAIL | TTERRNO, "flistxattr() failed");
61 		return;
62 	}
63 
64 	if (check_suitable_buf(fd[n], TEST_RETURN))
65 		tst_res(TPASS, "flistxattr() succeed with suitable buffer");
66 	else
67 		tst_res(TFAIL, "flistxattr() failed with small buffer");
68 }
69 
70 static void setup(void)
71 {
72 	fd[0] = SAFE_OPEN("testfile1", O_RDWR | O_CREAT, 0644);
73 
74 	fd[1] = SAFE_OPEN("testfile2", O_RDWR | O_CREAT, 0644);
75 
76 	SAFE_FSETXATTR(fd[1], SECURITY_KEY, VALUE, VALUE_SIZE, XATTR_CREATE);
77 }
78 
79 static void cleanup(void)
80 {
81 	SAFE_CLOSE(fd[1]);
82 	SAFE_CLOSE(fd[0]);
83 }
84 
85 static struct tst_test test = {
86 	.needs_tmpdir = 1,
87 	.needs_root = 1,
88 	.test = verify_flistxattr,
89 	.tcnt = ARRAY_SIZE(fd),
90 	.setup = setup,
91 	.cleanup = cleanup,
92 };
93 
94 #else /* HAVE_SYS_XATTR_H */
95 	TST_TEST_TCONF("<sys/xattr.h> does not exist.");
96 #endif
97