Skip to content

Commit 3026df2

Browse files
Enjia Maicarlescufi
authored andcommitted
tests: spinlock: add some error test cases
Add some error test cases for spinlock, include: 1.Validate indentical spinlock cannot be used recursively. 2.Validate unlocking incorrect spinlock will trigger assertion. 3.Validate releasing incorrect spinlock will trigger assertion. Signed-off-by: Enjia Mai <[email protected]>
1 parent ead41bc commit 3026df2

File tree

4 files changed

+133
-1
lines changed

4 files changed

+133
-1
lines changed

tests/kernel/spinlock/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
55
project(spinlock)
66

77
target_sources(app PRIVATE src/main.c)
8+
target_sources(app PRIVATE src/spinlock_error_case.c)

tests/kernel/spinlock/prj.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
CONFIG_ZTEST=y
2+
CONFIG_SPIN_VALIDATE=y

tests/kernel/spinlock/src/main.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,20 @@ void test_spinlock_mutual_exclusion(void)
174174
zassert_true(!lock_runtime.locked, "Spinlock failed to unlock");
175175
}
176176

177+
178+
extern void test_spinlock_no_recursive(void);
179+
extern void test_spinlock_unlock_error(void);
180+
extern void test_spinlock_release_error(void);
181+
182+
177183
void test_main(void)
178184
{
179185
ztest_test_suite(spinlock,
180186
ztest_unit_test(test_spinlock_basic),
181187
ztest_unit_test(test_spinlock_bounce),
182-
ztest_unit_test(test_spinlock_mutual_exclusion));
188+
ztest_unit_test(test_spinlock_mutual_exclusion),
189+
ztest_unit_test(test_spinlock_no_recursive),
190+
ztest_unit_test(test_spinlock_unlock_error),
191+
ztest_unit_test(test_spinlock_release_error));
183192
ztest_run_test_suite(spinlock);
184193
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* Copyright (c) 2018 Intel Corporation.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
#include <zephyr.h>
7+
#include <ztest.h>
8+
#include <spinlock.h>
9+
10+
BUILD_ASSERT(CONFIG_MP_NUM_CPUS > 1);
11+
12+
static struct k_spinlock lock;
13+
static struct k_spinlock mylock;
14+
static k_spinlock_key_t key;
15+
16+
static ZTEST_DMEM volatile bool valid_assert;
17+
18+
static inline void set_assert_valid(bool valid)
19+
{
20+
valid_assert = valid;
21+
}
22+
23+
static void action_after_assert_fail(void)
24+
{
25+
k_spin_unlock(&lock, key);
26+
27+
ztest_test_pass();
28+
}
29+
30+
#ifdef CONFIG_ASSERT_NO_FILE_INFO
31+
void assert_post_action(void)
32+
#else
33+
void assert_post_action(const char *file, unsigned int line)
34+
#endif
35+
{
36+
#ifndef CONFIG_ASSERT_NO_FILE_INFO
37+
ARG_UNUSED(file);
38+
ARG_UNUSED(line);
39+
#endif
40+
41+
printk("Caught an assert.\n");
42+
43+
if (valid_assert) {
44+
valid_assert = false; /* reset back to normal */
45+
printk("Assert error expected as part of test case.\n");
46+
47+
/* do some action after fatal error happened */
48+
action_after_assert_fail();
49+
} else {
50+
printk("Assert failed was unexpected, aborting...\n");
51+
#ifdef CONFIG_USERSPACE
52+
/* User threads aren't allowed to induce kernel panics; generate
53+
* an oops instead.
54+
*/
55+
if (_is_user_context()) {
56+
k_oops();
57+
}
58+
#endif
59+
k_panic();
60+
}
61+
}
62+
63+
64+
/**
65+
* @brief Test spinlock cannot be recursive
66+
*
67+
* @details Validate using spinlock recursive will trigger assertion.
68+
*
69+
* @ingroup kernel_spinlock_tests
70+
*
71+
* @see k_spin_lock()
72+
*/
73+
void test_spinlock_no_recursive(void)
74+
{
75+
k_spinlock_key_t re;
76+
77+
key = k_spin_lock(&lock);
78+
79+
set_assert_valid(true);
80+
re = k_spin_lock(&lock);
81+
82+
ztest_test_fail();
83+
}
84+
85+
/**
86+
* @brief Test unlocking incorrect spinlock
87+
*
88+
* @details Validate unlocking incorrect spinlock will trigger assertion.
89+
*
90+
* @ingroup kernel_spinlock_tests
91+
*
92+
* @see k_spin_unlock()
93+
*/
94+
void test_spinlock_unlock_error(void)
95+
{
96+
key = k_spin_lock(&lock);
97+
98+
set_assert_valid(true);
99+
k_spin_unlock(&mylock, key);
100+
101+
ztest_test_fail();
102+
}
103+
104+
/**
105+
* @brief Test unlocking incorrect spinlock
106+
*
107+
* @details Validate unlocking incorrect spinlock will trigger assertion.
108+
*
109+
* @ingroup kernel_spinlock_tests
110+
*
111+
* @see k_spin_release()
112+
*/
113+
void test_spinlock_release_error(void)
114+
{
115+
key = k_spin_lock(&lock);
116+
117+
set_assert_valid(true);
118+
k_spin_release(&mylock);
119+
120+
ztest_test_fail();
121+
}

0 commit comments

Comments
 (0)