From ea644f05a402a5f934c5c652680ab2d92db2ed2e Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Tue, 19 Sep 2023 09:53:14 +1000 Subject: [PATCH] decode: Add unit test for parsing segwit address Add a unit test that checks various invalid segwit addresses. --- src/primitives/decode.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/primitives/decode.rs b/src/primitives/decode.rs index f6f2b0759..012996f81 100644 --- a/src/primitives/decode.rs +++ b/src/primitives/decode.rs @@ -947,4 +947,26 @@ mod tests { assert!(CheckedHrpstring::new::(valid).is_ok()) } } + + macro_rules! check_invalid_segwit_addresses { + ($($test_name:ident, $reason:literal, $address:literal);* $(;)?) => { + $( + #[test] + fn $test_name() { + let res = SegwitHrpstring::new($address); + if res.is_ok() { + panic!("{} sting should not be valid: {}", $address, $reason); + } + } + )* + } + } + check_invalid_segwit_addresses! { + invalid_segwit_address_0, "missing hrp", "1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq"; + invalid_segwit_address_1, "missing data-checksum", "91111"; + invalid_segwit_address_2, "invalid witness version", "bc14r0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq"; + invalid_segwit_address_3, "invalid checksum length", "bc1q5mdq"; + invalid_segwit_address_4, "missing data", "bc1qwf5mdq"; + invalid_segwit_address_5, "invalid program length", "bc14r0srrr7xfkvy5l643lydnw9rewf5mdq"; + } }