• 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  * [Description]
9  *
10  * Check the basic functionality of the lgetxattr(2).
11  *
12  * In the case of a symbolic link, lgetxattr(2) only gets the value of the
13  * extended attribute related to the link itself by name.
14  */
15 
16 #include "config.h"
17 #include <errno.h>
18 #include <sys/types.h>
19 #include <string.h>
20 
21 #ifdef HAVE_SYS_XATTR_H
22 # include <sys/xattr.h>
23 #endif
24 
25 #include "tst_test.h"
26 
27 #ifdef HAVE_SYS_XATTR_H
28 
29 #define SECURITY_KEY1   "security.ltptest1"
30 #define SECURITY_KEY2   "security.ltptest2"
31 #define VALUE1   "test1"
32 #define VALUE2   "test2"
33 
set_xattr(char * path,char * key,void * value,size_t size)34 static void set_xattr(char *path, char *key, void *value, size_t size)
35 {
36 	int res;
37 
38 	res = lsetxattr(path, key, value, size, XATTR_CREATE);
39 	if (res == -1) {
40 		if (errno == ENOTSUP) {
41 			tst_brk(TCONF,
42 				"no xattr support in fs or mounted "
43 				"without user_xattr option");
44 		} else {
45 			tst_brk(TBROK | TERRNO, "lsetxattr(%s) failed", key);
46 		}
47 	}
48 }
49 
setup(void)50 static void setup(void)
51 {
52 	SAFE_TOUCH("testfile", 0644, NULL);
53 	SAFE_SYMLINK("testfile", "symlink");
54 
55 	set_xattr("testfile", SECURITY_KEY1, VALUE1, strlen(VALUE1));
56 	set_xattr("symlink", SECURITY_KEY2, VALUE2, strlen(VALUE2));
57 }
58 
verify_lgetxattr(void)59 static void verify_lgetxattr(void)
60 {
61 	int size = 64;
62 	char buf[size];
63 
64 	TEST(lgetxattr("symlink", SECURITY_KEY2, buf, size));
65 	if (TST_RET == -1) {
66 		tst_res(TFAIL | TTERRNO, "lgetxattr() failed");
67 		goto next;
68 	}
69 
70 	if (TST_RET != strlen(VALUE2)) {
71 		tst_res(TFAIL, "lgetxattr() got unexpected value size");
72 		goto next;
73 	}
74 
75 	if (!strncmp(buf, VALUE2, TST_RET))
76 		tst_res(TPASS, "lgetxattr() got expected value");
77 	else
78 		tst_res(TFAIL, "lgetxattr() got unexpected value");
79 
80 next:
81 	TEST(lgetxattr("symlink", SECURITY_KEY1, buf, size));
82 
83 	if (TST_RET != -1) {
84 		tst_res(TFAIL, "lgetxattr() succeeded unexpectedly");
85 		return;
86 	}
87 
88 	if (TST_ERR == ENODATA) {
89 		tst_res(TPASS | TTERRNO, "lgetxattr() failed as expected");
90 	} else {
91 		tst_res(TFAIL | TTERRNO, "lgetxattr() failed unexpectedly, expected %s",
92 			tst_strerrno(ENODATA));
93 	}
94 }
95 
96 static struct tst_test test = {
97 	.needs_tmpdir = 1,
98 	.needs_root = 1,
99 	.test_all = verify_lgetxattr,
100 	.setup = setup
101 };
102 
103 #else
104 	TST_TEST_TCONF("<sys/xattr.h> does not exist.");
105 #endif /* HAVE_SYS_XATTR_H */
106