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