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 <stdlib.h>
34 #include <sys/time.h>
35 #include <sys/types.h>
36 #include <unistd.h>
37 #include "sys/select.h"
38
SigPrint(int sig)39 static void SigPrint(int sig)
40 {
41 (void)sig;
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
54 int i = 0;
55 int status;
56
57 void (*retSig)(int);
58 sigset_t mask;
59
60 retSig = signal(SIGALRM, SigPrint);
61 ICUNIT_ASSERT_NOT_EQUAL(retSig, NULL, retSig);
62
63 retSig = signal(SIGUSR1, SigPrint);
64 ICUNIT_ASSERT_NOT_EQUAL(retSig, NULL, retSig);
65
66 retval = sigemptyset(&mask);
67 ICUNIT_ASSERT_EQUAL(retval, 0, retval);
68
69 retval = sigaddset(&mask, SIGALRM);
70 ICUNIT_ASSERT_EQUAL(retval, 0, retval);
71
72 retval = sigaddset(&mask, SIGUSR1);
73 ICUNIT_ASSERT_EQUAL(retval, 0, retval);
74
75 retval = pipe(pipeFd);
76 ICUNIT_GOTO_EQUAL(retval, 0, retval, OUT);
77
78 /* Watch fd to see when it has input. */
79 FD_ZERO(&rfds);
80 FD_SET(pipeFd[0], &rfds);
81
82 /* Wait up to three seconds. */
83 tv.tv_sec = 3; /* 3, wait timer, second */
84 tv.tv_nsec = 5; /* 5, wait timer, nano second */
85
86 pid = fork();
87 if (pid == 0) {
88 close(pipeFd[1]);
89
90 retval = pselect(pipeFd[0] + 1, &rfds, nullptr, nullptr, &tv, &mask);
91
92 close(pipeFd[0]);
93
94 if (retval != 0) {
95 exit(LOS_OK);
96 } else {
97 exit(LOS_NOK);
98 }
99 } else {
100 sleep(1);
101 close(pipeFd[0]);
102
103 retval = kill(pid, SIGALRM);
104 ICUNIT_ASSERT_EQUAL(retval, 0, retval);
105
106 retval = kill(pid, SIGUSR1);
107 ICUNIT_ASSERT_EQUAL(retval, 0, retval);
108 close(pipeFd[1]);
109
110 wait(&status);
111 status = WEXITSTATUS(status);
112 ICUNIT_ASSERT_EQUAL(status, LOS_OK, status);
113 }
114
115 return LOS_OK;
116 OUT:
117 return LOS_NOK;
118 }
119
IO_TEST_PSELECT_002(VOID)120 VOID IO_TEST_PSELECT_002(VOID)
121 {
122 TEST_ADD_CASE(__FUNCTION__, testcase, TEST_LIB, TEST_LIBC, TEST_LEVEL1, TEST_FUNCTION);
123 }
124
125