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