From 7272969c19557ad1960b7feb97607c379560aa0d Mon Sep 17 00:00:00 2001 From: Elias Rohrer Date: Mon, 6 Jan 2025 11:37:55 +0100 Subject: [PATCH] Add test coverage for upper-/mixed-case `Offer` encodings .. to ensure we're able to decode all-uppercase HRPs and reject mixed-case encodings. --- lightning/src/offers/parse.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lightning/src/offers/parse.rs b/lightning/src/offers/parse.rs index bd4c7283a82..f3c481a9f95 100644 --- a/lightning/src/offers/parse.rs +++ b/lightning/src/offers/parse.rs @@ -234,6 +234,7 @@ impl From 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() { @@ -250,6 +251,9 @@ mod bolt12_tests { // A complete string is valid "lno1pqps7sjqpgtyzm3qv4uxzmtsd3jjqer9wd3hy6tsw35k7msjzfpy7nz5yqcnygrfdej82um5wf5k2uckyypwa3eyt44h6txtxquqh7lz5djge4afgfjn7k4rgrkuag0jsd5xvxg", + // Uppercase is valid + "LNO1PQPS7SJQPGTYZM3QV4UXZMTSD3JJQER9WD3HY6TSW35K7MSJZFPY7NZ5YQCNYGRFDEJ82UM5WF5K2UCKYYPWA3EYT44H6TXTXQUQH7LZ5DJGE4AFGFJN7K4RGRKUAG0JSD5XVXG", + // + can join anywhere "l+no1pqps7sjqpgtyzm3qv4uxzmtsd3jjqer9wd3hy6tsw35k7msjzfpy7nz5yqcnygrfdej82um5wf5k2uckyypwa3eyt44h6txtxquqh7lz5djge4afgfjn7k4rgrkuag0jsd5xvxg", @@ -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::() { + Ok(_) => panic!("Valid offer: {}", mixed_case_offer), + Err(e) => assert_eq!(e, Bolt12ParseError::Bech32(CheckedHrpstringError::Parse(UncheckedHrpstringError::Char(CharError::MixedCase)))), + } + } } #[cfg(test)]