1*a66d733dSMiguel Ojeda // SPDX-License-Identifier: GPL-2.0
2*a66d733dSMiguel Ojeda
3*a66d733dSMiguel Ojeda //! KUnit-based macros for Rust unit tests.
4*a66d733dSMiguel Ojeda //!
5*a66d733dSMiguel Ojeda //! C header: [`include/kunit/test.h`](../../../../../include/kunit/test.h)
6*a66d733dSMiguel Ojeda //!
7*a66d733dSMiguel Ojeda //! Reference: <https://docs.kernel.org/dev-tools/kunit/index.html>
8*a66d733dSMiguel Ojeda
9*a66d733dSMiguel Ojeda use core::{ffi::c_void, fmt};
10*a66d733dSMiguel Ojeda
11*a66d733dSMiguel Ojeda /// Prints a KUnit error-level message.
12*a66d733dSMiguel Ojeda ///
13*a66d733dSMiguel Ojeda /// Public but hidden since it should only be used from KUnit generated code.
14*a66d733dSMiguel Ojeda #[doc(hidden)]
err(args: fmt::Arguments<'_>)15*a66d733dSMiguel Ojeda pub fn err(args: fmt::Arguments<'_>) {
16*a66d733dSMiguel Ojeda // SAFETY: The format string is null-terminated and the `%pA` specifier matches the argument we
17*a66d733dSMiguel Ojeda // are passing.
18*a66d733dSMiguel Ojeda #[cfg(CONFIG_PRINTK)]
19*a66d733dSMiguel Ojeda unsafe {
20*a66d733dSMiguel Ojeda bindings::_printk(
21*a66d733dSMiguel Ojeda b"\x013%pA\0".as_ptr() as _,
22*a66d733dSMiguel Ojeda &args as *const _ as *const c_void,
23*a66d733dSMiguel Ojeda );
24*a66d733dSMiguel Ojeda }
25*a66d733dSMiguel Ojeda }
26*a66d733dSMiguel Ojeda
27*a66d733dSMiguel Ojeda /// Prints a KUnit info-level message.
28*a66d733dSMiguel Ojeda ///
29*a66d733dSMiguel Ojeda /// Public but hidden since it should only be used from KUnit generated code.
30*a66d733dSMiguel Ojeda #[doc(hidden)]
info(args: fmt::Arguments<'_>)31*a66d733dSMiguel Ojeda pub fn info(args: fmt::Arguments<'_>) {
32*a66d733dSMiguel Ojeda // SAFETY: The format string is null-terminated and the `%pA` specifier matches the argument we
33*a66d733dSMiguel Ojeda // are passing.
34*a66d733dSMiguel Ojeda #[cfg(CONFIG_PRINTK)]
35*a66d733dSMiguel Ojeda unsafe {
36*a66d733dSMiguel Ojeda bindings::_printk(
37*a66d733dSMiguel Ojeda b"\x016%pA\0".as_ptr() as _,
38*a66d733dSMiguel Ojeda &args as *const _ as *const c_void,
39*a66d733dSMiguel Ojeda );
40*a66d733dSMiguel Ojeda }
41*a66d733dSMiguel Ojeda }
42*a66d733dSMiguel Ojeda
43*a66d733dSMiguel Ojeda /// Asserts that a boolean expression is `true` at runtime.
44*a66d733dSMiguel Ojeda ///
45*a66d733dSMiguel Ojeda /// Public but hidden since it should only be used from generated tests.
46*a66d733dSMiguel Ojeda ///
47*a66d733dSMiguel Ojeda /// Unlike the one in `core`, this one does not panic; instead, it is mapped to the KUnit
48*a66d733dSMiguel Ojeda /// facilities. See [`assert!`] for more details.
49*a66d733dSMiguel Ojeda #[doc(hidden)]
50*a66d733dSMiguel Ojeda #[macro_export]
51*a66d733dSMiguel Ojeda macro_rules! kunit_assert {
52*a66d733dSMiguel Ojeda ($name:literal, $file:literal, $diff:expr, $condition:expr $(,)?) => {
53*a66d733dSMiguel Ojeda 'out: {
54*a66d733dSMiguel Ojeda // Do nothing if the condition is `true`.
55*a66d733dSMiguel Ojeda if $condition {
56*a66d733dSMiguel Ojeda break 'out;
57*a66d733dSMiguel Ojeda }
58*a66d733dSMiguel Ojeda
59*a66d733dSMiguel Ojeda static FILE: &'static $crate::str::CStr = $crate::c_str!($file);
60*a66d733dSMiguel Ojeda static LINE: i32 = core::line!() as i32 - $diff;
61*a66d733dSMiguel Ojeda static CONDITION: &'static $crate::str::CStr = $crate::c_str!(stringify!($condition));
62*a66d733dSMiguel Ojeda
63*a66d733dSMiguel Ojeda // SAFETY: FFI call without safety requirements.
64*a66d733dSMiguel Ojeda let kunit_test = unsafe { $crate::bindings::kunit_get_current_test() };
65*a66d733dSMiguel Ojeda if kunit_test.is_null() {
66*a66d733dSMiguel Ojeda // The assertion failed but this task is not running a KUnit test, so we cannot call
67*a66d733dSMiguel Ojeda // KUnit, but at least print an error to the kernel log. This may happen if this
68*a66d733dSMiguel Ojeda // macro is called from an spawned thread in a test (see
69*a66d733dSMiguel Ojeda // `scripts/rustdoc_test_gen.rs`) or if some non-test code calls this macro by
70*a66d733dSMiguel Ojeda // mistake (it is hidden to prevent that).
71*a66d733dSMiguel Ojeda //
72*a66d733dSMiguel Ojeda // This mimics KUnit's failed assertion format.
73*a66d733dSMiguel Ojeda $crate::kunit::err(format_args!(
74*a66d733dSMiguel Ojeda " # {}: ASSERTION FAILED at {FILE}:{LINE}\n",
75*a66d733dSMiguel Ojeda $name
76*a66d733dSMiguel Ojeda ));
77*a66d733dSMiguel Ojeda $crate::kunit::err(format_args!(
78*a66d733dSMiguel Ojeda " Expected {CONDITION} to be true, but is false\n"
79*a66d733dSMiguel Ojeda ));
80*a66d733dSMiguel Ojeda $crate::kunit::err(format_args!(
81*a66d733dSMiguel Ojeda " Failure not reported to KUnit since this is a non-KUnit task\n"
82*a66d733dSMiguel Ojeda ));
83*a66d733dSMiguel Ojeda break 'out;
84*a66d733dSMiguel Ojeda }
85*a66d733dSMiguel Ojeda
86*a66d733dSMiguel Ojeda #[repr(transparent)]
87*a66d733dSMiguel Ojeda struct Location($crate::bindings::kunit_loc);
88*a66d733dSMiguel Ojeda
89*a66d733dSMiguel Ojeda #[repr(transparent)]
90*a66d733dSMiguel Ojeda struct UnaryAssert($crate::bindings::kunit_unary_assert);
91*a66d733dSMiguel Ojeda
92*a66d733dSMiguel Ojeda // SAFETY: There is only a static instance and in that one the pointer field points to
93*a66d733dSMiguel Ojeda // an immutable C string.
94*a66d733dSMiguel Ojeda unsafe impl Sync for Location {}
95*a66d733dSMiguel Ojeda
96*a66d733dSMiguel Ojeda // SAFETY: There is only a static instance and in that one the pointer field points to
97*a66d733dSMiguel Ojeda // an immutable C string.
98*a66d733dSMiguel Ojeda unsafe impl Sync for UnaryAssert {}
99*a66d733dSMiguel Ojeda
100*a66d733dSMiguel Ojeda static LOCATION: Location = Location($crate::bindings::kunit_loc {
101*a66d733dSMiguel Ojeda file: FILE.as_char_ptr(),
102*a66d733dSMiguel Ojeda line: LINE,
103*a66d733dSMiguel Ojeda });
104*a66d733dSMiguel Ojeda static ASSERTION: UnaryAssert = UnaryAssert($crate::bindings::kunit_unary_assert {
105*a66d733dSMiguel Ojeda assert: $crate::bindings::kunit_assert {},
106*a66d733dSMiguel Ojeda condition: CONDITION.as_char_ptr(),
107*a66d733dSMiguel Ojeda expected_true: true,
108*a66d733dSMiguel Ojeda });
109*a66d733dSMiguel Ojeda
110*a66d733dSMiguel Ojeda // SAFETY:
111*a66d733dSMiguel Ojeda // - FFI call.
112*a66d733dSMiguel Ojeda // - The `kunit_test` pointer is valid because we got it from
113*a66d733dSMiguel Ojeda // `kunit_get_current_test()` and it was not null. This means we are in a KUnit
114*a66d733dSMiguel Ojeda // test, and that the pointer can be passed to KUnit functions and assertions.
115*a66d733dSMiguel Ojeda // - The string pointers (`file` and `condition` above) point to null-terminated
116*a66d733dSMiguel Ojeda // strings since they are `CStr`s.
117*a66d733dSMiguel Ojeda // - The function pointer (`format`) points to the proper function.
118*a66d733dSMiguel Ojeda // - The pointers passed will remain valid since they point to `static`s.
119*a66d733dSMiguel Ojeda // - The format string is allowed to be null.
120*a66d733dSMiguel Ojeda // - There are, however, problems with this: first of all, this will end up stopping
121*a66d733dSMiguel Ojeda // the thread, without running destructors. While that is problematic in itself,
122*a66d733dSMiguel Ojeda // it is considered UB to have what is effectively a forced foreign unwind
123*a66d733dSMiguel Ojeda // with `extern "C"` ABI. One could observe the stack that is now gone from
124*a66d733dSMiguel Ojeda // another thread. We should avoid pinning stack variables to prevent library UB,
125*a66d733dSMiguel Ojeda // too. For the moment, given that test failures are reported immediately before the
126*a66d733dSMiguel Ojeda // next test runs, that test failures should be fixed and that KUnit is explicitly
127*a66d733dSMiguel Ojeda // documented as not suitable for production environments, we feel it is reasonable.
128*a66d733dSMiguel Ojeda unsafe {
129*a66d733dSMiguel Ojeda $crate::bindings::__kunit_do_failed_assertion(
130*a66d733dSMiguel Ojeda kunit_test,
131*a66d733dSMiguel Ojeda core::ptr::addr_of!(LOCATION.0),
132*a66d733dSMiguel Ojeda $crate::bindings::kunit_assert_type_KUNIT_ASSERTION,
133*a66d733dSMiguel Ojeda core::ptr::addr_of!(ASSERTION.0.assert),
134*a66d733dSMiguel Ojeda Some($crate::bindings::kunit_unary_assert_format),
135*a66d733dSMiguel Ojeda core::ptr::null(),
136*a66d733dSMiguel Ojeda );
137*a66d733dSMiguel Ojeda }
138*a66d733dSMiguel Ojeda
139*a66d733dSMiguel Ojeda // SAFETY: FFI call; the `test` pointer is valid because this hidden macro should only
140*a66d733dSMiguel Ojeda // be called by the generated documentation tests which forward the test pointer given
141*a66d733dSMiguel Ojeda // by KUnit.
142*a66d733dSMiguel Ojeda unsafe {
143*a66d733dSMiguel Ojeda $crate::bindings::__kunit_abort(kunit_test);
144*a66d733dSMiguel Ojeda }
145*a66d733dSMiguel Ojeda }
146*a66d733dSMiguel Ojeda };
147*a66d733dSMiguel Ojeda }
148*a66d733dSMiguel Ojeda
149*a66d733dSMiguel Ojeda /// Asserts that two expressions are equal to each other (using [`PartialEq`]).
150*a66d733dSMiguel Ojeda ///
151*a66d733dSMiguel Ojeda /// Public but hidden since it should only be used from generated tests.
152*a66d733dSMiguel Ojeda ///
153*a66d733dSMiguel Ojeda /// Unlike the one in `core`, this one does not panic; instead, it is mapped to the KUnit
154*a66d733dSMiguel Ojeda /// facilities. See [`assert!`] for more details.
155*a66d733dSMiguel Ojeda #[doc(hidden)]
156*a66d733dSMiguel Ojeda #[macro_export]
157*a66d733dSMiguel Ojeda macro_rules! kunit_assert_eq {
158*a66d733dSMiguel Ojeda ($name:literal, $file:literal, $diff:expr, $left:expr, $right:expr $(,)?) => {{
159*a66d733dSMiguel Ojeda // For the moment, we just forward to the expression assert because, for binary asserts,
160*a66d733dSMiguel Ojeda // KUnit supports only a few types (e.g. integers).
161*a66d733dSMiguel Ojeda $crate::kunit_assert!($name, $file, $diff, $left == $right);
162*a66d733dSMiguel Ojeda }};
163*a66d733dSMiguel Ojeda }
164