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 11, 2023
1 parent 45bb060 commit f0ef733
Show file tree
Hide file tree
Showing 3 changed files with 241 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);
}
}
37 changes: 37 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,40 @@ 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());
}

#[test]
fn test_flush_all() {
let mut vtpmio = VtpmIoTransport::new(101);
let res = vtpmio.flush_all();
assert!(res.is_ok());
}

#[test]
#[should_panic]
fn test_as_any() {
let mut vtpmio = VtpmIoTransport::new(101);
let res = vtpmio.as_any();
}
}
161 changes: 161 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,164 @@ impl SpdmTransportEncap for VtpmTransportEncap {
VTPM_MAX_RANDOME_DATA_COUNT
}
}

#[cfg(test)]
mod test {
use super::*;
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 = 0x10;
const VTPM_MESSAGE_HEADER_SIZE: usize = 4;
const VTPM_APP_MESSAGE_HEADER_SIZE: usize = 1;

#[test]
fn test_get_sequence_number_count() {
let mut vtpmtransport: VtpmTransportEncap = VtpmTransportEncap::default();
assert_eq!(
vtpmtransport.get_sequence_number_count(),
VTPM_SEQUENCE_NUM_COUNT
);
}

#[test]
fn test_get_max_random_count() {
let mut vtpmtransport: VtpmTransportEncap = VtpmTransportEncap::default();
assert_eq!(
vtpmtransport.get_max_random_count(),
VTPM_MAX_RANDOME_DATA_COUNT
);
}

#[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);
assert_eq!(res.is_err(), false);
let bytes = [1u8; DATA_SIZE];
let mut reader = Reader::init(&bytes);
let res = VtpmTransportAppMessageHeader::read(&mut reader);
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);
assert_eq!(res.is_err(), false);
let bytes = [1u8; DATA_SIZE];
let mut reader = Reader::init(&bytes);
let res = VtpmTransportMessageType::read(&mut reader);
assert_eq!(res.is_none(), false);
let mut reader = Reader::init(&[]);
let res = VtpmTransportMessageType::read(&mut reader);
assert_eq!(res.is_none(), true);
}

#[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);
assert_eq!(res.is_err(), false);
assert_eq!(res.unwrap(), spdm_buffer.len() + VTPM_MESSAGE_HEADER_SIZE);
let res = vtpm_encap.encap(&spdm_buffer, &mut buffer, true);
assert_eq!(res.is_err(), false);
assert_eq!(res.unwrap(), spdm_buffer.len() + VTPM_MESSAGE_HEADER_SIZE);
}

#[test]
fn test_encap_app() {
let mut vtpm_encap = VtpmTransportEncap::default();
let app_message_buffer = [1u8; TEST_SPDM_BUFFER_SIZE];
let mut app_data_buffer = [0u8; TEST_TRANSPORT_BUFFER_SIZE];
let res = vtpm_encap.encap_app(&app_message_buffer, &mut app_data_buffer, false);
assert_eq!(res.is_err(), false);
assert_eq!(
res.unwrap(),
app_message_buffer.len() + VTPM_APP_MESSAGE_HEADER_SIZE
);
let res = vtpm_encap.encap_app(&app_message_buffer, &mut app_data_buffer, true);
assert_eq!(res.is_err(), false);
assert_eq!(
res.unwrap(),
app_message_buffer.len() + VTPM_APP_MESSAGE_HEADER_SIZE
);
}

#[test]
fn test_encap_invalid_buffer() {
let mut vtpm_encap = VtpmTransportEncap::default();
let spdm_buffer = [1u8; TEST_SPDM_BUFFER_SIZE];
let mut buffer = [0u8; INVALID_TEST_TRANSPORT_BUFFER_SIZE];
let res = vtpm_encap.encap(&spdm_buffer, &mut buffer, false);
assert!(res.is_err());
let res = vtpm_encap.encap_app(&spdm_buffer, &mut buffer, false);
assert!(res.is_err());
let res = vtpm_encap.encap(&spdm_buffer, &mut [], false);
assert!(res.is_err());
let spdm_buffer = [0u8; TEST_SPDM_BUFFER_SIZE];
let mut buffer = [0u8; TEST_SPDM_BUFFER_SIZE];
let res = vtpm_encap.encap(&spdm_buffer, &mut buffer, false);
assert!(res.is_err());
}

#[test]
fn test_decap() {
let mut vtpm_encap = VtpmTransportEncap::default();
let spdm_buffer = [0u8; TEST_SPDM_BUFFER_SIZE];
let mut buffer = [0u8; TEST_TRANSPORT_BUFFER_SIZE];
let res = vtpm_encap.decap(&spdm_buffer, &mut buffer);
assert_eq!(res.unwrap().0, spdm_buffer.len() - VTPM_MESSAGE_HEADER_SIZE);
let spdm_buffer = [0x02; TEST_SPDM_BUFFER_SIZE];
let res = vtpm_encap.decap(&spdm_buffer, &mut buffer);
}

#[test]
fn test_decap_app() {
let mut vtpm_encap = VtpmTransportEncap::default();
let spdm_buffer = [0u8; TEST_SPDM_BUFFER_SIZE];
let mut buffer = [0u8; TEST_TRANSPORT_BUFFER_SIZE];
let res = vtpm_encap.decap_app(&spdm_buffer, &mut buffer);
assert_eq!(
res.unwrap().0,
spdm_buffer.len() - VTPM_APP_MESSAGE_HEADER_SIZE
);
let spdm_buffer = [0x03; TEST_SPDM_BUFFER_SIZE];
let res = vtpm_encap.decap_app(&spdm_buffer, &mut buffer);
assert_eq!(
res.unwrap().0,
spdm_buffer.len() - VTPM_APP_MESSAGE_HEADER_SIZE
);
}

#[test]
fn test_decap_invalid_buffer() {
let mut vtpm_encap = VtpmTransportEncap::default();
let mut buffer = [0u8; INVALID_TEST_TRANSPORT_BUFFER_SIZE];
let spdm_buffer = [1u8; TEST_SPDM_BUFFER_SIZE];
let res = vtpm_encap.decap(&spdm_buffer, &mut buffer);
assert!(res.is_err());
let res = vtpm_encap.decap_app(&spdm_buffer, &mut buffer);
assert!(res.is_err());
let res = vtpm_encap.decap(&spdm_buffer, &mut []);
assert!(res.is_err());
let res = vtpm_encap.decap(&[], &mut []);
assert!(res.is_err());
let res = vtpm_encap.decap_app(&spdm_buffer, &mut []);
assert!(res.is_err());
let res = vtpm_encap.decap_app(&[], &mut []);
assert!(res.is_err());
}
}

0 comments on commit f0ef733

Please sign in to comment.