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