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