1/*
2* Copyright (c) 2016 Fujitsu Ltd.
3* Author: Xiao Yang <yangx.jy@cn.fujitsu.com>
4*
5* This program is free software; you can redistribute it and/or modify it
6* under the terms of version 2 of the GNU General Public License as
7* published by the Free Software Foundation.
8*
9* This program is distributed in the hope that it would be useful, but
10* WITHOUT ANY WARRANTY; without even the implied warranty of
11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12*
13* You should have received a copy of the GNU General Public License
14* alone with this program.
15*/
16
17/*
18* Test Name: removexattr01
19*
20* Description:
21* The testcase checks the basic functionality of the removexattr(2).
22* removexattr(2) removes the extended attribute identified by
23* name and associated with the given path in the filesystem.
24*/
25
26#include "config.h"
27#include <errno.h>
28#include <sys/types.h>
29
30#ifdef HAVE_ATTR_XATTR_H
31#include <attr/xattr.h>
32#endif
33
34#include "test.h"
35#include "safe_macros.h"
36
37char *TCID = "removexattr01";
38
39#ifdef HAVE_ATTR_XATTR_H
40#define USER_KEY	"user.test"
41#define VALUE	"test"
42#define VALUE_SIZE	(sizeof(VALUE) - 1)
43
44static void verify_removexattr(void);
45static void setup(void);
46static void cleanup(void);
47
48int TST_TOTAL = 1;
49
50int main(int ac, char **av)
51{
52	int lc;
53
54	tst_parse_opts(ac, av, NULL, NULL);
55
56	setup();
57
58	for (lc = 0; TEST_LOOPING(lc); lc++) {
59		tst_count = 0;
60
61		verify_removexattr();
62	}
63
64	cleanup();
65	tst_exit();
66}
67
68static void verify_removexattr(void)
69{
70	int n;
71	int size = 64;
72	char buf[size];
73
74	n = setxattr("testfile", USER_KEY, VALUE, VALUE_SIZE, XATTR_CREATE);
75	if (n == -1) {
76		if (errno == ENOTSUP) {
77			tst_brkm(TCONF, cleanup, "no xattr support in fs or "
78				 "mounted without user_xattr option");
79		} else {
80			tst_brkm(TFAIL | TERRNO, cleanup, "setxattr() failed");
81		}
82	}
83
84	TEST(removexattr("testfile", USER_KEY));
85	if (TEST_RETURN != 0) {
86		tst_resm(TFAIL | TTERRNO, "removexattr() failed");
87		return;
88	}
89
90	n = getxattr("testfile", USER_KEY, buf, size);
91	if (n != -1) {
92		tst_resm(TFAIL, "getxattr() succeeded for deleted key");
93		return;
94	}
95
96	if (errno != ENOATTR) {
97		tst_resm(TFAIL | TTERRNO, "getxattr() failed unexpectedly");
98	} else {
99		tst_resm(TPASS, "removexattr() succeeded");
100	}
101}
102
103static void setup(void)
104{
105	tst_sig(NOFORK, DEF_HANDLER, cleanup);
106
107	TEST_PAUSE;
108
109	tst_tmpdir();
110
111	SAFE_TOUCH(cleanup, "testfile", 0644, NULL);
112}
113
114static void cleanup(void)
115{
116	tst_rmdir();
117}
118
119#else /* HAVE_ATTR_XATTR_H */
120int main(int ac, char **av)
121{
122	tst_brkm(TCONF, NULL, "<attr/xattr.h> does not exist.");
123}
124#endif
125