|
| 1 | +// Copyright 2023 Contributors to the Parsec project. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +mod return_code_tests; |
| 4 | +mod wrapper_error_kind_tests; |
| 5 | + |
| 6 | +use std::{convert::TryFrom, error::Error}; |
| 7 | + |
| 8 | +use tss_esapi::{ |
| 9 | + constants::tss::{TPM2_RC_INITIALIZE, TSS2_TPM_RC_LAYER}, |
| 10 | + error::{ReturnCode, WrapperErrorKind}, |
| 11 | +}; |
| 12 | + |
| 13 | +#[test] |
| 14 | +fn test_error_trait_implementation() { |
| 15 | + // The Error type is only expected to forward everything to the |
| 16 | + // underlaying error types. |
| 17 | + let expected_wrapper_error_kind = WrapperErrorKind::InconsistentParams; |
| 18 | + let wrapper_error = tss_esapi::Error::WrapperError(expected_wrapper_error_kind); |
| 19 | + let actual_wrapper_error_kind = wrapper_error |
| 20 | + .source() |
| 21 | + .expect("`source()` for an Error of type WrapperError returned None."); |
| 22 | + assert_eq!( |
| 23 | + format!("{}", expected_wrapper_error_kind), |
| 24 | + format!("{}", actual_wrapper_error_kind) |
| 25 | + ); |
| 26 | + |
| 27 | + let expected_return_code = ReturnCode::try_from(TSS2_TPM_RC_LAYER | TPM2_RC_INITIALIZE) |
| 28 | + .expect("Failed to convert TSS return code into a ReturnCode object."); |
| 29 | + let tss_error = tss_esapi::Error::TssError(expected_return_code); |
| 30 | + let actual_return_code = tss_error |
| 31 | + .source() |
| 32 | + .expect("`source()` for an Error of type ReturnCode returned None."); |
| 33 | + assert_eq!( |
| 34 | + format!("{}", expected_return_code), |
| 35 | + format!("{}", actual_return_code) |
| 36 | + ); |
| 37 | +} |
| 38 | + |
| 39 | +#[test] |
| 40 | +fn test_display_trait_implementation() { |
| 41 | + // The Error type is only expected to forward everything to the |
| 42 | + // underlaying error types. |
| 43 | + let expected_wrapper_error_kind = WrapperErrorKind::InconsistentParams; |
| 44 | + let wrapper_error = tss_esapi::Error::WrapperError(expected_wrapper_error_kind); |
| 45 | + assert_eq!( |
| 46 | + format!("{}", expected_wrapper_error_kind), |
| 47 | + format!("{}", wrapper_error) |
| 48 | + ); |
| 49 | + |
| 50 | + let expected_return_code = ReturnCode::try_from(TSS2_TPM_RC_LAYER | TPM2_RC_INITIALIZE) |
| 51 | + .expect("Failed to convert TSS return code into a ReturnCode object."); |
| 52 | + let tss_error = tss_esapi::Error::TssError(expected_return_code); |
| 53 | + assert_eq!( |
| 54 | + format!("{}", expected_return_code), |
| 55 | + format!("{}", tss_error) |
| 56 | + ); |
| 57 | +} |
0 commit comments