• 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 strbuff[] = "hello world";
95     CHAR *name = NULL;
96     INT32 ret;
97 
98     attr.name = "q1";
99     /* dynamic test */
100     msgQueueId = osMessageQueueNew(1, strlen(strbuff), &attr);
101     ICUNIT_ASSERT_NOT_EQUAL(msgQueueId, NULL, msgQueueId);
102 
103     name = (CHAR *)osMessageQueueGetName(msgQueueId);
104     ret = strcmp(name, "q1");
105     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
106 
107     ret = osMessageQueueDelete(msgQueueId);
108     ICUNIT_ASSERT_EQUAL(ret, osOK, ret);
109 
110     name = (CHAR *)osMessageQueueGetName(msgQueueId);
111     ICUNIT_ASSERT_EQUAL(name, NULL, name);
112 
113 #if (LOSCFG_BASE_IPC_QUEUE_STATIC == 1)
114     CHAR staticBuff[STATCI_BUFF_SIZE] = {0};
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 = (CHAR *)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 = (CHAR *)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_VOID(ret, osOK, ret);
159 
160     status = osMessageQueueGet(g_msgQueueId2, &data, NULL, QUEUE_WAIT_TIMEOUT);
161     ICUNIT_ASSERT_EQUAL_VOID(status, osOK, status);
162 
163     ret = strcmp(data, "world");
164     ICUNIT_ASSERT_EQUAL_VOID(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_VOID(status, osOK, status);
175 
176     ret = strcmp(data, "hello");
177     ICUNIT_ASSERT_EQUAL_VOID(ret, 0, ret);
178 
179     ret = osMessageQueuePut(g_msgQueueId2, &g_strbuff2, 0U, 0U);
180     ICUNIT_ASSERT_EQUAL_VOID(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((osThreadFunc_t)CmsisQueueTestThread1, NULL, NULL);
197     ICUNIT_ASSERT_NOT_EQUAL(threadId1, NULL, threadId1);
198 
199     threadId2 = osThreadNew((osThreadFunc_t)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     CHAR strbuff[] = "hello world";
243     CHAR data[STATCI_BUFF_SIZE] = {0};
244     INT32 ret;
245 
246     ret = osMessageQueuePut(NULL, &strbuff, 0U, 0U);
247     ICUNIT_ASSERT_EQUAL(ret, osErrorParameter, ret);
248 
249     ret = osMessageQueueGet(NULL, &data, NULL, 0U);
250     ICUNIT_ASSERT_EQUAL(ret, osErrorParameter, ret);
251 
252     /* dynmic test */
253     msgQueueId = osMessageQueueNew(1, strlen(strbuff), NULL);
254     ICUNIT_ASSERT_NOT_EQUAL(msgQueueId, NULL, msgQueueId);
255 
256     ret = osMessageQueuePut(msgQueueId, NULL, 0U, 0U);
257     ICUNIT_GOTO_EQUAL(ret, osErrorParameter, ret, EXIT);
258 
259     ret = osMessageQueueGet(msgQueueId, NULL, NULL, 0U);
260     ICUNIT_GOTO_EQUAL(ret, osErrorParameter, ret, EXIT);
261 
262     ret = osMessageQueueDelete(msgQueueId);
263     ICUNIT_ASSERT_EQUAL(ret, osOK, ret);
264 
265     ret = osMessageQueuePut(msgQueueId, &strbuff, 0U, 0U);
266     ICUNIT_GOTO_EQUAL(ret, osErrorParameter, ret, EXIT);
267 
268     ret = osMessageQueueGet(msgQueueId, &data, NULL, 0U);
269     ICUNIT_GOTO_EQUAL(ret, osErrorParameter, ret, EXIT);
270 
271 #if (LOSCFG_BASE_IPC_QUEUE_STATIC == 1)
272     /* static test */
273     osMessageQueueAttr_t attr = {0};
274     CHAR staticBuff[STATCI_BUFF_SIZE] = {0};
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     CHAR strbuff[] = "hello world";
315     CHAR data[STATCI_BUFF_SIZE] = {0};
316     INT32 ret;
317 
318     /* dynamic test */
319     msgQueueId = osMessageQueueNew(1, strlen(strbuff), NULL);
320     ICUNIT_ASSERT_NOT_EQUAL(msgQueueId, NULL, msgQueueId);
321 
322     ret = osMessageQueuePut(msgQueueId, &strbuff, 0U, 0U);
323     ICUNIT_ASSERT_EQUAL(ret, osOK, ret);
324 
325     ret = osMessageQueueGet(msgQueueId, &data, NULL, 0U);
326     ICUNIT_ASSERT_EQUAL(ret, osOK, ret);
327 
328     ret = strcmp(data, strbuff);
329     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
330 
331     ret = osMessageQueueDelete(msgQueueId);
332     ICUNIT_ASSERT_EQUAL(ret, osOK, ret);
333 
334 #if (LOSCFG_BASE_IPC_QUEUE_STATIC == 1)
335     /* static test */
336     osMessageQueueAttr_t attr = {0};
337     CHAR staticBuff[STATCI_BUFF_SIZE] = {0};
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 EXIT:
356     ret = osMessageQueueDelete(msgQueueId);
357     ICUNIT_ASSERT_EQUAL(ret, osOK, ret);
358 #endif
359     return LOS_OK;
360 }
361 
362 /**
363  * @tc.name: TestCmsis003
364  * @tc.desc: create exception parameters test
365  * @tc.type: FUNC
366  * @tc.require: issueI5LBE8
367  */
368 LITE_TEST_CASE(CmsisFuncTestSuite, TestCmsis003, Function | MediumTest | Level1)
369 {
370     osMessageQueueId_t msgQueueId;
371     CHAR strbuff[] = "hello world";
372 
373     /* dynmic test */
374     msgQueueId = osMessageQueueNew(0, strlen(strbuff), NULL);
375     ICUNIT_ASSERT_EQUAL(msgQueueId, NULL, msgQueueId);
376 
377     msgQueueId = osMessageQueueNew(1, 0xFFFFFFFF, NULL);
378     ICUNIT_ASSERT_EQUAL(msgQueueId, NULL, msgQueueId);
379 
380 #if (LOSCFG_BASE_IPC_QUEUE_STATIC == 1)
381     /* static test */
382     osMessageQueueAttr_t attr = {0};
383     CHAR staticBuff[STATCI_BUFF_SIZE] = {0};
384     attr.mq_mem = staticBuff;
385     attr.mq_size = STATCI_BUFF_SIZE;
386     msgQueueId = osMessageQueueNew(0, strlen(strbuff), &attr);
387     ICUNIT_ASSERT_EQUAL(msgQueueId, NULL, msgQueueId);
388 
389     msgQueueId = osMessageQueueNew(0xFFFFFFFF, strlen(strbuff), &attr);
390     ICUNIT_ASSERT_EQUAL(msgQueueId, NULL, msgQueueId);
391 
392     attr.mq_mem = staticBuff;
393     attr.mq_size = 0;
394     msgQueueId = osMessageQueueNew(1, strlen(strbuff), &attr);
395     ICUNIT_ASSERT_EQUAL(msgQueueId, NULL, msgQueueId);
396 
397     attr.mq_mem = NULL;
398     attr.mq_size = STATCI_BUFF_SIZE;
399     msgQueueId = osMessageQueueNew(1, strlen(strbuff), &attr);
400     ICUNIT_ASSERT_EQUAL(msgQueueId, NULL, msgQueueId);
401 #endif
402 
403     return LOS_OK;
404 };
405 
406 /**
407  * @tc.name: TestCmsis002
408  * @tc.desc: create and delete test
409  * @tc.type: FUNC
410  * @tc.require: issueI5LBE8
411  */
412 LITE_TEST_CASE(CmsisFuncTestSuite, TestCmsis002, Function | MediumTest | Level1)
413 {
414     osMessageQueueId_t msgQueueId;
415     CHAR strbuff[] = "hello world";
416     INT32 ret;
417 
418     /* dynamic test */
419     msgQueueId = osMessageQueueNew(1, strlen(strbuff), NULL);
420     ICUNIT_ASSERT_NOT_EQUAL(msgQueueId, NULL, msgQueueId);
421 
422     ret = osMessageQueueDelete(msgQueueId);
423     ICUNIT_ASSERT_EQUAL(ret, osOK, ret);
424 
425     ret = osMessageQueueDelete(msgQueueId);
426     ICUNIT_ASSERT_EQUAL(ret, osErrorParameter, ret);
427 
428 #if (LOSCFG_BASE_IPC_QUEUE_STATIC == 1)
429     /* static test */
430     osMessageQueueAttr_t attr = {0};
431     CHAR staticBuff[STATCI_BUFF_SIZE] = {0};
432     attr.mq_mem = staticBuff;
433     attr.mq_size = STATCI_BUFF_SIZE;
434     msgQueueId = osMessageQueueNew(1, STATCI_BUFF_SIZE, &attr);
435     ICUNIT_ASSERT_NOT_EQUAL(msgQueueId, NULL, msgQueueId);
436 
437     ret = osMessageQueueDelete(msgQueueId);
438     ICUNIT_ASSERT_EQUAL(ret, osOK, ret);
439 
440     ret = osMessageQueueDelete(msgQueueId);
441     ICUNIT_ASSERT_EQUAL(ret, osErrorParameter, ret);
442 #endif
443 
444     return LOS_OK;
445 };
446 
timerCallback(void * arg)447 static VOID timerCallback(void *arg)
448 {
449     return;
450 }
451 
452 /**
453  * @tc.name: TestCmsisTimer001
454  * @tc.desc: Timer Management test
455  * @tc.type: FUNC
456  * @tc.require: issueI5TQ0T
457  */
458 LITE_TEST_CASE(CmsisFuncTestSuite, TestCmsisTimer001, Function | MediumTest | Level1)
459 {
460     osTimerId_t time_id;
461     const char *timerGetName = NULL;
462     osStatus_t ret;
463 
464     time_id = osTimerNew(timerCallback, osTimerOnce, NULL, NULL);
465     ICUNIT_ASSERT_NOT_EQUAL(time_id, NULL, time_id);
466 
467     ret = osTimerStart(time_id, 100U); // 100, just for test
468     ICUNIT_ASSERT_EQUAL(ret, osOK, ret);
469 
470     timerGetName = osTimerGetName(time_id);
471     ICUNIT_GOTO_EQUAL(timerGetName, NULL, timerGetName, EXIT1);
472 
473     ret = osTimerIsRunning(time_id);
474     ICUNIT_GOTO_EQUAL(ret, 1, ret, EXIT1);
475 
476     ret = osTimerStop(time_id);
477     ICUNIT_GOTO_EQUAL(ret, osOK, ret, EXIT1);
478 
479     ret = osTimerIsRunning(time_id);
480     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT1);
481 
482     ret = osTimerDelete(time_id);
483     ICUNIT_ASSERT_EQUAL(ret, osOK, ret);
484 
485     return LOS_OK;
486 
487 EXIT1:
488     ret = osTimerDelete(time_id);
489     ICUNIT_ASSERT_EQUAL(ret, osOK, ret);
490 
491     return LOS_OK;
492 };
493 
494 
CmsisFuncTestSuite(void)495 void CmsisFuncTestSuite(void)
496 {
497     PRINTF("***********************BEGIN CMSIS TEST**********************\n");
498 
499     ADD_TEST_CASE(TestCmsis001);
500     ADD_TEST_CASE(TestCmsis002);
501     ADD_TEST_CASE(TestCmsis003);
502     ADD_TEST_CASE(TestCmsis004);
503     ADD_TEST_CASE(TestCmsis005);
504 
505 #if (LOSCFG_BASE_IPC_QUEUE_STATIC == 1)
506     ADD_TEST_CASE(TestCmsis006);
507 #endif
508 
509     ADD_TEST_CASE(TestCmsis007);
510 
511     ADD_TEST_CASE(TestCmsisTimer001);
512 }
513 
514