diff --git a/tests/posix/common/src/main.c b/tests/posix/common/src/main.c index 1637a19e891..1d9056bc680 100644 --- a/tests/posix/common/src/main.c +++ b/tests/posix/common/src/main.c @@ -16,6 +16,8 @@ extern void test_posix_rw_lock(void); extern void test_posix_realtime(void); extern void test_posix_timer(void); extern void test_posix_pthread_execution(void); +extern void test_posix_pthread_error_condition(void); +extern void test_posix_pthread_create_negative(void); extern void test_posix_pthread_termination(void); extern void test_posix_multiple_threads_single_key(void); extern void test_posix_single_thread_multiple_keys(void); @@ -39,6 +41,7 @@ void test_main(void) { ztest_test_suite(posix_apis, ztest_unit_test(test_posix_pthread_execution), + ztest_unit_test(test_posix_pthread_error_condition), ztest_unit_test(test_posix_pthread_termination), ztest_unit_test(test_posix_multiple_threads_single_key), ztest_unit_test(test_posix_single_thread_multiple_keys), @@ -63,7 +66,8 @@ void test_main(void) ztest_unit_test(test_nanosleep_0_500000000), ztest_unit_test(test_nanosleep_1_0), ztest_unit_test(test_nanosleep_1_1), - ztest_unit_test(test_nanosleep_1_1001) + ztest_unit_test(test_nanosleep_1_1001), + ztest_unit_test(test_posix_pthread_create_negative) ); ztest_run_test_suite(posix_apis); } diff --git a/tests/posix/common/src/pthread.c b/tests/posix/common/src/pthread.c index da46c3afb25..82cede2cdca 100644 --- a/tests/posix/common/src/pthread.c +++ b/tests/posix/common/src/pthread.c @@ -28,6 +28,7 @@ K_THREAD_STACK_ARRAY_DEFINE(stack_e, N_THR_E, STACKS); K_THREAD_STACK_ARRAY_DEFINE(stack_t, N_THR_T, STACKS); +K_THREAD_STACK_ARRAY_DEFINE(stack_1, 1, 32); void *thread_top_exec(void *p1); void *thread_top_term(void *p1); @@ -239,7 +240,7 @@ void test_posix_pthread_execution(void) void *retval, *stackaddr; size_t stacksize; int serial_threads = 0; - const char thr_name[] = "thread name"; + static const char thr_name[] = "thread name"; char thr_name_buf[CONFIG_THREAD_MAX_NAME_LEN]; sem_init(&main_sem, 0, 1); @@ -402,6 +403,70 @@ void test_posix_pthread_execution(void) printk("Barrier test OK\n"); } +void test_posix_pthread_error_condition(void) +{ + pthread_attr_t attr; + struct sched_param param; + void *stackaddr; + size_t stacksize; + int policy, detach; + static pthread_once_t key = 1; + + /* TESTPOINT: invoke pthread APIs with NULL */ + zassert_equal(pthread_attr_destroy(NULL), EINVAL, + "pthread destroy NULL error"); + zassert_equal(pthread_attr_getschedparam(NULL, ¶m), EINVAL, + "get scheduling param error"); + zassert_equal(pthread_attr_getstack(NULL, &stackaddr, &stacksize), + EINVAL, "get stack attributes error"); + zassert_equal(pthread_attr_getstacksize(NULL, &stacksize), + EINVAL, "get stack size error"); + zassert_equal(pthread_attr_setschedpolicy(NULL, 2), + EINVAL, "set scheduling policy error"); + zassert_equal(pthread_attr_getschedpolicy(NULL, &policy), + EINVAL, "get scheduling policy error"); + zassert_equal(pthread_attr_setdetachstate(NULL, 0), + EINVAL, "pthread set detach state with NULL error"); + zassert_equal(pthread_attr_getdetachstate(NULL, &detach), + EINVAL, "get datach state error"); + zassert_equal(pthread_detach(NULL), ESRCH, "detach with NULL error"); + zassert_equal(pthread_attr_init(NULL), ENOMEM, + "init with NULL error"); + zassert_equal(pthread_attr_setschedparam(NULL, ¶m), EINVAL, + "set sched param with NULL error"); + zassert_equal(pthread_cancel(NULL), ESRCH, + "cancel NULL error"); + zassert_equal(pthread_join(NULL, NULL), ESRCH, + "join with NULL has error"); + zassert_false(pthread_once(&key, NULL), + "pthread dynamic package initialization error"); + zassert_equal(pthread_getschedparam(NULL, &policy, ¶m), ESRCH, + "get schedparam with NULL error"); + zassert_equal(pthread_setschedparam(NULL, policy, ¶m), ESRCH, + "set schedparam with NULL error"); + + attr.initialized = 0U; + zassert_equal(pthread_attr_getdetachstate(&attr, &detach), + EINVAL, "get datach state error"); + + /* Initialise thread attribute to ensure won't be return with init error */ + pthread_attr_init(&attr); + zassert_false(pthread_attr_setschedpolicy(&attr, 0), + "set scheduling policy error"); + zassert_false(pthread_attr_setschedpolicy(&attr, 1), + "set scheduling policy error"); + zassert_equal(pthread_attr_setschedpolicy(&attr, 2), + EINVAL, "set scheduling policy error"); + zassert_false(pthread_attr_setdetachstate(&attr, 1), + "set detach state error"); + zassert_false(pthread_attr_setdetachstate(&attr, 2), + "set detach state error"); + zassert_equal(pthread_attr_setdetachstate(&attr, 3), + EINVAL, "set detach state error"); + zassert_false(pthread_attr_getdetachstate(&attr, &detach), + "get datach state error"); +} + void test_posix_pthread_termination(void) { int32_t i, ret; @@ -464,3 +529,33 @@ void test_posix_pthread_termination(void) ret = pthread_getschedparam(newthread[N_THR_T/2], &policy, &schedparam); zassert_equal(ret, ESRCH, "got attr from terminated thread!"); } + +static void *create_thread1(void *p1) +{ + /* do nothing */ + return NULL; +} + +void test_posix_pthread_create_negative(void) +{ + int ret; + pthread_t pthread1; + pthread_attr_t attr1; + + /* create pthread without attr initialized */ + ret = pthread_create(&pthread1, NULL, create_thread1, (void *)1); + zassert_equal(ret, EINVAL, "create thread with NULL successful"); + + /* initialized attr without set stack to create thread */ + ret = pthread_attr_init(&attr1); + zassert_false(ret, "attr1 initialized failed"); + + attr1.stack = NULL; + ret = pthread_create(&pthread1, &attr1, create_thread1, (void *)1); + zassert_equal(ret, EINVAL, "create successful with NULL attr"); + + /* set stack size 0 to create thread */ + pthread_attr_setstack(&attr1, &stack_1, 0); + ret = pthread_create(&pthread1, &attr1, create_thread1, (void *)1); + zassert_equal(ret, EINVAL, "create thread with 0 size"); +}