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 "pthread.h"
34 #include "signal.h"
35
36 const int BUF_SIZE = 128;
37 const int DELAY_TIME = 200;
38
39 static INT32 pipeFdPpoll[2];
40 static INT32 g_step = 1;
41 static CHAR strBuf[] = "hello world.";
42 static struct pollfd pfd;
43 static sigset_t sigMask;
44 static UINT32 count = 0;
45
signalHandle(INT32 sigNum)46 static void signalHandle(INT32 sigNum)
47 {
48 g_step++;
49 return;
50 }
51
pthread_01(void * arg)52 static void *pthread_01(void *arg)
53 {
54 INT32 retVal;
55 CHAR buf[BUF_SIZE];
56
57 (void)signal(SIGUSR1, signalHandle);
58
59 while (1) {
60 /* 执行ppoll监视文件描述符 */
61 while (g_step < 2) { /* 2, 2nd step */
62 usleep(DELAY_TIME);
63 }
64 g_step++;
65 retVal = ppoll(&pfd, 1, NULL, &sigMask);
66
67 ICUNIT_ASSERT_NOT_EQUAL_NULL(retVal, -1, retVal);
68
69 /* 判断revents */
70 if (pfd.revents & POLLIN) {
71 (void)memset_s(buf, sizeof(buf), 0, sizeof(buf));
72 retVal = read(pfd.fd, buf, BUF_SIZE);
73 ICUNIT_ASSERT_NOT_EQUAL_NULL(retVal, -1, retVal);
74
75 retVal = strcmp(strBuf, buf);
76 ICUNIT_ASSERT_EQUAL_NULL(retVal, 0, retVal);
77
78 count++;
79 } else {
80 ICUNIT_ASSERT_NOT_EQUAL_NULL(pfd.revents & POLLIN, 0, pfd.revents & POLLIN);
81 }
82 g_step++;
83
84 if (g_step >= 7) { /* 7, 7th step */
85 ICUNIT_ASSERT_EQUAL_NULL(count, 2, count); /* 2, 2nd step */
86 pthread_exit(NULL);
87 }
88 }
89
90 return LOS_OK;
91 }
92
testcase(VOID)93 static UINT32 testcase(VOID)
94 {
95 INT32 retVal;
96 pthread_t tid;
97
98 /* 建立管道 */
99 while (g_step < 1) {
100 usleep(DELAY_TIME);
101 }
102 retVal = pipe(pipeFdPpoll);
103 ICUNIT_ASSERT_NOT_EQUAL(retVal, -1, retVal);
104
105 /* 设置pfd sigmask */
106 pfd.fd = pipeFdPpoll[0];
107 pfd.events = POLLIN;
108 pfd.revents = 0x0;
109
110 sigemptyset(&sigMask);
111 sigaddset(&sigMask, SIGUSR1);
112
113 /* 开辟线程执行 ppoll */
114 retVal = pthread_create(&tid, NULL, pthread_01, NULL);
115 ICUNIT_ASSERT_EQUAL(retVal, 0, retVal);
116 g_step++;
117
118 /* 向管道写入数据 */
119 while (g_step < 3) { /* 3, 3ed step */
120 usleep(DELAY_TIME);
121 }
122 sleep(1); /* 保证先挂起再写入数据 */
123 retVal = write(pipeFdPpoll[1], "hello world.", sizeof(strBuf));
124 ICUNIT_ASSERT_NOT_EQUAL(retVal, -1, retVal);
125
126 /* 向线程发送信号 */
127 while (g_step < 5) { /* 5, 5th step */
128 usleep(DELAY_TIME);
129 }
130 sleep(1); /* 保证先挂起再发送信号 */
131 retVal = pthread_kill(tid, SIGUSR1);
132 ICUNIT_ASSERT_EQUAL(retVal, 0, retVal);
133
134 /* 继续向管道写入数据 */
135 ICUNIT_ASSERT_EQUAL(g_step, 5, g_step); /* 5, sth。判断挂起解除之前信号没有被处理 */
136 retVal = write(pipeFdPpoll[1], "hello world.", sizeof(strBuf));
137 ICUNIT_ASSERT_NOT_EQUAL(retVal, -1, retVal);
138
139 while (g_step < 7) { /* 7, 7th step */
140 usleep(DELAY_TIME);
141 }
142 ICUNIT_ASSERT_EQUAL(count, 2, count); /* 2, 2nd step */
143 /* 等待退出 */
144 pthread_join(tid, NULL);
145
146 return LOS_OK;
147 }
148
IO_TEST_PPOLL_003(VOID)149 VOID IO_TEST_PPOLL_003(VOID)
150 {
151 TEST_ADD_CASE(__FUNCTION__, testcase, TEST_LIB, TEST_LIBC, TEST_LEVEL1, TEST_FUNCTION);
152 }