* [PATCH v2] rust: error: add KUnit tests for errno conversions
@ 2026-11-25 12:09 Zixin Wang
2026-11-25 12:50 ` Miguel Ojeda
0 siblings, 1 reply; 3+ messages in thread
From: Zixin Wang @ 2026-11-25 12:09 UTC (permalink / raw)
To: Miguel Ojeda
Cc: Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
Andreas Hindborg, Alice Ryhl, Trevor Gross, Danilo Krummrich,
Daniel Almeida, Tamir Duberstein, Alexandre Courbot,
Onur Özkan, rust-for-linux, Zixin Wang
Add KUnit tests for the `error` module covering the conversions
between `Error` and the C error codes:
- Round-trips between the `code` constants, `Error::from_errno()`
and `Error::to_errno()`.
- The bounds accepted and rejected by `try_from_errno()`.
- The success and error cases of `to_result()`.
- The `to_ptr()` / `from_err_ptr()` round-trip, including `NULL`
and regular pointers.
- The `From` conversions from the standard error types.
- `name()` reporting the errno name, when `CONFIG_SYMBOLIC_ERRNAME`
is enabled.
The tests are gated behind the new `CONFIG_RUST_ERROR_KUNIT_TEST`
option.
The suite passes when booting the kernel under QEMU with
`kunit.filter_glob=rust_kernel_error*`, with and without
`CONFIG_SYMBOLIC_ERRNAME` (the latter reports 5 passed, 1 skipped).
Signed-off-by: Zixin Wang <w1355457260@gmail.com>
---
Changes in v2:
- Gate `name_reports_errno_name` on `CONFIG_SYMBOLIC_ERRNAME`: when the
option is disabled, `errname()` is a stub returning `NULL`, so
`Error::name()` returns `None` and the test would fail (reported by
Sashiko).
rust/kernel/Kconfig.test | 10 ++++
rust/kernel/error.rs | 102 +++++++++++++++++++++++++++++++++++++++
2 files changed, 112 insertions(+)
diff --git a/rust/kernel/Kconfig.test b/rust/kernel/Kconfig.test
index 3bf4194f28c7..54c8bfa66afa 100644
--- a/rust/kernel/Kconfig.test
+++ b/rust/kernel/Kconfig.test
@@ -104,4 +104,14 @@ config RUST_TIME_KUNIT_TEST
If unsure, say N.
+config RUST_ERROR_KUNIT_TEST
+ bool "KUnit tests for the Rust error API" if !KUNIT_ALL_TESTS
+ default KUNIT_ALL_TESTS
+ help
+ This option enables KUnit tests for the Rust error API.
+ These are only for development and testing, not for regular
+ kernel use cases.
+
+ If unsure, say N.
+
endif
diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs
index 9003a1b86da8..7b82e60b87b0 100644
--- a/rust/kernel/error.rs
+++ b/rust/kernel/error.rs
@@ -666,3 +666,105 @@ pub fn from_result<T, F>(f: F) -> T
/// Error message for calling a default function of a [`#[vtable]`](macros::vtable) trait.
pub const VTABLE_DEFAULT_ERROR: &str =
"This function must not be called, see the #[vtable] documentation.";
+
+#[cfg(CONFIG_RUST_ERROR_KUNIT_TEST)]
+#[macros::kunit_tests(rust_kernel_error)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn code_constants_round_trip() {
+ macro_rules! check {
+ ($($errno:ident),* $(,)?) => {
+ $(
+ assert_eq!(code::$errno.to_errno(), -(crate::bindings::$errno as i32));
+ assert_eq!(
+ Error::from_errno(-(crate::bindings::$errno as i32)),
+ code::$errno
+ );
+ )*
+ };
+ }
+
+ check!(
+ EPERM, ENOENT, EINTR, EIO, ENOMEM, EACCES, EFAULT, EBUSY, EEXIST, EINVAL, ENOSPC,
+ EPIPE, ERANGE, EOPNOTSUPP, ETIMEDOUT,
+ );
+ }
+
+ #[test]
+ fn try_from_errno_bounds() {
+ let max_errno = crate::bindings::MAX_ERRNO as i32;
+
+ // In-range values round-trip.
+ assert_eq!(Error::try_from_errno(-1).map(Error::to_errno), Some(-1));
+ assert_eq!(
+ Error::try_from_errno(-max_errno).map(Error::to_errno),
+ Some(-max_errno)
+ );
+
+ // Out-of-range values are rejected.
+ assert_eq!(Error::try_from_errno(0), None);
+ assert_eq!(Error::try_from_errno(1), None);
+ assert_eq!(Error::try_from_errno(-max_errno - 1), None);
+ assert_eq!(Error::try_from_errno(i32::MIN), None);
+ }
+
+ #[test]
+ fn to_result_converts() {
+ // Zero and positive values signal success.
+ assert_eq!(to_result(0), Ok(()));
+ assert_eq!(to_result(1), Ok(()));
+
+ // In-range negative values are errors.
+ assert_eq!(
+ to_result(-(crate::bindings::EINVAL as i32)),
+ Err(code::EINVAL)
+ );
+ let max_errno = crate::bindings::MAX_ERRNO as i32;
+ assert_eq!(
+ to_result(-max_errno).map_err(Error::to_errno),
+ Err(-max_errno)
+ );
+ }
+
+ #[test]
+ fn err_ptr_round_trip() {
+ // An error pointer is converted back to the same error.
+ let err = code::ENOMEM;
+ assert_eq!(from_err_ptr(err.to_ptr::<crate::ffi::c_void>()), Err(err));
+
+ // `NULL` and regular pointers are not errors.
+ let null = core::ptr::null_mut::<crate::ffi::c_void>();
+ assert_eq!(from_err_ptr(null), Ok(null));
+
+ let ptr = 0x1234 as *mut crate::ffi::c_void;
+ assert_eq!(from_err_ptr(ptr), Ok(ptr));
+ }
+
+ #[test]
+ fn conversions_from_error_types() {
+ // Allocation errors map to `ENOMEM`.
+ assert_eq!(Error::from(crate::alloc::AllocError), code::ENOMEM);
+ assert_eq!(Error::from(crate::alloc::layout::LayoutError), code::ENOMEM);
+
+ // Conversion, UTF-8 and formatting errors map to `EINVAL`.
+ assert_eq!(
+ Error::from(u8::try_from(u32::MAX).unwrap_err()),
+ code::EINVAL
+ );
+ // Deliberately invalid UTF-8 to exercise the error conversion.
+ #[expect(invalid_from_utf8)]
+ let utf8_err = core::str::from_utf8(&[0xff]).unwrap_err();
+ assert_eq!(Error::from(utf8_err), code::EINVAL);
+ assert_eq!(Error::from(crate::fmt::Error), code::EINVAL);
+ }
+
+ #[test]
+ #[cfg(CONFIG_SYMBOLIC_ERRNAME)]
+ fn name_reports_errno_name() {
+ assert_eq!(code::EPERM.name().unwrap().to_bytes(), b"EPERM");
+ assert_eq!(code::EINVAL.name().unwrap().to_bytes(), b"EINVAL");
+ assert_eq!(code::ENOMEM.name().unwrap().to_bytes(), b"ENOMEM");
+ }
+}
--
2.54.0
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH v2] rust: error: add KUnit tests for errno conversions
2026-11-25 12:09 [PATCH v2] rust: error: add KUnit tests for errno conversions Zixin Wang
@ 2026-11-25 12:50 ` Miguel Ojeda
2026-11-25 13:05 ` Zixin Wang
0 siblings, 1 reply; 3+ messages in thread
From: Miguel Ojeda @ 2026-11-25 12:50 UTC (permalink / raw)
To: Zixin Wang
Cc: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
Danilo Krummrich, Daniel Almeida, Tamir Duberstein,
Alexandre Courbot, Onur Özkan, rust-for-linux
On Wed, Nov 25, 2026 at 1:10 PM Zixin Wang <w1355457260@gmail.com> wrote:
>
> - Gate `name_reports_errno_name` on `CONFIG_SYMBOLIC_ERRNAME`: when the
> option is disabled, `errname()` is a stub returning `NULL`, so
> `Error::name()` returns `None` and the test would fail (reported by
> Sashiko).
We could keep the test adding the `!SYMBOLIC_ERRNAME` case instead, no?
i.e. gating with `cfg` two sets of asserts.
Cheers,
Miguel
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH v2] rust: error: add KUnit tests for errno conversions
2026-11-25 12:50 ` Miguel Ojeda
@ 2026-11-25 13:05 ` Zixin Wang
0 siblings, 0 replies; 3+ messages in thread
From: Zixin Wang @ 2026-11-25 13:05 UTC (permalink / raw)
To: Miguel Ojeda
Cc: Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
Andreas Hindborg, Alice Ryhl, Trevor Gross, Danilo Krummrich,
Daniel Almeida, Tamir Duberstein, Alexandre Courbot,
Onur Özkan, rust-for-linux
Yes, that is better -- the disabled case is now covered as well. I will
send a v3 with the two sets of `cfg`-gated assertions; both configurations
now run all six tests.
Thanks!
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-25 13:05 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25 12:09 [PATCH v2] rust: error: add KUnit tests for errno conversions Zixin Wang
2026-11-25 12:50 ` Miguel Ojeda
2026-11-25 13:05 ` Zixin Wang
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox