Skip to content

Commit

Permalink
Test: add unit tests for x509 parser
Browse files Browse the repository at this point in the history
Signed-off-by: xiaoyuxlu <[email protected]>
  • Loading branch information
xiaoyuxlu authored and jyao1 committed Feb 21, 2024
1 parent cdd0984 commit f2e9e4b
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions spdmlib/src/crypto/x509v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1442,4 +1442,58 @@ mod tests {
assert!(check_leaf_certificate(&ct1_wrong, true).is_err());
assert!(check_leaf_certificate(&ct1_wrong, false).is_err());
}

#[test]
fn test_check_length_short_form() {
let data = [0x03, 0x01, 0x02, 0x03];
assert_eq!(check_length(&data), Ok((3, 1)));
}

#[test]
fn test_check_length_short_form_large_than_128() {
let data = [0x80];
assert_eq!(check_length(&data), Err(SPDM_STATUS_VERIF_FAIL));
}

#[test]
fn test_check_length_long_form() {
let data = [0x81, 0x80, 0xFF, 0xFF];
assert_eq!(check_length(&data), Ok((0x80, 2)));
}

#[ignore = "https://github.com/ccc-spdm-tools/spdm-rs/issues/28"]
#[test]
fn test_check_length_long_form_smaller_than_128() {
let data: [u8; 2] = [0x81, 0x7F];
assert_eq!(check_length(&data), Err(SPDM_STATUS_VERIF_FAIL));
}

#[test]
fn test_check_length_overflow_the_length_of_k_octets_bytes_is_less_than_k() {
// First Octet + k + l octets
// bit0..=6 k Length l
// k = 5
// length(k(octets)) < 5
let data = [0x85, 0xFF, 0xFF, 0xFF, 0xFF];
assert_eq!(check_length(&data), Err(SPDM_STATUS_VERIF_FAIL));
}

#[ignore = "https://github.com/ccc-spdm-tools/spdm-rs/issues/15"]
#[test]
fn test_check_cert_format_large_length() {
let cert = std::fs::read("../test_key/ecp384/bundle_requester.certchain.der")
.expect("unable to read ca cert!");
let mut malformed_cert = cert.clone();
malformed_cert[1] = 0x88;
for i in 2..10 {
malformed_cert[i] = 0xff;
}
assert_eq!(
check_cert_format(
&malformed_cert,
SpdmBaseAsymAlgo::TPM_ALG_ECDSA_ECC_NIST_P384
),
Err(SPDM_STATUS_VERIF_FAIL)
);
}
}

0 comments on commit f2e9e4b

Please sign in to comment.