Skip to content

Commit

Permalink
Add unit test in src/spdm
Browse files Browse the repository at this point in the history
Signed-off-by: Ceping Sun <cepingx.sun@intel.com>
  • Loading branch information
sunceping committed Sep 7, 2023
1 parent 45bb060 commit e93df85
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/spdm/src/crypto_callback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
23 changes: 23 additions & 0 deletions src/spdm/src/vtpm_io_transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
47 changes: 47 additions & 0 deletions src/spdm/src/vtpm_transport_encap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


}

0 comments on commit e93df85

Please sign in to comment.