@@ -729,11 +729,10 @@ mod test {
729
729
730
730
assert_eq ! ( parse_int_be:: <u32 , u8 >( & [ 1 , 2 , 3 , 4 ] , 256 ) , Some ( 16909060 ) ) ;
731
731
assert_eq ! ( parse_int_be:: <u32 , u8 >( & [ 1 , 3 ] , 32 ) , Some ( 35 ) ) ;
732
- assert_eq ! ( parse_int_be:: <u32 , u8 >( & [ 1 , 2 , 3 , 4 , 5 ] , 256 ) , None ) ;
732
+ assert_eq ! ( parse_int_be:: <u32 , u8 >( & [ 255 , 255 , 255 , 255 ] , 256 ) , Some ( 4294967295 ) ) ;
733
+ assert_eq ! ( parse_int_be:: <u32 , u8 >( & [ 1 , 0 , 0 , 0 , 0 ] , 256 ) , None ) ;
733
734
}
734
735
735
- //TODO: test error conditions
736
-
737
736
#[ test]
738
737
fn test_parse_sha256_hash ( ) {
739
738
use Sha256 ;
@@ -749,6 +748,12 @@ mod test {
749
748
let expected = Ok ( Sha256 ( hash) ) ;
750
749
751
750
assert_eq ! ( Sha256 :: from_base32( & input) , expected) ;
751
+
752
+ // make sure hashes of unknown length get skipped
753
+ let input_unexpected_length = from_bech32 (
754
+ "qqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqypyq" . as_bytes ( )
755
+ ) ;
756
+ assert_eq ! ( Sha256 :: from_base32( & input_unexpected_length) , Err ( ParseError :: Skip ) ) ;
752
757
}
753
758
754
759
#[ test]
@@ -777,6 +782,12 @@ mod test {
777
782
) ) ;
778
783
779
784
assert_eq ! ( PayeePubKey :: from_base32( & input) , expected) ;
785
+
786
+ // expects 33 bytes
787
+ let input_unexpected_length = from_bech32 (
788
+ "q0n326hr8v9zprg8gsvezcch06gfaqqhde2aj730yg0durunfhvq" . as_bytes ( )
789
+ ) ;
790
+ assert_eq ! ( PayeePubKey :: from_base32( & input_unexpected_length) , Err ( ParseError :: Skip ) ) ;
780
791
}
781
792
782
793
#[ test]
@@ -786,8 +797,10 @@ mod test {
786
797
787
798
let input = from_bech32 ( "pu" . as_bytes ( ) ) ;
788
799
let expected = Ok ( ExpiryTime { seconds : 60 } ) ;
789
-
790
800
assert_eq ! ( ExpiryTime :: from_base32( & input) , expected) ;
801
+
802
+ let input_too_large = from_bech32 ( "sqqqqqqqqqqqq" . as_bytes ( ) ) ;
803
+ assert_eq ! ( ExpiryTime :: from_base32( & input_too_large) , Err ( ParseError :: IntegerOverflowError ) ) ;
791
804
}
792
805
793
806
#[ test]
@@ -809,32 +822,52 @@ mod test {
809
822
let cases = vec ! [
810
823
(
811
824
from_bech32( "3x9et2e20v6pu37c5d9vax37wxq72un98" . as_bytes( ) ) ,
812
- Fallback :: PubKeyHash ( [
825
+ Ok ( Fallback :: PubKeyHash ( [
813
826
0x31 , 0x72 , 0xb5 , 0x65 , 0x4f , 0x66 , 0x83 , 0xc8 , 0xfb , 0x14 , 0x69 , 0x59 , 0xd3 ,
814
827
0x47 , 0xce , 0x30 , 0x3c , 0xae , 0x4c , 0xa7
815
- ] )
828
+ ] ) )
816
829
) ,
817
830
(
818
831
from_bech32( "j3a24vwu6r8ejrss3axul8rxldph2q7z9" . as_bytes( ) ) ,
819
- Fallback :: ScriptHash ( [
832
+ Ok ( Fallback :: ScriptHash ( [
820
833
0x8f , 0x55 , 0x56 , 0x3b , 0x9a , 0x19 , 0xf3 , 0x21 , 0xc2 , 0x11 , 0xe9 , 0xb9 , 0xf3 ,
821
834
0x8c , 0xdf , 0x68 , 0x6e , 0xa0 , 0x78 , 0x45
822
- ] )
835
+ ] ) )
823
836
) ,
824
837
(
825
838
from_bech32( "qw508d6qejxtdg4y5r3zarvary0c5xw7k" . as_bytes( ) ) ,
826
- Fallback :: SegWitProgram {
839
+ Ok ( Fallback :: SegWitProgram {
827
840
version: u5:: try_from_u8( 0 ) . unwrap( ) ,
828
841
program: Vec :: from( & [
829
842
0x75u8 , 0x1e , 0x76 , 0xe8 , 0x19 , 0x91 , 0x96 , 0xd4 , 0x54 , 0x94 , 0x1c , 0x45 ,
830
843
0xd1 , 0xb3 , 0xa3 , 0x23 , 0xf1 , 0x43 , 0x3b , 0xd6
831
844
] [ ..] )
832
- }
845
+ } )
846
+ ) ,
847
+ (
848
+ vec![ u5:: try_from_u8( 21 ) . unwrap( ) ; 41 ] ,
849
+ Err ( ParseError :: Skip )
850
+ ) ,
851
+ (
852
+ vec![ ] ,
853
+ Err ( ParseError :: UnexpectedEndOfTaggedFields )
854
+ ) ,
855
+ (
856
+ vec![ u5:: try_from_u8( 1 ) . unwrap( ) ; 81 ] ,
857
+ Err ( ParseError :: InvalidSegWitProgramLength )
858
+ ) ,
859
+ (
860
+ vec![ u5:: try_from_u8( 17 ) . unwrap( ) ; 1 ] ,
861
+ Err ( ParseError :: InvalidPubKeyHashLength )
862
+ ) ,
863
+ (
864
+ vec![ u5:: try_from_u8( 18 ) . unwrap( ) ; 1 ] ,
865
+ Err ( ParseError :: InvalidScriptHashLength )
833
866
)
834
867
] ;
835
868
836
869
for ( input, expected) in cases. into_iter ( ) {
837
- assert_eq ! ( Fallback :: from_base32( & input) , Ok ( expected) ) ;
870
+ assert_eq ! ( Fallback :: from_base32( & input) , expected) ;
838
871
}
839
872
}
840
873
@@ -880,6 +913,11 @@ mod test {
880
913
} ) ;
881
914
882
915
assert_eq ! ( Route :: from_base32( & input) , Ok ( Route ( expected) ) ) ;
916
+
917
+ assert_eq ! (
918
+ Route :: from_base32( & [ u5:: try_from_u8( 0 ) . unwrap( ) ; 40 ] [ ..] ) ,
919
+ Err ( ParseError :: UnexpectedEndOfTaggedFields )
920
+ ) ;
883
921
}
884
922
885
923
#[ test]
0 commit comments