• 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 <stdlib.h>
34 #include <sys/time.h>
35 #include <sys/types.h>
36 #include <unistd.h>
37 #include "sys/select.h"
38 
Testcase1(VOID)39 static UINT32 Testcase1(VOID)
40 {
41     static const int TAR_STR_LEN = 12; /* 12, str len */
42     int pipeFd[2], ret; /* 2, pipe return 2 file descirpter */
43     fd_set reads;
44     ret = pipe(pipeFd);
45     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
46     ret = write(pipeFd[1], "Hello World", TAR_STR_LEN);
47     ICUNIT_GOTO_EQUAL(ret, TAR_STR_LEN, ret, EXIT);
48     FD_ZERO(&reads);
49     FD_SET(pipeFd[0], &reads);
50     ret = select(pipeFd[0] + 1, &reads, nullptr, nullptr, nullptr);
51     ICUNIT_GOTO_EQUAL(ret, 1, ret, EXIT);
52     ret = FD_ISSET(pipeFd[0], &reads);
53     ICUNIT_GOTO_NOT_EQUAL(ret, 0, ret, EXIT);
54     close(pipeFd[0]);
55     close(pipeFd[1]);
56     return LOS_OK;
57 EXIT:
58     close(pipeFd[0]);
59     close(pipeFd[1]);
60     return LOS_NOK;
61 }
62 
63 #define V_SIGMASK   0x5555
testcase(VOID)64 static UINT32 testcase(VOID)
65 {
66     fd_set rfds;
67     struct timespec tv;
68     int retval;
69     pid_t pid;
70     int pipeFd[2]; /* 2, pipe id num */
71     char buffer[40]; /* 40, buffer size */
72     int i = 0;
73     int status;
74 
75     sigset_t mask;
76 
77     retval = Testcase1(); /* first check select works */
78     ICUNIT_GOTO_EQUAL(retval, 0, retval, OUT);
79 
80     retval = pipe(pipeFd);
81     ICUNIT_GOTO_EQUAL(retval, 0, retval, OUT);
82 
83     /* Watch fd to see when it has input. */
84     FD_ZERO(&rfds);
85     FD_SET(pipeFd[0], &rfds);
86 
87     /* Wait up to three seconds. */
88     tv.tv_sec = 3; /* 3, wait timer, second */
89     tv.tv_nsec = 5; /* 5, wait timer, nano second */
90 
91     pid = fork();
92     if (pid == 0) {
93         close(pipeFd[1]);
94 
95         retval = pselect(pipeFd[0] + 1, &rfds, nullptr, nullptr, &tv, &mask);
96         close(pipeFd[0]);
97 
98         if (retval) {
99             exit(LOS_OK);
100         } else {
101             exit(LOS_NOK);
102         }
103     } else {
104         sleep(1);
105         close(pipeFd[0]);
106         retval = write(pipeFd[1], "0123456789012345678901234567890123456789", 40); /* write 40 bytes to stdin(fd 0) */
107         ICUNIT_GOTO_EQUAL(retval, 40, retval, OUT);
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_001(VOID)120 VOID IO_TEST_PSELECT_001(VOID)
121 {
122     TEST_ADD_CASE(__FUNCTION__, testcase, TEST_LIB, TEST_LIBC, TEST_LEVEL1, TEST_FUNCTION);
123 }
124