kernel: timer: Add some testcases to testing timer and clock

Add some error condition or testing cases to verify whether the
robustness of API. Such as give a NULL to some API and check
the response if get result that we were expacted.

Signed-off-by: Jian Kang <jianx.kang@intel.com>
This commit is contained in:
Jian Kang 2020-12-30 17:38:20 +08:00 committed by Anas Nashif
commit bf55de2d67
6 changed files with 429 additions and 0 deletions

View file

@ -173,6 +173,8 @@ void test_timer_duration_period(void)
TIMER_ASSERT(tdata.expire_cnt == EXPIRE_TIMES, &duration_timer);
TIMER_ASSERT(tdata.stop_cnt == 1, &duration_timer);
k_timer_start(&duration_timer, K_FOREVER, K_MSEC(PERIOD));
TIMER_ASSERT(tdata.stop_cnt == 1, &duration_timer);
/* cleanup environemtn */
k_timer_stop(&duration_timer);
}
@ -452,8 +454,13 @@ void test_timer_status_sync(void)
TIMER_ASSERT(tdata.expire_cnt == (i + 1), &status_sync_timer);
}
k_timer_start(&status_sync_timer, K_MSEC(DURATION), K_MSEC(PERIOD));
busy_wait_ms(PERIOD*2);
zassert_true(k_timer_status_sync(&status_sync_timer), NULL);
/* cleanup environment */
k_timer_stop(&status_sync_timer);
zassert_false(k_timer_status_sync(&status_sync_timer), NULL);
}
/**

View file

@ -46,11 +46,17 @@ static struct test_rec tests[] = {
TESTREC(ms, ticks, near, 64),
TESTREC(ms, ticks, ceil, 32),
TESTREC(ms, ticks, ceil, 64),
TESTREC(us, cyc, floor, 32),
TESTREC(us, cyc, floor, 64),
TESTREC(us, cyc, near, 32),
TESTREC(us, cyc, near, 64),
TESTREC(us, cyc, ceil, 32),
TESTREC(us, cyc, ceil, 64),
TESTREC(us, ticks, floor, 32),
TESTREC(us, ticks, floor, 64),
TESTREC(us, ticks, near, 32),
TESTREC(us, ticks, near, 64),
TESTREC(us, ticks, ceil, 32),
TESTREC(us, ticks, ceil, 64),
TESTREC(cyc, ms, floor, 32),
TESTREC(cyc, ms, floor, 64),
@ -58,8 +64,11 @@ static struct test_rec tests[] = {
TESTREC(cyc, ms, near, 64),
TESTREC(cyc, ms, ceil, 32),
TESTREC(cyc, ms, ceil, 64),
TESTREC(cyc, us, floor, 32),
TESTREC(cyc, us, floor, 64),
TESTREC(cyc, us, near, 32),
TESTREC(cyc, us, near, 64),
TESTREC(cyc, us, ceil, 32),
TESTREC(cyc, us, ceil, 64),
TESTREC(cyc, ticks, floor, 32),
TESTREC(cyc, ticks, floor, 64),
@ -73,8 +82,11 @@ static struct test_rec tests[] = {
TESTREC(ticks, ms, near, 64),
TESTREC(ticks, ms, ceil, 32),
TESTREC(ticks, ms, ceil, 64),
TESTREC(ticks, us, floor, 32),
TESTREC(ticks, us, floor, 64),
TESTREC(ticks, us, near, 32),
TESTREC(ticks, us, near, 64),
TESTREC(ticks, us, ceil, 32),
TESTREC(ticks, us, ceil, 64),
TESTREC(ticks, cyc, floor, 32),
TESTREC(ticks, cyc, floor, 64),
@ -82,17 +94,29 @@ static struct test_rec tests[] = {
TESTREC(ticks, cyc, near, 64),
TESTREC(ticks, cyc, ceil, 32),
TESTREC(ticks, cyc, ceil, 64),
TESTREC(ns, cyc, floor, 32),
TESTREC(ns, cyc, floor, 64),
TESTREC(ns, cyc, near, 32),
TESTREC(ns, cyc, near, 64),
TESTREC(ns, cyc, ceil, 32),
TESTREC(ns, cyc, ceil, 64),
TESTREC(ns, ticks, floor, 32),
TESTREC(ns, ticks, floor, 64),
TESTREC(ns, ticks, near, 32),
TESTREC(ns, ticks, near, 64),
TESTREC(ns, ticks, ceil, 32),
TESTREC(ns, ticks, ceil, 64),
TESTREC(cyc, ns, floor, 32),
TESTREC(cyc, ns, floor, 64),
TESTREC(cyc, ns, near, 32),
TESTREC(cyc, ns, near, 64),
TESTREC(cyc, ns, ceil, 32),
TESTREC(cyc, ns, ceil, 64),
TESTREC(ticks, ns, floor, 32),
TESTREC(ticks, ns, floor, 64),
TESTREC(ticks, ns, near, 32),
TESTREC(ticks, ns, near, 64),
TESTREC(ticks, ns, ceil, 32),
TESTREC(ticks, ns, ceil, 64),
};

View file

@ -0,0 +1,8 @@
# SPDX-License-Identifier: Apache-2.0
cmake_minimum_required(VERSION 3.13.1)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(timer_api)
FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE ${app_sources})

View file

@ -0,0 +1,6 @@
CONFIG_ZTEST=y
CONFIG_TEST_USERSPACE=y
CONFIG_IRQ_OFFLOAD=y
CONFIG_MP_NUM_CPUS=1
CONFIG_TEST_RANDOM_GENERATOR=y
CONFIG_ZTEST_FATAL_HOOK=y

View file

@ -0,0 +1,381 @@
/*
* Copyright (c) 2021 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdlib.h>
#include <ztest.h>
#include <zephyr/types.h>
#include <irq_offload.h>
#include <ztest_error_hook.h>
#define STACK_SIZE (512 + CONFIG_TEST_EXTRA_STACKSIZE)
#define THREAD_TEST_PRIORITY 0
#define TEST_TIMEOUT -20
#define PERIOD 50
#define DURATION 100
static struct k_timer mytimer, sync_timer;
static struct k_thread tdata;
static K_THREAD_STACK_DEFINE(tstack, STACK_SIZE);
#ifdef CONFIG_USERSPACE
static void thread_timer_start_null(void *p1, void *p2, void *p3)
{
ztest_set_fault_valid(true);
k_timer_start(NULL, K_MSEC(DURATION), K_NO_WAIT);
/* should not go here*/
ztest_test_fail();
}
/**
* @brief Test k_timer_start() API
*
* @details Create a thread and set k_timer_start() input to NULL
* and set a duration and period.
*
* @ingroup kernel_timer_tests
*
* @see k_timer_start()
*/
void test_timer_start_null(void)
{
k_tid_t tid = k_thread_create(&tdata, tstack, STACK_SIZE,
(k_thread_entry_t)thread_timer_start_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_timer_start_null(void)
{
/* Skip on platforms with no userspace support */
ztest_test_skip();
}
#endif
#ifdef CONFIG_USERSPACE
static void thread_timer_stop_null(void *p1, void *p2, void *p3)
{
ztest_set_fault_valid(true);
k_timer_stop(NULL);
/* should not go here*/
ztest_test_fail();
}
/**
* @brief Test k_timer_stop() API
*
* @details Create a thread and set k_timer_stop() input to NULL
*
* @ingroup kernel_timer_tests
*
* @see k_timer_stop()
*/
void test_timer_stop_null(void)
{
k_tid_t tid = k_thread_create(&tdata, tstack, STACK_SIZE,
(k_thread_entry_t)thread_timer_stop_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_timer_stop_null(void)
{
/* Skip on platforms with no userspace support */
ztest_test_skip();
}
#endif
#ifdef CONFIG_USERSPACE
static void thread_timer_status_get_null(void *p1, void *p2, void *p3)
{
ztest_set_fault_valid(true);
k_timer_status_get(NULL);
/* should not go here*/
ztest_test_fail();
}
/**
* @brief Test k_timer_status_get() API
*
* @details Create a thread and set k_timer_status_get() input to NULL
*
* @ingroup kernel_timer_tests
*
* @see k_timer_status_get()
*/
void test_timer_status_get_null(void)
{
k_tid_t tid = k_thread_create(&tdata, tstack, STACK_SIZE,
(k_thread_entry_t)thread_timer_status_get_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_timer_status_get_null(void)
{
/* Skip on platforms with no userspace support */
ztest_test_skip();
}
#endif
#ifdef CONFIG_USERSPACE
static void thread_timer_status_sync_null(void *p1, void *p2, void *p3)
{
ztest_set_fault_valid(true);
k_timer_status_sync(NULL);
/* should not go here*/
ztest_test_fail();
}
/**
* @brief Test k_timer_status_sync() API
*
* @details Create a thread and set k_timer_status_sync() input to NULL
*
* @ingroup kernel_timer_tests
*
* @see k_timer_status_sync()
*/
void test_timer_status_sync_null(void)
{
k_tid_t tid = k_thread_create(&tdata, tstack, STACK_SIZE,
(k_thread_entry_t)thread_timer_status_sync_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_timer_status_sync_null(void)
{
/* Skip on platforms with no userspace support */
ztest_test_skip();
}
#endif
#ifdef CONFIG_USERSPACE
static void thread_timer_remaining_ticks_null(void *p1, void *p2, void *p3)
{
ztest_set_fault_valid(true);
k_timer_remaining_ticks(NULL);
/* should not go here*/
ztest_test_fail();
}
/**
* @brief Test k_timer_remaining_ticks() API
*
* @details Create a thread and set k_timer_remaining_ticks() input to NULL
*
* @ingroup kernel_timer_tests
*
* @see k_timer_remaining_ticks()
*/
void test_timer_remaining_ticks_null(void)
{
k_tid_t tid = k_thread_create(&tdata, tstack, STACK_SIZE,
(k_thread_entry_t)thread_timer_remaining_ticks_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_timer_remaining_ticks_null(void)
{
/* Skip on platforms with no userspace support */
ztest_test_skip();
}
#endif
#ifdef CONFIG_USERSPACE
static void thread_timer_expires_ticks_null(void *p1, void *p2, void *p3)
{
ztest_set_fault_valid(true);
k_timer_expires_ticks(NULL);
/* should not go here*/
ztest_test_fail();
}
/**
* @brief Test k_timer_expires_ticks() API
*
* @details Create a thread and set k_timer_expires_ticks() input to NULL
*
* @ingroup kernel_timer_tests
*
* @see k_timer_expires_ticks()
*/
void test_timer_expires_ticks_null(void)
{
k_tid_t tid = k_thread_create(&tdata, tstack, STACK_SIZE,
(k_thread_entry_t)thread_timer_expires_ticks_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_timer_expires_ticks_null(void)
{
/* Skip on platforms with no userspace support */
ztest_test_skip();
}
#endif
#ifdef CONFIG_USERSPACE
static void thread_timer_user_data_get_null(void *p1, void *p2, void *p3)
{
ztest_set_fault_valid(true);
k_timer_user_data_get(NULL);
/* should not go here*/
ztest_test_fail();
}
/**
* @brief Test k_timer_user_data_get() API
*
* @details Create a thread and set k_timer_user_data_get() input to NULL
*
* @ingroup kernel_timer_tests
*
* @see k_timer_user_data_get()
*/
void test_timer_user_data_get_null(void)
{
k_tid_t tid = k_thread_create(&tdata, tstack, STACK_SIZE,
(k_thread_entry_t)thread_timer_user_data_get_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_timer_user_data_get_null(void)
{
/* Skip on platforms with no userspace support */
ztest_test_skip();
}
#endif
#ifdef CONFIG_USERSPACE
static void thread_timer_user_data_set_null(void *p1, void *p2, void *p3)
{
int user_data = 1;
ztest_set_fault_valid(true);
k_timer_user_data_set(NULL, &user_data);
/* should not go here*/
ztest_test_fail();
}
/**
* @brief Test k_timer_user_data_set() API
*
* @details Create a thread and set k_timer_user_data_set() input to NULL
*
* @ingroup kernel_timer_tests
*
* @see k_timer_user_data_set()
*/
void test_timer_user_data_set_null(void)
{
k_tid_t tid = k_thread_create(&tdata, tstack, STACK_SIZE,
(k_thread_entry_t)thread_timer_user_data_set_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_timer_user_data_set_null(void)
{
/* Skip on platforms with no userspace support */
ztest_test_skip();
}
#endif
extern void z_add_timeout(struct _timeout *to, _timeout_func_t fn,
k_timeout_t timeout);
static void test_timer_handle(struct _timeout *t)
{
/**do nothing here**/
}
void test_timer_add_timeout(void)
{
struct _timeout tm;
k_timeout_t timeout = K_FOREVER;
z_add_timeout(&tm, test_timer_handle, timeout);
ztest_test_pass();
}
extern uint64_t z_timeout_end_calc(k_timeout_t timeout);
extern void z_clock_announce(int32_t ticks);
void test_timeout_end_calc(void)
{
int ret;
k_timeout_t timeout;
timeout = K_MSEC(DURATION);
ret = z_timeout_end_calc(timeout);
zassert_true(ret, "---timeout end calc---");
timeout.ticks = TEST_TIMEOUT;
ret = z_timeout_end_calc(timeout);
zassert_true(ret, "timeout end calc error");
timeout = K_FOREVER;
ret = z_timeout_end_calc(timeout);
zassert_true(ret, "timeout end calc forever");
}
/**
* @brief Tests for the Timer kernel object
* @defgroup kernel_timer_tests Timer
* @ingroup all_tests
* @{
* @}
*/
void test_main(void)
{
k_thread_access_grant(k_current_get(), &tdata, &tstack, &mytimer, &sync_timer);
ztest_test_suite(timer_api,
ztest_user_unit_test(test_timer_stop_null),
ztest_user_unit_test(test_timer_status_get_null),
ztest_user_unit_test(test_timer_status_sync_null),
ztest_user_unit_test(test_timer_remaining_ticks_null),
ztest_user_unit_test(test_timer_expires_ticks_null),
ztest_user_unit_test(test_timer_user_data_get_null),
ztest_user_unit_test(test_timer_user_data_set_null),
ztest_user_unit_test(test_timer_add_timeout),
ztest_unit_test(test_timeout_end_calc),
ztest_user_unit_test(test_timer_start_null));
ztest_run_test_suite(timer_api);
}

View file

@ -0,0 +1,3 @@
tests:
kernel.timer:
tags: kernel timer userspace ignore_faults