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
SigPrint(int sig)40 static void SigPrint(int sig)
41 {
42 return;
43 }
44
testcase(VOID)45 static UINT32 testcase(VOID)
46 {
47 fd_set rfds;
48 struct timespec tv;
49 int retval;
50 pid_t pid;
51 int pipeFd[2]; /* 2, pipe id num */
52 char buffer[40]; /* 40, buffer size */
53 int i = 0;
54 int status;
55
56 int epFd = 0;
57 sigset_t mask;
58 void (*retSig)(int);
59 struct epoll_event ev;
60 struct epoll_event evWait[2];
61
62 retSig = signal(SIGALRM, SigPrint);
63 ICUNIT_ASSERT_NOT_EQUAL(retSig, NULL, retSig);
64
65 retSig = signal(SIGUSR1, SigPrint);
66 ICUNIT_ASSERT_NOT_EQUAL(retSig, NULL, retSig);
67
68 retval = sigemptyset(&mask);
69 ICUNIT_ASSERT_EQUAL(retval, 0, retval);
70
71 retval = sigaddset(&mask, SIGALRM);
72 ICUNIT_ASSERT_EQUAL(retval, 0, retval);
73
74 retval = sigaddset(&mask, SIGUSR1);
75 ICUNIT_ASSERT_EQUAL(retval, 0, retval);
76
77 retval = pipe(pipeFd);
78 ICUNIT_GOTO_EQUAL(retval, 0, retval, OUT3);
79
80 /* Watch fd to see when it has input. */
81 FD_ZERO(&rfds);
82 FD_SET(pipeFd[0], &rfds);
83
84 /* Wait up to three seconds. */
85 tv.tv_sec = 3; /* 3, wait timer, second */
86 tv.tv_nsec = 5; /* 5, wait timer, nano second */
87
88 epFd = epoll_create1(100); /* 100, cretae input, */
89 ICUNIT_GOTO_NOT_EQUAL(epFd, -1, epFd, OUT2);
90
91 ev.events = EPOLLRDNORM;
92 retval = epoll_ctl(epFd, EPOLL_CTL_ADD, pipeFd[0], &ev);
93
94 ICUNIT_GOTO_NOT_EQUAL(retval, -1, retval, OUT1);
95
96 pid = fork();
97 if (pid == 0) {
98 close(pipeFd[1]);
99
100 (void)memset_s(evWait, sizeof(struct epoll_event) * 2, 0, sizeof(struct epoll_event) * 2); /* 2, evs num */
101 evWait[0].data.fd = pipeFd[0];
102
103 retval = epoll_pwait(epFd, evWait, 2, 3000, &mask); /* 2, num of wait fd. 3000, wait time */
104 close(pipeFd[0]);
105
106 if (retval) {
107 exit(LOS_OK);
108 } else {
109 exit(LOS_NOK);
110 }
111 } else {
112 sleep(1);
113 close(pipeFd[0]);
114
115 retval = kill(pid, SIGALRM);
116 ICUNIT_ASSERT_EQUAL(retval, 0, retval);
117
118 retval = kill(pid, SIGUSR1);
119 ICUNIT_ASSERT_EQUAL(retval, 0, retval);
120 close(pipeFd[1]);
121
122 wait(&status);
123 status = WEXITSTATUS(status);
124 ICUNIT_ASSERT_EQUAL(status, LOS_OK, status);
125 }
126
127 return LOS_OK;
128 OUT1:
129 close(epFd);
130 OUT2:
131 close(pipeFd[0]);
132 close(pipeFd[1]);
133 OUT3:
134 return LOS_NOK;
135 }
136
137
IO_TEST_EPOLL_002(VOID)138 VOID IO_TEST_EPOLL_002(VOID)
139 {
140 TEST_ADD_CASE(__FUNCTION__, testcase, TEST_LIB, TEST_LIBC, TEST_LEVEL1, TEST_FUNCTION);
141 }
142