On Fri, Nov 01, 2024 at 02:45:02PM +0800, David Gow wrote: [...]
+/// ``` +/// // Import our mock naming it as the real module. +/// #[cfg(CONFIG_KUNIT)] +/// use bindings_mock_example as bindings; +/// +/// // This module mocks `bindings`. +/// mod bindings_mock_example { +/// use kernel::kunit::in_kunit_test; +/// use kernel::bindings::u64_; +/// +/// // Make the other binding functions available. +/// pub(crate) use kernel::bindings::*; +/// +/// // Mock `ktime_get_boot_fast_ns` to return a well-known value when running a KUnit test. +/// pub(crate) unsafe fn ktime_get_boot_fast_ns() -> u64_ {
Clippy complains this `unsafe` pub function doesn't have a "# Safety" section. Actually this function is not necessarily to be `unsafe`.
+/// if in_kunit_test() { +/// 1234 +/// } else { +/// unsafe { kernel::bindings::ktime_get_boot_fast_ns() }
Need safety comments here,
+/// } +/// } +/// } +/// +/// // This is the function we want to test. Since `bindings` has been mocked, we can use its +/// // functions seamlessly. +/// fn get_boot_ns() -> u64 { +/// unsafe { bindings::ktime_get_boot_fast_ns() }
and here. If you make ktime_get_boot_fast_ns() safe, then no unsafe block is needed here.
Regards, Boqun
+/// } +/// +/// let time = get_boot_ns(); +/// assert_eq!(time, 1234); +/// ``` +pub fn in_kunit_test() -> bool {
- // SAFETY: kunit_get_current_test() is always safe to call from C (it has fallbacks for
- // when KUnit is not enabled), and we're only comparing the result to NULL.
- unsafe { !bindings::kunit_get_current_test().is_null() }
+}
#[kunit_tests(rust_kernel_kunit)] mod tests {
- use super::*;
- #[test] fn rust_test_kunit_example_test() { assert_eq!(1 + 1, 2); }
- #[test]
- fn rust_test_kunit_in_kunit_test() {
let in_kunit = in_kunit_test();
assert!(in_kunit);
- }
}
2.47.0.199.ga7371fff76-goog