Skip to content

Commit

Permalink
decode: Add unit test for parsing segwit address
Browse files Browse the repository at this point in the history
Add a unit test that checks various invalid segwit addresses.
  • Loading branch information
tcharding committed Sep 18, 2023
1 parent 5d6e5c3 commit ea644f0
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/primitives/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -947,4 +947,26 @@ mod tests {
assert!(CheckedHrpstring::new::<Bech32>(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";
}
}

0 comments on commit ea644f0

Please sign in to comment.