@@ -87,3 +87,251 @@ impl TryFrom<Result> for ResultProto {
87
87
Ok ( ResultProto { keys } )
88
88
}
89
89
}
90
+
91
+ #[ cfg( test) ]
92
+ mod test {
93
+ // Operation <-> Proto conversions are not tested since they're too simple
94
+ use super :: super :: generated_ops:: list_keys:: { KeyInfo as KeyInfoProto , Result as ResultProto } ;
95
+ use super :: super :: generated_ops:: psa_key_attributes:: KeyAttributes as KeyAttributesProto ;
96
+ use super :: super :: { Convert , ProtobufConverter } ;
97
+ use crate :: operations:: list_keys:: { KeyInfo , Operation , Result } ;
98
+ use crate :: operations:: psa_algorithm:: { Algorithm , AsymmetricSignature , Hash } ;
99
+ use crate :: operations:: psa_key_attributes:: { self , Attributes , Lifetime , Policy , UsageFlags } ;
100
+ use crate :: operations:: { NativeOperation , NativeResult } ;
101
+ use crate :: requests:: { request:: RequestBody , response:: ResponseBody , Opcode , ProviderID } ;
102
+ use std:: convert:: TryInto ;
103
+
104
+ static CONVERTER : ProtobufConverter = ProtobufConverter { } ;
105
+
106
+ #[ test]
107
+ fn proto_to_resp ( ) {
108
+ let mut proto: ResultProto = Default :: default ( ) ;
109
+ let mut key_info = KeyInfoProto :: default ( ) ;
110
+ key_info. provider_id = ProviderID :: MbedCrypto as u32 ;
111
+ key_info. name = String :: from ( "Some Key Name" ) ;
112
+
113
+ let key_attrs = Attributes {
114
+ lifetime : Lifetime :: Persistent ,
115
+ key_type : psa_key_attributes:: Type :: RsaKeyPair ,
116
+ bits : 1024 ,
117
+ policy : Policy {
118
+ usage_flags : UsageFlags {
119
+ export : true ,
120
+ copy : true ,
121
+ cache : true ,
122
+ encrypt : true ,
123
+ decrypt : true ,
124
+ sign_message : true ,
125
+ verify_message : true ,
126
+ sign_hash : true ,
127
+ verify_hash : true ,
128
+ derive : true ,
129
+ } ,
130
+ permitted_algorithms : Algorithm :: AsymmetricSignature (
131
+ AsymmetricSignature :: RsaPkcs1v15Sign {
132
+ hash_alg : Hash :: Sha1 . into ( ) ,
133
+ } ,
134
+ ) ,
135
+ } ,
136
+ } ;
137
+
138
+ let key_attrs_proto: KeyAttributesProto = key_attrs. try_into ( ) . unwrap ( ) ;
139
+ key_info. attributes = Some ( key_attrs_proto) ;
140
+ proto. keys . push ( key_info) ;
141
+
142
+ let resp: Result = proto. try_into ( ) . unwrap ( ) ;
143
+
144
+ assert_eq ! ( resp. keys. len( ) , 1 ) ;
145
+ assert_eq ! ( resp. keys[ 0 ] . name, "Some Key Name" ) ;
146
+ assert_eq ! ( resp. keys[ 0 ] . provider_id, ProviderID :: MbedCrypto ) ;
147
+ assert_eq ! ( resp. keys[ 0 ] . attributes, key_attrs) ;
148
+ }
149
+
150
+ #[ test]
151
+ fn resp_to_proto ( ) {
152
+ let mut resp: Result = Result { keys : Vec :: new ( ) } ;
153
+ let key_attributes = Attributes {
154
+ lifetime : Lifetime :: Persistent ,
155
+ key_type : psa_key_attributes:: Type :: RsaKeyPair ,
156
+ bits : 1024 ,
157
+ policy : Policy {
158
+ usage_flags : UsageFlags {
159
+ export : true ,
160
+ copy : true ,
161
+ cache : true ,
162
+ encrypt : true ,
163
+ decrypt : true ,
164
+ sign_message : true ,
165
+ verify_message : true ,
166
+ sign_hash : true ,
167
+ verify_hash : true ,
168
+ derive : true ,
169
+ } ,
170
+ permitted_algorithms : Algorithm :: AsymmetricSignature (
171
+ AsymmetricSignature :: RsaPkcs1v15Sign {
172
+ hash_alg : Hash :: Sha1 . into ( ) ,
173
+ } ,
174
+ ) ,
175
+ } ,
176
+ } ;
177
+ let key_info = KeyInfo {
178
+ provider_id : ProviderID :: MbedCrypto ,
179
+ name : String :: from ( "Foo" ) ,
180
+ attributes : key_attributes,
181
+ } ;
182
+ resp. keys . push ( key_info) ;
183
+
184
+ let proto: ResultProto = resp. try_into ( ) . unwrap ( ) ;
185
+ let key_attributes_proto: KeyAttributesProto = key_attributes. try_into ( ) . unwrap ( ) ;
186
+
187
+ assert_eq ! ( proto. keys. len( ) , 1 ) ;
188
+ assert_eq ! ( proto. keys[ 0 ] . provider_id, ProviderID :: MbedCrypto as u32 ) ;
189
+ assert_eq ! ( proto. keys[ 0 ] . name, "Foo" ) ;
190
+ assert_eq ! ( proto. keys[ 0 ] . attributes, Some ( key_attributes_proto) ) ;
191
+ }
192
+
193
+ #[ test]
194
+ fn list_keys_req_to_native ( ) {
195
+ let req_body = RequestBody :: from_bytes ( Vec :: new ( ) ) ;
196
+ assert ! ( CONVERTER
197
+ . body_to_operation( req_body, Opcode :: ListKeys )
198
+ . is_ok( ) ) ;
199
+ }
200
+
201
+ #[ test]
202
+ fn op_list_keys_from_native ( ) {
203
+ let list_keys = Operation { } ;
204
+ let body = CONVERTER
205
+ . operation_to_body ( NativeOperation :: ListKeys ( list_keys) )
206
+ . expect ( "Failed to convert request" ) ;
207
+ assert ! ( body. is_empty( ) ) ;
208
+ }
209
+
210
+ #[ test]
211
+ fn op_list_keys_e2e ( ) {
212
+ let list_keys = Operation { } ;
213
+ let req_body = CONVERTER
214
+ . operation_to_body ( NativeOperation :: ListKeys ( list_keys) )
215
+ . expect ( "Failed to convert request" ) ;
216
+
217
+ assert ! ( CONVERTER
218
+ . body_to_operation( req_body, Opcode :: ListKeys )
219
+ . is_ok( ) ) ;
220
+ }
221
+
222
+ #[ test]
223
+ fn req_from_native_mangled_body ( ) {
224
+ let req_body =
225
+ RequestBody :: from_bytes ( vec ! [ 0x11 , 0x22 , 0x33 , 0x44 , 0x55 , 0x66 , 0x77 , 0x88 ] ) ;
226
+
227
+ assert ! ( CONVERTER
228
+ . body_to_operation( req_body, Opcode :: ListKeys )
229
+ . is_err( ) ) ;
230
+ }
231
+
232
+ #[ test]
233
+ fn list_keys_body_to_native ( ) {
234
+ let resp_body = ResponseBody :: from_bytes ( Vec :: new ( ) ) ;
235
+ assert ! ( CONVERTER
236
+ . body_to_result( resp_body, Opcode :: ListKeys )
237
+ . is_ok( ) ) ;
238
+ }
239
+
240
+ #[ test]
241
+ fn result_list_keys_from_native ( ) {
242
+ let mut list_keys = Result { keys : Vec :: new ( ) } ;
243
+ let key_info = KeyInfo {
244
+ provider_id : ProviderID :: MbedCrypto ,
245
+ name : String :: from ( "Bar" ) ,
246
+ attributes : Attributes {
247
+ lifetime : Lifetime :: Persistent ,
248
+ key_type : psa_key_attributes:: Type :: RsaKeyPair ,
249
+ bits : 1024 ,
250
+ policy : Policy {
251
+ usage_flags : UsageFlags {
252
+ export : true ,
253
+ copy : true ,
254
+ cache : true ,
255
+ encrypt : true ,
256
+ decrypt : true ,
257
+ sign_message : true ,
258
+ verify_message : true ,
259
+ sign_hash : true ,
260
+ verify_hash : true ,
261
+ derive : true ,
262
+ } ,
263
+ permitted_algorithms : Algorithm :: AsymmetricSignature (
264
+ AsymmetricSignature :: RsaPkcs1v15Sign {
265
+ hash_alg : Hash :: Sha1 . into ( ) ,
266
+ } ,
267
+ ) ,
268
+ } ,
269
+ } ,
270
+ } ;
271
+ list_keys. keys . push ( key_info) ;
272
+
273
+ let body = CONVERTER
274
+ . result_to_body ( NativeResult :: ListKeys ( list_keys) )
275
+ . expect ( "Failed to convert response" ) ;
276
+ assert ! ( !body. is_empty( ) ) ;
277
+ }
278
+
279
+ #[ test]
280
+ fn list_keys_result_e2e ( ) {
281
+ let mut list_keys = Result { keys : Vec :: new ( ) } ;
282
+ let key_info = KeyInfo {
283
+ provider_id : ProviderID :: MbedCrypto ,
284
+ name : String :: from ( "Baz" ) ,
285
+ attributes : Attributes {
286
+ lifetime : Lifetime :: Persistent ,
287
+ key_type : psa_key_attributes:: Type :: RsaKeyPair ,
288
+ bits : 1024 ,
289
+ policy : Policy {
290
+ usage_flags : UsageFlags {
291
+ export : true ,
292
+ copy : true ,
293
+ cache : true ,
294
+ encrypt : true ,
295
+ decrypt : true ,
296
+ sign_message : true ,
297
+ verify_message : true ,
298
+ sign_hash : true ,
299
+ verify_hash : true ,
300
+ derive : true ,
301
+ } ,
302
+ permitted_algorithms : Algorithm :: AsymmetricSignature (
303
+ AsymmetricSignature :: RsaPkcs1v15Sign {
304
+ hash_alg : Hash :: Sha1 . into ( ) ,
305
+ } ,
306
+ ) ,
307
+ } ,
308
+ } ,
309
+ } ;
310
+ list_keys. keys . push ( key_info) ;
311
+
312
+ let body = CONVERTER
313
+ . result_to_body ( NativeResult :: ListKeys ( list_keys) )
314
+ . expect ( "Failed to convert response" ) ;
315
+ assert ! ( !body. is_empty( ) ) ;
316
+
317
+ let result = CONVERTER
318
+ . body_to_result ( body, Opcode :: ListKeys )
319
+ . expect ( "Failed to convert back to result" ) ;
320
+
321
+ match result {
322
+ NativeResult :: ListKeys ( result) => {
323
+ assert_eq ! ( result. keys. len( ) , 1 ) ;
324
+ }
325
+ _ => panic ! ( "Expected list_keys" ) ,
326
+ }
327
+ }
328
+
329
+ #[ test]
330
+ fn resp_from_native_mangled_body ( ) {
331
+ let resp_body =
332
+ ResponseBody :: from_bytes ( vec ! [ 0x11 , 0x22 , 0x33 , 0x44 , 0x55 , 0x66 , 0x77 , 0x88 ] ) ;
333
+ assert ! ( CONVERTER
334
+ . body_to_result( resp_body, Opcode :: ListKeys )
335
+ . is_err( ) ) ;
336
+ }
337
+ }
0 commit comments