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