1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) 2018 Linaro Limited. All rights reserved.
4f08c3bdfSopenharmony_ci * Author: Rafael David Tinoco <rafael.tinoco@linaro.org>
5f08c3bdfSopenharmony_ci */
6f08c3bdfSopenharmony_ci
7f08c3bdfSopenharmony_ci/*
8f08c3bdfSopenharmony_ci * Basic tests for fgetxattr(2) and make sure fgetxattr(2) handles error
9f08c3bdfSopenharmony_ci * conditions correctly.
10f08c3bdfSopenharmony_ci *
11f08c3bdfSopenharmony_ci * There are 3 test cases:
12f08c3bdfSopenharmony_ci * 1. Get an non-existing attribute:
13f08c3bdfSopenharmony_ci *     - fgetxattr(2) should return -1 and set errno to ENODATA
14f08c3bdfSopenharmony_ci * 2. Buffer size is smaller than attribute value size:
15f08c3bdfSopenharmony_ci *     - fgetxattr(2) should return -1 and set errno to ERANGE
16f08c3bdfSopenharmony_ci * 3. Get attribute, fgetxattr(2) should succeed:
17f08c3bdfSopenharmony_ci *     - verify the attribute got by fgetxattr(2) is same as the value we set
18f08c3bdfSopenharmony_ci */
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_ci#include "config.h"
21f08c3bdfSopenharmony_ci#include <sys/types.h>
22f08c3bdfSopenharmony_ci#include <sys/stat.h>
23f08c3bdfSopenharmony_ci#include <sys/wait.h>
24f08c3bdfSopenharmony_ci#include <errno.h>
25f08c3bdfSopenharmony_ci#include <fcntl.h>
26f08c3bdfSopenharmony_ci#include <unistd.h>
27f08c3bdfSopenharmony_ci#include <signal.h>
28f08c3bdfSopenharmony_ci#include <stdio.h>
29f08c3bdfSopenharmony_ci#include <stdlib.h>
30f08c3bdfSopenharmony_ci#include <string.h>
31f08c3bdfSopenharmony_ci#ifdef HAVE_SYS_XATTR_H
32f08c3bdfSopenharmony_ci# include <sys/xattr.h>
33f08c3bdfSopenharmony_ci#endif
34f08c3bdfSopenharmony_ci#include "tst_test.h"
35f08c3bdfSopenharmony_ci
36f08c3bdfSopenharmony_ci#ifdef HAVE_SYS_XATTR_H
37f08c3bdfSopenharmony_ci#define XATTR_SIZE_MAX 65536
38f08c3bdfSopenharmony_ci#define XATTR_TEST_KEY "user.testkey"
39f08c3bdfSopenharmony_ci#define XATTR_TEST_VALUE "this is a test value"
40f08c3bdfSopenharmony_ci#define XATTR_TEST_VALUE_SIZE 20
41f08c3bdfSopenharmony_ci#define XATTR_TEST_INVALID_KEY "user.nosuchkey"
42f08c3bdfSopenharmony_ci#define MNTPOINT "mntpoint"
43f08c3bdfSopenharmony_ci#define FNAME MNTPOINT"/fgetxattr01testfile"
44f08c3bdfSopenharmony_ci
45f08c3bdfSopenharmony_cistatic int fd = -1;
46f08c3bdfSopenharmony_ci
47f08c3bdfSopenharmony_cistruct test_case {
48f08c3bdfSopenharmony_ci	char *key;
49f08c3bdfSopenharmony_ci	char *value;
50f08c3bdfSopenharmony_ci	size_t size;
51f08c3bdfSopenharmony_ci	int exp_ret;
52f08c3bdfSopenharmony_ci	int exp_err;
53f08c3bdfSopenharmony_ci};
54f08c3bdfSopenharmony_cistruct test_case tc[] = {
55f08c3bdfSopenharmony_ci	{			/* case 00, get non-existing attribute */
56f08c3bdfSopenharmony_ci	 .key = XATTR_TEST_INVALID_KEY,
57f08c3bdfSopenharmony_ci	 .value = NULL,
58f08c3bdfSopenharmony_ci	 .size = XATTR_SIZE_MAX,
59f08c3bdfSopenharmony_ci	 .exp_ret = -1,
60f08c3bdfSopenharmony_ci	 .exp_err = ENODATA,
61f08c3bdfSopenharmony_ci	 },
62f08c3bdfSopenharmony_ci	{			/* case 01, small value buffer */
63f08c3bdfSopenharmony_ci	 .key = XATTR_TEST_KEY,
64f08c3bdfSopenharmony_ci	 .value = NULL,
65f08c3bdfSopenharmony_ci	 .size = 1,
66f08c3bdfSopenharmony_ci	 .exp_ret = -1,
67f08c3bdfSopenharmony_ci	 .exp_err = ERANGE,
68f08c3bdfSopenharmony_ci	 },
69f08c3bdfSopenharmony_ci	{			/* case 02, get existing attribute */
70f08c3bdfSopenharmony_ci	 .key = XATTR_TEST_KEY,
71f08c3bdfSopenharmony_ci	 .value = NULL,
72f08c3bdfSopenharmony_ci	 .size = XATTR_TEST_VALUE_SIZE,
73f08c3bdfSopenharmony_ci	 .exp_ret = XATTR_TEST_VALUE_SIZE,
74f08c3bdfSopenharmony_ci	 .exp_err = 0,
75f08c3bdfSopenharmony_ci	 },
76f08c3bdfSopenharmony_ci};
77f08c3bdfSopenharmony_ci
78f08c3bdfSopenharmony_cistatic void verify_fgetxattr(unsigned int i)
79f08c3bdfSopenharmony_ci{
80f08c3bdfSopenharmony_ci	TEST(fgetxattr(fd, tc[i].key, tc[i].value, tc[i].size));
81f08c3bdfSopenharmony_ci
82f08c3bdfSopenharmony_ci	if (TST_RET == -1 && TST_ERR == EOPNOTSUPP)
83f08c3bdfSopenharmony_ci		tst_brk(TCONF, "fgetxattr(2) not supported");
84f08c3bdfSopenharmony_ci
85f08c3bdfSopenharmony_ci	if (TST_RET >= 0) {
86f08c3bdfSopenharmony_ci
87f08c3bdfSopenharmony_ci		if (tc[i].exp_ret == TST_RET)
88f08c3bdfSopenharmony_ci			tst_res(TPASS, "fgetxattr(2) passed");
89f08c3bdfSopenharmony_ci		else
90f08c3bdfSopenharmony_ci			tst_res(TFAIL, "fgetxattr(2) passed unexpectedly");
91f08c3bdfSopenharmony_ci
92f08c3bdfSopenharmony_ci		if (strncmp(tc[i].value, XATTR_TEST_VALUE,
93f08c3bdfSopenharmony_ci				XATTR_TEST_VALUE_SIZE)) {
94f08c3bdfSopenharmony_ci			tst_res(TFAIL, "wrong value, expect \"%s\" got \"%s\"",
95f08c3bdfSopenharmony_ci					 XATTR_TEST_VALUE, tc[i].value);
96f08c3bdfSopenharmony_ci		}
97f08c3bdfSopenharmony_ci
98f08c3bdfSopenharmony_ci		tst_res(TPASS, "got the right value");
99f08c3bdfSopenharmony_ci	}
100f08c3bdfSopenharmony_ci
101f08c3bdfSopenharmony_ci	if (tc[i].exp_err == TST_ERR) {
102f08c3bdfSopenharmony_ci		tst_res(TPASS | TTERRNO, "fgetxattr(2) passed");
103f08c3bdfSopenharmony_ci		return;
104f08c3bdfSopenharmony_ci	}
105f08c3bdfSopenharmony_ci
106f08c3bdfSopenharmony_ci	tst_res(TFAIL | TTERRNO, "fgetxattr(2) failed");
107f08c3bdfSopenharmony_ci}
108f08c3bdfSopenharmony_ci
109f08c3bdfSopenharmony_cistatic void setup(void)
110f08c3bdfSopenharmony_ci{
111f08c3bdfSopenharmony_ci	size_t i = 0;
112f08c3bdfSopenharmony_ci
113f08c3bdfSopenharmony_ci	SAFE_TOUCH(FNAME, 0644, NULL);
114f08c3bdfSopenharmony_ci	fd = SAFE_OPEN(FNAME, O_RDONLY);
115f08c3bdfSopenharmony_ci
116f08c3bdfSopenharmony_ci	for (i = 0; i < ARRAY_SIZE(tc); i++) {
117f08c3bdfSopenharmony_ci		tc[i].value = SAFE_MALLOC(tc[i].size);
118f08c3bdfSopenharmony_ci		memset(tc[i].value, 0, tc[i].size);
119f08c3bdfSopenharmony_ci	}
120f08c3bdfSopenharmony_ci
121f08c3bdfSopenharmony_ci	SAFE_FSETXATTR(fd, XATTR_TEST_KEY, XATTR_TEST_VALUE,
122f08c3bdfSopenharmony_ci			XATTR_TEST_VALUE_SIZE, XATTR_CREATE);
123f08c3bdfSopenharmony_ci}
124f08c3bdfSopenharmony_ci
125f08c3bdfSopenharmony_cistatic void cleanup(void)
126f08c3bdfSopenharmony_ci{
127f08c3bdfSopenharmony_ci	size_t i = 0;
128f08c3bdfSopenharmony_ci
129f08c3bdfSopenharmony_ci	for (i = 0; i < ARRAY_SIZE(tc); i++)
130f08c3bdfSopenharmony_ci		free(tc[i].value);
131f08c3bdfSopenharmony_ci
132f08c3bdfSopenharmony_ci	if (fd > 0)
133f08c3bdfSopenharmony_ci		SAFE_CLOSE(fd);
134f08c3bdfSopenharmony_ci}
135f08c3bdfSopenharmony_ci
136f08c3bdfSopenharmony_cistatic struct tst_test test = {
137f08c3bdfSopenharmony_ci	.setup = setup,
138f08c3bdfSopenharmony_ci	.test = verify_fgetxattr,
139f08c3bdfSopenharmony_ci	.cleanup = cleanup,
140f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(tc),
141f08c3bdfSopenharmony_ci	.mntpoint = MNTPOINT,
142f08c3bdfSopenharmony_ci	.mount_device = 1,
143f08c3bdfSopenharmony_ci	.all_filesystems = 1,
144f08c3bdfSopenharmony_ci	.needs_root = 1,
145f08c3bdfSopenharmony_ci};
146f08c3bdfSopenharmony_ci
147f08c3bdfSopenharmony_ci#else /* HAVE_SYS_XATTR_H */
148f08c3bdfSopenharmony_ciTST_TEST_TCONF("<sys/xattr.h> does not exist");
149f08c3bdfSopenharmony_ci#endif
150