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: listxattr02
9f08c3bdfSopenharmony_ci*
10f08c3bdfSopenharmony_ci* Description:
11f08c3bdfSopenharmony_ci* 1) listxattr(2) fails if the size of the list buffer is too small
12f08c3bdfSopenharmony_ci* to hold the result.
13f08c3bdfSopenharmony_ci* 2) listxattr(2) fails if path is an empty string.
14f08c3bdfSopenharmony_ci* 3) listxattr(2) fails when attempted to read from a invalid address.
15f08c3bdfSopenharmony_ci* 4) listxattr(2) fails if path is longer than allowed.
16f08c3bdfSopenharmony_ci*
17f08c3bdfSopenharmony_ci* Expected Result:
18f08c3bdfSopenharmony_ci* 1) listxattr(2) should return -1 and set errno to ERANGE.
19f08c3bdfSopenharmony_ci* 2) listxattr(2) should return -1 and set errno to ENOENT.
20f08c3bdfSopenharmony_ci* 3) listxattr(2) should return -1 and set errno to EFAULT.
21f08c3bdfSopenharmony_ci* 4) listxattr(2) should return -1 and set errno to ENAMETOOLONG.
22f08c3bdfSopenharmony_ci*/
23f08c3bdfSopenharmony_ci
24f08c3bdfSopenharmony_ci#include "config.h"
25f08c3bdfSopenharmony_ci#include <errno.h>
26f08c3bdfSopenharmony_ci#include <sys/types.h>
27f08c3bdfSopenharmony_ci
28f08c3bdfSopenharmony_ci#ifdef HAVE_SYS_XATTR_H
29f08c3bdfSopenharmony_ci# include <sys/xattr.h>
30f08c3bdfSopenharmony_ci#endif
31f08c3bdfSopenharmony_ci
32f08c3bdfSopenharmony_ci#include "tst_test.h"
33f08c3bdfSopenharmony_ci
34f08c3bdfSopenharmony_ci#ifdef HAVE_SYS_XATTR_H
35f08c3bdfSopenharmony_ci
36f08c3bdfSopenharmony_ci#define SECURITY_KEY	"security.ltptest"
37f08c3bdfSopenharmony_ci#define VALUE	"test"
38f08c3bdfSopenharmony_ci#define VALUE_SIZE	(sizeof(VALUE) - 1)
39f08c3bdfSopenharmony_ci#define TESTFILE    "testfile"
40f08c3bdfSopenharmony_ci
41f08c3bdfSopenharmony_cichar longpathname[PATH_MAX + 2];
42f08c3bdfSopenharmony_ci
43f08c3bdfSopenharmony_cistatic struct test_case {
44f08c3bdfSopenharmony_ci	const char *path;
45f08c3bdfSopenharmony_ci	size_t size;
46f08c3bdfSopenharmony_ci	int exp_err;
47f08c3bdfSopenharmony_ci} tc[] = {
48f08c3bdfSopenharmony_ci	{TESTFILE, 1, ERANGE},
49f08c3bdfSopenharmony_ci	{"", 20, ENOENT},
50f08c3bdfSopenharmony_ci	{(char *)-1, 20, EFAULT},
51f08c3bdfSopenharmony_ci	{longpathname, 20, ENAMETOOLONG}
52f08c3bdfSopenharmony_ci};
53f08c3bdfSopenharmony_ci
54f08c3bdfSopenharmony_cistatic void verify_listxattr(unsigned int n)
55f08c3bdfSopenharmony_ci{
56f08c3bdfSopenharmony_ci	struct test_case *t = tc + n;
57f08c3bdfSopenharmony_ci	char buf[t->size];
58f08c3bdfSopenharmony_ci
59f08c3bdfSopenharmony_ci	TEST(listxattr(t->path, buf, sizeof(buf)));
60f08c3bdfSopenharmony_ci	if (TST_RET != -1) {
61f08c3bdfSopenharmony_ci		tst_res(TFAIL,
62f08c3bdfSopenharmony_ci			"listxattr() succeeded unexpectedly (returned %ld)",
63f08c3bdfSopenharmony_ci			TST_RET);
64f08c3bdfSopenharmony_ci		return;
65f08c3bdfSopenharmony_ci	}
66f08c3bdfSopenharmony_ci
67f08c3bdfSopenharmony_ci	if (t->exp_err != TST_ERR) {
68f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "listxattr() failed "
69f08c3bdfSopenharmony_ci			 "unexpectedlly, expected %s",
70f08c3bdfSopenharmony_ci			 tst_strerrno(t->exp_err));
71f08c3bdfSopenharmony_ci	} else {
72f08c3bdfSopenharmony_ci		tst_res(TPASS | TTERRNO,
73f08c3bdfSopenharmony_ci			 "listxattr() failed as expected");
74f08c3bdfSopenharmony_ci	}
75f08c3bdfSopenharmony_ci}
76f08c3bdfSopenharmony_ci
77f08c3bdfSopenharmony_cistatic void setup(void)
78f08c3bdfSopenharmony_ci{
79f08c3bdfSopenharmony_ci	SAFE_TOUCH(TESTFILE, 0644, NULL);
80f08c3bdfSopenharmony_ci
81f08c3bdfSopenharmony_ci	SAFE_SETXATTR(TESTFILE, SECURITY_KEY, VALUE, VALUE_SIZE, XATTR_CREATE);
82f08c3bdfSopenharmony_ci
83f08c3bdfSopenharmony_ci	memset(&longpathname, 'a', sizeof(longpathname) - 1);
84f08c3bdfSopenharmony_ci}
85f08c3bdfSopenharmony_ci
86f08c3bdfSopenharmony_cistatic struct tst_test test = {
87f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
88f08c3bdfSopenharmony_ci	.needs_root = 1,
89f08c3bdfSopenharmony_ci	.test = verify_listxattr,
90f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(tc),
91f08c3bdfSopenharmony_ci	.setup = setup,
92f08c3bdfSopenharmony_ci};
93f08c3bdfSopenharmony_ci
94f08c3bdfSopenharmony_ci#else /* HAVE_SYS_XATTR_H */
95f08c3bdfSopenharmony_ci	TST_TEST_TCONF("<sys/xattr.h> does not exist.");
96f08c3bdfSopenharmony_ci#endif
97