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: llistxattr01
9f08c3bdfSopenharmony_ci*
10f08c3bdfSopenharmony_ci* Description:
11f08c3bdfSopenharmony_ci* The testcase checks the basic functionality of the llistxattr(2).
12f08c3bdfSopenharmony_ci* llistxattr(2) retrieves the list of extended attribute names
13f08c3bdfSopenharmony_ci* associated with the link 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 SECURITY_KEY2   "security.ltptest2"
32f08c3bdfSopenharmony_ci#define VALUE           "test"
33f08c3bdfSopenharmony_ci#define VALUE_SIZE      (sizeof(VALUE) - 1)
34f08c3bdfSopenharmony_ci#define KEY_SIZE        (sizeof(SECURITY_KEY1) - 1)
35f08c3bdfSopenharmony_ci#define TESTFILE        "testfile"
36f08c3bdfSopenharmony_ci#define SYMLINK         "symlink"
37f08c3bdfSopenharmony_ci
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_cistatic int has_attribute(const char *list, int llen, const char *attr)
40f08c3bdfSopenharmony_ci{
41f08c3bdfSopenharmony_ci	int i;
42f08c3bdfSopenharmony_ci
43f08c3bdfSopenharmony_ci	for (i = 0; i < llen; i += strlen(list + i) + 1) {
44f08c3bdfSopenharmony_ci		if (!strcmp(list + i, attr))
45f08c3bdfSopenharmony_ci			return 1;
46f08c3bdfSopenharmony_ci	}
47f08c3bdfSopenharmony_ci	return 0;
48f08c3bdfSopenharmony_ci}
49f08c3bdfSopenharmony_ci
50f08c3bdfSopenharmony_cistatic void verify_llistxattr(void)
51f08c3bdfSopenharmony_ci{
52f08c3bdfSopenharmony_ci	char buf[64];
53f08c3bdfSopenharmony_ci
54f08c3bdfSopenharmony_ci	TEST(llistxattr(SYMLINK, buf, sizeof(buf)));
55f08c3bdfSopenharmony_ci	if (TST_RET == -1) {
56f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "llistxattr() failed");
57f08c3bdfSopenharmony_ci		return;
58f08c3bdfSopenharmony_ci	}
59f08c3bdfSopenharmony_ci
60f08c3bdfSopenharmony_ci	if (has_attribute(buf, sizeof(buf), SECURITY_KEY1)) {
61f08c3bdfSopenharmony_ci		tst_res(TFAIL, "get file attribute %s unexpectlly",
62f08c3bdfSopenharmony_ci			 SECURITY_KEY1);
63f08c3bdfSopenharmony_ci		return;
64f08c3bdfSopenharmony_ci	}
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_ci	if (!has_attribute(buf, sizeof(buf), SECURITY_KEY2)) {
67f08c3bdfSopenharmony_ci		tst_res(TFAIL, "missing attribute %s", SECURITY_KEY2);
68f08c3bdfSopenharmony_ci		return;
69f08c3bdfSopenharmony_ci	}
70f08c3bdfSopenharmony_ci
71f08c3bdfSopenharmony_ci	tst_res(TPASS, "llistxattr() succeeded");
72f08c3bdfSopenharmony_ci}
73f08c3bdfSopenharmony_ci
74f08c3bdfSopenharmony_cistatic void setup(void)
75f08c3bdfSopenharmony_ci{
76f08c3bdfSopenharmony_ci	SAFE_TOUCH(TESTFILE, 0644, NULL);
77f08c3bdfSopenharmony_ci
78f08c3bdfSopenharmony_ci	SAFE_SYMLINK(TESTFILE, SYMLINK);
79f08c3bdfSopenharmony_ci
80f08c3bdfSopenharmony_ci	SAFE_LSETXATTR(TESTFILE, SECURITY_KEY1, VALUE, VALUE_SIZE, XATTR_CREATE);
81f08c3bdfSopenharmony_ci
82f08c3bdfSopenharmony_ci	SAFE_LSETXATTR(SYMLINK, SECURITY_KEY2, VALUE, VALUE_SIZE, XATTR_CREATE);
83f08c3bdfSopenharmony_ci}
84f08c3bdfSopenharmony_ci
85f08c3bdfSopenharmony_cistatic struct tst_test test = {
86f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
87f08c3bdfSopenharmony_ci	.needs_root = 1,
88f08c3bdfSopenharmony_ci	.test_all = verify_llistxattr,
89f08c3bdfSopenharmony_ci	.setup = setup,
90f08c3bdfSopenharmony_ci};
91f08c3bdfSopenharmony_ci
92f08c3bdfSopenharmony_ci#else
93f08c3bdfSopenharmony_ci	TST_TEST_TCONF("<sys/xattr.h> does not exist.");
94f08c3bdfSopenharmony_ci#endif /* HAVE_SYS_XATTR_H */
95f08c3bdfSopenharmony_ci
96