• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
55     retval = pipe(pipeFd);
56     ICUNIT_GOTO_EQUAL(retval, 0, retval, OUT);
57 
58     /* Watch fd to see when it has input. */
59     FD_ZERO(&rfds);
60     FD_SET(pipeFd[0], &rfds);
61 
62     /* Wait up to three seconds. */
63     epFd = epoll_create1(100); /* 100, cretae input, */
64     ICUNIT_GOTO_NOT_EQUAL(epFd, -1, epFd, OUT);
65 
66     ev.events = EPOLLIN | EPOLLOUT | EPOLLRDNORM | EPOLLWRNORM;
67     retval = epoll_ctl(epFd, EPOLL_CTL_ADD, pipeFd[0], &ev);
68     ICUNIT_GOTO_NOT_EQUAL(retval, -1, retval, OUT);
69 
70     pid = fork();
71     if (pid == 0) {
72         close(pipeFd[1]);
73 
74         memset(evWait, 0, sizeof(struct epoll_event) * 2); /* 2, evs num */
75         retval = epoll_wait(epFd, evWait, 2, 3000); /* 2, num of wait fd. 3000, wait time */
76         close(pipeFd[0]);
77 
78         if (retval) {
79             exit(LOS_OK);
80         } else {
81             exit(LOS_NOK);
82         }
83     } else {
84         sleep(1);
85         close(pipeFd[0]);
86         retval = write(pipeFd[1], "0123456789012345678901234567890123456789", 40); /* write 40 bytes to stdin(fd 0) */
87         ICUNIT_GOTO_EQUAL(retval, 40, retval, OUT);
88         close(pipeFd[1]);
89 
90         wait(&status);
91         status = WEXITSTATUS(status);
92         ICUNIT_ASSERT_EQUAL(status, LOS_OK, status);
93     }
94 
95     return LOS_OK;
96 OUT:
97     close(pipeFd[0]);
98     close(pipeFd[1]);
99     close(epFd);
100     return LOS_NOK;
101 }
102 
103 
IO_TEST_EPOLL_001(VOID)104 VOID IO_TEST_EPOLL_001(VOID)
105 {
106     TEST_ADD_CASE(__FUNCTION__, testcase, TEST_LIB, TEST_LIBC, TEST_LEVEL1, TEST_FUNCTION);
107 }
108