From a84d3e345a6874867749504271d24a44861c0488 Mon Sep 17 00:00:00 2001 From: Jesper Brynolf Date: Sat, 25 Nov 2023 10:36:22 +0100 Subject: [PATCH] Fixes some causes for type ambiguity in tests. - Many of tests uses macro to create the test code and in this test code it is common practice to use ```into``` or ```try_into``` conversions. But this can cause ambiguities later on if new types introduced implements traits that previously only was implemented by one type. So in order to avoid this kind of ambiguity some of the conversions have been changed. This only a first step of changes that probably needs to be done. This fixes the errors that occurs if one tries to add ```serde_json``` to the ```cargo.yaml``` which is probably needed in order to be able to write tests for #466. Signed-off-by: Jesper Brynolf --- tss-esapi/src/lib.rs | 3 +- .../command_code_attributes_tests.rs | 6 +- .../session_attributes_tests.rs | 6 +- .../constants_tests/algorithm_tests.rs | 31 +++++---- .../base_return_code_tests.rs | 2 +- .../return_code_layer_tests.rs | 2 +- .../tpm_format_one_error_tests.rs | 2 +- .../tpm_format_zero_error_tests.rs | 2 +- .../tpm_format_zero_warning_tests.rs | 2 +- .../integrity_collection_pcr_tests.rs | 7 +- .../return_code_tests/esapi_tests.rs | 3 +- .../return_code_tests/fapi_tests.rs | 3 +- .../return_code_tests/muapi_tests.rs | 3 +- .../resource_manager_tests.rs | 3 +- .../resource_manager_tpm_tests.rs | 5 +- .../return_code_tests/sapi_tests.rs | 3 +- .../return_code_tests/tcti_tests.rs | 3 +- .../return_code_tests/tpm_tests.rs | 3 +- .../tpm_tests/tpm_format_one_error_tests.rs | 3 +- .../tpm_tests/tpm_format_zero_tests.rs | 5 +- .../tpm_format_zero_error_tests.rs | 3 +- .../tpm_format_zero_warning_tests.rs | 3 +- .../interface_types_tests/algorithms_tests.rs | 35 ++++++---- .../interface_types_tests/key_bits_tests.rs | 64 ++++++++++--------- .../pcr_tests/pcr_slot_tests.rs | 64 +++++++++++-------- 25 files changed, 159 insertions(+), 107 deletions(-) diff --git a/tss-esapi/src/lib.rs b/tss-esapi/src/lib.rs index 0dd06f7a..9d0aed88 100644 --- a/tss-esapi/src/lib.rs +++ b/tss-esapi/src/lib.rs @@ -9,7 +9,8 @@ overflowing_literals, path_statements, patterns_in_fns_without_body, - private_in_public, + private_bounds, + private_interfaces, unconditional_recursion, unused, unused_allocation, diff --git a/tss-esapi/tests/integration_tests/attributes_tests/command_code_attributes_tests.rs b/tss-esapi/tests/integration_tests/attributes_tests/command_code_attributes_tests.rs index 72e0dd17..91a95651 100644 --- a/tss-esapi/tests/integration_tests/attributes_tests/command_code_attributes_tests.rs +++ b/tss-esapi/tests/integration_tests/attributes_tests/command_code_attributes_tests.rs @@ -92,7 +92,7 @@ fn test_conversions_non_vendor_specific() { assert_eq!( expected.0, - command_code_attributes.into(), + TPMA_CC::from(command_code_attributes), "CommandCodeAttributes did not convert into the expected TPMA_CC value" ); } @@ -162,7 +162,7 @@ fn test_conversions_vendor_specific() { assert_eq!( expected.0, - command_code_attributes.into(), + TPMA_CC::from(command_code_attributes), "CommandCodeAttributes did not convert into the expected TPMA_CC value" ); } @@ -307,7 +307,7 @@ fn test_builder() { assert_eq!( expected.0, - actual.into(), + TPMA_CC::from(actual), "CommandCodeAttributes built using the builder did not convert into the expected TPMA_CC value" ); } diff --git a/tss-esapi/tests/integration_tests/attributes_tests/session_attributes_tests.rs b/tss-esapi/tests/integration_tests/attributes_tests/session_attributes_tests.rs index a968cf9b..fac19a61 100644 --- a/tss-esapi/tests/integration_tests/attributes_tests/session_attributes_tests.rs +++ b/tss-esapi/tests/integration_tests/attributes_tests/session_attributes_tests.rs @@ -1,6 +1,6 @@ // Copyright 2022 Contributors to the Parsec project. // SPDX-License-Identifier: Apache-2.0 -use std::convert::{TryFrom, TryInto}; +use std::convert::TryFrom; use tss_esapi::{ attributes::{SessionAttributes, SessionAttributesBuilder, SessionAttributesMask}, tss2_esys::TPMA_SESSION, @@ -18,7 +18,7 @@ macro_rules! test_valid_conversion { ); assert_eq!( tpma_session, - session_attributes.try_into().expect("Failed to convert SessionAttributes into TPMA_SESSION_ATTRIBUTE."), + TPMA_SESSION::try_from(session_attributes).expect("Failed to convert SessionAttributes into TPMA_SESSION_ATTRIBUTE."), "Converting session attributes with {} set did not convert into the expected TPMA_SESSION value", std::stringify!($method), ); }; @@ -30,7 +30,7 @@ macro_rules! test_valid_mask_conversion { let session_attributes_mask = SessionAttributesMask::try_from(tpma_session).expect("Failed to convert TPMA_SESSION into SessionAttributesMask"); assert_eq!( tpma_session, - session_attributes_mask.try_into().expect("Failed to convert SessionAttributesMask into TPMA_SESSION"), + TPMA_SESSION::try_from(session_attributes_mask).expect("Failed to convert SessionAttributesMask into TPMA_SESSION"), "Converting session attributes mask with {} set did not convert into the expected TPMA_SESSION value", $attribute, ); }; diff --git a/tss-esapi/tests/integration_tests/constants_tests/algorithm_tests.rs b/tss-esapi/tests/integration_tests/constants_tests/algorithm_tests.rs index 814e45e7..014a6ac4 100644 --- a/tss-esapi/tests/integration_tests/constants_tests/algorithm_tests.rs +++ b/tss-esapi/tests/integration_tests/constants_tests/algorithm_tests.rs @@ -1,22 +1,29 @@ // Copyright 2021 Contributors to the Parsec project. // SPDX-License-Identifier: Apache-2.0 use std::convert::TryFrom; -use tss_esapi::constants::{ - tss::{ - TPM2_ALG_AES, TPM2_ALG_CAMELLIA, TPM2_ALG_CBC, TPM2_ALG_CFB, TPM2_ALG_CMAC, TPM2_ALG_CTR, - TPM2_ALG_ECB, TPM2_ALG_ECC, TPM2_ALG_ECDAA, TPM2_ALG_ECDH, TPM2_ALG_ECDSA, TPM2_ALG_ECMQV, - TPM2_ALG_ECSCHNORR, TPM2_ALG_ERROR, TPM2_ALG_HMAC, TPM2_ALG_KDF1_SP800_108, - TPM2_ALG_KDF1_SP800_56A, TPM2_ALG_KDF2, TPM2_ALG_KEYEDHASH, TPM2_ALG_MGF1, TPM2_ALG_NULL, - TPM2_ALG_OAEP, TPM2_ALG_OFB, TPM2_ALG_RSA, TPM2_ALG_RSAES, TPM2_ALG_RSAPSS, - TPM2_ALG_RSASSA, TPM2_ALG_SHA1, TPM2_ALG_SHA256, TPM2_ALG_SHA384, TPM2_ALG_SHA3_256, - TPM2_ALG_SHA3_384, TPM2_ALG_SHA3_512, TPM2_ALG_SHA512, TPM2_ALG_SM2, TPM2_ALG_SM3_256, - TPM2_ALG_SM4, TPM2_ALG_SYMCIPHER, TPM2_ALG_TDES, TPM2_ALG_XOR, +use tss_esapi::{ + constants::{ + tss::{ + TPM2_ALG_AES, TPM2_ALG_CAMELLIA, TPM2_ALG_CBC, TPM2_ALG_CFB, TPM2_ALG_CMAC, + TPM2_ALG_CTR, TPM2_ALG_ECB, TPM2_ALG_ECC, TPM2_ALG_ECDAA, TPM2_ALG_ECDH, + TPM2_ALG_ECDSA, TPM2_ALG_ECMQV, TPM2_ALG_ECSCHNORR, TPM2_ALG_ERROR, TPM2_ALG_HMAC, + TPM2_ALG_KDF1_SP800_108, TPM2_ALG_KDF1_SP800_56A, TPM2_ALG_KDF2, TPM2_ALG_KEYEDHASH, + TPM2_ALG_MGF1, TPM2_ALG_NULL, TPM2_ALG_OAEP, TPM2_ALG_OFB, TPM2_ALG_RSA, + TPM2_ALG_RSAES, TPM2_ALG_RSAPSS, TPM2_ALG_RSASSA, TPM2_ALG_SHA1, TPM2_ALG_SHA256, + TPM2_ALG_SHA384, TPM2_ALG_SHA3_256, TPM2_ALG_SHA3_384, TPM2_ALG_SHA3_512, + TPM2_ALG_SHA512, TPM2_ALG_SM2, TPM2_ALG_SM3_256, TPM2_ALG_SM4, TPM2_ALG_SYMCIPHER, + TPM2_ALG_TDES, TPM2_ALG_XOR, + }, + AlgorithmIdentifier, }, - AlgorithmIdentifier, + tss2_esys::TPM2_ALG_ID, }; macro_rules! test_conversion { ($tpm_alg_id:ident, $algorithm:ident) => { - assert_eq!($tpm_alg_id, AlgorithmIdentifier::$algorithm.into()); + assert_eq!( + $tpm_alg_id, + TPM2_ALG_ID::from(AlgorithmIdentifier::$algorithm) + ); assert_eq!( AlgorithmIdentifier::$algorithm, AlgorithmIdentifier::try_from($tpm_alg_id).expect(&format!( diff --git a/tss-esapi/tests/integration_tests/constants_tests/return_code_tests/base_return_code_tests.rs b/tss-esapi/tests/integration_tests/constants_tests/return_code_tests/base_return_code_tests.rs index 7a9805d9..54d4878a 100644 --- a/tss-esapi/tests/integration_tests/constants_tests/return_code_tests/base_return_code_tests.rs +++ b/tss-esapi/tests/integration_tests/constants_tests/return_code_tests/base_return_code_tests.rs @@ -35,7 +35,7 @@ macro_rules! test_valid_conversion { ($tss_rc_base:ident, $base_error_item:ident) => { assert_eq!( $tss_rc_base as u16, - BaseError::$base_error_item.into(), + u16::from(BaseError::$base_error_item), "Failed to convert {} into the expected TSS2_RC value {}", std::stringify!(BaseError::$base_error_item), std::stringify!($tss_rc_base), diff --git a/tss-esapi/tests/integration_tests/constants_tests/return_code_tests/return_code_layer_tests.rs b/tss-esapi/tests/integration_tests/constants_tests/return_code_tests/return_code_layer_tests.rs index 689b3247..23d80660 100644 --- a/tss-esapi/tests/integration_tests/constants_tests/return_code_tests/return_code_layer_tests.rs +++ b/tss-esapi/tests/integration_tests/constants_tests/return_code_tests/return_code_layer_tests.rs @@ -27,7 +27,7 @@ macro_rules! test_valid_conversion { assert_eq!( tss_rc_layer_unshifted, - ReturnCodeLayer::$return_code_layer.into(), + u8::from(ReturnCodeLayer::$return_code_layer), "Conversion of {} into TSS_RC did not result in the expected {}", std::stringify!(ReturnCodeLayer::$return_code_layer), std::stringify!($tss_rc_layer) diff --git a/tss-esapi/tests/integration_tests/constants_tests/return_code_tests/tpm_format_one_error_tests.rs b/tss-esapi/tests/integration_tests/constants_tests/return_code_tests/tpm_format_one_error_tests.rs index 05502021..2bce7a6b 100644 --- a/tss-esapi/tests/integration_tests/constants_tests/return_code_tests/tpm_format_one_error_tests.rs +++ b/tss-esapi/tests/integration_tests/constants_tests/return_code_tests/tpm_format_one_error_tests.rs @@ -30,7 +30,7 @@ macro_rules! test_valid_conversion { let tpm_rc = TpmFormatOneRc($tpm_fmt1_rc as u16); assert_eq!( tpm_rc.error_number(), - TpmFormatOneError::$item.into(), + u8::from(TpmFormatOneError::$item), "Conversion of {} into a u16 value without TPM2_RC_FMT1 did not produce the expected value {}", std::stringify!(TpmFormatOneError::$item), tpm_rc.error_number() diff --git a/tss-esapi/tests/integration_tests/constants_tests/return_code_tests/tpm_format_zero_error_tests.rs b/tss-esapi/tests/integration_tests/constants_tests/return_code_tests/tpm_format_zero_error_tests.rs index 37369a5a..0fea66fa 100644 --- a/tss-esapi/tests/integration_tests/constants_tests/return_code_tests/tpm_format_zero_error_tests.rs +++ b/tss-esapi/tests/integration_tests/constants_tests/return_code_tests/tpm_format_zero_error_tests.rs @@ -31,7 +31,7 @@ macro_rules! test_valid_conversion { let tpm_rc = TpmFormatZeroErrorRc($tpm_ver1_rc as u16); assert_eq!( tpm_rc.error_number(), - TpmFormatZeroError::$item.into(), + u8::from(TpmFormatZeroError::$item), "Conversion of {} into a u16 value without TPM2_RC_VER1 did not produce the expected value {}", std::stringify!(TpmFormatZeroError::$item), tpm_rc.error_number() diff --git a/tss-esapi/tests/integration_tests/constants_tests/return_code_tests/tpm_format_zero_warning_tests.rs b/tss-esapi/tests/integration_tests/constants_tests/return_code_tests/tpm_format_zero_warning_tests.rs index 3797dd33..c890306c 100644 --- a/tss-esapi/tests/integration_tests/constants_tests/return_code_tests/tpm_format_zero_warning_tests.rs +++ b/tss-esapi/tests/integration_tests/constants_tests/return_code_tests/tpm_format_zero_warning_tests.rs @@ -30,7 +30,7 @@ macro_rules! test_valid_conversion { let tpm_rc = TpmFormatZeroWarningRc($tpm_warn_rc as u16); assert_eq!( tpm_rc.error_number(), - TpmFormatZeroWarning::$item.into(), + u8::from(TpmFormatZeroWarning::$item), "Conversion of {} into a u16 value without TPM2_RC_VER1 did not produce the expected value {}", std::stringify!(TpmFormatZeroWarning::$item), tpm_rc.error_number() diff --git a/tss-esapi/tests/integration_tests/context_tests/tpm_commands/integrity_collection_pcr_tests.rs b/tss-esapi/tests/integration_tests/context_tests/tpm_commands/integrity_collection_pcr_tests.rs index fb141657..dc1168a9 100644 --- a/tss-esapi/tests/integration_tests/context_tests/tpm_commands/integrity_collection_pcr_tests.rs +++ b/tss-esapi/tests/integration_tests/context_tests/tpm_commands/integrity_collection_pcr_tests.rs @@ -151,7 +151,7 @@ mod test_pcr_read { use tss_esapi::{ interface_types::algorithm::HashingAlgorithm, structures::{PcrSelectionListBuilder, PcrSlot}, - tss2_esys::{TPM2_SHA256_DIGEST_SIZE, TPML_PCR_SELECTION}, + tss2_esys::{TPM2_SHA256_DIGEST_SIZE, TPMI_ALG_HASH, TPML_PCR_SELECTION}, }; #[test] @@ -167,7 +167,10 @@ mod test_pcr_read { assert_eq!(pcr_selection_list.len(), 1); assert_eq!(input.count as usize, pcr_selection_list.len()); assert_eq!(input.pcrSelections[0].sizeofSelect, 3); - assert_eq!(input.pcrSelections[0].hash, HashingAlgorithm::Sha256.into()); + assert_eq!( + input.pcrSelections[0].hash, + TPMI_ALG_HASH::from(HashingAlgorithm::Sha256) + ); assert_eq!(input.pcrSelections[0].pcrSelect[0], 0b0000_0001); assert_eq!(input.pcrSelections[0].pcrSelect[1], 0b0000_0000); assert_eq!(input.pcrSelections[0].pcrSelect[2], 0b0000_0000); diff --git a/tss-esapi/tests/integration_tests/error_tests/return_code_tests/esapi_tests.rs b/tss-esapi/tests/integration_tests/error_tests/return_code_tests/esapi_tests.rs index f1fb26e2..d5f5055c 100644 --- a/tss-esapi/tests/integration_tests/error_tests/return_code_tests/esapi_tests.rs +++ b/tss-esapi/tests/integration_tests/error_tests/return_code_tests/esapi_tests.rs @@ -22,6 +22,7 @@ use tss_esapi::{ error::{BaseReturnCode, EsapiReturnCode, ReturnCode}, interface_types::{algorithm::HashingAlgorithm, resource_handles::Hierarchy}, structures::{Auth, SymmetricDefinition}, + tss2_esys::TSS2_RC, Error, WrapperErrorKind, }; @@ -74,7 +75,7 @@ macro_rules! test_valid_conversion { assert_eq!( expected_tss_rc, - actual_rc.into(), + TSS2_RC::from(actual_rc), "EsapiReturnCode with {} did not convert into expected {} TSS2_RC in the ESAPI layer.", std::stringify!(BaseError::$base_error), std::stringify!($tss_rc_base_error), diff --git a/tss-esapi/tests/integration_tests/error_tests/return_code_tests/fapi_tests.rs b/tss-esapi/tests/integration_tests/error_tests/return_code_tests/fapi_tests.rs index 5174770b..19fc1a13 100644 --- a/tss-esapi/tests/integration_tests/error_tests/return_code_tests/fapi_tests.rs +++ b/tss-esapi/tests/integration_tests/error_tests/return_code_tests/fapi_tests.rs @@ -24,6 +24,7 @@ use tss_esapi::{ BaseError, }, error::{BaseReturnCode, FapiReturnCode, ReturnCode}, + tss2_esys::TSS2_RC, Error, WrapperErrorKind, }; @@ -76,7 +77,7 @@ macro_rules! test_valid_conversion { assert_eq!( expected_tss_rc, - actual_rc.into(), + TSS2_RC::from(actual_rc), "FapiReturnCode with {} did not convert into expected {} TSS2_RC in the FAPI layer.", std::stringify!(BaseError::$base_error), std::stringify!($tss_rc_base_error), diff --git a/tss-esapi/tests/integration_tests/error_tests/return_code_tests/muapi_tests.rs b/tss-esapi/tests/integration_tests/error_tests/return_code_tests/muapi_tests.rs index 738e6286..4eef029e 100644 --- a/tss-esapi/tests/integration_tests/error_tests/return_code_tests/muapi_tests.rs +++ b/tss-esapi/tests/integration_tests/error_tests/return_code_tests/muapi_tests.rs @@ -12,6 +12,7 @@ use tss_esapi::{ BaseError, }, error::{BaseReturnCode, MuapiReturnCode, ReturnCode}, + tss2_esys::TSS2_RC, Error, WrapperErrorKind, }; @@ -64,7 +65,7 @@ macro_rules! test_valid_conversion { assert_eq!( expected_tss_rc, - actual_rc.into(), + TSS2_RC::from(actual_rc), "{} did not convert into expected {} in TSS2_RC MUAPI layer.", std::stringify!(ReturnCode::Mu(MuapiReturnCode::$muapi_rc_item)), std::stringify!($tss_rc_base_error), diff --git a/tss-esapi/tests/integration_tests/error_tests/return_code_tests/resource_manager_tests.rs b/tss-esapi/tests/integration_tests/error_tests/return_code_tests/resource_manager_tests.rs index f2a2acdb..4e6110f5 100644 --- a/tss-esapi/tests/integration_tests/error_tests/return_code_tests/resource_manager_tests.rs +++ b/tss-esapi/tests/integration_tests/error_tests/return_code_tests/resource_manager_tests.rs @@ -30,6 +30,7 @@ use tss_esapi::{ }, error::{BaseReturnCode, ReturnCode}, tss2_esys::TSS2_LAYER_IMPLEMENTATION_SPECIFIC_OFFSET, + tss2_esys::TSS2_RC, Error, WrapperErrorKind, }; @@ -69,7 +70,7 @@ macro_rules! test_valid_conversion { assert_eq!( expected_tss_rc, - actual_rc.into(), + TSS2_RC::from(actual_rc), "BaseReturnCode with {} did not convert into expected {} TSS2_RC in the RESMGR layer.", std::stringify!(BaseError::$base_error), std::stringify!($tss_rc_base_error), diff --git a/tss-esapi/tests/integration_tests/error_tests/return_code_tests/resource_manager_tpm_tests.rs b/tss-esapi/tests/integration_tests/error_tests/return_code_tests/resource_manager_tpm_tests.rs index 3f8688bb..dd6bf843 100644 --- a/tss-esapi/tests/integration_tests/error_tests/return_code_tests/resource_manager_tpm_tests.rs +++ b/tss-esapi/tests/integration_tests/error_tests/return_code_tests/resource_manager_tpm_tests.rs @@ -4,6 +4,7 @@ use std::convert::TryFrom; use tss_esapi::{ constants::tss::{TPM2_RC_ASYMMETRIC, TPM2_RC_SEQUENCE, TSS2_RESMGR_TPM_RC_LAYER}, error::{ReturnCode, TpmResponseCode}, + tss2_esys::TSS2_RC, }; #[test] @@ -23,7 +24,7 @@ fn test_valid_tpm_resmgr_format_zero_response_code() { assert_eq!( expected_tss_rc, - actual_rc.into(), + TSS2_RC::from(actual_rc), "ReturnCode::TpmResourceManager did not convert into the expected TSS2_RC value" ); } @@ -45,7 +46,7 @@ fn test_valid_tpm_resmgr_format_one_response_code() { assert_eq!( expected_tss_rc, - actual_rc.into(), + TSS2_RC::from(actual_rc), "ReturnCode::TpmResourceManager did not convert into the expected TSS2_RC value" ); } diff --git a/tss-esapi/tests/integration_tests/error_tests/return_code_tests/sapi_tests.rs b/tss-esapi/tests/integration_tests/error_tests/return_code_tests/sapi_tests.rs index c5538625..6798c8ea 100644 --- a/tss-esapi/tests/integration_tests/error_tests/return_code_tests/sapi_tests.rs +++ b/tss-esapi/tests/integration_tests/error_tests/return_code_tests/sapi_tests.rs @@ -16,6 +16,7 @@ use tss_esapi::{ BaseError, }, error::{BaseReturnCode, ReturnCode, SapiReturnCode}, + tss2_esys::TSS2_RC, Error, WrapperErrorKind, }; @@ -68,7 +69,7 @@ macro_rules! test_valid_conversion { assert_eq!( expected_tss_rc, - actual_rc.into(), + TSS2_RC::from(actual_rc), "SapiReturnCode with {} did not convert into expected {} TSS2_RC in the SAPI layer.", std::stringify!(BaseError::$base_error), std::stringify!($tss_rc_base_error), diff --git a/tss-esapi/tests/integration_tests/error_tests/return_code_tests/tcti_tests.rs b/tss-esapi/tests/integration_tests/error_tests/return_code_tests/tcti_tests.rs index fe29840d..ad5fd9db 100644 --- a/tss-esapi/tests/integration_tests/error_tests/return_code_tests/tcti_tests.rs +++ b/tss-esapi/tests/integration_tests/error_tests/return_code_tests/tcti_tests.rs @@ -15,6 +15,7 @@ use tss_esapi::{ BaseError, }, error::{BaseReturnCode, ReturnCode, TctiReturnCode}, + tss2_esys::TSS2_RC, Error, WrapperErrorKind, }; @@ -67,7 +68,7 @@ macro_rules! test_valid_conversion { assert_eq!( expected_tss_rc, - actual_rc.into(), + TSS2_RC::from(actual_rc), "TctiReturnCode with {} did not convert into expected {} TSS2_RC in the TCTI layer.", std::stringify!(BaseError::$base_error), std::stringify!($tss_rc_base_error), diff --git a/tss-esapi/tests/integration_tests/error_tests/return_code_tests/tpm_tests.rs b/tss-esapi/tests/integration_tests/error_tests/return_code_tests/tpm_tests.rs index 98dae718..f858f093 100644 --- a/tss-esapi/tests/integration_tests/error_tests/return_code_tests/tpm_tests.rs +++ b/tss-esapi/tests/integration_tests/error_tests/return_code_tests/tpm_tests.rs @@ -8,6 +8,7 @@ use std::{convert::TryFrom, error::Error}; use tss_esapi::{ constants::tss::{TPM2_RC_ASYMMETRIC, TPM2_RC_SEQUENCE, TSS2_TPM_RC_LAYER}, error::{ReturnCode, TpmFormatOneResponseCode, TpmFormatZeroResponseCode, TpmResponseCode}, + tss2_esys::TSS2_RC, }; macro_rules! test_valid_conversions { @@ -41,7 +42,7 @@ macro_rules! test_valid_conversions { assert_eq!( expected_tss_rc, - actual_rc.into(), + TSS2_RC::from(actual_rc), "ReturnCode::Tpm did not convert into the expected TSS2_RC value." ); }; diff --git a/tss-esapi/tests/integration_tests/error_tests/return_code_tests/tpm_tests/tpm_format_one_error_tests.rs b/tss-esapi/tests/integration_tests/error_tests/return_code_tests/tpm_tests/tpm_format_one_error_tests.rs index b9dab691..43dc7182 100644 --- a/tss-esapi/tests/integration_tests/error_tests/return_code_tests/tpm_tests/tpm_format_one_error_tests.rs +++ b/tss-esapi/tests/integration_tests/error_tests/return_code_tests/tpm_tests/tpm_format_one_error_tests.rs @@ -18,6 +18,7 @@ use tss_esapi::{ }, }, error::{ArgumentNumber, ReturnCode, TpmFormatOneResponseCode, TpmResponseCode}, + tss2_esys::TSS2_RC, }; macro_rules! test_valid_conversions_with_all_argument_combinations { @@ -80,7 +81,7 @@ macro_rules! test_valid_conversion { assert_eq!( expected_tss_rc, - actual_rc.into(), + TSS2_RC::from(actual_rc), "TpmFormatOneResponseCode with {} and {} in the TPM layer did not convert into the expected TSS2_RC", std::stringify!(TpmFormatOneError::$tpm_format_one_error_item), std::stringify!(ArgumentNumber::$argument_number_item), diff --git a/tss-esapi/tests/integration_tests/error_tests/return_code_tests/tpm_tests/tpm_format_zero_tests.rs b/tss-esapi/tests/integration_tests/error_tests/return_code_tests/tpm_tests/tpm_format_zero_tests.rs index 8ab586aa..9d29b851 100644 --- a/tss-esapi/tests/integration_tests/error_tests/return_code_tests/tpm_tests/tpm_format_zero_tests.rs +++ b/tss-esapi/tests/integration_tests/error_tests/return_code_tests/tpm_tests/tpm_format_zero_tests.rs @@ -14,6 +14,7 @@ use tss_esapi::{ ReturnCode, TpmFormatZeroErrorResponseCode, TpmFormatZeroResponseCode, TpmFormatZeroWarningResponseCode, TpmResponseCode, }, + tss2_esys::TSS2_RC, }; bitfield! { @@ -39,7 +40,7 @@ fn test_vendor_specific_valid_conversions() { { assert_eq!( expected_tss_rc, - actual.into(), + TSS2_RC::from(actual), "Converting vendor specific return code did not return the original value." ); } else { @@ -48,7 +49,7 @@ fn test_vendor_specific_valid_conversions() { assert_eq!( expected_tss_rc, - actual_rc.into(), + TSS2_RC::from(actual_rc), "The vendor specific return code did not convert into the expected TSS2_RC in the TPM layer." ) } diff --git a/tss-esapi/tests/integration_tests/error_tests/return_code_tests/tpm_tests/tpm_format_zero_tests/tpm_format_zero_error_tests.rs b/tss-esapi/tests/integration_tests/error_tests/return_code_tests/tpm_tests/tpm_format_zero_tests/tpm_format_zero_error_tests.rs index e1a8a920..f65d3e5b 100644 --- a/tss-esapi/tests/integration_tests/error_tests/return_code_tests/tpm_tests/tpm_format_zero_tests/tpm_format_zero_error_tests.rs +++ b/tss-esapi/tests/integration_tests/error_tests/return_code_tests/tpm_tests/tpm_format_zero_tests/tpm_format_zero_error_tests.rs @@ -20,6 +20,7 @@ use tss_esapi::{ error::{ ReturnCode, TpmFormatZeroErrorResponseCode, TpmFormatZeroResponseCode, TpmResponseCode, }, + tss2_esys::TSS2_RC, Error, WrapperErrorKind, }; @@ -63,7 +64,7 @@ macro_rules! test_valid_conversion { assert_eq!( expected_tss_rc, - actual_rc.into(), + TSS2_RC::from(actual_rc), "{} with {} did not convert into expected {} TSS2_RC in the TPM layer.", std::any::type_name::(), std::stringify!(TpmFormatZeroError::$item), diff --git a/tss-esapi/tests/integration_tests/error_tests/return_code_tests/tpm_tests/tpm_format_zero_tests/tpm_format_zero_warning_tests.rs b/tss-esapi/tests/integration_tests/error_tests/return_code_tests/tpm_tests/tpm_format_zero_tests/tpm_format_zero_warning_tests.rs index 56a55786..27018555 100644 --- a/tss-esapi/tests/integration_tests/error_tests/return_code_tests/tpm_tests/tpm_format_zero_tests/tpm_format_zero_warning_tests.rs +++ b/tss-esapi/tests/integration_tests/error_tests/return_code_tests/tpm_tests/tpm_format_zero_tests/tpm_format_zero_warning_tests.rs @@ -19,6 +19,7 @@ use tss_esapi::{ error::{ ReturnCode, TpmFormatZeroResponseCode, TpmFormatZeroWarningResponseCode, TpmResponseCode, }, + tss2_esys::TSS2_RC, Error, WrapperErrorKind, }; @@ -59,7 +60,7 @@ macro_rules! test_valid_conversion { assert_eq!( expected_tss_rc, - actual_rc.into(), + TSS2_RC::from(actual_rc), "TpmFormatZeroResponseCode with {} did not convert into expected {} TSS2_RC in the TPM layer.", std::stringify!(TpmFormatZeroWarning::$item), std::stringify!($tpm_rc), diff --git a/tss-esapi/tests/integration_tests/interface_types_tests/algorithms_tests.rs b/tss-esapi/tests/integration_tests/interface_types_tests/algorithms_tests.rs index 34dcb8ce..9b2a7584 100644 --- a/tss-esapi/tests/integration_tests/interface_types_tests/algorithms_tests.rs +++ b/tss-esapi/tests/integration_tests/interface_types_tests/algorithms_tests.rs @@ -6,26 +6,39 @@ macro_rules! test_conversion { ($tpm_alg_id:ident, $interface_type:ident::$interface_type_item:ident) => { assert_eq!( AlgorithmIdentifier::$interface_type_item, - $interface_type::$interface_type_item.into() + AlgorithmIdentifier::from($interface_type::$interface_type_item) ); assert_eq!( - $interface_type::try_from(AlgorithmIdentifier::$interface_type_item).expect(&format!( - "Failed to parse from Algorithm for {}", - stringify!($interface_type_item) - )), + $interface_type::try_from(AlgorithmIdentifier::$interface_type_item).unwrap_or_else( + |_| { + panic!( + "Failed to convert from {} to {}.", + std::stringify!(AlgorithmIdentifier::$interface_type_item), + std::any::type_name::<$interface_type>() + ); + } + ), $interface_type::$interface_type_item ); assert_eq!( $tpm_alg_id, - AlgorithmIdentifier::from($interface_type::$interface_type_item).into() + tss_esapi::tss2_esys::TPM2_ALG_ID::from(AlgorithmIdentifier::from( + $interface_type::$interface_type_item + )) + ); + assert_eq!( + $tpm_alg_id, + tss_esapi::tss2_esys::TPM2_ALG_ID::from($interface_type::$interface_type_item) ); - assert_eq!($tpm_alg_id, $interface_type::$interface_type_item.into()); assert_eq!( $interface_type::$interface_type_item, - $interface_type::try_from($tpm_alg_id).expect(&format!( - "Failed to parse from alg if for {}", - stringify!($tpm_alg_id) - )) + $interface_type::try_from($tpm_alg_id).unwrap_or_else(|_| { + panic!( + "Failed to convert from {} to {}.", + std::stringify!($tpm_alg_id), + std::any::type_name::<$interface_type>() + ); + }) ); }; } diff --git a/tss-esapi/tests/integration_tests/interface_types_tests/key_bits_tests.rs b/tss-esapi/tests/integration_tests/interface_types_tests/key_bits_tests.rs index 685ed8ef..7836bce7 100644 --- a/tss-esapi/tests/integration_tests/interface_types_tests/key_bits_tests.rs +++ b/tss-esapi/tests/integration_tests/interface_types_tests/key_bits_tests.rs @@ -2,23 +2,27 @@ // SPDX-License-Identifier: Apache-2.0 macro_rules! test_conversions { - ($tss_ket_bits_interface_type:ident, $value:tt, $interface_type:ident::$interface_type_item:ident) => { + ($tss_key_bits_interface_type:ident, $value:expr, $interface_type:ident::$interface_type_item:ident) => { + let expected_interface_type = $tss_key_bits_interface_type::from($value); assert_eq!( - $value as $tss_ket_bits_interface_type, - $interface_type::$interface_type_item.into() + expected_interface_type, + $tss_key_bits_interface_type::from($interface_type::$interface_type_item) ); assert_eq!( - $interface_type::try_from($value as $tss_ket_bits_interface_type).expect(&format!( - "Failed to parse from a value {}", - $value as $tss_ket_bits_interface_type - )), + $interface_type::try_from(expected_interface_type).unwrap_or_else(|_| { + panic!( + "It should be possible to convert from {} to {}.", + std::any::type_name::<$interface_type>(), + expected_interface_type + ); + }), $interface_type::$interface_type_item, ); }; } macro_rules! test_invalid_conversions { - ($tss_ket_bits_interface_type:ident, $value:tt, $interface_type:ident, WrapperErrorKind::$expected_error:ident) => { + ($tss_ket_bits_interface_type:ident, $value:expr, $interface_type:ident, WrapperErrorKind::$expected_error:ident) => { assert_eq!( $interface_type::try_from($value as $tss_ket_bits_interface_type), Err(tss_esapi::Error::WrapperError( @@ -34,9 +38,9 @@ mod aes_key_bits_tests { #[test] fn test_valid_conversions() { - test_conversions!(TPMI_AES_KEY_BITS, 128, AesKeyBits::Aes128); - test_conversions!(TPMI_AES_KEY_BITS, 192, AesKeyBits::Aes192); - test_conversions!(TPMI_AES_KEY_BITS, 256, AesKeyBits::Aes256); + test_conversions!(TPMI_AES_KEY_BITS, 128u16, AesKeyBits::Aes128); + test_conversions!(TPMI_AES_KEY_BITS, 192u16, AesKeyBits::Aes192); + test_conversions!(TPMI_AES_KEY_BITS, 256u16, AesKeyBits::Aes256); } #[test] @@ -77,21 +81,21 @@ mod sm4_key_bits_tests { #[test] fn test_valid_conversions() { - test_conversions!(TPMI_SM4_KEY_BITS, 128, Sm4KeyBits::Sm4_128); + test_conversions!(TPMI_SM4_KEY_BITS, 128u16, Sm4KeyBits::Sm4_128); } #[test] fn test_invalid_conversions() { test_invalid_conversions!( TPMI_SM4_KEY_BITS, - 0, + 0u16, Sm4KeyBits, WrapperErrorKind::InvalidParam ); test_invalid_conversions!( TPMI_SM4_KEY_BITS, - 129, + 129u16, Sm4KeyBits, WrapperErrorKind::InvalidParam ); @@ -104,37 +108,37 @@ mod camellia_key_bits_tests { #[test] fn test_valid_conversions() { - test_conversions!(TPM2_KEY_BITS, 128, CamelliaKeyBits::Camellia128); - test_conversions!(TPM2_KEY_BITS, 192, CamelliaKeyBits::Camellia192); - test_conversions!(TPM2_KEY_BITS, 256, CamelliaKeyBits::Camellia256); + test_conversions!(TPM2_KEY_BITS, 128u16, CamelliaKeyBits::Camellia128); + test_conversions!(TPM2_KEY_BITS, 192u16, CamelliaKeyBits::Camellia192); + test_conversions!(TPM2_KEY_BITS, 256u16, CamelliaKeyBits::Camellia256); } #[test] fn test_invalid_conversions() { test_invalid_conversions!( TPM2_KEY_BITS, - 0, + 0u16, CamelliaKeyBits, WrapperErrorKind::InvalidParam ); test_invalid_conversions!( TPM2_KEY_BITS, - 129, + 129u16, CamelliaKeyBits, WrapperErrorKind::InvalidParam ); test_invalid_conversions!( TPM2_KEY_BITS, - 193, + 193u16, CamelliaKeyBits, WrapperErrorKind::InvalidParam ); test_invalid_conversions!( TPM2_KEY_BITS, - 257, + 257u16, CamelliaKeyBits, WrapperErrorKind::InvalidParam ); @@ -147,45 +151,45 @@ mod rsa_key_bits_tests { #[test] fn test_valid_conversions() { - test_conversions!(TPMI_RSA_KEY_BITS, 1024, RsaKeyBits::Rsa1024); - test_conversions!(TPMI_RSA_KEY_BITS, 2048, RsaKeyBits::Rsa2048); - test_conversions!(TPMI_RSA_KEY_BITS, 3072, RsaKeyBits::Rsa3072); - test_conversions!(TPMI_RSA_KEY_BITS, 4096, RsaKeyBits::Rsa4096); + test_conversions!(TPMI_RSA_KEY_BITS, 1024u16, RsaKeyBits::Rsa1024); + test_conversions!(TPMI_RSA_KEY_BITS, 2048u16, RsaKeyBits::Rsa2048); + test_conversions!(TPMI_RSA_KEY_BITS, 3072u16, RsaKeyBits::Rsa3072); + test_conversions!(TPMI_RSA_KEY_BITS, 4096u16, RsaKeyBits::Rsa4096); } #[test] fn test_invalid_conversions() { test_invalid_conversions!( TPMI_RSA_KEY_BITS, - 0, + 0u16, RsaKeyBits, WrapperErrorKind::InvalidParam ); test_invalid_conversions!( TPMI_RSA_KEY_BITS, - 1025, + 1025u16, RsaKeyBits, WrapperErrorKind::InvalidParam ); test_invalid_conversions!( TPMI_RSA_KEY_BITS, - 2049, + 2049u16, RsaKeyBits, WrapperErrorKind::InvalidParam ); test_invalid_conversions!( TPMI_RSA_KEY_BITS, - 2073, + 2073u16, RsaKeyBits, WrapperErrorKind::InvalidParam ); test_invalid_conversions!( TPMI_RSA_KEY_BITS, - 4097, + 4097u16, RsaKeyBits, WrapperErrorKind::InvalidParam ); diff --git a/tss-esapi/tests/integration_tests/structures_tests/pcr_tests/pcr_slot_tests.rs b/tss-esapi/tests/integration_tests/structures_tests/pcr_tests/pcr_slot_tests.rs index 23633584..c445c53a 100644 --- a/tss-esapi/tests/integration_tests/structures_tests/pcr_tests/pcr_slot_tests.rs +++ b/tss-esapi/tests/integration_tests/structures_tests/pcr_tests/pcr_slot_tests.rs @@ -3,34 +3,46 @@ use std::convert::TryFrom; use tss_esapi::{structures::PcrSlot, tss2_esys::TPM2_PCR_SELECT_MAX, Error, WrapperErrorKind}; +macro_rules! convert_to_u32_test { + ($value:expr, $slot_number:path) => { + assert_eq!( + $value, + u32::from($slot_number), + "Failed to convert {} to {}", + std::stringify!($value), + std::stringify!($slot_number) + ) + }; +} + #[test] fn test_conversion_to_u32() { - assert_eq!(0x0000_0001u32, PcrSlot::Slot0.into()); - assert_eq!(0x0000_0002u32, PcrSlot::Slot1.into()); - assert_eq!(0x0000_0004u32, PcrSlot::Slot2.into()); - assert_eq!(0x0000_0008u32, PcrSlot::Slot3.into()); - assert_eq!(0x0000_0010u32, PcrSlot::Slot4.into()); - assert_eq!(0x0000_0020u32, PcrSlot::Slot5.into()); - assert_eq!(0x0000_0040u32, PcrSlot::Slot6.into()); - assert_eq!(0x0000_0080u32, PcrSlot::Slot7.into()); - - assert_eq!(0x0000_0100u32, PcrSlot::Slot8.into()); - assert_eq!(0x0000_0200u32, PcrSlot::Slot9.into()); - assert_eq!(0x0000_0400u32, PcrSlot::Slot10.into()); - assert_eq!(0x0000_0800u32, PcrSlot::Slot11.into()); - assert_eq!(0x0000_1000u32, PcrSlot::Slot12.into()); - assert_eq!(0x0000_2000u32, PcrSlot::Slot13.into()); - assert_eq!(0x0000_4000u32, PcrSlot::Slot14.into()); - assert_eq!(0x0000_8000u32, PcrSlot::Slot15.into()); - - assert_eq!(0x0001_0000u32, PcrSlot::Slot16.into()); - assert_eq!(0x0002_0000u32, PcrSlot::Slot17.into()); - assert_eq!(0x0004_0000u32, PcrSlot::Slot18.into()); - assert_eq!(0x0008_0000u32, PcrSlot::Slot19.into()); - assert_eq!(0x0010_0000u32, PcrSlot::Slot20.into()); - assert_eq!(0x0020_0000u32, PcrSlot::Slot21.into()); - assert_eq!(0x0040_0000u32, PcrSlot::Slot22.into()); - assert_eq!(0x0080_0000u32, PcrSlot::Slot23.into()); + convert_to_u32_test!(0x0000_0001u32, PcrSlot::Slot0); + convert_to_u32_test!(0x0000_0002u32, PcrSlot::Slot1); + convert_to_u32_test!(0x0000_0004u32, PcrSlot::Slot2); + convert_to_u32_test!(0x0000_0008u32, PcrSlot::Slot3); + convert_to_u32_test!(0x0000_0010u32, PcrSlot::Slot4); + convert_to_u32_test!(0x0000_0020u32, PcrSlot::Slot5); + convert_to_u32_test!(0x0000_0040u32, PcrSlot::Slot6); + convert_to_u32_test!(0x0000_0080u32, PcrSlot::Slot7); + + convert_to_u32_test!(0x0000_0100u32, PcrSlot::Slot8); + convert_to_u32_test!(0x0000_0200u32, PcrSlot::Slot9); + convert_to_u32_test!(0x0000_0400u32, PcrSlot::Slot10); + convert_to_u32_test!(0x0000_0800u32, PcrSlot::Slot11); + convert_to_u32_test!(0x0000_1000u32, PcrSlot::Slot12); + convert_to_u32_test!(0x0000_2000u32, PcrSlot::Slot13); + convert_to_u32_test!(0x0000_4000u32, PcrSlot::Slot14); + convert_to_u32_test!(0x0000_8000u32, PcrSlot::Slot15); + + convert_to_u32_test!(0x0001_0000u32, PcrSlot::Slot16); + convert_to_u32_test!(0x0002_0000u32, PcrSlot::Slot17); + convert_to_u32_test!(0x0004_0000u32, PcrSlot::Slot18); + convert_to_u32_test!(0x0008_0000u32, PcrSlot::Slot19); + convert_to_u32_test!(0x0010_0000u32, PcrSlot::Slot20); + convert_to_u32_test!(0x0020_0000u32, PcrSlot::Slot21); + convert_to_u32_test!(0x0040_0000u32, PcrSlot::Slot22); + convert_to_u32_test!(0x0080_0000u32, PcrSlot::Slot23); } macro_rules! convert_from_u32_test { ($value:expr, $slot_number:path) => {