Skip to content

Commit 30bf585

Browse files
KangJianXnashif
authored andcommitted
tests: kernel: Add some testcases for thread
Add some error condition of testcases 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 <[email protected]>
1 parent b07065d commit 30bf585

File tree

4 files changed

+211
-0
lines changed

4 files changed

+211
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.13.1)
4+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
5+
project(thread_apis)
6+
7+
FILE(GLOB app_sources src/*.c)
8+
target_sources(app PRIVATE ${app_sources})
9+
10+
target_include_directories(app PRIVATE
11+
${ZEPHYR_BASE}/kernel/include
12+
${ZEPHYR_BASE}/arch/${ARCH}/include
13+
)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CONFIG_ZTEST=y
2+
CONFIG_THREAD_MONITOR=y
3+
CONFIG_THREAD_CUSTOM_DATA=y
4+
CONFIG_THREAD_NAME=y
5+
CONFIG_THREAD_STACK_INFO=y
6+
CONFIG_HEAP_MEM_POOL_SIZE=256
7+
CONFIG_SCHED_CPU_MASK=y
8+
CONFIG_TEST_USERSPACE=y
9+
CONFIG_MP_NUM_CPUS=1
10+
CONFIG_IRQ_OFFLOAD=y
11+
CONFIG_ZTEST_FATAL_HOOK=y
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
/*
2+
* Copyright (c) 2021 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
#include <ztest.h>
7+
#include <ztest_error_hook.h>
8+
9+
#define STACK_SIZE (512 + CONFIG_TEST_EXTRA_STACKSIZE)
10+
#define THREAD_TEST_PRIORITY 5
11+
12+
/* use to pass case type to threads */
13+
static ZTEST_DMEM int case_type;
14+
15+
static K_THREAD_STACK_DEFINE(tstack, STACK_SIZE);
16+
static K_THREAD_STACK_DEFINE(test_stack, STACK_SIZE);
17+
static struct k_thread tdata;
18+
static struct k_thread test_tdata;
19+
20+
/* enumerate our negative case scenario */
21+
enum {
22+
THREAD_START,
23+
FLOAT_DISABLE,
24+
TIMEOUT_REMAINING_TICKS,
25+
TIMEOUT_EXPIRES_TICKS,
26+
THREAD_CREATE_NEWTHREAD_NULL,
27+
THREAD_CREATE_STACK_NULL,
28+
THREAD_CTEATE_STACK_SIZE_OVERFLOW
29+
} neg_case;
30+
31+
static void test_thread(void *p1, void *p2, void *p3)
32+
{
33+
/* do nothing here */
34+
}
35+
36+
static void tThread_entry_negative(void *p1, void *p2, void *p3)
37+
{
38+
int choice = *((int *)p1);
39+
uint32_t perm = K_INHERIT_PERMS;
40+
41+
TC_PRINT("current case is %d\n", choice);
42+
43+
/* Set up the fault or assert are expected before we call
44+
* the target tested funciton.
45+
*/
46+
switch (choice) {
47+
case THREAD_START:
48+
ztest_set_fault_valid(true);
49+
k_thread_start(NULL);
50+
break;
51+
case FLOAT_DISABLE:
52+
ztest_set_fault_valid(true);
53+
k_float_disable(NULL);
54+
break;
55+
case TIMEOUT_REMAINING_TICKS:
56+
ztest_set_fault_valid(true);
57+
k_thread_timeout_remaining_ticks(NULL);
58+
break;
59+
case TIMEOUT_EXPIRES_TICKS:
60+
ztest_set_fault_valid(true);
61+
k_thread_timeout_expires_ticks(NULL);
62+
break;
63+
case THREAD_CREATE_NEWTHREAD_NULL:
64+
ztest_set_fault_valid(true);
65+
if (_is_user_context()) {
66+
perm = perm | K_USER;
67+
}
68+
69+
k_thread_create((struct k_thread *)NULL, test_stack, STACK_SIZE,
70+
test_thread, NULL, NULL, NULL,
71+
K_PRIO_PREEMPT(THREAD_TEST_PRIORITY),
72+
perm, K_NO_WAIT);
73+
break;
74+
case THREAD_CREATE_STACK_NULL:
75+
ztest_set_fault_valid(true);
76+
if (_is_user_context()) {
77+
perm = perm | K_USER;
78+
}
79+
80+
k_thread_create(&test_tdata, NULL, STACK_SIZE,
81+
test_thread, NULL, NULL, NULL,
82+
K_PRIO_PREEMPT(THREAD_TEST_PRIORITY),
83+
perm, K_NO_WAIT);
84+
break;
85+
case THREAD_CTEATE_STACK_SIZE_OVERFLOW:
86+
ztest_set_fault_valid(true);
87+
if (_is_user_context()) {
88+
perm = perm | K_USER;
89+
}
90+
k_thread_create(&test_tdata, test_stack, -1,
91+
test_thread, NULL, NULL, NULL,
92+
K_PRIO_PREEMPT(THREAD_TEST_PRIORITY),
93+
perm, K_NO_WAIT);
94+
break;
95+
default:
96+
TC_PRINT("should not be here!\n");
97+
break;
98+
}
99+
100+
/* If negative comes here, it means error condition not been
101+
* detected.
102+
*/
103+
ztest_test_fail();
104+
}
105+
106+
static void create_negative_test_thread(int choice)
107+
{
108+
int ret;
109+
uint32_t perm = K_INHERIT_PERMS;
110+
111+
if (_is_user_context()) {
112+
perm = perm | K_USER;
113+
}
114+
115+
case_type = choice;
116+
117+
k_tid_t tid = k_thread_create(&tdata, tstack, STACK_SIZE,
118+
(k_thread_entry_t)tThread_entry_negative,
119+
(void *)&case_type, NULL, NULL,
120+
K_PRIO_PREEMPT(THREAD_TEST_PRIORITY),
121+
perm, K_NO_WAIT);
122+
123+
ret = k_thread_join(tid, K_FOREVER);
124+
}
125+
126+
/* TESTPOINT: Pass a null pointer into the API k_thread_start() */
127+
static void test_thread_start(void)
128+
{
129+
create_negative_test_thread(THREAD_START);
130+
}
131+
132+
/* TESTPOINT: Pass a null pointer into the API k_float_disable() */
133+
static void test_float_disable(void)
134+
{
135+
create_negative_test_thread(FLOAT_DISABLE);
136+
}
137+
138+
/* TESTPOINT: Pass a null pointer into the API */
139+
static void test_timeout_remaining_ticks(void)
140+
{
141+
create_negative_test_thread(TIMEOUT_REMAINING_TICKS);
142+
}
143+
144+
/* TESTPOINT: Pass a null pointer into the API */
145+
static void test_timeout_expires_ticks(void)
146+
{
147+
create_negative_test_thread(TIMEOUT_EXPIRES_TICKS);
148+
}
149+
150+
/* TESTPOINT: Pass new thread with NULL into API */
151+
static void test_thread_create_uninit(void)
152+
{
153+
create_negative_test_thread(THREAD_CREATE_NEWTHREAD_NULL);
154+
}
155+
156+
/* TESTPOINT: Pass a NULL stack into API */
157+
static void test_thread_create_stack_null(void)
158+
{
159+
create_negative_test_thread(THREAD_CREATE_STACK_NULL);
160+
}
161+
162+
/* TESTPOINT: Pass a overflow stack into API */
163+
static void test_thread_create_stack_overflow(void)
164+
{
165+
create_negative_test_thread(THREAD_CTEATE_STACK_SIZE_OVERFLOW);
166+
}
167+
168+
/*test case main entry*/
169+
void test_main(void)
170+
{
171+
k_thread_access_grant(k_current_get(), &tdata, &tstack, &test_tdata, &test_stack);
172+
173+
ztest_test_suite(thread_error_case,
174+
ztest_user_unit_test(test_float_disable),
175+
ztest_user_unit_test(test_timeout_remaining_ticks),
176+
ztest_user_unit_test(test_timeout_expires_ticks),
177+
ztest_user_unit_test(test_thread_create_uninit),
178+
ztest_user_unit_test(test_thread_create_stack_null),
179+
ztest_user_unit_test(test_thread_create_stack_overflow),
180+
ztest_user_unit_test(test_thread_start)
181+
);
182+
ztest_run_test_suite(thread_error_case);
183+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
tests:
2+
kernel.threads.error.case:
3+
tags: kernel threads userspace ignore_faults
4+
filter: CONFIG_USERSPACE

0 commit comments

Comments
 (0)