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 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: flistxattr02
19 *
20 * Description:
21 * 1) flistxattr(2) fails if the size of the list buffer is too small
22 * to hold the result.
23 * 2) flistxattr(2) fails if fd is an invalid file descriptor.
24 *
25 * Expected Result:
26 * 1) flistxattr(2) should return -1 and set errno to ERANGE.
27 * 2) flistxattr(2) should return -1 and set errno to EBADF.
28 */
29 
30 #include "config.h"
31 #include <errno.h>
32 #include <sys/types.h>
33 
34 #ifdef HAVE_SYS_XATTR_H
35 # include <sys/xattr.h>
36 #endif
37 
38 #include "tst_test.h"
39 
40 #ifdef HAVE_SYS_XATTR_H
41 
42 #define SECURITY_KEY	"security.ltptest"
43 #define VALUE	"test"
44 #define VALUE_SIZE	(sizeof(VALUE) - 1)
45 
46 static int fd1;
47 static int fd2 = -1;
48 
49 static struct test_case {
50 	int *fd;
51 	size_t size;
52 	int exp_err;
53 } tc[] = {
54 	{&fd1, 1, ERANGE},
55 	{&fd2, 20, EBADF}
56 };
57 
verify_flistxattr(unsigned int n)58 static void verify_flistxattr(unsigned int n)
59 {
60 	struct test_case *t = tc + n;
61 	char buf[t->size];
62 
63 	TEST(flistxattr(*t->fd, buf, t->size));
64 	if (TST_RET != -1) {
65 		tst_res(TFAIL,
66 			"flistxattr() succeeded unexpectedly (returned %ld)",
67 			TST_RET);
68 		return;
69 	}
70 
71 	if (t->exp_err != TST_ERR) {
72 		tst_res(TFAIL | TTERRNO, "flistxattr() failed "
73 			 "unexpectedlly, expected %s",
74 			 tst_strerrno(t->exp_err));
75 	} else {
76 		tst_res(TPASS | TTERRNO,
77 			 "flistxattr() failed as expected");
78 	}
79 }
80 
setup(void)81 static void setup(void)
82 {
83 	fd1 = SAFE_OPEN("testfile", O_RDWR | O_CREAT, 0644);
84 
85 	SAFE_FSETXATTR(fd1, SECURITY_KEY, VALUE, VALUE_SIZE, XATTR_CREATE);
86 }
87 
cleanup(void)88 static void cleanup(void)
89 {
90 	if (fd1 > 0)
91 		SAFE_CLOSE(fd1);
92 }
93 
94 static struct tst_test test = {
95 	.needs_tmpdir = 1,
96 	.needs_root = 1,
97 	.test = verify_flistxattr,
98 	.tcnt = ARRAY_SIZE(tc),
99 	.setup = setup,
100 	.cleanup = cleanup,
101 };
102 
103 #else /* HAVE_SYS_XATTR_H */
104 	TST_TEST_TCONF("<sys/xattr.h> does not exist.");
105 #endif
106