X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=fuzz%2Fsrc%2Fonion_message.rs;h=a8a290fefb3379a176ff781e8b80c8df1036804f;hb=65efd92c4a931b3b3622c84c6055bc015152fde0;hp=13de5005cf8be1e2fee35f9fc501366bd4e46434;hpb=c6bcf75848cf6b0f0d1f873e211a711697c2b80a;p=rust-lightning diff --git a/fuzz/src/onion_message.rs b/fuzz/src/onion_message.rs index 13de5005..a8a290fe 100644 --- a/fuzz/src/onion_message.rs +++ b/fuzz/src/onion_message.rs @@ -1,21 +1,27 @@ // Imports that need to be added manually -use bitcoin::bech32::u5; +use bech32::u5; use bitcoin::blockdata::script::ScriptBuf; -use bitcoin::secp256k1::{PublicKey, Scalar, Secp256k1, SecretKey}; use bitcoin::secp256k1::ecdh::SharedSecret; use bitcoin::secp256k1::ecdsa::RecoverableSignature; use bitcoin::secp256k1::schnorr; +use bitcoin::secp256k1::{self, PublicKey, Scalar, Secp256k1, SecretKey}; -use lightning::sign::{Recipient, KeyMaterial, EntropySource, NodeSigner, SignerProvider}; +use lightning::blinded_path::{BlindedPath, EmptyNodeIdLookUp}; use lightning::ln::features::InitFeatures; use lightning::ln::msgs::{self, DecodeError, OnionMessageHandler}; use lightning::ln::script::ShutdownScript; use lightning::offers::invoice::UnsignedBolt12Invoice; use lightning::offers::invoice_request::UnsignedInvoiceRequest; -use lightning::util::test_channel_signer::TestChannelSigner; +use lightning::onion_message::messenger::{ + CustomOnionMessageHandler, Destination, MessageRouter, OnionMessagePath, OnionMessenger, + PendingOnionMessage, Responder, ResponseInstruction, +}; +use lightning::onion_message::offers::{OffersMessage, OffersMessageHandler}; +use lightning::onion_message::packet::OnionMessageContents; +use lightning::sign::{EntropySource, KeyMaterial, NodeSigner, Recipient, SignerProvider}; use lightning::util::logger::Logger; use lightning::util::ser::{Readable, Writeable, Writer}; -use lightning::onion_message::{CustomOnionMessageHandler, Destination, MessageRouter, OffersMessage, OffersMessageHandler, OnionMessageContents, OnionMessagePath, OnionMessenger, PendingOnionMessage}; +use lightning::util::test_channel_signer::TestChannelSigner; use crate::utils::test_logger; @@ -29,16 +35,19 @@ pub fn do_test(data: &[u8], logger: &L) { let mut secret_bytes = [1; 32]; secret_bytes[31] = 2; let secret = SecretKey::from_slice(&secret_bytes).unwrap(); - let keys_manager = KeyProvider { - node_secret: secret, - counter: AtomicU64::new(0), - }; + let keys_manager = KeyProvider { node_secret: secret, counter: AtomicU64::new(0) }; + let node_id_lookup = EmptyNodeIdLookUp {}; let message_router = TestMessageRouter {}; let offers_msg_handler = TestOffersMessageHandler {}; let custom_msg_handler = TestCustomMessageHandler {}; let onion_messenger = OnionMessenger::new( - &keys_manager, &keys_manager, logger, &message_router, &offers_msg_handler, - &custom_msg_handler + &keys_manager, + &keys_manager, + logger, + &node_id_lookup, + &message_router, + &offers_msg_handler, + &custom_msg_handler, ); let peer_node_id = { @@ -74,23 +83,29 @@ struct TestMessageRouter {} impl MessageRouter for TestMessageRouter { fn find_path( - &self, _sender: PublicKey, _peers: Vec, destination: Destination + &self, _sender: PublicKey, _peers: Vec, destination: Destination, ) -> Result { - Ok(OnionMessagePath { - intermediate_nodes: vec![], - destination, - }) + Ok(OnionMessagePath { intermediate_nodes: vec![], destination, first_node_addresses: None }) + } + + fn create_blinded_paths( + &self, _recipient: PublicKey, _peers: Vec, _secp_ctx: &Secp256k1, + ) -> Result, ()> { + unreachable!() } } struct TestOffersMessageHandler {} impl OffersMessageHandler for TestOffersMessageHandler { - fn handle_message(&self, _message: OffersMessage) -> Option { - None + fn handle_message( + &self, _message: OffersMessage, _responder: Option, + ) -> ResponseInstruction { + ResponseInstruction::NoResponse } } +#[derive(Debug)] struct TestCustomMessage {} const CUSTOM_MESSAGE_TYPE: u64 = 4242; @@ -100,6 +115,9 @@ impl OnionMessageContents for TestCustomMessage { fn tlv_type(&self) -> u64 { CUSTOM_MESSAGE_TYPE } + fn msg_type(&self) -> &'static str { + "Custom Message" + } } impl Writeable for TestCustomMessage { @@ -112,13 +130,20 @@ struct TestCustomMessageHandler {} impl CustomOnionMessageHandler for TestCustomMessageHandler { type CustomMessage = TestCustomMessage; - fn handle_custom_message(&self, _msg: Self::CustomMessage) -> Option { - Some(TestCustomMessage {}) + fn handle_custom_message( + &self, message: Self::CustomMessage, responder: Option, + ) -> ResponseInstruction { + match responder { + Some(responder) => responder.respond(message), + None => ResponseInstruction::NoResponse, + } } - fn read_custom_message(&self, _message_type: u64, buffer: &mut R) -> Result, msgs::DecodeError> { + fn read_custom_message( + &self, _message_type: u64, buffer: &mut R, + ) -> Result, msgs::DecodeError> { let mut buf = Vec::new(); buffer.read_to_end(&mut buf)?; - return Ok(Some(TestCustomMessage {})) + return Ok(Some(TestCustomMessage {})); } fn release_pending_custom_messages(&self) -> Vec> { vec![] @@ -140,8 +165,11 @@ struct KeyProvider { impl EntropySource for KeyProvider { fn get_secure_random_bytes(&self) -> [u8; 32] { let ctr = self.counter.fetch_add(1, Ordering::Relaxed); - [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - (ctr >> 8*7) as u8, (ctr >> 8*6) as u8, (ctr >> 8*5) as u8, (ctr >> 8*4) as u8, (ctr >> 8*3) as u8, (ctr >> 8*2) as u8, (ctr >> 8*1) as u8, 14, (ctr >> 8*0) as u8] + #[rustfmt::skip] + let random_bytes = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + (ctr >> 8*7) as u8, (ctr >> 8*6) as u8, (ctr >> 8*5) as u8, (ctr >> 8*4) as u8, + (ctr >> 8*3) as u8, (ctr >> 8*2) as u8, (ctr >> 8*1) as u8, 14, (ctr >> 8*0) as u8]; + random_bytes } } @@ -149,15 +177,17 @@ impl NodeSigner for KeyProvider { fn get_node_id(&self, recipient: Recipient) -> Result { let node_secret = match recipient { Recipient::Node => Ok(&self.node_secret), - Recipient::PhantomNode => Err(()) + Recipient::PhantomNode => Err(()), }?; Ok(PublicKey::from_secret_key(&Secp256k1::signing_only(), node_secret)) } - fn ecdh(&self, recipient: Recipient, other_key: &PublicKey, tweak: Option<&Scalar>) -> Result { + fn ecdh( + &self, recipient: Recipient, other_key: &PublicKey, tweak: Option<&Scalar>, + ) -> Result { let mut node_secret = match recipient { Recipient::Node => Ok(self.node_secret.clone()), - Recipient::PhantomNode => Err(()) + Recipient::PhantomNode => Err(()), }?; if let Some(tweak) = tweak { node_secret = node_secret.mul_tweak(tweak).map_err(|_| ())?; @@ -165,14 +195,18 @@ impl NodeSigner for KeyProvider { Ok(SharedSecret::new(other_key, &node_secret)) } - fn get_inbound_payment_key_material(&self) -> KeyMaterial { unreachable!() } + fn get_inbound_payment_key_material(&self) -> KeyMaterial { + unreachable!() + } - fn sign_invoice(&self, _hrp_bytes: &[u8], _invoice_data: &[u5], _recipient: Recipient) -> Result { + fn sign_invoice( + &self, _hrp_bytes: &[u8], _invoice_data: &[u5], _recipient: Recipient, + ) -> Result { unreachable!() } fn sign_bolt12_invoice_request( - &self, _invoice_request: &UnsignedInvoiceRequest + &self, _invoice_request: &UnsignedInvoiceRequest, ) -> Result { unreachable!() } @@ -183,7 +217,9 @@ impl NodeSigner for KeyProvider { unreachable!() } - fn sign_gossip_message(&self, _msg: lightning::ln::msgs::UnsignedGossipMessage) -> Result { + fn sign_gossip_message( + &self, _msg: lightning::ln::msgs::UnsignedGossipMessage, + ) -> Result { unreachable!() } } @@ -193,17 +229,29 @@ impl SignerProvider for KeyProvider { #[cfg(taproot)] type TaprootSigner = TestChannelSigner; - fn generate_channel_keys_id(&self, _inbound: bool, _channel_value_satoshis: u64, _user_channel_id: u128) -> [u8; 32] { unreachable!() } + fn generate_channel_keys_id( + &self, _inbound: bool, _channel_value_satoshis: u64, _user_channel_id: u128, + ) -> [u8; 32] { + unreachable!() + } - fn derive_channel_signer(&self, _channel_value_satoshis: u64, _channel_keys_id: [u8; 32]) -> Self::EcdsaSigner { + fn derive_channel_signer( + &self, _channel_value_satoshis: u64, _channel_keys_id: [u8; 32], + ) -> Self::EcdsaSigner { unreachable!() } - fn read_chan_signer(&self, _data: &[u8]) -> Result { unreachable!() } + fn read_chan_signer(&self, _data: &[u8]) -> Result { + unreachable!() + } - fn get_destination_script(&self, _channel_keys_id: [u8; 32]) -> Result { unreachable!() } + fn get_destination_script(&self, _channel_keys_id: [u8; 32]) -> Result { + unreachable!() + } - fn get_shutdown_scriptpubkey(&self) -> Result { unreachable!() } + fn get_shutdown_scriptpubkey(&self) -> Result { + unreachable!() + } } #[cfg(test)] @@ -218,9 +266,18 @@ mod tests { pub lines: Mutex>, } impl Logger for TrackingLogger { - fn log(&self, record: &Record) { - *self.lines.lock().unwrap().entry((record.module_path.to_string(), format!("{}", record.args))).or_insert(0) += 1; - println!("{:<5} [{} : {}, {}] {}", record.level.to_string(), record.module_path, record.file, record.line, record.args); + fn log(&self, record: Record) { + let mut lines_lock = self.lines.lock().unwrap(); + let key = (record.module_path.to_string(), format!("{}", record.args)); + *lines_lock.entry(key).or_insert(0) += 1; + println!( + "{:<5} [{} : {}, {}] {}", + record.level.to_string(), + record.module_path, + record.file, + record.line, + record.args + ); } } @@ -265,9 +322,12 @@ mod tests { { let log_entries = logger.lines.lock().unwrap(); assert_eq!(log_entries.get(&("lightning::onion_message::messenger".to_string(), - "Received an onion message with path_id None and a reply_path".to_string())), Some(&1)); + "Received an onion message with path_id None and a reply_path: Custom(TestCustomMessage)" + .to_string())), Some(&1)); + assert_eq!(log_entries.get(&("lightning::onion_message::messenger".to_string(), + "Constructing onion message when responding with Custom Message to an onion message with path_id None: TestCustomMessage".to_string())), Some(&1)); assert_eq!(log_entries.get(&("lightning::onion_message::messenger".to_string(), - "Sending onion message when responding to Custom onion message with path_id None".to_string())), Some(&1)); + "Buffered onion message when responding with Custom Message to an onion message with path_id None".to_string())), Some(&1)); } let two_unblinded_hops_om = "\