1 /*
2 * This file is part of the openHiTLS project.
3 *
4 * openHiTLS is licensed under the Mulan PSL v2.
5 * You can use this software according to the terms and conditions of the Mulan PSL v2.
6 * You may obtain a copy of Mulan PSL v2 at:
7 *
8 * http://license.coscl.org.cn/MulanPSL2
9 *
10 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13 * See the Mulan PSL v2 for more details.
14 */
15
16 /* BEGIN_HEADER */
17
18 #include <sys/types.h>
19 #include <unistd.h>
20 #include <pthread.h>
21 #include "securec.h"
22 #include "bsl_sal.h"
23 #include "bsl_errno.h"
24 #include "sal_atomic.h"
25
26 #define TEST_THREAD_DEFAULT_TC001_WRITE_CNT 100000
27 #define TEST_WRITE_PID_CNT 2
28 #define TEST_READ_PID_CNT 2
29
30 int g_threadDefaultWrite001 = 0;
31 int g_threadDefaultRead001 = 0;
32 uint64_t g_threadDefaultId001 = 0;
33
StdMalloc(uint32_t len)34 static void *StdMalloc(uint32_t len)
35 {
36 return malloc((size_t)len);
37 }
38
pthreadRWLockNew(BSL_SAL_ThreadLockHandle * lock)39 static int32_t pthreadRWLockNew(BSL_SAL_ThreadLockHandle *lock)
40 {
41 if (lock == NULL) {
42 return BSL_SAL_ERR_BAD_PARAM;
43 }
44 pthread_rwlock_t *newLock;
45 newLock = (pthread_rwlock_t *)BSL_SAL_Malloc(sizeof(pthread_rwlock_t));
46 if (newLock == NULL) {
47 return BSL_MALLOC_FAIL;
48 }
49 if (pthread_rwlock_init(newLock, NULL) != 0) {
50 return BSL_SAL_ERR_UNKNOWN;
51 }
52 *lock = newLock;
53 return BSL_SUCCESS;
54 }
55
pthreadRWLockFree(BSL_SAL_ThreadLockHandle lock)56 static void pthreadRWLockFree(BSL_SAL_ThreadLockHandle lock)
57 {
58 if (lock == NULL) {
59 return;
60 }
61 pthread_rwlock_destroy((pthread_rwlock_t *)lock);
62 BSL_SAL_FREE(lock);
63 }
64
pthreadRWLockReadLock(BSL_SAL_ThreadLockHandle lock)65 static int32_t pthreadRWLockReadLock(BSL_SAL_ThreadLockHandle lock)
66 {
67 if (lock == NULL) {
68 return BSL_SAL_ERR_BAD_PARAM;
69 }
70 if (pthread_rwlock_rdlock((pthread_rwlock_t *)lock) != 0) {
71 return BSL_SAL_ERR_UNKNOWN;
72 }
73 return BSL_SUCCESS;
74 }
75
pthreadRWLockWriteLock(BSL_SAL_ThreadLockHandle lock)76 static int32_t pthreadRWLockWriteLock(BSL_SAL_ThreadLockHandle lock)
77 {
78 if (lock == NULL) {
79 return BSL_SAL_ERR_BAD_PARAM;
80 }
81 if (pthread_rwlock_wrlock((pthread_rwlock_t *)lock) != 0) {
82 return BSL_SAL_ERR_UNKNOWN;
83 }
84 return BSL_SUCCESS;
85 }
86
pthreadRWLockUnlock(BSL_SAL_ThreadLockHandle lock)87 static int32_t pthreadRWLockUnlock(BSL_SAL_ThreadLockHandle lock)
88 {
89 if (lock == NULL) {
90 return BSL_SAL_ERR_BAD_PARAM;
91 }
92 if (pthread_rwlock_unlock((pthread_rwlock_t *)lock) != 0) {
93 return BSL_SAL_ERR_UNKNOWN;
94 }
95 return BSL_SUCCESS;
96 }
97
pthreadGetId(void)98 static uint64_t pthreadGetId(void)
99 {
100 return (uint64_t)pthread_self();
101 }
102
103 #ifdef HITLS_BSL_SAL_THREAD
TEST_Read(void * arg)104 static void *TEST_Read(void *arg)
105 {
106 BSL_SAL_ThreadLockHandle lock = (BSL_SAL_ThreadLockHandle)arg;
107 int32_t ret = BSL_SAL_ThreadReadLock(lock);
108 g_threadDefaultRead001 = g_threadDefaultWrite001;
109 BSL_SAL_ThreadUnlock(lock);
110 ASSERT_TRUE(ret == BSL_SUCCESS);
111 EXIT:
112 return NULL;
113 }
114
TEST_Write(void * arg)115 static void *TEST_Write(void *arg)
116 {
117 BSL_SAL_ThreadLockHandle lock = (BSL_SAL_ThreadLockHandle)arg;
118 int32_t ret = BSL_SUCCESS;
119 for (size_t i = 0; i < TEST_THREAD_DEFAULT_TC001_WRITE_CNT; i++) {
120 if (BSL_SAL_ThreadWriteLock(lock) != BSL_SUCCESS) {
121 ret = BSL_SAL_ERR_UNKNOWN;
122 }
123 g_threadDefaultWrite001++;
124 g_threadDefaultId001 = BSL_SAL_ThreadGetId();
125 BSL_SAL_ThreadUnlock(lock);
126 }
127
128 ASSERT_TRUE(ret == BSL_SUCCESS);
129 EXIT:
130 return NULL;
131 }
132 #endif
133
134 /* END_HEADER */
135
136 /**
137 * @test SDV_BSL_SAL_REGMEM_API_TC001
138 * @title Registering memory-related functions
139 * @precon nan
140 * @brief
141 * 1. Call BSL_SAL_Malloc to allocate 0-byte space. Expected result 1 is obtained.
142 * 2. Call BSL_SAL_Malloc to allocate 1-byte space. Expected result 2 is obtained.
143 * 3. Call BSL_SAL_Calloc to allocate a large memory space. Expected result 3 is obtained.
144 * 4. Call BSL_SAL_CallBack_Ctrl to transfer an exception parameter. Expected result 4 is obtained.
145 * 5. Call BSL_SAL_CallBack_Ctrl to transfer an normal parameter. Expected result 5 is obtained.
146 * 6. Call BSL_SAL_Malloc to allocate 8-byte space. Expected result 6 is obtained.
147 * 7. Call BSL_SAL_FREE to free 8-byte space. Expected result 7 is obtained.
148 * @expect
149 * 1. Failed to apply for the memory. NULL is returned.
150 * 2. Memory application succeeded.
151 * 3. Failed to apply for the memory. NULL is returned.
152 * 4. Failed to register the callback, return BSL_SAL_ERR_BAD_PARAM
153 * 5. Registration callback succeeded, return BSL_SUCCESS.
154 * 6. Memory application succeeded.
155 * 7. The memory is released successfully.
156 */
157 /* BEGIN_CASE */
SDV_BSL_SAL_REGMEM_API_TC001(void)158 void SDV_BSL_SAL_REGMEM_API_TC001(void)
159 {
160 void *ptr = NULL;
161
162 ptr = BSL_SAL_Malloc(0);
163 ASSERT_TRUE(ptr == NULL);
164 #ifdef HITLS_BSL_SAL_MEM
165 ptr = BSL_SAL_Malloc(1);
166 ASSERT_TRUE(ptr != NULL);
167 BSL_SAL_FREE(ptr);
168 #endif
169
170 ptr = BSL_SAL_Calloc(0xFFFFFFFF, 0xFFFFFFFF);
171 ASSERT_TRUE(ptr == NULL);
172
173 ASSERT_TRUE(BSL_SAL_CallBack_Ctrl(0, NULL) == BSL_SAL_ERR_BAD_PARAM);
174
175 ASSERT_TRUE(BSL_SAL_CallBack_Ctrl(BSL_SAL_MEM_MALLOC, NULL) == BSL_SUCCESS);
176
177 ASSERT_TRUE(BSL_SAL_CallBack_Ctrl(BSL_SAL_MEM_MALLOC, StdMalloc) == BSL_SUCCESS);
178 ASSERT_TRUE(BSL_SAL_CallBack_Ctrl(BSL_SAL_MEM_FREE, free) == BSL_SUCCESS);
179
180 ptr = BSL_SAL_Malloc(0);
181 ASSERT_TRUE(ptr != NULL);
182
183 BSL_SAL_FREE(ptr);
184 ASSERT_TRUE(ptr == NULL);
185 EXIT:
186 return;
187 }
188 /* END_CASE */
189
190 /**
191 * @test SDV_BSL_SAL_REG_THREAD_API_TC001
192 * @title Register thread-related functions.
193 * @precon nan
194 * @brief
195 * 1. Call BSL_SAL_CallBack_Ctrl to transfer an exception parameter. Expected result 1 is obtained.
196 * 2. Call BSL_SAL_CallBack_Ctrl to transfer an normal parameter. Expected result 2 is obtained.
197 * 3. Call BSL_SAL_ThreadLockNew to transfer an exception parameter. Expected result 3 is obtained.
198 * 4. Call BSL_SAL_ThreadReadLock to transfer an exception parameter. Expected result 4 is obtained.
199 * 5. Call BSL_SAL_ThreadWriteLock to transfer an exception parameter. Expected result 5 is obtained.
200 * 6. Call BSL_SAL_ThreadUnlock to transfer an exception parameter. Expected result 6 is obtained.
201 * 7. Call BSL_SAL_ThreadLockFree to transfer an exception parameter. Expected result 7 is obtained.
202 * @expect
203 * 1. Failed to register the callback, return BSL_SAL_ERR_BAD_PARAM
204 * 2. Registration callback succeeded, return BSL_SUCCESS.
205 * 9. Failed to create the new lock, return BSL_SAL_ERR_BAD_PARAM
206 * 10. Failed to create the read lock, return BSL_SAL_ERR_BAD_PARAM
207 * 11. Failed to create the write lock, return BSL_SAL_ERR_BAD_PARAM
208 * 12. Failed to unlock, return BSL_SAL_ERR_BAD_PARAM
209 * 13. No return value
210 */
211 /* BEGIN_CASE */
SDV_BSL_SAL_REG_THREAD_API_TC001(void)212 void SDV_BSL_SAL_REG_THREAD_API_TC001(void)
213 {
214 ASSERT_TRUE(BSL_SAL_CallBack_Ctrl(0, NULL) == BSL_SAL_ERR_BAD_PARAM);
215
216 ASSERT_TRUE(BSL_SAL_CallBack_Ctrl(0, NULL) == BSL_SAL_ERR_BAD_PARAM);
217
218 ASSERT_TRUE(BSL_SAL_CallBack_Ctrl(BSL_SAL_THREAD_LOCK_NEW_CB_FUNC, pthreadRWLockNew) == BSL_SUCCESS);
219 ASSERT_TRUE(BSL_SAL_CallBack_Ctrl(BSL_SAL_THREAD_LOCK_FREE_CB_FUNC, pthreadRWLockFree) == BSL_SUCCESS);
220 ASSERT_TRUE(BSL_SAL_CallBack_Ctrl(BSL_SAL_THREAD_LOCK_READ_LOCK_CB_FUNC, pthreadRWLockReadLock) == BSL_SUCCESS);
221 ASSERT_TRUE(BSL_SAL_CallBack_Ctrl(BSL_SAL_THREAD_LOCK_WRITE_LOCK_CB_FUNC, pthreadRWLockWriteLock) == BSL_SUCCESS);
222 ASSERT_TRUE(BSL_SAL_CallBack_Ctrl(BSL_SAL_THREAD_LOCK_UNLOCK_CB_FUNC, pthreadRWLockUnlock) == BSL_SUCCESS);
223 ASSERT_TRUE(BSL_SAL_CallBack_Ctrl(BSL_SAL_THREAD_GET_ID_CB_FUNC, pthreadGetId) == BSL_SUCCESS);
224
225 // Cannot create a lock handle because the pointer of the pointer is NULL.
226 ASSERT_TRUE(BSL_SAL_ThreadLockNew(NULL) == BSL_SAL_ERR_BAD_PARAM);
227 ASSERT_TRUE(BSL_SAL_ThreadReadLock(NULL) == BSL_SAL_ERR_BAD_PARAM);
228 ASSERT_TRUE(BSL_SAL_ThreadWriteLock(NULL) == BSL_SAL_ERR_BAD_PARAM);
229 ASSERT_TRUE(BSL_SAL_ThreadUnlock(NULL) == BSL_SAL_ERR_BAD_PARAM);
230 BSL_SAL_ThreadLockFree(NULL);
231 EXIT:
232 return;
233 }
234 /* END_CASE */
235
236 /**
237 * @test SDV_BSL_SAL_MEM_API_TC001
238 * @title Test the memory application function when the memory registration callback function is not invoked.
239 * @precon nan
240 * @brief
241 * 1. Call BSL_SAL_Malloc to allocate 100-byte space. Expected result 1 is obtained.
242 * 2. Call BSL_SAL_ClearFree to free 100-byte space. Expected result 2 is obtained.
243 * @expect
244 * 1. Memory application succeeded.
245 * 2. The memory is released successfully.
246 */
247 /* BEGIN_CASE */
SDV_BSL_SAL_MEM_API_TC001(void)248 void SDV_BSL_SAL_MEM_API_TC001(void)
249 {
250 #ifndef HITLS_BSL_SAL_MEM
251 SKIP_TEST();
252 #else
253 // 1
254 void *obj = BSL_SAL_Malloc(100);
255 ASSERT_TRUE(obj != NULL);
256
257 memset_s(obj, 100, 0x1, 100);
258
259 BSL_SAL_ClearFree(obj, 100);
260 EXIT:
261 return;
262 #endif
263 }
264 /* END_CASE */
265
266 /**
267 * @test SDV_BSL_SAL_MEM_API_TC001
268 * @title Test the memory application function when the memory registration callback function is not invoked.
269 * @precon nan
270 * @brief
271 * 1. Call BSL_SAL_Malloc to allocate 1-byte space. Expected result 1 is obtained.
272 * 2. Call BSL_SAL_Calloc to allocate 1000-byte space. Expected result 2 is obtained.
273 * @expect
274 * 1. Memory application succeeded.
275 * 2. Memory application succeeded.
276 */
277 /* BEGIN_CASE */
SDV_BSL_SAL_MEM_API_TC002(void)278 void SDV_BSL_SAL_MEM_API_TC002(void)
279 {
280 #ifndef HITLS_BSL_SAL_MEM
281 SKIP_TEST();
282 #else
283 // 1
284 void *obj = BSL_SAL_Malloc(1);
285 ASSERT_TRUE(obj != NULL);
286 BSL_SAL_FREE(obj);
287
288 uint8_t objZero3[1000] = {0};
289 uint8_t *obj3 = (uint8_t *)BSL_SAL_Calloc(1000, sizeof(uint8_t));
290 ASSERT_TRUE(obj3 != NULL);
291 ASSERT_TRUE(memcmp(objZero3, obj3, 1000) == 0);
292 BSL_SAL_FREE(obj3);
293 EXIT:
294 return;
295 #endif
296 }
297 /* END_CASE */
298
299 /**
300 * @test SDV_BSL_SAL_DUMP_API_TC001
301 * @title Test the function of the dump interface when the memory-related callback is not registered.
302 * @precon nan
303 * @brief
304 * 1. Call BSL_SAL_Dump with the source memory address set to NULL. Expected result 1 is obtained.
305 * 2. Call BSL_SAL_Dump to set the total memory size to 0. Expected result 2 is obtained.
306 * 3. Call BSL_SAL_Dump interface to transfer normal parameters. Expected result 3 is obtained.
307 * @expect
308 * 1. Failed to duplicate the memory space. Return NULL.
309 * 2. Failed to duplicate the memory space. Return NULL.
310 * 3. Succeeded in duplicate the memory space.
311 */
312 /* BEGIN_CASE */
SDV_BSL_SAL_DUMP_API_TC001(void)313 void SDV_BSL_SAL_DUMP_API_TC001(void)
314 {
315 #ifndef HITLS_BSL_SAL_MEM
316 SKIP_TEST();
317 #else
318 uint32_t memLen = 1024U;
319 void *testPtr = NULL;
320 void *srcPtr = BSL_SAL_Malloc(memLen);
321 ASSERT_TRUE(srcPtr != NULL);
322
323 // 1
324 ASSERT_TRUE(BSL_SAL_Dump(NULL, memLen) == NULL);
325
326 // 2
327 ASSERT_TRUE(BSL_SAL_Dump(srcPtr, 0) == NULL);
328
329 // 3
330 testPtr = BSL_SAL_Dump(srcPtr, memLen);
331 ASSERT_TRUE(testPtr != NULL);
332
333 ASSERT_TRUE(memcmp(testPtr, srcPtr, memLen) == 0);
334 EXIT:
335 BSL_SAL_FREE(srcPtr);
336 BSL_SAL_FREE(testPtr);
337 #endif
338 }
339 /* END_CASE */
340
341 /**
342 * @test SDV_BSL_SAL_REALLOC_API_TC001
343 * @title Test functions related to reallocation when the memory-related callback is not registered.
344 * @precon nan
345 * @brief
346 * 1. The size of the extended memory is smaller than the original size. Expected result 1 is obtained.
347 * 2. The size of the extended memory is smaller than the original size. Expected result 2 is obtained.
348 * @expect
349 * 1. Success. The extended memory address is returned.
350 * 2. Success. The extended memory address is returned. No ASAN alarm is generated for realloc memory read/write.
351 */
352 /* BEGIN_CASE */
SDV_BSL_SAL_REALLOC_API_TC001(void)353 void SDV_BSL_SAL_REALLOC_API_TC001(void)
354 {
355 #ifndef HITLS_BSL_SAL_MEM
356 SKIP_TEST();
357 #else
358 uint32_t originSize = 2000u;
359 uint32_t biggerSize = 3000u;
360 uint32_t smallerSize = 1000u;
361 void *obj = BSL_SAL_Malloc(originSize);
362 ASSERT_TRUE(obj != NULL);
363
364 // 1
365 void *obj2 = BSL_SAL_Realloc(obj, smallerSize, originSize);
366
367 // 2
368 uint8_t *obj3 = (uint8_t *)BSL_SAL_Realloc(obj2, biggerSize, smallerSize);
369 ASSERT_TRUE(obj3 != NULL);
370 ASSERT_TRUE(memset_s(obj3, biggerSize, 1, biggerSize) == EOK);
371 ASSERT_TRUE(obj3[biggerSize - 1] == 1);
372
373 // The realloc releases the obj. Therefore, the obj does not need to be released.
374 // The value of realloc size to 0 is an implementation definition. Therefore, the test is not performed.
375 EXIT:
376 BSL_SAL_FREE(obj3);
377 #endif
378 }
379 /* END_CASE */
380
381 /**
382 * @test SDV_BSL_SAL_THREAD_CREATE_FUNC_TC001
383 * @title Creating and Closing Threads test
384 * @precon nan
385 * @brief
386 * 1. Unregistered thread-related callback and create a thread lock. Expected result 1 is obtained.
387 * 2. Call BSL_SAL_ThreadCreate to transfer abnormal parameters. Expected result 2 is obtained.
388 * 3. Call BSL_SAL_ThreadCreate to transfer normal parameters. Expected result 3 is obtained.
389 * 4. Call BSL_SAL_ThreadClose to transfer normal parameters. Expected result 4 is obtained.
390 * 5. Call BSL_SAL_ThreadClose to transfer abnormal parameters. Expected result 5 is obtained.
391 * 6. Release the lock. Expected result 6 is obtained.
392 * @expect
393 * 1. BSL_SUCCESS
394 * 2. BSL_SAL_ERR_BAD_PARAM
395 * 3. BSL_SUCCESS
396 * 4. The thread is closed successfully.
397 * 5. No return value
398 * 6. Lock released successfully.
399 */
400 /* BEGIN_CASE */
SDV_BSL_SAL_THREAD_CREATE_FUNC_TC001(void)401 void SDV_BSL_SAL_THREAD_CREATE_FUNC_TC001(void)
402 {
403 #ifndef HITLS_BSL_SAL_THREAD
404 SKIP_TEST();
405 #else
406 BSL_SAL_ThreadLockHandle lock = NULL;
407 ASSERT_TRUE(BSL_SAL_ThreadLockNew(&lock) == BSL_SUCCESS);
408 BSL_SAL_ThreadId thread = NULL;
409 int32_t ret = BSL_SAL_ThreadCreate(&thread, NULL, NULL);
410 ASSERT_TRUE(ret == BSL_SAL_ERR_BAD_PARAM);
411 ret = BSL_SAL_ThreadCreate(NULL, TEST_Read, NULL);
412 ASSERT_TRUE(ret == BSL_SAL_ERR_BAD_PARAM);
413 ret = BSL_SAL_ThreadCreate(&thread, TEST_Read, lock);
414 ASSERT_TRUE(ret == BSL_SUCCESS);
415 BSL_SAL_ThreadClose(thread);
416 BSL_SAL_ThreadClose(NULL);
417 BSL_SAL_ThreadLockFree(lock);
418 EXIT:
419 return;
420 #endif
421 }
422 /* END_CASE */
423
TestRunOnce(void)424 static void TestRunOnce(void)
425 {
426 return;
427 }
428 /**
429 * @test SDV_BSL_SAL_THREAD_API_TC001
430 * @title Creating and Disabling Condition Variable Test
431 * @precon nan
432 * @brief
433 * 1. Call BSL_SAL_ThreadRunOnce to transfer abnormal parameters. Expected result 1 is obtained.
434 * 2. Call BSL_SAL_ThreadRunOnce to transfer normal parameters. Expected result 2 is obtained.
435 * @expect
436 * 1. BSL_SAL_ERR_BAD_PARAM
437 * 2. BSL_SUCCESS
438 */
439 /* BEGIN_CASE */
SDV_BSL_SAL_THREAD_API_TC001(void)440 void SDV_BSL_SAL_THREAD_API_TC001(void)
441 {
442 uint32_t isErrInit = 0;
443 ASSERT_EQ(BSL_SAL_ThreadRunOnce(NULL, TestRunOnce), BSL_SAL_ERR_BAD_PARAM);
444 ASSERT_EQ(BSL_SAL_ThreadRunOnce(&isErrInit, NULL), BSL_SAL_ERR_BAD_PARAM);
445 ASSERT_EQ(BSL_SAL_ThreadRunOnce(&isErrInit, TestRunOnce), BSL_SUCCESS);
446 EXIT:
447 return;
448 }
449 /* END_CASE */
450
451 /**
452 * @test SDV_BSL_SAL_CONDVAR_CREATE_FUNC_TC001
453 * @title Creating and Disabling Condition Variable Test
454 * @precon nan
455 * @brief
456 * 1. Call BSL_SAL_CreateCondVar to transfer abnormal parameters. Expected result 1 is obtained.
457 * 2. Call BSL_SAL_CreateCondVar to transfer normal parameters. Expected result 2 is obtained.
458 * 3. Call BSL_SAL_CondSignal to transfer abnormal parameters. Expected result 3 is obtained.
459 * 4. Call BSL_SAL_CondSignal to transfer normal parameters. Expected result 4 is obtained.
460 * 5. Call BSL_SAL_DeleteCondVar to transfer abnormal parameters. Expected result 5 is obtained.
461 * 6. Call BSL_SAL_DeleteCondVar to transfer normal parameters. Expected result 6 is obtained.
462 * 7. Call BSL_SAL_DeleteCondVar to delete the deleted condVar. Expected result 7 is obtained.
463 * @expect
464 * 1. BSL_SAL_ERR_BAD_PARAM
465 * 2. BSL_SUCCESS
466 * 3. BSL_SAL_ERR_BAD_PARAM
467 * 4. BSL_SUCCESS
468 * 5. BSL_SAL_ERR_BAD_PARAM
469 * 6. BSL_SUCCESS
470 * 7. BSL_SAL_ERR_UNKNOWN
471 */
472 /* BEGIN_CASE */
SDV_BSL_SAL_CONDVAR_CREATE_FUNC_TC001(void)473 void SDV_BSL_SAL_CONDVAR_CREATE_FUNC_TC001(void)
474 {
475 #ifndef HITLS_BSL_SAL_THREAD
476 SKIP_TEST();
477 #else
478 BSL_SAL_CondVar condVar = NULL;
479 int32_t ret = BSL_SAL_CreateCondVar(NULL);
480 ASSERT_TRUE(ret == BSL_SAL_ERR_BAD_PARAM);
481 ret = BSL_SAL_CreateCondVar(&condVar);
482 ASSERT_TRUE(ret == BSL_SUCCESS);
483 ret = BSL_SAL_CondSignal(NULL);
484 ASSERT_TRUE(ret == BSL_SAL_ERR_BAD_PARAM);
485 ret = BSL_SAL_CondSignal(condVar);
486 ASSERT_TRUE(ret == BSL_SUCCESS);
487 ret = BSL_SAL_DeleteCondVar(NULL);
488 ASSERT_TRUE(ret == BSL_SAL_ERR_BAD_PARAM);
489 ret = BSL_SAL_DeleteCondVar(condVar);
490 ASSERT_TRUE(ret == BSL_SUCCESS);
491 EXIT:
492 return;
493 #endif
494 }
495 /* END_CASE */
496
497 /**
498 * @test SDV_BSL_SAL_CONDVAR_WAIT_API_TC001
499 * @title Creating and Disabling Condition Variable Test
500 * @precon nan
501 * @brief
502 * 1. Call BSL_SAL_CreateCondVar to create a condition variable. Expected result 1 is obtained.
503 * 2. Unregistered thread-related callback and create a thread lock. Expected result 2 is obtained.
504 * 3. Call BSL_SAL_CondTimedwaitMs to transfer abnormal parameters. Expected result 3 is obtained.
505 * 4. Call BSL_SAL_CondTimedwaitMs to transfer normal parameters. Expected result 4 is obtained.
506 * 5. Release the lock. Expected result 5 is obtained.
507 * @expect
508 * 1. BSL_SUCCESS
509 * 2. BSL_SUCCESS
510 * 3. BSL_SAL_ERR_BAD_PARAM
511 * 4. BSL_SUCCESS
512 * 5. Lock released successfully.
513 */
514 /* BEGIN_CASE */
SDV_BSL_SAL_CONDVAR_WAIT_API_TC001(void)515 void SDV_BSL_SAL_CONDVAR_WAIT_API_TC001(void)
516 {
517 #ifndef HITLS_BSL_SAL_THREAD
518 SKIP_TEST();
519 #else
520 BSL_SAL_ThreadLockHandle lock = NULL;
521 ASSERT_TRUE(BSL_SAL_ThreadLockNew(&lock) == BSL_SUCCESS);
522
523 BSL_SAL_CondVar condVar = NULL;
524 int32_t ret = BSL_SAL_CreateCondVar(&condVar);
525 ASSERT_TRUE(ret == BSL_SUCCESS);
526
527 ret = BSL_SAL_CondTimedwaitMs(NULL, condVar, 10);
528 ASSERT_TRUE(ret == BSL_SAL_ERR_BAD_PARAM);
529
530 ret = BSL_SAL_CondTimedwaitMs(lock, condVar, 1);
531 ASSERT_TRUE(ret == BSL_SAL_ERR_UNKNOWN);
532 ret = BSL_SAL_DeleteCondVar(condVar);
533 ASSERT_TRUE(ret == BSL_SUCCESS);
534 BSL_SAL_ThreadLockFree(lock);
535 EXIT:
536 return;
537 #endif
538 }
539 /* END_CASE */
540
541 #ifdef HITLS_BSL_SAL_THREAD
542 static BSL_SAL_CondVar g_condVar = NULL;
543 static pthread_mutex_t g_lock;
544
ThreadTest(void * arg)545 static void *ThreadTest(void *arg)
546 {
547 (void)arg;
548 int32_t ret1 = BSL_SAL_CondTimedwaitMs(&g_lock, g_condVar, 10000000);
549 ASSERT_TRUE(ret1 == BSL_SUCCESS);
550 EXIT:
551 return NULL;
552 }
553 #endif
554
555 /**
556 * @test SDV_BSL_SAL_CONDVAR_WAIT_FUNC_TC001
557 * @title Creating and Disabling Condition Variable Test
558 * @precon nan
559 * @brief
560 * 1. Call BSL_SAL_CreateCondVar to create a condition variable. Expected result 1 is obtained.
561 * 2. Call BSL_SAL_ThreadCreate to create the timedwait thread. Expected result 2 is obtained.
562 * 3. Call BSL_SAL_CondSignal to transfer normal parameters. Expected result 3 is obtained.
563 * 4. Call BSL_SAL_DeleteCondVar to transfer normal parameters. Expected result 4 is obtained.
564 * 5. Release the lock. Expected result 5 is obtained.
565 * @expect
566 * 1. BSL_SUCCESS
567 * 2. BSL_SUCCESS
568 * 3. BSL_SUCCESS
569 * 4. BSL_SUCCESS
570 * 5. Lock released successfully.
571 */
572 /* BEGIN_CASE */
SDV_BSL_SAL_CONDVAR_WAIT_FUNC_TC001(void)573 void SDV_BSL_SAL_CONDVAR_WAIT_FUNC_TC001(void)
574 {
575 #ifndef HITLS_BSL_SAL_THREAD
576 SKIP_TEST();
577 #else
578 int32_t ret = BSL_SAL_CreateCondVar(&g_condVar);
579 ASSERT_TRUE(ret == BSL_SUCCESS);
580 pthread_mutex_init(&g_lock, NULL);
581
582 BSL_SAL_ThreadId thread = NULL;
583 ret = BSL_SAL_ThreadCreate(&thread, ThreadTest, NULL);
584 ASSERT_TRUE(ret == BSL_SUCCESS);
585
586 sleep(1); // Wait one seconds to send the signal
587 pthread_mutex_lock(&g_lock);
588 ret = BSL_SAL_CondSignal(g_condVar);
589 ASSERT_TRUE(ret == BSL_SUCCESS);
590 pthread_mutex_unlock(&g_lock);
591
592 BSL_SAL_ThreadClose(thread);
593 ret = BSL_SAL_DeleteCondVar(g_condVar);
594 ASSERT_TRUE(ret == BSL_SUCCESS);
595 pthread_mutex_destroy(&g_lock);
596 EXIT:
597 return;
598 #endif
599 }
600 /* END_CASE */
601
602 /**
603 * @test SDV_BSL_SAL_STR_API_TC001
604 * @title Test on the function of inputting abnormal parameters for character string processing of the BSL module
605 * @precon nan
606 * @brief
607 * 1. Call BSL_SAL_StrcaseCmp to transfer abnormal parameters. Expected result 1 is obtained.
608 * 2. Call BSL_SAL_Memchr to transfer abnormal parameters. Expected result 2 is obtained.
609 * 3. Call BSL_SAL_Atoi to transfer abnormal parameters. Expected result 3 is obtained.
610 * 4. Call BSL_SAL_Strnlen to transfer abnormal parameters. Expected result 4 is obtained.
611 * @expect
612 * 1. BSL_NULL_INPUT
613 * 2. NULL
614 * 3. 0
615 * 4. 0
616 */
617 /* BEGIN_CASE */
SDV_BSL_SAL_STR_API_TC001(void)618 void SDV_BSL_SAL_STR_API_TC001(void)
619 {
620 #ifndef HITLS_BSL_SAL_STR
621 SKIP_TEST();
622 #else
623 char *str1 = "aaastr1";
624 char *str2 = "aaastr2";
625 ASSERT_TRUE(BSL_SAL_StrcaseCmp(str1, NULL) == BSL_NULL_INPUT);
626 ASSERT_TRUE(BSL_SAL_StrcaseCmp(NULL, str2) == BSL_NULL_INPUT);
627 ASSERT_TRUE(BSL_SAL_Memchr(NULL, 's', 10) == NULL);
628 ASSERT_TRUE(BSL_SAL_Atoi(NULL) == 0);
629 ASSERT_TRUE(BSL_SAL_Strnlen(NULL, 0) == 0);
630 EXIT:
631 return;
632 #endif
633 }
634 /* END_CASE */
635
636 /**
637 * @test SDV_BSL_SAL_STR_FUNC_TC001
638 * @title Character string processing function of the BSL module
639 * @precon nan
640 * @brief
641 * 1. Call BSL_SAL_StrcaseCmp to transfer normal parameters. Expected result 1 is obtained.
642 * 2. Call BSL_SAL_Memchr to transfer normal parameters. Expected result 2 is obtained.
643 * 3. Call BSL_SAL_Atoi to transfer normal parameters. Expected result 3 is obtained.
644 * 4. Call BSL_SAL_Strnlen to transfer normal parameters. Expected result 4 is obtained.
645 * @expect
646 * 1. String comparison succeeded.
647 * 2. Searching for the corresponding character succeeded.
648 * 3. Succeeded in converting a character string to a number.
649 * 4. Obtaining the length of the given string succeeded.
650 */
651 /* BEGIN_CASE */
SDV_BSL_SAL_STR_FUNC_TC001(void)652 void SDV_BSL_SAL_STR_FUNC_TC001(void)
653 {
654 #ifndef HITLS_BSL_SAL_STR
655 SKIP_TEST();
656 #else
657 char *str1 = "aaastr1";
658 char *str2 = "aaastr2";
659 char *str3 = " aaastr3";
660 ASSERT_TRUE(BSL_SAL_StrcaseCmp(str1, str1) == 0);
661 ASSERT_TRUE(BSL_SAL_StrcaseCmp(str1, str2) == -1);
662 ASSERT_TRUE(BSL_SAL_StrcaseCmp(str2, str1) == 1);
663
664 ASSERT_TRUE(BSL_SAL_Memchr(str1, 's', strlen(str1)) != NULL);
665 ASSERT_TRUE(BSL_SAL_Memchr(str1, '1', 5) == NULL);
666 ASSERT_TRUE(BSL_SAL_Memchr(str1, '1', strlen(str1)) != NULL);
667 ASSERT_TRUE(BSL_SAL_Memchr(str1, 'b', strlen(str1)) == NULL);
668 ASSERT_TRUE(BSL_SAL_Memchr(str3, ' ', strlen(str3)) != NULL);
669
670 ASSERT_TRUE(BSL_SAL_Atoi("-100") == -100);
671 ASSERT_TRUE(BSL_SAL_Atoi("123") == 123);
672 ASSERT_TRUE(BSL_SAL_Atoi("123.456") == 123);
673 ASSERT_TRUE(BSL_SAL_Atoi(" 123 ") == 123);
674 ASSERT_TRUE(BSL_SAL_Atoi("000123") == 123);
675 ASSERT_TRUE(BSL_SAL_Atoi(" 1 23") == 1);
676 ASSERT_TRUE(BSL_SAL_Atoi("\n1 23") == 1);
677 ASSERT_TRUE(BSL_SAL_Atoi("1\n23") == 1);
678 ASSERT_TRUE(BSL_SAL_Atoi("0\n23") == 0);
679
680 ASSERT_TRUE(BSL_SAL_Strnlen(str1, strlen(str1)) == 7);
681 ASSERT_TRUE(BSL_SAL_Strnlen(str1, 100) == 7);
682 ASSERT_TRUE(BSL_SAL_Strnlen(str1, 3) == 3);
683 EXIT:
684 return;
685 #endif
686 }
687 /* END_CASE */
688
689 /**
690 * @test SDV_BSL_SAL_THREAD_DEFAULT_FUNC_TC001
691 * @title Default Thread Related Functions
692 * @precon nan
693 * @brief
694 * 1. Unregistered thread-related callback and create a thread lock. Expected result 1 is obtained.
695 * 2. Create two read threads and two write threads, read and write concurrent threads,
696 * and obtain IDs from the threads. Expected result 2 is obtained.
697 * 3. Obtain the process ID and compare it with the subthread ID. Expected result 3 is obtained.
698 * 4. Obtain the subprocess ID and compare it with the current process ID. Expected result 4 is obtained.
699 * 5. Release the lock. Expected result 5 is obtained.
700 * @expect
701 * 1. Thread lock created successfully.
702 * 2. The read thread and write thread are successfully created.
703 * 3. The process ID and subthread ID are different.
704 * 4. The current subprocess ID is the same as the current process ID.
705 * 5. Lock released successfully.
706 */
707 /* BEGIN_CASE */
SDV_BSL_SAL_THREAD_DEFAULT_FUNC_TC001(void)708 void SDV_BSL_SAL_THREAD_DEFAULT_FUNC_TC001(void)
709 {
710 #ifndef HITLS_BSL_SAL_THREAD
711 SKIP_TEST();
712 #else
713 g_threadDefaultWrite001 = 0;
714 g_threadDefaultRead001 = 0;
715 g_threadDefaultId001 = 0;
716
717 // 1
718 BSL_SAL_ThreadLockHandle lock = NULL;
719 ASSERT_TRUE(BSL_SAL_ThreadLockNew(&lock) == BSL_SUCCESS);
720
721 // 2
722 BSL_SAL_ThreadId pid[TEST_WRITE_PID_CNT];
723 BSL_SAL_ThreadId pid2[TEST_READ_PID_CNT];
724 size_t i = 0;
725 size_t m = 0;
726 for (i = 0u; i < TEST_WRITE_PID_CNT; i++) {
727 ASSERT_TRUE(BSL_SAL_ThreadCreate(&pid[i], TEST_Write, (void *)lock) == BSL_SUCCESS);
728 }
729 for (m = 0u; m < TEST_READ_PID_CNT; m++) {
730 ASSERT_TRUE(BSL_SAL_ThreadCreate(&pid2[m], TEST_Read, (void *)lock) == BSL_SUCCESS);
731 }
732 for (size_t j = 0; j < i; j++) {
733 BSL_SAL_ThreadClose(pid[j]);
734 }
735 for (size_t n = 0; n < m; n++) {
736 BSL_SAL_ThreadClose(pid2[n]);
737 }
738 ASSERT_EQ(g_threadDefaultWrite001, TEST_READ_PID_CNT * TEST_THREAD_DEFAULT_TC001_WRITE_CNT);
739 // Concurrent reads. The read result is uncertain and does not determine whether to perform the read operation.
740
741 // 3
742 uint64_t mainId = BSL_SAL_ThreadGetId();
743 ASSERT_TRUE(mainId != g_threadDefaultId001);
744
745 // 4
746 uint64_t childId = 0;
747 pid_t pidFork = fork();
748 if (pidFork == 0) {
749 // The child process
750 childId = BSL_SAL_ThreadGetId();
751 } else {
752 // The parent process
753 goto EXIT;
754 }
755 // The default implementation uses pthread_self. Therefore, the IDs of the parent and child processes are the same.
756 ASSERT_EQ(childId, mainId);
757 EXIT:
758 // 5
759 BSL_SAL_ThreadLockFree(lock);
760 g_threadDefaultWrite001 = 0;
761 g_threadDefaultRead001 = 0;
762 g_threadDefaultId001 = 0;
763 #endif
764 }
765 /* END_CASE */
766
767 /**
768 * @test SDV_BSL_SAL_CALLBACK_CTRL_FUNC_TC001
769 * @title test BSL_SAL_CallBack_Ctrl functions
770 * @precon nan
771 * @brief
772 * 1.Call BSL_SAL_CallBack_Ctrl registering file Callback Function, Expected result 1 is obtained.
773 * 2.Call BSL_SAL_CallBack_Ctrl registering time Callback Function, Expected result 1 is obtained.
774 * 3.Call BSL_SAL_CallBack_Ctrl registering net Callback Function, Expected result 1 is obtained.
775 * 4.Call BSL_SAL_CallBack_Ctrl registering invalid Callback Function, Expected result 2 is obtained.
776 * 5.Call BSL_SAL_SockGetLastSocketError obtaining the last socket error, Expected result 3 is obtained.
777 * @expect
778 * 1. BSL_SUCCESS
779 * 2. BSL_SAL_ERR_NET_IOCTL
780 * 3. Succeeded in obtaining the last socket error.
781 */
782 /* BEGIN_CASE */
SDV_BSL_SAL_CALLBACK_CTRL_FUNC_TC001(void)783 void SDV_BSL_SAL_CALLBACK_CTRL_FUNC_TC001(void)
784 {
785 #if defined(HITLS_BSL_SAL_FILE) || defined(HITLS_BSL_SAL_TIME) || defined(HITLS_BSL_SAL_NET)
786 #ifdef HITLS_BSL_SAL_FILE
787 ASSERT_EQ(BSL_SAL_CallBack_Ctrl(BSL_SAL_FILE_OPEN_CB_FUNC, NULL), BSL_SUCCESS);
788 #endif
789 #ifdef HITLS_BSL_SAL_TIME
790 ASSERT_EQ(BSL_SAL_CallBack_Ctrl(BSL_SAL_TIME_GET_UTC_TIME_CB_FUNC, NULL), BSL_SUCCESS);
791 #endif
792 #ifdef HITLS_BSL_SAL_NET
793 ASSERT_EQ(BSL_SAL_CallBack_Ctrl(BSL_SAL_NET_WRITE_CB_FUNC, NULL), BSL_SUCCESS);
794 ASSERT_EQ(BSL_SAL_Ioctlsocket(0, 0, NULL), BSL_SAL_ERR_NET_IOCTL);
795 #endif
796 EXIT:
797 return;
798 #endif
799 }
800 /* END_CASE */