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: lgetxattr01
9f08c3bdfSopenharmony_ci*
10f08c3bdfSopenharmony_ci* Description:
11f08c3bdfSopenharmony_ci* The testcase checks the basic functionality of the lgetxattr(2).
12f08c3bdfSopenharmony_ci* In the case of a symbolic link, we only get the value of the
13f08c3bdfSopenharmony_ci* extended attribute related to the link itself by name.
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 SECURITY_KEY2   "security.ltptest2"
32f08c3bdfSopenharmony_ci#define VALUE1   "test1"
33f08c3bdfSopenharmony_ci#define VALUE2   "test2"
34f08c3bdfSopenharmony_ci
35f08c3bdfSopenharmony_cistatic void set_xattr(char *path, char *key, void *value, size_t size)
36f08c3bdfSopenharmony_ci{
37f08c3bdfSopenharmony_ci	int res;
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_ci	res = lsetxattr(path, key, value, size, XATTR_CREATE);
40f08c3bdfSopenharmony_ci	if (res == -1) {
41f08c3bdfSopenharmony_ci		if (errno == ENOTSUP) {
42f08c3bdfSopenharmony_ci			tst_brk(TCONF,
43f08c3bdfSopenharmony_ci				"no xattr support in fs or mounted "
44f08c3bdfSopenharmony_ci				"without user_xattr option");
45f08c3bdfSopenharmony_ci		} else {
46f08c3bdfSopenharmony_ci			tst_brk(TBROK | TERRNO, "lsetxattr(%s) failed", key);
47f08c3bdfSopenharmony_ci		}
48f08c3bdfSopenharmony_ci	}
49f08c3bdfSopenharmony_ci}
50f08c3bdfSopenharmony_ci
51f08c3bdfSopenharmony_cistatic void setup(void)
52f08c3bdfSopenharmony_ci{
53f08c3bdfSopenharmony_ci	SAFE_TOUCH("testfile", 0644, NULL);
54f08c3bdfSopenharmony_ci	SAFE_SYMLINK("testfile", "symlink");
55f08c3bdfSopenharmony_ci
56f08c3bdfSopenharmony_ci	set_xattr("testfile", SECURITY_KEY1, VALUE1, strlen(VALUE1));
57f08c3bdfSopenharmony_ci	set_xattr("symlink", SECURITY_KEY2, VALUE2, strlen(VALUE2));
58f08c3bdfSopenharmony_ci}
59f08c3bdfSopenharmony_ci
60f08c3bdfSopenharmony_cistatic void verify_lgetxattr(void)
61f08c3bdfSopenharmony_ci{
62f08c3bdfSopenharmony_ci	int size = 64;
63f08c3bdfSopenharmony_ci	char buf[size];
64f08c3bdfSopenharmony_ci
65f08c3bdfSopenharmony_ci	TEST(lgetxattr("symlink", SECURITY_KEY2, buf, size));
66f08c3bdfSopenharmony_ci	if (TST_RET == -1) {
67f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "lgetxattr() failed");
68f08c3bdfSopenharmony_ci		goto next;
69f08c3bdfSopenharmony_ci	}
70f08c3bdfSopenharmony_ci
71f08c3bdfSopenharmony_ci	if (TST_RET != strlen(VALUE2)) {
72f08c3bdfSopenharmony_ci		tst_res(TFAIL, "lgetxattr() got unexpected value size");
73f08c3bdfSopenharmony_ci		goto next;
74f08c3bdfSopenharmony_ci	}
75f08c3bdfSopenharmony_ci
76f08c3bdfSopenharmony_ci	if (!strncmp(buf, VALUE2, TST_RET))
77f08c3bdfSopenharmony_ci		tst_res(TPASS, "lgetxattr() got expected value");
78f08c3bdfSopenharmony_ci	else
79f08c3bdfSopenharmony_ci		tst_res(TFAIL, "lgetxattr() got unexpected value");
80f08c3bdfSopenharmony_ci
81f08c3bdfSopenharmony_cinext:
82f08c3bdfSopenharmony_ci	TEST(lgetxattr("symlink", SECURITY_KEY1, buf, size));
83f08c3bdfSopenharmony_ci
84f08c3bdfSopenharmony_ci	if (TST_RET != -1) {
85f08c3bdfSopenharmony_ci		tst_res(TFAIL, "lgetxattr() succeeded unexpectedly");
86f08c3bdfSopenharmony_ci		return;
87f08c3bdfSopenharmony_ci	}
88f08c3bdfSopenharmony_ci
89f08c3bdfSopenharmony_ci	if (TST_ERR == ENODATA) {
90f08c3bdfSopenharmony_ci		tst_res(TPASS | TTERRNO, "lgetxattr() failed as expected");
91f08c3bdfSopenharmony_ci	} else {
92f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "lgetxattr() failed unexpectedly, expected %s",
93f08c3bdfSopenharmony_ci			tst_strerrno(ENODATA));
94f08c3bdfSopenharmony_ci	}
95f08c3bdfSopenharmony_ci}
96f08c3bdfSopenharmony_ci
97f08c3bdfSopenharmony_cistatic struct tst_test test = {
98f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
99f08c3bdfSopenharmony_ci	.needs_root = 1,
100f08c3bdfSopenharmony_ci	.test_all = verify_lgetxattr,
101f08c3bdfSopenharmony_ci	.setup = setup
102f08c3bdfSopenharmony_ci};
103f08c3bdfSopenharmony_ci
104f08c3bdfSopenharmony_ci#else
105f08c3bdfSopenharmony_ci	TST_TEST_TCONF("<sys/xattr.h> does not exist.");
106f08c3bdfSopenharmony_ci#endif /* HAVE_SYS_XATTR_H */
107