|
| 1 | +// Copyright 2023 Contributors to the Parsec project. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +use std::convert::TryFrom; |
| 4 | +use tss_esapi::{ |
| 5 | + constants::tss::{TPM2_RC_ASYMMETRIC, TPM2_RC_SEQUENCE, TSS2_RESMGR_TPM_RC_LAYER}, |
| 6 | + error::{ReturnCode, TpmResponseCode}, |
| 7 | +}; |
| 8 | + |
| 9 | +#[test] |
| 10 | +fn test_valid_tpm_resmgr_format_zero_response_code() { |
| 11 | + let expected_tss_rc = TSS2_RESMGR_TPM_RC_LAYER | TPM2_RC_SEQUENCE; |
| 12 | + let actual_rc = ReturnCode::try_from(expected_tss_rc) |
| 13 | + .expect("Failed to convert TPM2_RC_SEQUENCE in the TPM RESMGR layer to a ReturnCode."); |
| 14 | + |
| 15 | + if let ReturnCode::TpmResourceManager(actual_tpm_response_code) = actual_rc { |
| 16 | + assert!( |
| 17 | + matches!(actual_tpm_response_code,TpmResponseCode::FormatZero(_)), |
| 18 | + "TPM2_RC_SEQUENCE in the TPM RESMGR layer did not convert into the expected TpmResponseCode" |
| 19 | + ); |
| 20 | + } else { |
| 21 | + panic!("The TPM RESMGR layer did not convert into the expected ReturnCode"); |
| 22 | + } |
| 23 | + |
| 24 | + assert_eq!( |
| 25 | + expected_tss_rc, |
| 26 | + actual_rc.into(), |
| 27 | + "ReturnCode::TpmResourceManager did not convert into the expected TSS2_RC value" |
| 28 | + ); |
| 29 | +} |
| 30 | + |
| 31 | +#[test] |
| 32 | +fn test_valid_tpm_resmgr_format_one_response_code() { |
| 33 | + let expected_tss_rc = TSS2_RESMGR_TPM_RC_LAYER | TPM2_RC_ASYMMETRIC; |
| 34 | + let actual_rc = ReturnCode::try_from(expected_tss_rc) |
| 35 | + .expect("Failed to convert TPM2_RC_ASYMMETRIC in the TPM RESMGR layer to a ReturnCode."); |
| 36 | + |
| 37 | + if let ReturnCode::TpmResourceManager(actual_tpm_response_code) = actual_rc { |
| 38 | + assert!( |
| 39 | + matches!(actual_tpm_response_code, TpmResponseCode::FormatOne(_)), |
| 40 | + "TPM2_RC_ASYMMETRIC in the TPM RESMGR layer did not convert into the expected TpmResponseCode" |
| 41 | + ); |
| 42 | + } else { |
| 43 | + panic!("The TPM RESMGR layer did not convert into the expected ReturnCode"); |
| 44 | + } |
| 45 | + |
| 46 | + assert_eq!( |
| 47 | + expected_tss_rc, |
| 48 | + actual_rc.into(), |
| 49 | + "ReturnCode::TpmResourceManager did not convert into the expected TSS2_RC value" |
| 50 | + ); |
| 51 | +} |
0 commit comments