• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2016 RT-RK Institute for Computer Based Systems
4 * Author: Dejan Jovicevic <dejan.jovicevic@rt-rk.com>
5 */
6 
7 /*
8 * Test Name: flistxattr02
9 *
10 * Description:
11 * 1) flistxattr(2) fails if the size of the list buffer is too small
12 * to hold the result.
13 * 2) flistxattr(2) fails if fd is an invalid file descriptor.
14 *
15 * Expected Result:
16 * 1) flistxattr(2) should return -1 and set errno to ERANGE.
17 * 2) flistxattr(2) should return -1 and set errno to EBADF.
18 */
19 
20 #include "config.h"
21 #include <errno.h>
22 #include <sys/types.h>
23 
24 #ifdef HAVE_SYS_XATTR_H
25 # include <sys/xattr.h>
26 #endif
27 
28 #include "tst_test.h"
29 
30 #ifdef HAVE_SYS_XATTR_H
31 
32 #define SECURITY_KEY	"security.ltptest"
33 #define VALUE	"test"
34 #define VALUE_SIZE	(sizeof(VALUE) - 1)
35 
36 static int fd1;
37 static int fd2 = -1;
38 
39 static struct test_case {
40 	int *fd;
41 	size_t size;
42 	int exp_err;
43 } tc[] = {
44 	{&fd1, 1, ERANGE},
45 	{&fd2, 20, EBADF}
46 };
47 
verify_flistxattr(unsigned int n)48 static void verify_flistxattr(unsigned int n)
49 {
50 	struct test_case *t = tc + n;
51 	char buf[t->size];
52 
53 	TEST(flistxattr(*t->fd, buf, t->size));
54 	if (TST_RET != -1) {
55 		tst_res(TFAIL,
56 			"flistxattr() succeeded unexpectedly (returned %ld)",
57 			TST_RET);
58 		return;
59 	}
60 
61 	if (t->exp_err != TST_ERR) {
62 		tst_res(TFAIL | TTERRNO, "flistxattr() failed "
63 			 "unexpectedlly, expected %s",
64 			 tst_strerrno(t->exp_err));
65 	} else {
66 		tst_res(TPASS | TTERRNO,
67 			 "flistxattr() failed as expected");
68 	}
69 }
70 
setup(void)71 static void setup(void)
72 {
73 	fd1 = SAFE_OPEN("testfile", O_RDWR | O_CREAT, 0644);
74 
75 	SAFE_FSETXATTR(fd1, SECURITY_KEY, VALUE, VALUE_SIZE, XATTR_CREATE);
76 }
77 
cleanup(void)78 static void cleanup(void)
79 {
80 	if (fd1 > 0)
81 		SAFE_CLOSE(fd1);
82 }
83 
84 static struct tst_test test = {
85 	.needs_tmpdir = 1,
86 	.needs_root = 1,
87 	.test = verify_flistxattr,
88 	.tcnt = ARRAY_SIZE(tc),
89 	.setup = setup,
90 	.cleanup = cleanup,
91 };
92 
93 #else /* HAVE_SYS_XATTR_H */
94 	TST_TEST_TCONF("<sys/xattr.h> does not exist.");
95 #endif
96