]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Add parsing tests for experimental refund TLVs
authorJeffrey Czyz <jkczyz@gmail.com>
Thu, 24 Oct 2024 20:35:25 +0000 (15:35 -0500)
committerJeffrey Czyz <jkczyz@gmail.com>
Tue, 5 Nov 2024 00:00:24 +0000 (18:00 -0600)
lightning/src/offers/refund.rs

index 1d29785c5339f56866c8700f0037f73933db9e87..fd0f0916f762745ecf8384b068165c58503efd35 100644 (file)
@@ -997,7 +997,7 @@ mod tests {
        use crate::ln::features::{InvoiceRequestFeatures, OfferFeatures};
        use crate::ln::inbound_payment::ExpandedKey;
        use crate::ln::msgs::{DecodeError, MAX_VALUE_MSAT};
-       use crate::offers::invoice_request::{ExperimentalInvoiceRequestTlvStreamRef, INVOICE_REQUEST_TYPES, InvoiceRequestTlvStreamRef};
+       use crate::offers::invoice_request::{EXPERIMENTAL_INVOICE_REQUEST_TYPES, ExperimentalInvoiceRequestTlvStreamRef, INVOICE_REQUEST_TYPES, InvoiceRequestTlvStreamRef};
        use crate::offers::nonce::Nonce;
        use crate::offers::offer::{ExperimentalOfferTlvStreamRef, OfferTlvStreamRef};
        use crate::offers::parse::{Bolt12ParseError, Bolt12SemanticError};
@@ -1621,6 +1621,43 @@ mod tests {
                }
        }
 
+       #[test]
+       fn parses_refund_with_experimental_tlv_records() {
+               const UNKNOWN_ODD_TYPE: u64 = EXPERIMENTAL_INVOICE_REQUEST_TYPES.start + 1;
+               assert!(UNKNOWN_ODD_TYPE % 2 == 1);
+
+               let refund = RefundBuilder::new(vec![1; 32], payer_pubkey(), 1000).unwrap()
+                       .build().unwrap();
+
+               let mut encoded_refund = Vec::new();
+               refund.write(&mut encoded_refund).unwrap();
+               BigSize(UNKNOWN_ODD_TYPE).write(&mut encoded_refund).unwrap();
+               BigSize(32).write(&mut encoded_refund).unwrap();
+               [42u8; 32].write(&mut encoded_refund).unwrap();
+
+               match Refund::try_from(encoded_refund.clone()) {
+                       Ok(refund) => assert_eq!(refund.bytes, encoded_refund),
+                       Err(e) => panic!("error parsing refund: {:?}", e),
+               }
+
+               const UNKNOWN_EVEN_TYPE: u64 = EXPERIMENTAL_INVOICE_REQUEST_TYPES.start;
+               assert!(UNKNOWN_EVEN_TYPE % 2 == 0);
+
+               let refund = RefundBuilder::new(vec![1; 32], payer_pubkey(), 1000).unwrap()
+                       .build().unwrap();
+
+               let mut encoded_refund = Vec::new();
+               refund.write(&mut encoded_refund).unwrap();
+               BigSize(UNKNOWN_EVEN_TYPE).write(&mut encoded_refund).unwrap();
+               BigSize(32).write(&mut encoded_refund).unwrap();
+               [42u8; 32].write(&mut encoded_refund).unwrap();
+
+               match Refund::try_from(encoded_refund) {
+                       Ok(_) => panic!("expected error"),
+                       Err(e) => assert_eq!(e, Bolt12ParseError::Decode(DecodeError::UnknownRequiredFeature)),
+               }
+       }
+
        #[test]
        fn fails_parsing_refund_with_out_of_range_tlv_records() {
                let secp_ctx = Secp256k1::new();
@@ -1638,5 +1675,16 @@ mod tests {
                        Ok(_) => panic!("expected error"),
                        Err(e) => assert_eq!(e, Bolt12ParseError::Decode(DecodeError::InvalidValue)),
                }
+
+               let mut encoded_refund = Vec::new();
+               refund.write(&mut encoded_refund).unwrap();
+               BigSize(EXPERIMENTAL_INVOICE_REQUEST_TYPES.end).write(&mut encoded_refund).unwrap();
+               BigSize(32).write(&mut encoded_refund).unwrap();
+               [42u8; 32].write(&mut encoded_refund).unwrap();
+
+               match Refund::try_from(encoded_refund) {
+                       Ok(_) => panic!("expected error"),
+                       Err(e) => assert_eq!(e, Bolt12ParseError::Decode(DecodeError::InvalidValue)),
+               }
        }
 }