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 : removexattr_0100
29 * @tc.desc : remove an extended attribute
30 * @tc.level : Level 0
31 */
removexattr_0100(void)32 void removexattr_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 errno = 0;
72 result = removexattr(path, name);
73 if (result != 0) {
74 t_error("%s failed: result = %d\n", __func__, result);
75 }
76
77 errno = 0;
78 result = getxattr(path, name, buf, sizeof(buf));
79 if (result != -1) {
80 t_error("%s failed: result = %d\n", __func__, result);
81 }
82
83 if (errno != ENODATA) {
84 t_error("%s failed: errno = %d\n", __func__, errno);
85 }
86
87 remove(path);
88 }
89
90 /**
91 * @tc.name : removexattr_0200
92 * @tc.desc : remove an extended attribute with invalid parameters
93 * @tc.level : Level 2
94 */
removexattr_0200(void)95 void removexattr_0200(void)
96 {
97 errno = 0;
98 int result = removexattr(NULL, NULL);
99 if (result == 0) {
100 t_error("%s failed: result = %d\n", __func__, result);
101 }
102
103 if (errno != EFAULT) {
104 t_error("%s failed: errno = %d\n", __func__, errno);
105 }
106 }
107
main(int argc,char * argv[])108 int main(int argc, char *argv[])
109 {
110 removexattr_0100();
111 removexattr_0200();
112
113 return t_status;
114 }
115