From f2e9e4bcf85ef7adb3846cbb61f8bc15feb0cee0 Mon Sep 17 00:00:00 2001 From: "Lu, Xiaoyu1" Date: Sun, 18 Feb 2024 20:23:50 -0500 Subject: [PATCH] Test: add unit tests for x509 parser Signed-off-by: xiaoyuxlu --- spdmlib/src/crypto/x509v3.rs | 54 ++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/spdmlib/src/crypto/x509v3.rs b/spdmlib/src/crypto/x509v3.rs index c8ccace..8e825d1 100644 --- a/spdmlib/src/crypto/x509v3.rs +++ b/spdmlib/src/crypto/x509v3.rs @@ -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) + ); + } }