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: verify_flistxattr01
21 *
22 * Description:
23 * The testcase checks the basic functionality of the flistxattr(2).
24 * flistxattr(2) retrieves the list of extended attribute names
25 * associated with the file itself in the filesystem.
26 *
27 */
28 
29 #include "config.h"
30 #include <errno.h>
31 #include <sys/types.h>
32 #include <string.h>
33 
34 #ifdef HAVE_SYS_XATTR_H
35 # include <sys/xattr.h>
36 #endif
37 
38 #include "tst_test.h"
39 
40 #ifdef HAVE_SYS_XATTR_H
41 
42 #define SECURITY_KEY1	"security.ltptest1"
43 #define VALUE	"test"
44 #define VALUE_SIZE	(sizeof(VALUE) - 1)
45 #define KEY_SIZE    (sizeof(SECURITY_KEY1) - 1)
46 
47 static int fd;
48 
49 static int has_attribute(const char *list, int llen, const char *attr)
50 {
51 	int i;
52 
53 	for (i = 0; i < llen; i += strlen(list + i) + 1) {
54 		if (!strcmp(list + i, attr))
55 			return 1;
56 	}
57 	return 0;
58 }
59 
60 static void verify_flistxattr(void)
61 {
62 	char buf[64];
63 
64 	TEST(flistxattr(fd, buf, sizeof(buf)));
65 	if (TEST_RETURN == -1) {
66 		tst_res(TFAIL | TTERRNO, "flistxattr() failed");
67 		return;
68 	}
69 
70 	if (!has_attribute(buf, sizeof(buf), SECURITY_KEY1)) {
71 		tst_res(TFAIL, "missing attribute %s",
72 			 SECURITY_KEY1);
73 		return;
74 	}
75 
76 	tst_res(TPASS, "flistxattr() succeeded");
77 }
78 
79 static void setup(void)
80 {
81 	fd = SAFE_OPEN("testfile", O_RDWR | O_CREAT, 0644);
82 
83 	SAFE_FSETXATTR(fd, SECURITY_KEY1, VALUE, VALUE_SIZE, XATTR_CREATE);
84 }
85 
86 static void cleanup(void)
87 {
88 	if (fd > 0)
89 		SAFE_CLOSE(fd);
90 }
91 
92 static struct tst_test test = {
93 	.needs_tmpdir = 1,
94 	.needs_root = 1,
95 	.test_all = verify_flistxattr,
96 	.setup = setup,
97 	.cleanup = cleanup,
98 };
99 
100 #else
101 	TST_TEST_TCONF("<sys/xattr.h> does not exist.");
102 #endif /* HAVE_SYS_XATTR_H */
103