From d5475fccde0c6065911416df15ba618a672fcbe4 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 27 Jun 2023 03:21:47 +0200 Subject: [PATCH] fix(test): only listen on `127.0.0.1` for ephemeral swarm This resolves an issue where our tests were depending on the number of network interfaces available on the local machine. Related #4110. Pull-Request: #4122. Co-Authored-By: Thomas Eizinger --- misc/allow-block-list/src/lib.rs | 2 +- protocols/kad/tests/client_mode.rs | 2 +- protocols/mdns/tests/use-async-std.rs | 15 ++++++++++++++- protocols/mdns/tests/use-tokio.rs | 17 +++++++++++++++-- swarm-test/src/lib.rs | 2 +- 5 files changed, 32 insertions(+), 6 deletions(-) diff --git a/misc/allow-block-list/src/lib.rs b/misc/allow-block-list/src/lib.rs index 7aa0dd87..a9e3280c 100644 --- a/misc/allow-block-list/src/lib.rs +++ b/misc/allow-block-list/src/lib.rs @@ -418,7 +418,7 @@ mod tests { let ( [SwarmEvent::OutgoingConnectionError { error: DialError::Denied { cause: outgoing_cause }, .. }], - [_, _, _, SwarmEvent::IncomingConnectionError { error: ListenError::Denied { cause: incoming_cause }, .. }], + [_, SwarmEvent::IncomingConnectionError { error: ListenError::Denied { cause: incoming_cause }, .. }], ) = libp2p_swarm_test::drive(&mut dialer, &mut listener).await else { panic!("unexpected events") }; diff --git a/protocols/kad/tests/client_mode.rs b/protocols/kad/tests/client_mode.rs index b187b6b9..64197942 100644 --- a/protocols/kad/tests/client_mode.rs +++ b/protocols/kad/tests/client_mode.rs @@ -59,7 +59,7 @@ async fn two_servers_add_each_other_to_routing_table() { match libp2p_swarm_test::drive(&mut server2, &mut server1).await { ( - [Identify(_), Kad(RoutingUpdated { peer: peer2, .. }), Identify(_)], + [Identify(_), Kad(UnroutablePeer { .. }), Identify(_), Kad(RoutingUpdated { peer: peer2, .. }), Identify(_)], [Identify(_), Identify(_)], ) => { assert_eq!(peer2, server1_peer_id); diff --git a/protocols/mdns/tests/use-async-std.rs b/protocols/mdns/tests/use-async-std.rs index bfc3cd12..6d45d92c 100644 --- a/protocols/mdns/tests/use-async-std.rs +++ b/protocols/mdns/tests/use-async-std.rs @@ -148,7 +148,20 @@ async fn run_discovery_test(config: Config) { async fn create_swarm(config: Config) -> Swarm { let mut swarm = Swarm::new_ephemeral(|key| Behaviour::new(config, key.public().to_peer_id()).unwrap()); - swarm.listen().await; + + // Manually listen on all interfaces because mDNS only works for non-loopback addresses. + let expected_listener_id = swarm + .listen_on("/ip4/0.0.0.0/tcp/0".parse().unwrap()) + .unwrap(); + + swarm + .wait(|e| match e { + SwarmEvent::NewListenAddr { listener_id, .. } => { + (listener_id == expected_listener_id).then_some(()) + } + _ => None, + }) + .await; swarm } diff --git a/protocols/mdns/tests/use-tokio.rs b/protocols/mdns/tests/use-tokio.rs index 22941843..50d6be0c 100644 --- a/protocols/mdns/tests/use-tokio.rs +++ b/protocols/mdns/tests/use-tokio.rs @@ -19,7 +19,7 @@ // DEALINGS IN THE SOFTWARE.use futures::StreamExt; use futures::future::Either; use libp2p_mdns::{tokio::Behaviour, Config, Event}; -use libp2p_swarm::Swarm; +use libp2p_swarm::{Swarm, SwarmEvent}; use libp2p_swarm_test::SwarmExt as _; use std::time::Duration; @@ -104,7 +104,20 @@ async fn run_discovery_test(config: Config) { async fn create_swarm(config: Config) -> Swarm { let mut swarm = Swarm::new_ephemeral(|key| Behaviour::new(config, key.public().to_peer_id()).unwrap()); - swarm.listen().await; + + // Manually listen on all interfaces because mDNS only works for non-loopback addresses. + let expected_listener_id = swarm + .listen_on("/ip4/0.0.0.0/tcp/0".parse().unwrap()) + .unwrap(); + + swarm + .wait(|e| match e { + SwarmEvent::NewListenAddr { listener_id, .. } => { + (listener_id == expected_listener_id).then_some(()) + } + _ => None, + }) + .await; swarm } diff --git a/swarm-test/src/lib.rs b/swarm-test/src/lib.rs index d7d54383..0ed8dbce 100644 --- a/swarm-test/src/lib.rs +++ b/swarm-test/src/lib.rs @@ -312,7 +312,7 @@ where self.add_external_address(memory_multiaddr.clone()); let tcp_addr_listener_id = self - .listen_on("/ip4/0.0.0.0/tcp/0".parse().unwrap()) + .listen_on("/ip4/127.0.0.1/tcp/0".parse().unwrap()) .unwrap(); let tcp_multiaddr = self