1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci* Copyright (c) 2016 Fujitsu Ltd.
4f08c3bdfSopenharmony_ci* Author: Jinbao Huang <huangjb.jy@cn.fujitsu.com>
5f08c3bdfSopenharmony_ci*/
6f08c3bdfSopenharmony_ci
7f08c3bdfSopenharmony_ci/*
8f08c3bdfSopenharmony_ci* Test Name: lgetxattr02
9f08c3bdfSopenharmony_ci*
10f08c3bdfSopenharmony_ci* Description:
11f08c3bdfSopenharmony_ci* 1) lgetxattr(2) fails if the named attribute does not exist.
12f08c3bdfSopenharmony_ci* 2) lgetxattr(2) fails if the size of the value buffer is too small
13f08c3bdfSopenharmony_ci*    to hold the result.
14f08c3bdfSopenharmony_ci* 3) lgetxattr(2) fails when attemptes to read from a invalid address.
15f08c3bdfSopenharmony_ci*
16f08c3bdfSopenharmony_ci* Expected Result:
17f08c3bdfSopenharmony_ci* 1) lgetxattr(2) should return -1 and set errno to ENODATA.
18f08c3bdfSopenharmony_ci* 2) lgetxattr(2) should return -1 and set errno to ERANGE.
19f08c3bdfSopenharmony_ci* 3) lgetxattr(2) should return -1 and set errno to EFAULT.
20f08c3bdfSopenharmony_ci*/
21f08c3bdfSopenharmony_ci
22f08c3bdfSopenharmony_ci#include "config.h"
23f08c3bdfSopenharmony_ci#include <errno.h>
24f08c3bdfSopenharmony_ci#include <sys/types.h>
25f08c3bdfSopenharmony_ci#include <string.h>
26f08c3bdfSopenharmony_ci
27f08c3bdfSopenharmony_ci#ifdef HAVE_SYS_XATTR_H
28f08c3bdfSopenharmony_ci# include <sys/xattr.h>
29f08c3bdfSopenharmony_ci#endif
30f08c3bdfSopenharmony_ci
31f08c3bdfSopenharmony_ci#include "tst_test.h"
32f08c3bdfSopenharmony_ci
33f08c3bdfSopenharmony_ci#ifdef HAVE_SYS_XATTR_H
34f08c3bdfSopenharmony_ci
35f08c3bdfSopenharmony_ci#define SECURITY_KEY	"security.ltptest"
36f08c3bdfSopenharmony_ci#define VALUE	"this is a test value"
37f08c3bdfSopenharmony_ci
38f08c3bdfSopenharmony_cistatic struct test_case {
39f08c3bdfSopenharmony_ci	const char *path;
40f08c3bdfSopenharmony_ci	size_t size;
41f08c3bdfSopenharmony_ci	int exp_err;
42f08c3bdfSopenharmony_ci} tcase[] = {
43f08c3bdfSopenharmony_ci	{"testfile", sizeof(VALUE), ENODATA},
44f08c3bdfSopenharmony_ci	{"symlink", 1, ERANGE},
45f08c3bdfSopenharmony_ci	{(char *)-1, sizeof(VALUE), EFAULT}
46f08c3bdfSopenharmony_ci};
47f08c3bdfSopenharmony_ci
48f08c3bdfSopenharmony_cistatic void verify_lgetxattr(unsigned int n)
49f08c3bdfSopenharmony_ci{
50f08c3bdfSopenharmony_ci	struct test_case *tc = tcase + n;
51f08c3bdfSopenharmony_ci	char buf[tc->size];
52f08c3bdfSopenharmony_ci
53f08c3bdfSopenharmony_ci	TEST(lgetxattr(tc->path, SECURITY_KEY, buf, sizeof(buf)));
54f08c3bdfSopenharmony_ci	if (TST_RET != -1) {
55f08c3bdfSopenharmony_ci		tst_res(TFAIL, "lgetxattr() succeeded unexpectedly");
56f08c3bdfSopenharmony_ci		return;
57f08c3bdfSopenharmony_ci	}
58f08c3bdfSopenharmony_ci
59f08c3bdfSopenharmony_ci	if (TST_ERR != tc->exp_err) {
60f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "lgetxattr() failed unexpectedlly, "
61f08c3bdfSopenharmony_ci			"expected %s", tst_strerrno(tc->exp_err));
62f08c3bdfSopenharmony_ci	} else {
63f08c3bdfSopenharmony_ci		tst_res(TPASS | TTERRNO, "lgetxattr() failed as expected");
64f08c3bdfSopenharmony_ci	}
65f08c3bdfSopenharmony_ci}
66f08c3bdfSopenharmony_ci
67f08c3bdfSopenharmony_cistatic void setup(void)
68f08c3bdfSopenharmony_ci{
69f08c3bdfSopenharmony_ci	int res;
70f08c3bdfSopenharmony_ci
71f08c3bdfSopenharmony_ci	SAFE_TOUCH("testfile", 0644, NULL);
72f08c3bdfSopenharmony_ci	SAFE_SYMLINK("testfile", "symlink");
73f08c3bdfSopenharmony_ci
74f08c3bdfSopenharmony_ci	res = lsetxattr("symlink", SECURITY_KEY, VALUE, strlen(VALUE), XATTR_CREATE);
75f08c3bdfSopenharmony_ci	if (res == -1) {
76f08c3bdfSopenharmony_ci		if (errno == ENOTSUP) {
77f08c3bdfSopenharmony_ci			tst_brk(TCONF, "no xattr support in fs or "
78f08c3bdfSopenharmony_ci				"mounted without user_xattr option");
79f08c3bdfSopenharmony_ci		} else {
80f08c3bdfSopenharmony_ci			tst_brk(TBROK | TERRNO, "lsetxattr(%s) failed",
81f08c3bdfSopenharmony_ci				SECURITY_KEY);
82f08c3bdfSopenharmony_ci		}
83f08c3bdfSopenharmony_ci	}
84f08c3bdfSopenharmony_ci}
85f08c3bdfSopenharmony_ci
86f08c3bdfSopenharmony_cistatic struct tst_test test = {
87f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
88f08c3bdfSopenharmony_ci	.needs_root = 1,
89f08c3bdfSopenharmony_ci	.test = verify_lgetxattr,
90f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(tcase),
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