Skip to content

Commit

Permalink
Add test coverage for upper-/mixed-case Offer encodings
Browse files Browse the repository at this point in the history
.. to ensure we're able to decode all-uppercase HRPs and reject
mixed-case encodings.
  • Loading branch information
tnull committed Jan 6, 2025
1 parent 6c5a8ca commit 24b99a2
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lightning/src/offers/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ impl From<secp256k1::Error> for Bolt12ParseError {
mod bolt12_tests {
use super::Bolt12ParseError;
use crate::offers::offer::Offer;
use bech32::primitives::decode::{CheckedHrpstringError, UncheckedHrpstringError, CharError};

#[test]
fn encodes_offer_as_bech32_without_checksum() {
Expand All @@ -249,6 +250,9 @@ mod bolt12_tests {
// A complete string is valid
"lno1pqps7sjqpgtyzm3qv4uxzmtsd3jjqer9wd3hy6tsw35k7msjzfpy7nz5yqcnygrfdej82um5wf5k2uckyypwa3eyt44h6txtxquqh7lz5djge4afgfjn7k4rgrkuag0jsd5xvxg",

// An all-uppercase string is valid
&"lno1pqps7sjqpgtyzm3qv4uxzmtsd3jjqer9wd3hy6tsw35k7msjzfpy7nz5yqcnygrfdej82um5wf5k2uckyypwa3eyt44h6txtxquqh7lz5djge4afgfjn7k4rgrkuag0jsd5xvxg".to_uppercase(),

// + can join anywhere
"l+no1pqps7sjqpgtyzm3qv4uxzmtsd3jjqer9wd3hy6tsw35k7msjzfpy7nz5yqcnygrfdej82um5wf5k2uckyypwa3eyt44h6txtxquqh7lz5djge4afgfjn7k4rgrkuag0jsd5xvxg",

Expand Down Expand Up @@ -282,6 +286,16 @@ mod bolt12_tests {
}
}
}

#[test]
fn fails_parsing_bech32_encoded_offers_with_mixed_casing() {
// We assert that mixed-case encoding fails to parse.
let mixed_case_offer = "lno1pqpS7sjqpgtyzm3qv4uxzmtsd3jjqer9wd3hy6tsw35k7msjzfpy7nz5yqcnygrfdej82um5wf5k2uckyypwa3eyt44h6txtxquqh7lz5djge4afgfjn7k4rgrkuag0jsd5xvxg";
match mixed_case_offer.parse::<Offer>() {
Ok(_) => panic!("Valid offer: {}", mixed_case_offer),
Err(e) => assert_eq!(e, Bolt12ParseError::Bech32(CheckedHrpstringError::Parse(UncheckedHrpstringError::Char(CharError::MixedCase)))),
}
}
}

#[cfg(test)]
Expand Down

0 comments on commit 24b99a2

Please sign in to comment.