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 * An empty buffer of size zero can be passed into fgetxattr(2) to return
9f08c3bdfSopenharmony_ci * the current size of the named extended attribute.
10f08c3bdfSopenharmony_ci */
11f08c3bdfSopenharmony_ci
12f08c3bdfSopenharmony_ci#include "config.h"
13f08c3bdfSopenharmony_ci#include <sys/types.h>
14f08c3bdfSopenharmony_ci#include <sys/stat.h>
15f08c3bdfSopenharmony_ci#include <sys/wait.h>
16f08c3bdfSopenharmony_ci#include <errno.h>
17f08c3bdfSopenharmony_ci#include <fcntl.h>
18f08c3bdfSopenharmony_ci#include <unistd.h>
19f08c3bdfSopenharmony_ci#include <signal.h>
20f08c3bdfSopenharmony_ci#include <stdio.h>
21f08c3bdfSopenharmony_ci#include <stdlib.h>
22f08c3bdfSopenharmony_ci#include <string.h>
23f08c3bdfSopenharmony_ci#ifdef HAVE_SYS_XATTR_H
24f08c3bdfSopenharmony_ci# include <sys/xattr.h>
25f08c3bdfSopenharmony_ci#endif
26f08c3bdfSopenharmony_ci#include "tst_test.h"
27f08c3bdfSopenharmony_ci
28f08c3bdfSopenharmony_ci#ifdef HAVE_SYS_XATTR_H
29f08c3bdfSopenharmony_ci#define XATTR_TEST_KEY "user.testkey"
30f08c3bdfSopenharmony_ci#define XATTR_TEST_VALUE "this is a test value"
31f08c3bdfSopenharmony_ci#define XATTR_TEST_VALUE_SIZE 20
32f08c3bdfSopenharmony_ci#define FILENAME "fgetxattr03testfile"
33f08c3bdfSopenharmony_ci
34f08c3bdfSopenharmony_cistatic int fd = -1;
35f08c3bdfSopenharmony_ci
36f08c3bdfSopenharmony_cistatic void verify_fgetxattr(void)
37f08c3bdfSopenharmony_ci{
38f08c3bdfSopenharmony_ci	TEST(fgetxattr(fd, XATTR_TEST_KEY, NULL, 0));
39f08c3bdfSopenharmony_ci
40f08c3bdfSopenharmony_ci	if (TST_RET == XATTR_TEST_VALUE_SIZE) {
41f08c3bdfSopenharmony_ci		tst_res(TPASS, "fgetxattr(2) returned correct value");
42f08c3bdfSopenharmony_ci		return;
43f08c3bdfSopenharmony_ci	}
44f08c3bdfSopenharmony_ci
45f08c3bdfSopenharmony_ci	tst_res(TFAIL | TTERRNO, "fgetxattr(2) failed with %li", TST_RET);
46f08c3bdfSopenharmony_ci}
47f08c3bdfSopenharmony_ci
48f08c3bdfSopenharmony_cistatic void setup(void)
49f08c3bdfSopenharmony_ci{
50f08c3bdfSopenharmony_ci	SAFE_TOUCH(FILENAME, 0644, NULL);
51f08c3bdfSopenharmony_ci	fd = SAFE_OPEN(FILENAME, O_RDONLY);
52f08c3bdfSopenharmony_ci
53f08c3bdfSopenharmony_ci	SAFE_FSETXATTR(fd, XATTR_TEST_KEY, XATTR_TEST_VALUE,
54f08c3bdfSopenharmony_ci			XATTR_TEST_VALUE_SIZE, XATTR_CREATE);
55f08c3bdfSopenharmony_ci}
56f08c3bdfSopenharmony_ci
57f08c3bdfSopenharmony_cistatic void cleanup(void)
58f08c3bdfSopenharmony_ci{
59f08c3bdfSopenharmony_ci	if (fd > 0)
60f08c3bdfSopenharmony_ci		SAFE_CLOSE(fd);
61f08c3bdfSopenharmony_ci}
62f08c3bdfSopenharmony_ci
63f08c3bdfSopenharmony_cistatic struct tst_test test = {
64f08c3bdfSopenharmony_ci	.setup = setup,
65f08c3bdfSopenharmony_ci	.test_all = verify_fgetxattr,
66f08c3bdfSopenharmony_ci	.cleanup = cleanup,
67f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
68f08c3bdfSopenharmony_ci};
69f08c3bdfSopenharmony_ci
70f08c3bdfSopenharmony_ci#else /* HAVE_SYS_XATTR_H */
71f08c3bdfSopenharmony_ciTST_TEST_TCONF("<sys/xattr.h> does not exist");
72f08c3bdfSopenharmony_ci#endif
73f08c3bdfSopenharmony_ci
74