1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <fcntl.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <sys/xattr.h>
20 #include "functionalext.h"
21
22 const char *path = "file.txt";
23 const char *name = "user.txt";
24 const char *value = "dat";
25
26 /**
27 * @tc.name : listxattr_0100
28 * @tc.desc : Verify listxattr process success.
29 * @tc.level : Level 0
30 */
listxattr_0100(void)31 void listxattr_0100(void)
32 {
33 if (access(path, F_OK) == 0) {
34 remove(path);
35 }
36 mode_t perms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
37 int fd = open(path, O_RDWR | O_CREAT, perms);
38 if (fd == -1) {
39 t_error("%s failed: fd = %d\n", __func__, fd);
40 return;
41 }
42
43 char str[] = "dat";
44 write(fd, str, sizeof(str));
45 close(fd);
46
47 system("chmod 777 file.txt");
48 int ret = setxattr(path, name, value, strlen(value), XATTR_CREATE);
49 if (ret != 0) {
50 t_error("%s failed: ret = %d\n", __func__, ret);
51 remove(path);
52 return;
53 }
54
55 char list[BUFSIZ] = {0};
56 ret = listxattr(path, list, sizeof(list));
57 EXPECT_TRUE("listxattr_0100", ret > 0);
58 if (strcasecmp(list, name)) {
59 if (strcasecmp(list, "security.selinux")) {
60 t_error("%s failed: list error\n", __func__);
61 }
62 }
63 ret = remove(path);
64 EXPECT_EQ("listxattr_0100", ret, 0);
65 }
66
67 /**
68 * @tc.name : listxattr_0200
69 * @tc.desc : Verify listxattr process fail bacause param is null.
70 * @tc.level : Level 2
71 */
listxattr_0200(void)72 void listxattr_0200(void)
73 {
74 int ret = listxattr(NULL, NULL, -1);
75 EXPECT_EQ("listxattr_0200", ret, -1);
76 }
77
main(void)78 int main(void)
79 {
80 listxattr_0100();
81 listxattr_0200();
82 return t_status;
83 }
84