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