Skip to content

Commit 187697c

Browse files
Zhaoningxcarlescufi
authored andcommitted
tests: stack: add some testcases
Add some testcases for stack source code coverage, and add a fatal handler function to hand the error by null parameter. Signed-off-by: Ningx Zhao <[email protected]>
1 parent 05a08a3 commit 187697c

File tree

3 files changed

+151
-1
lines changed

3 files changed

+151
-1
lines changed

tests/kernel/stack/stack/src/main.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,16 @@ extern void test_stack_thread2isr(void);
7777
extern void test_stack_pop_fail(void);
7878
extern void test_stack_alloc_thread2thread(void);
7979
extern void test_stack_pop_can_wait(void);
80+
extern void test_stack_cleanup_error(void);
81+
extern void test_stack_push_full(void);
8082
#ifdef CONFIG_USERSPACE
8183
extern void test_stack_user_thread2thread(void);
8284
extern void test_stack_user_pop_fail(void);
85+
extern void test_stack_user_init_null(void);
86+
extern void test_stack_user_init_invalid_value(void);
87+
extern void test_stack_user_push_null(void);
88+
extern void test_stack_user_pop_null(void);
89+
extern void test_stack_user_pop_permission(void);
8390
#else
8491
#define dummy_test(_name) \
8592
static void _name(void) \
@@ -89,6 +96,11 @@ extern void test_stack_user_pop_fail(void);
8996

9097
dummy_test(test_stack_user_thread2thread);
9198
dummy_test(test_stack_user_pop_fail);
99+
dummy_test(test_stack_user_init_null);
100+
dummy_test(test_stack_user_init_invalid_value);
101+
dummy_test(test_stack_user_push_null);
102+
dummy_test(test_stack_user_pop_null);
103+
dummy_test(test_stack_user_pop_permission);
92104
#endif /* CONFIG_USERSPACE */
93105

94106
/* entry of contexts */
@@ -342,7 +354,14 @@ void test_main(void)
342354
ztest_user_unit_test(test_stack_user_thread2thread),
343355
ztest_unit_test(test_stack_thread2isr),
344356
ztest_unit_test(test_stack_pop_fail),
357+
ztest_unit_test(test_stack_cleanup_error),
358+
ztest_unit_test(test_stack_push_full),
345359
ztest_user_unit_test(test_stack_user_pop_fail),
360+
ztest_user_unit_test(test_stack_user_init_null),
361+
ztest_user_unit_test(test_stack_user_init_invalid_value),
362+
ztest_user_unit_test(test_stack_user_push_null),
363+
ztest_user_unit_test(test_stack_user_pop_null),
364+
ztest_user_unit_test(test_stack_user_pop_permission),
346365
ztest_unit_test(test_stack_alloc_thread2thread),
347366
ztest_user_unit_test(test_single_stack_play),
348367
ztest_1cpu_user_unit_test(test_dual_stack_play),

tests/kernel/stack/stack/src/test_stack_fail.c

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,33 @@
88
#include <irq_offload.h>
99

1010
#define TIMEOUT K_MSEC(100)
11+
#define STACK_SIZE (512 + CONFIG_TEST_EXTRA_STACKSIZE)
1112
#define STACK_LEN 2
1213

1314
static ZTEST_BMEM stack_data_t data[STACK_LEN];
1415
extern struct k_stack stack;
16+
K_THREAD_STACK_DEFINE(threadstack2, STACK_SIZE);
17+
struct k_thread thread_data2;
18+
19+
/* action after self-defined fatal handler. */
20+
static ZTEST_BMEM volatile bool valid_fault;
21+
22+
static inline void set_fault_valid(bool valid)
23+
{
24+
valid_fault = valid;
25+
}
26+
27+
void k_sys_fatal_error_handler(unsigned int reason, const z_arch_esf_t *pEsf)
28+
{
29+
printk("Caught system error -- reason %d %d\n", reason, valid_fault);
30+
if (valid_fault) {
31+
printk("Fatal error expected as part of test case.\n");
32+
valid_fault = false; /* reset back to normal */
33+
} else {
34+
printk("Fatal error was unexpected, aborting...\n");
35+
k_fatal_halt(reason);
36+
}
37+
}
1538

1639
static void stack_pop_fail(struct k_stack *stack)
1740
{
@@ -28,6 +51,12 @@ static void stack_pop_fail(struct k_stack *stack)
2851
* @{
2952
*/
3053

54+
/* Sub-thread entry */
55+
void tStack_pop_entry(void *p1, void *p2, void *p3)
56+
{
57+
zassert_true(k_stack_pop(p1, p2, K_FOREVER), "stack pop failed\n");
58+
}
59+
3160
/**
3261
* @brief Verifies stack pop functionality
3362
* @see k_stack_init(), k_stack_pop()
@@ -39,6 +68,46 @@ void test_stack_pop_fail(void)
3968
stack_pop_fail(&stack);
4069
}
4170

71+
/**
72+
* @brief Verifies cleanup a stack that still be needed by another
73+
* thread.
74+
* @see k_stack_cleanup()
75+
*/
76+
void test_stack_cleanup_error(void)
77+
{
78+
stack_data_t rx_data[STACK_LEN - 1];
79+
80+
k_stack_init(&stack, data, STACK_LEN);
81+
/* Creat a new thread */
82+
k_tid_t tid = k_thread_create(&thread_data2, threadstack2, STACK_SIZE,
83+
tStack_pop_entry, &stack,
84+
rx_data, NULL, K_PRIO_PREEMPT(0), 0,
85+
K_NO_WAIT);
86+
/* Delay for finishing some actions of the new thread */
87+
k_sleep(K_MSEC(500));
88+
/* Try to clean up the stack, that still be waited by the thread */
89+
zassert_true(k_stack_cleanup(&stack) == -EAGAIN, "The stack is cleanuped successful");
90+
/* clear the spawn thread to avoid side effect */
91+
k_thread_abort(tid);
92+
}
93+
94+
/**
95+
* @brief Verifies push a data in the full stack.
96+
* @see k_stack_push()
97+
*/
98+
void test_stack_push_full(void)
99+
{
100+
stack_data_t tx_data[STACK_LEN] = {0};
101+
stack_data_t data_tmp = 0;
102+
103+
k_stack_init(&stack, data, STACK_LEN);
104+
for (int i = 0; i < STACK_LEN; i++) {
105+
zassert_true(k_stack_push(&stack, tx_data[i]) == 0, "push data into stack failed");
106+
}
107+
/* Verify that push a data in the full stack, a nagetive value will be met */
108+
zassert_true(k_stack_push(&stack, data_tmp) == -ENOMEM, "push data successful");
109+
}
110+
42111
#ifdef CONFIG_USERSPACE
43112
/**
44113
* @brief Verifies stack pop from a user thread
@@ -54,6 +123,68 @@ void test_stack_user_pop_fail(void)
54123

55124
stack_pop_fail(alloc_stack);
56125
}
126+
127+
/**
128+
* @brief Verifies stack alloc and initialize a null pointer.
129+
* @see k_stack_alloc_init()
130+
*/
131+
void test_stack_user_init_null(void)
132+
{
133+
set_fault_valid(true);
134+
k_stack_alloc_init(NULL, STACK_LEN);
135+
}
136+
137+
/**
138+
* @brief Verify that alloc and initialize a stack with
139+
* 0 memory.
140+
* @see k_stack_alloc_init()
141+
*/
142+
void test_stack_user_init_invalid_value(void)
143+
{
144+
set_fault_valid(true);
145+
struct k_stack *alloc_stack = k_object_alloc(K_OBJ_STACK);
146+
147+
zassert_not_null(alloc_stack, "couldn't allocate stack object");
148+
k_stack_alloc_init(alloc_stack, 0);
149+
}
150+
151+
/**
152+
* @brief Verify that push some data into a NULL
153+
* pointer.
154+
* @see k_stack_push()
155+
*/
156+
void test_stack_user_push_null(void)
157+
{
158+
set_fault_valid(true);
159+
k_stack_push(NULL, 0);
160+
}
161+
162+
/**
163+
* @brief Verifies pop data from a NULL pointer.
164+
* @see k_stack_pop()
165+
*/
166+
void test_stack_user_pop_null(void)
167+
{
168+
set_fault_valid(true);
169+
k_stack_pop(NULL, 0, K_NO_WAIT);
170+
}
171+
172+
/**
173+
* @brief Verifies cleanup a stack that its data still be waited by
174+
* another thread.
175+
* @see k_stack_pop()
176+
*/
177+
void test_stack_user_pop_permission(void)
178+
{
179+
set_fault_valid(true);
180+
struct k_stack *alloc_stack = k_object_alloc(K_OBJ_STACK);
181+
182+
zassert_not_null(alloc_stack, "couldn't allocate stack object");
183+
zassert_false(k_stack_alloc_init(alloc_stack, STACK_LEN),
184+
"stack init failed");
185+
/* Try to access and to write data at invalid address */
186+
k_stack_pop(alloc_stack, (stack_data_t *)alloc_stack, K_NO_WAIT);
187+
}
57188
#endif
58189
/**
59190
* @}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
tests:
22
kernel.stack.usage:
3-
tags: kernel userspace
3+
tags: kernel userspace ignore_faults

0 commit comments

Comments
 (0)