@@ -6,6 +6,7 @@ use tss_esapi::{
6
6
interface_types:: algorithm:: HashingAlgorithm ,
7
7
structures:: { PcrSelectSize , PcrSelection , PcrSlot } ,
8
8
tss2_esys:: TPMS_PCR_SELECTION ,
9
+ Error , WrapperErrorKind ,
9
10
} ;
10
11
11
12
#[ test]
@@ -69,39 +70,243 @@ fn test_size_of_select_handling() {
69
70
70
71
#[ test]
71
72
fn test_subtract ( ) {
72
- let mut pcr_select_1 = PcrSelection :: create (
73
+ let mut pcr_selection_1 = PcrSelection :: create (
73
74
HashingAlgorithm :: Sha256 ,
74
75
PcrSelectSize :: TwoOctets ,
75
76
& [ PcrSlot :: Slot4 , PcrSlot :: Slot15 ] ,
76
77
)
77
- . expect ( "Failed to create PcrSelect pcr_select_1 " ) ;
78
+ . expect ( "Failed to create PcrSelection pcr_selection_1 " ) ;
78
79
79
- let pcr_select_2 = PcrSelection :: create (
80
+ let pcr_selection_2 = PcrSelection :: create (
80
81
HashingAlgorithm :: Sha256 ,
81
82
PcrSelectSize :: TwoOctets ,
82
83
& [ PcrSlot :: Slot4 ] ,
83
84
)
84
- . expect ( "Failed to create PcrSelect pcr_select_2 " ) ;
85
+ . expect ( "Failed to create PcrSelection pcr_selection_2 " ) ;
85
86
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 " ) ;
89
90
90
91
assert_eq ! (
91
- pcr_select_1 . hashing_algorithm( ) ,
92
+ pcr_selection_1 . hashing_algorithm( ) ,
92
93
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"
94
95
) ;
95
96
96
97
assert_eq ! (
97
- pcr_select_1 . size_of_select( ) ,
98
+ pcr_selection_1 . size_of_select( ) ,
98
99
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"
100
101
) ;
101
102
102
103
assert_eq ! (
103
- pcr_select_1 . selected( ) ,
104
+ pcr_selection_1 . selected( ) ,
104
105
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" ,
106
311
) ;
107
312
}
0 commit comments