1 /*
2 * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without specific prior written
16 * permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "xts_ipc.h"
32
33 LITE_TEST_SUIT(IPC, IpcMqApi, IpcMqApiTestSuite);
34
IpcMqApiTestSuiteSetUp(void)35 static BOOL IpcMqApiTestSuiteSetUp(void)
36 {
37 return TRUE;
38 }
39
IpcMqApiTestSuiteTearDown(void)40 static BOOL IpcMqApiTestSuiteTearDown(void)
41 {
42 return TRUE;
43 }
44
45 /* *
46 * @tc.number SUB_KERNEL_IPC_MQ_0100
47 * @tc.name mq_send and mq_receive function test
48 * @tc.desc [C- SOFTWARE -0200]
49 */
50 LITE_TEST_CASE(IpcMqApiTestSuite, testMqOneLevelCom, Function | MediumTest | Level0)
51 {
52 int ret;
53 mqd_t queue;
54 unsigned int prio;
55 struct mq_attr getAttr = { 0 };
56 struct mq_attr setAttr = { 0 };
57 char qName[MQ_NAME_LEN];
58 char rMsg[MQ_RX_LEN];
59
60 ret = sprintf_s(qName, sizeof(qName), "testMqOneLevelCom_%d", GetRandom(10000)); /* 10000, common data for test, no special meaning */
61 ICUNIT_ASSERT_EQUAL(ret, strlen(qName), ret);
62 setAttr.mq_msgsize = MQ_MSG_SIZE;
63 setAttr.mq_maxmsg = MQ_MAX_MSG;
64 queue = mq_open(qName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &setAttr);
65 ICUNIT_ASSERT_NOT_EQUAL(queue, (mqd_t)-1, queue); /* -1, common data for test, no special meaning */
66 ret = mq_send(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO);
67 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
68
69 ret = mq_getattr(queue, &getAttr);
70 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
71 ICUNIT_ASSERT_EQUAL(getAttr.mq_msgsize, MQ_MSG_SIZE, getAttr.mq_msgsize);
72 ICUNIT_ASSERT_EQUAL(getAttr.mq_maxmsg, MQ_MAX_MSG, getAttr.mq_maxmsg);
73
74 ret = mq_receive(queue, rMsg, getAttr.mq_msgsize, &prio);
75 ICUNIT_ASSERT_NOT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */
76 ICUNIT_ASSERT_EQUAL(prio, 0, prio);
77
78 ret = strncmp(MQ_MSG, rMsg, MQ_MSG_LEN);
79 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
80 ret = mq_close(queue);
81 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
82 ret = mq_unlink(qName);
83 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
84 return 0;
85 }
86
87 /* *
88 * @tc.number SUB_KERNEL_IPC_MQ_0200
89 * @tc.name mq_timedsend and mq_timedreceive function test
90 * @tc.desc [C- SOFTWARE -0200]
91 */
92 LITE_TEST_CASE(IpcMqApiTestSuite, testMqTimedOneLevelCom, Function | MediumTest | Level1)
93 {
94 int ret;
95 mqd_t queue;
96 unsigned int prio;
97 struct timespec tts = { 0 };
98 struct timespec rts = { 0 };
99 struct mq_attr getAttr = { 0 };
100 struct mq_attr setAttr = { 0 };
101 char qName[MQ_NAME_LEN];
102 char rMsg[MQ_RX_LEN];
103
104 ret = sprintf_s(qName, sizeof(qName), "testMqTimedOneLevelCom_%d", GetRandom(10000)); /* 10000, common data for test, no special meaning */
105 ICUNIT_ASSERT_EQUAL(ret, strlen(qName), ret);
106 setAttr.mq_msgsize = MQ_MSG_SIZE;
107 setAttr.mq_maxmsg = MQ_MAX_MSG;
108 queue = mq_open(qName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &setAttr);
109 ICUNIT_ASSERT_NOT_EQUAL(queue, (mqd_t)-1, queue); /* -1, common data for test, no special meaning */
110
111 tts.tv_sec = time(NULL) + 1; /* 1, common data for test, no special meaning */
112 tts.tv_nsec = 0;
113 ret = mq_timedsend(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO, &tts);
114 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
115
116 ret = mq_getattr(queue, &getAttr);
117 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
118 ICUNIT_ASSERT_EQUAL(getAttr.mq_msgsize, MQ_MSG_SIZE, getAttr.mq_msgsize);
119 ICUNIT_ASSERT_EQUAL(getAttr.mq_maxmsg, MQ_MAX_MSG, getAttr.mq_maxmsg);
120
121 rts.tv_sec = time(NULL) + 1; /* 1, common data for test, no special meaning */
122 rts.tv_nsec = 0;
123 ret = mq_timedreceive(queue, rMsg, getAttr.mq_msgsize, &prio, &rts);
124 ICUNIT_ASSERT_NOT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */
125 ICUNIT_ASSERT_EQUAL(prio, 0, prio);
126 ret = strncmp(MQ_MSG, rMsg, MQ_MSG_LEN);
127 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
128 ret = mq_close(queue);
129 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
130 ret = mq_unlink(qName);
131 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
132 return 0;
133 }
134
135 /* *
136 * @tc.number SUB_KERNEL_IPC_MQ_0300
137 * @tc.name all send and all receive function test
138 * @tc.desc [C- SOFTWARE -0200]
139 */
140 LITE_TEST_CASE(IpcMqApiTestSuite, testMqAllOneLevelCom001, Function | MediumTest | Level2)
141 {
142 int ret;
143 mqd_t queue;
144 unsigned int prio;
145 struct mq_attr getAttr = { 0 };
146 struct mq_attr setAttr = { 0 };
147 char qName[MQ_NAME_LEN];
148 char rMsg[MQ_RX_LEN];
149
150 ret = sprintf_s(qName, sizeof(qName), "testMqAllOneLevelCom_%d", GetRandom(10000)); /* 10000, common data for test, no special meaning */
151 ICUNIT_ASSERT_EQUAL(ret, strlen(qName), ret);
152 memset_s(&getAttr, sizeof(getAttr), 0, sizeof(getAttr));
153 memset_s(&setAttr, sizeof(setAttr), 0, sizeof(setAttr));
154 setAttr.mq_msgsize = MQ_MSG_SIZE;
155 setAttr.mq_maxmsg = MQ_MAX_MSG;
156 queue = mq_open(qName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &setAttr);
157 ICUNIT_ASSERT_NOT_EQUAL(queue, (mqd_t)-1, queue); /* -1, common data for test, no special meaning */
158 ret = mq_getattr(queue, &setAttr);
159 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
160 setAttr.mq_msgsize = MQ_MSG_SIZE;
161 setAttr.mq_maxmsg = MQ_MAX_MSG;
162 ret = mq_setattr(queue, &setAttr, NULL);
163 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
164 ret = mq_send(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO);
165 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
166 ret = mq_getattr(queue, &getAttr);
167 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
168 ICUNIT_ASSERT_EQUAL(getAttr.mq_msgsize, setAttr.mq_msgsize, getAttr.mq_msgsize);
169 ICUNIT_ASSERT_EQUAL(getAttr.mq_maxmsg, setAttr.mq_maxmsg, getAttr.mq_maxmsg);
170 ICUNIT_ASSERT_EQUAL(getAttr.mq_curmsgs, 1, getAttr.mq_curmsgs); /* 1, common data for test, no special meaning */
171 ret = mq_receive(queue, rMsg, getAttr.mq_msgsize, &prio);
172 ICUNIT_ASSERT_NOT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */
173 ret = strncmp(MQ_MSG, rMsg, MQ_MSG_LEN);
174 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
175
176 ret = mq_close(queue);
177 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
178 ret = mq_unlink(qName);
179 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
180 return 0;
181 }
182
183 /* *
184 * @tc.number SUB_KERNEL_IPC_MQ_0300
185 * @tc.name all send and all receive function test
186 * @tc.desc [C- SOFTWARE -0200]
187 */
188 LITE_TEST_CASE(IpcMqApiTestSuite, testMqAllOneLevelCom002, Function | MediumTest | Level2)
189 {
190 int ret;
191 mqd_t queue;
192 unsigned int prio;
193 struct timespec tts = { 0 };
194 struct timespec rts = { 0 };
195 struct mq_attr getAttr = { 0 };
196 struct mq_attr setAttr = { 0 };
197 char qName[MQ_NAME_LEN];
198 char rMsg[MQ_RX_LEN];
199
200 ret = sprintf_s(qName, sizeof(qName), "testMqAllOneLevelCom_%d", GetRandom(10000)); /* 10000, common data for test, no special meaning */
201 ICUNIT_ASSERT_EQUAL(ret, strlen(qName), ret);
202 memset_s(&getAttr, sizeof(getAttr), 0, sizeof(getAttr));
203 memset_s(&setAttr, sizeof(setAttr), 0, sizeof(setAttr));
204 setAttr.mq_msgsize = MQ_MSG_SIZE;
205 setAttr.mq_maxmsg = MQ_MAX_MSG;
206 queue = mq_open(qName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &setAttr);
207
208 tts.tv_sec = time(NULL) + 1; /* 1, common data for test, no special meaning */
209 tts.tv_nsec = 0;
210 ret = mq_timedsend(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO, &tts);
211 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
212 ret = mq_getattr(queue, &getAttr);
213 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
214 ICUNIT_ASSERT_EQUAL(getAttr.mq_msgsize, setAttr.mq_msgsize, getAttr.mq_msgsize);
215 ICUNIT_ASSERT_EQUAL(getAttr.mq_maxmsg, setAttr.mq_maxmsg, getAttr.mq_maxmsg);
216 ICUNIT_ASSERT_EQUAL(getAttr.mq_curmsgs, 1, getAttr.mq_curmsgs); /* 1, common data for test, no special meaning */
217
218 rts.tv_sec = time(NULL) + 1; /* 1, common data for test, no special meaning */
219 rts.tv_nsec = 0;
220 ret = mq_timedreceive(queue, rMsg, getAttr.mq_msgsize, &prio, &rts);
221 ICUNIT_ASSERT_NOT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */
222 ret = strncmp(MQ_MSG, rMsg, MQ_MSG_LEN);
223 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
224
225 ret = mq_close(queue);
226 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
227 ret = mq_unlink(qName);
228 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
229 return 0;
230 }
231
PthreadCom(void * arg)232 static void *PthreadCom(void *arg)
233 {
234 int ret;
235 mqd_t queue;
236 unsigned int prio;
237 struct mq_attr getAttr = { 0 };
238 char rMsg[MQ_RX_LEN];
239
240 queue = (mqd_t)arg;
241 ret = mq_getattr(queue, &getAttr);
242 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
243 ret = mq_receive(queue, rMsg, getAttr.mq_msgsize, &prio);
244 ICUNIT_ASSERT_NOT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */
245 ICUNIT_ASSERT_EQUAL(prio, MQ_MSG_PRIO, prio);
246 ret = strncmp(MQ_MSG, rMsg, MQ_MSG_LEN);
247 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
248 ret = mq_send(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO);
249 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
250 return NULL;
251 }
252
253 /* *
254 * @tc.number SUB_KERNEL_IPC_MQ_0700
255 * @tc.name mq_send and mq_receive function test in thread and process
256 * @tc.desc [C- SOFTWARE -0200]
257 */
258 LITE_TEST_CASE(IpcMqApiTestSuite, testMqTwoThreadCom, Function | MediumTest | Level1)
259 {
260 int ret;
261 mqd_t queue;
262 pthread_t tid;
263 unsigned int prio;
264 struct mq_attr getAttr = { 0 };
265 struct mq_attr setAttr = { 0 };
266 char qName[MQ_NAME_LEN];
267 char rMsg[MQ_RX_LEN];
268
269 ret = sprintf_s(qName, sizeof(qName), "testMqTwoLevelCom_%d", GetRandom(10000)); /* 10000, common data for test, no special meaning */
270 ICUNIT_ASSERT_EQUAL(ret, strlen(qName), ret);
271 setAttr.mq_msgsize = MQ_MSG_SIZE;
272 setAttr.mq_maxmsg = MQ_MAX_MSG;
273 queue = mq_open(qName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &setAttr);
274 ICUNIT_ASSERT_NOT_EQUAL(queue, (mqd_t)-1, queue); /* -1, common data for test, no special meaning */
275
276 ret = pthread_create(&tid, NULL, PthreadCom, (void *)queue);
277 ICUNIT_ASSERT_NOT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */
278
279 ret = mq_send(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO);
280 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
281 ret = pthread_join(tid, NULL);
282 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
283
284 ret = mq_getattr(queue, &getAttr);
285 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
286 ICUNIT_ASSERT_EQUAL(getAttr.mq_curmsgs, 1, getAttr.mq_curmsgs); /* 1, common data for test, no special meaning */
287 ret = mq_receive(queue, rMsg, getAttr.mq_msgsize, &prio);
288 ICUNIT_ASSERT_NOT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */
289 ICUNIT_ASSERT_EQUAL(prio, MQ_MSG_PRIO, prio);
290 ret = strncmp(MQ_MSG, rMsg, MQ_MSG_LEN);
291 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
292
293 ret = mq_close(queue);
294 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
295 ret = mq_unlink(qName);
296 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
297 return 0;
298 }
299
PthreadTimedCom(void * arg)300 static void *PthreadTimedCom(void *arg)
301 {
302 int ret;
303 mqd_t queue;
304 unsigned int prio;
305 struct timespec tts = { 0 };
306 struct timespec rts = { 0 };
307 struct mq_attr getAttr = { 0 };
308 char rMsg[MQ_RX_LEN];
309
310 queue = (mqd_t)arg;
311 ret = mq_getattr(queue, &getAttr);
312 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
313 rts.tv_sec = time(NULL) + 1; /* 1, common data for test, no special meaning */
314 rts.tv_nsec = 0;
315 ret = mq_timedreceive(queue, rMsg, getAttr.mq_msgsize, &prio, &rts);
316 ICUNIT_ASSERT_NOT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */
317 ICUNIT_ASSERT_EQUAL(prio, MQ_MSG_PRIO, prio);
318 ret = strncmp(MQ_MSG, rMsg, MQ_MSG_LEN);
319 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
320
321 tts.tv_sec = time(NULL) + 1; /* 1, common data for test, no special meaning */
322 tts.tv_nsec = 0;
323 ret = mq_timedsend(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO, &tts);
324 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
325
326 return NULL;
327 }
328
329 /* *
330 * @tc.number SUB_KERNEL_IPC_MQ_0800
331 * @tc.name mq_timedsend and mq_timedreceive function test in thread and process
332 * @tc.desc [C- SOFTWARE -0200]
333 */
334 LITE_TEST_CASE(IpcMqApiTestSuite, testMqTimedTwoThreadCom, Function | MediumTest | Level1)
335 {
336 int ret;
337 mqd_t queue;
338 pthread_t tid;
339 unsigned int prio;
340 struct timespec tts = { 0 };
341 struct timespec rts = { 0 };
342 struct mq_attr getAttr = { 0 };
343 struct mq_attr setAttr = { 0 };
344 char qName[MQ_NAME_LEN];
345 char rMsg[MQ_RX_LEN];
346 ret = sprintf_s(qName, sizeof(qName), "testMqTimedTwoThreadCom_%d", GetRandom(10000)); /* 10000, common data for test, no special meaning */
347 ICUNIT_ASSERT_EQUAL(ret, strlen(qName), ret);
348 setAttr.mq_msgsize = MQ_MSG_SIZE;
349 setAttr.mq_maxmsg = MQ_MAX_MSG;
350 queue = mq_open(qName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &setAttr);
351 ICUNIT_ASSERT_NOT_EQUAL(queue, (mqd_t)-1, queue); /* -1, common data for test, no special meaning */
352
353 ret = pthread_create(&tid, NULL, PthreadTimedCom, (void *)queue);
354 ICUNIT_ASSERT_NOT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */
355
356 tts.tv_sec = time(NULL) + 1; /* 1, common data for test, no special meaning */
357 tts.tv_nsec = 0;
358 ret = mq_timedsend(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO, &tts);
359 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
360 ret = pthread_join(tid, NULL);
361 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
362
363 ret = mq_getattr(queue, &getAttr);
364 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
365 ICUNIT_ASSERT_EQUAL(getAttr.mq_curmsgs, 1, getAttr.mq_curmsgs); /* 1, common data for test, no special meaning */
366 rts.tv_sec = time(NULL) + 1; /* 1, common data for test, no special meaning */
367 rts.tv_nsec = 0;
368 ret = mq_timedreceive(queue, rMsg, getAttr.mq_msgsize, &prio, &rts);
369 ICUNIT_ASSERT_NOT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */
370 ICUNIT_ASSERT_EQUAL(prio, MQ_MSG_PRIO, prio);
371 ret = strncmp(MQ_MSG, rMsg, MQ_MSG_LEN);
372 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
373
374 ret = mq_close(queue);
375 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
376 ret = mq_unlink(qName);
377 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
378 return 0;
379 }
380
PthreadAllCom(void * arg)381 static void *PthreadAllCom(void *arg)
382 {
383 int ret;
384 mqd_t queue;
385 unsigned int prio;
386 struct timespec tts = { 0 };
387 struct timespec rts = { 0 };
388 struct mq_attr getAttr = { 0 };
389 char rMsg[MQ_RX_LEN];
390
391 queue = (mqd_t)arg;
392 ret = mq_getattr(queue, &getAttr);
393 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
394 rts.tv_sec = time(NULL) + 1; /* 1, common data for test, no special meaning */
395 rts.tv_nsec = 0;
396 ret = mq_timedreceive(queue, rMsg, getAttr.mq_msgsize, &prio, &rts);
397 ICUNIT_ASSERT_NOT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */
398 ICUNIT_ASSERT_EQUAL(prio, MQ_MSG_PRIO, prio);
399 ret = strncmp(MQ_MSG, rMsg, MQ_MSG_LEN);
400 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
401 tts.tv_sec = time(NULL) + 1; /* 1, common data for test, no special meaning */
402 tts.tv_nsec = 0;
403 ret = mq_timedsend(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO, &tts);
404 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
405
406 return NULL;
407 }
408
409 /* *
410 * @tc.number SUB_KERNEL_IPC_MQ_0900
411 * @tc.name all send and all receive function test in thread and process
412 * @tc.desc [C- SOFTWARE -0200]
413 */
414 LITE_TEST_CASE(IpcMqApiTestSuite, testMqAllTwoThreadCom, Function | MediumTest | Level1)
415 {
416 int ret;
417 mqd_t queue;
418 pthread_t tid;
419 unsigned int prio;
420 struct mq_attr getAttr = { 0 };
421 struct mq_attr setAttr = { 0 };
422 char qName[MQ_NAME_LEN];
423 char rMsg[MQ_RX_LEN];
424
425 ret = sprintf_s(qName, sizeof(qName), "testMqAllTwoThreadCom_%d", GetRandom(10000)); /* 10000, common data for test, no special meaning */
426 ICUNIT_ASSERT_EQUAL(ret, strlen(qName), ret);
427 setAttr.mq_msgsize = MQ_MSG_SIZE;
428 setAttr.mq_maxmsg = MQ_MAX_MSG;
429 queue = mq_open(qName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &setAttr);
430 ICUNIT_ASSERT_NOT_EQUAL(queue, (mqd_t)-1, queue); /* -1, common data for test, no special meaning */
431
432 ret = pthread_create(&tid, NULL, PthreadAllCom, (void *)queue);
433 ICUNIT_ASSERT_NOT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */
434
435 ret = mq_send(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO);
436 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
437 ret = pthread_join(tid, NULL);
438 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
439
440 ret = mq_getattr(queue, &getAttr);
441 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
442 ICUNIT_ASSERT_EQUAL(getAttr.mq_curmsgs, 1, getAttr.mq_curmsgs); /* 1, common data for test, no special meaning */
443 ret = mq_receive(queue, rMsg, getAttr.mq_msgsize, &prio);
444 ICUNIT_ASSERT_NOT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */
445 ICUNIT_ASSERT_EQUAL(prio, MQ_MSG_PRIO, prio);
446 ret = strncmp(MQ_MSG, rMsg, MQ_MSG_LEN);
447 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
448
449 ret = mq_close(queue);
450 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
451 ret = mq_unlink(qName);
452 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
453 return 0;
454 }
455
456 /* *
457 * @tc.number SUB_KERNEL_IPC_MQ_1200
458 * @tc.name mq_setattr set and clean mq_flags for O_NONBLOCK function test
459 * @tc.desc [C- SOFTWARE -0200]
460 */
461 LITE_TEST_CASE(IpcMqApiTestSuite, testMqSetGetAttr, Function | MediumTest | Level1)
462 {
463 int ret;
464 mqd_t queue;
465 struct mq_attr getAttr = { 0 };
466 struct mq_attr setAttr = { 0 };
467 char qName[MQ_NAME_LEN];
468
469 ret = sprintf_s(qName, sizeof(qName), "testMqFunction_%d", GetRandom(10000)); /* 10000, common data for test, no special meaning */
470 ICUNIT_ASSERT_EQUAL(ret, strlen(qName), ret);
471 memset_s(&setAttr, sizeof(setAttr), 0, sizeof(setAttr));
472 setAttr.mq_msgsize = MQ_MSG_SIZE;
473 setAttr.mq_maxmsg = MQ_MAX_MSG;
474 setAttr.mq_flags = O_NONBLOCK;
475 queue = mq_open(qName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &setAttr);
476 ICUNIT_ASSERT_NOT_EQUAL(queue, (mqd_t)-1, queue); /* -1, common data for test, no special meaning */
477
478 memset_s(&getAttr, sizeof(getAttr), 0, sizeof(getAttr));
479 ret = mq_getattr(queue, &getAttr);
480 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
481 ret = (getAttr.mq_flags & O_NONBLOCK);
482 ICUNIT_ASSERT_EQUAL(ret, O_NONBLOCK, ret);
483
484 setAttr.mq_flags &= ~O_NONBLOCK;
485 ret = mq_setattr(queue, &setAttr, NULL);
486 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
487
488 ret = mq_getattr(queue, &getAttr);
489 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
490 ret = (getAttr.mq_flags & O_NONBLOCK);
491 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
492 ret = mq_close(queue);
493 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
494 ret = mq_unlink(qName);
495 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
496 return 0;
497 }
498
499 RUN_TEST_SUITE(IpcMqApiTestSuite);
500
IpcMqFuncTest(void)501 void IpcMqFuncTest(void)
502 {
503 RUN_ONE_TESTCASE(testMqOneLevelCom);
504 RUN_ONE_TESTCASE(testMqTimedOneLevelCom);
505 RUN_ONE_TESTCASE(testMqAllOneLevelCom001);
506 RUN_ONE_TESTCASE(testMqAllOneLevelCom002);
507 RUN_ONE_TESTCASE(testMqTwoThreadCom);
508 RUN_ONE_TESTCASE(testMqTimedTwoThreadCom);
509 RUN_ONE_TESTCASE(testMqAllTwoThreadCom);
510 RUN_ONE_TESTCASE(testMqSetGetAttr);
511 }
512