1 /*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include "It_test_IO.h"
33 #include <sys/epoll.h>
34 #include "signal.h"
35 #include "pthread.h"
36 #include <sys/time.h>
37 #include <sys/types.h>
38 #include <unistd.h>
39
testcase(VOID)40 static UINT32 testcase(VOID)
41 {
42 fd_set rfds;
43 int retval;
44 pid_t pid;
45 int pipeFd[2]; /* 2, pipe id num */
46 char buffer[40]; /* 40, buffer size */
47 int i = 0;
48 int status;
49
50 int epFd;
51 sigset_t mask;
52 struct epoll_event ev;
53 struct epoll_event evWait[2]; /* 2, evs num */
54 UINT32 ret;
55
56 retval = pipe(pipeFd);
57 ICUNIT_GOTO_EQUAL(retval, 0, retval, OUT4);
58
59 /* Watch fd to see when it has input. */
60 FD_ZERO(&rfds);
61 FD_SET(pipeFd[0], &rfds);
62
63 /* Wait up to three seconds. */
64 epFd = epoll_create1(100); /* 100, cretae input, */
65 ICUNIT_GOTO_NOT_EQUAL(epFd, -1, epFd, OUT2);
66
67 ev.events = EPOLLIN | EPOLLOUT | EPOLLRDNORM | EPOLLWRNORM;
68 retval = epoll_ctl(epFd, EPOLL_CTL_ADD, pipeFd[0], &ev);
69 ICUNIT_GOTO_NOT_EQUAL(retval, -1, retval, OUT1);
70
71 pid = fork();
72 if (pid == 0) {
73 close(pipeFd[1]);
74
75 ret = memset_s(evWait, sizeof(struct epoll_event) * 2, 0, sizeof(struct epoll_event) * 2); /* 2, evs num */
76 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
77 retval = epoll_wait(epFd, evWait, 2, 3000); /* 2, num of wait fd. 3000, wait time */
78 close(pipeFd[0]);
79
80 if (retval) {
81 exit(LOS_OK);
82 } else {
83 exit(LOS_NOK);
84 }
85 } else {
86 sleep(1);
87 close(pipeFd[0]);
88 retval = write(pipeFd[1], "0123456789012345678901234567890123456789", 40); /* write 40 bytes to stdin(fd 0) */
89 ICUNIT_GOTO_EQUAL(retval, 40, retval, OUT3);
90 close(pipeFd[1]);
91
92 wait(&status);
93 status = WEXITSTATUS(status);
94 ICUNIT_ASSERT_EQUAL(status, LOS_OK, status);
95 }
96
97 return LOS_OK;
98 OUT1:
99 close(epFd);
100 close(pipeFd[0]);
101 close(pipeFd[1]);
102 return LOS_NOK;
103 OUT2:
104 close(pipeFd[0]);
105 close(pipeFd[1]);
106 return LOS_NOK;
107 OUT3:
108 close(epFd);
109 close(pipeFd[1]);
110 return LOS_NOK;
111 OUT4:
112 return LOS_NOK;
113 }
114
115
IO_TEST_EPOLL_001(VOID)116 VOID IO_TEST_EPOLL_001(VOID)
117 {
118 TEST_ADD_CASE(__FUNCTION__, testcase, TEST_LIB, TEST_LIBC, TEST_LEVEL1, TEST_FUNCTION);
119 }
120