Skip to content

Commit

Permalink
Add RT Testing common function to assert error codes
Browse files Browse the repository at this point in the history
  • Loading branch information
sree-revoori1 authored and jhand2 committed Dec 5, 2023
1 parent 85ecfc7 commit a2bf305
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 94 deletions.
19 changes: 18 additions & 1 deletion runtime/tests/runtime_integration_tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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)
}
}
17 changes: 6 additions & 11 deletions runtime/tests/runtime_integration_tests/test_ecdsa.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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,
);
}
18 changes: 10 additions & 8 deletions runtime/tests/runtime_integration_tests/test_fips.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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,
);
}
38 changes: 22 additions & 16 deletions runtime/tests/runtime_integration_tests/test_mailbox.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
Expand All @@ -42,24 +40,32 @@ 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;
let payload = MailboxReqHeader {
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,
);
}
Original file line number Diff line number Diff line change
@@ -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::{
Expand All @@ -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() {
Expand Down Expand Up @@ -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;
}

Expand Down
69 changes: 22 additions & 47 deletions runtime/tests/runtime_integration_tests/test_tagging.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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,
);
}

Expand Down Expand Up @@ -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,
);
}

Expand All @@ -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,
);
}

Expand All @@ -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,
);
}

Expand Down Expand Up @@ -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,
);
}

0 comments on commit a2bf305

Please sign in to comment.