22
33use crate :: {
44 Choice , Class , Decode , DecodeValue , DerOrd , Encode , EncodeValue , EncodeValueRef , EncodingRules ,
5- Error , Header , Length , Reader , Tag , TagMode , TagNumber , Tagged , ValueOrd , Writer , asn1 :: AnyRef ,
6- tag:: IsConstructed ,
5+ Error , FixedTag , Header , Length , Reader , Tag , TagMode , TagNumber , Tagged , ValueOrd , Writer ,
6+ asn1 :: AnyRef , tag:: IsConstructed ,
77} ;
88use core:: cmp:: Ordering ;
99
@@ -23,13 +23,23 @@ impl_custom_class_ref!(
2323 "0b10000000"
2424) ;
2525
26+ impl_custom_class_explicit ! (
27+ ContextSpecificExplicit ,
28+ ContextSpecific ,
29+ "CONTEXT-SPECIFIC" ,
30+ "0b10000000"
31+ ) ;
32+
2633#[ cfg( test) ]
2734#[ allow( clippy:: unwrap_used) ]
2835mod tests {
2936 use super :: ContextSpecific ;
3037 use crate :: {
3138 Decode , Encode , SliceReader , TagMode , TagNumber ,
32- asn1:: { BitStringRef , ContextSpecificRef , SetOf , Utf8StringRef } ,
39+ asn1:: {
40+ BitStringRef , ContextSpecificRef , SetOf , Utf8StringRef ,
41+ context_specific:: ContextSpecificExplicit ,
42+ } ,
3343 } ;
3444 use hex_literal:: hex;
3545
@@ -194,4 +204,34 @@ mod tests {
194204 assert_eq ! ( field. value. get( 0 ) . cloned( ) , Some ( hello) ) ;
195205 assert_eq ! ( field. value. get( 1 ) . cloned( ) , Some ( world) ) ;
196206 }
207+
208+ #[ test]
209+ fn round_trip_explicit ( ) {
210+ let field =
211+ ContextSpecificExplicit :: < 1 , BitStringRef < ' _ > > :: from_der ( EXAMPLE_BYTES ) . unwrap ( ) ;
212+ assert_eq ! (
213+ field. value,
214+ BitStringRef :: from_bytes( & EXAMPLE_BYTES [ 5 ..] ) . unwrap( )
215+ ) ;
216+ assert_eq ! (
217+ ContextSpecificExplicit :: <1 , BitStringRef <' _>>:: tag_mode( ) ,
218+ TagMode :: Explicit
219+ ) ;
220+ assert_eq ! (
221+ ContextSpecificExplicit :: <1 , BitStringRef <' _>>:: tag_number( ) ,
222+ TagNumber ( 1 )
223+ ) ;
224+
225+ let mut buf = [ 0u8 ; 128 ] ;
226+ let encoded = field. encode_to_slice ( & mut buf) . unwrap ( ) ;
227+ assert_eq ! ( encoded, EXAMPLE_BYTES ) ;
228+
229+ // should not decode as tag CONTEXT-SPECIFIC [2]
230+ assert ! ( ContextSpecificExplicit :: <2 , BitStringRef <' _>>:: from_der( EXAMPLE_BYTES ) . is_err( ) ) ;
231+
232+ // should be different than CONTEXT-SPECIFIC [1]
233+ let invalid_field = ContextSpecificExplicit :: < 2 , BitStringRef < ' _ > > { value : field. value } ;
234+ let invalid_encoded = invalid_field. encode_to_slice ( & mut buf) . unwrap ( ) ;
235+ assert_ne ! ( invalid_encoded, EXAMPLE_BYTES ) ;
236+ }
197237}
0 commit comments