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: verify_flistxattr01
9f08c3bdfSopenharmony_ci*
10f08c3bdfSopenharmony_ci* Description:
11f08c3bdfSopenharmony_ci* The testcase checks the basic functionality of the flistxattr(2).
12f08c3bdfSopenharmony_ci* flistxattr(2) retrieves the list of extended attribute names
13f08c3bdfSopenharmony_ci* associated with the file itself in the filesystem.
14f08c3bdfSopenharmony_ci*
15f08c3bdfSopenharmony_ci*/
16f08c3bdfSopenharmony_ci
17f08c3bdfSopenharmony_ci#include "config.h"
18f08c3bdfSopenharmony_ci#include <errno.h>
19f08c3bdfSopenharmony_ci#include <sys/types.h>
20f08c3bdfSopenharmony_ci#include <string.h>
21f08c3bdfSopenharmony_ci
22f08c3bdfSopenharmony_ci#ifdef HAVE_SYS_XATTR_H
23f08c3bdfSopenharmony_ci# include <sys/xattr.h>
24f08c3bdfSopenharmony_ci#endif
25f08c3bdfSopenharmony_ci
26f08c3bdfSopenharmony_ci#include "tst_test.h"
27f08c3bdfSopenharmony_ci
28f08c3bdfSopenharmony_ci#ifdef HAVE_SYS_XATTR_H
29f08c3bdfSopenharmony_ci
30f08c3bdfSopenharmony_ci#define SECURITY_KEY1	"security.ltptest1"
31f08c3bdfSopenharmony_ci#define VALUE	"test"
32f08c3bdfSopenharmony_ci#define VALUE_SIZE	(sizeof(VALUE) - 1)
33f08c3bdfSopenharmony_ci#define KEY_SIZE    (sizeof(SECURITY_KEY1) - 1)
34f08c3bdfSopenharmony_ci
35f08c3bdfSopenharmony_cistatic int fd;
36f08c3bdfSopenharmony_ci
37f08c3bdfSopenharmony_cistatic int has_attribute(const char *list, int llen, const char *attr)
38f08c3bdfSopenharmony_ci{
39f08c3bdfSopenharmony_ci	int i;
40f08c3bdfSopenharmony_ci
41f08c3bdfSopenharmony_ci	for (i = 0; i < llen; i += strlen(list + i) + 1) {
42f08c3bdfSopenharmony_ci		if (!strcmp(list + i, attr))
43f08c3bdfSopenharmony_ci			return 1;
44f08c3bdfSopenharmony_ci	}
45f08c3bdfSopenharmony_ci	return 0;
46f08c3bdfSopenharmony_ci}
47f08c3bdfSopenharmony_ci
48f08c3bdfSopenharmony_cistatic void verify_flistxattr(void)
49f08c3bdfSopenharmony_ci{
50f08c3bdfSopenharmony_ci	char buf[128];
51f08c3bdfSopenharmony_ci
52f08c3bdfSopenharmony_ci	TEST(flistxattr(fd, buf, sizeof(buf)));
53f08c3bdfSopenharmony_ci	if (TST_RET == -1) {
54f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "flistxattr() failed");
55f08c3bdfSopenharmony_ci		return;
56f08c3bdfSopenharmony_ci	}
57f08c3bdfSopenharmony_ci
58f08c3bdfSopenharmony_ci	if (!has_attribute(buf, sizeof(buf), SECURITY_KEY1)) {
59f08c3bdfSopenharmony_ci		tst_res(TFAIL, "missing attribute %s",
60f08c3bdfSopenharmony_ci			 SECURITY_KEY1);
61f08c3bdfSopenharmony_ci		return;
62f08c3bdfSopenharmony_ci	}
63f08c3bdfSopenharmony_ci
64f08c3bdfSopenharmony_ci	tst_res(TPASS, "flistxattr() succeeded");
65f08c3bdfSopenharmony_ci}
66f08c3bdfSopenharmony_ci
67f08c3bdfSopenharmony_cistatic void setup(void)
68f08c3bdfSopenharmony_ci{
69f08c3bdfSopenharmony_ci	fd = SAFE_OPEN("testfile", O_RDWR | O_CREAT, 0644);
70f08c3bdfSopenharmony_ci
71f08c3bdfSopenharmony_ci	SAFE_FSETXATTR(fd, SECURITY_KEY1, VALUE, VALUE_SIZE, XATTR_CREATE);
72f08c3bdfSopenharmony_ci}
73f08c3bdfSopenharmony_ci
74f08c3bdfSopenharmony_cistatic void cleanup(void)
75f08c3bdfSopenharmony_ci{
76f08c3bdfSopenharmony_ci	if (fd > 0)
77f08c3bdfSopenharmony_ci		SAFE_CLOSE(fd);
78f08c3bdfSopenharmony_ci}
79f08c3bdfSopenharmony_ci
80f08c3bdfSopenharmony_cistatic struct tst_test test = {
81f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
82f08c3bdfSopenharmony_ci	.needs_root = 1,
83f08c3bdfSopenharmony_ci	.test_all = verify_flistxattr,
84f08c3bdfSopenharmony_ci	.setup = setup,
85f08c3bdfSopenharmony_ci	.cleanup = cleanup,
86f08c3bdfSopenharmony_ci};
87f08c3bdfSopenharmony_ci
88f08c3bdfSopenharmony_ci#else
89f08c3bdfSopenharmony_ci	TST_TEST_TCONF("<sys/xattr.h> does not exist.");
90f08c3bdfSopenharmony_ci#endif /* HAVE_SYS_XATTR_H */
91