2
2
3
3
use crate :: {
4
4
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 ,
7
7
} ;
8
8
use core:: cmp:: Ordering ;
9
9
@@ -23,13 +23,23 @@ impl_custom_class_ref!(
23
23
"0b10000000"
24
24
) ;
25
25
26
+ impl_custom_class_explicit ! (
27
+ ContextSpecificExplicit ,
28
+ ContextSpecific ,
29
+ "CONTEXT-SPECIFIC" ,
30
+ "0b10000000"
31
+ ) ;
32
+
26
33
#[ cfg( test) ]
27
34
#[ allow( clippy:: unwrap_used) ]
28
35
mod tests {
29
36
use super :: ContextSpecific ;
30
37
use crate :: {
31
38
Decode , Encode , SliceReader , TagMode , TagNumber ,
32
- asn1:: { BitStringRef , ContextSpecificRef , SetOf , Utf8StringRef } ,
39
+ asn1:: {
40
+ BitStringRef , ContextSpecificRef , SetOf , Utf8StringRef ,
41
+ context_specific:: ContextSpecificExplicit ,
42
+ } ,
33
43
} ;
34
44
use hex_literal:: hex;
35
45
@@ -194,4 +204,34 @@ mod tests {
194
204
assert_eq ! ( field. value. get( 0 ) . cloned( ) , Some ( hello) ) ;
195
205
assert_eq ! ( field. value. get( 1 ) . cloned( ) , Some ( world) ) ;
196
206
}
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
+ }
197
237
}
0 commit comments