Skip to content

Commit 2e62338

Browse files
committed
f - rework and expand tests
1 parent f93242c commit 2e62338

File tree

3 files changed

+109
-28
lines changed

3 files changed

+109
-28
lines changed

lightning/src/offers/invoice.rs

+63-28
Original file line numberDiff line numberDiff line change
@@ -2481,7 +2481,6 @@ mod tests {
24812481
.amount_msats(1000)
24822482
.build().unwrap()
24832483
.request_invoice(&expanded_key, nonce, &secp_ctx, payment_id).unwrap()
2484-
.amount_msats(1000).unwrap()
24852484
.build_and_sign().unwrap()
24862485
.respond_with_no_std(payment_paths(), payment_hash(), now()).unwrap()
24872486
.build().unwrap()
@@ -2501,33 +2500,6 @@ mod tests {
25012500
Ok(_) => panic!("expected error"),
25022501
Err(e) => assert_eq!(e, Bolt12ParseError::InvalidSemantics(Bolt12SemanticError::MissingAmount)),
25032502
}
2504-
2505-
let mut tlv_stream = invoice.as_tlv_stream();
2506-
tlv_stream.3.amount = Some(2000);
2507-
2508-
match Bolt12Invoice::try_from(tlv_stream.to_bytes()) {
2509-
Ok(_) => panic!("expected error"),
2510-
Err(e) => assert_eq!(e, Bolt12ParseError::InvalidSemantics(Bolt12SemanticError::InvalidAmount)),
2511-
}
2512-
2513-
let invoice = RefundBuilder::new(vec![1; 32], payer_pubkey(), 1000).unwrap()
2514-
.build().unwrap()
2515-
.respond_using_derived_keys_no_std(
2516-
payment_paths(), payment_hash(), now(), &expanded_key, &entropy
2517-
)
2518-
.unwrap()
2519-
.build_and_sign(&secp_ctx).unwrap();
2520-
2521-
let mut buffer = Vec::new();
2522-
invoice.write(&mut buffer).unwrap();
2523-
2524-
let mut tlv_stream = invoice.as_tlv_stream();
2525-
tlv_stream.3.amount = Some(2000);
2526-
2527-
match Bolt12Invoice::try_from(tlv_stream.to_bytes()) {
2528-
Ok(_) => panic!("expected error"),
2529-
Err(e) => assert_eq!(e, Bolt12ParseError::InvalidSemantics(Bolt12SemanticError::InvalidAmount)),
2530-
}
25312503
}
25322504

25332505
#[test]
@@ -2748,6 +2720,69 @@ mod tests {
27482720
}
27492721
}
27502722

2723+
#[test]
2724+
fn fails_parsing_invoice_with_wrong_amount() {
2725+
let expanded_key = ExpandedKey::new(&KeyMaterial([42; 32]));
2726+
let entropy = FixedEntropy {};
2727+
let nonce = Nonce::from_entropy_source(&entropy);
2728+
let secp_ctx = Secp256k1::new();
2729+
let payment_id = PaymentId([1; 32]);
2730+
2731+
let invoice = OfferBuilder::new(recipient_pubkey())
2732+
.amount_msats(1000)
2733+
.build().unwrap()
2734+
.request_invoice(&expanded_key, nonce, &secp_ctx, payment_id).unwrap()
2735+
.build_and_sign().unwrap()
2736+
.respond_with_no_std(payment_paths(), payment_hash(), now()).unwrap()
2737+
.amount_msats_unchecked(2000)
2738+
.build().unwrap()
2739+
.sign(recipient_sign).unwrap();
2740+
2741+
let mut buffer = Vec::new();
2742+
invoice.write(&mut buffer).unwrap();
2743+
2744+
match Bolt12Invoice::try_from(buffer) {
2745+
Ok(_) => panic!("expected error"),
2746+
Err(e) => assert_eq!(e, Bolt12ParseError::InvalidSemantics(Bolt12SemanticError::InvalidAmount)),
2747+
}
2748+
2749+
let invoice = OfferBuilder::new(recipient_pubkey())
2750+
.amount_msats(1000)
2751+
.build().unwrap()
2752+
.request_invoice(&expanded_key, nonce, &secp_ctx, payment_id).unwrap()
2753+
.amount_msats(1000).unwrap()
2754+
.build_and_sign().unwrap()
2755+
.respond_with_no_std(payment_paths(), payment_hash(), now()).unwrap()
2756+
.amount_msats_unchecked(2000)
2757+
.build().unwrap()
2758+
.sign(recipient_sign).unwrap();
2759+
2760+
let mut buffer = Vec::new();
2761+
invoice.write(&mut buffer).unwrap();
2762+
2763+
match Bolt12Invoice::try_from(buffer) {
2764+
Ok(_) => panic!("expected error"),
2765+
Err(e) => assert_eq!(e, Bolt12ParseError::InvalidSemantics(Bolt12SemanticError::InvalidAmount)),
2766+
}
2767+
2768+
let invoice = RefundBuilder::new(vec![1; 32], payer_pubkey(), 1000).unwrap()
2769+
.build().unwrap()
2770+
.respond_using_derived_keys_no_std(
2771+
payment_paths(), payment_hash(), now(), &expanded_key, &entropy
2772+
)
2773+
.unwrap()
2774+
.amount_msats_unchecked(2000)
2775+
.build_and_sign(&secp_ctx).unwrap();
2776+
2777+
let mut buffer = Vec::new();
2778+
invoice.write(&mut buffer).unwrap();
2779+
2780+
match Bolt12Invoice::try_from(buffer) {
2781+
Ok(_) => panic!("expected error"),
2782+
Err(e) => assert_eq!(e, Bolt12ParseError::InvalidSemantics(Bolt12SemanticError::InvalidAmount)),
2783+
}
2784+
}
2785+
27512786
#[test]
27522787
fn fails_parsing_invoice_without_signature() {
27532788
let expanded_key = ExpandedKey::new(&KeyMaterial([42; 32]));

lightning/src/offers/invoice_macros.rs

+8
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ macro_rules! invoice_builder_methods_test { (
8787
$self: ident, $self_type: ty, $invoice_fields: expr, $return_type: ty, $return_value: expr
8888
$(, $self_mut: tt)?
8989
) => {
90+
#[cfg_attr(c_bindings, allow(dead_code))]
91+
pub(crate) fn amount_msats_unchecked(
92+
$($self_mut)* $self: $self_type, amount_msats: u64,
93+
) -> $return_type {
94+
$invoice_fields.amount_msats = amount_msats;
95+
$return_value
96+
}
97+
9098
#[cfg_attr(c_bindings, allow(dead_code))]
9199
pub(crate) fn features_unchecked(
92100
$($self_mut)* $self: $self_type, features: Bolt12InvoiceFeatures

lightning/src/offers/invoice_request.rs

+38
Original file line numberDiff line numberDiff line change
@@ -1762,6 +1762,44 @@ mod tests {
17621762
}
17631763
}
17641764

1765+
#[test]
1766+
fn builds_invoice_request_without_amount() {
1767+
let expanded_key = ExpandedKey::new(&KeyMaterial([42; 32]));
1768+
let entropy = FixedEntropy {};
1769+
let nonce = Nonce::from_entropy_source(&entropy);
1770+
let secp_ctx = Secp256k1::new();
1771+
let payment_id = PaymentId([1; 32]);
1772+
1773+
let invoice_request = OfferBuilder::new(recipient_pubkey())
1774+
.amount_msats(1000)
1775+
.build().unwrap()
1776+
.request_invoice(&expanded_key, nonce, &secp_ctx, payment_id).unwrap()
1777+
.build_and_sign().unwrap();
1778+
let (_, _, tlv_stream, _, _, _) = invoice_request.as_tlv_stream();
1779+
assert_eq!(invoice_request.amount_msats(), Some(1000));
1780+
assert_eq!(tlv_stream.amount, None);
1781+
1782+
let invoice_request = OfferBuilder::new(recipient_pubkey())
1783+
.amount_msats(1000)
1784+
.supported_quantity(Quantity::Unbounded)
1785+
.build().unwrap()
1786+
.request_invoice(&expanded_key, nonce, &secp_ctx, payment_id).unwrap()
1787+
.quantity(2).unwrap()
1788+
.build_and_sign().unwrap();
1789+
let (_, _, tlv_stream, _, _, _) = invoice_request.as_tlv_stream();
1790+
assert_eq!(invoice_request.amount_msats(), Some(2000));
1791+
assert_eq!(tlv_stream.amount, None);
1792+
1793+
let invoice_request = OfferBuilder::new(recipient_pubkey())
1794+
.amount(Amount::Currency { iso4217_code: *b"USD", amount: 10 })
1795+
.build_unchecked()
1796+
.request_invoice(&expanded_key, nonce, &secp_ctx, payment_id).unwrap()
1797+
.build_unchecked_and_sign();
1798+
let (_, _, tlv_stream, _, _, _) = invoice_request.as_tlv_stream();
1799+
assert_eq!(invoice_request.amount_msats(), None);
1800+
assert_eq!(tlv_stream.amount, None);
1801+
}
1802+
17651803
#[test]
17661804
fn builds_invoice_request_with_features() {
17671805
let expanded_key = ExpandedKey::new(&KeyMaterial([42; 32]));

0 commit comments

Comments
 (0)