Skip to content

Commit 3ae568b

Browse files
committed
Add test coverage for process_onion_failure
1 parent eaeed77 commit 3ae568b

File tree

1 file changed

+51
-27
lines changed

1 file changed

+51
-27
lines changed

lightning/src/ln/onion_utils.rs

Lines changed: 51 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1764,7 +1764,10 @@ fn decode_next_hop<T, R: ReadableArgs<T>, N: NextPacketBytes>(
17641764

17651765
#[cfg(test)]
17661766
mod tests {
1767+
use std::sync::Arc;
1768+
17671769
use crate::io;
1770+
use crate::ln::channelmanager::PaymentId;
17681771
use crate::ln::msgs;
17691772
use crate::routing::router::{Path, PaymentParameters, Route, RouteHop};
17701773
use crate::types::features::{ChannelFeatures, NodeFeatures};
@@ -1773,6 +1776,7 @@ mod tests {
17731776

17741777
#[allow(unused_imports)]
17751778
use crate::prelude::*;
1779+
use crate::util::test_utils::TestLogger;
17761780

17771781
use bitcoin::hex::FromHex;
17781782
use bitcoin::secp256k1::Secp256k1;
@@ -1785,38 +1789,43 @@ mod tests {
17851789
SecretKey::from_slice(&<Vec<u8>>::from_hex(hex).unwrap()[..]).unwrap()
17861790
}
17871791

1792+
fn build_test_path() -> Path {
1793+
Path { hops: vec![
1794+
RouteHop {
1795+
pubkey: PublicKey::from_slice(&<Vec<u8>>::from_hex("02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619").unwrap()[..]).unwrap(),
1796+
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
1797+
short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0, maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
1798+
},
1799+
RouteHop {
1800+
pubkey: PublicKey::from_slice(&<Vec<u8>>::from_hex("0324653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c").unwrap()[..]).unwrap(),
1801+
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
1802+
short_channel_id: 1, fee_msat: 0, cltv_expiry_delta: 0, maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
1803+
},
1804+
RouteHop {
1805+
pubkey: PublicKey::from_slice(&<Vec<u8>>::from_hex("027f31ebc5462c1fdce1b737ecff52d37d75dea43ce11c74d25aa297165faa2007").unwrap()[..]).unwrap(),
1806+
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
1807+
short_channel_id: 2, fee_msat: 0, cltv_expiry_delta: 0, maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
1808+
},
1809+
RouteHop {
1810+
pubkey: PublicKey::from_slice(&<Vec<u8>>::from_hex("032c0b7cf95324a07d05398b240174dc0c2be444d96b159aa6c7f7b1e668680991").unwrap()[..]).unwrap(),
1811+
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
1812+
short_channel_id: 3, fee_msat: 0, cltv_expiry_delta: 0, maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
1813+
},
1814+
RouteHop {
1815+
pubkey: PublicKey::from_slice(&<Vec<u8>>::from_hex("02edabbd16b41c8371b92ef2f04c1185b4f03b6dcd52ba9b78d9d7c89c8f221145").unwrap()[..]).unwrap(),
1816+
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
1817+
short_channel_id: 4, fee_msat: 0, cltv_expiry_delta: 0, maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
1818+
},
1819+
], blinded_tail: None }
1820+
}
1821+
17881822
fn build_test_onion_keys() -> Vec<OnionKeys> {
17891823
// Keys from BOLT 4, used in both test vector tests
17901824
let secp_ctx = Secp256k1::new();
17911825

1826+
let path = build_test_path();
17921827
let route = Route {
1793-
paths: vec![Path { hops: vec![
1794-
RouteHop {
1795-
pubkey: PublicKey::from_slice(&<Vec<u8>>::from_hex("02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619").unwrap()[..]).unwrap(),
1796-
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
1797-
short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0, maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
1798-
},
1799-
RouteHop {
1800-
pubkey: PublicKey::from_slice(&<Vec<u8>>::from_hex("0324653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c").unwrap()[..]).unwrap(),
1801-
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
1802-
short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0, maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
1803-
},
1804-
RouteHop {
1805-
pubkey: PublicKey::from_slice(&<Vec<u8>>::from_hex("027f31ebc5462c1fdce1b737ecff52d37d75dea43ce11c74d25aa297165faa2007").unwrap()[..]).unwrap(),
1806-
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
1807-
short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0, maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
1808-
},
1809-
RouteHop {
1810-
pubkey: PublicKey::from_slice(&<Vec<u8>>::from_hex("032c0b7cf95324a07d05398b240174dc0c2be444d96b159aa6c7f7b1e668680991").unwrap()[..]).unwrap(),
1811-
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
1812-
short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0, maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
1813-
},
1814-
RouteHop {
1815-
pubkey: PublicKey::from_slice(&<Vec<u8>>::from_hex("02edabbd16b41c8371b92ef2f04c1185b4f03b6dcd52ba9b78d9d7c89c8f221145").unwrap()[..]).unwrap(),
1816-
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
1817-
short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0, maybe_announced_channel: true, // We fill in the payloads manually instead of generating them from RouteHops.
1818-
},
1819-
], blinded_tail: None }],
1828+
paths: vec![path],
18201829
route_params: None,
18211830
};
18221831

@@ -2078,6 +2087,21 @@ mod tests {
20782087
);
20792088
let hex = "9c5add3963fc7f6ed7f148623c84134b5647e1306419dbe2174e523fa9e2fbed3a06a19f899145610741c83ad40b7712aefaddec8c6baf7325d92ea4ca4d1df8bce517f7e54554608bf2bd8071a4f52a7a2f7ffbb1413edad81eeea5785aa9d990f2865dc23b4bc3c301a94eec4eabebca66be5cf638f693ec256aec514620cc28ee4a94bd9565bc4d4962b9d3641d4278fb319ed2b84de5b665f307a2db0f7fbb757366067d88c50f7e829138fde4f78d39b5b5802f1b92a8a820865af5cc79f9f30bc3f461c66af95d13e5e1f0381c184572a91dee1c849048a647a1158cf884064deddbf1b0b88dfe2f791428d0ba0f6fb2f04e14081f69165ae66d9297c118f0907705c9c4954a199bae0bb96fad763d690e7daa6cfda59ba7f2c8d11448b604d12d";
20802089
assert_eq!(onion_packet_5.data, <Vec<u8>>::from_hex(hex).unwrap());
2090+
2091+
let logger: Arc<TestLogger> = Arc::new(TestLogger::new());
2092+
let ctx_full = Secp256k1::new();
2093+
let path = build_test_path();
2094+
let htlc_source = HTLCSource::OutboundRoute {
2095+
path: path,
2096+
session_priv: get_test_session_key(),
2097+
first_hop_htlc_msat: 0,
2098+
payment_id: PaymentId([1; 32])
2099+
, };
2100+
2101+
// Assert that the original failure can be retrieved and that all hmacs check out.
2102+
let decrypted_failure = process_onion_failure(&ctx_full, &logger, &htlc_source, onion_packet_5.data);
2103+
2104+
assert_eq!(decrypted_failure.onion_error_code, Some(0x2002));
20812105
}
20822106

20832107
struct RawOnionHopData {

0 commit comments

Comments
 (0)