From a2bf3054d8f924879ccd9f91bc9786867bd0d542 Mon Sep 17 00:00:00 2001 From: Sree Revoori Date: Tue, 5 Dec 2023 17:01:53 +0000 Subject: [PATCH] Add RT Testing common function to assert error codes --- .../tests/runtime_integration_tests/common.rs | 19 ++++- .../runtime_integration_tests/test_ecdsa.rs | 17 ++--- .../runtime_integration_tests/test_fips.rs | 18 ++--- .../runtime_integration_tests/test_mailbox.rs | 38 +++++----- .../test_pauser_privilege_levels.rs | 18 ++--- .../runtime_integration_tests/test_tagging.rs | 69 ++++++------------- 6 files changed, 85 insertions(+), 94 deletions(-) diff --git a/runtime/tests/runtime_integration_tests/common.rs b/runtime/tests/runtime_integration_tests/common.rs index ea47416e78..e9812a7ec6 100644 --- a/runtime/tests/runtime_integration_tests/common.rs +++ b/runtime/tests/runtime_integration_tests/common.rs @@ -7,7 +7,8 @@ use caliptra_builder::{ use caliptra_common::mailbox_api::{ CommandId, InvokeDpeReq, InvokeDpeResp, MailboxReq, MailboxReqHeader, }; -use caliptra_hw_model::{BootParams, DefaultHwModel, HwModel, InitParams}; +use caliptra_error::CaliptraError; +use caliptra_hw_model::{BootParams, DefaultHwModel, HwModel, InitParams, ModelError}; use dpe::{ commands::{Command, CommandHdr}, response::{ @@ -173,3 +174,19 @@ pub fn execute_dpe_cmd(model: &mut DefaultHwModel, dpe_cmd: &mut Command) -> Res parse_dpe_response(dpe_cmd, &resp_hdr.data[..resp_hdr.data_size as usize]) } + +pub fn assert_error( + model: &mut DefaultHwModel, + expected_err: CaliptraError, + actual_err: ModelError, +) { + assert_eq!( + model.soc_ifc().cptra_fw_error_non_fatal().read(), + u32::from(expected_err) + ); + if let ModelError::MailboxCmdFailed(code) = actual_err { + assert_eq!(code, u32::from(expected_err)); + } else { + panic!("Mailbox command should have failed with MailboxCmdFailed error, instead failed with {} error", actual_err) + } +} diff --git a/runtime/tests/runtime_integration_tests/test_ecdsa.rs b/runtime/tests/runtime_integration_tests/test_ecdsa.rs index 26f38b95b9..b5dbc229bf 100644 --- a/runtime/tests/runtime_integration_tests/test_ecdsa.rs +++ b/runtime/tests/runtime_integration_tests/test_ecdsa.rs @@ -1,10 +1,10 @@ // Licensed under the Apache-2.0 license. -use crate::common::run_rt_test; +use crate::common::{assert_error, run_rt_test}; use caliptra_common::mailbox_api::{ CommandId, EcdsaVerifyReq, MailboxReq, MailboxReqHeader, MailboxRespHeader, }; -use caliptra_hw_model::{HwModel, ModelError, ShaAccMode}; +use caliptra_hw_model::{HwModel, ShaAccMode}; use caliptra_runtime::RtBootStatus; use zerocopy::{AsBytes, FromBytes, LayoutVerified}; @@ -242,14 +242,9 @@ fn test_ecdsa_verify_bad_chksum() { cmd.as_bytes().unwrap(), ) .unwrap_err(); - if let ModelError::MailboxCmdFailed(code) = resp { - assert_eq!( - code, - u32::from(caliptra_drivers::CaliptraError::RUNTIME_INVALID_CHECKSUM) - ); - } - assert_eq!( - model.soc_ifc().cptra_fw_error_non_fatal().read(), - u32::from(caliptra_drivers::CaliptraError::RUNTIME_INVALID_CHECKSUM) + assert_error( + &mut model, + caliptra_drivers::CaliptraError::RUNTIME_INVALID_CHECKSUM, + resp, ); } diff --git a/runtime/tests/runtime_integration_tests/test_fips.rs b/runtime/tests/runtime_integration_tests/test_fips.rs index 83ebd71572..6d089b8264 100644 --- a/runtime/tests/runtime_integration_tests/test_fips.rs +++ b/runtime/tests/runtime_integration_tests/test_fips.rs @@ -1,11 +1,10 @@ // Licensed under the Apache-2.0 license. -use crate::common::run_rt_test; +use crate::common::{assert_error, run_rt_test}; use caliptra_common::mailbox_api::{ CommandId, FipsVersionResp, MailboxReqHeader, MailboxRespHeader, }; -use caliptra_error::CaliptraError; -use caliptra_hw_model::{HwModel, ModelError}; +use caliptra_hw_model::HwModel; use caliptra_runtime::FipsVersionCmd; use zerocopy::{AsBytes, FromBytes}; @@ -60,12 +59,15 @@ fn test_fips_shutdown() { assert!(resp.is_ok()); // Check we are rejecting additional commands with the shutdown error code. - let expected_err = Err(ModelError::MailboxCmdFailed(u32::from( - CaliptraError::RUNTIME_SHUTDOWN, - ))); let payload = MailboxReqHeader { chksum: caliptra_common::checksum::calc_checksum(u32::from(CommandId::VERSION), &[]), }; - let resp = model.mailbox_execute(u32::from(CommandId::VERSION), payload.as_bytes()); - assert_eq!(resp, expected_err); + let resp = model + .mailbox_execute(u32::from(CommandId::VERSION), payload.as_bytes()) + .unwrap_err(); + assert_error( + &mut model, + caliptra_drivers::CaliptraError::RUNTIME_SHUTDOWN, + resp, + ); } diff --git a/runtime/tests/runtime_integration_tests/test_mailbox.rs b/runtime/tests/runtime_integration_tests/test_mailbox.rs index c444dd34fe..3f56e29f0a 100644 --- a/runtime/tests/runtime_integration_tests/test_mailbox.rs +++ b/runtime/tests/runtime_integration_tests/test_mailbox.rs @@ -1,11 +1,10 @@ // Licensed under the Apache-2.0 license use caliptra_common::mailbox_api::{CommandId, MailboxReqHeader}; -use caliptra_error::CaliptraError; -use caliptra_hw_model::{HwModel, ModelError}; +use caliptra_hw_model::HwModel; use zerocopy::AsBytes; -use crate::common::run_rt_test; +use crate::common::{assert_error, run_rt_test}; /// When a successful command runs after a failed command, ensure the error /// register is cleared. @@ -16,12 +15,11 @@ fn test_error_cleared() { model.step_until(|m| m.soc_mbox().status().read().mbox_fsm_ps().mbox_idle()); // Send invalid command to cause failure - let resp = model.mailbox_execute(0xffffffff, &[]); - assert_eq!( + let resp = model.mailbox_execute(0xffffffff, &[]).unwrap_err(); + assert_error( + &mut model, + caliptra_drivers::CaliptraError::RUNTIME_MAILBOX_INVALID_PARAMS, resp, - Err(ModelError::MailboxCmdFailed( - CaliptraError::RUNTIME_MAILBOX_INVALID_PARAMS.into() - )) ); // Succeed a command to make sure error gets cleared @@ -42,17 +40,19 @@ fn test_unimplemented_cmds() { model.step_until(|m| m.soc_mbox().status().read().mbox_fsm_ps().mbox_idle()); - let expected_err = Err(ModelError::MailboxCmdFailed(u32::from( - CaliptraError::RUNTIME_UNIMPLEMENTED_COMMAND, - ))); - // CAPABILITIES let payload = MailboxReqHeader { chksum: caliptra_common::checksum::calc_checksum(u32::from(CommandId::CAPABILITIES), &[]), }; - let resp = model.mailbox_execute(u32::from(CommandId::CAPABILITIES), payload.as_bytes()); - assert_eq!(resp, expected_err); + let resp = model + .mailbox_execute(u32::from(CommandId::CAPABILITIES), payload.as_bytes()) + .unwrap_err(); + assert_error( + &mut model, + caliptra_drivers::CaliptraError::RUNTIME_UNIMPLEMENTED_COMMAND, + resp, + ); // Send something that is not a valid RT command. const INVALID_CMD: u32 = 0xAABBCCDD; @@ -60,6 +60,12 @@ fn test_unimplemented_cmds() { chksum: caliptra_common::checksum::calc_checksum(INVALID_CMD, &[]), }; - let resp = model.mailbox_execute(INVALID_CMD, payload.as_bytes()); - assert_eq!(resp, expected_err); + let resp = model + .mailbox_execute(INVALID_CMD, payload.as_bytes()) + .unwrap_err(); + assert_error( + &mut model, + caliptra_drivers::CaliptraError::RUNTIME_UNIMPLEMENTED_COMMAND, + resp, + ); } diff --git a/runtime/tests/runtime_integration_tests/test_pauser_privilege_levels.rs b/runtime/tests/runtime_integration_tests/test_pauser_privilege_levels.rs index d8d132f8b9..eef873ff52 100644 --- a/runtime/tests/runtime_integration_tests/test_pauser_privilege_levels.rs +++ b/runtime/tests/runtime_integration_tests/test_pauser_privilege_levels.rs @@ -1,8 +1,7 @@ // Licensed under the Apache-2.0 license use caliptra_common::mailbox_api::{CommandId, InvokeDpeReq, MailboxReq, MailboxReqHeader}; -use caliptra_error::CaliptraError; -use caliptra_hw_model::{HwModel, ModelError}; +use caliptra_hw_model::HwModel; use caliptra_runtime::{InvokeDpeCmd, RtBootStatus}; use dpe::{ commands::{ @@ -14,7 +13,7 @@ use dpe::{ }; use zerocopy::AsBytes; -use crate::common::{execute_dpe_cmd, run_rt_test}; +use crate::common::{assert_error, execute_dpe_cmd, run_rt_test}; #[test] fn test_pl0_derive_child_dpe_context_thresholds() { @@ -73,14 +72,11 @@ fn test_pl0_derive_child_dpe_context_thresholds() { derive_child_mbox_cmd.as_bytes().unwrap(), ) .unwrap_err(); - if let ModelError::MailboxCmdFailed(code) = resp { - assert_eq!( - code, - u32::from(CaliptraError::RUNTIME_PL0_USED_DPE_CONTEXT_THRESHOLD_EXCEEDED) - ); - } else { - panic!("This DeriveChild call should have failed since it would have breached the PL0 non-inactive dpe context threshold.") - } + assert_error( + &mut model, + caliptra_drivers::CaliptraError::RUNTIME_PL0_USED_DPE_CONTEXT_THRESHOLD_EXCEEDED, + resp, + ); break; } diff --git a/runtime/tests/runtime_integration_tests/test_tagging.rs b/runtime/tests/runtime_integration_tests/test_tagging.rs index 8cb8336883..15b5089165 100644 --- a/runtime/tests/runtime_integration_tests/test_tagging.rs +++ b/runtime/tests/runtime_integration_tests/test_tagging.rs @@ -1,10 +1,10 @@ // Licensed under the Apache-2.0 license -use crate::common::{execute_dpe_cmd, run_rt_test}; +use crate::common::{assert_error, execute_dpe_cmd, run_rt_test}; use caliptra_common::mailbox_api::{ CommandId, GetTaggedTciReq, GetTaggedTciResp, MailboxReq, MailboxReqHeader, TagTciReq, }; -use caliptra_hw_model::{HwModel, ModelError}; +use caliptra_hw_model::HwModel; use dpe::{ commands::{Command, DestroyCtxCmd}, context::ContextHandle, @@ -75,15 +75,10 @@ fn test_tagging_a_tagged_context() { let resp = model .mailbox_execute(u32::from(CommandId::DPE_TAG_TCI), cmd.as_bytes().unwrap()) .unwrap_err(); - if let ModelError::MailboxCmdFailed(code) = resp { - assert_eq!( - code, - u32::from(caliptra_drivers::CaliptraError::RUNTIME_CONTEXT_ALREADY_TAGGED) - ); - } - assert_eq!( - model.soc_ifc().cptra_fw_error_non_fatal().read(), - u32::from(caliptra_drivers::CaliptraError::RUNTIME_CONTEXT_ALREADY_TAGGED) + assert_error( + &mut model, + caliptra_drivers::CaliptraError::RUNTIME_CONTEXT_ALREADY_TAGGED, + resp, ); } @@ -113,15 +108,10 @@ fn test_duplicate_tag() { let resp = model .mailbox_execute(u32::from(CommandId::DPE_TAG_TCI), cmd.as_bytes().unwrap()) .unwrap_err(); - if let ModelError::MailboxCmdFailed(code) = resp { - assert_eq!( - code, - u32::from(caliptra_drivers::CaliptraError::RUNTIME_DUPLICATE_TAG) - ); - } - assert_eq!( - model.soc_ifc().cptra_fw_error_non_fatal().read(), - u32::from(caliptra_drivers::CaliptraError::RUNTIME_DUPLICATE_TAG) + assert_error( + &mut model, + caliptra_drivers::CaliptraError::RUNTIME_DUPLICATE_TAG, + resp, ); } @@ -141,15 +131,10 @@ fn test_get_tagged_tci_on_non_existent_tag() { cmd.as_bytes().unwrap(), ) .unwrap_err(); - if let ModelError::MailboxCmdFailed(code) = resp { - assert_eq!( - code, - u32::from(caliptra_drivers::CaliptraError::RUNTIME_TAGGING_FAILURE) - ); - } - assert_eq!( - model.soc_ifc().cptra_fw_error_non_fatal().read(), - u32::from(caliptra_drivers::CaliptraError::RUNTIME_TAGGING_FAILURE) + assert_error( + &mut model, + caliptra_drivers::CaliptraError::RUNTIME_TAGGING_FAILURE, + resp, ); } @@ -167,15 +152,10 @@ fn test_tagging_inactive_context() { let resp = model .mailbox_execute(u32::from(CommandId::DPE_TAG_TCI), cmd.as_bytes().unwrap()) .unwrap_err(); - if let ModelError::MailboxCmdFailed(code) = resp { - assert_eq!( - code, - u32::from(caliptra_drivers::CaliptraError::RUNTIME_TAGGING_FAILURE) - ); - } - assert_eq!( - model.soc_ifc().cptra_fw_error_non_fatal().read(), - u32::from(caliptra_drivers::CaliptraError::RUNTIME_TAGGING_FAILURE) + assert_error( + &mut model, + caliptra_drivers::CaliptraError::RUNTIME_TAGGING_FAILURE, + resp, ); } @@ -216,14 +196,9 @@ fn test_tagging_destroyed_context() { cmd.as_bytes().unwrap(), ) .unwrap_err(); - if let ModelError::MailboxCmdFailed(code) = resp { - assert_eq!( - code, - u32::from(caliptra_drivers::CaliptraError::RUNTIME_TAGGING_FAILURE) - ); - } - assert_eq!( - model.soc_ifc().cptra_fw_error_non_fatal().read(), - u32::from(caliptra_drivers::CaliptraError::RUNTIME_TAGGING_FAILURE) + assert_error( + &mut model, + caliptra_drivers::CaliptraError::RUNTIME_TAGGING_FAILURE, + resp, ); }