xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/lgetxattr/lgetxattr02.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2016 Fujitsu Ltd.
4*49cdfc7eSAndroid Build Coastguard Worker * Author: Jinbao Huang <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker */
6*49cdfc7eSAndroid Build Coastguard Worker 
7*49cdfc7eSAndroid Build Coastguard Worker /*
8*49cdfc7eSAndroid Build Coastguard Worker * Test Name: lgetxattr02
9*49cdfc7eSAndroid Build Coastguard Worker *
10*49cdfc7eSAndroid Build Coastguard Worker * Description:
11*49cdfc7eSAndroid Build Coastguard Worker * 1) lgetxattr(2) fails if the named attribute does not exist.
12*49cdfc7eSAndroid Build Coastguard Worker * 2) lgetxattr(2) fails if the size of the value buffer is too small
13*49cdfc7eSAndroid Build Coastguard Worker *    to hold the result.
14*49cdfc7eSAndroid Build Coastguard Worker * 3) lgetxattr(2) fails when attemptes to read from a invalid address.
15*49cdfc7eSAndroid Build Coastguard Worker *
16*49cdfc7eSAndroid Build Coastguard Worker * Expected Result:
17*49cdfc7eSAndroid Build Coastguard Worker * 1) lgetxattr(2) should return -1 and set errno to ENODATA.
18*49cdfc7eSAndroid Build Coastguard Worker * 2) lgetxattr(2) should return -1 and set errno to ERANGE.
19*49cdfc7eSAndroid Build Coastguard Worker * 3) lgetxattr(2) should return -1 and set errno to EFAULT.
20*49cdfc7eSAndroid Build Coastguard Worker */
21*49cdfc7eSAndroid Build Coastguard Worker 
22*49cdfc7eSAndroid Build Coastguard Worker #include "config.h"
23*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
24*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
25*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
26*49cdfc7eSAndroid Build Coastguard Worker 
27*49cdfc7eSAndroid Build Coastguard Worker #ifdef HAVE_SYS_XATTR_H
28*49cdfc7eSAndroid Build Coastguard Worker # include <sys/xattr.h>
29*49cdfc7eSAndroid Build Coastguard Worker #endif
30*49cdfc7eSAndroid Build Coastguard Worker 
31*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
32*49cdfc7eSAndroid Build Coastguard Worker 
33*49cdfc7eSAndroid Build Coastguard Worker #ifdef HAVE_SYS_XATTR_H
34*49cdfc7eSAndroid Build Coastguard Worker 
35*49cdfc7eSAndroid Build Coastguard Worker #define SECURITY_KEY	"security.ltptest"
36*49cdfc7eSAndroid Build Coastguard Worker #define VALUE	"this is a test value"
37*49cdfc7eSAndroid Build Coastguard Worker 
38*49cdfc7eSAndroid Build Coastguard Worker static struct test_case {
39*49cdfc7eSAndroid Build Coastguard Worker 	const char *path;
40*49cdfc7eSAndroid Build Coastguard Worker 	size_t size;
41*49cdfc7eSAndroid Build Coastguard Worker 	int exp_err;
42*49cdfc7eSAndroid Build Coastguard Worker } tcase[] = {
43*49cdfc7eSAndroid Build Coastguard Worker 	{"testfile", sizeof(VALUE), ENODATA},
44*49cdfc7eSAndroid Build Coastguard Worker 	{"symlink", 1, ERANGE},
45*49cdfc7eSAndroid Build Coastguard Worker 	{(char *)-1, sizeof(VALUE), EFAULT}
46*49cdfc7eSAndroid Build Coastguard Worker };
47*49cdfc7eSAndroid Build Coastguard Worker 
verify_lgetxattr(unsigned int n)48*49cdfc7eSAndroid Build Coastguard Worker static void verify_lgetxattr(unsigned int n)
49*49cdfc7eSAndroid Build Coastguard Worker {
50*49cdfc7eSAndroid Build Coastguard Worker 	struct test_case *tc = tcase + n;
51*49cdfc7eSAndroid Build Coastguard Worker 	char buf[tc->size];
52*49cdfc7eSAndroid Build Coastguard Worker 
53*49cdfc7eSAndroid Build Coastguard Worker 	TEST(lgetxattr(tc->path, SECURITY_KEY, buf, sizeof(buf)));
54*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET != -1) {
55*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "lgetxattr() succeeded unexpectedly");
56*49cdfc7eSAndroid Build Coastguard Worker 		return;
57*49cdfc7eSAndroid Build Coastguard Worker 	}
58*49cdfc7eSAndroid Build Coastguard Worker 
59*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_ERR != tc->exp_err) {
60*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO, "lgetxattr() failed unexpectedlly, "
61*49cdfc7eSAndroid Build Coastguard Worker 			"expected %s", tst_strerrno(tc->exp_err));
62*49cdfc7eSAndroid Build Coastguard Worker 	} else {
63*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS | TTERRNO, "lgetxattr() failed as expected");
64*49cdfc7eSAndroid Build Coastguard Worker 	}
65*49cdfc7eSAndroid Build Coastguard Worker }
66*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)67*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
68*49cdfc7eSAndroid Build Coastguard Worker {
69*49cdfc7eSAndroid Build Coastguard Worker 	int res;
70*49cdfc7eSAndroid Build Coastguard Worker 
71*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_TOUCH("testfile", 0644, NULL);
72*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SYMLINK("testfile", "symlink");
73*49cdfc7eSAndroid Build Coastguard Worker 
74*49cdfc7eSAndroid Build Coastguard Worker 	res = lsetxattr("symlink", SECURITY_KEY, VALUE, strlen(VALUE), XATTR_CREATE);
75*49cdfc7eSAndroid Build Coastguard Worker 	if (res == -1) {
76*49cdfc7eSAndroid Build Coastguard Worker 		if (errno == ENOTSUP) {
77*49cdfc7eSAndroid Build Coastguard Worker 			tst_brk(TCONF, "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_brk(TBROK | TERRNO, "lsetxattr(%s) failed",
81*49cdfc7eSAndroid Build Coastguard Worker 				SECURITY_KEY);
82*49cdfc7eSAndroid Build Coastguard Worker 		}
83*49cdfc7eSAndroid Build Coastguard Worker 	}
84*49cdfc7eSAndroid Build Coastguard Worker }
85*49cdfc7eSAndroid Build Coastguard Worker 
86*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
87*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1,
88*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
89*49cdfc7eSAndroid Build Coastguard Worker 	.test = verify_lgetxattr,
90*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcase),
91*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup
92*49cdfc7eSAndroid Build Coastguard Worker };
93*49cdfc7eSAndroid Build Coastguard Worker 
94*49cdfc7eSAndroid Build Coastguard Worker #else /* HAVE_SYS_XATTR_H */
95*49cdfc7eSAndroid Build Coastguard Worker 	TST_TEST_TCONF("<sys/xattr.h> does not exist.");
96*49cdfc7eSAndroid Build Coastguard Worker #endif
97