• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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: listxattr01
9 *
10 * Description:
11 * The testcase checks the basic functionality of the listxattr(2).
12 * listxattr(2) retrieves the list of extended attribute names
13 * associated with the file itself in the filesystem.
14 *
15 */
16 
17 #include "config.h"
18 #include <errno.h>
19 #include <sys/types.h>
20 #include <string.h>
21 
22 #ifdef HAVE_SYS_XATTR_H
23 # include <sys/xattr.h>
24 #endif
25 
26 #include "tst_test.h"
27 
28 #ifdef HAVE_SYS_XATTR_H
29 
30 #define SECURITY_KEY1	"security.ltptest1"
31 #define VALUE	"test"
32 #define VALUE_SIZE	(sizeof(VALUE) - 1)
33 #define KEY_SIZE    (sizeof(SECURITY_KEY1) - 1)
34 #define TESTFILE    "testfile"
35 
has_attribute(const char * list,int llen,const char * attr)36 static int has_attribute(const char *list, int llen, const char *attr)
37 {
38 	int i;
39 
40 	for (i = 0; i < llen; i += strlen(list + i) + 1) {
41 		if (!strcmp(list + i, attr))
42 			return 1;
43 	}
44 	return 0;
45 }
46 
verify_listxattr(void)47 static void verify_listxattr(void)
48 {
49 	char buf[64];
50 
51 	TEST(listxattr(TESTFILE, buf, sizeof(buf)));
52 	if (TST_RET == -1) {
53 		tst_res(TFAIL | TTERRNO, "listxattr() failed");
54 		return;
55 	}
56 
57 	if (!has_attribute(buf, sizeof(buf), SECURITY_KEY1)) {
58 		tst_res(TFAIL, "missing attribute %s",
59 			 SECURITY_KEY1);
60 		return;
61 	}
62 
63 	tst_res(TPASS, "listxattr() succeeded");
64 }
65 
setup(void)66 static void setup(void)
67 {
68 	SAFE_TOUCH(TESTFILE, 0644, NULL);
69 
70 	SAFE_SETXATTR(TESTFILE, SECURITY_KEY1, VALUE, VALUE_SIZE, XATTR_CREATE);
71 }
72 
73 static struct tst_test test = {
74 	.needs_tmpdir = 1,
75 	.needs_root = 1,
76 	.test_all = verify_listxattr,
77 	.setup = setup,
78 };
79 
80 #else
81 	TST_TEST_TCONF("<sys/xattr.h> does not exist.");
82 #endif /* HAVE_SYS_XATTR_H */
83