From e93df859e81ae1f87f17ab16b5c4dcbb225c1070 Mon Sep 17 00:00:00 2001 From: "Sun, Ceping" Date: Thu, 27 Jul 2023 17:41:12 +0530 Subject: [PATCH] Add unit test in src/spdm Signed-off-by: Ceping Sun --- src/spdm/src/crypto_callback.rs | 43 +++++++++++++++++++++++++ src/spdm/src/vtpm_io_transport.rs | 23 ++++++++++++++ src/spdm/src/vtpm_transport_encap.rs | 47 ++++++++++++++++++++++++++++ 3 files changed, 113 insertions(+) diff --git a/src/spdm/src/crypto_callback.rs b/src/spdm/src/crypto_callback.rs index 939f09b..d4363b6 100644 --- a/src/spdm/src/crypto_callback.rs +++ b/src/spdm/src/crypto_callback.rs @@ -62,3 +62,46 @@ fn sign_ecdsa_asym_algo( data: full_signature, }) } + +#[cfg(test)] +mod test { + + use super::*; + use ring::rand::SystemRandom; + use ring::signature::{self, EcdsaKeyPair}; + #[test] + fn test_asym_sign() { + let mut data = [2u8;0x100]; + let base_hash_algo = SpdmBaseHashAlgo::TPM_ALG_SHA_384; + let base_asym_algo = SpdmBaseAsymAlgo::TPM_ALG_ECDSA_ECC_NIST_P384; + GLOBAL_SPDM_DATA.lock().valid = true; + let rand = SystemRandom::new(); + let pkcs8_bytes = + EcdsaKeyPair::generate_pkcs8(&signature::ECDSA_P384_SHA384_ASN1_SIGNING, &rand) + .map_err(|_| 0); + + assert_eq!(pkcs8_bytes.is_err(), false); + let pkcs8 = pkcs8_bytes.unwrap(); + let res = GLOBAL_SPDM_DATA.lock().set_pkcs8(pkcs8.as_ref()); + assert_eq!(res.is_err(), false); + + let res = asym_sign(base_hash_algo, base_asym_algo, &mut data); + assert_eq!(res.is_none(), false); + } + + #[test] + #[should_panic] + fn test_sign_ecdsa_asym_algo_other_algo(){ + let mut data = [2u8;96]; + sign_ecdsa_asym_algo(&ring::signature::ECDSA_P256_SHA256_FIXED_SIGNING, &mut data); + } + + #[test] + #[should_panic] + fn test_asym_sign_other_algo(){ + let mut data = [2u8;0x100]; + let base_hash_algo = SpdmBaseHashAlgo::TPM_ALG_SHA_256; + let base_asym_algo = SpdmBaseAsymAlgo::TPM_ALG_ECDSA_ECC_NIST_P256; + asym_sign(base_hash_algo, base_asym_algo, &mut data); + } +} diff --git a/src/spdm/src/vtpm_io_transport.rs b/src/spdm/src/vtpm_io_transport.rs index c564934..86b729d 100644 --- a/src/spdm/src/vtpm_io_transport.rs +++ b/src/spdm/src/vtpm_io_transport.rs @@ -124,3 +124,26 @@ impl SpdmDeviceIo for VtpmIoTransport { todo!() } } + +#[cfg(test)] +mod test { + use super::*; + + #[test] + #[should_panic] + fn test_vtpmio_transport_send() { + let mut vtpmio = VtpmIoTransport::new(101); + let buffer =[1u8;100]; + let res = vtpmio.send(&buffer); + assert!(res.is_err()); + } + + #[test] + #[should_panic] + fn test_vtpmio_transport_recive() { + let mut vtpmio = VtpmIoTransport::new(101); + let mut buffer =[1u8;100]; + let res = vtpmio.receive(&mut buffer, 0); + assert!(res.is_err()); + } +} diff --git a/src/spdm/src/vtpm_transport_encap.rs b/src/spdm/src/vtpm_transport_encap.rs index 329262f..949ba03 100644 --- a/src/spdm/src/vtpm_transport_encap.rs +++ b/src/spdm/src/vtpm_transport_encap.rs @@ -223,3 +223,50 @@ impl SpdmTransportEncap for VtpmTransportEncap { VTPM_MAX_RANDOME_DATA_COUNT } } + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn test_vtpmtransport_appmessage_header() { + let appmh = VtpmTransportAppMessageHeader { + message_type:VtpmTransportAppMessageType::VtpmTransportAppMessageTypeSpdm, + }; + let bytes = [1u8;10]; + let mut reader = Reader::init(&bytes); + let res = VtpmTransportAppMessageHeader::read(&mut reader); + print!("res is {:?}\n",res); + } + + + #[test] + fn test_encap(){ + let mut vtpm_encap = VtpmTransportEncap::default(); + let spdm_buffer = [1u8;100]; + let mut buffer = [0u8;1024]; + let res = vtpm_encap.encap(&spdm_buffer, &mut buffer, false); + print!("res is {:?}\n",res); + } + + #[test] + #[should_panic] + fn test_encap_invalid_buffer(){ + let mut vtpm_encap = VtpmTransportEncap::default(); + let spdm_buffer = [1u8;100]; + let res = vtpm_encap.encap(&spdm_buffer, &mut [], false); + print!("res is {:?}\n",res); + assert!(res.is_err()); + } + + #[test] + fn test_decap(){ + let mut vtpm_encap = VtpmTransportEncap::default(); + let spdm_buffer = [1u8;100]; + let mut buffer = [0u8;1024]; + let res = vtpm_encap.encap(&spdm_buffer, &mut buffer, false); + print!("res is {:?}\n",res); + } + + +}