Do a real handshake to set up a network in peer_handler tests 2020-02-peer_handler-handshake
authorMatt Corallo <git@bluematt.me>
Wed, 19 Feb 2020 19:30:47 +0000 (14:30 -0500)
committerMatt Corallo <git@bluematt.me>
Wed, 19 Feb 2020 19:30:47 +0000 (14:30 -0500)
lightning/src/ln/peer_handler.rs

index eafb2bde7c7874497e59e1470141304aeee9b991..4827f1935a781a3ffa983eabe2cc48bfcdaeb6ee 100644 (file)
@@ -1124,15 +1124,28 @@ mod tests {
 
        use rand::{thread_rng, Rng};
 
-       use std::sync::{Arc};
+       use std::sync::{Arc, Mutex};
 
-       #[derive(PartialEq, Eq, Clone, Hash)]
+       #[derive(Clone)]
        struct FileDescriptor {
                fd: u16,
+               outbound_data: Arc<Mutex<Vec<u8>>>,
+       }
+       impl PartialEq for FileDescriptor {
+               fn eq(&self, other: &Self) -> bool {
+                       self.fd == other.fd
+               }
+       }
+       impl Eq for FileDescriptor { }
+       impl std::hash::Hash for FileDescriptor {
+               fn hash<H: std::hash::Hasher>(&self, hasher: &mut H) {
+                       self.fd.hash(hasher)
+               }
        }
 
        impl SocketDescriptor for FileDescriptor {
                fn send_data(&mut self, data: &[u8], _resume_read: bool) -> usize {
+                       self.outbound_data.lock().unwrap().extend_from_slice(data);
                        data.len()
                }
 
@@ -1173,10 +1186,15 @@ mod tests {
 
        fn establish_connection<'a>(peer_a: &PeerManager<FileDescriptor, &'a test_utils::TestChannelMessageHandler>, peer_b: &PeerManager<FileDescriptor, &'a test_utils::TestChannelMessageHandler>) {
                let secp_ctx = Secp256k1::new();
-               let their_id = PublicKey::from_secret_key(&secp_ctx, &peer_b.our_node_secret);
-               let fd = FileDescriptor { fd: 1};
-               peer_a.new_inbound_connection(fd.clone()).unwrap();
-               peer_a.peers.lock().unwrap().node_id_to_descriptor.insert(their_id, fd.clone());
+               let a_id = PublicKey::from_secret_key(&secp_ctx, &peer_a.our_node_secret);
+               //let b_id = PublicKey::from_secret_key(&secp_ctx, &peer_b.our_node_secret);
+               let mut fd_a = FileDescriptor { fd: 1, outbound_data: Arc::new(Mutex::new(Vec::new())) };
+               let mut fd_b = FileDescriptor { fd: 1, outbound_data: Arc::new(Mutex::new(Vec::new())) };
+               let initial_data = peer_b.new_outbound_connection(a_id, fd_b.clone()).unwrap();
+               peer_a.new_inbound_connection(fd_a.clone()).unwrap();
+               assert_eq!(peer_a.read_event(&mut fd_a, initial_data).unwrap(), false);
+               assert_eq!(peer_b.read_event(&mut fd_b, fd_a.outbound_data.lock().unwrap().split_off(0)).unwrap(), false);
+               assert_eq!(peer_a.read_event(&mut fd_a, fd_b.outbound_data.lock().unwrap().split_off(0)).unwrap(), false);
        }
 
        #[test]