Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor common runtime tests so new parameters to run_rt_test do not require updating every test using it #1776

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 13 additions & 15 deletions runtime/tests/runtime_integration_tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,22 @@ pub const TEST_DIGEST: [u8; 48] = [
pub const DEFAULT_FMC_VERSION: u16 = 0xaaaa;
pub const DEFAULT_APP_VERSION: u32 = 0xbbbbbbbb;

pub fn run_rt_test_lms(
test_fwid: Option<&'static FwId>,
test_image_options: Option<ImageOptions>,
init_params: Option<InitParams>,
lms_verify: bool,
) -> DefaultHwModel {
#[derive(Default)]
pub struct RuntimeTestArgs<'a> {
pub test_fwid: Option<&'static FwId<'static>>,
pub test_image_options: Option<ImageOptions>,
pub init_params: Option<InitParams<'a>>,
}

pub fn run_rt_test_lms(args: RuntimeTestArgs, lms_verify: bool) -> DefaultHwModel {
let default_rt_fwid = if cfg!(feature = "fpga_realtime") {
&APP_WITH_UART_FPGA
} else {
&APP_WITH_UART
};
let runtime_fwid = test_fwid.unwrap_or(default_rt_fwid);
let runtime_fwid = args.test_fwid.unwrap_or(default_rt_fwid);

let image_options = test_image_options.unwrap_or_else(|| {
let image_options = args.test_image_options.unwrap_or_else(|| {
let mut opts = ImageOptions::default();
opts.vendor_config.pl0_pauser = Some(0x1);
opts.fmc_version = DEFAULT_FMC_VERSION;
Expand All @@ -62,7 +64,7 @@ pub fn run_rt_test_lms(
});

let rom = caliptra_builder::rom_for_fw_integration_tests().unwrap();
let init_params = match init_params {
let init_params = match args.init_params {
Some(init_params) => init_params,
None => InitParams {
rom: &rom,
Expand Down Expand Up @@ -93,12 +95,8 @@ pub fn run_rt_test_lms(

// Run a test which boots ROM -> FMC -> test_bin. If test_bin_name is None,
// run the production runtime image.
pub fn run_rt_test(
test_fwid: Option<&'static FwId>,
test_image_options: Option<ImageOptions>,
init_params: Option<InitParams>,
) -> DefaultHwModel {
run_rt_test_lms(test_fwid, test_image_options, init_params, false)
pub fn run_rt_test(args: RuntimeTestArgs) -> DefaultHwModel {
run_rt_test_lms(args, false)
}

pub fn generate_test_x509_cert(ec_key: PKey<Private>) -> X509 {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Licensed under the Apache-2.0 license

use crate::common::run_rt_test;
use crate::common::{run_rt_test, RuntimeTestArgs};
use caliptra_api::SocManager;
use caliptra_common::mailbox_api::{
AuthorizeAndStashReq, AuthorizeAndStashResp, CommandId, ImageHashSource, MailboxReq,
Expand All @@ -13,7 +13,7 @@ use zerocopy::FromBytes;

#[test]
fn test_authorize_and_stash_cmd_deny_authorization() {
let mut model = run_rt_test(None, None, None);
let mut model = run_rt_test(RuntimeTestArgs::default());

model.step_until(|m| {
m.soc_ifc().cptra_boot_status().read() == u32::from(RtBootStatus::RtReadyForCommands)
Expand Down
28 changes: 20 additions & 8 deletions runtime/tests/runtime_integration_tests/test_boot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use caliptra_runtime::RtBootStatus;
use sha2::{Digest, Sha384};
use zerocopy::AsBytes;

use crate::common::{run_rt_test, DEFAULT_APP_VERSION, DEFAULT_FMC_VERSION};
use crate::common::{run_rt_test, RuntimeTestArgs, DEFAULT_APP_VERSION, DEFAULT_FMC_VERSION};

const RT_READY_FOR_COMMANDS: u32 = 0x600;

Expand All @@ -22,14 +22,18 @@ fn test_standard() {
// Test that the normal runtime firmware boots.
// Ultimately, this will be useful for exercising Caliptra end-to-end
// via the mailbox.
let mut model = run_rt_test(None, None, None);
let mut model = run_rt_test(RuntimeTestArgs::default());

model.step_until_boot_status(RT_READY_FOR_COMMANDS, true);
}

#[test]
fn test_boot() {
let mut model = run_rt_test(Some(&firmware::runtime_tests::BOOT), None, None);
let args = RuntimeTestArgs {
test_fwid: Some(&firmware::runtime_tests::BOOT),
..Default::default()
};
let mut model = run_rt_test(args);

model.step_until_exit_success().unwrap();
}
Expand All @@ -38,14 +42,18 @@ fn test_boot() {
/// This test differs from the drivers' test_persistent() in that it is ran with the "runtime" flag so
/// it allows us to test conditionally compiled runtime-only persistent data that ROM/FMC may have corrupted.
fn test_persistent_data() {
let mut model = run_rt_test(Some(&firmware::runtime_tests::PERSISTENT_RT), None, None);
let args = RuntimeTestArgs {
test_fwid: Some(&firmware::runtime_tests::PERSISTENT_RT),
..Default::default()
};
let mut model = run_rt_test(args);

model.step_until_exit_success().unwrap();
}

#[test]
fn test_fw_version() {
let mut model = run_rt_test(None, None, None);
let mut model = run_rt_test(RuntimeTestArgs::default());
model.step_until(|m| {
m.soc_ifc().cptra_boot_status().read() == u32::from(RtBootStatus::RtReadyForCommands)
});
Expand Down Expand Up @@ -78,7 +86,7 @@ fn test_update() {
// Test that the normal runtime firmware boots.
// Ultimately, this will be useful for exercising Caliptra end-to-end
// via the mailbox.
let mut model = run_rt_test(None, None, None);
let mut model = run_rt_test(RuntimeTestArgs::default());

model.step_until(|m| m.soc_mbox().status().read().mbox_fsm_ps().mbox_idle());

Expand Down Expand Up @@ -117,7 +125,7 @@ fn test_stress_update() {
.unwrap(),
];

let mut model = run_rt_test(None, None, None);
let mut model = run_rt_test(RuntimeTestArgs::default());

let stress_num = if cfg!(feature = "slow_tests") { 500 } else { 1 };
let mut image_select = 0;
Expand Down Expand Up @@ -146,7 +154,11 @@ fn test_stress_update() {

#[test]
fn test_boot_tci_data() {
let mut model = run_rt_test(Some(&firmware::runtime_tests::MBOX), None, None);
let args = RuntimeTestArgs {
test_fwid: Some(&firmware::runtime_tests::MBOX),
..Default::default()
};
let mut model = run_rt_test(args);

let rt_journey_pcr_resp = model.mailbox_execute(0x1000_0000, &[]).unwrap().unwrap();
let rt_journey_pcr: [u8; 48] = rt_journey_pcr_resp.as_bytes().try_into().unwrap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ use x509_parser::{
};
use zerocopy::{AsBytes, FromBytes};

use crate::common::{assert_error, run_rt_test, TEST_LABEL};
use crate::common::{assert_error, run_rt_test, RuntimeTestArgs, TEST_LABEL};

#[test]
fn test_dmtf_other_name_validation_fail() {
let mut model = run_rt_test(None, None, None);
let mut model = run_rt_test(RuntimeTestArgs::default());

model.step_until(|m| {
m.soc_ifc().cptra_boot_status().read() == u32::from(RtBootStatus::RtReadyForCommands)
Expand Down Expand Up @@ -54,7 +54,7 @@ fn test_dmtf_other_name_validation_fail() {

#[test]
fn test_dmtf_other_name_extension_present() {
let mut model = run_rt_test(None, None, None);
let mut model = run_rt_test(RuntimeTestArgs::default());

model.step_until(|m| {
m.soc_ifc().cptra_boot_status().read() == u32::from(RtBootStatus::RtReadyForCommands)
Expand Down Expand Up @@ -123,7 +123,7 @@ fn test_dmtf_other_name_extension_present() {

#[test]
fn test_dmtf_other_name_extension_not_present() {
let mut model = run_rt_test(None, None, None);
let mut model = run_rt_test(RuntimeTestArgs::default());

model.step_until(|m| {
m.soc_ifc().cptra_boot_status().read() == u32::from(RtBootStatus::RtReadyForCommands)
Expand Down
20 changes: 12 additions & 8 deletions runtime/tests/runtime_integration_tests/test_certs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use crate::common::{
execute_dpe_cmd, generate_test_x509_cert, get_fmc_alias_cert, get_rt_alias_cert, run_rt_test,
DpeResult, TEST_LABEL,
DpeResult, RuntimeTestArgs, TEST_LABEL,
};
use caliptra_builder::firmware::{APP_WITH_UART, FMC_WITH_UART};
use caliptra_builder::ImageOptions;
Expand Down Expand Up @@ -58,7 +58,11 @@ fn test_rt_cert_with_custom_dates() {

opts.owner_config = Some(own_config);

let mut model = run_rt_test(None, Some(opts), None);
let args = RuntimeTestArgs {
test_image_options: Some(opts),
..Default::default()
};
let mut model = run_rt_test(args);

let payload = MailboxReqHeader {
chksum: caliptra_common::checksum::calc_checksum(
Expand All @@ -85,7 +89,7 @@ fn test_rt_cert_with_custom_dates() {

#[test]
fn test_idev_id_cert() {
let mut model = run_rt_test(None, None, None);
let mut model = run_rt_test(RuntimeTestArgs::default());

// generate 48 byte ECDSA key pair
let ec_group = EcGroup::from_curve_name(Nid::SECP384R1).unwrap();
Expand Down Expand Up @@ -172,7 +176,7 @@ fn get_ldev_cert(model: &mut DefaultHwModel) -> GetLdevCertResp {

#[test]
fn test_ldev_cert() {
let mut model = run_rt_test(None, None, None);
let mut model = run_rt_test(RuntimeTestArgs::default());

let ldev_resp = get_ldev_cert(&mut model);
let ldev_cert: X509 = X509::from_der(&ldev_resp.data[..ldev_resp.data_size as usize]).unwrap();
Expand Down Expand Up @@ -200,7 +204,7 @@ fn test_ldev_cert() {

#[test]
fn test_fmc_alias_cert() {
let mut model = run_rt_test(None, None, None);
let mut model = run_rt_test(RuntimeTestArgs::default());

let ldev_resp = get_ldev_cert(&mut model);
let ldev_cert: X509 = X509::from_der(&ldev_resp.data[..ldev_resp.data_size as usize]).unwrap();
Expand All @@ -221,7 +225,7 @@ fn test_fmc_alias_cert() {

#[test]
fn test_rt_alias_cert() {
let mut model = run_rt_test(None, None, None);
let mut model = run_rt_test(RuntimeTestArgs::default());

let fmc_resp = get_fmc_alias_cert(&mut model);
let fmc_cert: X509 = X509::from_der(&fmc_resp.data[..fmc_resp.data_size as usize]).unwrap();
Expand All @@ -242,7 +246,7 @@ fn test_rt_alias_cert() {

#[test]
fn test_dpe_leaf_cert() {
let mut model = run_rt_test(None, None, None);
let mut model = run_rt_test(RuntimeTestArgs::default());

let rt_resp = get_rt_alias_cert(&mut model);
let rt_cert: X509 = X509::from_der(&rt_resp.data[..rt_resp.data_size as usize]).unwrap();
Expand Down Expand Up @@ -279,7 +283,7 @@ fn test_dpe_leaf_cert() {

#[test]
fn test_full_cert_chain() {
let mut model = run_rt_test(None, None, None);
let mut model = run_rt_test(RuntimeTestArgs::default());

let ldev_resp = get_ldev_cert(&mut model);
let ldev_cert: X509 = X509::from_der(&ldev_resp.data[..ldev_resp.data_size as usize]).unwrap();
Expand Down
7 changes: 4 additions & 3 deletions runtime/tests/runtime_integration_tests/test_disable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ use openssl::{
use zerocopy::{AsBytes, FromBytes};

use crate::common::{
execute_dpe_cmd, get_rt_alias_cert, run_rt_test, DpeResult, TEST_DIGEST, TEST_LABEL,
execute_dpe_cmd, get_rt_alias_cert, run_rt_test, DpeResult, RuntimeTestArgs, TEST_DIGEST,
TEST_LABEL,
};

#[test]
fn test_disable_attestation_cmd() {
let mut model = run_rt_test(None, None, None);
let mut model = run_rt_test(RuntimeTestArgs::default());

// sign the digest
let sign_cmd = SignCmd {
Expand Down Expand Up @@ -92,7 +93,7 @@ fn test_disable_attestation_cmd() {

#[test]
fn test_attestation_disabled_flag_after_update_reset() {
let mut model = run_rt_test(None, None, None);
let mut model = run_rt_test(RuntimeTestArgs::default());

// disable attestation
let payload = MailboxReqHeader {
Expand Down
10 changes: 5 additions & 5 deletions runtime/tests/runtime_integration_tests/test_ecdsa.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Licensed under the Apache-2.0 license.

use crate::common::{assert_error, run_rt_test};
use crate::common::{assert_error, run_rt_test, RuntimeTestArgs};
use caliptra_api::SocManager;
use caliptra_common::mailbox_api::{
CommandId, EcdsaVerifyReq, MailboxReq, MailboxReqHeader, MailboxRespHeader,
Expand All @@ -19,7 +19,7 @@ fn ecdsa_cmd_run_wycheproof() {
// This test is too slow to run as part of the verilator nightly.
#![cfg_attr(all(not(feature = "slow_tests"), feature = "verilator"), ignore)]

let mut model = run_rt_test(None, None, None);
let mut model = run_rt_test(RuntimeTestArgs::default());

model.step_until(|m| {
m.soc_ifc().cptra_boot_status().read()
Expand Down Expand Up @@ -149,7 +149,7 @@ fn ecdsa_cmd_run_wycheproof() {

#[test]
fn test_ecdsa_verify_cmd() {
let mut model = run_rt_test(None, None, None);
let mut model = run_rt_test(RuntimeTestArgs::default());

model.step_until(|m| {
m.soc_ifc().cptra_boot_status().read() == u32::from(RtBootStatus::RtReadyForCommands)
Expand Down Expand Up @@ -227,7 +227,7 @@ fn test_ecdsa_verify_cmd() {

#[test]
fn test_ecdsa_verify_bad_chksum() {
let mut model = run_rt_test(None, None, None);
let mut model = run_rt_test(RuntimeTestArgs::default());

let cmd = MailboxReq::EcdsaVerify(EcdsaVerifyReq {
hdr: MailboxReqHeader { chksum: 0 },
Expand All @@ -254,7 +254,7 @@ fn test_ecdsa_verify_bad_chksum() {
#[cfg(any(feature = "verilator", feature = "fpga_realtime"))]
#[test]
fn test_ecdsa_hw_failure() {
let mut model = run_rt_test(None, None, None);
let mut model = run_rt_test(RuntimeTestArgs::default());

let mut cmd = MailboxReq::EcdsaVerify(EcdsaVerifyReq {
hdr: MailboxReqHeader { chksum: 0 },
Expand Down
14 changes: 7 additions & 7 deletions runtime/tests/runtime_integration_tests/test_fips.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Licensed under the Apache-2.0 license.

use crate::common::{assert_error, run_rt_test};
use crate::common::{assert_error, run_rt_test, RuntimeTestArgs};
use caliptra_api::SocManager;
use caliptra_builder::{version, ImageOptions};
use caliptra_common::mailbox_api::{
Expand All @@ -14,15 +14,15 @@ const HW_REV_ID: u32 = if cfg!(feature = "hw-1.0") { 0x1 } else { 0x11 };

#[test]
fn test_fips_version() {
let mut model = run_rt_test(
None,
Some(ImageOptions {
let args = RuntimeTestArgs {
test_image_options: Some(ImageOptions {
fmc_version: version::get_fmc_version(),
app_version: version::get_runtime_version(),
..Default::default()
}),
None,
);
..Default::default()
};
let mut model = run_rt_test(args);

model.step_until(|m| m.soc_mbox().status().read().mbox_fsm_ps().mbox_idle());

Expand Down Expand Up @@ -72,7 +72,7 @@ fn test_fips_version() {

#[test]
fn test_fips_shutdown() {
let mut model = run_rt_test(None, None, None);
let mut model = run_rt_test(RuntimeTestArgs::default());

model.step_until(|m| m.soc_mbox().status().read().mbox_fsm_ps().mbox_idle());

Expand Down
6 changes: 3 additions & 3 deletions runtime/tests/runtime_integration_tests/test_info.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Licensed under the Apache-2.0 license.

use crate::common::run_rt_test;
use crate::common::{run_rt_test, RuntimeTestArgs};
use caliptra_builder::{
firmware::{APP_WITH_UART, FMC_WITH_UART},
ImageOptions,
Expand Down Expand Up @@ -159,7 +159,7 @@ fn test_fw_info() {

#[test]
fn test_idev_id_info() {
let mut model = run_rt_test(None, None, None);
let mut model = run_rt_test(RuntimeTestArgs::default());
let payload = MailboxReqHeader {
chksum: caliptra_common::checksum::calc_checksum(u32::from(CommandId::GET_IDEV_INFO), &[]),
};
Expand All @@ -172,7 +172,7 @@ fn test_idev_id_info() {

#[test]
fn test_capabilities() {
let mut model = run_rt_test(None, None, None);
let mut model = run_rt_test(RuntimeTestArgs::default());
let payload = MailboxReqHeader {
chksum: caliptra_common::checksum::calc_checksum(u32::from(CommandId::CAPABILITIES), &[]),
};
Expand Down
Loading
Loading