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_read_0100
22  * @tc.desc      : Read the value of event through the file descriptor
23  * @tc.level     : Level 0
24  */
eventfd_read_0100(void)25 void eventfd_read_0100(void)
26 {
27     unsigned int initial_value = 2;
28     int fd = eventfd(initial_value, O_NONBLOCK);
29     if (fd == -1) {
30         t_error("%s eventfd failed\n", __func__);
31     }
32 
33     eventfd_t value = 123;
34     int result = eventfd_read(fd, &value);
35     if (result != 0) {
36         t_error("%s eventfd_read failed\n", __func__);
37     }
38 
39     if (value != initial_value) {
40         t_error("%s eventfd_read value invalid\n", __func__);
41     }
42 
43     close(fd);
44 }
45 
46 /**
47  * @tc.name      : eventfd_read_0200
48  * @tc.desc      : When the file descriptor is invalid, test the return value of this function
49  * @tc.level     : Level 2
50  */
eventfd_read_0200(void)51 void eventfd_read_0200(void)
52 {
53     eventfd_t value = 123;
54     int result = eventfd_read(-1, &value);
55     if (result != -1) {
56         t_error("%s eventfd_read should be failed\n", __func__);
57     }
58 }
59 
60 /**
61  * @tc.name      : eventfd_read_0300
62  * @tc.desc      : When the eventfd_t value is invalid, test the return value of this function
63  * @tc.level     : Level 2
64  */
eventfd_read_0300(void)65 void eventfd_read_0300(void)
66 {
67     unsigned int initial_value = 2;
68     int fd = eventfd(initial_value, O_NONBLOCK);
69     if (fd == -1) {
70         t_error("%s eventfd failed\n", __func__);
71     }
72 
73     int result = eventfd_read(fd, NULL);
74     if (result != -1) {
75         t_error("%s eventfd_read should be failed\n", __func__);
76     }
77 
78     close(fd);
79 }
80 
main(int argc, char *argv[])81 int main(int argc, char *argv[])
82 {
83     eventfd_read_0100();
84     eventfd_read_0200();
85     eventfd_read_0300();
86     return t_status;
87 }