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/eventfd.h>
18 #include "test.h"
19
20 /**
21 * @tc.name : eventfd_write_0100
22 * @tc.desc : Write the value through this function
23 * @tc.level : Level 0
24 */
eventfd_write_0100(void)25 void eventfd_write_0100(void)
26 {
27 eventfd_t value;
28 unsigned int initial_value = 0;
29 int fd = eventfd(initial_value, O_NONBLOCK);
30 if (fd == -1) {
31 t_error("%s eventfd failed\n", __func__);
32 }
33
34 int ret = eventfd_write(fd, 1);
35 if (ret != 0) {
36 t_error("%s eventfd_write failed\n", __func__);
37 }
38 ret = eventfd_write(fd, 1);
39 if (ret != 0) {
40 t_error("%s eventfd_write failed\n", __func__);
41 }
42 ret = eventfd_write(fd, 1);
43 if (ret != 0) {
44 t_error("%s eventfd_write failed\n", __func__);
45 }
46
47 ret = eventfd_read(fd, &value);
48 if (ret != 0) {
49 t_error("%s eventfd_read failed\n", __func__);
50 }
51 if (value != 3U) {
52 t_error("%s value is %d, not three\n", __func__);
53 }
54 close(fd);
55 }
56
57 /**
58 * @tc.name : eventfd_write_0200
59 * @tc.desc : When the file descriptor is invalid, test the return value of this function
60 * @tc.level : Level 2
61 */
eventfd_write_0200(void)62 void eventfd_write_0200(void)
63 {
64 int ret = eventfd_write(-1, 1);
65 if (ret != -1) {
66 t_error("%s eventfd_write should be failed\n", __func__);
67 }
68 }
69
70 /**
71 * @tc.name : eventfd_write_0300
72 * @tc.desc : When the eventfd_t value is invalid, test the return value of this function
73 * @tc.level : Level 2
74 */
eventfd_write_0300(void)75 void eventfd_write_0300(void)
76 {
77 unsigned int initial_value = 2;
78 int fd = eventfd(initial_value, O_NONBLOCK);
79 if (fd == -1) {
80 t_error("%s eventfd failed\n", __func__);
81 }
82
83 int result = eventfd_write(fd, -1);
84 if (result != -1) {
85 t_error("%s eventfd_write should be failed\n", __func__);
86 }
87
88 close(fd);
89 }
90
main(int argc,char * argv[])91 int main(int argc, char *argv[])
92 {
93 eventfd_write_0100();
94 eventfd_write_0200();
95 eventfd_write_0300();
96 return t_status;
97 }