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