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: fremovexattr02
9*49cdfc7eSAndroid Build Coastguard Worker *
10*49cdfc7eSAndroid Build Coastguard Worker * Test cases::
11*49cdfc7eSAndroid Build Coastguard Worker * 1) fremovexattr(2) fails if the named attribute does not exist.
12*49cdfc7eSAndroid Build Coastguard Worker * 2) fremovexattr(2) fails if file descriptor is not valid.
13*49cdfc7eSAndroid Build Coastguard Worker * 3) fremovexattr(2) fails if named attribute has an invalid address.
14*49cdfc7eSAndroid Build Coastguard Worker *
15*49cdfc7eSAndroid Build Coastguard Worker * Expected Results:
16*49cdfc7eSAndroid Build Coastguard Worker * fremovexattr(2) should return -1 and set errno to ENODATA.
17*49cdfc7eSAndroid Build Coastguard Worker * fremovexattr(2) should return -1 and set errno to EBADF.
18*49cdfc7eSAndroid Build Coastguard Worker * fremovexattr(2) should return -1 and set errno to EFAULT.
19*49cdfc7eSAndroid Build Coastguard Worker */
20*49cdfc7eSAndroid Build Coastguard Worker
21*49cdfc7eSAndroid Build Coastguard Worker #include "config.h"
22*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
23*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
24*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
25*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
26*49cdfc7eSAndroid Build Coastguard Worker
27*49cdfc7eSAndroid Build Coastguard Worker #ifdef HAVE_SYS_XATTR_H
28*49cdfc7eSAndroid Build Coastguard Worker # include <sys/xattr.h>
29*49cdfc7eSAndroid Build Coastguard Worker #endif
30*49cdfc7eSAndroid Build Coastguard Worker
31*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
32*49cdfc7eSAndroid Build Coastguard Worker
33*49cdfc7eSAndroid Build Coastguard Worker #ifdef HAVE_SYS_XATTR_H
34*49cdfc7eSAndroid Build Coastguard Worker
35*49cdfc7eSAndroid Build Coastguard Worker #define XATTR_TEST_KEY "user.testkey"
36*49cdfc7eSAndroid Build Coastguard Worker
37*49cdfc7eSAndroid Build Coastguard Worker #define MNTPOINT "mntpoint"
38*49cdfc7eSAndroid Build Coastguard Worker #define FNAME MNTPOINT"/fremovexattr02testfile"
39*49cdfc7eSAndroid Build Coastguard Worker
40*49cdfc7eSAndroid Build Coastguard Worker static int fd = -1;
41*49cdfc7eSAndroid Build Coastguard Worker
42*49cdfc7eSAndroid Build Coastguard Worker struct test_case {
43*49cdfc7eSAndroid Build Coastguard Worker int fd;
44*49cdfc7eSAndroid Build Coastguard Worker char *key;
45*49cdfc7eSAndroid Build Coastguard Worker int exp_err;
46*49cdfc7eSAndroid Build Coastguard Worker };
47*49cdfc7eSAndroid Build Coastguard Worker
48*49cdfc7eSAndroid Build Coastguard Worker struct test_case tc[] = {
49*49cdfc7eSAndroid Build Coastguard Worker { /* case 1: attribute does not exist */
50*49cdfc7eSAndroid Build Coastguard Worker .key = XATTR_TEST_KEY,
51*49cdfc7eSAndroid Build Coastguard Worker .exp_err = ENODATA,
52*49cdfc7eSAndroid Build Coastguard Worker },
53*49cdfc7eSAndroid Build Coastguard Worker { /* case 2: file descriptor is invalid */
54*49cdfc7eSAndroid Build Coastguard Worker .fd = -1,
55*49cdfc7eSAndroid Build Coastguard Worker .key = XATTR_TEST_KEY,
56*49cdfc7eSAndroid Build Coastguard Worker .exp_err = EBADF,
57*49cdfc7eSAndroid Build Coastguard Worker },
58*49cdfc7eSAndroid Build Coastguard Worker { /* case 3: bad name attribute */
59*49cdfc7eSAndroid Build Coastguard Worker .exp_err = EFAULT,
60*49cdfc7eSAndroid Build Coastguard Worker },
61*49cdfc7eSAndroid Build Coastguard Worker };
62*49cdfc7eSAndroid Build Coastguard Worker
verify_fremovexattr(unsigned int i)63*49cdfc7eSAndroid Build Coastguard Worker static void verify_fremovexattr(unsigned int i)
64*49cdfc7eSAndroid Build Coastguard Worker {
65*49cdfc7eSAndroid Build Coastguard Worker TEST(fremovexattr(tc[i].fd, tc[i].key));
66*49cdfc7eSAndroid Build Coastguard Worker
67*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET == -1 && TST_ERR == EOPNOTSUPP)
68*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TCONF, "fremovexattr(2) not supported");
69*49cdfc7eSAndroid Build Coastguard Worker
70*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET == -1) {
71*49cdfc7eSAndroid Build Coastguard Worker if (tc[i].exp_err == TST_ERR) {
72*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS | TTERRNO,
73*49cdfc7eSAndroid Build Coastguard Worker "fremovexattr(2) failed expectedly");
74*49cdfc7eSAndroid Build Coastguard Worker } else {
75*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TTERRNO,
76*49cdfc7eSAndroid Build Coastguard Worker "fremovexattr(2) should fail with %s",
77*49cdfc7eSAndroid Build Coastguard Worker tst_strerrno(tc[i].exp_err));
78*49cdfc7eSAndroid Build Coastguard Worker }
79*49cdfc7eSAndroid Build Coastguard Worker return;
80*49cdfc7eSAndroid Build Coastguard Worker }
81*49cdfc7eSAndroid Build Coastguard Worker
82*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "fremovexattr(2) returned %li", TST_RET);
83*49cdfc7eSAndroid Build Coastguard Worker }
84*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)85*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
86*49cdfc7eSAndroid Build Coastguard Worker {
87*49cdfc7eSAndroid Build Coastguard Worker if (fd > 0)
88*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd);
89*49cdfc7eSAndroid Build Coastguard Worker }
90*49cdfc7eSAndroid Build Coastguard Worker
setup(void)91*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
92*49cdfc7eSAndroid Build Coastguard Worker {
93*49cdfc7eSAndroid Build Coastguard Worker size_t i = 0;
94*49cdfc7eSAndroid Build Coastguard Worker
95*49cdfc7eSAndroid Build Coastguard Worker fd = SAFE_OPEN(FNAME, O_RDWR | O_CREAT, 0644);
96*49cdfc7eSAndroid Build Coastguard Worker
97*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < ARRAY_SIZE(tc); i++) {
98*49cdfc7eSAndroid Build Coastguard Worker
99*49cdfc7eSAndroid Build Coastguard Worker if (tc[i].fd != -1)
100*49cdfc7eSAndroid Build Coastguard Worker tc[i].fd = fd;
101*49cdfc7eSAndroid Build Coastguard Worker
102*49cdfc7eSAndroid Build Coastguard Worker if (!tc[i].key && tc[i].exp_err == EFAULT)
103*49cdfc7eSAndroid Build Coastguard Worker tc[i].key = tst_get_bad_addr(cleanup);
104*49cdfc7eSAndroid Build Coastguard Worker }
105*49cdfc7eSAndroid Build Coastguard Worker }
106*49cdfc7eSAndroid Build Coastguard Worker
107*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
108*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
109*49cdfc7eSAndroid Build Coastguard Worker .test = verify_fremovexattr,
110*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
111*49cdfc7eSAndroid Build Coastguard Worker .tcnt = ARRAY_SIZE(tc),
112*49cdfc7eSAndroid Build Coastguard Worker .mntpoint = MNTPOINT,
113*49cdfc7eSAndroid Build Coastguard Worker .mount_device = 1,
114*49cdfc7eSAndroid Build Coastguard Worker .all_filesystems = 1,
115*49cdfc7eSAndroid Build Coastguard Worker .needs_root = 1,
116*49cdfc7eSAndroid Build Coastguard Worker };
117*49cdfc7eSAndroid Build Coastguard Worker
118*49cdfc7eSAndroid Build Coastguard Worker #else /* HAVE_SYS_XATTR_H */
119*49cdfc7eSAndroid Build Coastguard Worker TST_TEST_TCONF("<sys/xattr.h> does not exist");
120*49cdfc7eSAndroid Build Coastguard Worker #endif
121