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: flistxattr03
9f08c3bdfSopenharmony_ci*
10f08c3bdfSopenharmony_ci* Description:
11f08c3bdfSopenharmony_ci* flistxattr is identical to listxattr. an empty buffer of size zero
12f08c3bdfSopenharmony_ci* can return the current size of the list of extended attribute names,
13f08c3bdfSopenharmony_ci* which can be used to estimate a suitable buffer.
14f08c3bdfSopenharmony_ci*/
15f08c3bdfSopenharmony_ci
16f08c3bdfSopenharmony_ci#include "config.h"
17f08c3bdfSopenharmony_ci#include <errno.h>
18f08c3bdfSopenharmony_ci#include <sys/types.h>
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_ci#ifdef HAVE_SYS_XATTR_H
21f08c3bdfSopenharmony_ci# include <sys/xattr.h>
22f08c3bdfSopenharmony_ci#endif
23f08c3bdfSopenharmony_ci
24f08c3bdfSopenharmony_ci#include "tst_test.h"
25f08c3bdfSopenharmony_ci
26f08c3bdfSopenharmony_ci#ifdef HAVE_SYS_XATTR_H
27f08c3bdfSopenharmony_ci
28f08c3bdfSopenharmony_ci#define SECURITY_KEY	"security.ltptest"
29f08c3bdfSopenharmony_ci#define VALUE	"test"
30f08c3bdfSopenharmony_ci#define VALUE_SIZE	(sizeof(VALUE) - 1)
31f08c3bdfSopenharmony_ci
32f08c3bdfSopenharmony_cistatic int fd[] = {0, 0};
33f08c3bdfSopenharmony_ci
34f08c3bdfSopenharmony_cistatic int check_suitable_buf(const int file, long size)
35f08c3bdfSopenharmony_ci{
36f08c3bdfSopenharmony_ci	int n;
37f08c3bdfSopenharmony_ci	char buf[size];
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_ci	n = flistxattr(file, buf, sizeof(buf));
40f08c3bdfSopenharmony_ci
41f08c3bdfSopenharmony_ci	return n != -1;
42f08c3bdfSopenharmony_ci}
43f08c3bdfSopenharmony_ci
44f08c3bdfSopenharmony_cistatic void verify_flistxattr(unsigned int n)
45f08c3bdfSopenharmony_ci{
46f08c3bdfSopenharmony_ci	TEST(flistxattr(fd[n], NULL, 0));
47f08c3bdfSopenharmony_ci	if (TST_RET == -1) {
48f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "flistxattr() failed");
49f08c3bdfSopenharmony_ci		return;
50f08c3bdfSopenharmony_ci	}
51f08c3bdfSopenharmony_ci
52f08c3bdfSopenharmony_ci	if (check_suitable_buf(fd[n], TST_RET))
53f08c3bdfSopenharmony_ci		tst_res(TPASS, "flistxattr() succeed with suitable buffer");
54f08c3bdfSopenharmony_ci	else
55f08c3bdfSopenharmony_ci		tst_res(TFAIL, "flistxattr() failed with small buffer");
56f08c3bdfSopenharmony_ci}
57f08c3bdfSopenharmony_ci
58f08c3bdfSopenharmony_cistatic void setup(void)
59f08c3bdfSopenharmony_ci{
60f08c3bdfSopenharmony_ci	fd[0] = SAFE_OPEN("testfile1", O_RDWR | O_CREAT, 0644);
61f08c3bdfSopenharmony_ci
62f08c3bdfSopenharmony_ci	fd[1] = SAFE_OPEN("testfile2", O_RDWR | O_CREAT, 0644);
63f08c3bdfSopenharmony_ci
64f08c3bdfSopenharmony_ci	SAFE_FSETXATTR(fd[1], SECURITY_KEY, VALUE, VALUE_SIZE, XATTR_CREATE);
65f08c3bdfSopenharmony_ci}
66f08c3bdfSopenharmony_ci
67f08c3bdfSopenharmony_cistatic void cleanup(void)
68f08c3bdfSopenharmony_ci{
69f08c3bdfSopenharmony_ci	SAFE_CLOSE(fd[1]);
70f08c3bdfSopenharmony_ci	SAFE_CLOSE(fd[0]);
71f08c3bdfSopenharmony_ci}
72f08c3bdfSopenharmony_ci
73f08c3bdfSopenharmony_cistatic struct tst_test test = {
74f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
75f08c3bdfSopenharmony_ci	.needs_root = 1,
76f08c3bdfSopenharmony_ci	.test = verify_flistxattr,
77f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(fd),
78f08c3bdfSopenharmony_ci	.setup = setup,
79f08c3bdfSopenharmony_ci	.cleanup = cleanup,
80f08c3bdfSopenharmony_ci};
81f08c3bdfSopenharmony_ci
82f08c3bdfSopenharmony_ci#else /* HAVE_SYS_XATTR_H */
83f08c3bdfSopenharmony_ci	TST_TEST_TCONF("<sys/xattr.h> does not exist.");
84f08c3bdfSopenharmony_ci#endif
85