22
33use crate :: {
44 Choice , Class , Decode , DecodeValue , DerOrd , Encode , EncodeValue , EncodeValueRef , Error , Header ,
5- Length , Reader , Tag , TagMode , TagNumber , Tagged , ValueOrd , Writer , asn1:: AnyRef ,
6- tag:: IsConstructed ,
5+ Length , Reader , Tag , TagMode , TagNumber , Tagged , ValueOrd , Writer ,
6+ asn1:: { AnyRef , class_tagged:: ClassTaggedExplicit } ,
7+ tag:: { IsConstructed , class:: CLASS_CONTEXT_SPECIFIC } ,
78} ;
89use core:: cmp:: Ordering ;
910
@@ -23,13 +24,17 @@ impl_custom_class_ref!(
2324 "0b10000000"
2425) ;
2526
27+ /// ContextSpecific class, EXPLICIT
28+ pub type ContextSpecificExplicit < const NUMBER : u32 , T > =
29+ ClassTaggedExplicit < NUMBER , T , CLASS_CONTEXT_SPECIFIC > ;
30+
2631#[ cfg( test) ]
2732#[ allow( clippy:: unwrap_used) ]
2833mod tests {
2934 use super :: ContextSpecific ;
3035 use crate :: {
3136 Decode , Encode , SliceReader , TagMode , TagNumber ,
32- asn1:: { BitStringRef , ContextSpecificRef , SetOf , Utf8StringRef } ,
37+ asn1:: { BitStringRef , ContextSpecificExplicit , ContextSpecificRef , SetOf , Utf8StringRef } ,
3338 } ;
3439 use hex_literal:: hex;
3540
@@ -194,4 +199,34 @@ mod tests {
194199 assert_eq ! ( field. value. get( 0 ) . cloned( ) , Some ( hello) ) ;
195200 assert_eq ! ( field. value. get( 1 ) . cloned( ) , Some ( world) ) ;
196201 }
202+
203+ #[ test]
204+ fn round_trip_explicit ( ) {
205+ let field =
206+ ContextSpecificExplicit :: < 1 , BitStringRef < ' _ > > :: from_der ( EXAMPLE_BYTES ) . unwrap ( ) ;
207+ assert_eq ! (
208+ field. value,
209+ BitStringRef :: from_bytes( & EXAMPLE_BYTES [ 5 ..] ) . unwrap( )
210+ ) ;
211+ assert_eq ! (
212+ ContextSpecificExplicit :: <1 , BitStringRef <' _>>:: tag_mode( ) ,
213+ TagMode :: Explicit
214+ ) ;
215+ assert_eq ! (
216+ ContextSpecificExplicit :: <1 , BitStringRef <' _>>:: tag_number( ) ,
217+ TagNumber ( 1 )
218+ ) ;
219+
220+ let mut buf = [ 0u8 ; 128 ] ;
221+ let encoded = field. encode_to_slice ( & mut buf) . unwrap ( ) ;
222+ assert_eq ! ( encoded, EXAMPLE_BYTES ) ;
223+
224+ // should not decode as tag CONTEXT-SPECIFIC [2]
225+ assert ! ( ContextSpecificExplicit :: <2 , BitStringRef <' _>>:: from_der( EXAMPLE_BYTES ) . is_err( ) ) ;
226+
227+ // should be different than CONTEXT-SPECIFIC [1]
228+ let invalid_field = ContextSpecificExplicit :: < 2 , BitStringRef < ' _ > > { value : field. value } ;
229+ let invalid_encoded = invalid_field. encode_to_slice ( & mut buf) . unwrap ( ) ;
230+ assert_ne ! ( invalid_encoded, EXAMPLE_BYTES ) ;
231+ }
197232}
0 commit comments