1 /*
2 * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <securec.h>
17
18 #include "softbus_adapter_thread.h"
19 #include "softbus_error_code.h"
20 #include "gtest/gtest.h"
21
22 using namespace std;
23 using namespace testing::ext;
24
25 namespace OHOS {
26 static SoftBusCond g_cond;
27 static SoftBusMutex g_mutex;
28 const int32_t DELAY_TIME = 1000;
29
30 class SoftbusThreadTest : public testing::Test {
31 protected:
32 static void SetUpTestCase(void);
33 static void TearDownTestCase(void);
34 void SetUp();
35 void TearDown();
36 };
37
SetUpTestCase(void)38 void SoftbusThreadTest::SetUpTestCase(void) { }
39
TearDownTestCase(void)40 void SoftbusThreadTest::TearDownTestCase(void) { }
41
SetUp()42 void SoftbusThreadTest::SetUp() { }
43
TearDown()44 void SoftbusThreadTest::TearDown() { }
45
SoftBusThreadTask(void * arg)46 static void *SoftBusThreadTask(void *arg)
47 {
48 printf("----------%s--------\n", __FUNCTION__);
49 SoftBusSleepMs(DELAY_TIME);
50 return static_cast<void *>(const_cast<char *>("SoftBusThreadTask"));
51 }
52
ThreadSelfTest(void * arg)53 static void *ThreadSelfTest(void *arg)
54 {
55 printf("----------%s--------\n", __FUNCTION__);
56 SoftBusThread thread = SoftBusThreadGetSelf();
57 EXPECT_TRUE(thread != 0);
58 SoftBusSleepMs(DELAY_TIME);
59 return nullptr;
60 }
61
ThreadWaitTest(void * arg)62 static void *ThreadWaitTest(void *arg)
63 {
64 printf("----------%s--------\n", __FUNCTION__);
65 int32_t ret = SoftBusCondWait(&g_cond, &g_mutex, nullptr);
66 EXPECT_EQ(SOFTBUS_OK, ret);
67 SoftBusSleepMs(DELAY_TIME);
68 return nullptr;
69 }
70
ThreadSignalTest(void * arg)71 static void *ThreadSignalTest(void *arg)
72 {
73 printf("----------%s--------\n", __FUNCTION__);
74 SoftBusSleepMs(DELAY_TIME);
75 int32_t ret = SoftBusCondSignal(&g_cond);
76 EXPECT_EQ(SOFTBUS_OK, ret);
77 return nullptr;
78 }
79
80 /*
81 * @tc.name: SoftbusMutexAttrInitTest001
82 * @tc.desc: mutexAttr is nullptr
83 * @tc.type: FUNC
84 * @tc.require: 1
85 */
86 HWTEST_F(SoftbusThreadTest, SoftbusMutexAttrInitTest001, TestSize.Level0)
87 {
88 int32_t ret = SoftBusMutexAttrInit(nullptr);
89 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
90 }
91
92 /*
93 * @tc.name: SoftbusMutexAttrInitTest002
94 * @tc.desc: mutexAttr is valid
95 * @tc.type: FUNC
96 * @tc.require: 1
97 */
98 HWTEST_F(SoftbusThreadTest, SoftbusMutexAttrInitTest002, TestSize.Level0)
99 {
100 SoftBusMutexAttr mutexAttr;
101 int32_t ret = SoftBusMutexAttrInit(&mutexAttr);
102 EXPECT_EQ(SOFTBUS_OK, ret);
103 EXPECT_EQ(SOFTBUS_MUTEX_NORMAL, mutexAttr.type);
104 }
105
106 /*
107 * @tc.name: SoftBusMutexInitTest001
108 * @tc.desc: mutexAttr is nullptr
109 * @tc.type: FUNC
110 * @tc.require: 1
111 */
112 HWTEST_F(SoftbusThreadTest, SoftBusMutexInitTest001, TestSize.Level0)
113 {
114 SoftBusMutex mutex = 0;
115 int32_t ret = SoftBusMutexInit(&mutex, nullptr);
116 EXPECT_EQ(SOFTBUS_OK, ret);
117 }
118
119 /*
120 * @tc.name: SoftBusMutexInitTest002
121 * @tc.desc: mutexAttr type is SOFTBUS_MUTEX_NORMAL
122 * @tc.type: FUNC
123 * @tc.require: 1
124 */
125 HWTEST_F(SoftbusThreadTest, SoftBusMutexInitTest002, TestSize.Level0)
126 {
127 SoftBusMutex mutex = 0;
128 SoftBusMutexAttr mutexAttr = {
129 .type = SOFTBUS_MUTEX_NORMAL,
130 };
131 int32_t ret = SoftBusMutexInit(&mutex, &mutexAttr);
132 EXPECT_EQ(SOFTBUS_OK, ret);
133 }
134
135 /*
136 * @tc.name: SoftBusMutexInitTest003
137 * @tc.desc: mutexAttr type is SOFTBUS_MUTEX_RECURSIVE
138 * @tc.type: FUNC
139 * @tc.require: 1
140 */
141 HWTEST_F(SoftbusThreadTest, SoftBusMutexInitTest003, TestSize.Level0)
142 {
143 SoftBusMutex mutex = 0;
144 SoftBusMutexAttr mutexAttr = {
145 .type = SOFTBUS_MUTEX_RECURSIVE,
146 };
147 int32_t ret = SoftBusMutexInit(&mutex, &mutexAttr);
148 EXPECT_EQ(SOFTBUS_OK, ret);
149 }
150
151 /*
152 * @tc.name: SoftBusMutexInitTest004
153 * @tc.desc: SoftBusMutexInit will return SOFTBUS_INVALID_PARAM when mutex=nullptr
154 * @tc.type: FUNC
155 * @tc.require: 1
156 */
157 HWTEST_F(SoftbusThreadTest, SoftBusMutexInitTest004, TestSize.Level0)
158 {
159 SoftBusMutexAttr mutexAttr = {
160 .type = SOFTBUS_MUTEX_RECURSIVE,
161 };
162 int32_t ret = SoftBusMutexInit(nullptr, &mutexAttr);
163 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
164 }
165
166 /*
167 * @tc.name: SoftBusMutexLockTest001
168 * @tc.desc: mutex is nullptr
169 * @tc.type: FUNC
170 * @tc.require: 1
171 */
172 HWTEST_F(SoftbusThreadTest, SoftBusMutexLockTest001, TestSize.Level0)
173 {
174 int32_t ret = SoftBusMutexLock(nullptr);
175 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
176 }
177
178 /*
179 * @tc.name: SoftBusMutexLockTest002
180 * @tc.desc: mutexAttr type is SOFTBUS_MUTEX_NORMAL
181 * @tc.type: FUNC
182 * @tc.require: 1
183 */
184 HWTEST_F(SoftbusThreadTest, SoftBusMutexLockTest002, TestSize.Level0)
185 {
186 SoftBusMutex mutex = 0;
187 SoftBusMutexAttr mutexAttr = {
188 .type = SOFTBUS_MUTEX_NORMAL,
189 };
190 int32_t ret = SoftBusMutexInit(&mutex, &mutexAttr);
191 EXPECT_EQ(SOFTBUS_OK, ret);
192 ret = SoftBusMutexLock(&mutex);
193 EXPECT_EQ(SOFTBUS_OK, ret);
194 }
195
196 /*
197 * @tc.name: SoftBusMutexLockTest003
198 * @tc.desc: mutexAttr type is SOFTBUS_MUTEX_RECURSIVE
199 * @tc.type: FUNC
200 * @tc.require: 1
201 */
202 HWTEST_F(SoftbusThreadTest, SoftBusMutexLockTest003, TestSize.Level0)
203 {
204 SoftBusMutex mutex = 0;
205 SoftBusMutexAttr mutexAttr = {
206 .type = SOFTBUS_MUTEX_RECURSIVE,
207 };
208 int32_t ret = SoftBusMutexInit(&mutex, &mutexAttr);
209 EXPECT_EQ(SOFTBUS_OK, ret);
210 ret = SoftBusMutexLock(&mutex);
211 EXPECT_EQ(SOFTBUS_OK, ret);
212 }
213
214 /*
215 * @tc.name: SoftBusMutexLockTest004
216 * @tc.desc: mutex is default
217 * @tc.type: FUNC
218 * @tc.require: 1
219 */
220 HWTEST_F(SoftbusThreadTest, SoftBusMutexLockTest004, TestSize.Level0)
221 {
222 SoftBusMutex mutex = 0;
223 int32_t ret = SoftBusMutexInit(&mutex, nullptr);
224 ret = SoftBusMutexLock(&mutex);
225 EXPECT_EQ(SOFTBUS_OK, ret);
226 }
227
228 /*
229 * @tc.name: SoftBusMutexLockTest005
230 * @tc.desc: mutex value is 0
231 * @tc.type: FUNC
232 * @tc.require: 1
233 */
234 HWTEST_F(SoftbusThreadTest, SoftBusMutexLockTest005, TestSize.Level0)
235 {
236 SoftBusMutex mutex = 0;
237 int32_t ret = SoftBusMutexLock(&mutex);
238 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
239 }
240
241 /*
242 * @tc.name: SoftBusMutexUnlockTest001
243 * @tc.desc: mutex is nullptr
244 * @tc.type: FUNC
245 * @tc.require: 1
246 */
247 HWTEST_F(SoftbusThreadTest, SoftBusMutexUnlockTest001, TestSize.Level0)
248 {
249 int32_t ret = SoftBusMutexUnlock(nullptr);
250 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
251 }
252
253 /*
254 * @tc.name: SoftBusMutexUnlockTest002
255 * @tc.desc: mutex value is 0
256 * @tc.type: FUNC
257 * @tc.require: 1
258 */
259 HWTEST_F(SoftbusThreadTest, SoftBusMutexUnlockTest002, TestSize.Level0)
260 {
261 SoftBusMutex mutex = 0;
262 int32_t ret = SoftBusMutexUnlock(&mutex);
263 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
264 }
265
266 /*
267 * @tc.name: SoftBusMutexUnlockTest003
268 * @tc.desc: mutexAttr type is SOFTBUS_MUTEX_NORMAL
269 * @tc.type: FUNC
270 * @tc.require: 1
271 */
272 HWTEST_F(SoftbusThreadTest, SoftBusMutexUnlockTest003, TestSize.Level0)
273 {
274 SoftBusMutex mutex = 0;
275 SoftBusMutexAttr mutexAttr = {
276 .type = SOFTBUS_MUTEX_NORMAL,
277 };
278 int32_t ret = SoftBusMutexInit(&mutex, &mutexAttr);
279 EXPECT_EQ(SOFTBUS_OK, ret);
280 ret = SoftBusMutexLock(&mutex);
281 EXPECT_EQ(SOFTBUS_OK, ret);
282 ret = SoftBusMutexUnlock(&mutex);
283 EXPECT_EQ(SOFTBUS_OK, ret);
284 }
285
286 /*
287 * @tc.name: SoftBusMutexUnlockTest004
288 * @tc.desc: mutexAttr type is SOFTBUS_MUTEX_NORMAL
289 * @tc.type: FUNC
290 * @tc.require: 1
291 */
292 HWTEST_F(SoftbusThreadTest, SoftBusMutexUnlockTest004, TestSize.Level0)
293 {
294 SoftBusMutex mutex = 0;
295 SoftBusMutexAttr mutexAttr = {
296 .type = SOFTBUS_MUTEX_RECURSIVE,
297 };
298 int32_t ret = SoftBusMutexInit(&mutex, &mutexAttr);
299 EXPECT_EQ(SOFTBUS_OK, ret);
300 ret = SoftBusMutexLock(&mutex);
301 EXPECT_EQ(SOFTBUS_OK, ret);
302 ret = SoftBusMutexUnlock(&mutex);
303 EXPECT_EQ(SOFTBUS_OK, ret);
304 }
305
306 /*
307 * @tc.name: SoftBusMutexUnlockTest005
308 * @tc.desc: mutex value is default
309 * @tc.type: FUNC
310 * @tc.require: 1
311 */
312 HWTEST_F(SoftbusThreadTest, SoftBusMutexUnlockTest005, TestSize.Level0)
313 {
314 SoftBusMutex mutex = 0;
315 int32_t ret = SoftBusMutexInit(&mutex, nullptr);
316 EXPECT_EQ(SOFTBUS_OK, ret);
317 ret = SoftBusMutexLock(&mutex);
318 EXPECT_EQ(SOFTBUS_OK, ret);
319 ret = SoftBusMutexUnlock(&mutex);
320 EXPECT_EQ(SOFTBUS_OK, ret);
321 }
322
323 /*
324 * @tc.name: SoftBusMutexDestroyTest001
325 * @tc.desc: mutex is nullptr
326 * @tc.type: FUNC
327 * @tc.require: 1
328 */
329 HWTEST_F(SoftbusThreadTest, SoftBusMutexDestroyTest001, TestSize.Level0)
330 {
331 int32_t ret = SoftBusMutexDestroy(nullptr);
332 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
333 }
334
335 /*
336 * @tc.name: SoftBusMutexDestroyTest002
337 * @tc.desc: mutex value is 0
338 * @tc.type: FUNC
339 * @tc.require: 1
340 */
341 HWTEST_F(SoftbusThreadTest, SoftBusMutexDestroyTest002, TestSize.Level0)
342 {
343 SoftBusMutex mutex = 0;
344 int32_t ret = SoftBusMutexDestroy(&mutex);
345 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
346 }
347
348 /*
349 * @tc.name: SoftBusMutexDestroyTest003
350 * @tc.desc: mutexAttr is nullptr
351 * @tc.type: FUNC
352 * @tc.require: 1
353 */
354 HWTEST_F(SoftbusThreadTest, SoftBusMutexDestroyTest003, TestSize.Level0)
355 {
356 SoftBusMutex mutex = 0;
357 int32_t ret = SoftBusMutexInit(&mutex, nullptr);
358 EXPECT_EQ(SOFTBUS_OK, ret);
359
360 ret = SoftBusMutexDestroy(&mutex);
361 EXPECT_EQ(SOFTBUS_OK, ret);
362 }
363
364 /*
365 * @tc.name: SoftBusMutexDestroyTest004
366 * @tc.desc: mutexAttr is SOFTBUS_MUTEX_NORMAL
367 * @tc.type: FUNC
368 * @tc.require: 1
369 */
370 HWTEST_F(SoftbusThreadTest, SoftBusMutexDestroyTest004, TestSize.Level0)
371 {
372 SoftBusMutex mutex = 0;
373 SoftBusMutexAttr mutexAttr = {
374 .type = SOFTBUS_MUTEX_NORMAL,
375 };
376 int32_t ret = SoftBusMutexInit(&mutex, &mutexAttr);
377 EXPECT_EQ(SOFTBUS_OK, ret);
378
379 ret = SoftBusMutexDestroy(&mutex);
380 EXPECT_EQ(SOFTBUS_OK, ret);
381 }
382
383 /*
384 * @tc.name: SoftBusMutexDestroyTest005
385 * @tc.desc: mutexAttr is SOFTBUS_MUTEX_RECURSIVE
386 * @tc.type: FUNC
387 * @tc.require: 1
388 */
389 HWTEST_F(SoftbusThreadTest, SoftBusMutexDestroyTest005, TestSize.Level0)
390 {
391 SoftBusMutex mutex = 0;
392 SoftBusMutexAttr mutexAttr = {
393 .type = SOFTBUS_MUTEX_RECURSIVE,
394 };
395 int32_t ret = SoftBusMutexInit(&mutex, &mutexAttr);
396 EXPECT_EQ(SOFTBUS_OK, ret);
397
398 ret = SoftBusMutexDestroy(&mutex);
399 EXPECT_EQ(SOFTBUS_OK, ret);
400 }
401
402 /*
403 * @tc.name: SoftBusMutexLockGuardTest001
404 * @tc.desc: should call SoftBusMutexUnlock automatically when leave bracket scope
405 * @tc.type: FUNC
406 * @tc.require:
407 */
408 HWTEST_F(SoftbusThreadTest, SoftBusMutexLockGuardTest001, TestSize.Level0)
409 {
410 SoftBusMutex mutex = 0;
411 int32_t ret = SoftBusMutexInit(&mutex, nullptr);
412 EXPECT_EQ(SOFTBUS_OK, ret);
413 {
414 ret = SoftBusMutexLock(&mutex);
415 EXPECT_EQ(SOFTBUS_OK, ret);
416 SOFTBUS_LOCK_GUARD(mutex);
417 }
418 {
419 ret = SoftBusMutexLock(&mutex);
420 EXPECT_EQ(SOFTBUS_OK, ret);
421 SOFTBUS_LOCK_GUARD(mutex);
422 }
423 ret = SoftBusMutexDestroy(&mutex);
424 EXPECT_EQ(SOFTBUS_OK, ret);
425 }
426
427 /*
428 * @tc.name: SoftBusThreadAttrInitTest001
429 * @tc.desc: threadAttr is nullptr
430 * @tc.type: FUNC
431 * @tc.require: 1
432 */
433 HWTEST_F(SoftbusThreadTest, SoftBusThreadAttrInitTest001, TestSize.Level0)
434 {
435 int32_t ret = SoftBusThreadAttrInit(nullptr);
436 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
437 }
438
439 /*
440 * @tc.name: SoftBusThreadAttrInitTest002
441 * @tc.desc: threadAttr is valid
442 * @tc.type: FUNC
443 * @tc.require: 1
444 */
445 HWTEST_F(SoftbusThreadTest, SoftBusThreadAttrInitTest002, TestSize.Level0)
446 {
447 SoftBusThreadAttr threadAttr = { 0 };
448 int32_t ret = SoftBusThreadAttrInit(&threadAttr);
449 EXPECT_EQ(SOFTBUS_OK, ret);
450 }
451
452 /*
453 * @tc.name: SoftBusThreadCreateTest001
454 * @tc.desc: thread is nullptr
455 * @tc.type: FUNC
456 * @tc.require: 1
457 */
458 HWTEST_F(SoftbusThreadTest, SoftBusThreadCreateTest001, TestSize.Level0)
459 {
460 SoftBusThreadAttr threadAttr = { 0 };
461 int32_t ret = SoftBusThreadAttrInit(&threadAttr);
462 EXPECT_EQ(SOFTBUS_OK, ret);
463
464 ret = SoftBusThreadCreate(nullptr, &threadAttr, SoftBusThreadTask, nullptr);
465 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
466 }
467
468 /*
469 * @tc.name: SoftBusThreadCreateTest002
470 * @tc.desc: threadAttr is nullptr
471 * @tc.type: FUNC
472 * @tc.require: 1
473 */
474 HWTEST_F(SoftbusThreadTest, SoftBusThreadCreateTest002, TestSize.Level0)
475 {
476 SoftBusThread thread = 0;
477
478 int32_t ret = SoftBusThreadCreate(&thread, nullptr, SoftBusThreadTask, nullptr);
479 EXPECT_EQ(SOFTBUS_OK, ret);
480 EXPECT_TRUE(thread != 0);
481 }
482
483 /*
484 * @tc.name: SoftBusThreadCreateTest003
485 * @tc.desc: threadAttr is valid
486 * @tc.type: FUNC
487 * @tc.require: 1
488 */
489 HWTEST_F(SoftbusThreadTest, SoftBusThreadCreateTest003, TestSize.Level0)
490 {
491 SoftBusThread thread = 0;
492 SoftBusThreadAttr threadAttr = { 0 };
493 int32_t ret = SoftBusThreadAttrInit(&threadAttr);
494 EXPECT_EQ(SOFTBUS_OK, ret);
495
496 ret = SoftBusThreadCreate(&thread, &threadAttr, SoftBusThreadTask, nullptr);
497 EXPECT_EQ(SOFTBUS_OK, ret);
498 EXPECT_TRUE(thread != 0);
499 }
500
501 #if HAVE_PRO
502 /*
503 * @tc.name: SoftBusThreadCreateTest004
504 * @tc.desc: threadAttr add taskName
505 * @tc.type: FUNC
506 * @tc.require: 1
507 */
508 HWTEST_F(SoftbusThreadTest, SoftBusThreadCreateTest004, TestSize.Level0)
509 {
510 SoftBusThread thread = 0;
511 SoftBusThreadAttr threadAttr = { 0 };
512 int32_t ret = SoftBusThreadAttrInit(&threadAttr);
513 EXPECT_EQ(SOFTBUS_OK, ret);
514
515 threadAttr.taskName = "ThreadTask";
516 ret = SoftBusThreadCreate(&thread, &threadAttr, SoftBusThreadTask, nullptr);
517 EXPECT_EQ(SOFTBUS_OK, ret);
518 EXPECT_TRUE(thread != 0);
519 }
520 #endif
521
522 /*
523 * @tc.name: SoftBusThreadCreateTest005
524 * @tc.desc: threadAttr modify prior
525 * @tc.type: FUNC
526 * @tc.require: 1
527 */
528 HWTEST_F(SoftbusThreadTest, SoftBusThreadCreateTest005, TestSize.Level0)
529 {
530 SoftBusThread thread = 0;
531 SoftBusThreadAttr threadAttr = { 0 };
532 int32_t ret = SoftBusThreadAttrInit(&threadAttr);
533 EXPECT_EQ(SOFTBUS_OK, ret);
534
535 threadAttr.prior = SOFTBUS_PRIORITY_HIGHEST;
536 ret = SoftBusThreadCreate(&thread, &threadAttr, SoftBusThreadTask, nullptr);
537 EXPECT_EQ(SOFTBUS_OK, ret);
538 EXPECT_TRUE(thread != 0);
539 }
540
541 /*
542 * @tc.name: SoftBusThreadCreateTest006
543 * @tc.desc: threadAttr modify prior
544 * @tc.type: FUNC
545 * @tc.require: 1
546 */
547 HWTEST_F(SoftbusThreadTest, SoftBusThreadCreateTest006, TestSize.Level0)
548 {
549 SoftBusThread thread = 0;
550 SoftBusThreadAttr threadAttr = { 0 };
551 int32_t ret = SoftBusThreadAttrInit(&threadAttr);
552 EXPECT_EQ(SOFTBUS_OK, ret);
553
554 threadAttr.prior = SOFTBUS_PRIORITY_HIGH;
555 ret = SoftBusThreadCreate(&thread, &threadAttr, SoftBusThreadTask, nullptr);
556 EXPECT_EQ(SOFTBUS_OK, ret);
557 EXPECT_TRUE(thread != 0);
558 }
559
560 /*
561 * @tc.name: SoftBusThreadCreateTest007
562 * @tc.desc: threadAttr modify prior
563 * @tc.type: FUNC
564 * @tc.require: 1
565 */
566 HWTEST_F(SoftbusThreadTest, SoftBusThreadCreateTest007, TestSize.Level0)
567 {
568 SoftBusThread thread = 0;
569 SoftBusThreadAttr threadAttr = { 0 };
570 int32_t ret = SoftBusThreadAttrInit(&threadAttr);
571 EXPECT_EQ(SOFTBUS_OK, ret);
572
573 threadAttr.prior = SOFTBUS_PRIORITY_DEFAULT;
574 ret = SoftBusThreadCreate(&thread, &threadAttr, SoftBusThreadTask, nullptr);
575 EXPECT_EQ(SOFTBUS_OK, ret);
576 EXPECT_TRUE(thread != 0);
577 }
578
579 /*
580 * @tc.name: SoftBusThreadCreateTest008
581 * @tc.desc: threadAttr modify prior
582 * @tc.type: FUNC
583 * @tc.require: 1
584 */
585 HWTEST_F(SoftbusThreadTest, SoftBusThreadCreateTest008, TestSize.Level0)
586 {
587 SoftBusThread thread = 0;
588 SoftBusThreadAttr threadAttr = { 0 };
589 int32_t ret = SoftBusThreadAttrInit(&threadAttr);
590 EXPECT_EQ(SOFTBUS_OK, ret);
591
592 threadAttr.prior = SOFTBUS_PRIORITY_LOW;
593 ret = SoftBusThreadCreate(&thread, &threadAttr, SoftBusThreadTask, nullptr);
594 EXPECT_EQ(SOFTBUS_OK, ret);
595 EXPECT_TRUE(thread != 0);
596 }
597
598 /*
599 * @tc.name: SoftBusThreadCreateTest009
600 * @tc.desc: threadAttr modify prior
601 * @tc.type: FUNC
602 * @tc.require: 1
603 */
604 HWTEST_F(SoftbusThreadTest, SoftBusThreadCreateTest009, TestSize.Level0)
605 {
606 SoftBusThread thread = 0;
607 SoftBusThreadAttr threadAttr = { 0 };
608 int32_t ret = SoftBusThreadAttrInit(&threadAttr);
609 EXPECT_EQ(SOFTBUS_OK, ret);
610
611 threadAttr.prior = SOFTBUS_PRIORITY_LOWEST;
612 ret = SoftBusThreadCreate(&thread, &threadAttr, SoftBusThreadTask, nullptr);
613 EXPECT_EQ(SOFTBUS_OK, ret);
614 EXPECT_TRUE(thread != 0);
615 }
616
617 #if HAVE_PRO
618 /*
619 * @tc.name: SoftBusThreadCreateTest010
620 * @tc.desc: threadEntry is nullptr
621 * @tc.type: FUNC
622 * @tc.require: 1
623 */
624 HWTEST_F(SoftbusThreadTest, SoftBusThreadCreateTest010, TestSize.Level0)
625 {
626 SoftBusThread thread = 0;
627 SoftBusThreadAttr threadAttr = { 0 };
628 int32_t ret = SoftBusThreadAttrInit(&threadAttr);
629 EXPECT_EQ(SOFTBUS_OK, ret);
630
631 ret = SoftBusThreadCreate(&thread, &threadAttr, nullptr, nullptr);
632 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
633 }
634
635 /*
636 * @tc.name: SoftBusThreadSetNameTest001
637 * @tc.desc: name is nullptr
638 * @tc.type: FUNC
639 * @tc.require: 1
640 */
641 HWTEST_F(SoftbusThreadTest, SoftBusThreadSetNameTest001, TestSize.Level0)
642 {
643 SoftBusThread thread = 0;
644 SoftBusThreadAttr threadAttr = { 0 };
645 int32_t ret = SoftBusThreadAttrInit(&threadAttr);
646 EXPECT_EQ(SOFTBUS_OK, ret);
647
648 threadAttr.prior = SOFTBUS_PRIORITY_HIGHEST;
649
650 ret = SoftBusThreadCreate(&thread, &threadAttr, SoftBusThreadTask, nullptr);
651 EXPECT_EQ(SOFTBUS_OK, ret);
652 EXPECT_TRUE(thread != 0);
653
654 ret = SoftBusThreadSetName(thread, nullptr);
655 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
656 }
657
658 /*
659 * @tc.name: SoftBusThreadSetNameTest002
660 * @tc.desc: name is large than TASK_NAME_MAX_LEN
661 * @tc.type: FUNC
662 * @tc.require: 1
663 */
664 HWTEST_F(SoftbusThreadTest, SoftBusThreadSetNameTest002, TestSize.Level0)
665 {
666 const char *name = "abcdefghijklmnopqrstuvwxyz";
667 SoftBusThread thread = 0;
668 SoftBusThreadAttr threadAttr = { 0 };
669 int32_t ret = SoftBusThreadAttrInit(&threadAttr);
670 EXPECT_EQ(SOFTBUS_OK, ret);
671
672 threadAttr.prior = SOFTBUS_PRIORITY_HIGHEST;
673
674 ret = SoftBusThreadCreate(&thread, &threadAttr, SoftBusThreadTask, nullptr);
675 EXPECT_EQ(SOFTBUS_OK, ret);
676 EXPECT_TRUE(thread != 0);
677
678 ret = SoftBusThreadSetName(thread, name);
679 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
680 }
681
682 /*
683 * @tc.name: SoftBusThreadSetNameTest003
684 * @tc.desc: name is equal to TASK_NAME_MAX_LEN
685 * @tc.type: FUNC
686 * @tc.require: 1
687 */
688 HWTEST_F(SoftbusThreadTest, SoftBusThreadSetNameTest003, TestSize.Level0)
689 {
690 const char *name = "abcdefghijklmnop";
691 SoftBusThread thread = 0;
692 SoftBusThreadAttr threadAttr = { 0 };
693 int32_t ret = SoftBusThreadAttrInit(&threadAttr);
694 EXPECT_EQ(SOFTBUS_OK, ret);
695
696 threadAttr.prior = SOFTBUS_PRIORITY_HIGHEST;
697
698 ret = SoftBusThreadCreate(&thread, &threadAttr, SoftBusThreadTask, nullptr);
699 EXPECT_EQ(SOFTBUS_OK, ret);
700 EXPECT_TRUE(thread != 0);
701
702 ret = SoftBusThreadSetName(thread, name);
703 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
704 }
705
706 /*
707 * @tc.name: SoftBusThreadSetNameTest004
708 * @tc.desc: name include chinese character
709 * @tc.type: FUNC
710 * @tc.require: 1
711 */
712 HWTEST_F(SoftbusThreadTest, SoftBusThreadSetNameTest004, TestSize.Level0)
713 {
714 const char *name = "a中文p";
715 SoftBusThread thread = 0;
716 SoftBusThreadAttr threadAttr = { 0 };
717 int32_t ret = SoftBusThreadAttrInit(&threadAttr);
718 EXPECT_EQ(SOFTBUS_OK, ret);
719
720 ret = SoftBusThreadCreate(&thread, &threadAttr, SoftBusThreadTask, nullptr);
721 EXPECT_EQ(SOFTBUS_OK, ret);
722 EXPECT_TRUE(thread != 0);
723
724 ret = SoftBusThreadSetName(thread, name);
725 EXPECT_EQ(SOFTBUS_OK, ret);
726 }
727
728 /*
729 * @tc.name: SoftBusThreadSetNameTest005
730 * @tc.desc: name is valid
731 * @tc.type: FUNC
732 * @tc.require: 1
733 */
734 HWTEST_F(SoftbusThreadTest, SoftBusThreadSetNameTest005, TestSize.Level0)
735 {
736 const char *name = "testThread";
737 SoftBusThread thread = 0;
738 SoftBusThreadAttr threadAttr = { 0 };
739 int32_t ret = SoftBusThreadAttrInit(&threadAttr);
740 EXPECT_EQ(SOFTBUS_OK, ret);
741
742 ret = SoftBusThreadCreate(&thread, &threadAttr, SoftBusThreadTask, nullptr);
743 EXPECT_EQ(SOFTBUS_OK, ret);
744 EXPECT_TRUE(thread != 0);
745
746 ret = SoftBusThreadSetName(thread, name);
747 EXPECT_EQ(SOFTBUS_OK, ret);
748 }
749
750 /*
751 * @tc.name: SoftBusThreadSetNameTest006
752 * @tc.desc: threadAttr is nullptr, name is valid
753 * @tc.type: FUNC
754 * @tc.require: 1
755 */
756 HWTEST_F(SoftbusThreadTest, SoftBusThreadSetNameTest006, TestSize.Level0)
757 {
758 const char *name = "testThread";
759 SoftBusThread thread = 0;
760
761 int32_t ret = SoftBusThreadCreate(&thread, nullptr, SoftBusThreadTask, nullptr);
762 EXPECT_EQ(SOFTBUS_OK, ret);
763 EXPECT_TRUE(thread != 0);
764
765 ret = SoftBusThreadSetName(thread, name);
766 EXPECT_EQ(SOFTBUS_OK, ret);
767 }
768
769 /*
770 * @tc.name: SoftBusThreadSetNameTest006
771 * @tc.desc: SoftBusThreadSetName will return SOFTBUS_INVALID_PARAM when thread=0
772 * @tc.type: FUNC
773 * @tc.require: 1
774 */
775 HWTEST_F(SoftbusThreadTest, SoftBusThreadSetNameTest007, TestSize.Level0)
776 {
777 const char *name = "testThread";
778 SoftBusThread thread = 0;
779
780 int32_t ret = SoftBusThreadSetName(thread, name);
781 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
782 }
783 #endif
784
785 /*
786 * @tc.name: SoftBusThreadGetSelfTest001
787 * @tc.desc: threadAttr modify prior
788 * @tc.type: FUNC
789 * @tc.require: 1
790 */
791 HWTEST_F(SoftbusThreadTest, SoftBusThreadGetSelfTest001, TestSize.Level0)
792 {
793 SoftBusThread thread = 0;
794
795 int32_t ret = SoftBusThreadCreate(&thread, nullptr, ThreadSelfTest, nullptr);
796 EXPECT_EQ(SOFTBUS_OK, ret);
797 EXPECT_TRUE(thread != 0);
798 }
799
800 /*
801 * @tc.name: SoftBusCondInitTest001
802 * @tc.desc: cond is nullptr
803 * @tc.type: FUNC
804 * @tc.require: 1
805 */
806 HWTEST_F(SoftbusThreadTest, SoftBusCondInitTest001, TestSize.Level0)
807 {
808 int32_t ret = SoftBusCondInit(nullptr);
809 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
810 }
811
812 /*
813 * @tc.name: SoftBusCondInitTest002
814 * @tc.desc: cond is valid
815 * @tc.type: FUNC
816 * @tc.require: 1
817 */
818 HWTEST_F(SoftbusThreadTest, SoftBusCondInitTest002, TestSize.Level0)
819 {
820 SoftBusCond cond = 0;
821 int32_t ret = SoftBusCondInit(&cond);
822 EXPECT_EQ(SOFTBUS_OK, ret);
823 EXPECT_TRUE(cond != 0);
824 }
825
826 /*
827 * @tc.name: SoftBusCondSignalTest001
828 * @tc.desc: cond is nullptr
829 * @tc.type: FUNC
830 * @tc.require: 1
831 */
832 HWTEST_F(SoftbusThreadTest, SoftBusCondSignalTest001, TestSize.Level0)
833 {
834 int32_t ret = SoftBusCondSignal(nullptr);
835 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
836 }
837
838 /*
839 * @tc.name: SoftBusCondSignalTest002
840 * @tc.desc: no wait thread
841 * @tc.type: FUNC
842 * @tc.require: 1
843 */
844 HWTEST_F(SoftbusThreadTest, SoftBusCondSignalTest002, TestSize.Level0)
845 {
846 SoftBusCond cond = 0;
847 int32_t ret = SoftBusCondSignal(&cond);
848 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
849 }
850
851 /*
852 * @tc.name: SoftBusCondSignalTest003
853 * @tc.desc: no wait thread
854 * @tc.type: FUNC
855 * @tc.require: 1
856 */
857 HWTEST_F(SoftbusThreadTest, SoftBusCondSignalTest003, TestSize.Level0)
858 {
859 SoftBusCond cond = 0;
860 int32_t ret = SoftBusCondInit(&cond);
861 EXPECT_EQ(SOFTBUS_OK, ret);
862 EXPECT_TRUE(cond != 0);
863 ret = SoftBusCondSignal(&cond);
864 EXPECT_EQ(SOFTBUS_OK, ret);
865 }
866
867 /*
868 * @tc.name: SoftBusCondBroadcastTest001
869 * @tc.desc: cond is nullptr
870 * @tc.type: FUNC
871 * @tc.require: 1
872 */
873 HWTEST_F(SoftbusThreadTest, SoftBusCondBroadcastTest001, TestSize.Level0)
874 {
875 int32_t ret = SoftBusCondBroadcast(nullptr);
876 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
877 }
878
879 /*
880 * @tc.name: SoftBusCondBroadcastTest002
881 * @tc.desc: cond is not init
882 * @tc.type: FUNC
883 * @tc.require: 1
884 */
885 HWTEST_F(SoftbusThreadTest, SoftBusCondBroadcastTest002, TestSize.Level0)
886 {
887 SoftBusCond cond = 0;
888
889 int32_t ret = SoftBusCondBroadcast(&cond);
890 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
891 }
892
893 /*
894 * @tc.name: SoftBusCondBroadcastTest003
895 * @tc.desc: cond is init value
896 * @tc.type: FUNC
897 * @tc.require: 1
898 */
899 HWTEST_F(SoftbusThreadTest, SoftBusCondBroadcastTest003, TestSize.Level0)
900 {
901 SoftBusCond cond = 0;
902 int32_t ret = SoftBusCondInit(&cond);
903 EXPECT_EQ(SOFTBUS_OK, ret);
904 EXPECT_TRUE(cond != 0);
905
906 ret = SoftBusCondBroadcast(&cond);
907 EXPECT_EQ(SOFTBUS_OK, ret);
908 }
909
910 /*
911 * @tc.name: SoftBusCondWaitTest001
912 * @tc.desc: cond is nullptr
913 * @tc.type: FUNC
914 * @tc.require: 1
915 */
916 HWTEST_F(SoftbusThreadTest, SoftBusCondWaitTest001, TestSize.Level0)
917 {
918 SoftBusMutex mutex = 0;
919 SoftBusMutexAttr mutexAttr = {
920 .type = SOFTBUS_MUTEX_NORMAL,
921 };
922 int32_t ret = SoftBusMutexInit(&mutex, &mutexAttr);
923 EXPECT_EQ(SOFTBUS_OK, ret);
924 ret = SoftBusCondWait(nullptr, &mutex, nullptr);
925 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
926 }
927
928 /*
929 * @tc.name: SoftBusCondWaitTest002
930 * @tc.desc: cond value is invalid
931 * @tc.type: FUNC
932 * @tc.require: 1
933 */
934 HWTEST_F(SoftbusThreadTest, SoftBusCondWaitTest002, TestSize.Level0)
935 {
936 SoftBusCond cond = 0;
937 SoftBusMutex mutex = 0;
938 SoftBusMutexAttr mutexAttr = {
939 .type = SOFTBUS_MUTEX_NORMAL,
940 };
941 int32_t ret = SoftBusMutexInit(&mutex, &mutexAttr);
942 EXPECT_EQ(SOFTBUS_OK, ret);
943 ret = SoftBusCondWait(&cond, &mutex, nullptr);
944 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
945 }
946
947 /*
948 * @tc.name: SoftBusCondWaitTest003
949 * @tc.desc: mutex is nullptr
950 * @tc.type: FUNC
951 * @tc.require: 1
952 */
953 HWTEST_F(SoftbusThreadTest, SoftBusCondWaitTest003, TestSize.Level0)
954 {
955 SoftBusCond cond = 0;
956 int32_t ret = SoftBusCondInit(&cond);
957 EXPECT_EQ(SOFTBUS_OK, ret);
958 EXPECT_TRUE(cond != 0);
959 ret = SoftBusCondWait(&cond, nullptr, nullptr);
960 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
961 }
962
963 /*
964 * @tc.name: SoftBusCondWaitTest004
965 * @tc.desc: mutex value is invalid
966 * @tc.type: FUNC
967 * @tc.require: 1
968 */
969 HWTEST_F(SoftbusThreadTest, SoftBusCondWaitTest004, TestSize.Level0)
970 {
971 SoftBusCond cond = 0;
972 SoftBusMutex mutex = 0;
973 int32_t ret = SoftBusCondInit(&cond);
974 EXPECT_EQ(SOFTBUS_OK, ret);
975 EXPECT_TRUE(cond != 0);
976
977 ret = SoftBusCondWait(&cond, &mutex, nullptr);
978 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
979 }
980
981 /*
982 * @tc.name: SoftBusCondDestroyTest001
983 * @tc.desc: cond is null
984 * @tc.type: FUNC
985 * @tc.require: 1
986 */
987 HWTEST_F(SoftbusThreadTest, SoftBusCondDestroyTest001, TestSize.Level0)
988 {
989 int32_t ret = SoftBusCondDestroy(nullptr);
990 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
991 }
992
993 /*
994 * @tc.name: SoftBusCondDestroyTest002
995 * @tc.desc: cond is valid
996 * @tc.type: FUNC
997 * @tc.require: 1
998 */
999 HWTEST_F(SoftbusThreadTest, SoftBusCondDestroyTest002, TestSize.Level0)
1000 {
1001 SoftBusCond cond = 0;
1002 int32_t ret = SoftBusCondInit(&cond);
1003 EXPECT_EQ(SOFTBUS_OK, ret);
1004 EXPECT_TRUE(cond != 0);
1005
1006 ret = SoftBusCondDestroy(&cond);
1007 EXPECT_EQ(SOFTBUS_OK, ret);
1008 }
1009
1010 /*
1011 * @tc.name: SoftBusCondDestroyTest003
1012 * @tc.desc: cond is valid
1013 * @tc.type: FUNC
1014 * @tc.require: 1
1015 */
1016 HWTEST_F(SoftbusThreadTest, SoftBusCondDestroyTest003, TestSize.Level0)
1017 {
1018 SoftBusCond cond = 0;
1019 int32_t ret = SoftBusCondDestroy(&cond);
1020 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
1021 }
1022
1023 /*
1024 * @tc.name: SoftBusThreadJoinTest001
1025 * @tc.desc: value is nullptr
1026 * @tc.type: FUNC
1027 * @tc.require: 1
1028 */
1029 HWTEST_F(SoftbusThreadTest, SoftBusThreadJoinTest001, TestSize.Level0)
1030 {
1031 SoftBusThread thread = 0;
1032 SoftBusThreadAttr threadAttr = { 0 };
1033 int32_t ret = SoftBusThreadAttrInit(&threadAttr);
1034 EXPECT_EQ(SOFTBUS_OK, ret);
1035
1036 ret = SoftBusThreadCreate(&thread, &threadAttr, SoftBusThreadTask, nullptr);
1037 EXPECT_EQ(SOFTBUS_OK, ret);
1038 EXPECT_TRUE(thread != 0);
1039 ret = SoftBusThreadJoin(thread, nullptr);
1040 EXPECT_EQ(SOFTBUS_OK, ret);
1041 }
1042
1043 /*
1044 * @tc.name: SoftBusThreadJoinTest002
1045 * @tc.desc: value is not nullptr
1046 * @tc.type: FUNC
1047 * @tc.require: 1
1048 */
1049 HWTEST_F(SoftbusThreadTest, SoftBusThreadJoinTest002, TestSize.Level0)
1050 {
1051 char *value = nullptr;
1052 SoftBusThread thread = 0;
1053 SoftBusThreadAttr threadAttr = { 0 };
1054 int32_t ret = SoftBusThreadAttrInit(&threadAttr);
1055 EXPECT_EQ(SOFTBUS_OK, ret);
1056
1057 ret = SoftBusThreadCreate(&thread, &threadAttr, SoftBusThreadTask, nullptr);
1058 EXPECT_EQ(SOFTBUS_OK, ret);
1059 EXPECT_TRUE(thread != 0);
1060 ret = SoftBusThreadJoin(thread, (void **)&value);
1061 EXPECT_EQ(SOFTBUS_OK, ret);
1062 EXPECT_TRUE(value != nullptr);
1063 }
1064
1065 /*
1066 * @tc.name: SoftBusThreadJoinTest003
1067 * @tc.desc: SoftBusThreadJoin will return SOFTBUS_INVALID_PARAM when thread=0
1068 * @tc.type: FUNC
1069 * @tc.require: 1
1070 */
1071 HWTEST_F(SoftbusThreadTest, SoftBusThreadJoinTest003, TestSize.Level0)
1072 {
1073 char *value = nullptr;
1074 SoftBusThread thread = 0;
1075 int32_t ret = SoftBusThreadJoin(thread, (void **)&value);
1076 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
1077 }
1078
1079 /*
1080 * @tc.name: SoftBusThreadFullTest001
1081 * @tc.desc: thread process test
1082 * @tc.type: FUNC
1083 * @tc.require: 1
1084 */
1085 HWTEST_F(SoftbusThreadTest, SoftBusThreadFullTest001, TestSize.Level0)
1086 {
1087 int32_t ret = SoftBusMutexInit(&g_mutex, nullptr);
1088 EXPECT_EQ(SOFTBUS_OK, ret);
1089
1090 ret = SoftBusCondInit(&g_cond);
1091 EXPECT_EQ(SOFTBUS_OK, ret);
1092
1093 SoftBusThread threadWait = 0;
1094 SoftBusThread threadSignal = 0;
1095 SoftBusThreadAttr threadAttr = { 0 };
1096 ret = SoftBusThreadAttrInit(&threadAttr);
1097 EXPECT_EQ(SOFTBUS_OK, ret);
1098
1099 ret = SoftBusThreadCreate(&threadWait, &threadAttr, ThreadWaitTest, nullptr);
1100 EXPECT_EQ(SOFTBUS_OK, ret);
1101 EXPECT_TRUE(threadWait != 0);
1102
1103 ret = SoftBusThreadCreate(&threadSignal, &threadAttr, ThreadSignalTest, nullptr);
1104 EXPECT_EQ(SOFTBUS_OK, ret);
1105 EXPECT_TRUE(threadSignal != 0);
1106
1107 ret = SoftBusThreadJoin(threadWait, nullptr);
1108 EXPECT_EQ(SOFTBUS_OK, ret);
1109 ret = SoftBusThreadJoin(threadSignal, nullptr);
1110 EXPECT_EQ(SOFTBUS_OK, ret);
1111 }
1112 } // namespace OHOS
1113