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