1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2018 Linaro Limited. All rights reserved.
4*49cdfc7eSAndroid Build Coastguard Worker * Author: Rafael David Tinoco <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker */
6*49cdfc7eSAndroid Build Coastguard Worker
7*49cdfc7eSAndroid Build Coastguard Worker /*
8*49cdfc7eSAndroid Build Coastguard Worker * Test Name: fremovexattr01
9*49cdfc7eSAndroid Build Coastguard Worker *
10*49cdfc7eSAndroid Build Coastguard Worker * Description:
11*49cdfc7eSAndroid Build Coastguard Worker * Like removexattr(2), fremovexattr(2) also removes an extended attribute,
12*49cdfc7eSAndroid Build Coastguard Worker * identified by a name, from a file but, instead of using a filename path, it
13*49cdfc7eSAndroid Build Coastguard Worker * uses a descriptor. This test verifies that a simple call to fremovexattr(2)
14*49cdfc7eSAndroid Build Coastguard Worker * removes, indeed, a previously set attribute key/value from a file.
15*49cdfc7eSAndroid Build Coastguard Worker */
16*49cdfc7eSAndroid Build Coastguard Worker
17*49cdfc7eSAndroid Build Coastguard Worker #include "config.h"
18*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
19*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
20*49cdfc7eSAndroid Build Coastguard Worker
21*49cdfc7eSAndroid Build Coastguard Worker #ifdef HAVE_SYS_XATTR_H
22*49cdfc7eSAndroid Build Coastguard Worker # include <sys/xattr.h>
23*49cdfc7eSAndroid Build Coastguard Worker #endif
24*49cdfc7eSAndroid Build Coastguard Worker
25*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
26*49cdfc7eSAndroid Build Coastguard Worker
27*49cdfc7eSAndroid Build Coastguard Worker #ifdef HAVE_SYS_XATTR_H
28*49cdfc7eSAndroid Build Coastguard Worker
29*49cdfc7eSAndroid Build Coastguard Worker #define ENOATTR ENODATA
30*49cdfc7eSAndroid Build Coastguard Worker
31*49cdfc7eSAndroid Build Coastguard Worker #define XATTR_TEST_KEY "user.testkey"
32*49cdfc7eSAndroid Build Coastguard Worker #define XATTR_TEST_VALUE "this is a test value"
33*49cdfc7eSAndroid Build Coastguard Worker #define XATTR_TEST_VALUE_SIZE 20
34*49cdfc7eSAndroid Build Coastguard Worker
35*49cdfc7eSAndroid Build Coastguard Worker #define MNTPOINT "mntpoint"
36*49cdfc7eSAndroid Build Coastguard Worker #define FNAME MNTPOINT"/fremovexattr01testfile"
37*49cdfc7eSAndroid Build Coastguard Worker
38*49cdfc7eSAndroid Build Coastguard Worker static int fd = -1;
39*49cdfc7eSAndroid Build Coastguard Worker static char got_value[XATTR_TEST_VALUE_SIZE];
40*49cdfc7eSAndroid Build Coastguard Worker
verify_fremovexattr(void)41*49cdfc7eSAndroid Build Coastguard Worker static void verify_fremovexattr(void)
42*49cdfc7eSAndroid Build Coastguard Worker {
43*49cdfc7eSAndroid Build Coastguard Worker SAFE_FSETXATTR(fd, XATTR_TEST_KEY, XATTR_TEST_VALUE,
44*49cdfc7eSAndroid Build Coastguard Worker XATTR_TEST_VALUE_SIZE, XATTR_CREATE);
45*49cdfc7eSAndroid Build Coastguard Worker
46*49cdfc7eSAndroid Build Coastguard Worker TEST(fremovexattr(fd, XATTR_TEST_KEY));
47*49cdfc7eSAndroid Build Coastguard Worker
48*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET != 0) {
49*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TTERRNO, "fremovexattr(2) failed");
50*49cdfc7eSAndroid Build Coastguard Worker return;
51*49cdfc7eSAndroid Build Coastguard Worker }
52*49cdfc7eSAndroid Build Coastguard Worker
53*49cdfc7eSAndroid Build Coastguard Worker TEST(fgetxattr(fd, XATTR_TEST_KEY, got_value, sizeof(got_value)));
54*49cdfc7eSAndroid Build Coastguard Worker
55*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET >= 0) {
56*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "fremovexattr(2) did not remove attribute");
57*49cdfc7eSAndroid Build Coastguard Worker return;
58*49cdfc7eSAndroid Build Coastguard Worker }
59*49cdfc7eSAndroid Build Coastguard Worker
60*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET < 0 && TST_ERR != ENOATTR) {
61*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK | TTERRNO,
62*49cdfc7eSAndroid Build Coastguard Worker "fremovexattr(2) could not verify removal");
63*49cdfc7eSAndroid Build Coastguard Worker return;
64*49cdfc7eSAndroid Build Coastguard Worker }
65*49cdfc7eSAndroid Build Coastguard Worker
66*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "fremovexattr(2) removed attribute as expected");
67*49cdfc7eSAndroid Build Coastguard Worker }
68*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)69*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
70*49cdfc7eSAndroid Build Coastguard Worker {
71*49cdfc7eSAndroid Build Coastguard Worker if (fd > 0)
72*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd);
73*49cdfc7eSAndroid Build Coastguard Worker }
74*49cdfc7eSAndroid Build Coastguard Worker
setup(void)75*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
76*49cdfc7eSAndroid Build Coastguard Worker {
77*49cdfc7eSAndroid Build Coastguard Worker fd = SAFE_OPEN(FNAME, O_RDWR | O_CREAT, 0644);
78*49cdfc7eSAndroid Build Coastguard Worker
79*49cdfc7eSAndroid Build Coastguard Worker TEST(fremovexattr(fd, XATTR_TEST_KEY));
80*49cdfc7eSAndroid Build Coastguard Worker
81*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET == -1 && TST_ERR == EOPNOTSUPP)
82*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TCONF, "fremovexattr(2) not supported");
83*49cdfc7eSAndroid Build Coastguard Worker }
84*49cdfc7eSAndroid Build Coastguard Worker
85*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
86*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
87*49cdfc7eSAndroid Build Coastguard Worker .test_all = verify_fremovexattr,
88*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
89*49cdfc7eSAndroid Build Coastguard Worker .mntpoint = MNTPOINT,
90*49cdfc7eSAndroid Build Coastguard Worker .mount_device = 1,
91*49cdfc7eSAndroid Build Coastguard Worker .all_filesystems = 1,
92*49cdfc7eSAndroid Build Coastguard Worker .needs_root = 1,
93*49cdfc7eSAndroid Build Coastguard Worker };
94*49cdfc7eSAndroid Build Coastguard Worker
95*49cdfc7eSAndroid Build Coastguard Worker #else /* HAVE_SYS_XATTR_H */
96*49cdfc7eSAndroid Build Coastguard Worker TST_TEST_TCONF("<sys/xattr.h> does not exist");
97*49cdfc7eSAndroid Build Coastguard Worker #endif
98