ostd_test/lib.rs
1// SPDX-License-Identifier: MPL-2.0
2
3//! # The kernel mode testing framework of OSTD.
4//!
5//! `ostd-test` stands for kernel-mode testing framework for OSTD. Its goal is to provide a
6//! `cargo test`-like experience for any `#![no_std]` bare metal crates.
7//!
8//! In OSTD, all the tests written in the source tree of the crates will be run
9//! immediately after the initialization of `ostd`. Thus you can use any
10//! feature provided by the frame including the heap allocator, etc.
11//!
12//! By all means, ostd-test is an individual crate that only requires:
13//! - a custom linker script section `.ktest_array`,
14//! - and an alloc implementation.
15//!
16//! And the OSTD happens to provide both of them. Thus, any crates depending
17//! on the OSTD can use ostd-test without any extra dependency.
18//!
19//! ## Usage
20//!
21//! To write a unit test for any crates, it is recommended to create a new test
22//! module, e.g.:
23//!
24//! ```rust
25//! #[cfg(ktest)]
26//! mod test {
27//! use ostd::prelude::*;
28//!
29//! #[ktest]
30//! fn trivial_assertion() {
31//! assert_eq!(0, 0);
32//! }
33//! #[ktest]
34//! #[should_panic]
35//! fn failing_assertion() {
36//! assert_eq!(0, 1);
37//! }
38//! #[ktest]
39//! #[should_panic(expected = "expected panic message")]
40//! fn expect_panic() {
41//! panic!("expected panic message");
42//! }
43//! }
44//! ```
45//!
46//! Any crates using the ostd-test framework should be linked with ostd.
47//!
48//! By the way, `#[ktest]` attribute along also works, but it hinders test control
49//! using cfgs since plain attribute marked test will be executed in all test runs
50//! no matter what cfgs are passed to the compiler. More importantly, using `#[ktest]`
51//! without cfgs occupies binary real estate since the `.ktest_array` section is not
52//! explicitly stripped in normal builds.
53//!
54//! Rust cfg is used to control the compilation of the test module. In cooperation
55//! with the `ktest` framework, OSDK will set the `RUSTFLAGS` environment variable
56//! to pass the cfgs to all rustc invocations. To run the tests, you simply need
57//! to use the command `cargo osdk test` in the crate directory. For more information,
58//! please refer to the OSDK documentation.
59//!
60//! We support the `#[should_panic]` attribute just in the same way as the standard
61//! library do, but the implementation is quite slow currently. Use it with cautious.
62//!
63//! Doctest is not taken into consideration yet, and the interface is subject to
64//! change.
65//!
66
67#![cfg_attr(not(test), no_std)]
68
69extern crate alloc;
70use alloc::{boxed::Box, string::String};
71
72#[derive(Clone, Debug)]
73pub struct PanicInfo {
74 pub message: String,
75 pub file: String,
76 pub line: usize,
77 pub col: usize,
78}
79
80impl core::fmt::Display for PanicInfo {
81 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
82 writeln!(f, "Panicked at {}:{}:{}", self.file, self.line, self.col)?;
83 writeln!(f, "{}", self.message)
84 }
85}
86
87/// The error that may occur during the test.
88#[derive(Clone)]
89pub enum KtestError {
90 Panic(Box<PanicInfo>),
91 ShouldPanicButNoPanic,
92 ExpectedPanicNotMatch(&'static str, Box<PanicInfo>),
93 Unknown,
94}
95
96/// The information of the unit test.
97#[derive(Clone, PartialEq, Debug)]
98pub struct KtestItemInfo {
99 /// The path of the module, not including the function name.
100 ///
101 /// It would be separated by `::`.
102 pub module_path: &'static str,
103 /// The name of the unit test function.
104 pub fn_name: &'static str,
105 /// The name of the crate.
106 pub package: &'static str,
107 /// The source file where the test function resides.
108 pub source: &'static str,
109 /// The line number of the test function in the file.
110 pub line: usize,
111 /// The column number of the test function in the file.
112 pub col: usize,
113}
114
115#[derive(Clone, PartialEq, Debug)]
116pub struct KtestItem {
117 fn_: fn() -> (),
118 should_panic: (bool, Option<&'static str>),
119 info: KtestItemInfo,
120}
121
122type CatchUnwindImpl = fn(f: fn() -> ()) -> Result<(), Box<dyn core::any::Any + Send>>;
123
124impl KtestItem {
125 /// Create a new [`KtestItem`].
126 ///
127 /// Do not use this function directly. Instead, use the `#[ktest]`
128 /// attribute to mark the test function.
129 #[doc(hidden)]
130 pub const fn new(
131 fn_: fn() -> (),
132 should_panic: (bool, Option<&'static str>),
133 info: KtestItemInfo,
134 ) -> Self {
135 Self {
136 fn_,
137 should_panic,
138 info,
139 }
140 }
141
142 /// Get the information of the test.
143 pub fn info(&self) -> &KtestItemInfo {
144 &self.info
145 }
146
147 /// Run the test with a given catch_unwind implementation.
148 pub fn run(&self, catch_unwind_impl: &CatchUnwindImpl) -> Result<(), KtestError> {
149 let test_result = catch_unwind_impl(self.fn_);
150 if !self.should_panic.0 {
151 // Should not panic.
152 match test_result {
153 Ok(()) => Ok(()),
154 Err(e) => match e.downcast::<PanicInfo>() {
155 Ok(s) => Err(KtestError::Panic(s)),
156 Err(_payload) => Err(KtestError::Unknown),
157 },
158 }
159 } else {
160 // Should panic.
161 match test_result {
162 Ok(()) => Err(KtestError::ShouldPanicButNoPanic),
163 Err(e) => match e.downcast::<PanicInfo>() {
164 Ok(s) => {
165 if let Some(expected) = self.should_panic.1 {
166 if s.message == expected {
167 Ok(())
168 } else {
169 Err(KtestError::ExpectedPanicNotMatch(expected, s))
170 }
171 } else {
172 Ok(())
173 }
174 }
175 Err(_payload) => Err(KtestError::Unknown),
176 },
177 }
178 }
179 }
180}
181
182macro_rules! ktest_array {
183 () => {{
184 extern "C" {
185 fn __ktest_array();
186 fn __ktest_array_end();
187 }
188 let item_size = core::mem::size_of::<KtestItem>();
189 let l = (__ktest_array_end as usize - __ktest_array as usize) / item_size;
190 // SAFETY: __ktest_array is a static section consisting of KtestItem.
191 unsafe { core::slice::from_raw_parts(__ktest_array as *const KtestItem, l) }
192 }};
193}
194
195/// The iterator of the ktest array.
196pub struct KtestIter {
197 index: usize,
198}
199
200impl Default for KtestIter {
201 fn default() -> Self {
202 Self::new()
203 }
204}
205
206impl KtestIter {
207 /// Create a new [`KtestIter`].
208 ///
209 /// It will iterate over all the tests (marked with `#[ktest]`).
210 pub fn new() -> Self {
211 Self { index: 0 }
212 }
213}
214
215impl core::iter::Iterator for KtestIter {
216 type Item = KtestItem;
217
218 fn next(&mut self) -> Option<Self::Item> {
219 let ktest_item = ktest_array!().get(self.index)?;
220 self.index += 1;
221 Some(ktest_item.clone())
222 }
223}
224
225// The whitelists that will be generated by the OSDK as static consts.
226// They deliver the target tests that the user wants to run.
227extern "Rust" {
228 static KTEST_TEST_WHITELIST: Option<&'static [&'static str]>;
229 static KTEST_CRATE_WHITELIST: Option<&'static [&'static str]>;
230}
231
232/// Get the whitelist of the tests.
233///
234/// The whitelist is generated by the OSDK runner, indicating name of the
235/// target tests that the user wants to run.
236pub fn get_ktest_test_whitelist() -> Option<&'static [&'static str]> {
237 // SAFETY: The two extern statics in the base crate are generated by OSDK.
238 unsafe { KTEST_TEST_WHITELIST }
239}
240
241/// Get the whitelist of the crates.
242///
243/// The whitelist is generated by the OSDK runner, indicating the target crate
244/// that the user wants to test.
245pub fn get_ktest_crate_whitelist() -> Option<&'static [&'static str]> {
246 // SAFETY: The two extern statics in the base crate are generated by OSDK.
247 unsafe { KTEST_CRATE_WHITELIST }
248}