• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2022 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 <securec.h>
32 #include "osTest.h"
33 #include "cmsis_os.h"
34 
35 #define TEST_STR(func) ItLos##func
36 #define TEST_TO_STR(x) #x
37 #define TEST_HEAD_TO_STR(x) TEST_TO_STR(x)
38 #define ADD_TEST_CASE(func) \
39     TEST_ADD_CASE(TEST_HEAD_TO_STR(TEST_STR(func)), func, TEST_LOS, TEST_TASK, TEST_LEVEL0, TEST_FUNCTION)
40 
41 #define Function   0
42 #define MediumTest 0
43 #define Level1     0
44 #define LITE_TEST_CASE(module, function, flag) static int function(void)
45 
46 #define STATCI_BUFF_SIZE   32
47 #define READ_BUFFER_SIZIE  7
48 #define QUEUE_WAIT_TIMEOUT 3
49 
CmsisStackFunc01(void)50 static VOID CmsisStackFunc01(void)
51 {
52     g_testCount++;
53     return;
54 }
55 
56 /**
57  * @tc.number    : SUB_KERNEL_PTHREAD_OPERATION_001
58  * @tc.name      : event operation for join
59  * @tc.desc      : [C- SOFTWARE -0200]
60  */
61 LITE_TEST_CASE(CmsisFuncTestSuite, TestCmsis001, Function | MediumTest | Level1)
62 {
63     osThreadId_t threadId;
64     osThreadAttr_t attr = {0};
65 
66     g_testCount = 0;
67 
68     void *stackAddr = malloc(OS_TSK_TEST_STACK_SIZE);
69     ICUNIT_ASSERT_NOT_EQUAL(stackAddr, NULL, stackAddr);
70 
71     attr.stack_mem = stackAddr;
72     attr.stack_size = OS_TSK_TEST_STACK_SIZE;
73     attr.priority = osPriorityNormal + 1;
74     attr.attr_bits = osThreadDetached;
75     threadId = osThreadNew((osThreadFunc_t)CmsisStackFunc01, NULL, &attr);
76     ICUNIT_GOTO_NOT_EQUAL(threadId, 0, threadId, EXIT);
77 
78     ICUNIT_GOTO_EQUAL(g_testCount, 1, g_testCount, EXIT);
79 EXIT:
80     free(stackAddr);
81     return LOS_OK;
82 };
83 
84 /**
85  * @tc.name: TestCmsis007
86  * @tc.desc: set and get queue name
87  * @tc.type: FUNC
88  * @tc.require: issueI5LBE8
89  */
90 LITE_TEST_CASE(CmsisFuncTestSuite, TestCmsis007, Function | MediumTest | Level1)
91 {
92     osMessageQueueId_t msgQueueId;
93     osMessageQueueAttr_t attr = {0};
94     CHAR staticBuff[STATCI_BUFF_SIZE] = {0};
95     CHAR strbuff[] = "hello world";
96     CHAR *name = NULL;
97     INT32 ret;
98 
99     attr.name = "q1";
100     /* dynamic test */
101     msgQueueId = osMessageQueueNew(1, strlen(strbuff), &attr);
102     ICUNIT_ASSERT_NOT_EQUAL(msgQueueId, NULL, msgQueueId);
103 
104     name = (CHAR *)osMessageQueueGetName(msgQueueId);
105     ret = strcmp(name, "q1");
106     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
107 
108     ret = osMessageQueueDelete(msgQueueId);
109     ICUNIT_ASSERT_EQUAL(ret, osOK, ret);
110 
111     name = (CHAR *)osMessageQueueGetName(msgQueueId);
112     ICUNIT_ASSERT_EQUAL(name, NULL, name);
113 
114 #if (LOSCFG_BASE_IPC_QUEUE_STATIC == 1)
115     attr.mq_mem = staticBuff;
116     attr.mq_size = STATCI_BUFF_SIZE;
117     msgQueueId = osMessageQueueNew(1, STATCI_BUFF_SIZE, &attr);
118     ICUNIT_ASSERT_NOT_EQUAL(msgQueueId, NULL, msgQueueId);
119 
120     name = osMessageQueueGetName(msgQueueId);
121     ret = strcmp(name, "q1");
122     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
123 
124     ret = osMessageQueueDelete(msgQueueId);
125     ICUNIT_ASSERT_EQUAL(ret, osOK, ret);
126 
127     name = osMessageQueueGetName(msgQueueId);
128     ICUNIT_ASSERT_EQUAL(name, NULL, name);
129 #endif
130 
131     return LOS_OK;
132 
133 EXIT:
134     ret = osMessageQueueDelete(msgQueueId);
135     ICUNIT_ASSERT_EQUAL(ret, osOK, ret);
136 
137     return LOS_OK;
138 }
139 
140 #if (LOSCFG_BASE_IPC_QUEUE_STATIC == 1)
141 static osMessageQueueId_t g_msgQueueId1;
142 static osMessageQueueId_t g_msgQueueId2;
143 
144 static osThreadId_t threadId1;
145 static osThreadId_t threadId2;
146 
147 static CHAR g_strbuff1[] = "hello";
148 static CHAR g_strbuff2[] = "world";
149 static CHAR g_staticBuff[STATCI_BUFF_SIZE] = {0};
150 
CmsisQueueTestThread1(VOID)151 static VOID CmsisQueueTestThread1(VOID)
152 {
153     CHAR data[READ_BUFFER_SIZIE] = {0};
154     INT32 ret;
155     osStatus_t status;
156 
157     ret = osMessageQueuePut(g_msgQueueId1, &g_strbuff1, 0U, 0U);
158     ICUNIT_ASSERT_EQUAL(ret, osOK, ret);
159 
160     status = osMessageQueueGet(g_msgQueueId2, &data, NULL, QUEUE_WAIT_TIMEOUT);
161     ICUNIT_ASSERT_EQUAL(status, osOK, status);
162 
163     ret = strcmp(data, "world");
164     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
165 }
166 
CmsisQueueTestThread2(VOID)167 static VOID CmsisQueueTestThread2(VOID)
168 {
169     CHAR data[READ_BUFFER_SIZIE] = {0};
170     INT32 ret;
171     osStatus_t status;
172 
173     status = osMessageQueueGet(g_msgQueueId1, &data, NULL, QUEUE_WAIT_TIMEOUT);
174     ICUNIT_ASSERT_EQUAL(status, osOK, status);
175 
176     ret = strcmp(data, "hello");
177     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
178 
179     ret = osMessageQueuePut(g_msgQueueId2, &g_strbuff2, 0U, 0U);
180     ICUNIT_ASSERT_EQUAL(ret, osOK, ret);
181 }
182 
ThreadReadWriteTest(VOID)183 static INT32 ThreadReadWriteTest(VOID)
184 {
185     osMessageQueueAttr_t attr = {0};
186     INT32 ret;
187 
188     g_msgQueueId1 = osMessageQueueNew(1, strlen(g_strbuff1), NULL);
189     ICUNIT_ASSERT_NOT_EQUAL(g_msgQueueId1, NULL, g_msgQueueId1);
190 
191     attr.mq_mem = g_staticBuff;
192     attr.mq_size = strlen(g_strbuff2) + 1;
193     g_msgQueueId2 = osMessageQueueNew(1, strlen(g_strbuff2), &attr);
194     ICUNIT_ASSERT_NOT_EQUAL(g_msgQueueId2, NULL, g_msgQueueId2);
195 
196     threadId1 = osThreadNew(CmsisQueueTestThread1, NULL, NULL);
197     ICUNIT_ASSERT_NOT_EQUAL(threadId1, NULL, threadId1);
198 
199     threadId2 = osThreadNew(CmsisQueueTestThread2, NULL, NULL);
200     ICUNIT_ASSERT_NOT_EQUAL(threadId2, NULL, threadId2);
201 
202     osThreadJoin(threadId1);
203 
204     ret = strcmp(g_staticBuff, "world");
205     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
206 
207     ret = osMessageQueueDelete(g_msgQueueId1);
208     ICUNIT_ASSERT_EQUAL(ret, osOK, ret);
209 
210     ret = osMessageQueueDelete(g_msgQueueId2);
211     ICUNIT_ASSERT_EQUAL(ret, osOK, ret);
212 
213     return 0;
214 }
215 
216 /**
217  * @tc.name: TestCmsis006
218  * @tc.desc: mix read write
219  * @tc.type: FUNC
220  * @tc.require: issueI5LBE8
221  */
222 LITE_TEST_CASE(CmsisFuncTestSuite, TestCmsis006, Function | MediumTest | Level1)
223 {
224     INT32 ret;
225 
226     ret = ThreadReadWriteTest();
227     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
228 
229     return LOS_OK;
230 }
231 #endif
232 
233 /**
234  * @tc.name: TestCmsis005
235  * @tc.desc: read-write exception
236  * @tc.type: FUNC
237  * @tc.require: issueI5LBE8
238  */
239 LITE_TEST_CASE(CmsisFuncTestSuite, TestCmsis005, Function | MediumTest | Level1)
240 {
241     osMessageQueueId_t msgQueueId;
242     osMessageQueueAttr_t attr = {0};
243     CHAR staticBuff[STATCI_BUFF_SIZE] = {0};
244     CHAR strbuff[] = "hello world";
245     CHAR data[STATCI_BUFF_SIZE] = {0};
246     INT32 ret;
247 
248     ret = osMessageQueuePut(NULL, &strbuff, 0U, 0U);
249     ICUNIT_ASSERT_EQUAL(ret, osErrorParameter, ret);
250 
251     ret = osMessageQueueGet(NULL, &data, NULL, 0U);
252     ICUNIT_ASSERT_EQUAL(ret, osErrorParameter, ret);
253 
254     /* dynmic test */
255     msgQueueId = osMessageQueueNew(1, strlen(strbuff), NULL);
256     ICUNIT_ASSERT_NOT_EQUAL(msgQueueId, NULL, msgQueueId);
257 
258     ret = osMessageQueuePut(msgQueueId, NULL, 0U, 0U);
259     ICUNIT_GOTO_EQUAL(ret, osErrorParameter, ret, EXIT);
260 
261     ret = osMessageQueueGet(msgQueueId, NULL, NULL, 0U);
262     ICUNIT_GOTO_EQUAL(ret, osErrorParameter, ret, EXIT);
263 
264     ret = osMessageQueueDelete(msgQueueId);
265     ICUNIT_ASSERT_EQUAL(ret, osOK, ret);
266 
267     ret = osMessageQueuePut(msgQueueId, &strbuff, 0U, 0U);
268     ICUNIT_GOTO_EQUAL(ret, osErrorParameter, ret, EXIT);
269 
270     ret = osMessageQueueGet(msgQueueId, &data, NULL, 0U);
271     ICUNIT_GOTO_EQUAL(ret, osErrorParameter, ret, EXIT);
272 
273 #if (LOSCFG_BASE_IPC_QUEUE_STATIC == 1)
274     /* static test */
275     attr.mq_mem = staticBuff;
276     attr.mq_size = STATCI_BUFF_SIZE;
277     msgQueueId = osMessageQueueNew(1, STATCI_BUFF_SIZE, &attr);
278     ICUNIT_ASSERT_NOT_EQUAL(msgQueueId, NULL, msgQueueId);
279 
280     ret = osMessageQueuePut(msgQueueId, NULL, 0U, 0U);
281     ICUNIT_GOTO_EQUAL(ret, osErrorParameter, ret, EXIT);
282 
283     ret = osMessageQueueGet(msgQueueId, NULL, NULL, 0U);
284     ICUNIT_GOTO_EQUAL(ret, osErrorParameter, ret, EXIT);
285 
286     ret = osMessageQueueDelete(msgQueueId);
287     ICUNIT_ASSERT_EQUAL(ret, osOK, ret);
288 
289     ret = osMessageQueuePut(msgQueueId, &strbuff, 0U, 0U);
290     ICUNIT_GOTO_EQUAL(ret, osErrorParameter, ret, EXIT);
291 
292     ret = osMessageQueueGet(msgQueueId, &data, NULL, 0U);
293     ICUNIT_GOTO_EQUAL(ret, osErrorParameter, ret, EXIT);
294 #endif
295 
296     return LOS_OK;
297 
298 EXIT:
299     ret = osMessageQueueDelete(msgQueueId);
300     ICUNIT_ASSERT_EQUAL(ret, osOK, ret);
301 
302     return LOS_OK;
303 }
304 
305 /**
306  * @tc.name: TestCmsis004
307  * @tc.desc: read write test
308  * @tc.type: FUNC
309  * @tc.require: issueI5LBE8
310  */
311 LITE_TEST_CASE(CmsisFuncTestSuite, TestCmsis004, Function | MediumTest | Level1)
312 {
313     osMessageQueueId_t msgQueueId;
314     osMessageQueueAttr_t attr = {0};
315     CHAR staticBuff[STATCI_BUFF_SIZE] = {0};
316     CHAR strbuff[] = "hello world";
317     CHAR data[STATCI_BUFF_SIZE] = {0};
318     INT32 ret;
319 
320     /* dynamic test */
321     msgQueueId = osMessageQueueNew(1, strlen(strbuff), NULL);
322     ICUNIT_ASSERT_NOT_EQUAL(msgQueueId, NULL, msgQueueId);
323 
324     ret = osMessageQueuePut(msgQueueId, &strbuff, 0U, 0U);
325     ICUNIT_ASSERT_EQUAL(ret, osOK, ret);
326 
327     ret = osMessageQueueGet(msgQueueId, &data, NULL, 0U);
328     ICUNIT_ASSERT_EQUAL(ret, osOK, ret);
329 
330     ret = strcmp(data, strbuff);
331     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
332 
333     ret = osMessageQueueDelete(msgQueueId);
334     ICUNIT_ASSERT_EQUAL(ret, osOK, ret);
335 
336 #if (LOSCFG_BASE_IPC_QUEUE_STATIC == 1)
337     /* static test */
338     attr.mq_mem = staticBuff;
339     attr.mq_size = strlen(strbuff) + 1;
340     msgQueueId = osMessageQueueNew(1, strlen(strbuff), &attr);
341     ICUNIT_ASSERT_NOT_EQUAL(msgQueueId, NULL, msgQueueId);
342 
343     ret = osMessageQueuePut(msgQueueId, &strbuff, 0U, 0U);
344     ICUNIT_GOTO_EQUAL(ret, osOK, ret, EXIT);
345 
346     ret = osMessageQueueGet(msgQueueId, &data, NULL, 0U);
347     ICUNIT_GOTO_EQUAL(ret, osOK, ret, EXIT);
348 
349     ret = strcmp(data, strbuff);
350     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
351 
352     ret = strcmp(staticBuff, strbuff);
353     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
354 
355     ret = osMessageQueueDelete(msgQueueId);
356     ICUNIT_ASSERT_EQUAL(ret, osOK, ret);
357 #endif
358     return LOS_OK;
359 
360 EXIT:
361     ret = osMessageQueueDelete(msgQueueId);
362     ICUNIT_ASSERT_EQUAL(ret, osOK, ret);
363 
364     return LOS_OK;
365 }
366 
367 /**
368  * @tc.name: TestCmsis003
369  * @tc.desc: create exception parameters test
370  * @tc.type: FUNC
371  * @tc.require: issueI5LBE8
372  */
373 LITE_TEST_CASE(CmsisFuncTestSuite, TestCmsis003, Function | MediumTest | Level1)
374 {
375     osMessageQueueId_t msgQueueId;
376     osMessageQueueAttr_t attr = {0};
377     CHAR staticBuff[STATCI_BUFF_SIZE] = {0};
378     CHAR strbuff[] = "hello world";
379 
380     /* dynmic test */
381     msgQueueId = osMessageQueueNew(0, strlen(strbuff), NULL);
382     ICUNIT_ASSERT_EQUAL(msgQueueId, NULL, msgQueueId);
383 
384     msgQueueId = osMessageQueueNew(1, 0xFFFFFFFF, NULL);
385     ICUNIT_ASSERT_EQUAL(msgQueueId, NULL, msgQueueId);
386 
387 #if (LOSCFG_BASE_IPC_QUEUE_STATIC == 1)
388     /* static test */
389     attr.mq_mem = staticBuff;
390     attr.mq_size = STATCI_BUFF_SIZE;
391     msgQueueId = osMessageQueueNew(0, strlen(strbuff), &attr);
392     ICUNIT_ASSERT_EQUAL(msgQueueId, NULL, msgQueueId);
393 
394     msgQueueId = osMessageQueueNew(0xFFFFFFFF, strlen(strbuff), &attr);
395     ICUNIT_ASSERT_EQUAL(msgQueueId, NULL, msgQueueId);
396 
397     attr.mq_mem = staticBuff;
398     attr.mq_size = 0;
399     msgQueueId = osMessageQueueNew(1, strlen(strbuff), &attr);
400     ICUNIT_ASSERT_EQUAL(msgQueueId, NULL, msgQueueId);
401 
402     attr.mq_mem = NULL;
403     attr.mq_size = STATCI_BUFF_SIZE;
404     msgQueueId = osMessageQueueNew(1, strlen(strbuff), &attr);
405     ICUNIT_ASSERT_EQUAL(msgQueueId, NULL, msgQueueId);
406 #endif
407 
408     return LOS_OK;
409 };
410 
411 /**
412  * @tc.name: TestCmsis002
413  * @tc.desc: create and delete test
414  * @tc.type: FUNC
415  * @tc.require: issueI5LBE8
416  */
417 LITE_TEST_CASE(CmsisFuncTestSuite, TestCmsis002, Function | MediumTest | Level1)
418 {
419     osMessageQueueId_t msgQueueId;
420     osMessageQueueAttr_t attr = {0};
421     CHAR staticBuff[STATCI_BUFF_SIZE] = {0};
422     CHAR strbuff[] = "hello world";
423     INT32 ret;
424 
425     /* dynamic test */
426     msgQueueId = osMessageQueueNew(1, strlen(strbuff), NULL);
427     ICUNIT_ASSERT_NOT_EQUAL(msgQueueId, NULL, msgQueueId);
428 
429     ret = osMessageQueueDelete(msgQueueId);
430     ICUNIT_ASSERT_EQUAL(ret, osOK, ret);
431 
432     ret = osMessageQueueDelete(msgQueueId);
433     ICUNIT_ASSERT_EQUAL(ret, osErrorParameter, ret);
434 
435 #if (LOSCFG_BASE_IPC_QUEUE_STATIC == 1)
436     /* static test */
437     attr.mq_mem = staticBuff;
438     attr.mq_size = STATCI_BUFF_SIZE;
439     msgQueueId = osMessageQueueNew(1, STATCI_BUFF_SIZE, &attr);
440     ICUNIT_ASSERT_NOT_EQUAL(msgQueueId, NULL, msgQueueId);
441 
442     ret = osMessageQueueDelete(msgQueueId);
443     ICUNIT_ASSERT_EQUAL(ret, osOK, ret);
444 
445     ret = osMessageQueueDelete(msgQueueId);
446     ICUNIT_ASSERT_EQUAL(ret, osErrorParameter, ret);
447 #endif
448 
449     return LOS_OK;
450 };
451 
452 
CmsisFuncTestSuite(void)453 void CmsisFuncTestSuite(void)
454 {
455     PRINTF("***********************BEGIN CMSIS TEST**********************\n");
456 
457     ADD_TEST_CASE(TestCmsis001);
458     ADD_TEST_CASE(TestCmsis002);
459     ADD_TEST_CASE(TestCmsis003);
460     ADD_TEST_CASE(TestCmsis004);
461     ADD_TEST_CASE(TestCmsis005);
462 
463 #if (LOSCFG_BASE_IPC_QUEUE_STATIC == 1)
464     ADD_TEST_CASE(TestCmsis006);
465 #endif
466 
467     ADD_TEST_CASE(TestCmsis007);
468 }
469 
470