blob: e91ac4283c8e63149585c1e9144673453aea03db [file] [log] [blame]
Eryu Guanf63c53e2012-01-20 17:54:07 +08001/*
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 Guanf63c53e2012-01-20 17:54:07 +080045
46char *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
54static void setup(void);
55static void cleanup(void);
56
57int TST_TOTAL = 1;
58
59int main(int argc, char *argv[])
60{
61 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020062 const char *msg;
Eryu Guanf63c53e2012-01-20 17:54:07 +080063
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 Zhangd59a6592013-03-07 14:59:12 +080071 tst_count = 0;
Eryu Guanf63c53e2012-01-20 17:54:07 +080072
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
85static 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 Gao354ebb42012-12-07 10:10:04 +0800100 XATTR_TEST_VALUE_SIZE, XATTR_CREATE) == -1) {
Eryu Guanf63c53e2012-01-20 17:54:07 +0800101 if (errno == ENOTSUP)
102 tst_brkm(TCONF, cleanup, "No xattr support in fs or "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800103 "fs mounted without user_xattr option");
Eryu Guanf63c53e2012-01-20 17:54:07 +0800104 else
105 tst_brkm(TBROK | TERRNO, cleanup, "setxattr %s failed",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800106 TESTFILE);
Eryu Guanf63c53e2012-01-20 17:54:07 +0800107 }
108
109 TEST_PAUSE;
110}
111
112static void cleanup(void)
113{
Eryu Guanf63c53e2012-01-20 17:54:07 +0800114 tst_rmdir();
115}
116#else /* HAVE_ATTR_XATTR_H */
117int main(int argc, char *argv[])
118{
119 tst_brkm(TCONF, NULL, "<attr/xattr.h> does not exist.");
120}
121#endif