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: SoftBusMutexLockTest001
153 * @tc.desc: mutex is nullptr
154 * @tc.type: FUNC
155 * @tc.require: 1
156 */
157 HWTEST_F(SoftbusThreadTest, SoftBusMutexLockTest001, TestSize.Level0)
158 {
159 int32_t ret = SoftBusMutexLock(nullptr);
160 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
161 }
162
163 /*
164 * @tc.name: SoftBusMutexLockTest002
165 * @tc.desc: mutexAttr type is SOFTBUS_MUTEX_NORMAL
166 * @tc.type: FUNC
167 * @tc.require: 1
168 */
169 HWTEST_F(SoftbusThreadTest, SoftBusMutexLockTest002, TestSize.Level0)
170 {
171 SoftBusMutex mutex = 0;
172 SoftBusMutexAttr mutexAttr = {
173 .type = SOFTBUS_MUTEX_NORMAL,
174 };
175 int32_t ret = SoftBusMutexInit(&mutex, &mutexAttr);
176 EXPECT_EQ(SOFTBUS_OK, ret);
177 ret = SoftBusMutexLock(&mutex);
178 EXPECT_EQ(SOFTBUS_OK, ret);
179 }
180
181 /*
182 * @tc.name: SoftBusMutexLockTest003
183 * @tc.desc: mutexAttr type is SOFTBUS_MUTEX_RECURSIVE
184 * @tc.type: FUNC
185 * @tc.require: 1
186 */
187 HWTEST_F(SoftbusThreadTest, SoftBusMutexLockTest003, TestSize.Level0)
188 {
189 SoftBusMutex mutex = 0;
190 SoftBusMutexAttr mutexAttr = {
191 .type = SOFTBUS_MUTEX_RECURSIVE,
192 };
193 int32_t ret = SoftBusMutexInit(&mutex, &mutexAttr);
194 EXPECT_EQ(SOFTBUS_OK, ret);
195 ret = SoftBusMutexLock(&mutex);
196 EXPECT_EQ(SOFTBUS_OK, ret);
197 }
198
199 /*
200 * @tc.name: SoftBusMutexLockTest004
201 * @tc.desc: mutex is default
202 * @tc.type: FUNC
203 * @tc.require: 1
204 */
205 HWTEST_F(SoftbusThreadTest, SoftBusMutexLockTest004, TestSize.Level0)
206 {
207 SoftBusMutex mutex = 0;
208 int32_t ret = SoftBusMutexInit(&mutex, nullptr);
209 ret = SoftBusMutexLock(&mutex);
210 EXPECT_EQ(SOFTBUS_OK, ret);
211 }
212
213 /*
214 * @tc.name: SoftBusMutexLockTest005
215 * @tc.desc: mutex value is 0
216 * @tc.type: FUNC
217 * @tc.require: 1
218 */
219 HWTEST_F(SoftbusThreadTest, SoftBusMutexLockTest005, TestSize.Level0)
220 {
221 SoftBusMutex mutex = 0;
222 int32_t ret = SoftBusMutexLock(&mutex);
223 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
224 }
225
226 /*
227 * @tc.name: SoftBusMutexUnlockTest001
228 * @tc.desc: mutex is nullptr
229 * @tc.type: FUNC
230 * @tc.require: 1
231 */
232 HWTEST_F(SoftbusThreadTest, SoftBusMutexUnlockTest001, TestSize.Level0)
233 {
234 int32_t ret = SoftBusMutexUnlock(nullptr);
235 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
236 }
237
238 /*
239 * @tc.name: SoftBusMutexUnlockTest002
240 * @tc.desc: mutex value is 0
241 * @tc.type: FUNC
242 * @tc.require: 1
243 */
244 HWTEST_F(SoftbusThreadTest, SoftBusMutexUnlockTest002, TestSize.Level0)
245 {
246 SoftBusMutex mutex = 0;
247 int32_t ret = SoftBusMutexUnlock(&mutex);
248 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
249 }
250
251 /*
252 * @tc.name: SoftBusMutexUnlockTest003
253 * @tc.desc: mutexAttr type is SOFTBUS_MUTEX_NORMAL
254 * @tc.type: FUNC
255 * @tc.require: 1
256 */
257 HWTEST_F(SoftbusThreadTest, SoftBusMutexUnlockTest003, TestSize.Level0)
258 {
259 SoftBusMutex mutex = 0;
260 SoftBusMutexAttr mutexAttr = {
261 .type = SOFTBUS_MUTEX_NORMAL,
262 };
263 int32_t ret = SoftBusMutexInit(&mutex, &mutexAttr);
264 EXPECT_EQ(SOFTBUS_OK, ret);
265 ret = SoftBusMutexLock(&mutex);
266 EXPECT_EQ(SOFTBUS_OK, ret);
267 ret = SoftBusMutexUnlock(&mutex);
268 EXPECT_EQ(SOFTBUS_OK, ret);
269 }
270
271 /*
272 * @tc.name: SoftBusMutexUnlockTest004
273 * @tc.desc: mutexAttr type is SOFTBUS_MUTEX_NORMAL
274 * @tc.type: FUNC
275 * @tc.require: 1
276 */
277 HWTEST_F(SoftbusThreadTest, SoftBusMutexUnlockTest004, TestSize.Level0)
278 {
279 SoftBusMutex mutex = 0;
280 SoftBusMutexAttr mutexAttr = {
281 .type = SOFTBUS_MUTEX_RECURSIVE,
282 };
283 int32_t ret = SoftBusMutexInit(&mutex, &mutexAttr);
284 EXPECT_EQ(SOFTBUS_OK, ret);
285 ret = SoftBusMutexLock(&mutex);
286 EXPECT_EQ(SOFTBUS_OK, ret);
287 ret = SoftBusMutexUnlock(&mutex);
288 EXPECT_EQ(SOFTBUS_OK, ret);
289 }
290
291 /*
292 * @tc.name: SoftBusMutexUnlockTest005
293 * @tc.desc: mutex value is default
294 * @tc.type: FUNC
295 * @tc.require: 1
296 */
297 HWTEST_F(SoftbusThreadTest, SoftBusMutexUnlockTest005, TestSize.Level0)
298 {
299 SoftBusMutex mutex = 0;
300 int32_t ret = SoftBusMutexInit(&mutex, nullptr);
301 EXPECT_EQ(SOFTBUS_OK, ret);
302 ret = SoftBusMutexLock(&mutex);
303 EXPECT_EQ(SOFTBUS_OK, ret);
304 ret = SoftBusMutexUnlock(&mutex);
305 EXPECT_EQ(SOFTBUS_OK, ret);
306 }
307
308 /*
309 * @tc.name: SoftBusMutexDestroyTest001
310 * @tc.desc: mutex is nullptr
311 * @tc.type: FUNC
312 * @tc.require: 1
313 */
314 HWTEST_F(SoftbusThreadTest, SoftBusMutexDestroyTest001, TestSize.Level0)
315 {
316 int32_t ret = SoftBusMutexDestroy(nullptr);
317 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
318 }
319
320 /*
321 * @tc.name: SoftBusMutexDestroyTest002
322 * @tc.desc: mutex value is 0
323 * @tc.type: FUNC
324 * @tc.require: 1
325 */
326 HWTEST_F(SoftbusThreadTest, SoftBusMutexDestroyTest002, TestSize.Level0)
327 {
328 SoftBusMutex mutex = 0;
329 int32_t ret = SoftBusMutexDestroy(&mutex);
330 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
331 }
332
333 /*
334 * @tc.name: SoftBusMutexDestroyTest003
335 * @tc.desc: mutexAttr is nullptr
336 * @tc.type: FUNC
337 * @tc.require: 1
338 */
339 HWTEST_F(SoftbusThreadTest, SoftBusMutexDestroyTest003, TestSize.Level0)
340 {
341 SoftBusMutex mutex = 0;
342 int32_t ret = SoftBusMutexInit(&mutex, nullptr);
343 EXPECT_EQ(SOFTBUS_OK, ret);
344
345 ret = SoftBusMutexDestroy(&mutex);
346 EXPECT_EQ(SOFTBUS_OK, ret);
347 }
348
349 /*
350 * @tc.name: SoftBusMutexDestroyTest004
351 * @tc.desc: mutexAttr is SOFTBUS_MUTEX_NORMAL
352 * @tc.type: FUNC
353 * @tc.require: 1
354 */
355 HWTEST_F(SoftbusThreadTest, SoftBusMutexDestroyTest004, TestSize.Level0)
356 {
357 SoftBusMutex mutex = 0;
358 SoftBusMutexAttr mutexAttr = {
359 .type = SOFTBUS_MUTEX_NORMAL,
360 };
361 int32_t ret = SoftBusMutexInit(&mutex, &mutexAttr);
362 EXPECT_EQ(SOFTBUS_OK, ret);
363
364 ret = SoftBusMutexDestroy(&mutex);
365 EXPECT_EQ(SOFTBUS_OK, ret);
366 }
367
368 /*
369 * @tc.name: SoftBusMutexDestroyTest005
370 * @tc.desc: mutexAttr is SOFTBUS_MUTEX_RECURSIVE
371 * @tc.type: FUNC
372 * @tc.require: 1
373 */
374 HWTEST_F(SoftbusThreadTest, SoftBusMutexDestroyTest005, TestSize.Level0)
375 {
376 SoftBusMutex mutex = 0;
377 SoftBusMutexAttr mutexAttr = {
378 .type = SOFTBUS_MUTEX_RECURSIVE,
379 };
380 int32_t ret = SoftBusMutexInit(&mutex, &mutexAttr);
381 EXPECT_EQ(SOFTBUS_OK, ret);
382
383 ret = SoftBusMutexDestroy(&mutex);
384 EXPECT_EQ(SOFTBUS_OK, ret);
385 }
386
387 /*
388 * @tc.name: SoftBusMutexLockGuardTest001
389 * @tc.desc: should call SoftBusMutexUnlock automatically when leave bracket scope
390 * @tc.type: FUNC
391 * @tc.require:
392 */
393 HWTEST_F(SoftbusThreadTest, SoftBusMutexLockGuardTest001, TestSize.Level0)
394 {
395 SoftBusMutex mutex = 0;
396 int32_t ret = SoftBusMutexInit(&mutex, nullptr);
397 EXPECT_EQ(SOFTBUS_OK, ret);
398 {
399 ret = SoftBusMutexLock(&mutex);
400 EXPECT_EQ(SOFTBUS_OK, ret);
401 SOFTBUS_LOCK_GUARD(mutex);
402 }
403 {
404 ret = SoftBusMutexLock(&mutex);
405 EXPECT_EQ(SOFTBUS_OK, ret);
406 SOFTBUS_LOCK_GUARD(mutex);
407 }
408 ret = SoftBusMutexDestroy(&mutex);
409 EXPECT_EQ(SOFTBUS_OK, ret);
410 }
411
412 /*
413 * @tc.name: SoftBusThreadAttrInitTest001
414 * @tc.desc: threadAttr is nullptr
415 * @tc.type: FUNC
416 * @tc.require: 1
417 */
418 HWTEST_F(SoftbusThreadTest, SoftBusThreadAttrInitTest001, TestSize.Level0)
419 {
420 int32_t ret = SoftBusThreadAttrInit(nullptr);
421 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
422 }
423
424 /*
425 * @tc.name: SoftBusThreadAttrInitTest002
426 * @tc.desc: threadAttr is valid
427 * @tc.type: FUNC
428 * @tc.require: 1
429 */
430 HWTEST_F(SoftbusThreadTest, SoftBusThreadAttrInitTest002, TestSize.Level0)
431 {
432 SoftBusThreadAttr threadAttr = { 0 };
433 int32_t ret = SoftBusThreadAttrInit(&threadAttr);
434 EXPECT_EQ(SOFTBUS_OK, ret);
435 }
436
437 /*
438 * @tc.name: SoftBusThreadCreateTest001
439 * @tc.desc: thread is nullptr
440 * @tc.type: FUNC
441 * @tc.require: 1
442 */
443 HWTEST_F(SoftbusThreadTest, SoftBusThreadCreateTest001, TestSize.Level0)
444 {
445 SoftBusThreadAttr threadAttr = { 0 };
446 int32_t ret = SoftBusThreadAttrInit(&threadAttr);
447 EXPECT_EQ(SOFTBUS_OK, ret);
448
449 ret = SoftBusThreadCreate(nullptr, &threadAttr, SoftBusThreadTask, nullptr);
450 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
451 }
452
453 /*
454 * @tc.name: SoftBusThreadCreateTest002
455 * @tc.desc: threadAttr is nullptr
456 * @tc.type: FUNC
457 * @tc.require: 1
458 */
459 HWTEST_F(SoftbusThreadTest, SoftBusThreadCreateTest002, TestSize.Level0)
460 {
461 SoftBusThread thread = 0;
462
463 int32_t ret = SoftBusThreadCreate(&thread, nullptr, SoftBusThreadTask, nullptr);
464 EXPECT_EQ(SOFTBUS_OK, ret);
465 EXPECT_TRUE(thread != 0);
466 }
467
468 /*
469 * @tc.name: SoftBusThreadCreateTest003
470 * @tc.desc: threadAttr is valid
471 * @tc.type: FUNC
472 * @tc.require: 1
473 */
474 HWTEST_F(SoftbusThreadTest, SoftBusThreadCreateTest003, TestSize.Level0)
475 {
476 SoftBusThread thread = 0;
477 SoftBusThreadAttr threadAttr = { 0 };
478 int32_t ret = SoftBusThreadAttrInit(&threadAttr);
479 EXPECT_EQ(SOFTBUS_OK, ret);
480
481 ret = SoftBusThreadCreate(&thread, &threadAttr, SoftBusThreadTask, nullptr);
482 EXPECT_EQ(SOFTBUS_OK, ret);
483 EXPECT_TRUE(thread != 0);
484 }
485
486 #if HAVE_PRO
487 /*
488 * @tc.name: SoftBusThreadCreateTest004
489 * @tc.desc: threadAttr add taskName
490 * @tc.type: FUNC
491 * @tc.require: 1
492 */
493 HWTEST_F(SoftbusThreadTest, SoftBusThreadCreateTest004, TestSize.Level0)
494 {
495 SoftBusThread thread = 0;
496 SoftBusThreadAttr threadAttr = { 0 };
497 int32_t ret = SoftBusThreadAttrInit(&threadAttr);
498 EXPECT_EQ(SOFTBUS_OK, ret);
499
500 threadAttr.taskName = "ThreadTask";
501 ret = SoftBusThreadCreate(&thread, &threadAttr, SoftBusThreadTask, nullptr);
502 EXPECT_EQ(SOFTBUS_ERR, ret);
503 EXPECT_TRUE(thread != 0);
504 }
505 #endif
506
507 /*
508 * @tc.name: SoftBusThreadCreateTest005
509 * @tc.desc: threadAttr modify prior
510 * @tc.type: FUNC
511 * @tc.require: 1
512 */
513 HWTEST_F(SoftbusThreadTest, SoftBusThreadCreateTest005, TestSize.Level0)
514 {
515 SoftBusThread thread = 0;
516 SoftBusThreadAttr threadAttr = { 0 };
517 int32_t ret = SoftBusThreadAttrInit(&threadAttr);
518 EXPECT_EQ(SOFTBUS_OK, ret);
519
520 threadAttr.prior = SOFTBUS_PRIORITY_HIGHEST;
521 ret = SoftBusThreadCreate(&thread, &threadAttr, SoftBusThreadTask, nullptr);
522 EXPECT_EQ(SOFTBUS_OK, ret);
523 EXPECT_TRUE(thread != 0);
524 }
525
526 /*
527 * @tc.name: SoftBusThreadCreateTest006
528 * @tc.desc: threadAttr modify prior
529 * @tc.type: FUNC
530 * @tc.require: 1
531 */
532 HWTEST_F(SoftbusThreadTest, SoftBusThreadCreateTest006, TestSize.Level0)
533 {
534 SoftBusThread thread = 0;
535 SoftBusThreadAttr threadAttr = { 0 };
536 int32_t ret = SoftBusThreadAttrInit(&threadAttr);
537 EXPECT_EQ(SOFTBUS_OK, ret);
538
539 threadAttr.prior = SOFTBUS_PRIORITY_HIGH;
540 ret = SoftBusThreadCreate(&thread, &threadAttr, SoftBusThreadTask, nullptr);
541 EXPECT_EQ(SOFTBUS_OK, ret);
542 EXPECT_TRUE(thread != 0);
543 }
544
545 /*
546 * @tc.name: SoftBusThreadCreateTest007
547 * @tc.desc: threadAttr modify prior
548 * @tc.type: FUNC
549 * @tc.require: 1
550 */
551 HWTEST_F(SoftbusThreadTest, SoftBusThreadCreateTest007, TestSize.Level0)
552 {
553 SoftBusThread thread = 0;
554 SoftBusThreadAttr threadAttr = { 0 };
555 int32_t ret = SoftBusThreadAttrInit(&threadAttr);
556 EXPECT_EQ(SOFTBUS_OK, ret);
557
558 threadAttr.prior = SOFTBUS_PRIORITY_DEFAULT;
559 ret = SoftBusThreadCreate(&thread, &threadAttr, SoftBusThreadTask, nullptr);
560 EXPECT_EQ(SOFTBUS_OK, ret);
561 EXPECT_TRUE(thread != 0);
562 }
563
564 /*
565 * @tc.name: SoftBusThreadCreateTest008
566 * @tc.desc: threadAttr modify prior
567 * @tc.type: FUNC
568 * @tc.require: 1
569 */
570 HWTEST_F(SoftbusThreadTest, SoftBusThreadCreateTest008, TestSize.Level0)
571 {
572 SoftBusThread thread = 0;
573 SoftBusThreadAttr threadAttr = { 0 };
574 int32_t ret = SoftBusThreadAttrInit(&threadAttr);
575 EXPECT_EQ(SOFTBUS_OK, ret);
576
577 threadAttr.prior = SOFTBUS_PRIORITY_LOW;
578 ret = SoftBusThreadCreate(&thread, &threadAttr, SoftBusThreadTask, nullptr);
579 EXPECT_EQ(SOFTBUS_OK, ret);
580 EXPECT_TRUE(thread != 0);
581 }
582
583 /*
584 * @tc.name: SoftBusThreadCreateTest009
585 * @tc.desc: threadAttr modify prior
586 * @tc.type: FUNC
587 * @tc.require: 1
588 */
589 HWTEST_F(SoftbusThreadTest, SoftBusThreadCreateTest009, TestSize.Level0)
590 {
591 SoftBusThread thread = 0;
592 SoftBusThreadAttr threadAttr = { 0 };
593 int32_t ret = SoftBusThreadAttrInit(&threadAttr);
594 EXPECT_EQ(SOFTBUS_OK, ret);
595
596 threadAttr.prior = SOFTBUS_PRIORITY_LOWEST;
597 ret = SoftBusThreadCreate(&thread, &threadAttr, SoftBusThreadTask, nullptr);
598 EXPECT_EQ(SOFTBUS_OK, ret);
599 EXPECT_TRUE(thread != 0);
600 }
601
602 #if HAVE_PRO
603 /*
604 * @tc.name: SoftBusThreadCreateTest010
605 * @tc.desc: threadEntry is nullptr
606 * @tc.type: FUNC
607 * @tc.require: 1
608 */
609 HWTEST_F(SoftbusThreadTest, SoftBusThreadCreateTest010, TestSize.Level0)
610 {
611 SoftBusThread thread = 0;
612 SoftBusThreadAttr threadAttr = { 0 };
613 int32_t ret = SoftBusThreadAttrInit(&threadAttr);
614 EXPECT_EQ(SOFTBUS_OK, ret);
615
616 ret = SoftBusThreadCreate(&thread, &threadAttr, nullptr, nullptr);
617 EXPECT_EQ(SOFTBUS_ERR, ret);
618 }
619
620 /*
621 * @tc.name: SoftBusThreadSetNameTest001
622 * @tc.desc: name is nullptr
623 * @tc.type: FUNC
624 * @tc.require: 1
625 */
626 HWTEST_F(SoftbusThreadTest, SoftBusThreadSetNameTest001, TestSize.Level0)
627 {
628 SoftBusThread thread = 0;
629 SoftBusThreadAttr threadAttr = { 0 };
630 int32_t ret = SoftBusThreadAttrInit(&threadAttr);
631 EXPECT_EQ(SOFTBUS_OK, ret);
632
633 threadAttr.prior = SOFTBUS_PRIORITY_HIGHEST;
634
635 ret = SoftBusThreadCreate(&thread, &threadAttr, SoftBusThreadTask, nullptr);
636 EXPECT_EQ(SOFTBUS_OK, ret);
637 EXPECT_TRUE(thread != 0);
638
639 ret = SoftBusThreadSetName(thread, nullptr);
640 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
641 }
642
643 /*
644 * @tc.name: SoftBusThreadSetNameTest002
645 * @tc.desc: name is large than TASK_NAME_MAX_LEN
646 * @tc.type: FUNC
647 * @tc.require: 1
648 */
649 HWTEST_F(SoftbusThreadTest, SoftBusThreadSetNameTest002, TestSize.Level0)
650 {
651 const char *name = "abcdefghijklmnopqrstuvwxyz";
652 SoftBusThread thread = 0;
653 SoftBusThreadAttr threadAttr = { 0 };
654 int32_t ret = SoftBusThreadAttrInit(&threadAttr);
655 EXPECT_EQ(SOFTBUS_OK, ret);
656
657 threadAttr.prior = SOFTBUS_PRIORITY_HIGHEST;
658
659 ret = SoftBusThreadCreate(&thread, &threadAttr, SoftBusThreadTask, nullptr);
660 EXPECT_EQ(SOFTBUS_OK, ret);
661 EXPECT_TRUE(thread != 0);
662
663 ret = SoftBusThreadSetName(thread, name);
664 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
665 }
666
667 /*
668 * @tc.name: SoftBusThreadSetNameTest003
669 * @tc.desc: name is equal to TASK_NAME_MAX_LEN
670 * @tc.type: FUNC
671 * @tc.require: 1
672 */
673 HWTEST_F(SoftbusThreadTest, SoftBusThreadSetNameTest003, TestSize.Level0)
674 {
675 const char *name = "abcdefghijklmnop";
676 SoftBusThread thread = 0;
677 SoftBusThreadAttr threadAttr = { 0 };
678 int32_t ret = SoftBusThreadAttrInit(&threadAttr);
679 EXPECT_EQ(SOFTBUS_OK, ret);
680
681 threadAttr.prior = SOFTBUS_PRIORITY_HIGHEST;
682
683 ret = SoftBusThreadCreate(&thread, &threadAttr, SoftBusThreadTask, nullptr);
684 EXPECT_EQ(SOFTBUS_OK, ret);
685 EXPECT_TRUE(thread != 0);
686
687 ret = SoftBusThreadSetName(thread, name);
688 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
689 }
690
691 /*
692 * @tc.name: SoftBusThreadSetNameTest004
693 * @tc.desc: name include chinese character
694 * @tc.type: FUNC
695 * @tc.require: 1
696 */
697 HWTEST_F(SoftbusThreadTest, SoftBusThreadSetNameTest004, TestSize.Level0)
698 {
699 const char *name = "a中文p";
700 SoftBusThread thread = 0;
701 SoftBusThreadAttr threadAttr = { 0 };
702 int32_t ret = SoftBusThreadAttrInit(&threadAttr);
703 EXPECT_EQ(SOFTBUS_OK, ret);
704
705 ret = SoftBusThreadCreate(&thread, &threadAttr, SoftBusThreadTask, nullptr);
706 EXPECT_EQ(SOFTBUS_OK, ret);
707 EXPECT_TRUE(thread != 0);
708
709 ret = SoftBusThreadSetName(thread, name);
710 EXPECT_EQ(SOFTBUS_ERR, ret);
711 }
712
713 /*
714 * @tc.name: SoftBusThreadSetNameTest005
715 * @tc.desc: name is valid
716 * @tc.type: FUNC
717 * @tc.require: 1
718 */
719 HWTEST_F(SoftbusThreadTest, SoftBusThreadSetNameTest005, TestSize.Level0)
720 {
721 const char *name = "testThread";
722 SoftBusThread thread = 0;
723 SoftBusThreadAttr threadAttr = { 0 };
724 int32_t ret = SoftBusThreadAttrInit(&threadAttr);
725 EXPECT_EQ(SOFTBUS_OK, ret);
726
727 ret = SoftBusThreadCreate(&thread, &threadAttr, SoftBusThreadTask, nullptr);
728 EXPECT_EQ(SOFTBUS_OK, ret);
729 EXPECT_TRUE(thread != 0);
730
731 ret = SoftBusThreadSetName(thread, name);
732 EXPECT_EQ(SOFTBUS_ERR, ret);
733 }
734
735 /*
736 * @tc.name: SoftBusThreadSetNameTest006
737 * @tc.desc: threadAttr is nullptr, name is valid
738 * @tc.type: FUNC
739 * @tc.require: 1
740 */
741 HWTEST_F(SoftbusThreadTest, SoftBusThreadSetNameTest006, TestSize.Level0)
742 {
743 const char *name = "testThread";
744 SoftBusThread thread = 0;
745
746 int32_t ret = SoftBusThreadCreate(&thread, nullptr, SoftBusThreadTask, nullptr);
747 EXPECT_EQ(SOFTBUS_OK, ret);
748 EXPECT_TRUE(thread != 0);
749
750 ret = SoftBusThreadSetName(thread, name);
751 EXPECT_EQ(SOFTBUS_ERR, ret);
752 }
753 #endif
754
755 /*
756 * @tc.name: SoftBusThreadGetSelfTest001
757 * @tc.desc: threadAttr modify prior
758 * @tc.type: FUNC
759 * @tc.require: 1
760 */
761 HWTEST_F(SoftbusThreadTest, SoftBusThreadGetSelfTest001, TestSize.Level0)
762 {
763 SoftBusThread thread = 0;
764
765 int32_t ret = SoftBusThreadCreate(&thread, nullptr, ThreadSelfTest, nullptr);
766 EXPECT_EQ(SOFTBUS_OK, ret);
767 EXPECT_TRUE(thread != 0);
768 }
769
770 /*
771 * @tc.name: SoftBusCondInitTest001
772 * @tc.desc: cond is nullptr
773 * @tc.type: FUNC
774 * @tc.require: 1
775 */
776 HWTEST_F(SoftbusThreadTest, SoftBusCondInitTest001, TestSize.Level0)
777 {
778 int32_t ret = SoftBusCondInit(nullptr);
779 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
780 }
781
782 /*
783 * @tc.name: SoftBusCondInitTest002
784 * @tc.desc: cond is valid
785 * @tc.type: FUNC
786 * @tc.require: 1
787 */
788 HWTEST_F(SoftbusThreadTest, SoftBusCondInitTest002, TestSize.Level0)
789 {
790 SoftBusCond cond = 0;
791 int32_t ret = SoftBusCondInit(&cond);
792 EXPECT_EQ(SOFTBUS_OK, ret);
793 EXPECT_TRUE(cond != 0);
794 }
795
796 /*
797 * @tc.name: SoftBusCondSignalTest001
798 * @tc.desc: cond is nullptr
799 * @tc.type: FUNC
800 * @tc.require: 1
801 */
802 HWTEST_F(SoftbusThreadTest, SoftBusCondSignalTest001, TestSize.Level0)
803 {
804 int32_t ret = SoftBusCondSignal(nullptr);
805 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
806 }
807
808 /*
809 * @tc.name: SoftBusCondSignalTest002
810 * @tc.desc: no wait thread
811 * @tc.type: FUNC
812 * @tc.require: 1
813 */
814 HWTEST_F(SoftbusThreadTest, SoftBusCondSignalTest002, TestSize.Level0)
815 {
816 SoftBusCond cond = 0;
817 int32_t ret = SoftBusCondSignal(&cond);
818 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
819 }
820
821 /*
822 * @tc.name: SoftBusCondSignalTest003
823 * @tc.desc: no wait thread
824 * @tc.type: FUNC
825 * @tc.require: 1
826 */
827 HWTEST_F(SoftbusThreadTest, SoftBusCondSignalTest003, TestSize.Level0)
828 {
829 SoftBusCond cond = 0;
830 int32_t ret = SoftBusCondInit(&cond);
831 EXPECT_EQ(SOFTBUS_OK, ret);
832 EXPECT_TRUE(cond != 0);
833 ret = SoftBusCondSignal(&cond);
834 EXPECT_EQ(SOFTBUS_OK, ret);
835 }
836
837 /*
838 * @tc.name: SoftBusCondBroadcastTest001
839 * @tc.desc: cond is nullptr
840 * @tc.type: FUNC
841 * @tc.require: 1
842 */
843 HWTEST_F(SoftbusThreadTest, SoftBusCondBroadcastTest001, TestSize.Level0)
844 {
845 int32_t ret = SoftBusCondBroadcast(nullptr);
846 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
847 }
848
849 /*
850 * @tc.name: SoftBusCondBroadcastTest002
851 * @tc.desc: cond is not init
852 * @tc.type: FUNC
853 * @tc.require: 1
854 */
855 HWTEST_F(SoftbusThreadTest, SoftBusCondBroadcastTest002, TestSize.Level0)
856 {
857 SoftBusCond cond = 0;
858
859 int32_t ret = SoftBusCondBroadcast(&cond);
860 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
861 }
862
863 /*
864 * @tc.name: SoftBusCondBroadcastTest003
865 * @tc.desc: cond is init value
866 * @tc.type: FUNC
867 * @tc.require: 1
868 */
869 HWTEST_F(SoftbusThreadTest, SoftBusCondBroadcastTest003, TestSize.Level0)
870 {
871 SoftBusCond cond = 0;
872 int32_t ret = SoftBusCondInit(&cond);
873 EXPECT_EQ(SOFTBUS_OK, ret);
874 EXPECT_TRUE(cond != 0);
875
876 ret = SoftBusCondBroadcast(&cond);
877 EXPECT_EQ(SOFTBUS_OK, ret);
878 }
879
880 /*
881 * @tc.name: SoftBusCondWaitTest001
882 * @tc.desc: cond is nullptr
883 * @tc.type: FUNC
884 * @tc.require: 1
885 */
886 HWTEST_F(SoftbusThreadTest, SoftBusCondWaitTest001, TestSize.Level0)
887 {
888 SoftBusMutex mutex = 0;
889 SoftBusMutexAttr mutexAttr = {
890 .type = SOFTBUS_MUTEX_NORMAL,
891 };
892 int32_t ret = SoftBusMutexInit(&mutex, &mutexAttr);
893 EXPECT_EQ(SOFTBUS_OK, ret);
894 ret = SoftBusCondWait(nullptr, &mutex, nullptr);
895 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
896 }
897
898 /*
899 * @tc.name: SoftBusCondWaitTest002
900 * @tc.desc: cond value is invalid
901 * @tc.type: FUNC
902 * @tc.require: 1
903 */
904 HWTEST_F(SoftbusThreadTest, SoftBusCondWaitTest002, TestSize.Level0)
905 {
906 SoftBusCond cond = 0;
907 SoftBusMutex mutex = 0;
908 SoftBusMutexAttr mutexAttr = {
909 .type = SOFTBUS_MUTEX_NORMAL,
910 };
911 int32_t ret = SoftBusMutexInit(&mutex, &mutexAttr);
912 EXPECT_EQ(SOFTBUS_OK, ret);
913 ret = SoftBusCondWait(&cond, &mutex, nullptr);
914 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
915 }
916
917 /*
918 * @tc.name: SoftBusCondWaitTest003
919 * @tc.desc: mutex is nullptr
920 * @tc.type: FUNC
921 * @tc.require: 1
922 */
923 HWTEST_F(SoftbusThreadTest, SoftBusCondWaitTest003, TestSize.Level0)
924 {
925 SoftBusCond cond = 0;
926 int32_t ret = SoftBusCondInit(&cond);
927 EXPECT_EQ(SOFTBUS_OK, ret);
928 EXPECT_TRUE(cond != 0);
929 ret = SoftBusCondWait(&cond, nullptr, nullptr);
930 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
931 }
932
933 /*
934 * @tc.name: SoftBusCondWaitTest004
935 * @tc.desc: mutex value is invalid
936 * @tc.type: FUNC
937 * @tc.require: 1
938 */
939 HWTEST_F(SoftbusThreadTest, SoftBusCondWaitTest004, TestSize.Level0)
940 {
941 SoftBusCond cond = 0;
942 SoftBusMutex mutex = 0;
943 int32_t ret = SoftBusCondInit(&cond);
944 EXPECT_EQ(SOFTBUS_OK, ret);
945 EXPECT_TRUE(cond != 0);
946
947 ret = SoftBusCondWait(&cond, &mutex, nullptr);
948 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
949 }
950
951 /*
952 * @tc.name: SoftBusCondDestroyTest001
953 * @tc.desc: cond is null
954 * @tc.type: FUNC
955 * @tc.require: 1
956 */
957 HWTEST_F(SoftbusThreadTest, SoftBusCondDestroyTest001, TestSize.Level0)
958 {
959 int32_t ret = SoftBusCondDestroy(nullptr);
960 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
961 }
962
963 /*
964 * @tc.name: SoftBusCondDestroyTest002
965 * @tc.desc: cond is valid
966 * @tc.type: FUNC
967 * @tc.require: 1
968 */
969 HWTEST_F(SoftbusThreadTest, SoftBusCondDestroyTest002, TestSize.Level0)
970 {
971 SoftBusCond cond = 0;
972 int32_t ret = SoftBusCondInit(&cond);
973 EXPECT_EQ(SOFTBUS_OK, ret);
974 EXPECT_TRUE(cond != 0);
975
976 ret = SoftBusCondDestroy(&cond);
977 EXPECT_EQ(SOFTBUS_OK, ret);
978 }
979
980 /*
981 * @tc.name: SoftBusCondDestroyTest003
982 * @tc.desc: cond is valid
983 * @tc.type: FUNC
984 * @tc.require: 1
985 */
986 HWTEST_F(SoftbusThreadTest, SoftBusCondDestroyTest003, TestSize.Level0)
987 {
988 SoftBusCond cond = 0;
989 int32_t ret = SoftBusCondDestroy(&cond);
990 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
991 }
992
993 /*
994 * @tc.name: SoftBusThreadJoinTest001
995 * @tc.desc: value is nullptr
996 * @tc.type: FUNC
997 * @tc.require: 1
998 */
999 HWTEST_F(SoftbusThreadTest, SoftBusThreadJoinTest001, TestSize.Level0)
1000 {
1001 SoftBusThread thread = 0;
1002 SoftBusThreadAttr threadAttr = { 0 };
1003 int32_t ret = SoftBusThreadAttrInit(&threadAttr);
1004 EXPECT_EQ(SOFTBUS_OK, ret);
1005
1006 ret = SoftBusThreadCreate(&thread, &threadAttr, SoftBusThreadTask, nullptr);
1007 EXPECT_EQ(SOFTBUS_OK, ret);
1008 EXPECT_TRUE(thread != 0);
1009 ret = SoftBusThreadJoin(thread, nullptr);
1010 EXPECT_EQ(SOFTBUS_OK, ret);
1011 }
1012
1013 /*
1014 * @tc.name: SoftBusThreadJoinTest002
1015 * @tc.desc: value is not nullptr
1016 * @tc.type: FUNC
1017 * @tc.require: 1
1018 */
1019 HWTEST_F(SoftbusThreadTest, SoftBusThreadJoinTest002, TestSize.Level0)
1020 {
1021 char *value = nullptr;
1022 SoftBusThread thread = 0;
1023 SoftBusThreadAttr threadAttr = { 0 };
1024 int32_t ret = SoftBusThreadAttrInit(&threadAttr);
1025 EXPECT_EQ(SOFTBUS_OK, ret);
1026
1027 ret = SoftBusThreadCreate(&thread, &threadAttr, SoftBusThreadTask, nullptr);
1028 EXPECT_EQ(SOFTBUS_OK, ret);
1029 EXPECT_TRUE(thread != 0);
1030 ret = SoftBusThreadJoin(thread, (void **)&value);
1031 EXPECT_EQ(SOFTBUS_OK, ret);
1032 EXPECT_TRUE(value != nullptr);
1033 }
1034
1035 /*
1036 * @tc.name: SoftBusThreadFullTest001
1037 * @tc.desc: thread process test
1038 * @tc.type: FUNC
1039 * @tc.require: 1
1040 */
1041 HWTEST_F(SoftbusThreadTest, SoftBusThreadFullTest001, TestSize.Level0)
1042 {
1043 int32_t ret = SoftBusMutexInit(&g_mutex, nullptr);
1044 EXPECT_EQ(SOFTBUS_OK, ret);
1045
1046 ret = SoftBusCondInit(&g_cond);
1047 EXPECT_EQ(SOFTBUS_OK, ret);
1048
1049 SoftBusThread threadWait = 0;
1050 SoftBusThread threadSignal = 0;
1051 SoftBusThreadAttr threadAttr = { 0 };
1052 ret = SoftBusThreadAttrInit(&threadAttr);
1053 EXPECT_EQ(SOFTBUS_OK, ret);
1054
1055 ret = SoftBusThreadCreate(&threadWait, &threadAttr, ThreadWaitTest, nullptr);
1056 EXPECT_EQ(SOFTBUS_OK, ret);
1057 EXPECT_TRUE(threadWait != 0);
1058
1059 ret = SoftBusThreadCreate(&threadSignal, &threadAttr, ThreadSignalTest, nullptr);
1060 EXPECT_EQ(SOFTBUS_OK, ret);
1061 EXPECT_TRUE(threadSignal != 0);
1062
1063 ret = SoftBusThreadJoin(threadWait, nullptr);
1064 EXPECT_EQ(SOFTBUS_OK, ret);
1065 ret = SoftBusThreadJoin(threadSignal, nullptr);
1066 EXPECT_EQ(SOFTBUS_OK, ret);
1067 }
1068 } // namespace OHOS
1069