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: listxattr01
9f08c3bdfSopenharmony_ci*
10f08c3bdfSopenharmony_ci* Description:
11f08c3bdfSopenharmony_ci* The testcase checks the basic functionality of the listxattr(2).
12f08c3bdfSopenharmony_ci* listxattr(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#define TESTFILE    "testfile"
35f08c3bdfSopenharmony_ci
36f08c3bdfSopenharmony_cistatic int has_attribute(const char *list, int llen, const char *attr)
37f08c3bdfSopenharmony_ci{
38f08c3bdfSopenharmony_ci	int i;
39f08c3bdfSopenharmony_ci
40f08c3bdfSopenharmony_ci	for (i = 0; i < llen; i += strlen(list + i) + 1) {
41f08c3bdfSopenharmony_ci		if (!strcmp(list + i, attr))
42f08c3bdfSopenharmony_ci			return 1;
43f08c3bdfSopenharmony_ci	}
44f08c3bdfSopenharmony_ci	return 0;
45f08c3bdfSopenharmony_ci}
46f08c3bdfSopenharmony_ci
47f08c3bdfSopenharmony_cistatic void verify_listxattr(void)
48f08c3bdfSopenharmony_ci{
49f08c3bdfSopenharmony_ci	char buf[64];
50f08c3bdfSopenharmony_ci
51f08c3bdfSopenharmony_ci	TEST(listxattr(TESTFILE, buf, sizeof(buf)));
52f08c3bdfSopenharmony_ci	if (TST_RET == -1) {
53f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "listxattr() failed");
54f08c3bdfSopenharmony_ci		return;
55f08c3bdfSopenharmony_ci	}
56f08c3bdfSopenharmony_ci
57f08c3bdfSopenharmony_ci	if (!has_attribute(buf, sizeof(buf), SECURITY_KEY1)) {
58f08c3bdfSopenharmony_ci		tst_res(TFAIL, "missing attribute %s",
59f08c3bdfSopenharmony_ci			 SECURITY_KEY1);
60f08c3bdfSopenharmony_ci		return;
61f08c3bdfSopenharmony_ci	}
62f08c3bdfSopenharmony_ci
63f08c3bdfSopenharmony_ci	tst_res(TPASS, "listxattr() succeeded");
64f08c3bdfSopenharmony_ci}
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_cistatic void setup(void)
67f08c3bdfSopenharmony_ci{
68f08c3bdfSopenharmony_ci	SAFE_TOUCH(TESTFILE, 0644, NULL);
69f08c3bdfSopenharmony_ci
70f08c3bdfSopenharmony_ci	SAFE_SETXATTR(TESTFILE, SECURITY_KEY1, VALUE, VALUE_SIZE, XATTR_CREATE);
71f08c3bdfSopenharmony_ci}
72f08c3bdfSopenharmony_ci
73f08c3bdfSopenharmony_cistatic struct tst_test test = {
74f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
75f08c3bdfSopenharmony_ci	.needs_root = 1,
76f08c3bdfSopenharmony_ci	.test_all = verify_listxattr,
77f08c3bdfSopenharmony_ci	.setup = setup,
78f08c3bdfSopenharmony_ci};
79f08c3bdfSopenharmony_ci
80f08c3bdfSopenharmony_ci#else
81f08c3bdfSopenharmony_ci	TST_TEST_TCONF("<sys/xattr.h> does not exist.");
82f08c3bdfSopenharmony_ci#endif /* HAVE_SYS_XATTR_H */
83