@@ -258,7 +258,10 @@ where
258
258
#[ allow( clippy:: unwrap_used) ]
259
259
mod tests {
260
260
use super :: ContextSpecific ;
261
- use crate :: { Decode , Encode , SliceReader , TagMode , TagNumber , asn1:: BitStringRef } ;
261
+ use crate :: {
262
+ Decode , Encode , SliceReader , TagMode , TagNumber ,
263
+ asn1:: { BitStringRef , ContextSpecificRef , SetOf , Utf8StringRef } ,
264
+ } ;
262
265
use hex_literal:: hex;
263
266
264
267
// Public key data from `pkcs8` crate's `ed25519-pkcs8-v2.der`
@@ -353,4 +356,75 @@ mod tests {
353
356
None
354
357
) ;
355
358
}
359
+
360
+ #[ test]
361
+ fn context_specific_explicit_ref ( ) {
362
+ let mut set = SetOf :: new ( ) ;
363
+ set. insert ( 8u16 ) . unwrap ( ) ;
364
+ set. insert ( 7u16 ) . unwrap ( ) ;
365
+
366
+ let field = ContextSpecificRef :: < SetOf < u16 , 2 > > {
367
+ value : & set,
368
+ tag_number : TagNumber ( 2 ) ,
369
+ tag_mode : TagMode :: Explicit ,
370
+ } ;
371
+
372
+ let mut buf = [ 0u8 ; 16 ] ;
373
+ let encoded = field. encode_to_slice ( & mut buf) . unwrap ( ) ;
374
+ assert_eq ! (
375
+ encoded,
376
+ & [
377
+ /* CONTEXT-SPECIFIC [2] */ 0xA2 , 0x08 , /* SET 0x11 | 0x20 */ 0x31 , 0x06 ,
378
+ /* INTEGER */ 0x02 , 0x01 , 0x07 , /* INTEGER */ 0x02 , 0x01 , 0x08
379
+ ]
380
+ ) ;
381
+
382
+ let mut reader = SliceReader :: new ( encoded) . unwrap ( ) ;
383
+ let field = ContextSpecific :: < SetOf < u16 , 2 > > :: decode_explicit ( & mut reader, TagNumber ( 2 ) )
384
+ . unwrap ( )
385
+ . unwrap ( ) ;
386
+
387
+ assert_eq ! ( field. value. len( ) , 2 ) ;
388
+ assert_eq ! ( field. value. get( 0 ) . cloned( ) , Some ( 7 ) ) ;
389
+ assert_eq ! ( field. value. get( 1 ) . cloned( ) , Some ( 8 ) ) ;
390
+ }
391
+
392
+ #[ test]
393
+ fn context_specific_implicit_ref ( ) {
394
+ let hello = Utf8StringRef :: new ( "Hello" ) . unwrap ( ) ;
395
+ let world = Utf8StringRef :: new ( "world" ) . unwrap ( ) ;
396
+
397
+ let mut set = SetOf :: new ( ) ;
398
+ set. insert ( hello) . unwrap ( ) ;
399
+ set. insert ( world) . unwrap ( ) ;
400
+
401
+ let field = ContextSpecificRef :: < SetOf < Utf8StringRef < ' _ > , 2 > > {
402
+ value : & set,
403
+ tag_number : TagNumber ( 2 ) ,
404
+ tag_mode : TagMode :: Implicit ,
405
+ } ;
406
+
407
+ let mut buf = [ 0u8 ; 16 ] ;
408
+ let encoded = field. encode_to_slice ( & mut buf) . unwrap ( ) ;
409
+ assert_eq ! (
410
+ encoded,
411
+ & [
412
+ 0xA2 , 0x0E , // CONTEXT-SPECIFIC [2]
413
+ 0x0C , 0x05 , b'H' , b'e' , b'l' , b'l' , b'o' , // UTF8String "Hello"
414
+ 0x0C , 0x05 , b'w' , b'o' , b'r' , b'l' , b'd' , // UTF8String "world"
415
+ ]
416
+ ) ;
417
+
418
+ let mut reader = SliceReader :: new ( encoded) . unwrap ( ) ;
419
+ let field = ContextSpecific :: < SetOf < Utf8StringRef < ' _ > , 2 > > :: decode_implicit (
420
+ & mut reader,
421
+ TagNumber ( 2 ) ,
422
+ )
423
+ . unwrap ( )
424
+ . unwrap ( ) ;
425
+
426
+ assert_eq ! ( field. value. len( ) , 2 ) ;
427
+ assert_eq ! ( field. value. get( 0 ) . cloned( ) , Some ( hello) ) ;
428
+ assert_eq ! ( field. value. get( 1 ) . cloned( ) , Some ( world) ) ;
429
+ }
356
430
}
0 commit comments