• 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 #include "It_test_IO.h"
32 
33 int g_pipeFd[10][2]; // 2, read and write; 10, listen fd number.
34 static pthread_t g_tid = -1;
35 static const int LISTEN_FD_NUM = 10;
36 
Pthread01(void * arg)37 static void *Pthread01(void *arg)
38 {
39     int totalNum = 0;
40     int times = 3; // 3, loop number for test.
41     int i, ret;
42     struct pollfd fds[LISTEN_FD_NUM] = {0};
43     char buffer[20]; // 20, enough space for test.
44     const int pollEvents = 1;
45 
46     for (i = 0; i < LISTEN_FD_NUM; i++) {
47         fds[i].fd = g_pipeFd[i][0];
48         fds[i].events = pollEvents;
49     }
50 
51     while (times--) {
52         ret = poll(fds, LISTEN_FD_NUM, 1000); // 1000, wait time.
53         totalNum += ((ret > 0) ? ret : 0);
54 
55         if (ret <= 0) {
56             continue;
57         }
58 
59         for (i = 0; i < LISTEN_FD_NUM; i++) {
60             if (fds[i].revents & pollEvents) {
61                 ret = read(fds[i].fd, buffer, 12); // 12, "hello world" length and '\0'
62                 ICUNIT_GOTO_EQUAL(ret, 12, ret, EXIT); // 12, "hello world" length and '\0'
63                 ret = strcmp(buffer, "hello world");
64                 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
65             }
66         }
67 
68         if (totalNum == LISTEN_FD_NUM) {
69             break;
70         }
71     }
72 
73     ICUNIT_GOTO_EQUAL(totalNum, LISTEN_FD_NUM, -1, EXIT);
74 
75 EXIT:
76     return NULL;
77 }
78 
Testcase(VOID)79 static UINT32 Testcase(VOID)
80 {
81     int i;
82     int ret;
83 
84     for (i = 0; i < LISTEN_FD_NUM; i++) {
85         ret = pipe(g_pipeFd[i]);
86         ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
87     }
88 
89     ret = pthread_create(&g_tid, NULL, Pthread01, NULL);
90     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
91 
92     for (i = 0; i < LISTEN_FD_NUM; i++) {
93         ret = write(g_pipeFd[i][1], "hello world", 12); // 12, "hello world" length and '\0'
94         ICUNIT_GOTO_EQUAL(ret, 12, ret, EXIT);          // 12, "hello world" length and '\0'
95     }
96 
97     pthread_join(g_tid, NULL);
98 
99     for (i = 0; i < LISTEN_FD_NUM; i++) {
100         close(g_pipeFd[i][0]);
101         close(g_pipeFd[i][1]);
102     }
103 
104     return LOS_OK;
105 
106 EXIT:
107     for (i = 0; i < LISTEN_FD_NUM; i++) {
108         close(g_pipeFd[i][0]);
109         close(g_pipeFd[i][1]);
110     }
111     return LOS_NOK;
112 }
113 
ItStdlibPoll002(void)114 VOID ItStdlibPoll002(void)
115 {
116     TEST_ADD_CASE(__FUNCTION__, Testcase, TEST_LIB, TEST_LIBC, TEST_LEVEL1, TEST_FUNCTION);
117 }
118