2021-08-03 14:55:06 +02:00
|
|
|
// Copyright 2018 Parity Technologies (UK) Ltd.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the "Software"),
|
|
|
|
// to deal in the Software without restriction, including without limitation
|
|
|
|
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
// and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
// Software is furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
// DEALINGS IN THE SOFTWARE.use futures::StreamExt;
|
2023-03-08 20:36:35 +11:00
|
|
|
use futures::future::Either;
|
2022-12-13 07:58:01 +11:00
|
|
|
use libp2p_mdns::{tokio::Behaviour, Config, Event};
|
|
|
|
use libp2p_swarm::Swarm;
|
2023-03-08 20:36:35 +11:00
|
|
|
use libp2p_swarm_test::SwarmExt as _;
|
2021-12-06 16:52:31 +01:00
|
|
|
use std::time::Duration;
|
2021-08-03 14:55:06 +02:00
|
|
|
|
2022-09-01 23:53:38 -04:00
|
|
|
#[tokio::test]
|
2023-03-08 20:36:35 +11:00
|
|
|
async fn test_discovery_tokio_ipv4() {
|
|
|
|
env_logger::try_init().ok();
|
|
|
|
|
2022-11-18 01:12:23 +00:00
|
|
|
run_discovery_test(Config::default()).await
|
2022-09-01 23:53:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
2023-03-08 20:36:35 +11:00
|
|
|
async fn test_discovery_tokio_ipv6() {
|
|
|
|
env_logger::try_init().ok();
|
|
|
|
|
2022-11-18 01:12:23 +00:00
|
|
|
let config = Config {
|
2022-09-01 23:53:38 -04:00
|
|
|
enable_ipv6: true,
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
run_discovery_test(config).await
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
2023-03-08 20:36:35 +11:00
|
|
|
async fn test_expired_tokio() {
|
2022-09-01 23:53:38 -04:00
|
|
|
env_logger::try_init().ok();
|
2023-03-08 20:36:35 +11:00
|
|
|
|
2022-11-18 01:12:23 +00:00
|
|
|
let config = Config {
|
2022-09-01 23:53:38 -04:00
|
|
|
ttl: Duration::from_secs(1),
|
|
|
|
query_interval: Duration::from_secs(10),
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
|
2023-03-08 20:36:35 +11:00
|
|
|
let mut a = create_swarm(config.clone()).await;
|
|
|
|
let a_peer_id = *a.local_peer_id();
|
2022-09-01 23:53:38 -04:00
|
|
|
|
2023-03-08 20:36:35 +11:00
|
|
|
let mut b = create_swarm(config).await;
|
|
|
|
let b_peer_id = *b.local_peer_id();
|
2021-08-03 14:55:06 +02:00
|
|
|
|
|
|
|
loop {
|
2023-03-08 20:36:35 +11:00
|
|
|
match futures::future::select(a.next_behaviour_event(), b.next_behaviour_event()).await {
|
|
|
|
Either::Left((Event::Expired(mut peers), _)) => {
|
|
|
|
if peers.any(|(p, _)| p == b_peer_id) {
|
|
|
|
return;
|
2021-08-03 14:55:06 +02:00
|
|
|
}
|
2023-03-08 20:36:35 +11:00
|
|
|
}
|
|
|
|
Either::Right((Event::Expired(mut peers), _)) => {
|
|
|
|
if peers.any(|(p, _)| p == a_peer_id) {
|
|
|
|
return;
|
2021-08-03 14:55:06 +02:00
|
|
|
}
|
|
|
|
}
|
2023-03-08 20:36:35 +11:00
|
|
|
_ => {}
|
2021-08-03 14:55:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-08 20:36:35 +11:00
|
|
|
async fn run_discovery_test(config: Config) {
|
|
|
|
let mut a = create_swarm(config.clone()).await;
|
|
|
|
let a_peer_id = *a.local_peer_id();
|
2021-12-06 16:52:31 +01:00
|
|
|
|
2023-03-08 20:36:35 +11:00
|
|
|
let mut b = create_swarm(config).await;
|
|
|
|
let b_peer_id = *b.local_peer_id();
|
|
|
|
|
|
|
|
let mut discovered_a = false;
|
|
|
|
let mut discovered_b = false;
|
|
|
|
|
|
|
|
while !discovered_a && !discovered_b {
|
|
|
|
match futures::future::select(a.next_behaviour_event(), b.next_behaviour_event()).await {
|
|
|
|
Either::Left((Event::Discovered(mut peers), _)) => {
|
|
|
|
if peers.any(|(p, _)| p == b_peer_id) {
|
|
|
|
discovered_b = true;
|
2021-12-06 16:52:31 +01:00
|
|
|
}
|
2023-03-08 20:36:35 +11:00
|
|
|
}
|
|
|
|
Either::Right((Event::Discovered(mut peers), _)) => {
|
|
|
|
if peers.any(|(p, _)| p == a_peer_id) {
|
|
|
|
discovered_a = true;
|
2022-09-01 23:53:38 -04:00
|
|
|
}
|
2021-12-06 16:52:31 +01:00
|
|
|
}
|
2023-03-08 20:36:35 +11:00
|
|
|
_ => {}
|
2021-12-06 16:52:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-08 20:36:35 +11:00
|
|
|
|
|
|
|
async fn create_swarm(config: Config) -> Swarm<Behaviour> {
|
|
|
|
let mut swarm =
|
|
|
|
Swarm::new_ephemeral(|key| Behaviour::new(config, key.public().to_peer_id()).unwrap());
|
|
|
|
swarm.listen().await;
|
|
|
|
|
|
|
|
swarm
|
|
|
|
}
|