|
| 1 | +// Copyright 2023 Contributors to the Parsec project. |
| 2 | +// SPDX-License-Identifier: Apache-2. |
| 3 | + |
| 4 | +mod tpm_format_zero_error_tests; |
| 5 | +mod tpm_format_zero_warning_tests; |
| 6 | + |
| 7 | +use bitfield::bitfield; |
| 8 | +use std::{convert::TryFrom, error::Error}; |
| 9 | +use tss_esapi::{ |
| 10 | + constants::tss::{ |
| 11 | + TPM2_RC_AUTHSIZE, TPM2_RC_CONTEXT_GAP, TPM2_RC_INITIALIZE, TSS2_TPM_RC_LAYER, |
| 12 | + }, |
| 13 | + error::{ |
| 14 | + ReturnCode, TpmFormatZeroErrorResponseCode, TpmFormatZeroResponseCode, |
| 15 | + TpmFormatZeroWarningResponseCode, TpmResponseCode, |
| 16 | + }, |
| 17 | +}; |
| 18 | + |
| 19 | +bitfield! { |
| 20 | + pub struct VendorSpecificBitHelper(u32); |
| 21 | + _, set_is_vendor_specific: 10; |
| 22 | +} |
| 23 | + |
| 24 | +#[test] |
| 25 | +fn test_vendor_specific_valid_conversions() { |
| 26 | + // Bit 10 In the TPM format zero return code is the bit indicating vendor specific. |
| 27 | + // |11|10| 9| 8 | 7| 6| 5| 4| 3| 2| 1| 0| |
| 28 | + // | W| V| R|TPM 2.0| | error number | |
| 29 | + let mut helper = VendorSpecificBitHelper(TSS2_TPM_RC_LAYER | TPM2_RC_INITIALIZE); |
| 30 | + helper.set_is_vendor_specific(true); |
| 31 | + let expected_tss_rc = helper.0; |
| 32 | + |
| 33 | + let actual_rc = ReturnCode::try_from(expected_tss_rc) |
| 34 | + .expect("Failed to convert TPM zero error return code value with vendor specific bit set into a ReturnCode."); |
| 35 | + |
| 36 | + if let ReturnCode::Tpm(TpmResponseCode::FormatZero( |
| 37 | + TpmFormatZeroResponseCode::VendorSpecific(actual), |
| 38 | + )) = actual_rc |
| 39 | + { |
| 40 | + assert_eq!( |
| 41 | + expected_tss_rc, |
| 42 | + actual.into(), |
| 43 | + "Converting vendor specific return code did not return the original value." |
| 44 | + ); |
| 45 | + } else { |
| 46 | + panic!("TPM TSS2_RC layer did no convert into ReturnCode::Tpm"); |
| 47 | + } |
| 48 | + |
| 49 | + assert_eq!( |
| 50 | + expected_tss_rc, |
| 51 | + actual_rc.into(), |
| 52 | + "The vendor specific return code did not convert into the expected TSS2_RC in the TPM layer." |
| 53 | + ) |
| 54 | +} |
| 55 | + |
| 56 | +#[test] |
| 57 | +fn test_vendor_specific_error_trait_implementation() { |
| 58 | + // Bit 10 In the TPM format zero return code is the bit indicating vendor specific. |
| 59 | + // |11|10| 9| 8 | 7| 6| 5| 4| 3| 2| 1| 0| |
| 60 | + // | W| V| R|TPM 2.0| | error number | |
| 61 | + let mut helper = VendorSpecificBitHelper(TSS2_TPM_RC_LAYER | TPM2_RC_INITIALIZE); |
| 62 | + helper.set_is_vendor_specific(true); |
| 63 | + let expected_tss_rc = helper.0; |
| 64 | + let expected_tss_rc_error_part = u16::try_from(expected_tss_rc) |
| 65 | + .expect("A TSS return code with the vendor specific bit set from the TPM layer should a valid u16 value."); |
| 66 | + let vendor_specific_rc = TpmFormatZeroResponseCode::VendorSpecific(expected_tss_rc_error_part); |
| 67 | + |
| 68 | + assert!( |
| 69 | + vendor_specific_rc.source().is_none(), |
| 70 | + "`source() method for vendor specific error did not return the expected value." |
| 71 | + ); |
| 72 | +} |
| 73 | + |
| 74 | +#[test] |
| 75 | +fn test_vendor_specific_display_trait_implementation() { |
| 76 | + // Bit 10 In the TPM format zero return code is the bit indicating vendor specific. |
| 77 | + // |11|10| 9| 8 | 7| 6| 5| 4| 3| 2| 1| 0| |
| 78 | + // | W| V| R|TPM 2.0| | error number | |
| 79 | + let mut helper = VendorSpecificBitHelper(TSS2_TPM_RC_LAYER | TPM2_RC_INITIALIZE); |
| 80 | + helper.set_is_vendor_specific(true); |
| 81 | + let expected_tss_rc = helper.0; |
| 82 | + let expected_tss_rc_error_part = u16::try_from(expected_tss_rc) |
| 83 | + .expect("A TSS return code with the vendor specific bit set from the TPM layer should a valid u16 value."); |
| 84 | + let vendor_specific_rc = TpmFormatZeroResponseCode::VendorSpecific(expected_tss_rc_error_part); |
| 85 | + |
| 86 | + assert_eq!( |
| 87 | + "Vendor specific error.", |
| 88 | + format!("{}", vendor_specific_rc), |
| 89 | + "The vendor specific return code did not produce the expected error message." |
| 90 | + ); |
| 91 | +} |
| 92 | + |
| 93 | +bitfield! { |
| 94 | + pub struct ErrorNumberHelper(u32); |
| 95 | + u8, error_number, _: 6, 0; |
| 96 | +} |
| 97 | + |
| 98 | +macro_rules! test_display_trait_impl { |
| 99 | + ($source_rc:ident, $tss_rc:ident) => { |
| 100 | + let value = u16::try_from(TSS2_TPM_RC_LAYER | $tss_rc) |
| 101 | + .unwrap_or_else(|_| { |
| 102 | + panic!( |
| 103 | + "It should be possible to convert the valid TSS2 response code {} from the TPM layer into a u16 value.", |
| 104 | + std::stringify!($tss_rc)); |
| 105 | + }); |
| 106 | + |
| 107 | + let response_code = TpmFormatZeroResponseCode::try_from(value) |
| 108 | + .unwrap_or_else(|_| { |
| 109 | + panic!( |
| 110 | + "It should be possible to convert a u16 value representing a valid TSS2 response code {} from the TPM layer into a `{}` object.", |
| 111 | + std::stringify!($tss_rc), |
| 112 | + std::any::type_name::<TpmFormatZeroResponseCode>()); |
| 113 | + }); |
| 114 | + |
| 115 | + let helper = ErrorNumberHelper(TSS2_TPM_RC_LAYER | $tss_rc); |
| 116 | + |
| 117 | + let source_response_code = $source_rc::try_from(helper.error_number()) |
| 118 | + .unwrap_or_else(|_| { |
| 119 | + panic!( |
| 120 | + "It should be possible to convert the bits 0-6 a value representing a valid TSS2 response code {} from the TPM layer into a `{}` object.", |
| 121 | + std::stringify!($tss_rc), |
| 122 | + std::any::type_name::<$source_rc>()); |
| 123 | + }); |
| 124 | + |
| 125 | + assert_eq!( |
| 126 | + format!("{}", response_code), |
| 127 | + format!("{}", source_response_code) |
| 128 | + ); |
| 129 | + }; |
| 130 | +} |
| 131 | + |
| 132 | +#[test] |
| 133 | +fn test_error_display_implementation() { |
| 134 | + test_display_trait_impl!(TpmFormatZeroWarningResponseCode, TPM2_RC_CONTEXT_GAP); |
| 135 | + test_display_trait_impl!(TpmFormatZeroErrorResponseCode, TPM2_RC_AUTHSIZE); |
| 136 | +} |
| 137 | + |
| 138 | +macro_rules! test_error_trait_impl { |
| 139 | + ($source_rc:ident, $tss_rc:ident) => { |
| 140 | + let value = u16::try_from(TSS2_TPM_RC_LAYER | $tss_rc) |
| 141 | + .unwrap_or_else(|_| { |
| 142 | + panic!( |
| 143 | + "It should be possible to convert the valid TSS2 response code {} from the TPM layer into a u16 value.", |
| 144 | + std::stringify!($tss_rc)); |
| 145 | + }); |
| 146 | + |
| 147 | + let response_code = TpmFormatZeroResponseCode::try_from(value) |
| 148 | + .unwrap_or_else(|_| { |
| 149 | + panic!( |
| 150 | + "It should be possible to convert a u16 value representing a valid TSS2 response code {} from the TPM layer into a `{}` object.", |
| 151 | + std::stringify!($tss_rc), |
| 152 | + std::any::type_name::<TpmFormatZeroResponseCode>()); |
| 153 | + }); |
| 154 | + |
| 155 | + let helper = ErrorNumberHelper(TSS2_TPM_RC_LAYER | $tss_rc); |
| 156 | + |
| 157 | + let actual_source_rc = response_code.source() |
| 158 | + .unwrap_or_else(|| { |
| 159 | + panic!( |
| 160 | + "The `{}` object produced from the valid TSS2 response code {} from the TPM layer should have a source.", |
| 161 | + std::any::type_name::<TpmFormatZeroResponseCode>(), |
| 162 | + std::stringify!($tss_rc)); |
| 163 | + }); |
| 164 | + |
| 165 | + let expected_source_rc = $source_rc::try_from(helper.error_number()) |
| 166 | + .unwrap_or_else(|_| { |
| 167 | + panic!( |
| 168 | + "It should be possible to convert the bits 0-6 a value representing a valid TSS2 response code {} from the TPM layer into a `{}` object.", |
| 169 | + std::stringify!($tss_rc), |
| 170 | + std::any::type_name::<$source_rc>()); |
| 171 | + }); |
| 172 | + |
| 173 | + assert_eq!( |
| 174 | + format!("{}", actual_source_rc), |
| 175 | + format!("{}", expected_source_rc) |
| 176 | + ); |
| 177 | + }; |
| 178 | +} |
| 179 | + |
| 180 | +#[test] |
| 181 | +fn test_error_trait_implementation() { |
| 182 | + test_error_trait_impl!(TpmFormatZeroWarningResponseCode, TPM2_RC_CONTEXT_GAP); |
| 183 | + test_error_trait_impl!(TpmFormatZeroErrorResponseCode, TPM2_RC_AUTHSIZE); |
| 184 | +} |
0 commit comments