X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fonion_message%2Ffunctional_tests.rs;h=1521b9d77ef6eafd6ef34325c4105b4f33b34b69;hb=87b637c990e4c7aec7f62544f2d313e2d59db115;hp=1b1ce0a6e253519cfe9ebed0b3da74e8803a7d36;hpb=d2a9ae0b8eb5bdb1724be0b8d398d0fdf9870266;p=rust-lightning diff --git a/lightning/src/onion_message/functional_tests.rs b/lightning/src/onion_message/functional_tests.rs index 1b1ce0a6..1521b9d7 100644 --- a/lightning/src/onion_message/functional_tests.rs +++ b/lightning/src/onion_message/functional_tests.rs @@ -11,19 +11,21 @@ use chain::keysinterface::{KeysInterface, Recipient}; use ln::features::InitFeatures; -use ln::msgs::{self, OnionMessageHandler}; -use super::{BlindedRoute, Destination, OnionMessenger, SendError}; +use ln::msgs::{self, DecodeError, OnionMessageHandler}; +use super::{BlindedRoute, CustomOnionMessageContents, CustomOnionMessageHandler, Destination, OnionMessenger, SendError}; use util::enforcing_trait_impls::EnforcingSigner; +use util::ser::{MaybeReadableArgs, Writeable, Writer}; use util::test_utils; use bitcoin::network::constants::Network; use bitcoin::secp256k1::{PublicKey, Secp256k1}; +use io; use sync::Arc; struct MessengerNode { keys_manager: Arc, - messenger: OnionMessenger, Arc>, + messenger: OnionMessenger, Arc, Arc>, logger: Arc, } @@ -34,6 +36,43 @@ impl MessengerNode { } } +#[derive(Clone)] +struct TestCustomMessage {} + +const CUSTOM_MESSAGE_TYPE: u64 = 4242; +const CUSTOM_MESSAGE_CONTENTS: [u8; 32] = [42; 32]; + +impl CustomOnionMessageContents for TestCustomMessage { + fn tlv_type(&self) -> u64 { + CUSTOM_MESSAGE_TYPE + } +} + +impl Writeable for TestCustomMessage { + fn write(&self, w: &mut W) -> Result<(), io::Error> { + Ok(CUSTOM_MESSAGE_CONTENTS.write(w)?) + } +} + +impl MaybeReadableArgs for TestCustomMessage { + fn read(buffer: &mut R, message_type: u64) -> Result, DecodeError> where Self: Sized { + if message_type == CUSTOM_MESSAGE_TYPE { + let mut buf = Vec::new(); + buffer.read_to_end(&mut buf)?; + assert_eq!(buf, CUSTOM_MESSAGE_CONTENTS); + return Ok(Some(TestCustomMessage {})) + } + Ok(None) + } +} + +struct TestCustomMessageHandler {} + +impl CustomOnionMessageHandler for TestCustomMessageHandler { + type CustomMessage = TestCustomMessage; + fn handle_custom_message(&self, _msg: Self::CustomMessage) {} +} + fn create_nodes(num_messengers: u8) -> Vec { let mut nodes = Vec::new(); for i in 0..num_messengers { @@ -42,13 +81,13 @@ fn create_nodes(num_messengers: u8) -> Vec { let keys_manager = Arc::new(test_utils::TestKeysInterface::new(&seed, Network::Testnet)); nodes.push(MessengerNode { keys_manager: keys_manager.clone(), - messenger: OnionMessenger::new(keys_manager, logger.clone()), + messenger: OnionMessenger::new(keys_manager, logger.clone(), Arc::new(TestCustomMessageHandler {})), logger, }); } for idx in 0..num_messengers - 1 { let i = idx as usize; - let mut features = InitFeatures::known(); + let mut features = InitFeatures::empty(); features.set_onion_messages_optional(); let init_msg = msgs::Init { features, remote_network_address: None }; nodes[i].messenger.peer_connected(&nodes[i + 1].get_node_pk(), &init_msg.clone()).unwrap(); @@ -158,7 +197,7 @@ fn reply_path() { // Make sure the last node successfully decoded the reply path. nodes[3].logger.assert_log_contains( "lightning::onion_message::messenger".to_string(), - format!("Received an onion message with path_id: None and reply_path").to_string(), 1); + format!("Received an onion message with path_id None and a reply_path").to_string(), 1); // Destination::BlindedRoute let blinded_route = BlindedRoute::new(&[nodes[1].get_node_pk(), nodes[2].get_node_pk(), nodes[3].get_node_pk()], &*nodes[3].keys_manager, &secp_ctx).unwrap(); @@ -168,7 +207,7 @@ fn reply_path() { pass_along_path(&nodes, None); nodes[3].logger.assert_log_contains( "lightning::onion_message::messenger".to_string(), - format!("Received an onion message with path_id: None and reply_path").to_string(), 2); + format!("Received an onion message with path_id None and a reply_path").to_string(), 2); } #[test]