Skip to content

Commit b51ea27

Browse files
JoseExpositomatthewtgilbride
authored andcommitted
rust: kunit: add KUnit case and suite macros
Add a couple of Rust macros to allow to develop KUnit tests without relying on generated C code: - The `kunit_unsafe_test_suite!` Rust macro is similar to the `kunit_test_suite` C macro. - The `kunit_case!` Rust macro is similar to the `KUNIT_CASE` C macro. It can be used with parameters to generate a test case or without parameters to be used as delimiter in `kunit_test_suite!`. While these macros can be used on their own, a future patch will introduce another macro to create KUnit tests using a user-space like syntax. Co-developed-by: David Gow <davidgow@google.com> Signed-off-by: David Gow <davidgow@google.com> Signed-off-by: José Expósito <jose.exposito89@gmail.com>
1 parent 64bd464 commit b51ea27

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

rust/kernel/kunit.rs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,95 @@ macro_rules! kunit_assert_eq {
161161
$crate::kunit_assert!($name, $file, $diff, $left == $right);
162162
}};
163163
}
164+
165+
/// Represents an individual test case.
166+
///
167+
/// The test case should have the signature
168+
/// `unsafe extern "C" fn test_case(test: *mut crate::bindings::kunit)`.
169+
///
170+
/// The `kunit_unsafe_test_suite!` macro expects a NULL-terminated list of test cases. This macro
171+
/// can be invoked without parameters to generate the delimiter.
172+
#[macro_export]
173+
macro_rules! kunit_case {
174+
() => {
175+
$crate::bindings::kunit_case {
176+
run_case: None,
177+
name: core::ptr::null_mut(),
178+
generate_params: None,
179+
status: $crate::bindings::kunit_status_KUNIT_SUCCESS,
180+
log: core::ptr::null_mut(),
181+
}
182+
};
183+
($name:ident, $run_case:ident) => {
184+
$crate::bindings::kunit_case {
185+
run_case: Some($run_case),
186+
name: $crate::c_str!(core::stringify!($name)).as_char_ptr(),
187+
generate_params: None,
188+
status: $crate::bindings::kunit_status_KUNIT_SUCCESS,
189+
log: core::ptr::null_mut(),
190+
}
191+
};
192+
}
193+
194+
/// Registers a KUnit test suite.
195+
///
196+
/// # Safety
197+
///
198+
/// `test_cases` must be a NULL terminated array of test cases.
199+
///
200+
/// # Examples
201+
///
202+
/// ```ignore
203+
/// unsafe extern "C" fn test_fn(_test: *mut crate::bindings::kunit) {
204+
/// let actual = 1 + 1;
205+
/// let expected = 2;
206+
/// assert_eq!(actual, expected);
207+
/// }
208+
///
209+
/// static mut KUNIT_TEST_CASE: crate::bindings::kunit_case = crate::kunit_case!(name, test_fn);
210+
/// static mut KUNIT_NULL_CASE: crate::bindings::kunit_case = crate::kunit_case!();
211+
/// static mut KUNIT_TEST_CASES: &mut[crate::bindings::kunit_case] = unsafe {
212+
/// &mut[KUNIT_TEST_CASE, KUNIT_NULL_CASE]
213+
/// };
214+
/// crate::kunit_unsafe_test_suite!(suite_name, KUNIT_TEST_CASES);
215+
/// ```
216+
#[macro_export]
217+
macro_rules! kunit_unsafe_test_suite {
218+
($name:ident, $test_cases:ident) => {
219+
const _: () = {
220+
static KUNIT_TEST_SUITE_NAME: [i8; 256] = {
221+
let name_u8 = core::stringify!($name).as_bytes();
222+
let mut ret = [0; 256];
223+
224+
let mut i = 0;
225+
while i < name_u8.len() {
226+
ret[i] = name_u8[i] as i8;
227+
i += 1;
228+
}
229+
230+
ret
231+
};
232+
233+
// SAFETY: `test_cases` is valid as it should be static.
234+
static mut KUNIT_TEST_SUITE: core::cell::UnsafeCell<$crate::bindings::kunit_suite> =
235+
core::cell::UnsafeCell::new($crate::bindings::kunit_suite {
236+
name: KUNIT_TEST_SUITE_NAME,
237+
test_cases: unsafe { $test_cases.as_mut_ptr() },
238+
suite_init: None,
239+
suite_exit: None,
240+
init: None,
241+
exit: None,
242+
status_comment: [0; 256usize],
243+
debugfs: core::ptr::null_mut(),
244+
log: core::ptr::null_mut(),
245+
suite_init_err: 0,
246+
});
247+
248+
// SAFETY: `KUNIT_TEST_SUITE` is static.
249+
#[used]
250+
#[link_section = ".kunit_test_suites"]
251+
static mut KUNIT_TEST_SUITE_ENTRY: *const $crate::bindings::kunit_suite =
252+
unsafe { KUNIT_TEST_SUITE.get() };
253+
};
254+
};
255+
}

rust/kernel/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#![feature(new_uninit)]
1919
#![feature(receiver_trait)]
2020
#![feature(unsize)]
21+
#![feature(const_mut_refs)]
2122

2223
// Ensure conditional compilation based on the kernel configuration works;
2324
// otherwise we may silently break things like initcall handling.

0 commit comments

Comments
 (0)