Skip to content

Commit b8baee9

Browse files
committed
Make tests touching forward_htlcs more strict
Previously, some of the tests manipulating `forward_htlcs` were just iterating, but not actually checking whether the expected entries were present and they were actually changed. Here we make these test cases more strict.
1 parent 8fd7cbf commit b8baee9

File tree

2 files changed

+96
-70
lines changed

2 files changed

+96
-70
lines changed

lightning/src/ln/onion_route_tests.rs

Lines changed: 57 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,17 +1173,21 @@ fn test_onion_failure() {
11731173
|_| {},
11741174
|| {
11751175
nodes[1].node.process_pending_update_add_htlcs();
1176-
for (_, pending_forwards) in nodes[1].node.forward_htlcs.lock().unwrap().iter_mut() {
1177-
for f in pending_forwards.iter_mut() {
1178-
match f {
1179-
&mut HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo {
1180-
ref mut forward_info,
1181-
..
1182-
}) => forward_info.outgoing_cltv_value -= 1,
1183-
_ => {},
1184-
}
1176+
assert_eq!(nodes[1].node.forward_htlcs.lock().unwrap().len(), 1);
1177+
if let Some((_, pending_forwards)) =
1178+
nodes[1].node.forward_htlcs.lock().unwrap().iter_mut().next()
1179+
{
1180+
assert_eq!(pending_forwards.len(), 1);
1181+
match pending_forwards.get_mut(0).unwrap() {
1182+
&mut HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo {
1183+
ref mut forward_info,
1184+
..
1185+
}) => forward_info.outgoing_cltv_value -= 1,
1186+
_ => panic!("Unexpected HTLCForwardInfo"),
11851187
}
1186-
}
1188+
} else {
1189+
panic!("Expected pending forwards!");
1190+
};
11871191
},
11881192
true,
11891193
Some(LocalHTLCFailureReason::FinalIncorrectCLTVExpiry),
@@ -1203,17 +1207,21 @@ fn test_onion_failure() {
12031207
|| {
12041208
nodes[1].node.process_pending_update_add_htlcs();
12051209
// violate amt_to_forward > msg.amount_msat
1206-
for (_, pending_forwards) in nodes[1].node.forward_htlcs.lock().unwrap().iter_mut() {
1207-
for f in pending_forwards.iter_mut() {
1208-
match f {
1209-
&mut HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo {
1210-
ref mut forward_info,
1211-
..
1212-
}) => forward_info.outgoing_amt_msat -= 1,
1213-
_ => {},
1214-
}
1210+
assert_eq!(nodes[1].node.forward_htlcs.lock().unwrap().len(), 1);
1211+
if let Some((_, pending_forwards)) =
1212+
nodes[1].node.forward_htlcs.lock().unwrap().iter_mut().next()
1213+
{
1214+
assert_eq!(pending_forwards.len(), 1);
1215+
match pending_forwards.get_mut(0).unwrap() {
1216+
&mut HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo {
1217+
ref mut forward_info,
1218+
..
1219+
}) => forward_info.outgoing_amt_msat -= 1,
1220+
_ => panic!("Unexpected HTLCForwardInfo"),
12151221
}
1216-
}
1222+
} else {
1223+
panic!("Expected pending forwards!");
1224+
};
12171225
},
12181226
true,
12191227
Some(LocalHTLCFailureReason::FinalIncorrectHTLCAmount),
@@ -1553,16 +1561,21 @@ fn test_overshoot_final_cltv() {
15531561
commitment_signed_dance!(nodes[1], nodes[0], &update_0.commitment_signed, false, true);
15541562

15551563
assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
1556-
for (_, pending_forwards) in nodes[1].node.forward_htlcs.lock().unwrap().iter_mut() {
1557-
for f in pending_forwards.iter_mut() {
1558-
match f {
1559-
&mut HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo {
1560-
ref mut forward_info, ..
1561-
}) => forward_info.outgoing_cltv_value += 1,
1562-
_ => {},
1563-
}
1564+
nodes[1].node.process_pending_update_add_htlcs();
1565+
assert_eq!(nodes[1].node.forward_htlcs.lock().unwrap().len(), 1);
1566+
if let Some((_, pending_forwards)) =
1567+
nodes[1].node.forward_htlcs.lock().unwrap().iter_mut().next()
1568+
{
1569+
assert_eq!(pending_forwards.len(), 1);
1570+
match pending_forwards.get_mut(0).unwrap() {
1571+
&mut HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo { ref mut forward_info, .. }) => {
1572+
forward_info.outgoing_cltv_value += 1
1573+
},
1574+
_ => panic!("Unexpected HTLCForwardInfo"),
15641575
}
1565-
}
1576+
} else {
1577+
panic!("Expected pending forwards!");
1578+
};
15661579
expect_and_process_pending_htlcs(&nodes[1], false);
15671580

15681581
check_added_monitors!(&nodes[1], 1);
@@ -2614,19 +2627,22 @@ fn test_phantom_final_incorrect_cltv_expiry() {
26142627
nodes[1].node.process_pending_update_add_htlcs();
26152628

26162629
// Modify the payload so the phantom hop's HMAC is bogus.
2617-
for (_, pending_forwards) in nodes[1].node.forward_htlcs.lock().unwrap().iter_mut() {
2618-
for f in pending_forwards.iter_mut() {
2619-
match f {
2620-
&mut HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo {
2621-
forward_info: PendingHTLCInfo { ref mut outgoing_cltv_value, .. },
2622-
..
2623-
}) => {
2624-
*outgoing_cltv_value -= 1;
2625-
},
2626-
_ => panic!("Unexpected forward"),
2627-
}
2630+
if let Some((_, pending_forwards)) =
2631+
nodes[1].node.forward_htlcs.lock().unwrap().iter_mut().next()
2632+
{
2633+
assert_eq!(pending_forwards.len(), 1);
2634+
match pending_forwards.get_mut(0).unwrap() {
2635+
&mut HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo {
2636+
forward_info: PendingHTLCInfo { ref mut outgoing_cltv_value, .. },
2637+
..
2638+
}) => {
2639+
*outgoing_cltv_value -= 1;
2640+
},
2641+
_ => panic!("Unexpected HTLCForwardInfo"),
26282642
}
2629-
}
2643+
} else {
2644+
panic!("Expected pending forwards!");
2645+
};
26302646
nodes[1].node.process_pending_htlc_forwards();
26312647
expect_htlc_failure_conditions(
26322648
nodes[1].node.get_and_clear_pending_events(),

lightning/src/ln/payment_tests.rs

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -636,24 +636,30 @@ fn test_reject_mpp_keysend_htlc_mismatching_secret() {
636636
nodes[3].node.process_pending_update_add_htlcs();
637637

638638
assert!(nodes[3].node.get_and_clear_pending_msg_events().is_empty());
639-
for (_, pending_forwards) in nodes[3].node.forward_htlcs.lock().unwrap().iter_mut() {
640-
for f in pending_forwards.iter_mut() {
641-
match f {
642-
&mut HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo {
643-
ref mut forward_info, ..
644-
}) => match forward_info.routing {
639+
assert_eq!(nodes[3].node.forward_htlcs.lock().unwrap().len(), 1);
640+
if let Some((_, pending_forwards)) =
641+
nodes[3].node.forward_htlcs.lock().unwrap().iter_mut().next()
642+
{
643+
assert_eq!(pending_forwards.len(), 1);
644+
match pending_forwards.get_mut(0).unwrap() {
645+
&mut HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo { ref mut forward_info, .. }) => {
646+
match forward_info.routing {
645647
PendingHTLCRouting::ReceiveKeysend { ref mut payment_data, .. } => {
646648
*payment_data = Some(msgs::FinalOnionHopData {
647649
payment_secret: PaymentSecret([42; 32]),
648650
total_msat: amount * 2,
649651
});
650652
},
651653
_ => panic!("Expected PendingHTLCRouting::ReceiveKeysend"),
652-
},
653-
_ => {},
654-
}
654+
}
655+
},
656+
_ => {
657+
panic!("Unexpected HTLCForwardInfo");
658+
},
655659
}
656-
}
660+
} else {
661+
panic!("Expected pending receive");
662+
};
657663
nodes[3].node.process_pending_htlc_forwards();
658664

659665
// Pay along nodes[2]
@@ -685,26 +691,30 @@ fn test_reject_mpp_keysend_htlc_mismatching_secret() {
685691
nodes[3].node.process_pending_update_add_htlcs();
686692

687693
assert!(nodes[3].node.get_and_clear_pending_msg_events().is_empty());
688-
for (_, pending_forwards) in nodes[3].node.forward_htlcs.lock().unwrap().iter_mut() {
689-
for f in pending_forwards.iter_mut() {
690-
match f {
691-
&mut HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo {
692-
ref mut forward_info, ..
693-
}) => {
694-
match forward_info.routing {
695-
PendingHTLCRouting::ReceiveKeysend { ref mut payment_data, .. } => {
696-
*payment_data = Some(msgs::FinalOnionHopData {
697-
payment_secret: PaymentSecret([43; 32]), // Doesn't match the secret used above
698-
total_msat: amount * 2,
699-
});
700-
},
701-
_ => panic!("Expected PendingHTLCRouting::ReceiveKeysend"),
702-
}
703-
},
704-
_ => {},
705-
}
694+
assert_eq!(nodes[3].node.forward_htlcs.lock().unwrap().len(), 1);
695+
if let Some((_, pending_forwards)) =
696+
nodes[3].node.forward_htlcs.lock().unwrap().iter_mut().next()
697+
{
698+
assert_eq!(pending_forwards.len(), 1);
699+
match pending_forwards.get_mut(0).unwrap() {
700+
&mut HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo { ref mut forward_info, .. }) => {
701+
match forward_info.routing {
702+
PendingHTLCRouting::ReceiveKeysend { ref mut payment_data, .. } => {
703+
*payment_data = Some(msgs::FinalOnionHopData {
704+
payment_secret: PaymentSecret([43; 32]), // Doesn't match the secret used above
705+
total_msat: amount * 2,
706+
});
707+
},
708+
_ => panic!("Expected PendingHTLCRouting::ReceiveKeysend"),
709+
}
710+
},
711+
_ => {
712+
panic!("Unexpected HTLCForwardInfo");
713+
},
706714
}
707-
}
715+
} else {
716+
panic!("Expected pending receive");
717+
};
708718
nodes[3].node.process_pending_htlc_forwards();
709719
let fail_type = HTLCHandlingFailureType::Receive { payment_hash };
710720
expect_and_process_pending_htlcs_and_htlc_handling_failed(&nodes[3], &[fail_type]);

0 commit comments

Comments
 (0)