Skip to content

Commit a84d3e3

Browse files
committed
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 <[email protected]>
1 parent 29b7278 commit a84d3e3

25 files changed

+159
-107
lines changed

tss-esapi/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
overflowing_literals,
1010
path_statements,
1111
patterns_in_fns_without_body,
12-
private_in_public,
12+
private_bounds,
13+
private_interfaces,
1314
unconditional_recursion,
1415
unused,
1516
unused_allocation,

tss-esapi/tests/integration_tests/attributes_tests/command_code_attributes_tests.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ fn test_conversions_non_vendor_specific() {
9292

9393
assert_eq!(
9494
expected.0,
95-
command_code_attributes.into(),
95+
TPMA_CC::from(command_code_attributes),
9696
"CommandCodeAttributes did not convert into the expected TPMA_CC value"
9797
);
9898
}
@@ -162,7 +162,7 @@ fn test_conversions_vendor_specific() {
162162

163163
assert_eq!(
164164
expected.0,
165-
command_code_attributes.into(),
165+
TPMA_CC::from(command_code_attributes),
166166
"CommandCodeAttributes did not convert into the expected TPMA_CC value"
167167
);
168168
}
@@ -307,7 +307,7 @@ fn test_builder() {
307307

308308
assert_eq!(
309309
expected.0,
310-
actual.into(),
310+
TPMA_CC::from(actual),
311311
"CommandCodeAttributes built using the builder did not convert into the expected TPMA_CC value"
312312
);
313313
}

tss-esapi/tests/integration_tests/attributes_tests/session_attributes_tests.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright 2022 Contributors to the Parsec project.
22
// SPDX-License-Identifier: Apache-2.0
3-
use std::convert::{TryFrom, TryInto};
3+
use std::convert::TryFrom;
44
use tss_esapi::{
55
attributes::{SessionAttributes, SessionAttributesBuilder, SessionAttributesMask},
66
tss2_esys::TPMA_SESSION,
@@ -18,7 +18,7 @@ macro_rules! test_valid_conversion {
1818
);
1919
assert_eq!(
2020
tpma_session,
21-
session_attributes.try_into().expect("Failed to convert SessionAttributes into TPMA_SESSION_ATTRIBUTE."),
21+
TPMA_SESSION::try_from(session_attributes).expect("Failed to convert SessionAttributes into TPMA_SESSION_ATTRIBUTE."),
2222
"Converting session attributes with {} set did not convert into the expected TPMA_SESSION value", std::stringify!($method),
2323
);
2424
};
@@ -30,7 +30,7 @@ macro_rules! test_valid_mask_conversion {
3030
let session_attributes_mask = SessionAttributesMask::try_from(tpma_session).expect("Failed to convert TPMA_SESSION into SessionAttributesMask");
3131
assert_eq!(
3232
tpma_session,
33-
session_attributes_mask.try_into().expect("Failed to convert SessionAttributesMask into TPMA_SESSION"),
33+
TPMA_SESSION::try_from(session_attributes_mask).expect("Failed to convert SessionAttributesMask into TPMA_SESSION"),
3434
"Converting session attributes mask with {} set did not convert into the expected TPMA_SESSION value", $attribute,
3535
);
3636
};

tss-esapi/tests/integration_tests/constants_tests/algorithm_tests.rs

+19-12
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
11
// Copyright 2021 Contributors to the Parsec project.
22
// SPDX-License-Identifier: Apache-2.0
33
use std::convert::TryFrom;
4-
use tss_esapi::constants::{
5-
tss::{
6-
TPM2_ALG_AES, TPM2_ALG_CAMELLIA, TPM2_ALG_CBC, TPM2_ALG_CFB, TPM2_ALG_CMAC, TPM2_ALG_CTR,
7-
TPM2_ALG_ECB, TPM2_ALG_ECC, TPM2_ALG_ECDAA, TPM2_ALG_ECDH, TPM2_ALG_ECDSA, TPM2_ALG_ECMQV,
8-
TPM2_ALG_ECSCHNORR, TPM2_ALG_ERROR, TPM2_ALG_HMAC, TPM2_ALG_KDF1_SP800_108,
9-
TPM2_ALG_KDF1_SP800_56A, TPM2_ALG_KDF2, TPM2_ALG_KEYEDHASH, TPM2_ALG_MGF1, TPM2_ALG_NULL,
10-
TPM2_ALG_OAEP, TPM2_ALG_OFB, TPM2_ALG_RSA, TPM2_ALG_RSAES, TPM2_ALG_RSAPSS,
11-
TPM2_ALG_RSASSA, TPM2_ALG_SHA1, TPM2_ALG_SHA256, TPM2_ALG_SHA384, TPM2_ALG_SHA3_256,
12-
TPM2_ALG_SHA3_384, TPM2_ALG_SHA3_512, TPM2_ALG_SHA512, TPM2_ALG_SM2, TPM2_ALG_SM3_256,
13-
TPM2_ALG_SM4, TPM2_ALG_SYMCIPHER, TPM2_ALG_TDES, TPM2_ALG_XOR,
4+
use tss_esapi::{
5+
constants::{
6+
tss::{
7+
TPM2_ALG_AES, TPM2_ALG_CAMELLIA, TPM2_ALG_CBC, TPM2_ALG_CFB, TPM2_ALG_CMAC,
8+
TPM2_ALG_CTR, TPM2_ALG_ECB, TPM2_ALG_ECC, TPM2_ALG_ECDAA, TPM2_ALG_ECDH,
9+
TPM2_ALG_ECDSA, TPM2_ALG_ECMQV, TPM2_ALG_ECSCHNORR, TPM2_ALG_ERROR, TPM2_ALG_HMAC,
10+
TPM2_ALG_KDF1_SP800_108, TPM2_ALG_KDF1_SP800_56A, TPM2_ALG_KDF2, TPM2_ALG_KEYEDHASH,
11+
TPM2_ALG_MGF1, TPM2_ALG_NULL, TPM2_ALG_OAEP, TPM2_ALG_OFB, TPM2_ALG_RSA,
12+
TPM2_ALG_RSAES, TPM2_ALG_RSAPSS, TPM2_ALG_RSASSA, TPM2_ALG_SHA1, TPM2_ALG_SHA256,
13+
TPM2_ALG_SHA384, TPM2_ALG_SHA3_256, TPM2_ALG_SHA3_384, TPM2_ALG_SHA3_512,
14+
TPM2_ALG_SHA512, TPM2_ALG_SM2, TPM2_ALG_SM3_256, TPM2_ALG_SM4, TPM2_ALG_SYMCIPHER,
15+
TPM2_ALG_TDES, TPM2_ALG_XOR,
16+
},
17+
AlgorithmIdentifier,
1418
},
15-
AlgorithmIdentifier,
19+
tss2_esys::TPM2_ALG_ID,
1620
};
1721
macro_rules! test_conversion {
1822
($tpm_alg_id:ident, $algorithm:ident) => {
19-
assert_eq!($tpm_alg_id, AlgorithmIdentifier::$algorithm.into());
23+
assert_eq!(
24+
$tpm_alg_id,
25+
TPM2_ALG_ID::from(AlgorithmIdentifier::$algorithm)
26+
);
2027
assert_eq!(
2128
AlgorithmIdentifier::$algorithm,
2229
AlgorithmIdentifier::try_from($tpm_alg_id).expect(&format!(

tss-esapi/tests/integration_tests/constants_tests/return_code_tests/base_return_code_tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ macro_rules! test_valid_conversion {
3535
($tss_rc_base:ident, $base_error_item:ident) => {
3636
assert_eq!(
3737
$tss_rc_base as u16,
38-
BaseError::$base_error_item.into(),
38+
u16::from(BaseError::$base_error_item),
3939
"Failed to convert {} into the expected TSS2_RC value {}",
4040
std::stringify!(BaseError::$base_error_item),
4141
std::stringify!($tss_rc_base),

tss-esapi/tests/integration_tests/constants_tests/return_code_tests/return_code_layer_tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ macro_rules! test_valid_conversion {
2727

2828
assert_eq!(
2929
tss_rc_layer_unshifted,
30-
ReturnCodeLayer::$return_code_layer.into(),
30+
u8::from(ReturnCodeLayer::$return_code_layer),
3131
"Conversion of {} into TSS_RC did not result in the expected {}",
3232
std::stringify!(ReturnCodeLayer::$return_code_layer),
3333
std::stringify!($tss_rc_layer)

tss-esapi/tests/integration_tests/constants_tests/return_code_tests/tpm_format_one_error_tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ macro_rules! test_valid_conversion {
3030
let tpm_rc = TpmFormatOneRc($tpm_fmt1_rc as u16);
3131
assert_eq!(
3232
tpm_rc.error_number(),
33-
TpmFormatOneError::$item.into(),
33+
u8::from(TpmFormatOneError::$item),
3434
"Conversion of {} into a u16 value without TPM2_RC_FMT1 did not produce the expected value {}",
3535
std::stringify!(TpmFormatOneError::$item),
3636
tpm_rc.error_number()

tss-esapi/tests/integration_tests/constants_tests/return_code_tests/tpm_format_zero_error_tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ macro_rules! test_valid_conversion {
3131
let tpm_rc = TpmFormatZeroErrorRc($tpm_ver1_rc as u16);
3232
assert_eq!(
3333
tpm_rc.error_number(),
34-
TpmFormatZeroError::$item.into(),
34+
u8::from(TpmFormatZeroError::$item),
3535
"Conversion of {} into a u16 value without TPM2_RC_VER1 did not produce the expected value {}",
3636
std::stringify!(TpmFormatZeroError::$item),
3737
tpm_rc.error_number()

tss-esapi/tests/integration_tests/constants_tests/return_code_tests/tpm_format_zero_warning_tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ macro_rules! test_valid_conversion {
3030
let tpm_rc = TpmFormatZeroWarningRc($tpm_warn_rc as u16);
3131
assert_eq!(
3232
tpm_rc.error_number(),
33-
TpmFormatZeroWarning::$item.into(),
33+
u8::from(TpmFormatZeroWarning::$item),
3434
"Conversion of {} into a u16 value without TPM2_RC_VER1 did not produce the expected value {}",
3535
std::stringify!(TpmFormatZeroWarning::$item),
3636
tpm_rc.error_number()

tss-esapi/tests/integration_tests/context_tests/tpm_commands/integrity_collection_pcr_tests.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ mod test_pcr_read {
151151
use tss_esapi::{
152152
interface_types::algorithm::HashingAlgorithm,
153153
structures::{PcrSelectionListBuilder, PcrSlot},
154-
tss2_esys::{TPM2_SHA256_DIGEST_SIZE, TPML_PCR_SELECTION},
154+
tss2_esys::{TPM2_SHA256_DIGEST_SIZE, TPMI_ALG_HASH, TPML_PCR_SELECTION},
155155
};
156156

157157
#[test]
@@ -167,7 +167,10 @@ mod test_pcr_read {
167167
assert_eq!(pcr_selection_list.len(), 1);
168168
assert_eq!(input.count as usize, pcr_selection_list.len());
169169
assert_eq!(input.pcrSelections[0].sizeofSelect, 3);
170-
assert_eq!(input.pcrSelections[0].hash, HashingAlgorithm::Sha256.into());
170+
assert_eq!(
171+
input.pcrSelections[0].hash,
172+
TPMI_ALG_HASH::from(HashingAlgorithm::Sha256)
173+
);
171174
assert_eq!(input.pcrSelections[0].pcrSelect[0], 0b0000_0001);
172175
assert_eq!(input.pcrSelections[0].pcrSelect[1], 0b0000_0000);
173176
assert_eq!(input.pcrSelections[0].pcrSelect[2], 0b0000_0000);

tss-esapi/tests/integration_tests/error_tests/return_code_tests/esapi_tests.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use tss_esapi::{
2222
error::{BaseReturnCode, EsapiReturnCode, ReturnCode},
2323
interface_types::{algorithm::HashingAlgorithm, resource_handles::Hierarchy},
2424
structures::{Auth, SymmetricDefinition},
25+
tss2_esys::TSS2_RC,
2526
Error, WrapperErrorKind,
2627
};
2728

@@ -74,7 +75,7 @@ macro_rules! test_valid_conversion {
7475

7576
assert_eq!(
7677
expected_tss_rc,
77-
actual_rc.into(),
78+
TSS2_RC::from(actual_rc),
7879
"EsapiReturnCode with {} did not convert into expected {} TSS2_RC in the ESAPI layer.",
7980
std::stringify!(BaseError::$base_error),
8081
std::stringify!($tss_rc_base_error),

tss-esapi/tests/integration_tests/error_tests/return_code_tests/fapi_tests.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use tss_esapi::{
2424
BaseError,
2525
},
2626
error::{BaseReturnCode, FapiReturnCode, ReturnCode},
27+
tss2_esys::TSS2_RC,
2728
Error, WrapperErrorKind,
2829
};
2930

@@ -76,7 +77,7 @@ macro_rules! test_valid_conversion {
7677

7778
assert_eq!(
7879
expected_tss_rc,
79-
actual_rc.into(),
80+
TSS2_RC::from(actual_rc),
8081
"FapiReturnCode with {} did not convert into expected {} TSS2_RC in the FAPI layer.",
8182
std::stringify!(BaseError::$base_error),
8283
std::stringify!($tss_rc_base_error),

tss-esapi/tests/integration_tests/error_tests/return_code_tests/muapi_tests.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use tss_esapi::{
1212
BaseError,
1313
},
1414
error::{BaseReturnCode, MuapiReturnCode, ReturnCode},
15+
tss2_esys::TSS2_RC,
1516
Error, WrapperErrorKind,
1617
};
1718

@@ -64,7 +65,7 @@ macro_rules! test_valid_conversion {
6465

6566
assert_eq!(
6667
expected_tss_rc,
67-
actual_rc.into(),
68+
TSS2_RC::from(actual_rc),
6869
"{} did not convert into expected {} in TSS2_RC MUAPI layer.",
6970
std::stringify!(ReturnCode::Mu(MuapiReturnCode::$muapi_rc_item)),
7071
std::stringify!($tss_rc_base_error),

tss-esapi/tests/integration_tests/error_tests/return_code_tests/resource_manager_tests.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use tss_esapi::{
3030
},
3131
error::{BaseReturnCode, ReturnCode},
3232
tss2_esys::TSS2_LAYER_IMPLEMENTATION_SPECIFIC_OFFSET,
33+
tss2_esys::TSS2_RC,
3334
Error, WrapperErrorKind,
3435
};
3536

@@ -69,7 +70,7 @@ macro_rules! test_valid_conversion {
6970

7071
assert_eq!(
7172
expected_tss_rc,
72-
actual_rc.into(),
73+
TSS2_RC::from(actual_rc),
7374
"BaseReturnCode with {} did not convert into expected {} TSS2_RC in the RESMGR layer.",
7475
std::stringify!(BaseError::$base_error),
7576
std::stringify!($tss_rc_base_error),

tss-esapi/tests/integration_tests/error_tests/return_code_tests/resource_manager_tpm_tests.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::convert::TryFrom;
44
use tss_esapi::{
55
constants::tss::{TPM2_RC_ASYMMETRIC, TPM2_RC_SEQUENCE, TSS2_RESMGR_TPM_RC_LAYER},
66
error::{ReturnCode, TpmResponseCode},
7+
tss2_esys::TSS2_RC,
78
};
89

910
#[test]
@@ -23,7 +24,7 @@ fn test_valid_tpm_resmgr_format_zero_response_code() {
2324

2425
assert_eq!(
2526
expected_tss_rc,
26-
actual_rc.into(),
27+
TSS2_RC::from(actual_rc),
2728
"ReturnCode::TpmResourceManager did not convert into the expected TSS2_RC value"
2829
);
2930
}
@@ -45,7 +46,7 @@ fn test_valid_tpm_resmgr_format_one_response_code() {
4546

4647
assert_eq!(
4748
expected_tss_rc,
48-
actual_rc.into(),
49+
TSS2_RC::from(actual_rc),
4950
"ReturnCode::TpmResourceManager did not convert into the expected TSS2_RC value"
5051
);
5152
}

tss-esapi/tests/integration_tests/error_tests/return_code_tests/sapi_tests.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use tss_esapi::{
1616
BaseError,
1717
},
1818
error::{BaseReturnCode, ReturnCode, SapiReturnCode},
19+
tss2_esys::TSS2_RC,
1920
Error, WrapperErrorKind,
2021
};
2122

@@ -68,7 +69,7 @@ macro_rules! test_valid_conversion {
6869

6970
assert_eq!(
7071
expected_tss_rc,
71-
actual_rc.into(),
72+
TSS2_RC::from(actual_rc),
7273
"SapiReturnCode with {} did not convert into expected {} TSS2_RC in the SAPI layer.",
7374
std::stringify!(BaseError::$base_error),
7475
std::stringify!($tss_rc_base_error),

tss-esapi/tests/integration_tests/error_tests/return_code_tests/tcti_tests.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use tss_esapi::{
1515
BaseError,
1616
},
1717
error::{BaseReturnCode, ReturnCode, TctiReturnCode},
18+
tss2_esys::TSS2_RC,
1819
Error, WrapperErrorKind,
1920
};
2021

@@ -67,7 +68,7 @@ macro_rules! test_valid_conversion {
6768

6869
assert_eq!(
6970
expected_tss_rc,
70-
actual_rc.into(),
71+
TSS2_RC::from(actual_rc),
7172
"TctiReturnCode with {} did not convert into expected {} TSS2_RC in the TCTI layer.",
7273
std::stringify!(BaseError::$base_error),
7374
std::stringify!($tss_rc_base_error),

tss-esapi/tests/integration_tests/error_tests/return_code_tests/tpm_tests.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::{convert::TryFrom, error::Error};
88
use tss_esapi::{
99
constants::tss::{TPM2_RC_ASYMMETRIC, TPM2_RC_SEQUENCE, TSS2_TPM_RC_LAYER},
1010
error::{ReturnCode, TpmFormatOneResponseCode, TpmFormatZeroResponseCode, TpmResponseCode},
11+
tss2_esys::TSS2_RC,
1112
};
1213

1314
macro_rules! test_valid_conversions {
@@ -41,7 +42,7 @@ macro_rules! test_valid_conversions {
4142

4243
assert_eq!(
4344
expected_tss_rc,
44-
actual_rc.into(),
45+
TSS2_RC::from(actual_rc),
4546
"ReturnCode::Tpm did not convert into the expected TSS2_RC value."
4647
);
4748
};

tss-esapi/tests/integration_tests/error_tests/return_code_tests/tpm_tests/tpm_format_one_error_tests.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use tss_esapi::{
1818
},
1919
},
2020
error::{ArgumentNumber, ReturnCode, TpmFormatOneResponseCode, TpmResponseCode},
21+
tss2_esys::TSS2_RC,
2122
};
2223

2324
macro_rules! test_valid_conversions_with_all_argument_combinations {
@@ -80,7 +81,7 @@ macro_rules! test_valid_conversion {
8081

8182
assert_eq!(
8283
expected_tss_rc,
83-
actual_rc.into(),
84+
TSS2_RC::from(actual_rc),
8485
"TpmFormatOneResponseCode with {} and {} in the TPM layer did not convert into the expected TSS2_RC",
8586
std::stringify!(TpmFormatOneError::$tpm_format_one_error_item),
8687
std::stringify!(ArgumentNumber::$argument_number_item),

tss-esapi/tests/integration_tests/error_tests/return_code_tests/tpm_tests/tpm_format_zero_tests.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use tss_esapi::{
1414
ReturnCode, TpmFormatZeroErrorResponseCode, TpmFormatZeroResponseCode,
1515
TpmFormatZeroWarningResponseCode, TpmResponseCode,
1616
},
17+
tss2_esys::TSS2_RC,
1718
};
1819

1920
bitfield! {
@@ -39,7 +40,7 @@ fn test_vendor_specific_valid_conversions() {
3940
{
4041
assert_eq!(
4142
expected_tss_rc,
42-
actual.into(),
43+
TSS2_RC::from(actual),
4344
"Converting vendor specific return code did not return the original value."
4445
);
4546
} else {
@@ -48,7 +49,7 @@ fn test_vendor_specific_valid_conversions() {
4849

4950
assert_eq!(
5051
expected_tss_rc,
51-
actual_rc.into(),
52+
TSS2_RC::from(actual_rc),
5253
"The vendor specific return code did not convert into the expected TSS2_RC in the TPM layer."
5354
)
5455
}

tss-esapi/tests/integration_tests/error_tests/return_code_tests/tpm_tests/tpm_format_zero_tests/tpm_format_zero_error_tests.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use tss_esapi::{
2020
error::{
2121
ReturnCode, TpmFormatZeroErrorResponseCode, TpmFormatZeroResponseCode, TpmResponseCode,
2222
},
23+
tss2_esys::TSS2_RC,
2324
Error, WrapperErrorKind,
2425
};
2526

@@ -63,7 +64,7 @@ macro_rules! test_valid_conversion {
6364

6465
assert_eq!(
6566
expected_tss_rc,
66-
actual_rc.into(),
67+
TSS2_RC::from(actual_rc),
6768
"{} with {} did not convert into expected {} TSS2_RC in the TPM layer.",
6869
std::any::type_name::<TpmFormatZeroResponseCode>(),
6970
std::stringify!(TpmFormatZeroError::$item),

tss-esapi/tests/integration_tests/error_tests/return_code_tests/tpm_tests/tpm_format_zero_tests/tpm_format_zero_warning_tests.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use tss_esapi::{
1919
error::{
2020
ReturnCode, TpmFormatZeroResponseCode, TpmFormatZeroWarningResponseCode, TpmResponseCode,
2121
},
22+
tss2_esys::TSS2_RC,
2223
Error, WrapperErrorKind,
2324
};
2425

@@ -59,7 +60,7 @@ macro_rules! test_valid_conversion {
5960

6061
assert_eq!(
6162
expected_tss_rc,
62-
actual_rc.into(),
63+
TSS2_RC::from(actual_rc),
6364
"TpmFormatZeroResponseCode with {} did not convert into expected {} TSS2_RC in the TPM layer.",
6465
std::stringify!(TpmFormatZeroWarning::$item),
6566
std::stringify!($tpm_rc),

0 commit comments

Comments
 (0)