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