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 <sys/xattr.h>
19 #include "functionalext.h"
20 
21 const char *path = "file.txt";
22 const char *name = "user.txt";
23 const char *value = "dat";
24 const int size = 3;
25 
26 /**
27  * @tc.name      : lgetxattr_0100
28  * @tc.desc      : Verify lgetxattr process success.
29  * @tc.level     : Level 0
30  */
lgetxattr_0100(void)31 void lgetxattr_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     system("chmod 777 file.txt");
47 
48     int ret = lsetxattr(path, name, value, strlen(value), XATTR_CREATE);
49     if (ret != 0) {
50         remove(path);
51         t_error("%s failed: ret = %d\n", __func__, ret);
52         return;
53     }
54 
55     char buf[BUFSIZ] = {0};
56     ret = lgetxattr(path, name, buf, sizeof(buf));
57     EXPECT_EQ("lgetxattr_0100", ret, size);
58     EXPECT_STREQ("lgetxattr_0100", buf, str);
59     remove(path);
60 }
61 
62 /**
63  * @tc.name      : lgetxattr_0200
64  * @tc.desc      : Verify lgetxattr process fail bacause param is null.
65  * @tc.level     : Level 2
66  */
lgetxattr_0200(void)67 void lgetxattr_0200(void)
68 {
69     int ret = lgetxattr(NULL, NULL, NULL, -1);
70     EXPECT_EQ("lgetxattr_0200", ret, -1);
71 }
72 
main(void)73 int main(void)
74 {
75     lgetxattr_0100();
76     lgetxattr_0200();
77     return t_status;
78 }
79