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