• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020-2022 Huawei Device Co., Ltd.
3  *
4  * HDF is dual licensed: you can use it either under the terms of
5  * the GPL, or the BSD license, at your option.
6  * See the LICENSE file in the root of this repository for complete details.
7  */
8 
9 #include "hdf_base.h"
10 #include "hdf_log.h"
11 #include "osal_firmware.h"
12 #include "osal_irq.h"
13 #include "osal_list_test.h"
14 #include "osal_file_test.h"
15 #include "osal_case_cmd_test.h"
16 #include "osal_get_case_test.h"
17 #include "osal_test_case_def.h"
18 #include "osal_mem.h"
19 #include "osal_mutex.h"
20 #include "osal_spinlock.h"
21 #ifndef __USER__
22 #include "osal_test_type.h"
23 #endif
24 #include "osal_thread.h"
25 #include "osal_time.h"
26 #include "osal_timer.h"
27 #include "securec.h"
28 
29 #define IRQ_NUM_TEST 33
30 #define HDF_LOG_TAG osal_test
31 
32 #define OSAL_TEST_TIME_DEFAULT 100
33 
34 uint32_t g_osalTestCases[OSAL_TEST_CASE_CNT];
35 
36 OsalTimespec g_hdfTestBegin;
37 OsalTimespec g_hdfTestEnd;
38 static int32_t g_waitMutexTime = 3100;
39 static bool g_threadTest1Flag = true;
40 OSAL_DECLARE_THREAD(thread1);
41 OSAL_DECLARE_THREAD(thread2);
42 OSAL_DECLARE_THREAD(thread);
43 struct OsalMutex g_mutexTest;
44 OSAL_DECLARE_SPINLOCK(g_spinTest);
45 
46 #define HDF_THREAD_TEST_SLEEP_S 1
47 #define HDF_THREAD_TEST_SLEEP_US 600
48 #define HDF_THREAD_TEST_SLEEP_MS 300
49 #define HDF_THREAD_TEST_MUX_CNT 20
50 static bool g_thread1RunFlag;
51 static bool g_threadMuxLockFlag;
52 static int32_t g_test1Para = 120;
53 static int32_t g_test2Para = 123;
54 #define TIME_RANGE 200000
55 #define NUMERATOR 15
56 #define DENOMINATOR 100
OsalCheckTime(OsalTimespec * time,uint32_t ms)57 static bool OsalCheckTime(OsalTimespec *time, uint32_t ms)
58 {
59     uint64_t t1 = time->sec * HDF_KILO_UNIT * HDF_KILO_UNIT + time->usec;
60     uint64_t t2 = (uint64_t)ms * HDF_KILO_UNIT;
61     uint64_t diff = (t1 < t2) ? (t2 - t1) : (t1 - t2);
62     uint64_t timeRange = t2 / DENOMINATOR * NUMERATOR;
63     timeRange = (timeRange < TIME_RANGE) ? TIME_RANGE: timeRange;
64 
65     return diff < timeRange;
66 }
67 
ThreadTest1(void * arg)68 static int ThreadTest1(void *arg)
69 {
70     static int cnt = 0;
71     int ret;
72 
73     HDF_LOGI("[OSAL_UT_TEST]%s test thread para end", __func__);
74     (void)arg;
75 
76     g_thread1RunFlag = true;
77 
78     while (g_threadTest1Flag) {
79         OsalSleep(HDF_THREAD_TEST_SLEEP_S);
80         HDF_LOGE("%s %d", __func__, cnt);
81         cnt++;
82         if (cnt > HDF_THREAD_TEST_MUX_CNT) {
83             g_waitMutexTime = HDF_WAIT_FOREVER;
84         }
85         ret = OsalMutexTimedLock(&g_mutexTest, g_waitMutexTime);
86         if (g_threadMuxLockFlag) {
87             UT_TEST_CHECK_RET(ret == HDF_FAILURE, OSAL_MUTEX_LOCK_TIMEOUT);
88         } else {
89             UT_TEST_CHECK_RET(ret == HDF_FAILURE, OSAL_MUTEX_LOCK_TIMEOUT);
90         }
91 
92         OsalMSleep(HDF_THREAD_TEST_SLEEP_MS);
93         if (ret == HDF_SUCCESS) {
94             ret = OsalMutexUnlock(&g_mutexTest);
95             UT_TEST_CHECK_RET(ret != HDF_SUCCESS, OSAL_MUTEX_UNLOCK);
96         }
97         OsalMSleep(HDF_THREAD_TEST_SLEEP_US);
98 
99         ret = OsalSpinLock(&g_spinTest);
100         UT_TEST_CHECK_RET(ret != HDF_SUCCESS, OSAL_SPIN_LOCK);
101         ret = OsalSpinUnlock(&g_spinTest);
102         UT_TEST_CHECK_RET(ret != HDF_SUCCESS, OSAL_SPIN_UNLOCK);
103 
104         OsalMSleep(HDF_THREAD_TEST_SLEEP_MS);
105         if (cnt % HDF_THREAD_TEST_MUX_CNT == 0) {
106             HDF_LOGE("%s ", __func__);
107         }
108         if (g_testEndFlag) {
109             break;
110         }
111     }
112     HDF_LOGE("%s thread return\n", __func__);
113     return 0;
114 }
115 
116 static bool g_thread2RunFlag;
117 static bool g_threadTest2Flag = true;
ThreadTest2(void * arg)118 static int ThreadTest2(void *arg)
119 {
120     static int cnt = 0;
121     OsalTimespec hdfTs1 = { 0, 0 };
122     OsalTimespec hdfTs2 = { 0, 0 };
123     OsalTimespec hdfTsDiff = { 0, 0 };
124     int ret;
125     int32_t para;
126 
127     HDF_LOGI("[OSAL_UT_TEST]%s test thread para end", __func__);
128     if (arg != NULL) {
129         para = *(int32_t *)arg;
130         UT_TEST_CHECK_RET(para != g_test2Para, OSAL_THREAD_PARA_CHECK);
131     } else {
132         UT_TEST_CHECK_RET(true, OSAL_THREAD_PARA_CHECK);
133     }
134 
135     g_thread2RunFlag = true;
136 
137     while (g_threadTest2Flag) {
138         OsalSleep(HDF_THREAD_TEST_SLEEP_S);
139         OsalGetTime(&hdfTs1);
140         HDF_LOGE("%s %d", __func__, cnt);
141 
142         cnt++;
143         ret = OsalMutexTimedLock(&g_mutexTest, g_waitMutexTime);
144         if (g_threadMuxLockFlag) {
145             UT_TEST_CHECK_RET(ret == HDF_FAILURE, OSAL_MUTEX_LOCK_TIMEOUT);
146         } else {
147             UT_TEST_CHECK_RET(ret == HDF_FAILURE, OSAL_MUTEX_LOCK_TIMEOUT);
148         }
149 
150         OsalMSleep(HDF_THREAD_TEST_SLEEP_MS);
151         if (ret == HDF_SUCCESS) {
152             ret = OsalMutexUnlock(&g_mutexTest);
153             UT_TEST_CHECK_RET(ret != HDF_SUCCESS, OSAL_MUTEX_UNLOCK);
154         }
155 
156         OsalMSleep(HDF_THREAD_TEST_SLEEP_US);
157 
158         ret = OsalSpinLock(&g_spinTest);
159         UT_TEST_CHECK_RET(ret != HDF_SUCCESS, OSAL_SPIN_LOCK);
160         ret = OsalSpinUnlock(&g_spinTest);
161         UT_TEST_CHECK_RET(ret != HDF_SUCCESS, OSAL_SPIN_UNLOCK);
162 
163         OsalMSleep(HDF_THREAD_TEST_SLEEP_MS);
164         OsalGetTime(&hdfTs2);
165         OsalDiffTime(&hdfTs1, &hdfTs2, &hdfTsDiff);
166         if (cnt % HDF_THREAD_TEST_MUX_CNT == 0) {
167             HDF_LOGE("%s %us %uus", __func__,
168                 (uint32_t)hdfTsDiff.sec, (uint32_t)hdfTsDiff.usec);
169         }
170         if (g_testEndFlag) {
171             break;
172         }
173     }
174     HDF_LOGE("%s thread return\n", __func__);
175     return 0;
176 }
177 
178 #define HDF_DBG_CNT_CTRL 10
179 #ifndef __USER__
180 OSAL_DECLARE_TIMER(g_testTimerLoop1);
181 OSAL_DECLARE_TIMER(g_testTimerLoop2);
182 OSAL_DECLARE_TIMER(g_testTimerOnce);
183 #define HDF_TIMER1_PERIOD 630
184 #define HDF_TIMER2_PERIOD 1350
185 static int32_t g_timerPeriod1 = HDF_TIMER1_PERIOD;
186 static int32_t g_timerPeriod2 = HDF_TIMER2_PERIOD;
187 static int32_t g_timerPeriod3 = 10300;
188 static int32_t g_timerPeriod1Modify = 1250;
189 static int32_t g_timerPeriod2Modify = 750;
190 #define HDF_TIMER_TEST_MODIFY 8
191 #define HDF_TEST_TIMER_PARA 1
192 #define HDF_TEST_TIMER_MODIFY 2
193 #define HDF_TEST_TIMER_END 3
194 
195 static bool g_timerLoop1RunFlag;
196 static int g_timer1Cnt = 0;
TimerLoopTest1(uintptr_t arg)197 static void TimerLoopTest1(uintptr_t arg)
198 {
199     int32_t para;
200     OsalTimespec hdfTs1 = { 0, 0 };
201     static OsalTimespec hdfTs2 = { 0, 0 };
202     OsalTimespec hdfTsDiff = { 0, 0 };
203     static int index = HDF_TEST_TIMER_PARA;
204     int32_t ret;
205 
206     if (g_timer1Cnt == 0) {
207         OsalGetTime(&hdfTs2);
208     }
209 
210     para = *(int32_t *)arg;
211     if ((g_timer1Cnt >= 1) && (g_timer1Cnt != HDF_TIMER_TEST_MODIFY + 1)) {
212         OsalGetTime(&hdfTs1);
213         OsalDiffTime(&hdfTs2, &hdfTs1, &hdfTsDiff);
214         HDF_LOGE("%s %d %d %d %d %d", __func__, g_timer1Cnt, para, (int32_t)hdfTsDiff.sec,
215             (int32_t)hdfTsDiff.usec, g_timerPeriod1);
216         UT_TEST_CHECK_RET(!OsalCheckTime(&hdfTsDiff, g_timerPeriod1), OSAL_TIMER_PERIOD_CHECK);
217         UT_TEST_CHECK_RET(g_timerPeriod1 != para, OSAL_TIMER_PARA_CHECK);
218         if (index == HDF_TEST_TIMER_PARA) {
219             HDF_LOGE("[OSAL_UT_TEST]%s test timer para end", __func__);
220             index = HDF_TEST_TIMER_END;
221         }
222         if (index == HDF_TEST_TIMER_MODIFY) {
223             HDF_LOGE("[OSAL_UT_TEST]%s test timer modify function end", __func__);
224             index = HDF_TEST_TIMER_END;
225         }
226     }
227 
228     if (g_timer1Cnt == HDF_TIMER_TEST_MODIFY) {
229         g_timerPeriod1 = g_timerPeriod1Modify;
230         ret = OsalTimerSetTimeout(&g_testTimerLoop1, g_timerPeriod1);
231         UT_TEST_CHECK_RET(ret != HDF_SUCCESS, OSAL_TIMER_MODIFY_CHECK);
232         index = HDF_TEST_TIMER_MODIFY;
233     }
234 
235     OsalGetTime(&hdfTs2);
236 
237     g_timer1Cnt++;
238     g_timerLoop1RunFlag = true;
239 }
240 
241 static bool g_timerLoop2RunFlag;
242 static int g_timer2Cnt = 0;
TimerLoopTest2(uintptr_t arg)243 static void TimerLoopTest2(uintptr_t arg)
244 {
245     int32_t para;
246     OsalTimespec hdfTs1 = { 0, 0 };
247     static OsalTimespec hdfTs2 = { 0, 0 };
248     OsalTimespec hdfTsDiff = { 0, 0 };
249     static int index = HDF_TEST_TIMER_PARA;
250     int32_t ret;
251 
252     if (g_timer2Cnt == 0) {
253         OsalGetTime(&hdfTs2);
254     }
255 
256     para = *(int32_t *)arg;
257     if ((g_timer2Cnt >= 1) && (g_timer2Cnt != HDF_TIMER_TEST_MODIFY + 1)) {
258         OsalGetTime(&hdfTs1);
259         OsalDiffTime(&hdfTs2, &hdfTs1, &hdfTsDiff);
260         HDF_LOGE("%s %d %d %d %d %d", __func__, g_timer2Cnt, para, (int32_t)hdfTsDiff.sec,
261             (int32_t)hdfTsDiff.usec, g_timerPeriod2);
262 
263         UT_TEST_CHECK_RET(!OsalCheckTime(&hdfTsDiff, g_timerPeriod2), OSAL_TIMER_PERIOD_CHECK);
264         UT_TEST_CHECK_RET(g_timerPeriod2 != para, OSAL_TIMER_PARA_CHECK);
265         if (index == HDF_TEST_TIMER_PARA) {
266             HDF_LOGE("[OSAL_UT_TEST]%s test timer para end", __func__);
267             index = HDF_TEST_TIMER_END;
268         }
269         if (index == HDF_TEST_TIMER_MODIFY) {
270             HDF_LOGE("[OSAL_UT_TEST]%s test timer modify function end", __func__);
271             index = HDF_TEST_TIMER_END;
272         }
273     }
274 
275     if (g_timer2Cnt == HDF_TIMER_TEST_MODIFY) {
276         g_timerPeriod2 = g_timerPeriod2Modify;
277         HDF_LOGE("[OSAL_UT_TEST]%s modify timer", __func__);
278         ret = OsalTimerSetTimeout(&g_testTimerLoop2, g_timerPeriod2);
279         UT_TEST_CHECK_RET(ret != HDF_SUCCESS, OSAL_TIMER_MODIFY_CHECK);
280         index = HDF_TEST_TIMER_MODIFY;
281     }
282     OsalGetTime(&hdfTs2);
283     g_timer2Cnt++;
284     g_timerLoop2RunFlag = true;
285 }
286 
287 static int g_timerOnceRunCnt = 0;
TimerOnceTest(uintptr_t arg)288 static void TimerOnceTest(uintptr_t arg)
289 {
290     int32_t para;
291     para = *(int32_t *)arg;
292 
293     HDF_LOGE("%s %d", __func__, para);
294     UT_TEST_CHECK_RET(para != g_timerPeriod3, OSAL_TIMER_PARA_CHECK);
295     g_timerOnceRunCnt++;
296 }
297 
OsaTimerTest(void)298 static void OsaTimerTest(void)
299 {
300     int32_t ret;
301 
302     HDF_LOGI("[OSAL_UT_TEST]%s start", __func__);
303     g_timerPeriod1 = HDF_TIMER1_PERIOD;
304     g_timerPeriod2 = HDF_TIMER2_PERIOD;
305 
306     ret = OsalTimerCreate(&g_testTimerLoop1, g_timerPeriod1, TimerLoopTest1, (uintptr_t)&g_timerPeriod1);
307     UT_TEST_CHECK_RET(ret != HDF_SUCCESS, OSAL_TIMER_CREATE_LOOP);
308     OsalTimerStartLoop(&g_testTimerLoop1);
309 
310     ret = OsalTimerCreate(&g_testTimerLoop2, g_timerPeriod2, TimerLoopTest2, (uintptr_t)&g_timerPeriod2);
311     UT_TEST_CHECK_RET(ret != HDF_SUCCESS, OSAL_TIMER_CREATE_LOOP);
312     OsalTimerStartLoop(&g_testTimerLoop2);
313 
314     ret = OsalTimerCreate(&g_testTimerOnce, g_timerPeriod3, TimerOnceTest, (uintptr_t)&g_timerPeriod3);
315     UT_TEST_CHECK_RET(ret != HDF_SUCCESS, OSAL_TIMER_CREATE_ONCE);
316     OsalTimerStartOnce(&g_testTimerOnce);
317 
318     HDF_LOGI("[OSAL_UT_TEST]%s end", __func__);
319 }
320 
321 #define HDF_ONCE_TIMER_DEL_TIME 10
OsaTimerTestStop(void)322 static void OsaTimerTestStop(void)
323 {
324     int32_t ret;
325 
326     HDF_LOGI("[OSAL_UT_TEST]%s start", __func__);
327     ret = OsalTimerDelete(&g_testTimerLoop2);
328     UT_TEST_CHECK_RET(ret != HDF_SUCCESS, OSAL_TIMER_STOP_CHECK);
329     g_timerLoop2RunFlag = false;
330 
331     ret = OsalTimerDelete(&g_testTimerOnce);
332     UT_TEST_CHECK_RET(ret != HDF_SUCCESS, OSAL_TIMER_STOP_CHECK);
333 
334     ret = OsalTimerCreate(&g_testTimerOnce, g_timerPeriod3, TimerOnceTest, (uintptr_t)&g_timerPeriod3);
335     UT_TEST_CHECK_RET(ret != HDF_SUCCESS, OSAL_TIMER_CREATE_ONCE);
336 
337     OsalTimerStartOnce(&g_testTimerOnce);
338     HDF_LOGI("[OSAL_UT_TEST]%s OsalTimerStartOnce", __func__);
339     OsalMSleep(HDF_ONCE_TIMER_DEL_TIME);
340     ret = OsalTimerDelete(&g_testTimerOnce);
341     UT_TEST_CHECK_RET(ret != HDF_SUCCESS, OSAL_TIMER_STOP_CHECK);
342 
343     HDF_LOGI("[OSAL_UT_TEST]%s end", __func__);
344 }
345 
346 #define HDF_FILE_SEEK_TEST 8
OsaFWTest(int flag)347 static void OsaFWTest(int flag)
348 {
349     struct OsalFirmware fw;
350     struct OsalFwBlock block;
351     int32_t ret;
352     char *name = NULL;
353     char *fwBuf = OsalFWTestBuff();
354 
355     HDF_LOGE("[OSAL_UT_TEST]%s start", __func__);
356     if (flag != 0) {
357         HDF_LOGE("%s file do not exist %d", __func__, flag);
358         return;
359     }
360 #if defined(__LITEOS__)
361     name = TEST_FW_PATH_NAME;
362 #else
363     name = TEST_FW_NAME;
364 #endif
365     (void)memset_s(&fw, sizeof(fw), 0, sizeof(fw));
366     ret = OsalRequestFirmware(&fw, name, NULL);
367     UT_TEST_CHECK_RET(ret != HDF_SUCCESS, OSAL_FW_REQUEST);
368     HDF_LOGE("%s %u", name, fw.fwSize);
369 
370     (void)memset_s(&block, sizeof(block), 0, sizeof(block));
371     ret = OsalReadFirmware(&fw, &block);
372     UT_TEST_CHECK_RET(ret != HDF_SUCCESS, OSAL_FW_READ);
373     HDF_LOGE("%s %d %d", name, block.dataSize, block.curOffset);
374     UT_TEST_CHECK_RET(memcmp(block.data, fwBuf, block.dataSize) != 0, OSAL_FW_DATA_CHECK);
375 #if defined(__LITEOS__)
376     ret = OsalSeekFirmware(&fw, HDF_FILE_SEEK_TEST);
377     UT_TEST_CHECK_RET(ret != HDF_SUCCESS, OSAL_FW_SEEK);
378     HDF_LOGE("%s %u %d", name, block.dataSize, block.curOffset);
379     ret = OsalReadFirmware(&fw, &block);
380     UT_TEST_CHECK_RET(ret != HDF_SUCCESS, OSAL_FW_READ);
381     HDF_LOGE("%s %u %d", name, block.dataSize, block.curOffset);
382     fwBuf += HDF_FILE_SEEK_TEST;
383     UT_TEST_CHECK_RET(memcmp(block.data, fwBuf, block.dataSize) != 0, OSAL_FW_DATA_CHECK);
384 #endif
385     ret = OsalReleaseFirmware(&fw);
386     UT_TEST_CHECK_RET(ret != HDF_SUCCESS, OSAL_FW_RELEASE);
387     ret = OsalReadFirmware(&fw, &block);
388     UT_TEST_CHECK_RET(ret == HDF_SUCCESS, OSAL_FW_READ_AFTER_RELEASE);
389     HDF_LOGE("[OSAL_UT_TEST]%s end", __func__);
390 }
391 
392 #define THREAD_TEST_TIMER_RUN 20
393 #define THREAD_TEST_TIMER_STOP 25
394 
OsaCheckRun(int cnt)395 static void OsaCheckRun(int cnt)
396 {
397     OsalTimespec diffTime = { 0, 0 };
398 
399     if (cnt == THREAD_TEST_TIMER_RUN) {
400         UT_TEST_CHECK_RET(g_timerOnceRunCnt == 0, OSAL_TIMER_RUN_CHECK);
401         UT_TEST_CHECK_RET(!g_timerLoop2RunFlag, OSAL_TIMER_RUN_CHECK);
402         UT_TEST_CHECK_RET(!g_timerLoop1RunFlag, OSAL_TIMER_RUN_CHECK);
403         HDF_LOGI("[OSAL_UT_TEST]%s timer run end", __func__);
404     }
405 
406     if (cnt == THREAD_TEST_TIMER_STOP) {
407         UT_TEST_CHECK_RET(g_timerOnceRunCnt != 1, OSAL_TIMER_STOP_CHECK);
408         UT_TEST_CHECK_RET(g_timerLoop2RunFlag, OSAL_TIMER_STOP_CHECK);
409         HDF_LOGI("[OSAL_UT_TEST]%s timer stop end", __func__);
410     }
411     if (cnt == THREAD_TEST_TIMER_STOP) {
412         OsalGetTime(&g_hdfTestEnd);
413         OsalDiffTime(&g_hdfTestBegin, &g_hdfTestEnd, &diffTime);
414         HDF_LOGI("[OSAL_UT_TEST]%s **** All case test end, use %ds****", __func__, (uint32_t)diffTime.sec);
415         HDF_LOGI("[OSAL_UT_TEST]%s ***************************", __func__);
416     }
417 }
418 static uint8_t g_irqData = 100;
IRQHandle(uint32_t irqId,void * data)419 static uint32_t IRQHandle(uint32_t irqId, void *data)
420 {
421     uint8_t *p = data;
422     static OsalTimespec hdfTs = { 0, 0 };
423     OsalTimespec hdfTs2 = { 0, 0 };
424     OsalTimespec hdfTsDiff = { 0, 0 };
425     if (p == NULL) {
426         HDF_LOGE("%s nul ptr", __func__);
427         return 1;
428     }
429 
430     OsalGetTime(&hdfTs2);
431     OsalDiffTime(&hdfTs, &hdfTs2, &hdfTsDiff);
432 
433     HDF_LOGE("%s IRQ handle, irqId:%u, data:%u, %ds %dus", __func__, irqId, *p,
434         (int32_t)(hdfTsDiff.sec), (int32_t)(hdfTsDiff.usec));
435     OsalGetTime(&hdfTs);
436 
437     return 0;
438 }
439 
440 #define IRQ_TEST_CNT 10
OsaIrqTest(void)441 static void OsaIrqTest(void)
442 {
443     int i = 0;
444     int32_t ret;
445     int32_t retFlag = HDF_FAILURE;
446 #if defined(__LITEOS__)
447     retFlag = HDF_SUCCESS;
448 #endif
449 
450     HDF_LOGI("[OSAL_UT_TEST]%s start", __func__);
451 #if defined(__LITEOS__)
452     ret = OsalUnregisterIrq(IRQ_NUM_TEST, &g_irqData);
453     UT_TEST_CHECK_RET(ret == retFlag, OSAL_IRQ_UNREG_ABNORMAL);
454 #endif
455     ret = OsalRegisterIrq(IRQ_NUM_TEST, 0, IRQHandle, "test", &g_irqData);
456     UT_TEST_CHECK_RET(ret != retFlag, OSAL_IRQ_REG_NORMAL);
457     ret = OsalDisableIrq(IRQ_NUM_TEST);
458     UT_TEST_CHECK_RET(ret != HDF_SUCCESS, OSAL_IRQ_REG_NORMAL);
459     ret = OsalEnableIrq(IRQ_NUM_TEST);
460     UT_TEST_CHECK_RET(ret != HDF_SUCCESS, OSAL_IRQ_REG_NORMAL);
461 #if defined(__LITEOS__)
462     ret = OsalUnregisterIrq(IRQ_NUM_TEST, &g_irqData);
463     UT_TEST_CHECK_RET(ret != retFlag, OSAL_IRQ_UNREG_NORMAL);
464 #endif
465 
466     ret = OsalDisableIrq(IRQ_NUM_TEST);
467     UT_TEST_CHECK_RET(ret != HDF_SUCCESS, OSAL_IRQ_REG_NORMAL);
468 
469     ret = OsalEnableIrq(IRQ_NUM_TEST);
470     UT_TEST_CHECK_RET(ret != HDF_SUCCESS, OSAL_IRQ_REG_NORMAL);
471 
472 #if defined(__LITEOS__)
473     ret = OsalUnregisterIrq(IRQ_NUM_TEST, &g_irqData);
474     UT_TEST_CHECK_RET(ret == retFlag, OSAL_IRQ_UNREG_ABNORMAL);
475 #endif
476 
477     for (; i < IRQ_TEST_CNT; i++) {
478         ret = OsalRegisterIrq(IRQ_NUM_TEST, 0, IRQHandle, "test", &g_irqData);
479         UT_TEST_CHECK_RET(ret != retFlag, OSAL_IRQ_REG_NORMAL_STRESS);
480         ret = OsalDisableIrq(IRQ_NUM_TEST);
481         UT_TEST_CHECK_RET(ret != HDF_SUCCESS, OSAL_IRQ_REG_NORMAL_STRESS);
482         ret = OsalEnableIrq(IRQ_NUM_TEST);
483         UT_TEST_CHECK_RET(ret != HDF_SUCCESS, OSAL_IRQ_REG_NORMAL_STRESS);
484 #if defined(__LITEOS__)
485         ret = OsalUnregisterIrq(IRQ_NUM_TEST, &g_irqData);
486         UT_TEST_CHECK_RET(ret != retFlag, OSAL_IRQ_UNREG_NORMAL_STRESS);
487 #endif
488     }
489 
490     HDF_LOGI("[OSAL_UT_TEST]%s end", __func__);
491 }
492 
493 #define IRQ_TEST_REG 1
494 #define IRQ_TEST_ENABLE 10
495 #define IRQ_TEST_DISABLE 20
496 #define IRQ_TEST_UNREG 30
497 #define THREAD_TEST_STOP_TIMER 25
498 #define THREAD_TEST_STOP_TIMER_CHECK 45
499 #endif
500 
501 #define THREAD_TEST_DBG_CTRL 200
502 #define THREAD_TEST_MUX_BEGIN 3
503 #define THREAD_TEST_MUX_END 5
504 #define THREAD_TEST_SLEEP_MS 1
505 static bool g_thread3RunFlag;
506 static bool g_threadTestFlag = true;
ThreadTest(void * arg)507 static int ThreadTest(void *arg)
508 {
509     static int cnt = 0;
510     HDF_STATUS ret;
511     HDF_LOGI("in threadTest %s %d", __func__, __LINE__);
512 
513     g_thread3RunFlag = true;
514     (void)arg;
515 
516     while (g_threadTestFlag) {
517         OsalSleep(THREAD_TEST_SLEEP_MS);
518 
519         if (cnt < THREAD_TEST_DBG_CTRL && cnt % HDF_DBG_CNT_CTRL == 0) {
520             HDF_LOGI("in threadTest %d", cnt);
521         }
522         cnt++;
523 
524         if (cnt == THREAD_TEST_MUX_BEGIN) {
525             HDF_LOGE("%s mutex Lock", __func__);
526             g_threadMuxLockFlag = true;
527             ret = OsalMutexTimedLock(&g_mutexTest, g_waitMutexTime);
528             UT_TEST_CHECK_RET(ret != HDF_SUCCESS, OSAL_MUTEX_STRESS_TEST);
529         }
530         if (cnt == THREAD_TEST_MUX_END) {
531             ret = OsalMutexUnlock(&g_mutexTest);
532             UT_TEST_CHECK_RET(ret != HDF_SUCCESS, OSAL_MUTEX_STRESS_TEST);
533             HDF_LOGE("%s mutex unLock", __func__);
534             g_threadMuxLockFlag = false;
535         }
536 #ifndef __USER__
537         if (cnt == THREAD_TEST_STOP_TIMER) {
538             OsaTimerTestStop();
539         }
540         OsaCheckRun(cnt);
541 #endif
542         if (g_testEndFlag) {
543             break;
544         }
545     }
546     HDF_LOGE("%s thread return\n", __func__);
547     return 0;
548 }
549 
550 #define HDF_TEST_STACK_SIZE (1024 * 16) // 16KB
OsaThreadTest1(void)551 static void OsaThreadTest1(void)
552 {
553     struct OsalThreadParam threadCfg;
554     static int para = 120;
555     int32_t ret;
556 
557     (void)memset_s(&threadCfg, sizeof(threadCfg), 0, sizeof(threadCfg));
558     threadCfg.name = "hdf_test0";
559     threadCfg.priority = OSAL_THREAD_PRI_HIGHEST;
560     threadCfg.stackSize = HDF_TEST_STACK_SIZE;
561     ret = OsalThreadCreate(&thread, (OsalThreadEntry)ThreadTest, (void *)&para);
562     UT_TEST_CHECK_RET(ret != HDF_SUCCESS, OSAL_THREAD_CREATE);
563     ret = OsalThreadStart(&thread, &threadCfg);
564     UT_TEST_CHECK_RET(ret != HDF_SUCCESS, OSAL_THREAD_CREATE);
565 }
566 
OsaThreadTest(void)567 static void OsaThreadTest(void)
568 {
569     struct OsalThreadParam threadCfg;
570     int ret;
571 
572     HDF_LOGI("[OSAL_UT_TEST]%s start", __func__);
573     ret = OsalMutexInit(&g_mutexTest);
574     UT_TEST_CHECK_RET(ret != HDF_SUCCESS, OSAL_MUTEX_CREATE);
575     ret = OsalSpinInit(&g_spinTest);
576     UT_TEST_CHECK_RET(ret != HDF_SUCCESS, OSAL_SPIN_CREATE);
577 
578     (void)memset_s(&threadCfg, sizeof(threadCfg), 0, sizeof(threadCfg));
579     threadCfg.name = "hdf_test1";
580     threadCfg.priority = OSAL_THREAD_PRI_HIGH;
581     threadCfg.stackSize = HDF_TEST_STACK_SIZE;
582     ret = OsalThreadCreate(&thread1, (OsalThreadEntry)ThreadTest1, (void *)&g_test1Para);
583     UT_TEST_CHECK_RET(ret != HDF_SUCCESS, OSAL_THREAD_CREATE);
584     ret = OsalThreadStart(&thread1, &threadCfg);
585     UT_TEST_CHECK_RET(ret != HDF_SUCCESS, OSAL_THREAD_CREATE);
586 
587     OsalMSleep(HDF_THREAD_TEST_SLEEP_S);
588 
589     (void)memset_s(&threadCfg, sizeof(threadCfg), 0, sizeof(threadCfg));
590     threadCfg.name = "hdf_test2";
591     threadCfg.priority = OSAL_THREAD_PRI_DEFAULT;
592     threadCfg.stackSize = HDF_TEST_STACK_SIZE;
593     ret = OsalThreadCreate(&thread2, (OsalThreadEntry)ThreadTest2, (void *)&g_test2Para);
594     UT_TEST_CHECK_RET(ret != HDF_SUCCESS, OSAL_THREAD_CREATE);
595     ret = OsalThreadStart(&thread2, &threadCfg);
596     UT_TEST_CHECK_RET(ret != HDF_SUCCESS, OSAL_THREAD_CREATE);
597 
598     OsaThreadTest1();
599     HDF_LOGI("[OSAL_UT_TEST]%s end", __func__);
600 }
601 
602 #define TIME_TEST_SLEEP_S 2
603 #define TIME_TEST_SLEEP_MS 200
604 #define TIME_TEST_SLEEP_US 200
605 #define TIME_TEST_MS_RANGE_H 210
606 #define TIME_TEST_MS_RANGE_L 190
607 #define TIME_TEST_US_RANGE_H 1000
608 #define TIME_TEST_US_RANGE_L 200
OsaTimeTest(void)609 static void OsaTimeTest(void)
610 {
611     int32_t ret;
612     OsalTimespec hdfTs = { 0, 0 };
613     OsalTimespec hdfTs2 = { 0, 0 };
614     OsalTimespec hdfTsDiff = { 0, 0 };
615 
616     OsalGetTime(&hdfTs);
617     OsalSleep(TIME_TEST_SLEEP_S);
618     ret = OsalGetTime(&hdfTs2);
619     UT_TEST_CHECK_RET(ret != HDF_SUCCESS, OSAL_TIME_GETTIME);
620     ret = OsalDiffTime(&hdfTs, &hdfTs2, &hdfTsDiff);
621     HDF_LOGE("%s %us %uus", __func__, (uint32_t)hdfTsDiff.sec, (uint32_t)hdfTsDiff.usec);
622     UT_TEST_CHECK_RET(!OsalCheckTime(&hdfTsDiff, TIME_TEST_SLEEP_S * HDF_KILO_UNIT), OSAL_TIME_DIFFTIME);
623 
624     OsalGetTime(&hdfTs);
625     OsalUDelay(TIME_TEST_SLEEP_MS);
626     (void)OsalGetTime(&hdfTs2);
627     (void)OsalDiffTime(&hdfTs, &hdfTs2, &hdfTsDiff);
628     HDF_LOGE("%s %us %uus", __func__, (uint32_t)hdfTsDiff.sec, (uint32_t)hdfTsDiff.usec);
629 
630     OsalGetTime(&hdfTs);
631     OsalMDelay(TIME_TEST_SLEEP_MS);
632     (void)OsalGetTime(&hdfTs2);
633     (void)OsalDiffTime(&hdfTs, &hdfTs2, &hdfTsDiff);
634     HDF_LOGE("%s %us %uus", __func__, (uint32_t)hdfTsDiff.sec, (uint32_t)hdfTsDiff.usec);
635 
636     OsalGetTime(&hdfTs);
637     OsalMSleep(TIME_TEST_SLEEP_MS);
638     ret = OsalGetTime(&hdfTs2);
639     UT_TEST_CHECK_RET(ret != HDF_SUCCESS, OSAL_TIME_GETTIME);
640     ret = OsalDiffTime(&hdfTs, &hdfTs2, &hdfTsDiff);
641     HDF_LOGE("%s %us %uus", __func__, (uint32_t)hdfTsDiff.sec, (uint32_t)hdfTsDiff.usec);
642     UT_TEST_CHECK_RET(ret != HDF_SUCCESS, OSAL_TIME_DIFFTIME);
643     UT_TEST_CHECK_RET(!OsalCheckTime(&hdfTsDiff, TIME_TEST_SLEEP_MS), OSAL_TIME_DIFFTIME);
644 }
645 enum {
646     MEM_ALIGN_TEST_0 = 1,
647     MEM_ALIGN_TEST_1 = 2,
648     MEM_ALIGN_TEST_2 = 3,
649     MEM_ALIGN_TEST_3 = 4,
650     MEM_ALIGN_TEST_4 = 7,
651     MEM_ALIGN_TEST_5 = 8,
652     MEM_ALIGN_TEST_6 = 16,
653 };
654 #define MALLOC_TEST_CASE1_SIZE 0x4000
655 #define MALLOC_TEST_CASE2_SIZE 0x1FF
656 #define MALLOC_TEST_CNT 1000
OsaMemoryTest(void)657 static void OsaMemoryTest(void)
658 {
659     void *buf = NULL;
660     int i = 0;
661 
662     HDF_LOGI("[OSAL_UT_TEST]%s start", __func__);
663     buf = OsalMemCalloc(MALLOC_TEST_CASE1_SIZE);
664     UT_TEST_CHECK_RET(buf == NULL, OSAL_MALLOC_BIG);
665     OsalMemFree(buf);
666 
667     buf = OsalMemCalloc(MALLOC_TEST_CASE2_SIZE);
668     UT_TEST_CHECK_RET(buf == NULL, OSAL_MALLOC_SMALL);
669     OsalMemFree(buf);
670 
671     buf = OsalMemAllocAlign(MEM_ALIGN_TEST_0, MALLOC_TEST_CASE1_SIZE);
672     UT_TEST_CHECK_RET(buf != NULL, OSAL_MALLOC_BIG);
673     OsalMemFree(buf);
674     buf = OsalMemAllocAlign(MEM_ALIGN_TEST_1, MALLOC_TEST_CASE1_SIZE);
675     UT_TEST_CHECK_RET(buf != NULL, OSAL_MALLOC_BIG);
676     OsalMemFree(buf);
677     buf = OsalMemAllocAlign(MEM_ALIGN_TEST_2, MALLOC_TEST_CASE1_SIZE);
678     UT_TEST_CHECK_RET(buf != NULL, OSAL_MALLOC_BIG);
679     OsalMemFree(buf);
680     buf = OsalMemAllocAlign(MEM_ALIGN_TEST_3, MALLOC_TEST_CASE1_SIZE);
681     if (sizeof(void *) == MEM_ALIGN_TEST_3) {
682         UT_TEST_CHECK_RET(buf == NULL, OSAL_MALLOC_BIG);
683     } else {
684         UT_TEST_CHECK_RET(buf != NULL, OSAL_MALLOC_BIG);
685     }
686     OsalMemFree(buf);
687     buf = OsalMemAllocAlign(MEM_ALIGN_TEST_4, MALLOC_TEST_CASE1_SIZE);
688     UT_TEST_CHECK_RET(buf != NULL, OSAL_MALLOC_BIG);
689     OsalMemFree(buf);
690     buf = OsalMemAllocAlign(MEM_ALIGN_TEST_5, MALLOC_TEST_CASE1_SIZE);
691     UT_TEST_CHECK_RET(buf == NULL, OSAL_MALLOC_BIG);
692     OsalMemFree(buf);
693     buf = OsalMemAllocAlign(MEM_ALIGN_TEST_6, MALLOC_TEST_CASE1_SIZE);
694     UT_TEST_CHECK_RET(buf == NULL, OSAL_MALLOC_BIG);
695     OsalMemFree(buf);
696 
697     for (; i < MALLOC_TEST_CNT; i++) {
698         buf = OsalMemCalloc(MALLOC_TEST_CASE1_SIZE);
699         UT_TEST_CHECK_RET(buf == NULL, OSAL_MALLOC_BIG_STRESS);
700         OsalMemFree(buf);
701 
702         buf = OsalMemCalloc(MALLOC_TEST_CASE2_SIZE);
703         UT_TEST_CHECK_RET(buf == NULL, OSAL_MALLOC_SMALL_STRESS);
704         OsalMemFree(buf);
705     }
706     HDF_LOGI("[OSAL_UT_TEST]%s end", __func__);
707 }
708 
OsaLogTest(void)709 static void OsaLogTest(void)
710 {
711     HDF_LOGI("[OSAL_UT_TEST]%s start", __func__);
712     HDF_LOGE("{private}%{private}s %{private}d{private}", __func__, __LINE__);
713     HDF_LOGW("%{public}s %{public}d", __func__, __LINE__);
714     HDF_LOGI("%{public}s %{private}d", __func__, __LINE__);
715     HDF_LOGI("%{private}s %{public}d", __func__, __LINE__);
716     HDF_LOGD("%s", __func__);
717     HDF_LOGV("%s", __func__);
718     HDF_LOGI("[OSAL_UT_TEST]%{private}s end", __func__);
719     HDF_LOGI("[OSAL_UT_TEST]%s end", __func__);
720 }
721 
OsaMutexTest(void)722 static void OsaMutexTest(void)
723 {
724     HDF_STATUS ret;
725     struct OsalMutex mutex;
726 
727     HDF_LOGE("[OSAL_UT_TEST]%s start", __func__);
728 
729     (void)memset_s(&mutex, sizeof(mutex), 0, sizeof(mutex));
730     ret = OsalMutexInit(&mutex);
731     UT_TEST_CHECK_RET(ret != HDF_SUCCESS, OSAL_MUTEX_CREATE);
732     ret = OsalMutexTimedLock(&mutex, OSAL_TEST_TIME_DEFAULT);
733     UT_TEST_CHECK_RET(ret != HDF_SUCCESS, OSAL_MUTEX_LOCK_TIMEOUT);
734     ret = OsalMutexUnlock(&mutex);
735     UT_TEST_CHECK_RET(ret != HDF_SUCCESS, OSAL_MUTEX_UNLOCK);
736     ret = OsalMutexLock(&mutex);
737     UT_TEST_CHECK_RET(ret != HDF_SUCCESS, OSAL_MUTEX_LOCK_FOREVER);
738     ret = OsalMutexUnlock(&mutex);
739     UT_TEST_CHECK_RET(ret != HDF_SUCCESS, OSAL_MUTEX_UNLOCK);
740     ret = OsalMutexDestroy(&mutex);
741     UT_TEST_CHECK_RET(ret != HDF_SUCCESS, OSAL_MUTEX_DESTROY);
742 
743     HDF_LOGE("[OSAL_UT_TEST]%s test no init", __func__);
744     ret = OsalMutexTimedLock(&mutex, HDF_WAIT_FOREVER);
745     UT_TEST_CHECK_RET(ret == HDF_SUCCESS, OSAL_MUTEX_VISIT_DESTROY);
746     ret = OsalMutexUnlock(&mutex);
747     UT_TEST_CHECK_RET(ret == HDF_SUCCESS, OSAL_MUTEX_VISIT_DESTROY);
748     ret = OsalMutexDestroy(&mutex);
749     UT_TEST_CHECK_RET(ret == HDF_SUCCESS, OSAL_MUTEX_VISIT_DESTROY);
750 
751     HDF_LOGI("[OSAL_UT_TEST]%s end", __func__);
752 }
753 
OsaSpinTest(void)754 static void OsaSpinTest(void)
755 {
756     int32_t ret;
757 #ifndef __USER__
758     uint32_t flag = 0;
759 #endif
760     OSAL_DECLARE_SPINLOCK(spin);
761 
762     HDF_LOGE("[OSAL_UT_TEST]%s start", __func__);
763     (void)memset_s(&spin, sizeof(spin), 0, sizeof(spin));
764     ret = OsalSpinInit(&spin);
765     UT_TEST_CHECK_RET(ret != HDF_SUCCESS, OSAL_SPIN_CREATE);
766 
767     ret = OsalSpinLock(&spin);
768     UT_TEST_CHECK_RET(ret != HDF_SUCCESS, OSAL_SPIN_LOCK);
769     ret = OsalSpinUnlock(&spin);
770     UT_TEST_CHECK_RET(ret != HDF_SUCCESS, OSAL_SPIN_UNLOCK);
771 #ifndef __USER__
772     ret = OsalSpinLockIrq(&spin);
773     UT_TEST_CHECK_RET(ret != HDF_SUCCESS, OSAL_SPIN_LOCK_IRQ);
774     ret = OsalSpinUnlockIrq(&spin);
775     UT_TEST_CHECK_RET(ret != HDF_SUCCESS, OSAL_SPIN_UNLOCK_IRQ);
776 
777     ret = OsalSpinLockIrqSave(&spin, &flag);
778     UT_TEST_CHECK_RET(ret != HDF_SUCCESS, OSAL_SPIN_LOCK_IRQ_SAVE);
779     ret = OsalSpinUnlockIrqRestore(&spin, &flag);
780     UT_TEST_CHECK_RET(ret != HDF_SUCCESS, OSAL_SPIN_UNLOCK_IRQ_RESTORE);
781 #endif
782     ret = OsalSpinDestroy(&spin);
783     UT_TEST_CHECK_RET(ret != HDF_SUCCESS, OSAL_SPIN_DESTROY);
784 
785     HDF_LOGE("[OSAL_UT_TEST]%s test no init", __func__);
786     ret = OsalSpinLock(&spin);
787     UT_TEST_CHECK_RET(ret == HDF_SUCCESS, OSAL_SPIN_LOCK_AFTER_DESTROY);
788     ret = OsalSpinUnlock(&spin);
789     UT_TEST_CHECK_RET(ret == HDF_SUCCESS, OSAL_SPIN_LOCK_AFTER_DESTROY);
790     ret = OsalSpinDestroy(&spin);
791     UT_TEST_CHECK_RET(ret == HDF_SUCCESS, OSAL_SPIN_LOCK_AFTER_DESTROY);
792 
793     HDF_LOGI("[OSAL_UT_TEST]%s end", __func__);
794 }
795 
OsaCheckThreadRun(void)796 static void OsaCheckThreadRun(void)
797 {
798     UT_TEST_CHECK_RET(!g_thread1RunFlag, OSAL_THREAD_RUN_CHECK);
799     UT_TEST_CHECK_RET(!g_thread2RunFlag, OSAL_THREAD_RUN_CHECK);
800     UT_TEST_CHECK_RET(!g_thread3RunFlag, OSAL_THREAD_RUN_CHECK);
801     HDF_LOGI("[OSAL_UT_TEST]%s end", __func__);
802 }
803 
804 #define HDF_MAIN_SLEEP_S 20
OsaTestBegin(void)805 int OsaTestBegin(void)
806 {
807     int ret;
808     (void)memset_s(g_osalTestCases, sizeof(g_osalTestCases), 0, sizeof(g_osalTestCases));
809     g_testEndFlag = false;
810 #ifndef __USER__
811     g_timer1Cnt = 0;
812     g_timer2Cnt = 0;
813 #endif
814     OsalGetTime(&g_hdfTestBegin);
815     OsaLogTest();
816 #ifndef __USER__
817     ret = OsalTestFileInit();
818 #else
819     ret = 0;
820 #endif
821     OsalTestOther(ret);
822     OsaTimeTest();
823     OsaMutexTest();
824     OsaSpinTest();
825 #ifndef __USER__
826     OsaIrqTest();
827     OsaTimerTest();
828 #endif
829     OsaThreadTest();
830     OsaMemoryTest();
831     HDF_LOGD("%s ", __func__);
832 #ifndef __USER__
833     OsaFWTest(ret);
834     OsalTestFileDeInit();
835 #endif
836     OsalSleep(HDF_MAIN_SLEEP_S);
837     HDF_LOGI("%s", __func__);
838     OsaCheckThreadRun();
839 
840     return 0;
841 }
842 
OsaTestEnd(void)843 int OsaTestEnd(void)
844 {
845 #ifndef __USER__
846     OsalTimerDelete(&g_testTimerLoop1);
847     OsalTimerDelete(&g_testTimerLoop2);
848     OsalTimerDelete(&g_testTimerOnce);
849     OsalStopThread();
850 #endif
851     g_testEndFlag = true;
852     OsalThreadDestroy(&thread1);
853     OsalThreadDestroy(&thread);
854     OsalThreadDestroy(&thread2);
855     HDF_LOGE("%s", __func__);
856 
857     return 0;
858 }
859 
OsaTestALLResult(void)860 int OsaTestALLResult(void)
861 {
862     int index;
863     for (index = 0; index < OSAL_TEST_CASE_CNT; index++) {
864         if (g_osalTestCases[index] != 0) {
865             HDF_LOGE("%s %d %d", __func__, g_osalTestCases[index], index);
866             return 1;
867         }
868     }
869     return 0;
870 }
871 
872