Fix accidental newline in OnionMessenger docs
[rust-lightning] / lightning / src / onion_message / functional_tests.rs
index 1b1ce0a6e253519cfe9ebed0b3da74e8803a7d36..1521b9d77ef6eafd6ef34325c4105b4f33b34b69 100644 (file)
 
 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<test_utils::TestKeysInterface>,
-       messenger: OnionMessenger<EnforcingSigner, Arc<test_utils::TestKeysInterface>, Arc<test_utils::TestLogger>>,
+       messenger: OnionMessenger<EnforcingSigner, Arc<test_utils::TestKeysInterface>, Arc<test_utils::TestLogger>, Arc<TestCustomMessageHandler>>,
        logger: Arc<test_utils::TestLogger>,
 }
 
@@ -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<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
+               Ok(CUSTOM_MESSAGE_CONTENTS.write(w)?)
+       }
+}
+
+impl MaybeReadableArgs<u64> for TestCustomMessage {
+       fn read<R: io::Read>(buffer: &mut R, message_type: u64) -> Result<Option<Self>, 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<MessengerNode> {
        let mut nodes = Vec::new();
        for i in 0..num_messengers {
@@ -42,13 +81,13 @@ fn create_nodes(num_messengers: u8) -> Vec<MessengerNode> {
                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]