Skip to content

Commit

Permalink
Fixes some causes for type ambiguity in tests.
Browse files Browse the repository at this point in the history
- 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 parallaxsecond#466.

Signed-off-by: Jesper Brynolf <jesper.brynolf@gmail.com>
  • Loading branch information
Superhepper committed Nov 25, 2023
1 parent 29b7278 commit a84d3e3
Show file tree
Hide file tree
Showing 25 changed files with 159 additions and 107 deletions.
3 changes: 2 additions & 1 deletion tss-esapi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
);
}
Expand Down Expand Up @@ -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"
);
}
Expand Down Expand Up @@ -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"
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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),
);
};
Expand All @@ -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,
);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -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!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use tss_esapi::{
BaseError,
},
error::{BaseReturnCode, FapiReturnCode, ReturnCode},
tss2_esys::TSS2_RC,
Error, WrapperErrorKind,
};

Expand Down Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use tss_esapi::{
BaseError,
},
error::{BaseReturnCode, MuapiReturnCode, ReturnCode},
tss2_esys::TSS2_RC,
Error, WrapperErrorKind,
};

Expand Down Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use tss_esapi::{
},
error::{BaseReturnCode, ReturnCode},
tss2_esys::TSS2_LAYER_IMPLEMENTATION_SPECIFIC_OFFSET,
tss2_esys::TSS2_RC,
Error, WrapperErrorKind,
};

Expand Down Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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"
);
}
Expand All @@ -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"
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use tss_esapi::{
BaseError,
},
error::{BaseReturnCode, ReturnCode, SapiReturnCode},
tss2_esys::TSS2_RC,
Error, WrapperErrorKind,
};

Expand Down Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use tss_esapi::{
BaseError,
},
error::{BaseReturnCode, ReturnCode, TctiReturnCode},
tss2_esys::TSS2_RC,
Error, WrapperErrorKind,
};

Expand Down Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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."
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use tss_esapi::{
ReturnCode, TpmFormatZeroErrorResponseCode, TpmFormatZeroResponseCode,
TpmFormatZeroWarningResponseCode, TpmResponseCode,
},
tss2_esys::TSS2_RC,
};

bitfield! {
Expand All @@ -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 {
Expand All @@ -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."
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use tss_esapi::{
error::{
ReturnCode, TpmFormatZeroErrorResponseCode, TpmFormatZeroResponseCode, TpmResponseCode,
},
tss2_esys::TSS2_RC,
Error, WrapperErrorKind,
};

Expand Down Expand Up @@ -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::<TpmFormatZeroResponseCode>(),
std::stringify!(TpmFormatZeroError::$item),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use tss_esapi::{
error::{
ReturnCode, TpmFormatZeroResponseCode, TpmFormatZeroWarningResponseCode, TpmResponseCode,
},
tss2_esys::TSS2_RC,
Error, WrapperErrorKind,
};

Expand Down Expand Up @@ -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),
Expand Down
Loading

0 comments on commit a84d3e3

Please sign in to comment.