• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2016 Fujitsu Ltd.
4 * Author: Jinbao Huang <huangjb.jy@cn.fujitsu.com>
5 */
6 
7 /*
8 * Test Name: lgetxattr01
9 *
10 * Description:
11 * The testcase checks the basic functionality of the lgetxattr(2).
12 * In the case of a symbolic link, we only get the value of the
13 * extended attribute related to the link itself by name.
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 SECURITY_KEY2   "security.ltptest2"
32 #define VALUE1   "test1"
33 #define VALUE2   "test2"
34 
set_xattr(char * path,char * key,void * value,size_t size)35 static void set_xattr(char *path, char *key, void *value, size_t size)
36 {
37 	int res;
38 
39 	res = lsetxattr(path, key, value, size, XATTR_CREATE);
40 	if (res == -1) {
41 		if (errno == ENOTSUP) {
42 			tst_brk(TCONF,
43 				"no xattr support in fs or mounted "
44 				"without user_xattr option");
45 		} else {
46 			tst_brk(TBROK | TERRNO, "lsetxattr(%s) failed", key);
47 		}
48 	}
49 }
50 
setup(void)51 static void setup(void)
52 {
53 	SAFE_TOUCH("testfile", 0644, NULL);
54 	SAFE_SYMLINK("testfile", "symlink");
55 
56 	set_xattr("testfile", SECURITY_KEY1, VALUE1, strlen(VALUE1));
57 	set_xattr("symlink", SECURITY_KEY2, VALUE2, strlen(VALUE2));
58 }
59 
verify_lgetxattr(void)60 static void verify_lgetxattr(void)
61 {
62 	int size = 64;
63 	char buf[size];
64 
65 	TEST(lgetxattr("symlink", SECURITY_KEY2, buf, size));
66 	if (TST_RET == -1) {
67 		tst_res(TFAIL | TTERRNO, "lgetxattr() failed");
68 		goto next;
69 	}
70 
71 	if (TST_RET != strlen(VALUE2)) {
72 		tst_res(TFAIL, "lgetxattr() got unexpected value size");
73 		goto next;
74 	}
75 
76 	if (!strncmp(buf, VALUE2, TST_RET))
77 		tst_res(TPASS, "lgetxattr() got expected value");
78 	else
79 		tst_res(TFAIL, "lgetxattr() got unexpected value");
80 
81 next:
82 	TEST(lgetxattr("symlink", SECURITY_KEY1, buf, size));
83 
84 	if (TST_RET != -1) {
85 		tst_res(TFAIL, "lgetxattr() succeeded unexpectedly");
86 		return;
87 	}
88 
89 	if (TST_ERR == ENODATA) {
90 		tst_res(TPASS | TTERRNO, "lgetxattr() failed as expected");
91 	} else {
92 		tst_res(TFAIL | TTERRNO, "lgetxattr() failed unexpectedly, expected %s",
93 			tst_strerrno(ENODATA));
94 	}
95 }
96 
97 static struct tst_test test = {
98 	.needs_tmpdir = 1,
99 	.needs_root = 1,
100 	.test_all = verify_lgetxattr,
101 	.setup = setup
102 };
103 
104 #else
105 	TST_TEST_TCONF("<sys/xattr.h> does not exist.");
106 #endif /* HAVE_SYS_XATTR_H */
107