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