1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2016 Fujitsu Ltd.
4 * Author: Xiao Yang <yangx.jy@cn.fujitsu.com>
5 */
6 
7 /*
8 * Test Name: llistxattr02
9 *
10 * Description:
11 * 1) llistxattr(2) fails if the size of the list buffer is too small
12 * to hold the result.
13 * 2) llistxattr(2) fails if path is an empty string.
14 * 3) llistxattr(2) fails when attempted to read from a invalid address.
15 * 4) llistxattr(2) fails if path is longer than allowed.
16 *
17 * Expected Result:
18 * 1) llistxattr(2) should return -1 and set errno to ERANGE.
19 * 2) llistxattr(2) should return -1 and set errno to ENOENT.
20 * 3) llistxattr(2) should return -1 and set errno to EFAULT.
21 * 4) llistxattr(2) should return -1 and set errno to ENAMETOOLONG.
22 */
23 
24 #include "config.h"
25 #include <errno.h>
26 #include <sys/types.h>
27 
28 #ifdef HAVE_SYS_XATTR_H
29 # include <sys/xattr.h>
30 #endif
31 
32 #include "tst_test.h"
33 
34 #ifdef HAVE_SYS_XATTR_H
35 
36 #define SECURITY_KEY    "security.ltptest"
37 #define VALUE           "test"
38 #define VALUE_SIZE      (sizeof(VALUE) - 1)
39 #define TESTFILE        "testfile"
40 #define SYMLINK         "symlink"
41 
42 static char longpathname[PATH_MAX + 2];
43 
44 static struct test_case {
45 	const char *path;
46 	size_t size;
47 	int exp_err;
48 } tc[] = {
49 	{SYMLINK, 1, ERANGE},
50 	{"", 20, ENOENT},
51 	{(char *)-1, 20, EFAULT},
52 	{longpathname, 20, ENAMETOOLONG}
53 };
54 
verify_llistxattr(unsigned int n)55 static void verify_llistxattr(unsigned int n)
56 {
57 	struct test_case *t = tc + n;
58 	char buf[t->size];
59 
60 	TEST(llistxattr(t->path, buf, sizeof(buf)));
61 	if (TST_RET != -1) {
62 		tst_res(TFAIL,
63 			"llistxattr() succeeded unexpectedly (returned %ld)",
64 			TST_RET);
65 		return;
66 	}
67 
68 	if (TST_ERR != t->exp_err) {
69 		tst_res(TFAIL | TTERRNO, "llistxattr() failed "
70 			 "unexpectedlly, expected %s",
71 			 tst_strerrno(t->exp_err));
72 	} else {
73 		tst_res(TPASS | TTERRNO,
74 			 "llistxattr() failed as expected");
75 	}
76 }
77 
setup(void)78 static void setup(void)
79 {
80 	SAFE_TOUCH(TESTFILE, 0644, NULL);
81 
82 	SAFE_SYMLINK(TESTFILE, SYMLINK);
83 
84 	SAFE_LSETXATTR(SYMLINK, SECURITY_KEY, VALUE, VALUE_SIZE, XATTR_CREATE);
85 
86 	memset(longpathname, 'a', sizeof(longpathname) - 1);
87 }
88 
89 static struct tst_test test = {
90 	.needs_tmpdir = 1,
91 	.needs_root = 1,
92 	.test = verify_llistxattr,
93 	.tcnt = ARRAY_SIZE(tc),
94 	.setup = setup,
95 };
96 
97 #else /* HAVE_SYS_XATTR_H */
98 	TST_TEST_TCONF("<sys/xattr.h> does not exist.");
99 #endif
100