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: listxattr03
21 *
22 * Description:
23 * An empty buffer of size zero can return the current size of the list
24 * of extended attribute names, which can be used to estimate a suitable buffer.
25 */
26
27 #include "config.h"
28 #include <errno.h>
29 #include <sys/types.h>
30
31 #ifdef HAVE_SYS_XATTR_H
32 # include <sys/xattr.h>
33 #endif
34
35 #include "tst_test.h"
36
37 #ifdef HAVE_SYS_XATTR_H
38
39 #define SECURITY_KEY "security.ltptest"
40 #define VALUE "test"
41 #define VALUE_SIZE (sizeof(VALUE) - 1)
42
43 static const char * const filename[] = {"testfile1", "testfile2"};
44
check_suitable_buf(const char * name,long size)45 static int check_suitable_buf(const char *name, long size)
46 {
47 int n;
48 char buf[size];
49
50 n = listxattr(name, buf, sizeof(buf));
51
52 return n != -1;
53 }
54
verify_listxattr(unsigned int n)55 static void verify_listxattr(unsigned int n)
56 {
57 const char *name = filename[n];
58
59 TEST(listxattr(name, NULL, 0));
60 if (TST_RET == -1) {
61 tst_res(TFAIL | TTERRNO, "listxattr() failed");
62 return;
63 }
64
65 if (check_suitable_buf(name, TST_RET))
66 tst_res(TPASS, "listxattr() succeed with suitable buffer");
67 else
68 tst_res(TFAIL, "listxattr() failed with small buffer");
69 }
70
setup(void)71 static void setup(void)
72 {
73 SAFE_TOUCH(filename[0], 0644, NULL);
74
75 SAFE_TOUCH(filename[1], 0644, NULL);
76
77 SAFE_SETXATTR(filename[1], SECURITY_KEY, VALUE, VALUE_SIZE, XATTR_CREATE);
78 }
79
80 static struct tst_test test = {
81 .needs_tmpdir = 1,
82 .needs_root = 1,
83 .test = verify_listxattr,
84 .tcnt = ARRAY_SIZE(filename),
85 .setup = setup,
86 };
87
88 #else /* HAVE_SYS_XATTR_H */
89 TST_TEST_TCONF("<sys/xattr.h> does not exist.");
90 #endif
91