Skip to content

Commit 079d6d8

Browse files
authored
Merge pull request #407 from Superhepper/format-zero-missing-tests
Adds tests for vendor specific errors.
2 parents 7f90968 + 4c3259a commit 079d6d8

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
mod tpm_format_one_argument_number_tests;
55
mod tpm_format_one_error_tests;
66
mod tpm_format_zero_error_tests;
7+
mod tpm_format_zero_vendor_specific_tests;
78
mod tpm_format_zero_warning_tests;
89
mod tpm_response_code_tests;

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Copyright 2022 Contributors to the Parsec project.
22
// SPDX-License-Identifier: Apache-2.
3+
use bitfield::bitfield;
34
use std::convert::TryFrom;
45
use tss_esapi::{
56
constants::{
@@ -145,6 +146,48 @@ fn test_invalid_conversions() {
145146
);
146147
}
147148

149+
bitfield! {
150+
pub struct ReservedBitHelper(u32);
151+
_, set_reserved: 9;
152+
}
153+
154+
#[test]
155+
fn test_conversion_of_invalid_value_with_reserved_bits_set() {
156+
// Bit 9 In the TPM format zero return code is the reserved bit.
157+
// |11|10| 9| 8 | 7| 6| 5| 4| 3| 2| 1| 0|
158+
// | W| V| R|TPM 2.0| | error number |
159+
let mut helper = ReservedBitHelper(TSS2_TPM_RC_LAYER | TPM2_RC_INITIALIZE);
160+
helper.set_reserved(true);
161+
let tss_rc_with_reserved_bit_set = helper.0;
162+
163+
assert_eq!(
164+
ReturnCode::try_from(tss_rc_with_reserved_bit_set),
165+
Err(Error::WrapperError(WrapperErrorKind::InvalidParam)),
166+
"Converting invalid TPM format zero error with reserve bit set did not result in the expected error."
167+
);
168+
}
169+
170+
bitfield! {
171+
pub struct Tpm2BitHelper(u32);
172+
_, set_tpm_2_0: 8;
173+
}
174+
175+
#[test]
176+
fn test_conversion_of_invalid_value_without_tpm2_bit_set() {
177+
// Bit 9 In the TPM format zero return code is the reserved bit.
178+
// |11|10| 9| 8 | 7| 6| 5| 4| 3| 2| 1| 0|
179+
// | W| V| R|TPM 2.0| | error number |
180+
let mut helper = Tpm2BitHelper(TSS2_TPM_RC_LAYER | TPM2_RC_INITIALIZE);
181+
helper.set_tpm_2_0(false);
182+
let tss_rc_with_tpm2_bit_clear = helper.0;
183+
184+
assert_eq!(
185+
ReturnCode::try_from(tss_rc_with_tpm2_bit_clear),
186+
Err(Error::WrapperError(WrapperErrorKind::UnsupportedParam)),
187+
"Converting invalid TPM format zero error with TPM 2.0 bit clear did not result in the expected error."
188+
);
189+
}
190+
148191
#[test]
149192
fn test_display_implementation() {
150193
test_display_trait_impl!(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2023 Contributors to the Parsec project.
2+
// SPDX-License-Identifier: Apache-2.
3+
use bitfield::bitfield;
4+
use std::convert::TryFrom;
5+
use tss_esapi::{
6+
constants::tss::{TPM2_RC_INITIALIZE, TSS2_TPM_RC_LAYER},
7+
error::{ReturnCode, TpmFormatZeroResponseCode, TpmResponseCode},
8+
};
9+
10+
bitfield! {
11+
pub struct VendorSpecificBitHelper(u32);
12+
_, set_is_vendor_specific: 10;
13+
}
14+
15+
#[test]
16+
fn test_valid_conversions() {
17+
// Bit 10 In the TPM format zero return code is the bit indicating vendor specific.
18+
// |11|10| 9| 8 | 7| 6| 5| 4| 3| 2| 1| 0|
19+
// | W| V| R|TPM 2.0| | error number |
20+
let mut helper = VendorSpecificBitHelper(TSS2_TPM_RC_LAYER | TPM2_RC_INITIALIZE);
21+
helper.set_is_vendor_specific(true);
22+
let expected_tss_rc = helper.0;
23+
24+
let actual_rc = ReturnCode::try_from(expected_tss_rc)
25+
.expect("Failed to convert TPM zero error return code value with vendor specific bit set into a Result.");
26+
27+
if let ReturnCode::Tpm(TpmResponseCode::FormatZero(
28+
TpmFormatZeroResponseCode::VendorSpecific(actual),
29+
)) = actual_rc
30+
{
31+
assert_eq!(
32+
expected_tss_rc,
33+
actual.into(),
34+
"Converting vendor specific return code did not return the original value."
35+
);
36+
} else {
37+
panic!("TPM TSS2_RC layer did no convert into ReturnCode::Tpm");
38+
}
39+
40+
assert_eq!(
41+
expected_tss_rc,
42+
actual_rc.into(),
43+
"The vendor specific return code did not convert into the expected TSS2_RC in the TPM layer."
44+
)
45+
}

0 commit comments

Comments
 (0)