Eryu Guan | f63c53e | 2012-01-20 17:54:07 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | * An empty buffer of size zero can be passed into getxattr(2) to return |
| 27 | * the current size of the named extended attribute. |
| 28 | */ |
| 29 | |
| 30 | #include "config.h" |
| 31 | #include <sys/types.h> |
| 32 | #include <sys/stat.h> |
| 33 | #include <sys/wait.h> |
| 34 | #include <errno.h> |
| 35 | #include <fcntl.h> |
| 36 | #include <unistd.h> |
| 37 | #include <signal.h> |
| 38 | #include <stdio.h> |
| 39 | #include <stdlib.h> |
| 40 | #include <string.h> |
| 41 | #ifdef HAVE_ATTR_XATTR_H |
| 42 | #include <attr/xattr.h> |
| 43 | #endif |
| 44 | #include "test.h" |
Eryu Guan | f63c53e | 2012-01-20 17:54:07 +0800 | [diff] [blame] | 45 | |
| 46 | char *TCID = "getxattr03"; |
| 47 | |
| 48 | #ifdef HAVE_ATTR_XATTR_H |
| 49 | #define XATTR_TEST_KEY "user.testkey" |
| 50 | #define XATTR_TEST_VALUE "test value" |
| 51 | #define XATTR_TEST_VALUE_SIZE (sizeof(XATTR_TEST_VALUE) - 1) |
| 52 | #define TESTFILE "getxattr03testfile" |
| 53 | |
| 54 | static void setup(void); |
| 55 | static void cleanup(void); |
| 56 | |
| 57 | int TST_TOTAL = 1; |
| 58 | |
| 59 | int main(int argc, char *argv[]) |
| 60 | { |
| 61 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 62 | const char *msg; |
Eryu Guan | f63c53e | 2012-01-20 17:54:07 +0800 | [diff] [blame] | 63 | |
| 64 | msg = parse_opts(argc, argv, NULL, NULL); |
| 65 | if (msg != NULL) |
| 66 | tst_brkm(TBROK, tst_exit, "OPTION PARSING ERROR - %s", msg); |
| 67 | |
| 68 | setup(); |
| 69 | |
| 70 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 71 | tst_count = 0; |
Eryu Guan | f63c53e | 2012-01-20 17:54:07 +0800 | [diff] [blame] | 72 | |
| 73 | TEST(getxattr(TESTFILE, XATTR_TEST_KEY, NULL, 0)); |
| 74 | |
| 75 | if (TEST_RETURN == XATTR_TEST_VALUE_SIZE) |
| 76 | tst_resm(TPASS, "getxattr(2) returned correct value"); |
| 77 | else |
| 78 | tst_resm(TFAIL | TTERRNO, "getxattr(2) failed"); |
| 79 | } |
| 80 | |
| 81 | cleanup(); |
| 82 | tst_exit(); |
| 83 | } |
| 84 | |
| 85 | static void setup(void) |
| 86 | { |
| 87 | int fd; |
| 88 | |
| 89 | tst_require_root(NULL); |
| 90 | |
| 91 | tst_tmpdir(); |
| 92 | |
| 93 | /* Test for xattr support and set attr value */ |
| 94 | fd = creat(TESTFILE, 0644); |
| 95 | if (fd == -1) |
| 96 | tst_brkm(TBROK | TERRNO, cleanup, "Create %s failed", TESTFILE); |
| 97 | close(fd); |
| 98 | |
| 99 | if (setxattr(TESTFILE, XATTR_TEST_KEY, XATTR_TEST_VALUE, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 100 | XATTR_TEST_VALUE_SIZE, XATTR_CREATE) == -1) { |
Eryu Guan | f63c53e | 2012-01-20 17:54:07 +0800 | [diff] [blame] | 101 | if (errno == ENOTSUP) |
| 102 | tst_brkm(TCONF, cleanup, "No xattr support in fs or " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 103 | "fs mounted without user_xattr option"); |
Eryu Guan | f63c53e | 2012-01-20 17:54:07 +0800 | [diff] [blame] | 104 | else |
| 105 | tst_brkm(TBROK | TERRNO, cleanup, "setxattr %s failed", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 106 | TESTFILE); |
Eryu Guan | f63c53e | 2012-01-20 17:54:07 +0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | TEST_PAUSE; |
| 110 | } |
| 111 | |
| 112 | static void cleanup(void) |
| 113 | { |
Eryu Guan | f63c53e | 2012-01-20 17:54:07 +0800 | [diff] [blame] | 114 | tst_rmdir(); |
| 115 | } |
| 116 | #else /* HAVE_ATTR_XATTR_H */ |
| 117 | int main(int argc, char *argv[]) |
| 118 | { |
| 119 | tst_brkm(TCONF, NULL, "<attr/xattr.h> does not exist."); |
| 120 | } |
| 121 | #endif |