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 7, 2025
1 parent e283127 commit 7272969
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 @@ -234,6 +234,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 @@ -250,6 +251,9 @@ mod bolt12_tests {
// A complete string is valid
"lno1pqps7sjqpgtyzm3qv4uxzmtsd3jjqer9wd3hy6tsw35k7msjzfpy7nz5yqcnygrfdej82um5wf5k2uckyypwa3eyt44h6txtxquqh7lz5djge4afgfjn7k4rgrkuag0jsd5xvxg",

// Uppercase is valid
"LNO1PQPS7SJQPGTYZM3QV4UXZMTSD3JJQER9WD3HY6TSW35K7MSJZFPY7NZ5YQCNYGRFDEJ82UM5WF5K2UCKYYPWA3EYT44H6TXTXQUQH7LZ5DJGE4AFGFJN7K4RGRKUAG0JSD5XVXG",

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

Expand Down Expand Up @@ -283,6 +287,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 7272969

Please sign in to comment.