OM functional tests: update util to take nodes by reference
[rust-lightning] / lightning / src / onion_message / functional_tests.rs
1 // This file is Copyright its original authors, visible in version control
2 // history.
3 //
4 // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5 // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7 // You may not use this file except in accordance with one or both of these
8 // licenses.
9
10 //! Onion message testing and test utilities live here.
11
12 use chain::keysinterface::{KeysInterface, Recipient};
13 use super::{BlindedRoute, Destination, OnionMessenger, SendError};
14 use util::enforcing_trait_impls::EnforcingSigner;
15 use util::test_utils;
16
17 use bitcoin::network::constants::Network;
18 use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
19
20 use sync::Arc;
21
22 struct MessengerNode {
23         keys_manager: Arc<test_utils::TestKeysInterface>,
24         messenger: OnionMessenger<EnforcingSigner, Arc<test_utils::TestKeysInterface>, Arc<test_utils::TestLogger>>,
25         logger: Arc<test_utils::TestLogger>,
26 }
27
28 impl MessengerNode {
29         fn get_node_pk(&self) -> PublicKey {
30                 let secp_ctx = Secp256k1::new();
31                 PublicKey::from_secret_key(&secp_ctx, &self.keys_manager.get_node_secret(Recipient::Node).unwrap())
32         }
33 }
34
35 fn create_nodes(num_messengers: u8) -> Vec<MessengerNode> {
36         let mut res = Vec::new();
37         for i in 0..num_messengers {
38                 let logger = Arc::new(test_utils::TestLogger::with_id(format!("node {}", i)));
39                 let seed = [i as u8; 32];
40                 let keys_manager = Arc::new(test_utils::TestKeysInterface::new(&seed, Network::Testnet));
41                 res.push(MessengerNode {
42                         keys_manager: keys_manager.clone(),
43                         messenger: OnionMessenger::new(keys_manager, logger.clone()),
44                         logger,
45                 });
46         }
47         res
48 }
49
50 fn pass_along_path(path: &Vec<MessengerNode>, expected_path_id: Option<[u8; 32]>) {
51         let mut prev_node = &path[0];
52         let num_nodes = path.len();
53         for (idx, node) in path.into_iter().skip(1).enumerate() {
54                 let events = prev_node.messenger.release_pending_msgs();
55                 assert_eq!(events.len(), 1);
56                 let onion_msg =  {
57                         let msgs = events.get(&node.get_node_pk()).unwrap();
58                         assert_eq!(msgs.len(), 1);
59                         msgs[0].clone()
60                 };
61                 node.messenger.handle_onion_message(&prev_node.get_node_pk(), &onion_msg);
62                 if idx == num_nodes - 1 {
63                         node.logger.assert_log_contains(
64                                 "lightning::onion_message::messenger".to_string(),
65                                 format!("Received an onion message with path_id: {:02x?}", expected_path_id).to_string(), 1);
66                 }
67                 prev_node = node;
68         }
69 }
70
71 #[test]
72 fn one_hop() {
73         let nodes = create_nodes(2);
74
75         nodes[0].messenger.send_onion_message(&[], Destination::Node(nodes[1].get_node_pk())).unwrap();
76         pass_along_path(&nodes, None);
77 }
78
79 #[test]
80 fn two_unblinded_hops() {
81         let nodes = create_nodes(3);
82
83         nodes[0].messenger.send_onion_message(&[nodes[1].get_node_pk()], Destination::Node(nodes[2].get_node_pk())).unwrap();
84         pass_along_path(&nodes, None);
85 }
86
87 #[test]
88 fn two_unblinded_two_blinded() {
89         let nodes = create_nodes(5);
90
91         let secp_ctx = Secp256k1::new();
92         let blinded_route = BlindedRoute::new::<EnforcingSigner, _, _>(&[nodes[3].get_node_pk(), nodes[4].get_node_pk()], &*nodes[4].keys_manager, &secp_ctx).unwrap();
93
94         nodes[0].messenger.send_onion_message(&[nodes[1].get_node_pk(), nodes[2].get_node_pk()], Destination::BlindedRoute(blinded_route)).unwrap();
95         pass_along_path(&nodes, None);
96 }
97
98 #[test]
99 fn three_blinded_hops() {
100         let nodes = create_nodes(4);
101
102         let secp_ctx = Secp256k1::new();
103         let blinded_route = BlindedRoute::new::<EnforcingSigner, _, _>(&[nodes[1].get_node_pk(), nodes[2].get_node_pk(), nodes[3].get_node_pk()], &*nodes[3].keys_manager, &secp_ctx).unwrap();
104
105         nodes[0].messenger.send_onion_message(&[], Destination::BlindedRoute(blinded_route)).unwrap();
106         pass_along_path(&nodes, None);
107 }
108
109 #[test]
110 fn too_big_packet_error() {
111         // Make sure we error as expected if a packet is too big to send.
112         let nodes = create_nodes(1);
113
114         let hop_secret = SecretKey::from_slice(&hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap();
115         let secp_ctx = Secp256k1::new();
116         let hop_node_id = PublicKey::from_secret_key(&secp_ctx, &hop_secret);
117
118         let hops = [hop_node_id; 400];
119         let err = nodes[0].messenger.send_onion_message(&hops, Destination::Node(hop_node_id)).unwrap_err();
120         assert_eq!(err, SendError::TooBigPacket);
121 }
122
123 #[test]
124 fn invalid_blinded_route_error() {
125         // Make sure we error as expected if a provided blinded route has 0 or 1 hops.
126         let mut nodes = create_nodes(3);
127
128         // 0 hops
129         let secp_ctx = Secp256k1::new();
130         let mut blinded_route = BlindedRoute::new::<EnforcingSigner, _, _>(&[nodes[1].get_node_pk(), nodes[2].get_node_pk()], &*nodes[2].keys_manager, &secp_ctx).unwrap();
131         blinded_route.blinded_hops.clear();
132         let err = nodes[0].messenger.send_onion_message(&[], Destination::BlindedRoute(blinded_route)).unwrap_err();
133         assert_eq!(err, SendError::TooFewBlindedHops);
134
135         // 1 hop
136         let mut blinded_route = BlindedRoute::new::<EnforcingSigner, _, _>(&[nodes[1].get_node_pk(), nodes[2].get_node_pk()], &*nodes[2].keys_manager, &secp_ctx).unwrap();
137         blinded_route.blinded_hops.remove(0);
138         assert_eq!(blinded_route.blinded_hops.len(), 1);
139         let err = nodes[0].messenger.send_onion_message(&[], Destination::BlindedRoute(blinded_route)).unwrap_err();
140         assert_eq!(err, SendError::TooFewBlindedHops);
141 }