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 <sys/xattr.h>
18 #include "functionalext.h"
19
20 const char *path = "file.txt";
21 const char *name = "user.txt";
22 const char *value = "dat";
23 const int size = 3;
24
25 /**
26 * @tc.name : getxattr_0100
27 * @tc.desc : Verify getxattr process success
28 * @tc.level : Level 0
29 */
getxattr_0100(void)30 void getxattr_0100(void)
31 {
32 int fd = open(path, O_RDWR | O_CREAT, TEST_MODE);
33 if (fd < 0) {
34 t_error("%s failed: fd = %d\n", __func__, fd);
35 return;
36 }
37
38 char str[] = "dat";
39 write(fd, str, sizeof(str));
40 close(fd);
41
42 int ret = setxattr(path, name, value, strlen(value), XATTR_CREATE);
43 if (ret != 0) {
44 t_error("%s failed: ret = %d\n", __func__, ret);
45 return;
46 }
47
48 char buf[BUFSIZ] = {0};
49
50 ret = getxattr(path, name, buf, sizeof(buf));
51 EXPECT_EQ("getxattr_0100", ret, size);
52 EXPECT_STREQ("getxattr_0100", buf, str);
53 ret = remove(path);
54 EXPECT_EQ("getxattr_0100", ret, 0);
55 }
56
57 /**
58 * @tc.name : getxattr_0200
59 * @tc.desc : Verify getxattr process fail bacause param is null
60 * @tc.level : Level 0
61 */
getxattr_0200(void)62 void getxattr_0200(void)
63 {
64 int ret = getxattr(NULL, NULL, NULL, -1);
65 EXPECT_EQ("getxattr_0200", ret, -1);
66 }
67
main(void)68 int main(void)
69 {
70 getxattr_0100();
71 getxattr_0200();
72 return t_status;
73 }
74