From 26de8e68ee4d4562e1d9170434fb323f142a4f05 Mon Sep 17 00:00:00 2001 From: David Craven Date: Sat, 18 Jul 2020 22:00:14 +0200 Subject: [PATCH] Fix tests. --- core/src/connection/listeners.rs | 6 +++--- core/src/transport/memory.rs | 6 +++--- core/tests/network_dial_error.rs | 14 +++++++------- core/tests/transport_upgrade.rs | 6 ++++-- muxers/mplex/tests/async_write.rs | 2 +- muxers/mplex/tests/two_peers.rs | 10 ++++++++-- protocols/deflate/tests/test.rs | 3 ++- protocols/identify/src/protocol.rs | 3 ++- protocols/noise/tests/smoke.rs | 5 ++++- protocols/ping/src/protocol.rs | 4 ++-- protocols/plaintext/tests/smoke.rs | 5 ++++- src/lib.rs | 2 +- transports/dns/src/lib.rs | 8 ++++---- transports/tcp/src/lib.rs | 16 ++++++++++------ transports/uds/src/lib.rs | 2 +- transports/websocket/src/lib.rs | 8 ++++---- 16 files changed, 60 insertions(+), 40 deletions(-) diff --git a/core/src/connection/listeners.rs b/core/src/connection/listeners.rs index b905b44421d1..047185a25935 100644 --- a/core/src/connection/listeners.rs +++ b/core/src/connection/listeners.rs @@ -394,7 +394,7 @@ mod tests { let address2 = address.clone(); async_std::task::spawn(async move { - mem_transport.dial(address2).unwrap().await.unwrap(); + mem_transport.dial(Multiaddr::empty(), address2).unwrap().await.unwrap(); }); match listeners.next().await.unwrap() { @@ -427,7 +427,7 @@ mod tests { }))) } - fn dial(self, _: Multiaddr) -> Result> { + fn dial(self, _: Multiaddr, _: Multiaddr) -> Result> { panic!() } } @@ -465,7 +465,7 @@ mod tests { }))) } - fn dial(self, _: Multiaddr) -> Result> { + fn dial(self, _: Multiaddr, _: Multiaddr) -> Result> { panic!() } } diff --git a/core/src/transport/memory.rs b/core/src/transport/memory.rs index f2c3fafada1a..874f7f35d798 100644 --- a/core/src/transport/memory.rs +++ b/core/src/transport/memory.rs @@ -306,9 +306,9 @@ mod tests { #[test] fn port_not_in_use() { let transport = MemoryTransport::default(); - assert!(transport.dial("/memory/810172461024613".parse().unwrap()).is_err()); + assert!(transport.dial(Multiaddr::empty(), "/memory/810172461024613".parse().unwrap()).is_err()); let _listener = transport.listen_on("/memory/810172461024613".parse().unwrap()).unwrap(); - assert!(transport.dial("/memory/810172461024613".parse().unwrap()).is_ok()); + assert!(transport.dial(Multiaddr::empty(), "/memory/810172461024613".parse().unwrap()).is_ok()); } #[test] @@ -342,7 +342,7 @@ mod tests { let t2 = MemoryTransport::default(); let dialer = async move { - let mut socket = t2.dial(cloned_t1_addr).unwrap().await.unwrap(); + let mut socket = t2.dial(Multiaddr::empty(), cloned_t1_addr).unwrap().await.unwrap(); socket.write_all(&msg).await.unwrap(); }; diff --git a/core/tests/network_dial_error.rs b/core/tests/network_dial_error.rs index 630eccc01e18..36b40ebf17e6 100644 --- a/core/tests/network_dial_error.rs +++ b/core/tests/network_dial_error.rs @@ -89,7 +89,7 @@ fn deny_incoming_connec() { swarm2 .peer(swarm1.local_peer_id().clone()) - .dial(address.clone(), Vec::new(), TestHandler()) + .dial("/ip4/127.0.0.1/tcp/0".parse().unwrap(), address.clone(), Vec::new(), TestHandler()) .unwrap(); async_std::task::block_on(future::poll_fn(|cx| -> Poll> { @@ -144,7 +144,7 @@ fn dial_self() { })) .unwrap(); - swarm.dial(&local_address, TestHandler()).unwrap(); + swarm.dial(&"/ip4/127.0.0.1/tcp/0".parse().unwrap(), &local_address, TestHandler()).unwrap(); let mut got_dial_err = false; let mut got_inc_err = false; @@ -215,7 +215,7 @@ fn multiple_addresses_err() { let target = PeerId::random(); swarm.peer(target.clone()) - .dial(first, rest, TestHandler()) + .dial("/ip4/127.0.0.1/tcp/0".parse().unwrap(), first, rest, TestHandler()) .unwrap(); async_std::task::block_on(future::poll_fn(|cx| -> Poll> { @@ -257,13 +257,13 @@ fn connection_limit() { let target = PeerId::random(); for _ in 0 .. outgoing_per_peer_limit { network.peer(target.clone()) - .dial(Multiaddr::empty(), Vec::new(), TestHandler()) + .dial("/ip4/127.0.0.1/tcp/0".parse().unwrap(), Multiaddr::empty(), Vec::new(), TestHandler()) .ok() .expect("Unexpected connection limit."); } let err = network.peer(target) - .dial(Multiaddr::empty(), Vec::new(), TestHandler()) + .dial("/ip4/127.0.0.1/tcp/0".parse().unwrap(), Multiaddr::empty(), Vec::new(), TestHandler()) .expect_err("Unexpected dialing success."); assert_eq!(err.current, outgoing_per_peer_limit); @@ -272,13 +272,13 @@ fn connection_limit() { let target2 = PeerId::random(); for _ in outgoing_per_peer_limit .. outgoing_limit { network.peer(target2.clone()) - .dial(Multiaddr::empty(), Vec::new(), TestHandler()) + .dial("/ip4/127.0.0.1/tcp/0".parse().unwrap(), Multiaddr::empty(), Vec::new(), TestHandler()) .ok() .expect("Unexpected connection limit."); } let err = network.peer(target2) - .dial(Multiaddr::empty(), Vec::new(), TestHandler()) + .dial("/ip4/127.0.0.1/tcp/0".parse().unwrap(), Multiaddr::empty(), Vec::new(), TestHandler()) .expect_err("Unexpected dialing success."); assert_eq!(err.current, outgoing_limit); diff --git a/core/tests/transport_upgrade.rs b/core/tests/transport_upgrade.rs index b4c732b5de29..efc924f62013 100644 --- a/core/tests/transport_upgrade.rs +++ b/core/tests/transport_upgrade.rs @@ -127,11 +127,13 @@ fn upgrade_pipeline() { }; let client = async move { - let (peer, _mplex) = dialer_transport.dial(listen_addr2).unwrap().await.unwrap(); + let (peer, _mplex) = dialer_transport.dial( + "/ip4/127.0.0.1/tcp/0".parse().unwrap(), + listen_addr2, + ).unwrap().await.unwrap(); assert_eq!(peer, listener_id); }; async_std::task::spawn(server); async_std::task::block_on(client); } - diff --git a/muxers/mplex/tests/async_write.rs b/muxers/mplex/tests/async_write.rs index 1414db148475..0c9921b40ae7 100644 --- a/muxers/mplex/tests/async_write.rs +++ b/muxers/mplex/tests/async_write.rs @@ -65,7 +65,7 @@ fn async_write() { let transport = TcpConfig::new().and_then(move |c, e| upgrade::apply(c, mplex, e, upgrade::Version::V1)); - let client = Arc::new(transport.dial(rx.await.unwrap()).unwrap().await.unwrap()); + let client = Arc::new(transport.dial("/ip4/127.0.0.1/tcp/0".parse().unwrap(), rx.await.unwrap()).unwrap().await.unwrap()); let mut inbound = loop { if let Some(s) = muxing::event_from_ref_and_wrap(client.clone()).await.unwrap() .into_inbound_substream() { diff --git a/muxers/mplex/tests/two_peers.rs b/muxers/mplex/tests/two_peers.rs index 54b939a548ab..0185fce23aa8 100644 --- a/muxers/mplex/tests/two_peers.rs +++ b/muxers/mplex/tests/two_peers.rs @@ -65,7 +65,10 @@ fn client_to_server_outbound() { let transport = TcpConfig::new().and_then(move |c, e| upgrade::apply(c, mplex, e, upgrade::Version::V1)); - let client = Arc::new(transport.dial(rx.await.unwrap()).unwrap().await.unwrap()); + let client = Arc::new(transport.dial( + "/ip4/127.0.0.1/tcp/0".parse().unwrap(), + rx.await.unwrap(), + ).unwrap().await.unwrap()); let mut inbound = loop { if let Some(s) = muxing::event_from_ref_and_wrap(client.clone()).await.unwrap() .into_inbound_substream() { @@ -126,7 +129,10 @@ fn client_to_server_inbound() { let transport = TcpConfig::new().and_then(move |c, e| upgrade::apply(c, mplex, e, upgrade::Version::V1)); - let client = transport.dial(rx.await.unwrap()).unwrap().await.unwrap(); + let client = transport.dial( + "/ip4/127.0.0.1/tcp/0".parse().unwrap(), + rx.await.unwrap(), + ).unwrap().await.unwrap(); let mut outbound = muxing::outbound_from_ref_and_wrap(Arc::new(client)).await.unwrap(); outbound.write_all(b"hello world").await.unwrap(); outbound.close().await.unwrap(); diff --git a/protocols/deflate/tests/test.rs b/protocols/deflate/tests/test.rs index 896fb491349f..8d47ad75764a 100644 --- a/protocols/deflate/tests/test.rs +++ b/protocols/deflate/tests/test.rs @@ -82,7 +82,8 @@ async fn run(message1: Vec) { conn.close().await.expect("close") }); - let mut conn = transport.dial(listen_addr).expect("dialer").await.expect("connection"); + let mut conn = transport.dial("/ip4/127.0.0.1/tcp/0".parse().unwrap(), listen_addr) + .expect("dialer").await.expect("connection"); conn.write_all(&message1).await.expect("write_all"); conn.close().await.expect("close"); diff --git a/protocols/identify/src/protocol.rs b/protocols/identify/src/protocol.rs index 79f3bcdd0871..7f37804dae91 100644 --- a/protocols/identify/src/protocol.rs +++ b/protocols/identify/src/protocol.rs @@ -258,7 +258,8 @@ mod tests { async_std::task::block_on(async move { let transport = TcpConfig::new(); - let socket = transport.dial(rx.await.unwrap()).unwrap().await.unwrap(); + let socket = transport.dial("/ip4/127.0.0.1/tcp/0".parse().unwrap(), rx.await.unwrap()) + .unwrap().await.unwrap(); let RemoteInfo { info, observed_addr, .. } = apply_outbound(socket, IdentifyProtocolConfig, upgrade::Version::V1).await.unwrap(); assert_eq!(observed_addr, "/ip4/100.101.102.103/tcp/5000".parse().unwrap()); diff --git a/protocols/noise/tests/smoke.rs b/protocols/noise/tests/smoke.rs index 744d447a247d..3e35f78558e3 100644 --- a/protocols/noise/tests/smoke.rs +++ b/protocols/noise/tests/smoke.rs @@ -203,7 +203,10 @@ where let outbound_msgs = messages.clone(); let client_fut = async { - let mut client_session = client_transport.dial(server_address.clone()) + let mut client_session = client_transport.dial( + "/ip4/127.0.0.1/tcp/0".parse().unwrap(), + server_address.clone(), + ) .unwrap() .await .map(|(_, session)| session) diff --git a/protocols/ping/src/protocol.rs b/protocols/ping/src/protocol.rs index 8e7e963beeff..24bc76fe0d5d 100644 --- a/protocols/ping/src/protocol.rs +++ b/protocols/ping/src/protocol.rs @@ -108,7 +108,7 @@ mod tests { use futures::prelude::*; use libp2p_core::{ upgrade, - multiaddr::multiaddr, + multiaddr::{multiaddr, Multiaddr}, transport::{ Transport, ListenerEvent, @@ -138,7 +138,7 @@ mod tests { }); async_std::task::block_on(async move { - let c = MemoryTransport.dial(listener_addr).unwrap().await.unwrap(); + let c = MemoryTransport.dial(Multiaddr::empty(), listener_addr).unwrap().await.unwrap(); let rtt = upgrade::apply_outbound(c, Ping::default(), upgrade::Version::V1).await.unwrap(); assert!(rtt > Duration::from_secs(0)); }); diff --git a/protocols/plaintext/tests/smoke.rs b/protocols/plaintext/tests/smoke.rs index aedbda21239f..2e35ac4d8ae7 100644 --- a/protocols/plaintext/tests/smoke.rs +++ b/protocols/plaintext/tests/smoke.rs @@ -85,7 +85,10 @@ fn variable_msg_length() { let client_fut = async { debug!("dialing {:?}", server_address); - let (received_server_id, mut client_channel) = client_transport.dial(server_address).unwrap().await.unwrap(); + let (received_server_id, mut client_channel) = client_transport.dial( + "/ip4/127.0.0.1/tcp/0".parse().unwrap(), + server_address, + ).unwrap().await.unwrap(); assert_eq!(received_server_id, server_id.public().into_peer_id()); debug!("Client: writing message."); diff --git a/src/lib.rs b/src/lib.rs index 6f019e19287c..768251a18d04 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -50,7 +50,7 @@ //! use libp2p::{Multiaddr, Transport, tcp::TcpConfig}; //! let tcp = TcpConfig::new(); //! let addr: Multiaddr = "/ip4/98.97.96.95/tcp/20500".parse().expect("invalid multiaddr"); -//! let _conn = tcp.dial(addr); +//! let _conn = tcp.dial("/ip4/0.0.0.0/tcp/0".parse().unwrap(), addr); //! ``` //! In the above example, `_conn` is a [`Future`] that needs to be polled in order for //! the dialing to take place and eventually resolve to a connection. Polling diff --git a/transports/dns/src/lib.rs b/transports/dns/src/lib.rs index 1ec719f17ac2..36e44b7ac793 100644 --- a/transports/dns/src/lib.rs +++ b/transports/dns/src/lib.rs @@ -275,7 +275,7 @@ mod tests { unreachable!() } - fn dial(self, addr: Multiaddr) -> Result> { + fn dial(self, _local_addr: Multiaddr, addr: Multiaddr) -> Result> { let addr = addr.iter().collect::>(); assert_eq!(addr.len(), 2); match addr[1] { @@ -296,20 +296,20 @@ mod tests { let _ = transport .clone() - .dial("/dns4/example.com/tcp/20000".parse().unwrap()) + .dial("/ip4/127.0.0.1/tcp/0".parse().unwrap(), "/dns4/example.com/tcp/20000".parse().unwrap()) .unwrap() .await .unwrap(); let _ = transport .clone() - .dial("/dns6/example.com/tcp/20000".parse().unwrap()) + .dial("/ip4/127.0.0.1/tcp/0".parse().unwrap(), "/dns6/example.com/tcp/20000".parse().unwrap()) .unwrap() .await .unwrap(); let _ = transport - .dial("/ip4/1.2.3.4/tcp/20000".parse().unwrap()) + .dial("/ip4/127.0.0.1/tcp/0".parse().unwrap(), "/ip4/1.2.3.4/tcp/20000".parse().unwrap()) .unwrap() .await .unwrap(); diff --git a/transports/tcp/src/lib.rs b/transports/tcp/src/lib.rs index 475031bb4808..1977cbd2fbd3 100644 --- a/transports/tcp/src/lib.rs +++ b/transports/tcp/src/lib.rs @@ -521,8 +521,8 @@ mod tests { #[test] #[cfg(feature = "async-std")] fn wildcard_expansion() { - fn test(addr: Multiaddr) { - let mut listener = TcpConfig::new().listen_on(addr).expect("listener"); + fn test(default_addr: Multiaddr) { + let mut listener = TcpConfig::new().listen_on(default_addr.clone()).expect("listener"); // Get the first address. let addr = futures::executor::block_on_stream(listener.by_ref()) @@ -554,7 +554,7 @@ mod tests { }) .for_each(|_| futures::future::ready(())); - let client = TcpConfig::new().dial(addr).expect("dialer"); + let client = TcpConfig::new().dial(default_addr, addr).expect("dialer"); async_std::task::block_on(futures::future::join(server, client)).1.unwrap(); } @@ -614,13 +614,14 @@ mod tests { #[test] #[cfg(feature = "async-std")] fn communicating_between_dialer_and_listener() { - fn test(addr: Multiaddr) { + fn test(default_addr: Multiaddr) { let (ready_tx, ready_rx) = futures::channel::oneshot::channel(); let mut ready_tx = Some(ready_tx); + let default_addr2 = default_addr.clone(); async_std::task::spawn(async move { let tcp = TcpConfig::new(); - let mut listener = tcp.listen_on(addr).unwrap(); + let mut listener = tcp.listen_on(default_addr2).unwrap(); loop { match listener.next().await.unwrap().unwrap() { @@ -644,7 +645,10 @@ mod tests { let tcp = TcpConfig::new(); // Obtain a future socket through dialing - let mut socket = tcp.dial(addr.clone()).unwrap().await.unwrap(); + let mut socket = tcp.dial( + default_addr, + addr.clone(), + ).unwrap().await.unwrap(); socket.write_all(&[0x1, 0x2, 0x3]).await.unwrap(); let mut buf = [0u8; 3]; diff --git a/transports/uds/src/lib.rs b/transports/uds/src/lib.rs index 353dbc59de31..a0843d100103 100644 --- a/transports/uds/src/lib.rs +++ b/transports/uds/src/lib.rs @@ -213,7 +213,7 @@ mod tests { async_std::task::block_on(async move { let uds = UdsConfig::new(); let addr = rx.await.unwrap(); - let mut socket = uds.dial(addr).unwrap().await.unwrap(); + let mut socket = uds.dial(Multiaddr::empty(), addr).unwrap().await.unwrap(); socket.write(&[1, 2, 3]).await.unwrap(); }); } diff --git a/transports/websocket/src/lib.rs b/transports/websocket/src/lib.rs index 378e71820f3c..6e55bc2dc02d 100644 --- a/transports/websocket/src/lib.rs +++ b/transports/websocket/src/lib.rs @@ -192,16 +192,16 @@ mod tests { #[test] fn dialer_connects_to_listener_ipv4() { let a = "/ip4/127.0.0.1/tcp/0/ws".parse().unwrap(); - futures::executor::block_on(connect(a)) + futures::executor::block_on(connect("/ip4/127.0.0.1/tcp/0".parse().unwrap(), a)) } #[test] fn dialer_connects_to_listener_ipv6() { let a = "/ip6/::1/tcp/0/ws".parse().unwrap(); - futures::executor::block_on(connect(a)) + futures::executor::block_on(connect("/ip6/::1/tcp/0".parse().unwrap(), a)) } - async fn connect(listen_addr: Multiaddr) { + async fn connect(local_addr: Multiaddr, listen_addr: Multiaddr) { let ws_config = WsConfig::new(tcp::TcpConfig::new()); let mut listener = ws_config.clone() @@ -226,7 +226,7 @@ mod tests { conn.await }; - let outbound = ws_config.dial(addr).unwrap(); + let outbound = ws_config.dial(local_addr, addr).unwrap(); let (a, b) = futures::join!(inbound, outbound); a.and(b).unwrap();