-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #411 from Superhepper/return-code-trait-impl-tests
Adds Error and Display trait implementation tests for the Error and ReturnCode types.
- Loading branch information
Showing
4 changed files
with
244 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright 2023 Contributors to the Parsec project. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
mod return_code_tests; | ||
mod wrapper_error_kind_tests; | ||
|
||
use std::{convert::TryFrom, error::Error}; | ||
|
||
use tss_esapi::{ | ||
constants::tss::{TPM2_RC_INITIALIZE, TSS2_TPM_RC_LAYER}, | ||
error::{ReturnCode, WrapperErrorKind}, | ||
}; | ||
|
||
#[test] | ||
fn test_error_trait_implementation() { | ||
// The Error type is only expected to forward everything to the | ||
// underlying error types. | ||
let expected_wrapper_error_kind = WrapperErrorKind::InconsistentParams; | ||
let wrapper_error = tss_esapi::Error::WrapperError(expected_wrapper_error_kind); | ||
let actual_wrapper_error_kind = wrapper_error | ||
.source() | ||
.expect("`source()` for an Error of type WrapperError returned None."); | ||
assert_eq!( | ||
format!("{}", expected_wrapper_error_kind), | ||
format!("{}", actual_wrapper_error_kind) | ||
); | ||
|
||
let expected_return_code = ReturnCode::try_from(TSS2_TPM_RC_LAYER | TPM2_RC_INITIALIZE) | ||
.expect("Failed to convert TSS return code into a ReturnCode object."); | ||
let tss_error = tss_esapi::Error::TssError(expected_return_code); | ||
let actual_return_code = tss_error | ||
.source() | ||
.expect("`source()` for an Error of type ReturnCode returned None."); | ||
assert_eq!( | ||
format!("{}", expected_return_code), | ||
format!("{}", actual_return_code) | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_display_trait_implementation() { | ||
// The Error type is only expected to forward everything to the | ||
// underlying error types. | ||
let expected_wrapper_error_kind = WrapperErrorKind::InconsistentParams; | ||
let wrapper_error = tss_esapi::Error::WrapperError(expected_wrapper_error_kind); | ||
assert_eq!( | ||
format!("{}", expected_wrapper_error_kind), | ||
format!("{}", wrapper_error) | ||
); | ||
|
||
let expected_return_code = ReturnCode::try_from(TSS2_TPM_RC_LAYER | TPM2_RC_INITIALIZE) | ||
.expect("Failed to convert TSS return code into a ReturnCode object."); | ||
let tss_error = tss_esapi::Error::TssError(expected_return_code); | ||
assert_eq!( | ||
format!("{}", expected_return_code), | ||
format!("{}", tss_error) | ||
); | ||
} |
This file was deleted.
Oops, something went wrong.
187 changes: 187 additions & 0 deletions
187
tss-esapi/tests/integration_tests/error_tests/return_code_tests.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
// Copyright 2022 Contributors to the Parsec project. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
mod base_tests; | ||
mod esapi_tests; | ||
mod fapi_tests; | ||
mod muapi_tests; | ||
mod resource_manager_tests; | ||
mod resource_manager_tpm_tests; | ||
mod sapi_tests; | ||
mod tcti_tests; | ||
mod tpm_tests; | ||
|
||
use tss_esapi::{ | ||
constants::tss::{ | ||
TPM2_RC_INITIALIZE, TSS2_BASE_RC_BAD_REFERENCE, TSS2_BASE_RC_BAD_SEQUENCE, | ||
TSS2_ESYS_RC_LAYER, TSS2_FEATURE_RC_LAYER, TSS2_MU_RC_LAYER, TSS2_RESMGR_RC_LAYER, | ||
TSS2_RESMGR_TPM_RC_LAYER, TSS2_SYS_RC_LAYER, TSS2_TCTI_RC_LAYER, TSS2_TPM_RC_LAYER, | ||
}, | ||
error::{ | ||
BaseReturnCode, EsapiReturnCode, FapiReturnCode, MuapiReturnCode, ReturnCode, | ||
SapiReturnCode, TctiReturnCode, TpmResponseCode, | ||
}, | ||
}; | ||
|
||
use std::{convert::TryFrom, error::Error}; | ||
|
||
macro_rules! test_error_trait_impl { | ||
($native_rc:ident, $tss_rc_layer:ident, $tss_rc:ident) => { | ||
let return_code = ReturnCode::try_from($tss_rc_layer | $tss_rc).unwrap_or_else(|_| { | ||
panic!( | ||
"Failed to convert {} error in {} layer return code into a ReturnCode object.", | ||
std::stringify!($tss_rc), | ||
std::stringify!($tss_rc_layer) | ||
) | ||
}); | ||
|
||
let response_code = $native_rc::try_from(u16::try_from($tss_rc).unwrap_or_else(|_| { | ||
panic!( | ||
"Failed to convert {} into a u16 value.", | ||
std::stringify!($tss_rc) | ||
) | ||
})) | ||
.unwrap_or_else(|_| { | ||
panic!( | ||
"Failed to convert {} into a {}.", | ||
std::stringify!($tss_rc), | ||
std::any::type_name::<$native_rc>() | ||
) | ||
}); | ||
|
||
assert_eq!( | ||
format!( | ||
"{}", | ||
return_code.source().unwrap_or_else(|| { | ||
panic!( | ||
"`source` function for a {} layer return code should not return None.", | ||
std::stringify!($tss_rc_layer) | ||
) | ||
}) | ||
), | ||
format!("{}", response_code), | ||
"Tss2_RC with from {} error in {} layer did not convert into the expected type {}", | ||
std::stringify!($tss_rc), | ||
std::stringify!($tss_rc_layer), | ||
std::any::type_name::<$native_rc>() | ||
); | ||
}; | ||
} | ||
|
||
#[test] | ||
fn test_error_trait_implementation() { | ||
test_error_trait_impl!(TpmResponseCode, TSS2_TPM_RC_LAYER, TPM2_RC_INITIALIZE); | ||
test_error_trait_impl!( | ||
FapiReturnCode, | ||
TSS2_FEATURE_RC_LAYER, | ||
TSS2_BASE_RC_BAD_SEQUENCE | ||
); | ||
test_error_trait_impl!( | ||
EsapiReturnCode, | ||
TSS2_ESYS_RC_LAYER, | ||
TSS2_BASE_RC_BAD_SEQUENCE | ||
); | ||
test_error_trait_impl!(SapiReturnCode, TSS2_SYS_RC_LAYER, TSS2_BASE_RC_BAD_SEQUENCE); | ||
test_error_trait_impl!( | ||
MuapiReturnCode, | ||
TSS2_MU_RC_LAYER, | ||
TSS2_BASE_RC_BAD_REFERENCE | ||
); | ||
test_error_trait_impl!( | ||
TctiReturnCode, | ||
TSS2_TCTI_RC_LAYER, | ||
TSS2_BASE_RC_BAD_REFERENCE | ||
); | ||
test_error_trait_impl!( | ||
BaseReturnCode, | ||
TSS2_RESMGR_RC_LAYER, | ||
TSS2_BASE_RC_BAD_SEQUENCE | ||
); | ||
test_error_trait_impl!( | ||
TpmResponseCode, | ||
TSS2_RESMGR_TPM_RC_LAYER, | ||
TPM2_RC_INITIALIZE | ||
); | ||
} | ||
|
||
macro_rules! test_display_trait_impl { | ||
($expected_error_message:tt, $native_rc:ident, $tss_rc_layer:ident, $tss_rc:ident) => { | ||
let return_code = ReturnCode::try_from($tss_rc_layer | $tss_rc).unwrap_or_else(|_| { | ||
panic!( | ||
"Failed to convert {} error in {} layer return code into a ReturnCode object.", | ||
std::stringify!($tss_rc), | ||
std::stringify!($tss_rc_layer) | ||
) | ||
}); | ||
|
||
let response_code = $native_rc::try_from(u16::try_from($tss_rc).unwrap_or_else(|_| { | ||
panic!( | ||
"Failed to convert {} into a u16 value.", | ||
std::stringify!($tss_rc) | ||
) | ||
})) | ||
.unwrap_or_else(|_| { | ||
panic!( | ||
"Failed to convert {} into a {}.", | ||
std::stringify!($tss_rc), | ||
std::any::type_name::<$native_rc>() | ||
) | ||
}); | ||
|
||
assert_eq!( | ||
format!("{} {}", $expected_error_message, response_code), | ||
format!("{}", return_code) | ||
); | ||
}; | ||
} | ||
|
||
#[test] | ||
fn test_display_trait_implementation() { | ||
test_display_trait_impl!( | ||
"TSS Layer: TPM, Code: 0x00000100, Message:", | ||
TpmResponseCode, | ||
TSS2_TPM_RC_LAYER, | ||
TPM2_RC_INITIALIZE | ||
); | ||
test_display_trait_impl!( | ||
"TSS Layer: FAPI, Code: 0x00060007, Message:", | ||
FapiReturnCode, | ||
TSS2_FEATURE_RC_LAYER, | ||
TSS2_BASE_RC_BAD_SEQUENCE | ||
); | ||
test_display_trait_impl!( | ||
"TSS Layer: ESAPI, Code: 0x00070007, Message:", | ||
EsapiReturnCode, | ||
TSS2_ESYS_RC_LAYER, | ||
TSS2_BASE_RC_BAD_SEQUENCE | ||
); | ||
test_display_trait_impl!( | ||
"TSS Layer: SAPI, Code: 0x00080007, Message:", | ||
SapiReturnCode, | ||
TSS2_SYS_RC_LAYER, | ||
TSS2_BASE_RC_BAD_SEQUENCE | ||
); | ||
test_display_trait_impl!( | ||
"TSS Layer: MUAPI, Code: 0x00090005, Message:", | ||
MuapiReturnCode, | ||
TSS2_MU_RC_LAYER, | ||
TSS2_BASE_RC_BAD_REFERENCE | ||
); | ||
test_display_trait_impl!( | ||
"TSS Layer: TCTI, Code: 0x000A0005, Message:", | ||
TctiReturnCode, | ||
TSS2_TCTI_RC_LAYER, | ||
TSS2_BASE_RC_BAD_REFERENCE | ||
); | ||
test_display_trait_impl!( | ||
"TSS Layer: RESOURCE MANAGER, Code: 0x000B0007, Message:", | ||
BaseReturnCode, | ||
TSS2_RESMGR_RC_LAYER, | ||
TSS2_BASE_RC_BAD_SEQUENCE | ||
); | ||
test_display_trait_impl!( | ||
"TSS Layer: TPM RESOURCE MANAGER, Code: 0x000C0100, Message:", | ||
TpmResponseCode, | ||
TSS2_RESMGR_TPM_RC_LAYER, | ||
TPM2_RC_INITIALIZE | ||
); | ||
} |
11 changes: 0 additions & 11 deletions
11
tss-esapi/tests/integration_tests/error_tests/return_code_tests/mod.rs
This file was deleted.
Oops, something went wrong.