blob: e9bc9f36bd0c447542f2aaf05baed2dd9831cead [file] [log] [blame]
Eryu Guan95422992012-01-06 20:46:11 +08001/*
2 * Copyright (C) 2011 Red Hat, Inc.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * Further, this software is distributed without any warranty that it
13 * is free of the rightful claim of any third person regarding
14 * infringement or the like. Any license provided herein, whether
15 * implied or otherwise, applies only to this software file. Patent
16 * licenses, if any, provided herein do not apply to combinations of
17 * this program with other software, or any other product whatsoever.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 * 02110-1301, USA.
23 */
24
25/*
26 * In the user.* namespace, only regular files and directories can
27 * have extended attributes. Otherwise setxattr(2) will return -1
28 * and set errno to EPERM.
29 *
30 * There are 7 test cases:
31 * 1. Set attribute to a regular file, setxattr(2) should succeed
32 * 2. Set attribute to a directory, setxattr(2) should succeed
33 * 3. Set attribute to a symlink which points to the regular file,
34 * setxattr(2) should return -1 and set errno to EEXIST
35 * 4. Set attribute to a FIFO, setxattr(2) should return -1 and set
36 * errno to EPERM
37 * 5. Set attribute to a char special file, setxattr(2) should
38 * return -1 and set errno to EPERM
39 * 6. Set attribute to a block special file, setxattr(2) should
40 * return -1 and set errno to EPERM
41 * 7. Set attribute to a UNIX domain socket, setxattr(2) should
42 * return -1 and set errno to EPERM
43 */
44
Caspar Zhanga97441d2012-01-10 11:01:10 +080045#include "config.h"
Eryu Guan95422992012-01-06 20:46:11 +080046#include <sys/types.h>
47#include <sys/stat.h>
48#include <sys/wait.h>
Eryu Guan95422992012-01-06 20:46:11 +080049#include <errno.h>
50#include <fcntl.h>
51#include <unistd.h>
52#include <signal.h>
53#include <stdio.h>
54#include <stdlib.h>
55#include <string.h>
Caspar Zhanga97441d2012-01-10 11:01:10 +080056#ifdef HAVE_ATTR_XATTR_H
57#include <attr/xattr.h>
58#endif
Eryu Guan95422992012-01-06 20:46:11 +080059#include "test.h"
Eryu Guan95422992012-01-06 20:46:11 +080060
Caspar Zhanga97441d2012-01-10 11:01:10 +080061char *TCID = "setxattr02";
62
63#ifdef HAVE_ATTR_XATTR_H
Eryu Guan95422992012-01-06 20:46:11 +080064#define XATTR_TEST_KEY "user.testkey"
65#define XATTR_TEST_VALUE "this is a test value"
66#define XATTR_TEST_VALUE_SIZE 20
67
68#define FILENAME "setxattr02testfile"
69#define DIRNAME "setxattr02testdir"
70#define SYMLINK "setxattr02symlink"
71#define FIFO "setxattr02fifo"
72#define CHR "setxattr02chr"
73#define BLK "setxattr02blk"
74#define SOCK "setxattr02sock"
75
76static void setup(void);
77static void cleanup(void);
78
Eryu Guan95422992012-01-06 20:46:11 +080079struct test_case {
80 char *fname;
81 char *key;
82 char *value;
83 size_t size;
84 int flags;
85 int exp_err;
86};
87static struct test_case tc[] = {
Wanlong Gao354ebb42012-12-07 10:10:04 +080088 { /* case 00, set attr to reg */
89 .fname = FILENAME,
90 .key = XATTR_TEST_KEY,
91 .value = XATTR_TEST_VALUE,
92 .size = XATTR_TEST_VALUE_SIZE,
93 .flags = XATTR_CREATE,
94 .exp_err = 0,
95 },
96 { /* case 01, set attr to dir */
97 .fname = DIRNAME,
98 .key = XATTR_TEST_KEY,
99 .value = XATTR_TEST_VALUE,
100 .size = XATTR_TEST_VALUE_SIZE,
101 .flags = XATTR_CREATE,
102 .exp_err = 0,
103 },
104 { /* case 02, set attr to symlink */
105 .fname = SYMLINK,
106 .key = XATTR_TEST_KEY,
107 .value = XATTR_TEST_VALUE,
108 .size = XATTR_TEST_VALUE_SIZE,
109 .flags = XATTR_CREATE,
110 .exp_err = EEXIST,
111 },
112 { /* case 03, set attr to fifo */
113 .fname = FIFO,
114 .key = XATTR_TEST_KEY,
115 .value = XATTR_TEST_VALUE,
116 .size = XATTR_TEST_VALUE_SIZE,
117 .flags = XATTR_CREATE,
118 .exp_err = EPERM,
119 },
120 { /* case 04, set attr to character special */
121 .fname = CHR,
122 .key = XATTR_TEST_KEY,
123 .value = XATTR_TEST_VALUE,
124 .size = XATTR_TEST_VALUE_SIZE,
125 .flags = XATTR_CREATE,
126 .exp_err = EPERM,
127 },
128 { /* case 05, set attr to block special */
129 .fname = BLK,
130 .key = XATTR_TEST_KEY,
131 .value = XATTR_TEST_VALUE,
132 .size = XATTR_TEST_VALUE_SIZE,
133 .flags = XATTR_CREATE,
134 .exp_err = EPERM,
135 },
136 { /* case 06, set attr to socket */
137 .fname = SOCK,
138 .key = XATTR_TEST_KEY,
139 .value = XATTR_TEST_VALUE,
140 .size = XATTR_TEST_VALUE_SIZE,
141 .flags = XATTR_CREATE,
142 .exp_err = EPERM,
143 },
Eryu Guan95422992012-01-06 20:46:11 +0800144};
145
146int TST_TOTAL = sizeof(tc) / sizeof(tc[0]);
147
148int main(int argc, char *argv[])
149{
150 int lc;
151 int i;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200152 const char *msg;
Eryu Guan95422992012-01-06 20:46:11 +0800153
154 msg = parse_opts(argc, argv, NULL, NULL);
155 if (msg != NULL)
156 tst_brkm(TBROK, tst_exit, "OPTION PARSING ERROR - %s", msg);
157
158 setup();
159
160 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800161 tst_count = 0;
Eryu Guan95422992012-01-06 20:46:11 +0800162
Wanlong Gao354ebb42012-12-07 10:10:04 +0800163 for (i = 0; i < TST_TOTAL; i++) {
Eryu Guan95422992012-01-06 20:46:11 +0800164 TEST(setxattr(tc[i].fname, tc[i].key, tc[i].value,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800165 tc[i].size, tc[i].flags));
Eryu Guan95422992012-01-06 20:46:11 +0800166
167 if (TEST_ERRNO == tc[i].exp_err) {
168 tst_resm(TPASS | TTERRNO, "expected behavior");
169 } else {
170 tst_resm(TFAIL | TTERRNO, "unexpected behavior "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800171 "- expected errno %d - Got",
172 tc[i].exp_err);
Eryu Guan95422992012-01-06 20:46:11 +0800173 }
174 }
175 }
176
177 cleanup();
178 tst_exit();
179}
180
181static void setup(void)
182{
183 int fd;
184
185 tst_require_root(NULL);
186
187 tst_tmpdir();
188
189 /* Test for xattr support */
190 fd = creat("testfile", 0644);
191 if (fd == -1)
192 tst_brkm(TBROK | TERRNO, cleanup, "Create testfile failed");
193 close(fd);
194 if (setxattr("testfile", "user.test", "test", 4, XATTR_CREATE) == -1)
195 if (errno == ENOTSUP)
196 tst_brkm(TCONF, cleanup, "No xattr support in fs or "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800197 "mount without user_xattr option");
Eryu Guan95422992012-01-06 20:46:11 +0800198 unlink("testfile");
199
200 /* Create test files */
201 fd = creat(FILENAME, 0644);
202 if (fd == -1)
203 tst_brkm(TBROK | TERRNO, cleanup, "Create test file(%s) failed",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800204 FILENAME);
Eryu Guan95422992012-01-06 20:46:11 +0800205 close(fd);
206
207 if (mkdir(DIRNAME, 0644) == -1)
208 tst_brkm(TBROK | TERRNO, cleanup, "Create test dir(%s) failed",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800209 DIRNAME);
Eryu Guan95422992012-01-06 20:46:11 +0800210
211 if (symlink(FILENAME, SYMLINK) == -1)
212 tst_brkm(TBROK | TERRNO, cleanup, "Create symlink(%s->%s)"
Wanlong Gao354ebb42012-12-07 10:10:04 +0800213 " failed", SYMLINK, FILENAME);
Eryu Guan95422992012-01-06 20:46:11 +0800214
215 if (mknod(FIFO, S_IFIFO | 0777, 0) == -1)
216 tst_brkm(TBROK | TERRNO, cleanup, "Create FIFO(%s) failed",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800217 FIFO);
Eryu Guan95422992012-01-06 20:46:11 +0800218
219 if (mknod(CHR, S_IFCHR | 0777, 0) == -1)
220 tst_brkm(TBROK | TERRNO, cleanup, "Create char special(%s)"
Wanlong Gao354ebb42012-12-07 10:10:04 +0800221 " failed", CHR);
Eryu Guan95422992012-01-06 20:46:11 +0800222
223 if (mknod(BLK, S_IFBLK | 0777, 0) == -1)
224 tst_brkm(TBROK | TERRNO, cleanup, "Create block special(%s)"
Wanlong Gao354ebb42012-12-07 10:10:04 +0800225 " failed", BLK);
Eryu Guan95422992012-01-06 20:46:11 +0800226
227 if (mknod(SOCK, S_IFSOCK | 0777, 0) == -1)
228 tst_brkm(TBROK | TERRNO, cleanup, "Create socket(%s) failed",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800229 SOCK);
Eryu Guan95422992012-01-06 20:46:11 +0800230
231 TEST_PAUSE;
232}
233
234static void cleanup(void)
235{
Eryu Guan95422992012-01-06 20:46:11 +0800236 tst_rmdir();
237}
Caspar Zhanga97441d2012-01-10 11:01:10 +0800238#else /* HAVE_ATTR_XATTR_H */
239int main(int argc, char *argv[])
240{
241 tst_brkm(TCONF, NULL, "<attr/xattr.h> does not exist.");
242}
243#endif