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..96bf7b1 100644 --- a/src/spdm/src/vtpm_transport_encap.rs +++ b/src/spdm/src/vtpm_transport_encap.rs @@ -223,3 +223,81 @@ impl SpdmTransportEncap for VtpmTransportEncap { VTPM_MAX_RANDOME_DATA_COUNT } } + +#[cfg(test)] +mod test { + use super::*; + use core::mem::size_of; + const DATA_SIZE:usize = 0x100; + const TEST_SPDM_BUFFER_SIZE:usize = 0x100; + const TEST_TRANSPORT_BUFFER_SIZE:usize = 0x1024; + const INVALID_TEST_TRANSPORT_BUFFER_SIZE:usize = 0x1000; + + #[test] + fn test_vtpmtransport_appmessage_header() { + let test: VtpmTransportAppMessageHeader = VtpmTransportAppMessageHeader { + message_type:VtpmTransportAppMessageType::VtpmTransportAppMessageTypeSpdm, + }; + let mut bytes = [1u8; DATA_SIZE]; + let mut writer = Writer::init(&mut bytes); + let res = test.encode(&mut writer); + print!("res is {:?}\n",res); + assert_eq!(res.is_err(), false); + let bytes = [1u8;DATA_SIZE]; + let mut reader = Reader::init(&bytes); + let res = VtpmTransportAppMessageHeader::read(&mut reader); + print!("res is {:?}\n",res); + assert_eq!(res.is_none(), false); + } + + #[test] + fn test_vtpmtransport_message_header() { + let test = VtpmTransportMessageHeader { + message_length: 2, + version: 1, + message_type: VtpmTransportMessageType::VtpmTransportMessageTypeSecureSpdm + }; + let mut bytes = [1u8; DATA_SIZE]; + let mut writer = Writer::init(&mut bytes); + let res = test.encode(&mut writer); + print!("res is {:?}\n",res); + assert_eq!(res.is_err(), false); + let bytes = [1u8; DATA_SIZE]; + let mut reader = Reader::init(&bytes); + let res = VtpmTransportMessageType::read(&mut reader); + print!("res is {:?}\n",res); + assert_eq!(res.is_none(), false); + } + + + #[test] + fn test_encap(){ + let mut vtpm_encap = VtpmTransportEncap::default(); + let spdm_buffer = [1u8;TEST_SPDM_BUFFER_SIZE]; + let mut buffer = [0u8;TEST_TRANSPORT_BUFFER_SIZE]; + let res = vtpm_encap.encap(&spdm_buffer, &mut buffer, false); + print!("res is {:?}\n",res); + assert_eq!(res.is_err(), false); + assert_eq!(res.unwrap(),spdm_buffer.len() + size_of::()) + } + + #[test] + fn test_encap_invalid_buffer(){ + let mut vtpm_encap = VtpmTransportEncap::default(); + let spdm_buffer = [1u8;TEST_SPDM_BUFFER_SIZE]; + 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;TEST_SPDM_BUFFER_SIZE]; + let mut buffer = [0u8;TEST_TRANSPORT_BUFFER_SIZE]; + let res = vtpm_encap.encap(&spdm_buffer, &mut buffer, false); + print!("res is {:?}\n",res); + } + + +}