Skip to content

Commit 362ba2c

Browse files
committed
Adds tests for display trait implementation TpmFormatZeroWarningResponseCode.
- Changes text in display trait implementation of the TpmFormatZeroWarningResponseCode struct. - Adds test for display trait implementation of the TpmFormatZeroWarningResponseCode struct. Signed-off-by: Jesper Brynolf <[email protected]>
1 parent a572335 commit 362ba2c

File tree

2 files changed

+157
-29
lines changed

2 files changed

+157
-29
lines changed

tss-esapi/src/error/return_code/tpm/format_zero/warning.rs

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -49,35 +49,38 @@ impl std::error::Error for TpmFormatZeroWarningResponseCode {}
4949

5050
impl std::fmt::Display for TpmFormatZeroWarningResponseCode {
5151
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
52+
// These display values are taken from the specification. Some have been
53+
// changed so that the sentence starts with a capital letter and ends with
54+
// period.
5255
match self.error_number {
53-
TpmFormatZeroWarning::ContextGap => write!(f, "gap for context ID is too large"),
54-
TpmFormatZeroWarning::ObjectMemory => write!(f, "out of memory for object contexts"),
55-
TpmFormatZeroWarning::SessionMemory => write!(f, "out of memory for session contexts"),
56-
TpmFormatZeroWarning::Memory => write!(f, "out of shared object/session memory or need space for internal operations"),
57-
TpmFormatZeroWarning::SessionHandles => write!(f, "out of session handles - a session must be flushed before a new session may be created"),
58-
TpmFormatZeroWarning::ObjectHandles => write!(f, "out of object handles - the handle space for objects is depleted and a reboot is required. NOTE 1: This cannot occur on the reference implementation. NOTE 2: There is no reason why an implementation would implement a design that would delete handle space. Platform specifications are encouraged to forbid it."),
59-
TpmFormatZeroWarning::Locality => write!(f, "bad locality"),
60-
TpmFormatZeroWarning::Yielded => write!(f, "the TPM has suspended operation on the command; forward progress was made and the command may be retried. See TPM 2.0 Part 1, “Multi-tasking.” NOTE: This cannot occur on the reference implementation."),
61-
TpmFormatZeroWarning::Canceled => write!(f, "the command was canceled"),
62-
TpmFormatZeroWarning::Testing => write!(f, "TPM is performing self-tests"),
63-
TpmFormatZeroWarning::ReferenceH0 => write!(f, "the 1st handle in the handle area references a transient object or session that is not loaded"),
64-
TpmFormatZeroWarning::ReferenceH1 => write!(f, "the 2nd handle in the handle area references a transient object or session that is not loaded"),
65-
TpmFormatZeroWarning::ReferenceH2 => write!(f, "the 3rd handle in the handle area references a transient object or session that is not loaded"),
66-
TpmFormatZeroWarning::ReferenceH3 => write!(f, "the 4th handle in the handle area references a transient object or session that is not loaded"),
67-
TpmFormatZeroWarning::ReferenceH4 => write!(f, "the 5th handle in the handle area references a transient object or session that is not loaded"),
68-
TpmFormatZeroWarning::ReferenceH5 => write!(f, "the 6th handle in the handle area references a transient object or session that is not loaded"),
69-
TpmFormatZeroWarning::ReferenceH6 => write!(f, "the 7th handle in the handle area references a transient object or session that is not loaded"),
70-
TpmFormatZeroWarning::ReferenceS0 => write!(f, "the 1st authorization session handle references a session that is not loaded"),
71-
TpmFormatZeroWarning::ReferenceS1 => write!(f, "the 2nd authorization session handle references a session that is not loaded"),
72-
TpmFormatZeroWarning::ReferenceS2 => write!(f, "the 3rd authorization session handle references a session that is not loaded"),
73-
TpmFormatZeroWarning::ReferenceS3 => write!(f, "the 4th authorization session handle references a session that is not loaded"),
74-
TpmFormatZeroWarning::ReferenceS4 => write!(f, "the 5th session handle references a session that is not loaded"),
75-
TpmFormatZeroWarning::ReferenceS5 => write!(f, "the 6th session handle references a session that is not loaded"),
76-
TpmFormatZeroWarning::ReferenceS6 => write!(f, "the 7th authorization session handle references a session that is not loaded"),
77-
TpmFormatZeroWarning::NvRate => write!(f, "the TPM is rate-limiting accesses to prevent wearout of NV"),
78-
TpmFormatZeroWarning::Lockout => write!(f, "authorizations for objects subject to DA protection are not allowed at this time because the TPM is in DA lockout mode"),
79-
TpmFormatZeroWarning::Retry => write!(f, "the TPM was not able to start the command"),
80-
TpmFormatZeroWarning::NvUnavailable => write!(f, "the command may require writing of NV and NV is not current accessible"),
56+
TpmFormatZeroWarning::ContextGap => write!(f, "Gap for context ID is too large."),
57+
TpmFormatZeroWarning::ObjectMemory => write!(f, "Out of memory for object contexts."),
58+
TpmFormatZeroWarning::SessionMemory => write!(f, "Out of memory for session contexts."),
59+
TpmFormatZeroWarning::Memory => write!(f, "Out of shared object or session memory or need space for internal operations."),
60+
TpmFormatZeroWarning::SessionHandles => write!(f, "Out of session handles."),
61+
TpmFormatZeroWarning::ObjectHandles => write!(f, "Out of object handles."),
62+
TpmFormatZeroWarning::Locality => write!(f, "Bad locality."),
63+
TpmFormatZeroWarning::Yielded => write!(f, "The TPM has suspended operation on the command; forward progress was made and the command may be retried."),
64+
TpmFormatZeroWarning::Canceled => write!(f, "The command was canceled."),
65+
TpmFormatZeroWarning::Testing => write!(f, "TPM is performing self-tests."),
66+
TpmFormatZeroWarning::ReferenceH0 => write!(f, "The 1st handle in the handle area references a transient object or session that is not loaded."),
67+
TpmFormatZeroWarning::ReferenceH1 => write!(f, "The 2nd handle in the handle area references a transient object or session that is not loaded."),
68+
TpmFormatZeroWarning::ReferenceH2 => write!(f, "The 3rd handle in the handle area references a transient object or session that is not loaded."),
69+
TpmFormatZeroWarning::ReferenceH3 => write!(f, "The 4th handle in the handle area references a transient object or session that is not loaded."),
70+
TpmFormatZeroWarning::ReferenceH4 => write!(f, "The 5th handle in the handle area references a transient object or session that is not loaded."),
71+
TpmFormatZeroWarning::ReferenceH5 => write!(f, "The 6th handle in the handle area references a transient object or session that is not loaded."),
72+
TpmFormatZeroWarning::ReferenceH6 => write!(f, "The 7th handle in the handle area references a transient object or session that is not loaded."),
73+
TpmFormatZeroWarning::ReferenceS0 => write!(f, "The 1st authorization session handle references a session that is not loaded."),
74+
TpmFormatZeroWarning::ReferenceS1 => write!(f, "The 2nd authorization session handle references a session that is not loaded."),
75+
TpmFormatZeroWarning::ReferenceS2 => write!(f, "The 3rd authorization session handle references a session that is not loaded."),
76+
TpmFormatZeroWarning::ReferenceS3 => write!(f, "The 4th authorization session handle references a session that is not loaded."),
77+
TpmFormatZeroWarning::ReferenceS4 => write!(f, "The 5th session handle references a session that is not loaded."),
78+
TpmFormatZeroWarning::ReferenceS5 => write!(f, "The 6th session handle references a session that is not loaded."),
79+
TpmFormatZeroWarning::ReferenceS6 => write!(f, "The 7th authorization session handle references a session that is not loaded."),
80+
TpmFormatZeroWarning::NvRate => write!(f, "The TPM is rate-limiting accesses to prevent wearout of NV."),
81+
TpmFormatZeroWarning::Lockout => write!(f, "Authorizations for objects subject to DA protection are not allowed at this time because the TPM is in DA lockout mode."),
82+
TpmFormatZeroWarning::Retry => write!(f, "The TPM was not able to start the command."),
83+
TpmFormatZeroWarning::NvUnavailable => write!(f, "The command may require writing of NV and NV is not current accessible."),
8184
}
8285
}
8386
}

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

Lines changed: 126 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,26 @@ macro_rules! test_valid_conversion {
6060
expected_tss_rc,
6161
actual_rc.into(),
6262
"TpmFormatZeroResponseCode with {} did not convert into expected {} TSS2_RC in the TPM layer.",
63-
std::stringify!(TpmFormatZeroError::$item),
63+
std::stringify!(TpmFormatZeroWarning::$item),
6464
std::stringify!($tpm_rc),
6565
);
6666
};
6767
}
6868

69+
macro_rules! test_display_trait_impl {
70+
($expected_error_message:tt, TpmFormatZeroWarning::$zero_warning:ident) => {
71+
assert_eq!(
72+
format!(
73+
"{}",
74+
TpmFormatZeroWarningResponseCode::from(TpmFormatZeroWarning::$zero_warning)
75+
),
76+
$expected_error_message,
77+
"BaseReturnCode with {} did not produce the expected error message",
78+
std::stringify!(TpmFormatZeroWarning::$zero_warning),
79+
);
80+
};
81+
}
82+
6983
#[test]
7084
fn test_valid_conversions() {
7185
test_valid_conversion!(TPM2_RC_CONTEXT_GAP, TpmFormatZeroWarning::ContextGap);
@@ -105,3 +119,114 @@ fn test_invalid_conversions() {
105119
"Converting invalid TPM layer response code did not produce the expected error"
106120
);
107121
}
122+
123+
#[test]
124+
fn test_display_implementation() {
125+
test_display_trait_impl!(
126+
"Gap for context ID is too large.",
127+
TpmFormatZeroWarning::ContextGap
128+
);
129+
test_display_trait_impl!(
130+
"Out of memory for object contexts.",
131+
TpmFormatZeroWarning::ObjectMemory
132+
);
133+
test_display_trait_impl!(
134+
"Out of memory for session contexts.",
135+
TpmFormatZeroWarning::SessionMemory
136+
);
137+
test_display_trait_impl!(
138+
"Out of shared object or session memory or need space for internal operations.",
139+
TpmFormatZeroWarning::Memory
140+
);
141+
test_display_trait_impl!(
142+
"Out of session handles.",
143+
TpmFormatZeroWarning::SessionHandles
144+
);
145+
test_display_trait_impl!(
146+
"Out of object handles.",
147+
TpmFormatZeroWarning::ObjectHandles
148+
);
149+
test_display_trait_impl!("Bad locality.", TpmFormatZeroWarning::Locality);
150+
test_display_trait_impl!(
151+
"The TPM has suspended operation on the command; forward progress was made and the command may be retried.",
152+
TpmFormatZeroWarning::Yielded
153+
);
154+
test_display_trait_impl!("The command was canceled.", TpmFormatZeroWarning::Canceled);
155+
test_display_trait_impl!(
156+
"TPM is performing self-tests.",
157+
TpmFormatZeroWarning::Testing
158+
);
159+
test_display_trait_impl!(
160+
"The 1st handle in the handle area references a transient object or session that is not loaded.",
161+
TpmFormatZeroWarning::ReferenceH0
162+
);
163+
test_display_trait_impl!(
164+
"The 2nd handle in the handle area references a transient object or session that is not loaded.",
165+
TpmFormatZeroWarning::ReferenceH1
166+
);
167+
test_display_trait_impl!(
168+
"The 3rd handle in the handle area references a transient object or session that is not loaded.",
169+
TpmFormatZeroWarning::ReferenceH2
170+
);
171+
test_display_trait_impl!(
172+
"The 4th handle in the handle area references a transient object or session that is not loaded.",
173+
TpmFormatZeroWarning::ReferenceH3
174+
);
175+
test_display_trait_impl!(
176+
"The 5th handle in the handle area references a transient object or session that is not loaded.",
177+
TpmFormatZeroWarning::ReferenceH4
178+
);
179+
test_display_trait_impl!(
180+
"The 6th handle in the handle area references a transient object or session that is not loaded.",
181+
TpmFormatZeroWarning::ReferenceH5
182+
);
183+
test_display_trait_impl!(
184+
"The 7th handle in the handle area references a transient object or session that is not loaded.",
185+
TpmFormatZeroWarning::ReferenceH6
186+
);
187+
188+
test_display_trait_impl!(
189+
"The 1st authorization session handle references a session that is not loaded.",
190+
TpmFormatZeroWarning::ReferenceS0
191+
);
192+
test_display_trait_impl!(
193+
"The 2nd authorization session handle references a session that is not loaded.",
194+
TpmFormatZeroWarning::ReferenceS1
195+
);
196+
test_display_trait_impl!(
197+
"The 3rd authorization session handle references a session that is not loaded.",
198+
TpmFormatZeroWarning::ReferenceS2
199+
);
200+
test_display_trait_impl!(
201+
"The 4th authorization session handle references a session that is not loaded.",
202+
TpmFormatZeroWarning::ReferenceS3
203+
);
204+
test_display_trait_impl!(
205+
"The 5th session handle references a session that is not loaded.",
206+
TpmFormatZeroWarning::ReferenceS4
207+
);
208+
test_display_trait_impl!(
209+
"The 6th session handle references a session that is not loaded.",
210+
TpmFormatZeroWarning::ReferenceS5
211+
);
212+
test_display_trait_impl!(
213+
"The 7th authorization session handle references a session that is not loaded.",
214+
TpmFormatZeroWarning::ReferenceS6
215+
);
216+
test_display_trait_impl!(
217+
"The TPM is rate-limiting accesses to prevent wearout of NV.",
218+
TpmFormatZeroWarning::NvRate
219+
);
220+
test_display_trait_impl!(
221+
"Authorizations for objects subject to DA protection are not allowed at this time because the TPM is in DA lockout mode.",
222+
TpmFormatZeroWarning::Lockout
223+
);
224+
test_display_trait_impl!(
225+
"The TPM was not able to start the command.",
226+
TpmFormatZeroWarning::Retry
227+
);
228+
test_display_trait_impl!(
229+
"The command may require writing of NV and NV is not current accessible.",
230+
TpmFormatZeroWarning::NvUnavailable
231+
);
232+
}

0 commit comments

Comments
 (0)