]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Add parsing tests for experimental offer TLVs
authorJeffrey Czyz <jkczyz@gmail.com>
Tue, 6 Aug 2024 16:22:19 +0000 (11:22 -0500)
committerJeffrey Czyz <jkczyz@gmail.com>
Tue, 5 Nov 2024 00:00:23 +0000 (18:00 -0600)
lightning/src/offers/offer.rs

index 3f7e049dc9cecb4ef2696f848a89e1f8284d887a..68f5c10fc212c9726f9b0a635fb2f9220b8e5ed8 100644 (file)
@@ -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)),
+               }
        }
 }