Skip to content
This repository was archived by the owner on Apr 13, 2021. It is now read-only.

Commit fc43d15

Browse files
committed
Add error case unit tests to deserialization module
1 parent ba4463f commit fc43d15

File tree

1 file changed

+49
-11
lines changed

1 file changed

+49
-11
lines changed

src/de.rs

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -729,11 +729,10 @@ mod test {
729729

730730
assert_eq!(parse_int_be::<u32, u8>(&[1, 2, 3, 4], 256), Some(16909060));
731731
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);
733734
}
734735

735-
//TODO: test error conditions
736-
737736
#[test]
738737
fn test_parse_sha256_hash() {
739738
use Sha256;
@@ -749,6 +748,12 @@ mod test {
749748
let expected = Ok(Sha256(hash));
750749

751750
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));
752757
}
753758

754759
#[test]
@@ -777,6 +782,12 @@ mod test {
777782
));
778783

779784
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));
780791
}
781792

782793
#[test]
@@ -786,8 +797,10 @@ mod test {
786797

787798
let input = from_bech32("pu".as_bytes());
788799
let expected = Ok(ExpiryTime{seconds: 60});
789-
790800
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));
791804
}
792805

793806
#[test]
@@ -809,32 +822,52 @@ mod test {
809822
let cases = vec![
810823
(
811824
from_bech32("3x9et2e20v6pu37c5d9vax37wxq72un98".as_bytes()),
812-
Fallback::PubKeyHash([
825+
Ok(Fallback::PubKeyHash([
813826
0x31, 0x72, 0xb5, 0x65, 0x4f, 0x66, 0x83, 0xc8, 0xfb, 0x14, 0x69, 0x59, 0xd3,
814827
0x47, 0xce, 0x30, 0x3c, 0xae, 0x4c, 0xa7
815-
])
828+
]))
816829
),
817830
(
818831
from_bech32("j3a24vwu6r8ejrss3axul8rxldph2q7z9".as_bytes()),
819-
Fallback::ScriptHash([
832+
Ok(Fallback::ScriptHash([
820833
0x8f, 0x55, 0x56, 0x3b, 0x9a, 0x19, 0xf3, 0x21, 0xc2, 0x11, 0xe9, 0xb9, 0xf3,
821834
0x8c, 0xdf, 0x68, 0x6e, 0xa0, 0x78, 0x45
822-
])
835+
]))
823836
),
824837
(
825838
from_bech32("qw508d6qejxtdg4y5r3zarvary0c5xw7k".as_bytes()),
826-
Fallback::SegWitProgram {
839+
Ok(Fallback::SegWitProgram {
827840
version: u5::try_from_u8(0).unwrap(),
828841
program: Vec::from(&[
829842
0x75u8, 0x1e, 0x76, 0xe8, 0x19, 0x91, 0x96, 0xd4, 0x54, 0x94, 0x1c, 0x45,
830843
0xd1, 0xb3, 0xa3, 0x23, 0xf1, 0x43, 0x3b, 0xd6
831844
][..])
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)
833866
)
834867
];
835868

836869
for (input, expected) in cases.into_iter() {
837-
assert_eq!(Fallback::from_base32(&input), Ok(expected));
870+
assert_eq!(Fallback::from_base32(&input), expected);
838871
}
839872
}
840873

@@ -880,6 +913,11 @@ mod test {
880913
});
881914

882915
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+
);
883921
}
884922

885923
#[test]

0 commit comments

Comments
 (0)