• 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_liteipc.h"
33 #include "sys/wait.h"
34 
35 #include "unistd.h"
36 #include "liteipc.h"
37 #include "stdlib.h"
38 #include "stdio.h"
39 #include "string.h"
40 #include "sys/time.h"
41 #include "sys/ioctl.h"
42 #include "fcntl.h"
43 
44 #include "smgr_demo.h"
45 
46 #define NEED_BREAK YES
47 
48 static int g_ipcFd;
49 char g_serviceName[] = "ohos.testservice";
50 
CallTestServiceLoop(uint32_t id)51 static int CallTestServiceLoop(uint32_t id)
52 {
53     IpcContent data1;
54     IpcMsg dataIn;
55     IpcMsg dataOut;
56     uint32_t ret;
57     void *retptr = nullptr;
58     uint32_t num = 0;
59     uint32_t *ptr = nullptr;
60     struct timeval test_time;
61     struct timeval test_time2;
62     unsigned int serviceHandle;
63 
64     printf("i am the client%d process, my process id is %d\n", id, getpid());
65     ret = GetService(g_ipcFd, g_serviceName, sizeof(g_serviceName), &serviceHandle);
66     ICUNIT_ASSERT_NOT_EQUAL(ret, 0, ret);
67     retptr = mmap(NULL, 4096, PROT_READ, MAP_PRIVATE, g_ipcFd, 0);
68     ICUNIT_ASSERT_NOT_EQUAL((int)(intptr_t)retptr, -1, retptr);
69     ret = GetService(g_ipcFd, g_serviceName, sizeof(g_serviceName), &serviceHandle);
70     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
71 
72     while (1) {
73         num++;
74 #if (NEED_BREAK == YES)
75         if (num > 50000) {
76             break;
77         }
78 #endif
79         data1.flag = SEND | RECV;
80         data1.outMsg = &dataOut;
81         memset(data1.outMsg, 0, sizeof(IpcMsg));
82         data1.outMsg->type = MT_REQUEST;
83         data1.outMsg->target.handle = serviceHandle;
84         data1.outMsg->dataSz = 4;
85         data1.outMsg->data = #
86         ret = ioctl(g_ipcFd, IPC_SEND_RECV_MSG, &data1);
87         if (ret != 0) {
88             printf("client%d CallTestServiceLoop ioctl ret:%d, errno:%d, num:%d\n", id, ret, errno, num);
89             ICUNIT_ASSERT_EQUAL(errno, ETIME, errno);
90             goto EXIT;
91         } else {
92             ptr = (uint32_t*)(data1.inMsg->data);
93             ICUNIT_ASSERT_EQUAL(ptr[0], 0, ptr[0]);
94             ICUNIT_ASSERT_EQUAL(ptr[1], 2 * num, ptr[1]);
95             FreeBuffer(g_ipcFd, data1.inMsg);
96         }
97     }
98     char tmpBuff[2048];
99     data1.flag = SEND | RECV;
100     data1.outMsg = &dataOut;
101     memset(data1.outMsg, 0, sizeof(IpcMsg));
102     data1.outMsg->type = MT_REQUEST;
103     data1.outMsg->target.handle = serviceHandle;
104     data1.outMsg->dataSz = 1024;
105     data1.outMsg->data = tmpBuff;
106     ret = ioctl(g_ipcFd, IPC_SEND_RECV_MSG, &data1);
107     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
108     FreeBuffer(g_ipcFd, data1.inMsg);
109     data1.outMsg->dataSz = 2048;
110     data1.outMsg->data = tmpBuff;
111     ret = ioctl(g_ipcFd, IPC_SEND_RECV_MSG, &data1);
112     ICUNIT_ASSERT_NOT_EQUAL(ret, 0, ret);
113     data1.outMsg->spObjNum = 300;
114     data1.outMsg->offsets = tmpBuff;
115     ret = ioctl(g_ipcFd, IPC_SEND_RECV_MSG, &data1);
116     ICUNIT_ASSERT_NOT_EQUAL(ret, 0, ret);
117 EXIT:
118     exit(0);
119     return 0;
120 }
121 
HandleRequest(IpcMsg * data)122 static void HandleRequest(IpcMsg *data)
123 {
124     uint32_t *ptr = (uint32_t*)data->data;
125     SendReply(g_ipcFd, data, 0, 2 * ptr[0]);
126 }
127 
TestServiceLoop(void)128 static int TestServiceLoop(void)
129 {
130     IpcContent data1;
131     IpcMsg dataIn;
132     IpcMsg dataOut;
133     int ret;
134     void *retptr = nullptr;
135     struct timeval last_time;
136     struct timeval test_time;
137     int cnt = 0;
138     unsigned int serviceHandle;
139 
140     printf("i am the test service process, my process id is %d\n", getpid());
141     ret = RegService(g_ipcFd, g_serviceName, sizeof(g_serviceName), &serviceHandle);
142     ICUNIT_ASSERT_NOT_EQUAL(ret, 0, ret);
143     retptr = mmap(NULL, 4096, PROT_READ, MAP_PRIVATE, g_ipcFd, 0);
144     ICUNIT_ASSERT_NOT_EQUAL((int)(intptr_t)retptr, -1, retptr);
145     ret = RegService(g_ipcFd, g_serviceName, sizeof(g_serviceName), &serviceHandle);
146     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
147 
148     gettimeofday(&last_time, 0);
149     while (1) {
150         cnt++;
151 #if (NEED_BREAK == YES)
152         if (cnt > 100000 - 10) {
153             printf("TestServiceLoop break!\n");
154             break;
155         }
156 #endif
157         data1.flag = RECV;
158         ret = ioctl(g_ipcFd, IPC_SEND_RECV_MSG, &data1);
159         if ((cnt % 1000000) == 0) {
160             gettimeofday(&test_time, 0);
161             printf("LiteIPC cnt:%d, time used:%d sec\n", cnt, test_time.tv_sec - last_time.tv_sec);
162         }
163         ICUNIT_ASSERT_EQUAL(ret, 0, ret);
164         switch (data1.inMsg->type) {
165             case MT_REQUEST:
166                 HandleRequest(data1.inMsg);
167                 break;
168             default:
169                 printf("request not support:%d!\n", data1.inMsg->type);
170                 FreeBuffer(g_ipcFd, data1.inMsg);
171                 break;
172         }
173     }
174     sleep(6);
175     exit(0);
176     return 0;
177 }
178 
LiteIpcTest(void)179 static int LiteIpcTest(void)
180 {
181 
182     int count = 0;
183     unsigned int ret;
184     void *retptr = nullptr;
185     pid_t farPid, sonPid, pid;
186     int status;
187 
188     retptr = mmap(NULL, 16 * 4096, PROT_READ, MAP_PRIVATE, g_ipcFd, 0);
189     ICUNIT_GOTO_NOT_EQUAL((int)(intptr_t)retptr, -1, retptr, EXIT1);
190 
191     pid = fork();
192     ICUNIT_GOTO_WITHIN_EQUAL(pid, 0, 100000, pid, EXIT1);
193     if (pid == 0) {
194         TestServiceLoop();
195         exit(-1);
196     }
197     sleep(1);//wait server start
198 
199     pid = fork();
200     ICUNIT_GOTO_WITHIN_EQUAL(pid, 0, 100000, pid, EXIT1);
201     if (pid == 0) {
202         CallTestServiceLoop(1);
203         exit(-1);
204     }
205 
206     pid = fork();
207     ICUNIT_GOTO_WITHIN_EQUAL(pid, 0, 100000, pid, EXIT1);
208     if (pid == 0) {
209         CallTestServiceLoop(2);
210         exit(-1);
211     }
212 
213     ret = waitpid(-1, &status, 0);
214     printf("waitpid1 ret:%d\n", ret);
215     status = WEXITSTATUS(status);
216     ICUNIT_GOTO_EQUAL(status, 0, status, EXIT2);
217 
218     ret = waitpid(-1, &status, 0);
219     printf("waitpid2 ret:%d\n", ret);
220     status = WEXITSTATUS(status);
221     ICUNIT_GOTO_EQUAL(status, 0, status, EXIT2);
222 
223     ret = waitpid(-1, &status, 0);
224     printf("waitpid3 ret:%d\n", ret);
225     status = WEXITSTATUS(status);
226     ICUNIT_GOTO_EQUAL(status, 0, status, EXIT2);
227 
228     return 0;
229 EXIT1:
230     return 1;
231 EXIT2:
232     printf("EXIT2 status:%d\n", status);
233     return status;
234 }
235 
TestCase(void)236 static int TestCase(void)
237 {
238     int ret;
239     int status;
240     g_ipcFd = open(LITEIPC_DRIVER, O_RDWR);
241     ICUNIT_ASSERT_NOT_EQUAL(g_ipcFd, -1, g_ipcFd);
242 
243     pid_t pid = fork();
244     ICUNIT_GOTO_WITHIN_EQUAL(pid, 0, 100000, pid, EXIT);
245     if (pid == 0) {
246         sleep(1);//wait cms start
247         ret = LiteIpcTest();
248         StopCms(g_ipcFd);
249         if (ret == 0) {
250             exit(0);
251         }
252         if (ret == 1) {
253             exit(-1);
254         }
255         exit(ret);
256     }
257 
258     StartCms(g_ipcFd);
259 
260     ret = waitpid(pid, &status, 0);
261     ICUNIT_GOTO_EQUAL(ret, pid, ret, EXIT);
262     status = WEXITSTATUS(status);
263     ICUNIT_GOTO_EQUAL(status, 0, status, EXIT);
264 
265     return 0;
266 EXIT:
267     return 1;
268 }
269 
ItPosixLiteIpc002(void)270 void ItPosixLiteIpc002(void)
271 {
272     TEST_ADD_CASE("ItPosixLiteIpc002", TestCase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
273 }
274 
275