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