From efdadfcdb9378a98f80092b1530e1e7e8a9b50a7 Mon Sep 17 00:00:00 2001 From: Jakob Naucke Date: Wed, 15 Jan 2025 16:39:13 +0100 Subject: [PATCH] rust/pv*: Unit test flag parsing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add a unit test for flag parsing. In order to ease this test, add derive Eq to ControlFlags and Msb0Flags64. For the same test, add derive Default to CreateBootImageArgs and the structs used in it. The latter can be limited to only derive when testing to avoid confusion with any user-level default. Acked-by: Steffen Eiden Reviewed-by: Marc Hartmayer Signed-off-by: Jakob Naucke Signed-off-by: Jan Höppner --- rust/pv_core/src/utils.rs | 2 +- rust/pvimg/src/cli.rs | 4 ++++ rust/pvimg/src/cmd/create.rs | 22 ++++++++++++++++++++++ rust/pvimg/src/pv_utils/se_hdr/flags.rs | 2 +- 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/rust/pv_core/src/utils.rs b/rust/pv_core/src/utils.rs index 04aeb80d..099106ac 100644 --- a/rust/pv_core/src/utils.rs +++ b/rust/pv_core/src/utils.rs @@ -34,7 +34,7 @@ pub trait Flags: From + for<'a> From<&'a T> { /// /// Wraps an u64 to set/get individual bits #[repr(C)] -#[derive(Debug, Clone, Copy, Default, AsBytes, FromZeroes, FromBytes)] +#[derive(Debug, Clone, Copy, Default, AsBytes, FromZeroes, FromBytes, Eq, PartialEq)] pub struct Msb0Flags64(U64); impl Flags for Msb0Flags64 { #[track_caller] diff --git a/rust/pvimg/src/cli.rs b/rust/pvimg/src/cli.rs index f5a8c308..1431acba 100644 --- a/rust/pvimg/src/cli.rs +++ b/rust/pvimg/src/cli.rs @@ -72,6 +72,7 @@ pub fn validate_cli(opts: &CliOptions) -> Result<(), clap::error::Error> { /// CLI Argument collection for handling input components. #[derive(Args, Debug)] +#[cfg_attr(test, derive(Default))] pub struct ComponentPaths { /// Use the content of FILE as a raw binary Linux kernel. /// @@ -93,6 +94,7 @@ pub struct ComponentPaths { } #[derive(Args, Debug)] +#[cfg_attr(test, derive(Default))] #[command(group(ArgGroup::new("header-flags").multiple(true).conflicts_with_all(["x_pcf", "x_scf"])))] pub struct CreateBootImageLegacyFlags { /// Enable Secure Execution guest dump support. This option requires the @@ -306,6 +308,7 @@ impl GenprotimgCliOptions { } #[derive(Parser, Debug)] +#[cfg_attr(test, derive(Default))] pub struct CreateBootImageArgs { #[clap(flatten)] pub component_paths: ComponentPaths, @@ -352,6 +355,7 @@ pub struct CreateBootImageArgs { /// Experimental options #[derive(Args, Debug)] +#[cfg_attr(test, derive(Default))] pub struct CreateBootImageExperimentalArgs { /// Manually set the directory used to load the Secure Execution bootloaders /// (stage3a and stage3b) (experimental option). diff --git a/rust/pvimg/src/cmd/create.rs b/rust/pvimg/src/cmd/create.rs index ceeb90c2..1c56577f 100644 --- a/rust/pvimg/src/cmd/create.rs +++ b/rust/pvimg/src/cmd/create.rs @@ -213,3 +213,25 @@ pub fn create(opt: &CreateBootImageArgs) -> Result { warn!("Successfully generated the Secure Execution image."); Ok(OwnExitCode::Success) } + +#[cfg(test)] +mod test { + use super::*; + use crate::cli::CreateBootImageLegacyFlags; + + #[test] + fn parse_flags() { + let args = CreateBootImageArgs { + legacy_flags: CreateBootImageLegacyFlags { + enable_dump: Some(true), + ..Default::default() + }, + ..Default::default() + }; + let parsed_flags = super::parse_flags(&args).expect("Failed to parse flags {args:?}"); + let mut exp_flags = Vec::from(PlaintextControlFlagsV1::PCKMO); + exp_flags.push(PcfV1::AllowDumping); + let pcf = PlaintextControlFlagsV1::from_flags(PcfV1::all_enabled(exp_flags)); + assert_eq!(parsed_flags.0, pcf); + } +} diff --git a/rust/pvimg/src/pv_utils/se_hdr/flags.rs b/rust/pvimg/src/pv_utils/se_hdr/flags.rs index fdb6b5b8..76f10a07 100644 --- a/rust/pvimg/src/pv_utils/se_hdr/flags.rs +++ b/rust/pvimg/src/pv_utils/se_hdr/flags.rs @@ -85,7 +85,7 @@ pub trait ControlFlagsTrait: Display { /// Bitflags as used by the Secure Execution in MSB0 ordering /// /// Wraps an u64 to set/get individual bits -#[derive(Clone, Copy, Debug)] +#[derive(Clone, Copy, Debug, Eq, PartialEq)] pub struct ControlFlags { flags: Msb0Flags64, t: PhantomData,