• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (c) 2016 Fujitsu Ltd.
3 * Author: Xiao Yang <yangx.jy@cn.fujitsu.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 *
13 * You should have received a copy of the GNU General Public License
14 * alone with this program.
15 */
16 
17 /*
18 * Test Name: llistxattr02
19 *
20 * Description:
21 * 1) llistxattr(2) fails if the size of the list buffer is too small
22 * to hold the result.
23 * 2) llistxattr(2) fails if path is an empty string.
24 * 3) llistxattr(2) fails when attempted to read from a invalid address.
25 * 4) llistxattr(2) fails if path is longer than allowed.
26 *
27 * Expected Result:
28 * 1) llistxattr(2) should return -1 and set errno to ERANGE.
29 * 2) llistxattr(2) should return -1 and set errno to ENOENT.
30 * 3) llistxattr(2) should return -1 and set errno to EFAULT.
31 * 4) llistxattr(2) should return -1 and set errno to ENAMETOOLONG.
32 */
33 
34 #include "config.h"
35 #include <errno.h>
36 #include <sys/types.h>
37 
38 #ifdef HAVE_SYS_XATTR_H
39 # include <sys/xattr.h>
40 #endif
41 
42 #include "tst_test.h"
43 
44 #ifdef HAVE_SYS_XATTR_H
45 
46 #define SECURITY_KEY    "security.ltptest"
47 #define VALUE           "test"
48 #define VALUE_SIZE      (sizeof(VALUE) - 1)
49 #define TESTFILE        "testfile"
50 #define SYMLINK         "symlink"
51 
52 static char longpathname[PATH_MAX + 2];
53 
54 static struct test_case {
55 	const char *path;
56 	size_t size;
57 	int exp_err;
58 } tc[] = {
59 	{SYMLINK, 1, ERANGE},
60 	{"", 20, ENOENT},
61 	{(char *)-1, 20, EFAULT},
62 	{longpathname, 20, ENAMETOOLONG}
63 };
64 
verify_llistxattr(unsigned int n)65 static void verify_llistxattr(unsigned int n)
66 {
67 	struct test_case *t = tc + n;
68 	char buf[t->size];
69 
70 	TEST(llistxattr(t->path, buf, sizeof(buf)));
71 	if (TEST_RETURN != -1) {
72 		tst_res(TFAIL,
73 			"llistxattr() succeeded unexpectedly (returned %ld)",
74 			TEST_RETURN);
75 		return;
76 	}
77 
78 	if (TEST_ERRNO != t->exp_err) {
79 		tst_res(TFAIL | TTERRNO, "llistxattr() failed "
80 			 "unexpectedlly, expected %s",
81 			 tst_strerrno(t->exp_err));
82 	} else {
83 		tst_res(TPASS | TTERRNO,
84 			 "llistxattr() failed as expected");
85 	}
86 }
87 
setup(void)88 static void setup(void)
89 {
90 	SAFE_TOUCH(TESTFILE, 0644, NULL);
91 
92 	SAFE_SYMLINK(TESTFILE, SYMLINK);
93 
94 	SAFE_LSETXATTR(SYMLINK, SECURITY_KEY, VALUE, VALUE_SIZE, XATTR_CREATE);
95 
96 	memset(longpathname, 'a', sizeof(longpathname) - 1);
97 }
98 
99 static struct tst_test test = {
100 	.tid = "llistxattr02",
101 	.needs_tmpdir = 1,
102 	.needs_root = 1,
103 	.test = verify_llistxattr,
104 	.tcnt = ARRAY_SIZE(tc),
105 	.setup = setup,
106 };
107 
108 #else /* HAVE_SYS_XATTR_H */
109 	TST_TEST_TCONF("<sys/xattr.h> does not exist.");
110 #endif
111