1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci* Copyright (c) 2016 Fujitsu Ltd.
4f08c3bdfSopenharmony_ci* Author: Xiao Yang <yangx.jy@cn.fujitsu.com>
5f08c3bdfSopenharmony_ci*/
6f08c3bdfSopenharmony_ci
7f08c3bdfSopenharmony_ci/*
8f08c3bdfSopenharmony_ci* Test Name: llistxattr03
9f08c3bdfSopenharmony_ci*
10f08c3bdfSopenharmony_ci* Description:
11f08c3bdfSopenharmony_ci* llistxattr 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 const char *filename[] = {"testfile1", "testfile2"};
33f08c3bdfSopenharmony_ci
34f08c3bdfSopenharmony_cistatic int check_suitable_buf(const char *name, long size)
35f08c3bdfSopenharmony_ci{
36f08c3bdfSopenharmony_ci	int n;
37f08c3bdfSopenharmony_ci	char buf[size];
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_ci	n = llistxattr(name, buf, sizeof(buf));
40f08c3bdfSopenharmony_ci
41f08c3bdfSopenharmony_ci	return n != -1;
42f08c3bdfSopenharmony_ci}
43f08c3bdfSopenharmony_ci
44f08c3bdfSopenharmony_cistatic void verify_llistxattr(unsigned int n)
45f08c3bdfSopenharmony_ci{
46f08c3bdfSopenharmony_ci	const char *name = filename[n];
47f08c3bdfSopenharmony_ci
48f08c3bdfSopenharmony_ci	TEST(llistxattr(name, NULL, 0));
49f08c3bdfSopenharmony_ci	if (TST_RET == -1) {
50f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "llistxattr() failed");
51f08c3bdfSopenharmony_ci		return;
52f08c3bdfSopenharmony_ci	}
53f08c3bdfSopenharmony_ci
54f08c3bdfSopenharmony_ci	if (check_suitable_buf(name, TST_RET))
55f08c3bdfSopenharmony_ci		tst_res(TPASS, "llistxattr() succeed with suitable buffer");
56f08c3bdfSopenharmony_ci	else
57f08c3bdfSopenharmony_ci		tst_res(TFAIL, "llistxattr() failed with small buffer");
58f08c3bdfSopenharmony_ci}
59f08c3bdfSopenharmony_ci
60f08c3bdfSopenharmony_cistatic void setup(void)
61f08c3bdfSopenharmony_ci{
62f08c3bdfSopenharmony_ci	SAFE_TOUCH(filename[0], 0644, NULL);
63f08c3bdfSopenharmony_ci
64f08c3bdfSopenharmony_ci	SAFE_TOUCH(filename[1], 0644, NULL);
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_ci	SAFE_LSETXATTR(filename[1], SECURITY_KEY, VALUE, VALUE_SIZE, XATTR_CREATE);
67f08c3bdfSopenharmony_ci}
68f08c3bdfSopenharmony_ci
69f08c3bdfSopenharmony_cistatic struct tst_test test = {
70f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
71f08c3bdfSopenharmony_ci	.needs_root = 1,
72f08c3bdfSopenharmony_ci	.test = verify_llistxattr,
73f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(filename),
74f08c3bdfSopenharmony_ci	.setup = setup,
75f08c3bdfSopenharmony_ci};
76f08c3bdfSopenharmony_ci
77f08c3bdfSopenharmony_ci#else /* HAVE_SYS_XATTR_H */
78f08c3bdfSopenharmony_ci	TST_TEST_TCONF("<sys/xattr.h> does not exist.");
79f08c3bdfSopenharmony_ci#endif
80