Skip to content

Commit

Permalink
Adds tests for trait implementations in ReturnCode type.
Browse files Browse the repository at this point in the history
- Adds tests that tests the implementation of the Error trait
  in ReturnCode.

- Adds tests that tests the implementation of the Display trait
  in ReturnCode.

Signed-off-by: Jesper Brynolf <[email protected]>
  • Loading branch information
Superhepper committed Jun 8, 2023
1 parent 7598e40 commit 04de014
Show file tree
Hide file tree
Showing 2 changed files with 178 additions and 2 deletions.
4 changes: 2 additions & 2 deletions tss-esapi/tests/integration_tests/error_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use tss_esapi::{
#[test]
fn test_error_trait_implementation() {
// The Error type is only expected to forward everything to the
// underlaying error types.
// 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
Expand All @@ -39,7 +39,7 @@ fn test_error_trait_implementation() {
#[test]
fn test_display_trait_implementation() {
// The Error type is only expected to forward everything to the
// underlaying error types.
// underlying error types.
let expected_wrapper_error_kind = WrapperErrorKind::InconsistentParams;
let wrapper_error = tss_esapi::Error::WrapperError(expected_wrapper_error_kind);
assert_eq!(
Expand Down
176 changes: 176 additions & 0 deletions tss-esapi/tests/integration_tests/error_tests/return_code_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,179 @@ 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
);
}

0 comments on commit 04de014

Please sign in to comment.