@@ -627,6 +627,164 @@ mod sequence {
627
627
}
628
628
}
629
629
630
+ /// Custom derive test cases for the `BitString` macro.
631
+ #[ cfg( feature = "std" ) ]
632
+ mod bitstring {
633
+ use der:: BitString ;
634
+ use der:: Decode ;
635
+ use der:: Encode ;
636
+ use hex_literal:: hex;
637
+
638
+ const BITSTRING_EXAMPLE : & [ u8 ] = & hex ! ( "03 03 06 03 80" ) ;
639
+
640
+ // this BitString allows only 10..=10 bits
641
+ #[ derive( BitString ) ]
642
+ pub struct MyBitStringTest {
643
+ pub first_bit : bool ,
644
+ pub second_bit : bool ,
645
+ pub third_bit : bool ,
646
+ pub fourth_bit : bool ,
647
+ pub a : bool ,
648
+ pub b : bool ,
649
+ pub almost_least_significant : bool ,
650
+ pub least_significant_bit : bool ,
651
+
652
+ // second byte
653
+ pub second_byte_bit : bool ,
654
+ pub second_byte_bit2 : bool ,
655
+ }
656
+
657
+ #[ test]
658
+ fn decode_bitstring ( ) {
659
+ let test_flags = MyBitStringTest :: from_der ( BITSTRING_EXAMPLE ) . unwrap ( ) ;
660
+
661
+ assert ! ( !test_flags. first_bit) ;
662
+
663
+ assert ! ( test_flags. almost_least_significant) ;
664
+ assert ! ( test_flags. least_significant_bit) ;
665
+ assert ! ( test_flags. second_byte_bit) ;
666
+ assert ! ( !test_flags. second_byte_bit2) ;
667
+
668
+ let reencoded = test_flags. to_der ( ) . unwrap ( ) ;
669
+
670
+ assert_eq ! ( reencoded, BITSTRING_EXAMPLE ) ;
671
+ }
672
+
673
+ /// this BitString will allow only 3..=4 bits
674
+ #[ derive( BitString ) ]
675
+ pub struct MyBitString3or4 {
676
+ pub bit_0 : bool ,
677
+ pub bit_1 : bool ,
678
+ pub bit_2 : bool ,
679
+
680
+ #[ asn1( optional = "true" ) ]
681
+ pub bit_3 : bool ,
682
+ }
683
+
684
+ #[ test]
685
+ fn decode_bitstring_3_used_first_lit ( ) {
686
+ // 5 unused bits, so 3 used
687
+ let bits_3 = MyBitString3or4 :: from_der ( & hex ! ( "03 02 05 80" ) ) . unwrap ( ) ;
688
+
689
+ assert ! ( bits_3. bit_0) ;
690
+ assert ! ( !bits_3. bit_1) ;
691
+ assert ! ( !bits_3. bit_2) ;
692
+ assert ! ( !bits_3. bit_3) ;
693
+ }
694
+ #[ test]
695
+ fn decode_bitstring_3_used_all_lit ( ) {
696
+ // 5 unused bits, so 3 used
697
+ let bits_3 = MyBitString3or4 :: from_der ( & hex ! ( "03 02 05 FF" ) ) . unwrap ( ) ;
698
+
699
+ assert ! ( bits_3. bit_0) ;
700
+ assert ! ( bits_3. bit_1) ;
701
+ assert ! ( bits_3. bit_2) ;
702
+ assert ! ( !bits_3. bit_3) ;
703
+ }
704
+
705
+ #[ test]
706
+ fn decode_bitstring_4_used_all_lit ( ) {
707
+ // 4 unused bits, so 4 used
708
+ let bits_3 = MyBitString3or4 :: from_der ( & hex ! ( "03 02 04 FF" ) ) . unwrap ( ) ;
709
+
710
+ assert ! ( bits_3. bit_0) ;
711
+ assert ! ( bits_3. bit_1) ;
712
+ assert ! ( bits_3. bit_2) ;
713
+ assert ! ( bits_3. bit_3) ;
714
+ }
715
+
716
+ #[ test]
717
+ fn decode_invalid_bitstring_5_used ( ) {
718
+ // 3 unused bits, so 5 used
719
+ assert ! ( MyBitString3or4 :: from_der( & hex!( "03 02 03 FF" ) ) . is_err( ) ) ;
720
+ }
721
+
722
+ #[ test]
723
+ fn decode_invalid_bitstring_2_used ( ) {
724
+ // 6 unused bits, so 2 used
725
+ assert ! ( MyBitString3or4 :: from_der( & hex!( "03 02 06 FF" ) ) . is_err( ) ) ;
726
+ }
727
+
728
+ #[ test]
729
+ fn encode_3_zero_bits ( ) {
730
+ let encoded_3_zeros = MyBitString3or4 {
731
+ bit_0 : false ,
732
+ bit_1 : false ,
733
+ bit_2 : false ,
734
+ bit_3 : false ,
735
+ }
736
+ . to_der ( )
737
+ . unwrap ( ) ;
738
+
739
+ // 3 bits used, 5 unused
740
+ assert_eq ! ( encoded_3_zeros, hex!( "03 02 05 00" ) ) ;
741
+ }
742
+
743
+ #[ test]
744
+ fn encode_3_one_bits ( ) {
745
+ let encoded_3_zeros = MyBitString3or4 {
746
+ bit_0 : true ,
747
+ bit_1 : true ,
748
+ bit_2 : true ,
749
+ bit_3 : false ,
750
+ }
751
+ . to_der ( )
752
+ . unwrap ( ) ;
753
+
754
+ // 3 bits used, 5 unused
755
+ assert_eq ! ( encoded_3_zeros, hex!( "03 02 05 E0" ) ) ;
756
+ }
757
+
758
+ #[ test]
759
+ fn encode_4_one_bits ( ) {
760
+ let encoded_4_zeros = MyBitString3or4 {
761
+ bit_0 : true ,
762
+ bit_1 : true ,
763
+ bit_2 : true ,
764
+ bit_3 : true ,
765
+ }
766
+ . to_der ( )
767
+ . unwrap ( ) ;
768
+
769
+ // 4 bits used, 4 unused
770
+ assert_eq ! ( encoded_4_zeros, hex!( "03 02 04 F0" ) ) ;
771
+ }
772
+
773
+ #[ test]
774
+ fn encode_optional_one_4_used ( ) {
775
+ let encoded_4_zeros = MyBitString3or4 {
776
+ bit_0 : false ,
777
+ bit_1 : false ,
778
+ bit_2 : false ,
779
+ bit_3 : true ,
780
+ }
781
+ . to_der ( )
782
+ . unwrap ( ) ;
783
+
784
+ // 4 bits used, 4 unused
785
+ assert_eq ! ( encoded_4_zeros, hex!( "03 02 04 10" ) ) ;
786
+ }
787
+ }
630
788
mod infer_default {
631
789
//! When another crate might define a PartialEq for another type, the use of
632
790
//! `default="Default::default"` in the der derivation will not provide enough
0 commit comments