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 <errno.h>
17 #include <fcntl.h>
18 #include <stdio.h>
19 #include <string.h>
20 #include <sys/xattr.h>
21
22 #include "test.h"
23
24 const char *path = "/data/tests/libc-test/src/file.txt";
25 const char *name = "user.foo";
26 const char *value = "bar";
27
28 /**
29 * @tc.name : setxattr_0100
30 * @tc.desc : set an extended attribute value
31 * @tc.level : Level 0
32 */
setxattr_0100(void)33 void setxattr_0100(void)
34 {
35 int fd = open(path, O_RDWR | O_CREAT);
36 if (fd < 0) {
37 t_error("%s failed: fd = %d\n", __func__, fd);
38 }
39
40 char str[] = "bar";
41 write(fd, str, sizeof(str));
42 close(fd);
43
44 errno = 0;
45 int result = setxattr(path, name, value, strlen(value), XATTR_CREATE);
46 if (result != 0) {
47 t_error("%s failed: result = %d\n", __func__, result);
48 }
49
50 if (errno != 0) {
51 t_error("%s failed: errno = %d\n", __func__, errno);
52 }
53
54 char buf[BUFSIZ] = {0};
55
56 errno = 0;
57 result = getxattr(path, name, buf, sizeof(buf));
58 if (result < 0) {
59 t_error("%s failed: result = %d\n", __func__, result);
60 }
61
62 if (errno != 0) {
63 t_error("%s failed: errno = %d\n", __func__, errno);
64 }
65
66 if (strcmp(buf, str)) {
67 t_error("%s failed: buf = %s\n", __func__, buf);
68 }
69
70 remove(path);
71 }
72
73 /**
74 * @tc.name : setxattr_0200
75 * @tc.desc : set an extended attribute value with invalid parameters
76 * @tc.level : Level 2
77 */
setxattr_0200(void)78 void setxattr_0200(void)
79 {
80 errno = 0;
81 int result = setxattr(NULL, NULL, NULL, -1, -1);
82 if (result == 0) {
83 t_error("%s failed: result = %d\n", __func__, result);
84 }
85
86 if (errno != EFAULT) {
87 t_error("%s failed: errno = %d\n", __func__, errno);
88 }
89 }
90
main(int argc,char * argv[])91 int main(int argc, char *argv[])
92 {
93 setxattr_0100();
94 setxattr_0200();
95
96 return t_status;
97 }
98