Skip to content

Commit ea644f0

Browse files
committed
decode: Add unit test for parsing segwit address
Add a unit test that checks various invalid segwit addresses.
1 parent 5d6e5c3 commit ea644f0

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/primitives/decode.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,4 +947,26 @@ mod tests {
947947
assert!(CheckedHrpstring::new::<Bech32>(valid).is_ok())
948948
}
949949
}
950+
951+
macro_rules! check_invalid_segwit_addresses {
952+
($($test_name:ident, $reason:literal, $address:literal);* $(;)?) => {
953+
$(
954+
#[test]
955+
fn $test_name() {
956+
let res = SegwitHrpstring::new($address);
957+
if res.is_ok() {
958+
panic!("{} sting should not be valid: {}", $address, $reason);
959+
}
960+
}
961+
)*
962+
}
963+
}
964+
check_invalid_segwit_addresses! {
965+
invalid_segwit_address_0, "missing hrp", "1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq";
966+
invalid_segwit_address_1, "missing data-checksum", "91111";
967+
invalid_segwit_address_2, "invalid witness version", "bc14r0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq";
968+
invalid_segwit_address_3, "invalid checksum length", "bc1q5mdq";
969+
invalid_segwit_address_4, "missing data", "bc1qwf5mdq";
970+
invalid_segwit_address_5, "invalid program length", "bc14r0srrr7xfkvy5l643lydnw9rewf5mdq";
971+
}
950972
}

0 commit comments

Comments
 (0)