Skip to content

Commit

Permalink
test: schedule_api : test some negative test
Browse files Browse the repository at this point in the history
Add error test of api to improve
branch and line coverage in sched.c.

Signed-off-by: Ying ming <mingx.ying@intel.com>
  • Loading branch information
Ying ming authored and nashif committed Apr 19, 2021
1 parent f59cba3 commit facd0f5
Show file tree
Hide file tree
Showing 7 changed files with 307 additions and 12 deletions.
1 change: 1 addition & 0 deletions tests/kernel/sched/schedule_api/prj.conf
Expand Up @@ -6,3 +6,4 @@ CONFIG_NUM_COOP_PRIORITIES=30
CONFIG_MAX_THREAD_BYTES=5
CONFIG_TEST_USERSPACE=y
CONFIG_MP_NUM_CPUS=1
CONFIG_ZTEST_FATAL_HOOK=y
1 change: 1 addition & 0 deletions tests/kernel/sched/schedule_api/prj_dumb.conf
Expand Up @@ -4,3 +4,4 @@ CONFIG_TEST_USERSPACE=y
CONFIG_SCHED_DUMB=y
CONFIG_MAX_THREAD_BYTES=5
CONFIG_MP_NUM_CPUS=1
CONFIG_ZTEST_FATAL_HOOK=y
1 change: 1 addition & 0 deletions tests/kernel/sched/schedule_api/prj_multiq.conf
Expand Up @@ -4,3 +4,4 @@ CONFIG_TEST_USERSPACE=y
CONFIG_SCHED_MULTIQ=y
CONFIG_MAX_THREAD_BYTES=5
CONFIG_MP_NUM_CPUS=1
CONFIG_ZTEST_FATAL_HOOK=y
10 changes: 9 additions & 1 deletion tests/kernel/sched/schedule_api/src/main.c
Expand Up @@ -52,6 +52,7 @@ void test_main(void)
ztest_1cpu_unit_test(test_priority_preemptible_wait_prio),
ztest_unit_test(test_yield_cooperative),
ztest_unit_test(test_sleep_cooperative),
ztest_unit_test(test_busy_wait_cooperative),
ztest_unit_test(test_sleep_wakeup_preemptible),
ztest_unit_test(test_pending_thread_wakeup),
ztest_unit_test(test_time_slicing_preemptible),
Expand All @@ -65,7 +66,14 @@ void test_main(void)
ztest_unit_test(test_priority_scheduling),
ztest_unit_test(test_wakeup_expired_timer_thread),
ztest_user_unit_test(test_user_k_wakeup),
ztest_user_unit_test(test_user_k_is_preempt)
ztest_user_unit_test(test_user_k_is_preempt),
ztest_user_unit_test(test_k_thread_suspend_init_null),
ztest_user_unit_test(test_k_thread_resume_init_null),
ztest_user_unit_test(test_k_thread_priority_get_init_null),
ztest_user_unit_test(test_k_thread_priority_set_init_null),
ztest_user_unit_test(test_k_thread_priority_set_overmax),
ztest_user_unit_test(test_k_thread_priority_set_upgrade),
ztest_user_unit_test(test_k_wakeup_init_null)
);
ztest_run_test_suite(threads_scheduling);
}
8 changes: 8 additions & 0 deletions tests/kernel/sched/schedule_api/src/test_sched.h
Expand Up @@ -32,6 +32,7 @@ void test_priority_preemptible_wait_prio(void);
void test_bad_priorities(void);
void test_yield_cooperative(void);
void test_sleep_cooperative(void);
void test_busy_wait_cooperative(void);
void test_sleep_wakeup_preemptible(void);
void test_pending_thread_wakeup(void);
void test_time_slicing_preemptible(void);
Expand All @@ -46,5 +47,12 @@ void test_priority_scheduling(void);
void test_wakeup_expired_timer_thread(void);
void test_user_k_wakeup(void);
void test_user_k_is_preempt(void);
void test_k_thread_suspend_init_null(void);
void test_k_thread_resume_init_null(void);
void test_k_thread_priority_get_init_null(void);
void test_k_thread_priority_set_init_null(void);
void test_k_thread_priority_set_overmax(void);
void test_k_thread_priority_set_upgrade(void);
void test_k_wakeup_init_null(void);

#endif /* __TEST_SCHED_H__ */
284 changes: 280 additions & 4 deletions tests/kernel/sched/schedule_api/src/user_api.c
Expand Up @@ -5,12 +5,18 @@
*/

#include "test_sched.h"
#include <ztest.h>
#include <irq_offload.h>
#include <kernel_internal.h>
#include <ztest_error_hook.h>

struct k_thread user_thread;
K_SEM_DEFINE(user_sem, 0, 1);

ZTEST_BMEM volatile int thread_was_preempt;

#define THREAD_TEST_PRIORITY 0

K_THREAD_STACK_DEFINE(ustack, STACK_SIZE);

static void sleepy_thread(void *p1, void *p2, void *p3)
Expand All @@ -25,14 +31,15 @@ static void sleepy_thread(void *p1, void *p2, void *p3)

void test_user_k_wakeup(void)
{
k_thread_create(&user_thread, ustack, STACK_SIZE, sleepy_thread,
k_tid_t tid = k_thread_create(&user_thread, ustack, STACK_SIZE, sleepy_thread,
NULL, NULL, NULL,
k_thread_priority_get(k_current_get()),
K_USER | K_INHERIT_PERMS, K_NO_WAIT);

k_yield(); /* Let thread run and start sleeping forever */
k_wakeup(&user_thread);
k_sem_take(&user_sem, K_FOREVER);
k_thread_abort(tid);
}

static void preempt_test_thread(void *p1, void *p2, void *p3)
Expand All @@ -59,17 +66,18 @@ void test_user_k_is_preempt(void)
*/
int twp;

k_thread_create(&user_thread, ustack, STACK_SIZE, preempt_test_thread,
NULL, NULL, NULL,
k_tid_t tid = k_thread_create(&user_thread, ustack,
STACK_SIZE, preempt_test_thread, NULL, NULL, NULL,
k_thread_priority_get(k_current_get()),
K_USER | K_INHERIT_PERMS, K_NO_WAIT);

k_sem_take(&user_sem, K_FOREVER);

twp = thread_was_preempt;
zassert_false(twp, "unexpected return value");
k_thread_abort(tid);

k_thread_create(&user_thread, ustack, STACK_SIZE, preempt_test_thread,
tid = k_thread_create(&user_thread, ustack, STACK_SIZE, preempt_test_thread,
NULL, NULL, NULL,
K_PRIO_PREEMPT(1),
K_USER | K_INHERIT_PERMS, K_NO_WAIT);
Expand All @@ -78,4 +86,272 @@ void test_user_k_is_preempt(void)

twp = thread_was_preempt;
zassert_true(twp, "unexpected return value");
k_thread_abort(tid);
}

/**
* userspace negative test: take NULL as input param to verify
* the api will trigger a fatal exception
*/
#ifdef CONFIG_USERSPACE
static void thread_suspend_init_null(void *p1, void *p2, void *p3)
{
ztest_set_fault_valid(true);
k_thread_suspend(NULL);

/* should not go here */
ztest_test_fail();
}

/**
* @brief Test k_thread_suspend() API
*
* @details Create a thread and set k_thread_suspend() input param to NULL
* will trigger a fatal error.
*
* @ingroup kernel_sched_tests
*
* @see k_thread_suspend()
*/
void test_k_thread_suspend_init_null(void)
{
k_tid_t tid = k_thread_create(&user_thread, ustack, STACK_SIZE,
(k_thread_entry_t)thread_suspend_init_null,
NULL, NULL, NULL,
K_PRIO_PREEMPT(THREAD_TEST_PRIORITY),
K_USER | K_INHERIT_PERMS, K_NO_WAIT);

k_thread_join(tid, K_FOREVER);
}
#else
void test_k_thread_suspend_init_null(void)
{
ztest_test_skip();
}
#endif

#ifdef CONFIG_USERSPACE
static void thread_resume_init_null(void *p1, void *p2, void *p3)
{
ztest_set_fault_valid(true);
k_thread_resume(NULL);

/* should not go here */
ztest_test_fail();
}

/**
* @brief Test k_thread_resume() API
*
* @details Create a thread and set k_thread_resume() input param to NULL
* will trigger a fatal error.
*
* @ingroup kernel_sched_tests
*
* @see k_thread_resume()
*/
void test_k_thread_resume_init_null(void)
{
k_tid_t tid = k_thread_create(&user_thread, ustack, STACK_SIZE,
(k_thread_entry_t)thread_resume_init_null,
NULL, NULL, NULL,
K_PRIO_PREEMPT(THREAD_TEST_PRIORITY),
K_USER | K_INHERIT_PERMS, K_NO_WAIT);

k_thread_join(tid, K_FOREVER);
}
#else
void test_k_thread_resume_init_null(void)
{
ztest_test_skip();
}
#endif

#ifdef CONFIG_USERSPACE
static void thread_priority_get_init_null(void *p1, void *p2, void *p3)
{
ztest_set_fault_valid(true);
k_thread_priority_get(NULL);

/* should not go here */
ztest_test_fail();
}

/**
* @brief Test k_thread_priority_get() API
*
* @details Create a thread and set thread_k_thread_priority_get() param input to
* NULL will trigger a fatal error.
*
* @ingroup kernel_sched_tests
*
* @see thread_k_thread_priority_get()
*/
void test_k_thread_priority_get_init_null(void)
{
k_tid_t tid = k_thread_create(&user_thread, ustack, STACK_SIZE,
(k_thread_entry_t)thread_priority_get_init_null,
NULL, NULL, NULL,
K_PRIO_PREEMPT(THREAD_TEST_PRIORITY),
K_USER | K_INHERIT_PERMS, K_NO_WAIT);

k_thread_join(tid, K_FOREVER);
}
#else
void test_k_thread_priority_get_init_null(void)
{
ztest_test_skip();
}
#endif

#ifdef CONFIG_USERSPACE
static void thread_priority_set_init_null(void *p1, void *p2, void *p3)
{
ztest_set_fault_valid(true);
k_thread_priority_set(NULL, 0);

/* should not go here */
ztest_test_fail();
}

/**
* @brief Test k_thread_priority_set() API
*
* @details Create a thread and set k_thread_priority_set() param input to
* NULL will trigger a fatal error.
*
* @ingroup kernel_sched_tests
*
* @see k_thread_priority_set()
*/
void test_k_thread_priority_set_init_null(void)
{
k_tid_t tid = k_thread_create(&user_thread, ustack, STACK_SIZE,
(k_thread_entry_t)thread_priority_set_init_null,
NULL, NULL, NULL,
K_PRIO_PREEMPT(THREAD_TEST_PRIORITY),
K_USER | K_INHERIT_PERMS, K_NO_WAIT);

k_thread_join(tid, K_FOREVER);
}
#else
void test_k_thread_priority_set_init_null(void)
{
ztest_test_skip();
}
#endif

#ifdef CONFIG_USERSPACE
static void thread_priority_set_overmax(void *p1, void *p2, void *p3)
{
ztest_set_fault_valid(true);

/* set valid priority value outside the priority range will invoke fatal error */
k_thread_priority_set(k_current_get(), K_LOWEST_APPLICATION_THREAD_PRIO + 1);

/* should not go here */
ztest_test_fail();
}

/**
* @brief Test k_thread_priority_set() API
*
* @details Check input param range overmax in userspace test.
*
* @ingroup kernel_sched_tests
*
* @see k_thread_priority_set()
*/
void test_k_thread_priority_set_overmax(void)
{
k_tid_t tid = k_thread_create(&user_thread, ustack, STACK_SIZE,
(k_thread_entry_t)thread_priority_set_overmax,
NULL, NULL, NULL,
K_PRIO_PREEMPT(THREAD_TEST_PRIORITY),
K_USER | K_INHERIT_PERMS, K_NO_WAIT);

k_thread_join(tid, K_FOREVER);
}
#else
void test_k_thread_priority_set_overmax(void)
{
ztest_test_skip();
}
#endif

#ifdef CONFIG_USERSPACE
static void thread_priority_set_upgrade(void *p1, void *p2, void *p3)
{
ztest_set_fault_valid(true);

/* at first, set an valid priority */
k_thread_priority_set(k_current_get(), THREAD_TEST_PRIORITY);
/* it cannot upgraded thread priority in usermode */
k_thread_priority_set(k_current_get(), THREAD_TEST_PRIORITY - 1);

/* should not go here */
ztest_test_fail();
}

/**
* @brief Test k_thread_priority_set() API
*
* @details Check input param range fail in userspace test.
*
* @ingroup kernel_sched_tests
*
* @see k_thread_priority_set()
*/
void test_k_thread_priority_set_upgrade(void)
{
k_tid_t tid = k_thread_create(&user_thread, ustack, STACK_SIZE,
(k_thread_entry_t)thread_priority_set_upgrade,
NULL, NULL, NULL,
K_PRIO_PREEMPT(THREAD_TEST_PRIORITY),
K_USER | K_INHERIT_PERMS, K_NO_WAIT);

k_thread_join(tid, K_FOREVER);
}
#else
void test_k_thread_priority_set_upgrade(void)
{
ztest_test_skip();
}
#endif

#ifdef CONFIG_USERSPACE
static void thread_wakeup_init_null(void *p1, void *p2, void *p3)
{
ztest_set_fault_valid(true);
k_wakeup(NULL);

/* should not go here */
ztest_test_fail();
}

/**
* @brief Test k_wakeup() API
*
* @details Create a thread and set k_wakeup() input param to NULL
* will trigger a fatal error
*
* @ingroup kernel_sched_tests
*
* @see k_wakeup()
*/
void test_k_wakeup_init_null(void)
{
k_tid_t tid = k_thread_create(&user_thread, ustack, STACK_SIZE,
(k_thread_entry_t)thread_wakeup_init_null,
NULL, NULL, NULL,
K_PRIO_PREEMPT(THREAD_TEST_PRIORITY),
K_USER | K_INHERIT_PERMS, K_NO_WAIT);

k_thread_join(tid, K_FOREVER);
}
#else
void test_k_wakeup_init_null(void)
{
ztest_test_skip();
}
#endif

0 comments on commit facd0f5

Please sign in to comment.