From 0315b332ebb61c2ae667e4005033753d4255c3f7 Mon Sep 17 00:00:00 2001 From: David Pursell Date: Tue, 28 Nov 2023 15:40:43 -0800 Subject: libavb_rs: export Result types We have a few error types that are commonly going to be wrapped in Results; define and export those Result types here to simplify common usage. Bug: b/290110273 Test: atest libavb_rs_test libavb_rs_uuid_test Change-Id: I6bae0f0e0bcb836899e6f93dc8c0ec1d3509d84e --- rust/src/error.rs | 30 +++++++++++------ rust/src/lib.rs | 5 ++- rust/src/ops.rs | 80 +++++++++++++++++++++++----------------------- rust/src/verify.rs | 16 +++++----- rust/tests/test_ops.rs | 35 ++++++++++---------- rust/tests/verify_tests.rs | 17 +++------- 6 files changed, 93 insertions(+), 90 deletions(-) diff --git a/rust/src/error.rs b/rust/src/error.rs index b5d781e..1e78f5e 100644 --- a/rust/src/error.rs +++ b/rust/src/error.rs @@ -60,6 +60,15 @@ pub enum SlotVerifyError<'a> { Internal, } +/// `Result` type for `SlotVerifyError` errors. +pub type SlotVerifyResult<'a, T> = Result>; + +/// `Result` type for `SlotVerifyError` errors without any `SlotVerifyData`. +/// +/// If the contained error will never hold a `SlotVerifyData`, this is easier to work with compared +/// to `SlotVerifyResult` due to the static lifetime bound. +pub type SlotVerifyNoDataResult = SlotVerifyResult<'static, T>; + impl<'a> SlotVerifyError<'a> { /// Returns a copy of this error without any contained `SlotVerifyData`. /// @@ -109,9 +118,7 @@ impl<'a> fmt::Display for SlotVerifyError<'a> { /// /// TODO(b/290110273): this can be limited to pub(crate) once we've moved the full libavb wrapper /// here. -pub fn slot_verify_enum_to_result( - result: AvbSlotVerifyResult, -) -> Result<(), SlotVerifyError<'static>> { +pub fn slot_verify_enum_to_result(result: AvbSlotVerifyResult) -> SlotVerifyNoDataResult<()> { match result { AvbSlotVerifyResult::AVB_SLOT_VERIFY_RESULT_OK => Ok(()), AvbSlotVerifyResult::AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT => { @@ -159,6 +166,9 @@ pub enum IoError { NotImplemented, } +/// `Result` type for `IoError` errors. +pub type IoResult = Result; + impl fmt::Display for IoError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { @@ -183,11 +193,10 @@ impl From for IoError { // Converts a bindgen `AvbIOResult` enum to a `Result<>`, mapping `AVB_IO_RESULT_OK` to the Rust // equivalent `Ok(())` and errors to the corresponding `Err(IoError)`. // -// This function is also important to serve as a compile-time check that we're handling all the -// libavb enums; if a new one is added to (or removed from) the C code, this will fail to compile +// This function is not currently used, but serves as a compile-time check that we're handling all +// the libavb enums; if a new one is added to or removed from the C code, this will fail to compile // until it is updated to match. -#[allow(dead_code)] -pub(crate) fn io_enum_to_result(result: AvbIOResult) -> Result<(), IoError> { +fn io_enum_to_result(result: AvbIOResult) -> IoResult<()> { match result { AvbIOResult::AVB_IO_RESULT_OK => Ok(()), AvbIOResult::AVB_IO_RESULT_ERROR_OOM => Err(IoError::Oom), @@ -249,6 +258,9 @@ pub enum VbmetaVerifyError { SignatureMismatch, } +/// `Result` type for `VbmetaVerifyError` errors. +pub type VbmetaVerifyResult = Result; + impl fmt::Display for VbmetaVerifyError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { @@ -268,9 +280,7 @@ impl fmt::Display for VbmetaVerifyError { // This function is also important to serve as a compile-time check that we're handling all the // libavb enums; if a new one is added to (or removed from) the C code, this will fail to compile // until it is updated to match. -pub fn vbmeta_verify_enum_to_result( - result: AvbVBMetaVerifyResult, -) -> Result<(), VbmetaVerifyError> { +pub fn vbmeta_verify_enum_to_result(result: AvbVBMetaVerifyResult) -> VbmetaVerifyResult<()> { match result { AvbVBMetaVerifyResult::AVB_VBMETA_VERIFY_RESULT_OK => Ok(()), AvbVBMetaVerifyResult::AVB_VBMETA_VERIFY_RESULT_OK_NOT_SIGNED => { diff --git a/rust/src/lib.rs b/rust/src/lib.rs index c580497..63c3818 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -30,7 +30,10 @@ mod error; mod ops; mod verify; -pub use error::{IoError, SlotVerifyError}; +pub use error::{ + IoError, IoResult, SlotVerifyError, SlotVerifyNoDataResult, SlotVerifyResult, + VbmetaVerifyError, VbmetaVerifyResult, +}; pub use ops::{Ops, PublicKeyForPartitionInfo}; pub use verify::{ slot_verify, HashtreeErrorMode, PartitionData, SlotVerifyData, SlotVerifyFlags, VbmetaData, diff --git a/rust/src/ops.rs b/rust/src/ops.rs index f613fb5..005aa98 100644 --- a/rust/src/ops.rs +++ b/rust/src/ops.rs @@ -19,7 +19,7 @@ extern crate alloc; -use crate::{error::result_to_io_enum, IoError}; +use crate::{error::result_to_io_enum, IoError, IoResult}; use avb_bindgen::{AvbIOResult, AvbOps}; use core::{ cmp::min, @@ -30,9 +30,6 @@ use core::{ #[cfg(feature = "uuid")] use uuid::Uuid; -/// Common `Result` type for `IoError` errors. -type Result = core::result::Result; - /// Base implementation-provided callbacks for verification. /// /// See libavb `AvbOps` for more complete documentation. @@ -54,7 +51,7 @@ pub trait Ops { partition: &CStr, offset: i64, buffer: &mut [u8], - ) -> Result; + ) -> IoResult; /// Returns a reference to preloaded partition contents. /// @@ -71,7 +68,7 @@ pub trait Ops { /// * `Err` if the requested partition has not been preloaded; /// verification will next attempt to load the partition via `read_from_partition()`. /// * Any other `Err` if an error occurred; verification will exit immediately. - fn get_preloaded_partition(&mut self, partition: &CStr) -> Result<&[u8]> { + fn get_preloaded_partition(&mut self, partition: &CStr) -> IoResult<&[u8]> { Err(IoError::NotImplemented) } @@ -88,7 +85,7 @@ pub trait Ops { &mut self, public_key: &[u8], public_key_metadata: Option<&[u8]>, - ) -> Result; + ) -> IoResult; /// Reads the rollback index at the given location. /// @@ -97,7 +94,7 @@ pub trait Ops { /// /// # Returns /// The rollback index at this location or `IoError` on error. - fn read_rollback_index(&mut self, rollback_index_location: usize) -> Result; + fn read_rollback_index(&mut self, rollback_index_location: usize) -> IoResult; /// Writes the rollback index at the given location. /// @@ -113,13 +110,13 @@ pub trait Ops { /// /// # Returns /// Unit on success or `IoError` on error. - fn write_rollback_index(&mut self, rollback_index_location: usize, index: u64) -> Result<()>; + fn write_rollback_index(&mut self, rollback_index_location: usize, index: u64) -> IoResult<()>; /// Returns the device unlock state. /// /// # Returns /// True if the device is unlocked, false if locked, `IoError` on error. - fn read_is_device_unlocked(&mut self) -> Result; + fn read_is_device_unlocked(&mut self) -> IoResult; /// Returns the GUID of the requested partition. /// @@ -134,7 +131,7 @@ pub trait Ops { /// # Returns /// The partition GUID or `IoError` on error. #[cfg(feature = "uuid")] - fn get_unique_guid_for_partition(&mut self, partition: &CStr) -> Result; + fn get_unique_guid_for_partition(&mut self, partition: &CStr) -> IoResult; /// Returns the size of the requested partition. /// @@ -143,7 +140,7 @@ pub trait Ops { /// /// # Returns /// The partition size in bytes or `IoError` on error. - fn get_size_of_partition(&mut self, partition: &CStr) -> Result; + fn get_size_of_partition(&mut self, partition: &CStr) -> IoResult; /// Reads the requested persistent value. /// @@ -163,7 +160,7 @@ pub trait Ops { /// * `IoError::NoSuchValue` if `name` is not a known persistent value. /// * `IoError::InsufficientSpace` with the required size if the `value` buffer is too small. /// * Any other `IoError` on failure. - fn read_persistent_value(&mut self, name: &CStr, value: &mut [u8]) -> Result; + fn read_persistent_value(&mut self, name: &CStr, value: &mut [u8]) -> IoResult; /// Writes the requested persistent value. /// @@ -180,7 +177,7 @@ pub trait Ops { /// * `IoError::NoSuchValue` if `name` is not a supported persistent value. /// * `IoError::InvalidValueSize` if `value` is too large to save as a persistent value. /// * Any other `IoError` on failure. - fn write_persistent_value(&mut self, name: &CStr, value: &[u8]) -> Result<()>; + fn write_persistent_value(&mut self, name: &CStr, value: &[u8]) -> IoResult<()>; /// Erases the requested persistent value. /// @@ -198,7 +195,7 @@ pub trait Ops { /// * Unit on success. /// * `IoError::NoSuchValue` if `name` is not a supported persistent value. /// * Any other `IoError` on failure. - fn erase_persistent_value(&mut self, name: &CStr) -> Result<()>; + fn erase_persistent_value(&mut self, name: &CStr) -> IoResult<()>; /// Checks if the given public key is valid for the given partition. /// @@ -223,7 +220,7 @@ pub trait Ops { partition: &CStr, public_key: &[u8], public_key_metadata: Option<&[u8]>, - ) -> Result; + ) -> IoResult; } /// Info returned from `validare_public_key_for_partition()`. @@ -348,7 +345,7 @@ impl<'a> AsMut for ScopedAvbOps<'a> { /// /// In practice, these conditions are met since we call this exactly once in each callback /// to extract the `Ops`, and drop it at callback completion. -unsafe fn as_ops<'a>(avb_ops: *mut AvbOps) -> Result<&'a mut dyn Ops> { +unsafe fn as_ops<'a>(avb_ops: *mut AvbOps) -> IoResult<&'a mut dyn Ops> { // SAFETY: we created this AvbOps object and passed it to libavb so we know it meets all // the criteria for `as_mut()`. let avb_ops = unsafe { avb_ops.as_mut() }.ok_or(IoError::Io)?; @@ -360,14 +357,14 @@ unsafe fn as_ops<'a>(avb_ops: *mut AvbOps) -> Result<&'a mut dyn Ops> { } /// Converts a non-NULL `ptr` to `()`, NULL to `Err(IoError::Io)`. -fn check_nonnull(ptr: *const T) -> Result<()> { +fn check_nonnull(ptr: *const T) -> IoResult<()> { match ptr.is_null() { true => Err(IoError::Io), false => Ok(()), } } -/// Wraps a callback to convert the given `Result<>` to raw `AvbIOResult` for libavb. +/// Wraps a callback to convert the given `IoResult<>` to raw `AvbIOResult` for libavb. /// /// See corresponding `try_*` function docs. unsafe extern "C" fn read_from_partition( @@ -402,7 +399,7 @@ unsafe fn try_read_from_partition( num_bytes: usize, buffer: *mut c_void, out_num_read: *mut usize, -) -> Result<()> { +) -> IoResult<()> { check_nonnull(partition)?; check_nonnull(buffer)?; check_nonnull(out_num_read)?; @@ -438,7 +435,7 @@ unsafe fn try_read_from_partition( Ok(()) } -/// Wraps a callback to convert the given `Result<>` to raw `AvbIOResult` for libavb. +/// Wraps a callback to convert the given `IoResult<>` to raw `AvbIOResult` for libavb. /// /// See corresponding `try_*` function docs. unsafe extern "C" fn get_preloaded_partition( @@ -471,7 +468,7 @@ unsafe fn try_get_preloaded_partition( num_bytes: usize, out_pointer: *mut *mut u8, out_num_bytes_preloaded: *mut usize, -) -> Result<()> { +) -> IoResult<()> { check_nonnull(partition)?; check_nonnull(out_pointer)?; check_nonnull(out_num_bytes_preloaded)?; @@ -522,7 +519,7 @@ unsafe fn try_get_preloaded_partition( Ok(()) } -/// Wraps a callback to convert the given `Result<>` to raw `AvbIOResult` for libavb. +/// Wraps a callback to convert the given `IoResult<>` to raw `AvbIOResult` for libavb. /// /// See corresponding `try_*` function docs. unsafe extern "C" fn validate_vbmeta_public_key( @@ -556,7 +553,7 @@ unsafe fn try_validate_vbmeta_public_key( public_key_metadata: *const u8, public_key_metadata_length: usize, out_is_trusted: *mut bool, -) -> Result<()> { +) -> IoResult<()> { check_nonnull(public_key_data)?; check_nonnull(out_is_trusted)?; @@ -595,7 +592,7 @@ unsafe fn try_validate_vbmeta_public_key( Ok(()) } -/// Wraps a callback to convert the given `Result<>` to raw `AvbIOResult` for libavb. +/// Wraps a callback to convert the given `IoResult<>` to raw `AvbIOResult` for libavb. /// /// See corresponding `try_*` function docs. unsafe extern "C" fn read_rollback_index( @@ -619,7 +616,7 @@ unsafe fn try_read_rollback_index( ops: *mut AvbOps, rollback_index_location: usize, out_rollback_index: *mut u64, -) -> Result<()> { +) -> IoResult<()> { check_nonnull(out_rollback_index)?; // Initialize the output variables first in case something fails. @@ -641,7 +638,7 @@ unsafe fn try_read_rollback_index( Ok(()) } -/// Wraps a callback to convert the given `Result<>` to raw `AvbIOResult` for libavb. +/// Wraps a callback to convert the given `IoResult<>` to raw `AvbIOResult` for libavb. /// /// See corresponding `try_*` function docs. unsafe extern "C" fn write_rollback_index( @@ -664,7 +661,7 @@ unsafe fn try_write_rollback_index( ops: *mut AvbOps, rollback_index_location: usize, rollback_index: u64, -) -> Result<()> { +) -> IoResult<()> { // SAFETY: // * we only use `ops` objects created via `ScopedAvbOps` as required. // * `ops` is only extracted once and is dropped at the end of the callback. @@ -672,7 +669,7 @@ unsafe fn try_write_rollback_index( ops.write_rollback_index(rollback_index_location, rollback_index) } -/// Wraps a callback to convert the given `Result<>` to raw `AvbIOResult` for libavb. +/// Wraps a callback to convert the given `IoResult<>` to raw `AvbIOResult` for libavb. /// /// See corresponding `try_*` function docs. unsafe extern "C" fn read_is_device_unlocked( @@ -687,7 +684,10 @@ unsafe extern "C" fn read_is_device_unlocked( /// # Safety /// * `ops` must have been created via `ScopedAvbOps`. /// * `out_is_unlocked` must adhere to the requirements of `ptr::write()`. -unsafe fn try_read_is_device_unlocked(ops: *mut AvbOps, out_is_unlocked: *mut bool) -> Result<()> { +unsafe fn try_read_is_device_unlocked( + ops: *mut AvbOps, + out_is_unlocked: *mut bool, +) -> IoResult<()> { check_nonnull(out_is_unlocked)?; // Initialize the output variables first in case something fails. @@ -709,7 +709,7 @@ unsafe fn try_read_is_device_unlocked(ops: *mut AvbOps, out_is_unlocked: *mut bo Ok(()) } -/// Wraps a callback to convert the given `Result<>` to raw `AvbIOResult` for libavb. +/// Wraps a callback to convert the given `IoResult<>` to raw `AvbIOResult` for libavb. /// /// See corresponding `try_*` function docs. unsafe extern "C" fn get_unique_guid_for_partition( @@ -737,7 +737,7 @@ unsafe fn try_get_unique_guid_for_partition( partition: *const c_char, guid_buf: *mut c_char, guid_buf_size: usize, -) -> Result<()> { +) -> IoResult<()> { check_nonnull(partition)?; check_nonnull(guid_buf)?; @@ -794,7 +794,7 @@ unsafe fn try_get_unique_guid_for_partition( Ok(()) } -/// Wraps a callback to convert the given `Result<>` to raw `AvbIOResult` for libavb. +/// Wraps a callback to convert the given `IoResult<>` to raw `AvbIOResult` for libavb. /// /// See corresponding `try_*` function docs. unsafe extern "C" fn get_size_of_partition( @@ -819,7 +819,7 @@ unsafe fn try_get_size_of_partition( ops: *mut AvbOps, partition: *const c_char, out_size_num_bytes: *mut u64, -) -> Result<()> { +) -> IoResult<()> { check_nonnull(partition)?; check_nonnull(out_size_num_bytes)?; @@ -848,7 +848,7 @@ unsafe fn try_get_size_of_partition( Ok(()) } -/// Wraps a callback to convert the given `Result<>` to raw `AvbIOResult` for libavb. +/// Wraps a callback to convert the given `IoResult<>` to raw `AvbIOResult` for libavb. /// /// See corresponding `try_*` function docs. unsafe extern "C" fn read_persistent_value( @@ -880,7 +880,7 @@ unsafe fn try_read_persistent_value( buffer_size: usize, out_buffer: *mut u8, out_num_bytes_read: *mut usize, -) -> Result<()> { +) -> IoResult<()> { check_nonnull(name)?; check_nonnull(out_num_bytes_read)?; @@ -926,7 +926,7 @@ unsafe fn try_read_persistent_value( result.map(|_| ()) } -/// Wraps a callback to convert the given `Result<>` to raw `AvbIOResult` for libavb. +/// Wraps a callback to convert the given `IoResult<>` to raw `AvbIOResult` for libavb. /// /// See corresponding `try_*` function docs. unsafe extern "C" fn write_persistent_value( @@ -949,7 +949,7 @@ unsafe fn try_write_persistent_value( name: *const c_char, value_size: usize, value: *const u8, -) -> Result<()> { +) -> IoResult<()> { check_nonnull(name)?; // SAFETY: @@ -977,7 +977,7 @@ unsafe fn try_write_persistent_value( } } -/// Wraps a callback to convert the given `Result<>` to raw `AvbIOResult` for libavb. +/// Wraps a callback to convert the given `IoResult<>` to raw `AvbIOResult` for libavb. /// /// See corresponding `try_*` function docs. unsafe extern "C" fn validate_public_key_for_partition( @@ -1018,7 +1018,7 @@ unsafe fn try_validate_public_key_for_partition( public_key_metadata_length: usize, out_is_trusted: *mut bool, out_rollback_index_location: *mut u32, -) -> Result<()> { +) -> IoResult<()> { check_nonnull(partition)?; check_nonnull(public_key_data)?; check_nonnull(out_is_trusted)?; diff --git a/rust/src/verify.rs b/rust/src/verify.rs index 62ecf0b..288aed2 100644 --- a/rust/src/verify.rs +++ b/rust/src/verify.rs @@ -20,7 +20,7 @@ use crate::{ error::{ slot_verify_enum_to_result, vbmeta_verify_enum_to_result, SlotVerifyError, - VbmetaVerifyError, + SlotVerifyNoDataResult, SlotVerifyResult, VbmetaVerifyResult, }, ops, IoError, Ops, }; @@ -41,7 +41,7 @@ pub use avb_bindgen::AvbHashtreeErrorMode as HashtreeErrorMode; pub use avb_bindgen::AvbSlotVerifyFlags as SlotVerifyFlags; /// Returns `Err(SlotVerifyError::Internal)` if the given pointer is `NULL`. -fn check_nonnull(ptr: *const T) -> Result<(), SlotVerifyError<'static>> { +fn check_nonnull(ptr: *const T) -> SlotVerifyNoDataResult<()> { match ptr.is_null() { true => Err(SlotVerifyError::Internal), false => Ok(()), @@ -66,7 +66,7 @@ impl VbmetaData { /// objects ourselves, we just cast them from the C structs provided by libavb. /// /// Returns `Err(SlotVerifyError::Internal)` on failure. - fn validate(&self) -> Result<(), SlotVerifyError<'static>> { + fn validate(&self) -> SlotVerifyNoDataResult<()> { check_nonnull(self.0.partition_name)?; check_nonnull(self.0.vbmeta_data)?; Ok(()) @@ -89,7 +89,7 @@ impl VbmetaData { } /// Returns the vbmeta verification result. - pub fn verify_result(&self) -> Result<(), VbmetaVerifyError> { + pub fn verify_result(&self) -> VbmetaVerifyResult<()> { vbmeta_verify_enum_to_result(self.0.verify_result) } } @@ -122,7 +122,7 @@ impl PartitionData { /// objects ourselves, we just cast them from the C structs provided by libavb. /// /// Returns `Err(SlotVerifyError::Internal)` on failure. - fn validate(&self) -> Result<(), SlotVerifyError<'static>> { + fn validate(&self) -> SlotVerifyNoDataResult<()> { check_nonnull(self.0.partition_name)?; check_nonnull(self.0.data)?; Ok(()) @@ -153,7 +153,7 @@ impl PartitionData { /// /// Only top-level `Verification` errors will contain valid `SlotVerifyData` objects, if this /// individual partition returns a `Verification` error the error will always contain `None`. - pub fn verify_result(&self) -> Result<(), SlotVerifyError<'static>> { + pub fn verify_result(&self) -> SlotVerifyNoDataResult<()> { slot_verify_enum_to_result(self.0.verify_result) } } @@ -215,7 +215,7 @@ impl<'a> SlotVerifyData<'a> { unsafe fn new( data: *mut AvbSlotVerifyData, ops: &'a mut dyn Ops, - ) -> Result> { + ) -> SlotVerifyNoDataResult { let ret = Self { raw_data: NonNull::new(data).ok_or(SlotVerifyError::Internal)?, _ops: PhantomData, @@ -356,7 +356,7 @@ pub fn slot_verify<'a>( ab_suffix: Option<&CStr>, flags: SlotVerifyFlags, hashtree_error_mode: HashtreeErrorMode, -) -> Result, SlotVerifyError<'a>> { +) -> SlotVerifyResult<'a, SlotVerifyData<'a>> { let mut user_data = ops::UserData::new(ops); let mut scoped_ops = ops::ScopedAvbOps::new(&mut user_data); let avb_ops = scoped_ops.as_mut(); diff --git a/rust/tests/test_ops.rs b/rust/tests/test_ops.rs index 1e1ad1b..aa8c579 100644 --- a/rust/tests/test_ops.rs +++ b/rust/tests/test_ops.rs @@ -14,14 +14,11 @@ //! Provides `avb::Ops` test fixtures. -use avb::{IoError, Ops, PublicKeyForPartitionInfo}; +use avb::{IoError, IoResult, Ops, PublicKeyForPartitionInfo}; use std::{cmp::min, collections::HashMap, ffi::CStr}; #[cfg(feature = "uuid")] use uuid::Uuid; -/// Common `Result` type for `IoError` errors. -type Result = core::result::Result; - /// Represents a single fake partition. #[derive(Default)] pub struct FakePartition { @@ -64,12 +61,12 @@ pub struct TestOps { pub rollbacks: HashMap, /// Unlock state. Set an error to simulate IoError during access. - pub unlock_state: Result, + pub unlock_state: IoResult, /// Persistent named values. Set an error to simulate `IoError` during access. Writing /// a non-existent persistent value will create it; to simulate `NoSuchValue` instead, /// create an entry with `Err(IoError::NoSuchValue)` as the value. - pub persistent_values: HashMap>>, + pub persistent_values: HashMap>>, } impl TestOps { @@ -105,7 +102,7 @@ impl TestOps { /// test_ops.add_persistent_value("foo", Ok(b"contents")); /// test_ops.add_persistent_value("bar", Err(IoError::NoSuchValue)); /// ``` - pub fn add_persistent_value(&mut self, name: &str, contents: Result<&[u8]>) { + pub fn add_persistent_value(&mut self, name: &str, contents: IoResult<&[u8]>) { self.persistent_values .insert(name.into(), contents.map(|b| b.into())); } @@ -166,7 +163,7 @@ impl Ops for TestOps { partition: &CStr, offset: i64, buffer: &mut [u8], - ) -> Result { + ) -> IoResult { let partition = self .partitions .get(partition.to_str()?) @@ -205,7 +202,7 @@ impl Ops for TestOps { Ok(bytes_read) } - fn get_preloaded_partition(&mut self, partition: &CStr) -> Result<&[u8]> { + fn get_preloaded_partition(&mut self, partition: &CStr) -> IoResult<&[u8]> { match self.partitions.get(partition.to_str()?) { Some(FakePartition { contents, @@ -220,7 +217,7 @@ impl Ops for TestOps { &mut self, public_key: &[u8], public_key_metadata: Option<&[u8]>, - ) -> Result { + ) -> IoResult { self.vbmeta_keys // The compiler can't match (&[u8], Option<&[u8]>) to keys of type // (Vec, Option>) so we turn the &[u8] into vectors here. This is a bit @@ -230,35 +227,35 @@ impl Ops for TestOps { .map(|k| k.info.trusted) } - fn read_rollback_index(&mut self, location: usize) -> Result { + fn read_rollback_index(&mut self, location: usize) -> IoResult { self.rollbacks.get(&location).ok_or(IoError::Io).copied() } - fn write_rollback_index(&mut self, location: usize, index: u64) -> Result<()> { + fn write_rollback_index(&mut self, location: usize, index: u64) -> IoResult<()> { *(self.rollbacks.get_mut(&location).ok_or(IoError::Io)?) = index; Ok(()) } - fn read_is_device_unlocked(&mut self) -> Result { + fn read_is_device_unlocked(&mut self) -> IoResult { self.unlock_state.clone() } #[cfg(feature = "uuid")] - fn get_unique_guid_for_partition(&mut self, partition: &CStr) -> Result { + fn get_unique_guid_for_partition(&mut self, partition: &CStr) -> IoResult { self.partitions .get(partition.to_str()?) .map(|p| p.uuid) .ok_or(IoError::NoSuchPartition) } - fn get_size_of_partition(&mut self, partition: &CStr) -> Result { + fn get_size_of_partition(&mut self, partition: &CStr) -> IoResult { self.partitions .get(partition.to_str()?) .map(|p| u64::try_from(p.contents.len()).unwrap()) .ok_or(IoError::NoSuchPartition) } - fn read_persistent_value(&mut self, name: &CStr, value: &mut [u8]) -> Result { + fn read_persistent_value(&mut self, name: &CStr, value: &mut [u8]) -> IoResult { match self .persistent_values .get(name.to_str()?) @@ -276,7 +273,7 @@ impl Ops for TestOps { } } - fn write_persistent_value(&mut self, name: &CStr, value: &[u8]) -> Result<()> { + fn write_persistent_value(&mut self, name: &CStr, value: &[u8]) -> IoResult<()> { let name = name.to_str()?; // If the test requested a simulated error on this value, return it. @@ -289,7 +286,7 @@ impl Ops for TestOps { Ok(()) } - fn erase_persistent_value(&mut self, name: &CStr) -> Result<()> { + fn erase_persistent_value(&mut self, name: &CStr) -> IoResult<()> { let name = name.to_str()?; // If the test requested a simulated error on this value, return it. @@ -306,7 +303,7 @@ impl Ops for TestOps { partition: &CStr, public_key: &[u8], public_key_metadata: Option<&[u8]>, - ) -> Result { + ) -> IoResult { let key = self .vbmeta_keys .get(&(public_key.to_vec(), public_key_metadata.map(|m| m.to_vec()))) diff --git a/rust/tests/verify_tests.rs b/rust/tests/verify_tests.rs index 416e0ec..13d4912 100644 --- a/rust/tests/verify_tests.rs +++ b/rust/tests/verify_tests.rs @@ -17,6 +17,7 @@ use crate::test_ops::TestOps; use avb::{ slot_verify, HashtreeErrorMode, IoError, SlotVerifyData, SlotVerifyError, SlotVerifyFlags, + SlotVerifyResult, }; use std::{ffi::CString, fs}; #[cfg(feature = "uuid")] @@ -50,9 +51,7 @@ fn test_ops_one_image_one_vbmeta() -> TestOps { } /// Calls `slot_verify()` using standard args for `test_ops_one_image_one_vbmeta()` setup. -fn verify_one_image_one_vbmeta<'a>( - ops: &'a mut TestOps, -) -> Result, SlotVerifyError<'a>> { +fn verify_one_image_one_vbmeta(ops: &mut TestOps) -> SlotVerifyResult { slot_verify( ops, &[&CString::new(TEST_PARTITION_NAME).unwrap()], @@ -74,9 +73,7 @@ fn test_ops_two_images_one_vbmeta() -> TestOps { } /// Calls `slot_verify()` using standard args for `test_ops_two_images_one_vbmeta()` setup. -fn verify_two_images_one_vbmeta<'a>( - ops: &'a mut TestOps, -) -> Result, SlotVerifyError<'a>> { +fn verify_two_images_one_vbmeta(ops: &mut TestOps) -> SlotVerifyResult { slot_verify( ops, &[ @@ -102,9 +99,7 @@ fn test_ops_boot_partition() -> TestOps { } /// Calls `slot_verify()` using standard args for `test_ops_boot_partition()` setup. -fn verify_boot_partition<'a>( - ops: &'a mut TestOps, -) -> Result, SlotVerifyError<'a>> { +fn verify_boot_partition(ops: &mut TestOps) -> SlotVerifyResult { slot_verify( ops, &[&CString::new("boot").unwrap()], @@ -133,9 +128,7 @@ fn test_ops_persistent_digest(image: Vec) -> TestOps { } /// Calls `slot_verify()` using standard args for `test_ops_persistent_digest()` setup. -fn verify_persistent_digest<'a>( - ops: &'a mut TestOps, -) -> Result, SlotVerifyError<'a>> { +fn verify_persistent_digest(ops: &mut TestOps) -> SlotVerifyResult { slot_verify( ops, &[&CString::new(TEST_PARTITION_PERSISTENT_DIGEST_NAME).unwrap()], -- cgit v1.2.3