From: Jeffrey Czyz Date: Tue, 6 Aug 2024 16:22:19 +0000 (-0500) Subject: Add parsing tests for experimental offer TLVs X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=a728d1dc2fca0c6dbd00a24a0b40874415d1ad1d;p=rust-lightning Add parsing tests for experimental offer TLVs --- diff --git a/lightning/src/offers/offer.rs b/lightning/src/offers/offer.rs index 3f7e049dc..68f5c10fc 100644 --- a/lightning/src/offers/offer.rs +++ b/lightning/src/offers/offer.rs @@ -1234,7 +1234,7 @@ impl core::fmt::Display for Offer { #[cfg(test)] mod tests { - use super::{Amount, ExperimentalOfferTlvStreamRef, OFFER_TYPES, Offer, OfferTlvStreamRef, Quantity}; + use super::{Amount, EXPERIMENTAL_OFFER_TYPES, ExperimentalOfferTlvStreamRef, OFFER_TYPES, Offer, OfferTlvStreamRef, Quantity}; #[cfg(not(c_bindings))] use { super::OfferBuilder, @@ -1962,6 +1962,35 @@ mod tests { } } + #[test] + fn parses_offer_with_experimental_tlv_records() { + let offer = OfferBuilder::new(pubkey(42)).build().unwrap(); + + let mut encoded_offer = Vec::new(); + offer.write(&mut encoded_offer).unwrap(); + BigSize(EXPERIMENTAL_OFFER_TYPES.start + 1).write(&mut encoded_offer).unwrap(); + BigSize(32).write(&mut encoded_offer).unwrap(); + [42u8; 32].write(&mut encoded_offer).unwrap(); + + match Offer::try_from(encoded_offer.clone()) { + Ok(offer) => assert_eq!(offer.bytes, encoded_offer), + Err(e) => panic!("error parsing offer: {:?}", e), + } + + let offer = OfferBuilder::new(pubkey(42)).build().unwrap(); + + let mut encoded_offer = Vec::new(); + offer.write(&mut encoded_offer).unwrap(); + BigSize(EXPERIMENTAL_OFFER_TYPES.start).write(&mut encoded_offer).unwrap(); + BigSize(32).write(&mut encoded_offer).unwrap(); + [42u8; 32].write(&mut encoded_offer).unwrap(); + + match Offer::try_from(encoded_offer) { + Ok(_) => panic!("expected error"), + Err(e) => assert_eq!(e, Bolt12ParseError::Decode(DecodeError::UnknownRequiredFeature)), + } + } + #[test] fn fails_parsing_offer_with_out_of_range_tlv_records() { let offer = OfferBuilder::new(pubkey(42)).build().unwrap(); @@ -1976,6 +2005,19 @@ mod tests { Ok(_) => panic!("expected error"), Err(e) => assert_eq!(e, Bolt12ParseError::Decode(DecodeError::InvalidValue)), } + + let offer = OfferBuilder::new(pubkey(42)).build().unwrap(); + + let mut encoded_offer = Vec::new(); + offer.write(&mut encoded_offer).unwrap(); + BigSize(EXPERIMENTAL_OFFER_TYPES.end).write(&mut encoded_offer).unwrap(); + BigSize(32).write(&mut encoded_offer).unwrap(); + [42u8; 32].write(&mut encoded_offer).unwrap(); + + match Offer::try_from(encoded_offer) { + Ok(_) => panic!("expected error"), + Err(e) => assert_eq!(e, Bolt12ParseError::Decode(DecodeError::InvalidValue)), + } } }