Eryu Guan | 9542299 | 2012-01-06 20:46:11 +0800 | [diff] [blame] | 1 | /* |
| 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 Zhang | a97441d | 2012-01-10 11:01:10 +0800 | [diff] [blame] | 45 | #include "config.h" |
Eryu Guan | 9542299 | 2012-01-06 20:46:11 +0800 | [diff] [blame] | 46 | #include <sys/types.h> |
| 47 | #include <sys/stat.h> |
| 48 | #include <sys/wait.h> |
Eryu Guan | 9542299 | 2012-01-06 20:46:11 +0800 | [diff] [blame] | 49 | #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 Zhang | a97441d | 2012-01-10 11:01:10 +0800 | [diff] [blame] | 56 | #ifdef HAVE_ATTR_XATTR_H |
| 57 | #include <attr/xattr.h> |
| 58 | #endif |
Eryu Guan | 9542299 | 2012-01-06 20:46:11 +0800 | [diff] [blame] | 59 | #include "test.h" |
Eryu Guan | 9542299 | 2012-01-06 20:46:11 +0800 | [diff] [blame] | 60 | |
Caspar Zhang | a97441d | 2012-01-10 11:01:10 +0800 | [diff] [blame] | 61 | char *TCID = "setxattr02"; |
| 62 | |
| 63 | #ifdef HAVE_ATTR_XATTR_H |
Eryu Guan | 9542299 | 2012-01-06 20:46:11 +0800 | [diff] [blame] | 64 | #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 | |
| 76 | static void setup(void); |
| 77 | static void cleanup(void); |
| 78 | |
Eryu Guan | 9542299 | 2012-01-06 20:46:11 +0800 | [diff] [blame] | 79 | struct test_case { |
| 80 | char *fname; |
| 81 | char *key; |
| 82 | char *value; |
| 83 | size_t size; |
| 84 | int flags; |
| 85 | int exp_err; |
| 86 | }; |
| 87 | static struct test_case tc[] = { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 88 | { /* 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 Guan | 9542299 | 2012-01-06 20:46:11 +0800 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | int TST_TOTAL = sizeof(tc) / sizeof(tc[0]); |
| 147 | |
| 148 | int main(int argc, char *argv[]) |
| 149 | { |
| 150 | int lc; |
| 151 | int i; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 152 | const char *msg; |
Eryu Guan | 9542299 | 2012-01-06 20:46:11 +0800 | [diff] [blame] | 153 | |
| 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 Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 161 | tst_count = 0; |
Eryu Guan | 9542299 | 2012-01-06 20:46:11 +0800 | [diff] [blame] | 162 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 163 | for (i = 0; i < TST_TOTAL; i++) { |
Eryu Guan | 9542299 | 2012-01-06 20:46:11 +0800 | [diff] [blame] | 164 | TEST(setxattr(tc[i].fname, tc[i].key, tc[i].value, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 165 | tc[i].size, tc[i].flags)); |
Eryu Guan | 9542299 | 2012-01-06 20:46:11 +0800 | [diff] [blame] | 166 | |
| 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 171 | "- expected errno %d - Got", |
| 172 | tc[i].exp_err); |
Eryu Guan | 9542299 | 2012-01-06 20:46:11 +0800 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | cleanup(); |
| 178 | tst_exit(); |
| 179 | } |
| 180 | |
| 181 | static 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 197 | "mount without user_xattr option"); |
Eryu Guan | 9542299 | 2012-01-06 20:46:11 +0800 | [diff] [blame] | 198 | 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 204 | FILENAME); |
Eryu Guan | 9542299 | 2012-01-06 20:46:11 +0800 | [diff] [blame] | 205 | close(fd); |
| 206 | |
| 207 | if (mkdir(DIRNAME, 0644) == -1) |
| 208 | tst_brkm(TBROK | TERRNO, cleanup, "Create test dir(%s) failed", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 209 | DIRNAME); |
Eryu Guan | 9542299 | 2012-01-06 20:46:11 +0800 | [diff] [blame] | 210 | |
| 211 | if (symlink(FILENAME, SYMLINK) == -1) |
| 212 | tst_brkm(TBROK | TERRNO, cleanup, "Create symlink(%s->%s)" |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 213 | " failed", SYMLINK, FILENAME); |
Eryu Guan | 9542299 | 2012-01-06 20:46:11 +0800 | [diff] [blame] | 214 | |
| 215 | if (mknod(FIFO, S_IFIFO | 0777, 0) == -1) |
| 216 | tst_brkm(TBROK | TERRNO, cleanup, "Create FIFO(%s) failed", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 217 | FIFO); |
Eryu Guan | 9542299 | 2012-01-06 20:46:11 +0800 | [diff] [blame] | 218 | |
| 219 | if (mknod(CHR, S_IFCHR | 0777, 0) == -1) |
| 220 | tst_brkm(TBROK | TERRNO, cleanup, "Create char special(%s)" |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 221 | " failed", CHR); |
Eryu Guan | 9542299 | 2012-01-06 20:46:11 +0800 | [diff] [blame] | 222 | |
| 223 | if (mknod(BLK, S_IFBLK | 0777, 0) == -1) |
| 224 | tst_brkm(TBROK | TERRNO, cleanup, "Create block special(%s)" |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 225 | " failed", BLK); |
Eryu Guan | 9542299 | 2012-01-06 20:46:11 +0800 | [diff] [blame] | 226 | |
| 227 | if (mknod(SOCK, S_IFSOCK | 0777, 0) == -1) |
| 228 | tst_brkm(TBROK | TERRNO, cleanup, "Create socket(%s) failed", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 229 | SOCK); |
Eryu Guan | 9542299 | 2012-01-06 20:46:11 +0800 | [diff] [blame] | 230 | |
| 231 | TEST_PAUSE; |
| 232 | } |
| 233 | |
| 234 | static void cleanup(void) |
| 235 | { |
Eryu Guan | 9542299 | 2012-01-06 20:46:11 +0800 | [diff] [blame] | 236 | tst_rmdir(); |
| 237 | } |
Caspar Zhang | a97441d | 2012-01-10 11:01:10 +0800 | [diff] [blame] | 238 | #else /* HAVE_ATTR_XATTR_H */ |
| 239 | int main(int argc, char *argv[]) |
| 240 | { |
| 241 | tst_brkm(TCONF, NULL, "<attr/xattr.h> does not exist."); |
| 242 | } |
| 243 | #endif |