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 // Solve the build error of this test code when importing the header file <sys/epoll.h>
17 #ifndef _EPOLL_CTL_TEST
18 #define _EPOLL_CTL_TEST
19 #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
20 #undef _GNU_SOURCE
21 #undef _BSD_SOURCE
22 #endif
23 #endif
24
25 #include <errno.h>
26 #include <sys/epoll.h>
27 #include "test.h"
28
29 /**
30 * @tc.name : epoll_ctl_0100
31 * @tc.desc : Add entries in the interest list of the epoll instance referred to by the file descriptor.
32 * @tc.level : Level 0
33 */
epoll_ctl_0100(void)34 void epoll_ctl_0100(void)
35 {
36 errno = 0;
37 int fds[2];
38 const uint64_t expected = 0x123456789abcdef0;
39
40 int fd = epoll_create(1);
41 if (fd == -1) {
42 t_error("%s epoll_create failed\n", __func__);
43 }
44 if (errno != 0) {
45 t_error("%s errno is %d\n", __func__, errno);
46 }
47
48 int ret = pipe(fds);
49 if (ret == -1) {
50 t_error("%s pipe failed\n", __func__);
51 }
52
53 struct epoll_event ev;
54 ev.events = EPOLLIN;
55 ev.data.u64 = expected;
56
57 int result = epoll_ctl(fd, EPOLL_CTL_ADD, fds[0], &ev);
58 if (result == -1) {
59 t_error("%s epoll_ctl failed\n", __func__);
60 }
61
62 ret = write(fds[1], "\n", 1);
63 if (ret != 1) {
64 t_error("%s nothing in pipe\n", __func__);
65 }
66
67 struct epoll_event events[1];
68 result = epoll_wait(fd, events, 1, 1);
69 if (events[0].data.u64 != expected) {
70 t_error("%s events[0].data.u64 is not right\n", __func__);
71 }
72
73 close(fds[0]);
74 close(fds[1]);
75 }
76
main(int argc,char * argv[])77 int main(int argc, char *argv[])
78 {
79 epoll_ctl_0100();
80 return t_status;
81 }
82
83 #ifdef _EPOLL_CTL_TEST
84 #undef _EPOLL_CTL_TEST
85 #endif