Skip to content

Commit 064d450

Browse files
KangJianXcarlescufi
authored andcommitted
tests: posix: Add new testcases for posix APIs
Add error condition to test pthread. Like use pthread with uninitialize stack or stack size is 0, and verify the result is as expected. Signed-off-by: Jian Kang <[email protected]>
1 parent 84d23fd commit 064d450

File tree

2 files changed

+101
-2
lines changed

2 files changed

+101
-2
lines changed

tests/posix/common/src/main.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ extern void test_posix_rw_lock(void);
1616
extern void test_posix_realtime(void);
1717
extern void test_posix_timer(void);
1818
extern void test_posix_pthread_execution(void);
19+
extern void test_posix_pthread_error_condition(void);
20+
extern void test_posix_pthread_create_negative(void);
1921
extern void test_posix_pthread_termination(void);
2022
extern void test_posix_multiple_threads_single_key(void);
2123
extern void test_posix_single_thread_multiple_keys(void);
@@ -39,6 +41,7 @@ void test_main(void)
3941
{
4042
ztest_test_suite(posix_apis,
4143
ztest_unit_test(test_posix_pthread_execution),
44+
ztest_unit_test(test_posix_pthread_error_condition),
4245
ztest_unit_test(test_posix_pthread_termination),
4346
ztest_unit_test(test_posix_multiple_threads_single_key),
4447
ztest_unit_test(test_posix_single_thread_multiple_keys),
@@ -63,7 +66,8 @@ void test_main(void)
6366
ztest_unit_test(test_nanosleep_0_500000000),
6467
ztest_unit_test(test_nanosleep_1_0),
6568
ztest_unit_test(test_nanosleep_1_1),
66-
ztest_unit_test(test_nanosleep_1_1001)
69+
ztest_unit_test(test_nanosleep_1_1001),
70+
ztest_unit_test(test_posix_pthread_create_negative)
6771
);
6872
ztest_run_test_suite(posix_apis);
6973
}

tests/posix/common/src/pthread.c

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
K_THREAD_STACK_ARRAY_DEFINE(stack_e, N_THR_E, STACKS);
3030
K_THREAD_STACK_ARRAY_DEFINE(stack_t, N_THR_T, STACKS);
31+
K_THREAD_STACK_ARRAY_DEFINE(stack_1, 1, 32);
3132

3233
void *thread_top_exec(void *p1);
3334
void *thread_top_term(void *p1);
@@ -239,7 +240,7 @@ void test_posix_pthread_execution(void)
239240
void *retval, *stackaddr;
240241
size_t stacksize;
241242
int serial_threads = 0;
242-
const char thr_name[] = "thread name";
243+
static const char thr_name[] = "thread name";
243244
char thr_name_buf[CONFIG_THREAD_MAX_NAME_LEN];
244245

245246
sem_init(&main_sem, 0, 1);
@@ -402,6 +403,70 @@ void test_posix_pthread_execution(void)
402403
printk("Barrier test OK\n");
403404
}
404405

406+
void test_posix_pthread_error_condition(void)
407+
{
408+
pthread_attr_t attr;
409+
struct sched_param param;
410+
void *stackaddr;
411+
size_t stacksize;
412+
int policy, detach;
413+
static pthread_once_t key = 1;
414+
415+
/* TESTPOINT: invoke pthread APIs with NULL */
416+
zassert_equal(pthread_attr_destroy(NULL), EINVAL,
417+
"pthread destroy NULL error");
418+
zassert_equal(pthread_attr_getschedparam(NULL, &param), EINVAL,
419+
"get scheduling param error");
420+
zassert_equal(pthread_attr_getstack(NULL, &stackaddr, &stacksize),
421+
EINVAL, "get stack attributes error");
422+
zassert_equal(pthread_attr_getstacksize(NULL, &stacksize),
423+
EINVAL, "get stack size error");
424+
zassert_equal(pthread_attr_setschedpolicy(NULL, 2),
425+
EINVAL, "set scheduling policy error");
426+
zassert_equal(pthread_attr_getschedpolicy(NULL, &policy),
427+
EINVAL, "get scheduling policy error");
428+
zassert_equal(pthread_attr_setdetachstate(NULL, 0),
429+
EINVAL, "pthread set detach state with NULL error");
430+
zassert_equal(pthread_attr_getdetachstate(NULL, &detach),
431+
EINVAL, "get datach state error");
432+
zassert_equal(pthread_detach(NULL), ESRCH, "detach with NULL error");
433+
zassert_equal(pthread_attr_init(NULL), ENOMEM,
434+
"init with NULL error");
435+
zassert_equal(pthread_attr_setschedparam(NULL, &param), EINVAL,
436+
"set sched param with NULL error");
437+
zassert_equal(pthread_cancel(NULL), ESRCH,
438+
"cancel NULL error");
439+
zassert_equal(pthread_join(NULL, NULL), ESRCH,
440+
"join with NULL has error");
441+
zassert_false(pthread_once(&key, NULL),
442+
"pthread dynamic package initialization error");
443+
zassert_equal(pthread_getschedparam(NULL, &policy, &param), ESRCH,
444+
"get schedparam with NULL error");
445+
zassert_equal(pthread_setschedparam(NULL, policy, &param), ESRCH,
446+
"set schedparam with NULL error");
447+
448+
attr.initialized = 0U;
449+
zassert_equal(pthread_attr_getdetachstate(&attr, &detach),
450+
EINVAL, "get datach state error");
451+
452+
/* Initialise thread attribute to ensure won't be return with init error */
453+
pthread_attr_init(&attr);
454+
zassert_false(pthread_attr_setschedpolicy(&attr, 0),
455+
"set scheduling policy error");
456+
zassert_false(pthread_attr_setschedpolicy(&attr, 1),
457+
"set scheduling policy error");
458+
zassert_equal(pthread_attr_setschedpolicy(&attr, 2),
459+
EINVAL, "set scheduling policy error");
460+
zassert_false(pthread_attr_setdetachstate(&attr, 1),
461+
"set detach state error");
462+
zassert_false(pthread_attr_setdetachstate(&attr, 2),
463+
"set detach state error");
464+
zassert_equal(pthread_attr_setdetachstate(&attr, 3),
465+
EINVAL, "set detach state error");
466+
zassert_false(pthread_attr_getdetachstate(&attr, &detach),
467+
"get datach state error");
468+
}
469+
405470
void test_posix_pthread_termination(void)
406471
{
407472
int32_t i, ret;
@@ -464,3 +529,33 @@ void test_posix_pthread_termination(void)
464529
ret = pthread_getschedparam(newthread[N_THR_T/2], &policy, &schedparam);
465530
zassert_equal(ret, ESRCH, "got attr from terminated thread!");
466531
}
532+
533+
static void *create_thread1(void *p1)
534+
{
535+
/* do nothing */
536+
return NULL;
537+
}
538+
539+
void test_posix_pthread_create_negative(void)
540+
{
541+
int ret;
542+
pthread_t pthread1;
543+
pthread_attr_t attr1;
544+
545+
/* create pthread without attr initialized */
546+
ret = pthread_create(&pthread1, NULL, create_thread1, (void *)1);
547+
zassert_equal(ret, EINVAL, "create thread with NULL successful");
548+
549+
/* initialized attr without set stack to create thread */
550+
ret = pthread_attr_init(&attr1);
551+
zassert_false(ret, "attr1 initialized failed");
552+
553+
attr1.stack = NULL;
554+
ret = pthread_create(&pthread1, &attr1, create_thread1, (void *)1);
555+
zassert_equal(ret, EINVAL, "create successful with NULL attr");
556+
557+
/* set stack size 0 to create thread */
558+
pthread_attr_setstack(&attr1, &stack_1, 0);
559+
ret = pthread_create(&pthread1, &attr1, create_thread1, (void *)1);
560+
zassert_equal(ret, EINVAL, "create thread with 0 size");
561+
}

0 commit comments

Comments
 (0)