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: removexattr02
19f08c3bdfSopenharmony_ci*
20f08c3bdfSopenharmony_ci* Description:
21f08c3bdfSopenharmony_ci* 1) removexattr(2) fails if the named attribute does not exist.
22f08c3bdfSopenharmony_ci* 2) removexattr(2) fails if path is an empty string.
23f08c3bdfSopenharmony_ci* 3) removexattr(2) fails when attempted to read from a invalid address.
24f08c3bdfSopenharmony_ci*
25f08c3bdfSopenharmony_ci* Expected Result:
26f08c3bdfSopenharmony_ci* 1) removexattr(2) should return -1 and set errno to ENODATA.
27f08c3bdfSopenharmony_ci* 2) removcxattr(2) should return -1 and set errno to ENOENT.
28f08c3bdfSopenharmony_ci* 3) removexattr(2) should return -1 and set errno to EFAULT.
29f08c3bdfSopenharmony_ci*/
30f08c3bdfSopenharmony_ci
31f08c3bdfSopenharmony_ci#include "config.h"
32f08c3bdfSopenharmony_ci#include <errno.h>
33f08c3bdfSopenharmony_ci#include <sys/types.h>
34f08c3bdfSopenharmony_ci
35f08c3bdfSopenharmony_ci#ifdef HAVE_SYS_XATTR_H
36f08c3bdfSopenharmony_ci# include <sys/xattr.h>
37f08c3bdfSopenharmony_ci#endif
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_ci#include "test.h"
40f08c3bdfSopenharmony_ci#include "safe_macros.h"
41f08c3bdfSopenharmony_ci
42f08c3bdfSopenharmony_cichar *TCID = "removexattr02";
43f08c3bdfSopenharmony_ci
44f08c3bdfSopenharmony_ci#ifdef HAVE_SYS_XATTR_H
45f08c3bdfSopenharmony_ci
46f08c3bdfSopenharmony_cistatic struct test_case {
47f08c3bdfSopenharmony_ci	const char *path;
48f08c3bdfSopenharmony_ci	char *name;
49f08c3bdfSopenharmony_ci	int exp_err;
50f08c3bdfSopenharmony_ci} tc[] = {
51f08c3bdfSopenharmony_ci	/* test1 */
52f08c3bdfSopenharmony_ci	{"testfile", "user.test", ENODATA},
53f08c3bdfSopenharmony_ci	/* test2 */
54f08c3bdfSopenharmony_ci	{"", "user.test", ENOENT},
55f08c3bdfSopenharmony_ci	/* test3 */
56f08c3bdfSopenharmony_ci	{(char *)-1, "user.test", EFAULT}
57f08c3bdfSopenharmony_ci};
58f08c3bdfSopenharmony_ci
59f08c3bdfSopenharmony_cistatic void verify_removexattr(struct test_case *tc);
60f08c3bdfSopenharmony_cistatic void setup(void);
61f08c3bdfSopenharmony_cistatic void cleanup(void);
62f08c3bdfSopenharmony_ci
63f08c3bdfSopenharmony_ciint TST_TOTAL = ARRAY_SIZE(tc);
64f08c3bdfSopenharmony_ci
65f08c3bdfSopenharmony_ciint main(int ac, char **av)
66f08c3bdfSopenharmony_ci{
67f08c3bdfSopenharmony_ci	int lc;
68f08c3bdfSopenharmony_ci	int i;
69f08c3bdfSopenharmony_ci
70f08c3bdfSopenharmony_ci	tst_parse_opts(ac, av, NULL, NULL);
71f08c3bdfSopenharmony_ci
72f08c3bdfSopenharmony_ci	setup();
73f08c3bdfSopenharmony_ci
74f08c3bdfSopenharmony_ci	for (lc = 0; TEST_LOOPING(lc); lc++) {
75f08c3bdfSopenharmony_ci		tst_count = 0;
76f08c3bdfSopenharmony_ci		for (i = 0; i < TST_TOTAL; i++)
77f08c3bdfSopenharmony_ci			verify_removexattr(&tc[i]);
78f08c3bdfSopenharmony_ci	}
79f08c3bdfSopenharmony_ci
80f08c3bdfSopenharmony_ci	cleanup();
81f08c3bdfSopenharmony_ci	tst_exit();
82f08c3bdfSopenharmony_ci}
83f08c3bdfSopenharmony_ci
84f08c3bdfSopenharmony_cistatic void verify_removexattr(struct test_case *tc)
85f08c3bdfSopenharmony_ci{
86f08c3bdfSopenharmony_ci
87f08c3bdfSopenharmony_ci	TEST(removexattr(tc->path, tc->name));
88f08c3bdfSopenharmony_ci	if (TEST_RETURN == -1 && TEST_ERRNO == ENOTSUP) {
89f08c3bdfSopenharmony_ci		tst_brkm(TCONF, cleanup, "No xattr support in fs or "
90f08c3bdfSopenharmony_ci			 "mount without user_xattr option");
91f08c3bdfSopenharmony_ci	}
92f08c3bdfSopenharmony_ci
93f08c3bdfSopenharmony_ci	if (TEST_RETURN != -1) {
94f08c3bdfSopenharmony_ci		tst_resm(TFAIL, "removexattr() succeeded unexpectedly");
95f08c3bdfSopenharmony_ci		return;
96f08c3bdfSopenharmony_ci	}
97f08c3bdfSopenharmony_ci
98f08c3bdfSopenharmony_ci	if (TEST_ERRNO != tc->exp_err) {
99f08c3bdfSopenharmony_ci		tst_resm(TFAIL | TTERRNO, "removexattr() failed unexpectedly,"
100f08c3bdfSopenharmony_ci			 " expected %s", tst_strerrno(tc->exp_err));
101f08c3bdfSopenharmony_ci	} else {
102f08c3bdfSopenharmony_ci		tst_resm(TPASS | TTERRNO,
103f08c3bdfSopenharmony_ci			 "removexattr() failed as expected");
104f08c3bdfSopenharmony_ci	}
105f08c3bdfSopenharmony_ci}
106f08c3bdfSopenharmony_ci
107f08c3bdfSopenharmony_cistatic void setup(void)
108f08c3bdfSopenharmony_ci{
109f08c3bdfSopenharmony_ci	tst_sig(NOFORK, DEF_HANDLER, cleanup);
110f08c3bdfSopenharmony_ci
111f08c3bdfSopenharmony_ci	TEST_PAUSE;
112f08c3bdfSopenharmony_ci
113f08c3bdfSopenharmony_ci	tst_tmpdir();
114f08c3bdfSopenharmony_ci
115f08c3bdfSopenharmony_ci	SAFE_TOUCH(cleanup, "testfile", 0644, NULL);
116f08c3bdfSopenharmony_ci}
117f08c3bdfSopenharmony_ci
118f08c3bdfSopenharmony_cistatic void cleanup(void)
119f08c3bdfSopenharmony_ci{
120f08c3bdfSopenharmony_ci	tst_rmdir();
121f08c3bdfSopenharmony_ci}
122f08c3bdfSopenharmony_ci
123f08c3bdfSopenharmony_ci#else /* HAVE_SYS_XATTR_H */
124f08c3bdfSopenharmony_ciint main(int ac, char **av)
125f08c3bdfSopenharmony_ci{
126f08c3bdfSopenharmony_ci	tst_brkm(TCONF, NULL, "<sys/xattr.h> does not exist.");
127f08c3bdfSopenharmony_ci}
128f08c3bdfSopenharmony_ci#endif
129