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