Skip to content

Commit ad0cfa4

Browse files
authored
Merge pull request #328 from Superhepper/more-pcr-structure-tests
Adds more pcr structure tests.
2 parents 805b40e + f325b28 commit ad0cfa4

File tree

3 files changed

+294
-15
lines changed

3 files changed

+294
-15
lines changed

tss-esapi/tests/integration_tests/structures_tests/pcr_tests/pcr_select_size_tests.rs

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
use std::convert::TryFrom;
55
use tss_esapi::{structures::PcrSelectSize, Error, WrapperErrorKind};
66

7+
fn bad_u8_values() -> Vec<u8> {
8+
let mut bad_values = Vec::<u8>::with_capacity(u8::MAX as usize);
9+
bad_values.push(0);
10+
bad_values.extend(5u8..=u8::MAX);
11+
bad_values
12+
}
13+
714
macro_rules! test_valid_conversions {
815
(PcrSelectSize::$expected:ident, $value:expr) => {
916
let expected_u8 = $value;
@@ -101,11 +108,11 @@ fn test_valid_conversions() {
101108

102109
#[test]
103110
fn test_invalid_conversions() {
104-
for value in 5..=u8::MAX {
111+
for value in bad_u8_values() {
105112
assert_eq!(
106113
Err(Error::WrapperError(WrapperErrorKind::InvalidParam)),
107114
PcrSelectSize::try_from(value),
108-
"Converting an invalid size_of_select value{} did not result in the expected error",
115+
"Converting an invalid size_of_select value {} did not result in the expected error",
109116
value,
110117
);
111118
}
@@ -121,3 +128,39 @@ fn test_default() {
121128
"PcrSelectSize did not have the expected default value",
122129
);
123130
}
131+
132+
#[test]
133+
fn test_try_parse_u8_with_invalid_values() {
134+
for value in bad_u8_values() {
135+
assert_eq!(
136+
Err(Error::WrapperError(WrapperErrorKind::InvalidParam)),
137+
PcrSelectSize::try_parse_u8(value),
138+
"try_parse_u8 using an invalid size_of_select value {} did not result in the expected error",
139+
value,
140+
);
141+
}
142+
}
143+
144+
#[test]
145+
fn test_try_parse_u32_with_invalid_values() {
146+
for value in bad_u8_values() {
147+
assert_eq!(
148+
Err(Error::WrapperError(WrapperErrorKind::InvalidParam)),
149+
PcrSelectSize::try_parse_u32(value as u32),
150+
"try_parse_u32 using an invalid size_of_select value {} did not result in the expected error",
151+
value,
152+
);
153+
}
154+
}
155+
156+
#[test]
157+
fn test_try_parse_usize_with_invalid_values() {
158+
for value in bad_u8_values() {
159+
assert_eq!(
160+
Err(Error::WrapperError(WrapperErrorKind::InvalidParam)),
161+
PcrSelectSize::try_parse_usize(value as usize),
162+
"try_parse_usize using an invalid size_of_select value {} did not result in the expected error",
163+
value,
164+
);
165+
}
166+
}

tss-esapi/tests/integration_tests/structures_tests/pcr_tests/pcr_select_tests.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,34 @@ fn test_conversion_from_tss_pcr_select() {
6262
.expect("Failed to create PcrSelect");
6363
assert_eq!(expected, actual);
6464
}
65+
66+
#[test]
67+
fn test_size_of_select() {
68+
let expected_pcr_select_size = PcrSelectSize::ThreeOctets;
69+
let pcr_select = PcrSelect::create(
70+
expected_pcr_select_size,
71+
&[
72+
PcrSlot::Slot1,
73+
PcrSlot::Slot8,
74+
PcrSlot::Slot16,
75+
PcrSlot::Slot17,
76+
],
77+
)
78+
.expect("Failed to create PcrSelect");
79+
80+
assert_eq!(expected_pcr_select_size, pcr_select.size_of_select());
81+
}
82+
83+
#[test]
84+
fn test_selected_pcrs() {
85+
let expected_selected_pcrs = vec![
86+
PcrSlot::Slot1,
87+
PcrSlot::Slot8,
88+
PcrSlot::Slot16,
89+
PcrSlot::Slot17,
90+
];
91+
let pcr_select = PcrSelect::create(PcrSelectSize::default(), expected_selected_pcrs.as_slice())
92+
.expect("Failed to create PcrSelect");
93+
94+
assert_eq!(expected_selected_pcrs, pcr_select.selected_pcrs());
95+
}

tss-esapi/tests/integration_tests/structures_tests/pcr_tests/pcr_selection_tests.rs

Lines changed: 218 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use tss_esapi::{
66
interface_types::algorithm::HashingAlgorithm,
77
structures::{PcrSelectSize, PcrSelection, PcrSlot},
88
tss2_esys::TPMS_PCR_SELECTION,
9+
Error, WrapperErrorKind,
910
};
1011

1112
#[test]
@@ -69,39 +70,243 @@ fn test_size_of_select_handling() {
6970

7071
#[test]
7172
fn test_subtract() {
72-
let mut pcr_select_1 = PcrSelection::create(
73+
let mut pcr_selection_1 = PcrSelection::create(
7374
HashingAlgorithm::Sha256,
7475
PcrSelectSize::TwoOctets,
7576
&[PcrSlot::Slot4, PcrSlot::Slot15],
7677
)
77-
.expect("Failed to create PcrSelect pcr_select_1");
78+
.expect("Failed to create PcrSelection pcr_selection_1");
7879

79-
let pcr_select_2 = PcrSelection::create(
80+
let pcr_selection_2 = PcrSelection::create(
8081
HashingAlgorithm::Sha256,
8182
PcrSelectSize::TwoOctets,
8283
&[PcrSlot::Slot4],
8384
)
84-
.expect("Failed to create PcrSelect pcr_select_2");
85+
.expect("Failed to create PcrSelection pcr_selection_2");
8586

86-
pcr_select_1
87-
.subtract_exact(&pcr_select_2)
88-
.expect("Failed to subtract pcr_select_2 from pcr_select_1");
87+
pcr_selection_1
88+
.subtract_exact(&pcr_selection_2)
89+
.expect("Failed to subtract pcr_selection_2 from pcr_selection_1");
8990

9091
assert_eq!(
91-
pcr_select_1.hashing_algorithm(),
92+
pcr_selection_1.hashing_algorithm(),
9293
HashingAlgorithm::Sha256,
93-
"The pcr_select_1 did not contain expected HashingAlgorithm after subtract"
94+
"The pcr_selection_1 did not contain expected HashingAlgorithm after subtract"
9495
);
9596

9697
assert_eq!(
97-
pcr_select_1.size_of_select(),
98+
pcr_selection_1.size_of_select(),
9899
PcrSelectSize::TwoOctets,
99-
"The pcr_select_1 did not have the expected size of select after subtract"
100+
"The pcr_selection_1 did not have the expected size of select after subtract"
100101
);
101102

102103
assert_eq!(
103-
pcr_select_1.selected(),
104+
pcr_selection_1.selected(),
104105
vec![PcrSlot::Slot15],
105-
"The pcr_select_1 did not contain expected PcrSlots after subtract"
106+
"The pcr_selection_1 did not contain expected PcrSlots after subtract"
107+
);
108+
}
109+
110+
#[test]
111+
fn test_deselect() {
112+
let mut pcr_selection = PcrSelection::create(
113+
HashingAlgorithm::Sha256,
114+
PcrSelectSize::TwoOctets,
115+
&[
116+
PcrSlot::Slot4,
117+
PcrSlot::Slot5,
118+
PcrSlot::Slot6,
119+
PcrSlot::Slot7,
120+
PcrSlot::Slot15,
121+
],
122+
)
123+
.expect("Failed to create PcrSelection pcr_selection");
124+
125+
pcr_selection.deselect(PcrSlot::Slot7);
126+
127+
assert_eq!(
128+
PcrSelection::create(
129+
HashingAlgorithm::Sha256,
130+
PcrSelectSize::TwoOctets,
131+
&[
132+
PcrSlot::Slot4,
133+
PcrSlot::Slot5,
134+
PcrSlot::Slot6,
135+
PcrSlot::Slot15,
136+
],
137+
)
138+
.expect("Failed to create PcrSelection"),
139+
pcr_selection,
140+
"PcrSelection did not match expected value after calling deselect."
141+
)
142+
}
143+
144+
#[test]
145+
fn test_merge_exact() {
146+
let mut pcr_selection_1 = PcrSelection::create(
147+
HashingAlgorithm::Sha256,
148+
PcrSelectSize::TwoOctets,
149+
&[PcrSlot::Slot4, PcrSlot::Slot15],
150+
)
151+
.expect("Failed to create PcrSelection pcr_selection_1");
152+
153+
let pcr_selection_2 = PcrSelection::create(
154+
HashingAlgorithm::Sha256,
155+
PcrSelectSize::TwoOctets,
156+
&[PcrSlot::Slot5],
157+
)
158+
.expect("Failed to create PcrSelection pcr_selection_2");
159+
160+
pcr_selection_1
161+
.merge_exact(&pcr_selection_2)
162+
.expect("Failed to exactly merge pcr_selection_2 into pcr_selection_1");
163+
164+
assert_eq!(
165+
PcrSelection::create(
166+
HashingAlgorithm::Sha256,
167+
PcrSelectSize::TwoOctets,
168+
&[PcrSlot::Slot4, PcrSlot::Slot5, PcrSlot::Slot15],
169+
)
170+
.expect("Failed to create PcrSelection"),
171+
pcr_selection_1,
172+
"PcrSelection did not contain expected value calling merge_exact",
173+
);
174+
}
175+
176+
#[test]
177+
fn test_merge_exact_hasahing_algorithm_mismatch_errors() {
178+
let mut pcr_selection_1 = PcrSelection::create(
179+
HashingAlgorithm::Sha256,
180+
PcrSelectSize::TwoOctets,
181+
&[PcrSlot::Slot5],
182+
)
183+
.expect("Failed to create PcrSelection pcr_selection_1");
184+
185+
let pcr_selection_2 = PcrSelection::create(
186+
HashingAlgorithm::Sha384,
187+
PcrSelectSize::TwoOctets,
188+
&[PcrSlot::Slot6],
189+
)
190+
.expect("Failed to create PcrSelection pcr_selection_2");
191+
192+
assert_eq!(
193+
Err(Error::WrapperError(WrapperErrorKind::InvalidParam)),
194+
pcr_selection_1.merge_exact(&pcr_selection_2),
195+
"Merge exact PcrSelections with different hashing algorithm did not produce the expected error",
196+
);
197+
}
198+
199+
#[test]
200+
fn test_merge_exact_size_of_select_mismatch_errors() {
201+
let mut pcr_selection_1 = PcrSelection::create(
202+
HashingAlgorithm::Sha256,
203+
PcrSelectSize::ThreeOctets,
204+
&[PcrSlot::Slot5],
205+
)
206+
.expect("Failed to create PcrSelection pcr_selection_1");
207+
208+
let pcr_selection_2 = PcrSelection::create(
209+
HashingAlgorithm::Sha256,
210+
PcrSelectSize::TwoOctets,
211+
&[PcrSlot::Slot6],
212+
)
213+
.expect("Failed to create PcrSelection pcr_selection_2");
214+
215+
assert_eq!(
216+
Err(Error::WrapperError(WrapperErrorKind::InvalidParam)),
217+
pcr_selection_1.merge_exact(&pcr_selection_2),
218+
"Merge exact PcrSelections with different size of select did not produce the expected error",
219+
);
220+
}
221+
222+
#[test]
223+
fn test_merge_exact_non_unique_pcr_slot_errors() {
224+
let mut pcr_selection_1 = PcrSelection::create(
225+
HashingAlgorithm::Sha256,
226+
PcrSelectSize::ThreeOctets,
227+
&[PcrSlot::Slot5],
228+
)
229+
.expect("Failed to create PcrSelection pcr_selection_1");
230+
231+
let pcr_selection_2 = PcrSelection::create(
232+
HashingAlgorithm::Sha256,
233+
PcrSelectSize::ThreeOctets,
234+
&[PcrSlot::Slot5],
235+
)
236+
.expect("Failed to create PcrSelection pcr_selection_2");
237+
238+
assert_eq!(
239+
Err(Error::WrapperError(WrapperErrorKind::InvalidParam)),
240+
pcr_selection_1.merge_exact(&pcr_selection_2),
241+
"Merge exact PcrSelections with non unique PcrSlot did not produce the expected error",
242+
);
243+
}
244+
245+
#[test]
246+
fn test_subtract_exact_hasahing_algorithm_mismatch_errors() {
247+
let mut pcr_selection_1 = PcrSelection::create(
248+
HashingAlgorithm::Sha256,
249+
PcrSelectSize::TwoOctets,
250+
&[PcrSlot::Slot5],
251+
)
252+
.expect("Failed to create PcrSelection pcr_selection_1");
253+
254+
let pcr_selection_2 = PcrSelection::create(
255+
HashingAlgorithm::Sha384,
256+
PcrSelectSize::TwoOctets,
257+
&[PcrSlot::Slot5],
258+
)
259+
.expect("Failed to create PcrSelection pcr_selection_2");
260+
261+
assert_eq!(
262+
Err(Error::WrapperError(WrapperErrorKind::InconsistentParams)),
263+
pcr_selection_1.subtract_exact(&pcr_selection_2),
264+
"Subtract exact PcrSelections with different hashing algorithm did not produce the expected error",
265+
);
266+
}
267+
268+
#[test]
269+
fn test_subtract_exact_size_of_select_mismatch_errors() {
270+
let mut pcr_selection_1 = PcrSelection::create(
271+
HashingAlgorithm::Sha256,
272+
PcrSelectSize::ThreeOctets,
273+
&[PcrSlot::Slot5],
274+
)
275+
.expect("Failed to create PcrSelection pcr_selection_1");
276+
277+
let pcr_selection_2 = PcrSelection::create(
278+
HashingAlgorithm::Sha256,
279+
PcrSelectSize::TwoOctets,
280+
&[PcrSlot::Slot5],
281+
)
282+
.expect("Failed to create PcrSelection pcr_selection_2");
283+
284+
assert_eq!(
285+
Err(Error::WrapperError(WrapperErrorKind::InvalidParam)),
286+
pcr_selection_1.subtract_exact(&pcr_selection_2),
287+
"Subtract exact PcrSelections with different size of select did not produce the expected error",
288+
);
289+
}
290+
291+
#[test]
292+
fn test_subtract_exact_unique_pcr_slot_errors() {
293+
let mut pcr_selection_1 = PcrSelection::create(
294+
HashingAlgorithm::Sha256,
295+
PcrSelectSize::ThreeOctets,
296+
&[PcrSlot::Slot5],
297+
)
298+
.expect("Failed to create PcrSelection pcr_selection_1");
299+
300+
let pcr_selection_2 = PcrSelection::create(
301+
HashingAlgorithm::Sha256,
302+
PcrSelectSize::ThreeOctets,
303+
&[PcrSlot::Slot6],
304+
)
305+
.expect("Failed to create PcrSelection pcr_selection_2");
306+
307+
assert_eq!(
308+
Err(Error::WrapperError(WrapperErrorKind::InvalidParam)),
309+
pcr_selection_1.subtract_exact(&pcr_selection_2),
310+
"Subtract exact PcrSelections with unique PcrSlot did not produce the expected error",
106311
);
107312
}

0 commit comments

Comments
 (0)