1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2016 RT-RK Institute for Computer Based Systems
4 * Author: Dejan Jovicevic <dejan.jovicevic@rt-rk.com>
5 */
6
7 /*
8 * Test Name: listxattr02
9 *
10 * Description:
11 * 1) listxattr(2) fails if the size of the list buffer is too small
12 * to hold the result.
13 * 2) listxattr(2) fails if path is an empty string.
14 * 3) listxattr(2) fails when attempted to read from a invalid address.
15 * 4) listxattr(2) fails if path is longer than allowed.
16 *
17 * Expected Result:
18 * 1) listxattr(2) should return -1 and set errno to ERANGE.
19 * 2) listxattr(2) should return -1 and set errno to ENOENT.
20 * 3) listxattr(2) should return -1 and set errno to EFAULT.
21 * 4) listxattr(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
41 char longpathname[PATH_MAX + 2];
42
43 static struct test_case {
44 const char *path;
45 size_t size;
46 int exp_err;
47 } tc[] = {
48 {TESTFILE, 1, ERANGE},
49 {"", 20, ENOENT},
50 {(char *)-1, 20, EFAULT},
51 {longpathname, 20, ENAMETOOLONG}
52 };
53
verify_listxattr(unsigned int n)54 static void verify_listxattr(unsigned int n)
55 {
56 struct test_case *t = tc + n;
57 char buf[t->size];
58
59 TEST(listxattr(t->path, buf, sizeof(buf)));
60 if (TST_RET != -1) {
61 tst_res(TFAIL,
62 "listxattr() succeeded unexpectedly (returned %ld)",
63 TST_RET);
64 return;
65 }
66
67 if (t->exp_err != TST_ERR) {
68 tst_res(TFAIL | TTERRNO, "listxattr() failed "
69 "unexpectedlly, expected %s",
70 tst_strerrno(t->exp_err));
71 } else {
72 tst_res(TPASS | TTERRNO,
73 "listxattr() failed as expected");
74 }
75 }
76
setup(void)77 static void setup(void)
78 {
79 SAFE_TOUCH(TESTFILE, 0644, NULL);
80
81 SAFE_SETXATTR(TESTFILE, SECURITY_KEY, VALUE, VALUE_SIZE, XATTR_CREATE);
82
83 memset(&longpathname, 'a', sizeof(longpathname) - 1);
84 }
85
86 static struct tst_test test = {
87 .needs_tmpdir = 1,
88 .needs_root = 1,
89 .test = verify_listxattr,
90 .tcnt = ARRAY_SIZE(tc),
91 .setup = setup,
92 };
93
94 #else /* HAVE_SYS_XATTR_H */
95 TST_TEST_TCONF("<sys/xattr.h> does not exist.");
96 #endif
97