2019-04-16 15:57:29 +02:00
|
|
|
// Copyright 2019 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.
|
|
|
|
|
|
|
|
//! Integration tests for the `Ping` network behaviour.
|
|
|
|
|
2023-01-18 13:35:07 +11:00
|
|
|
use either::Either;
|
2021-08-11 13:12:12 +02:00
|
|
|
use futures::{channel::mpsc, prelude::*};
|
2022-12-13 07:58:01 +11:00
|
|
|
use libp2p_core::{
|
2019-04-16 15:57:29 +02:00
|
|
|
identity,
|
2019-11-25 17:33:59 +01:00
|
|
|
muxing::StreamMuxerBox,
|
2020-10-07 11:10:54 +02:00
|
|
|
transport::{self, Transport},
|
2021-08-11 13:12:12 +02:00
|
|
|
upgrade, Multiaddr, PeerId,
|
2019-04-16 15:57:29 +02:00
|
|
|
};
|
2022-12-13 07:58:01 +11:00
|
|
|
use libp2p_mplex as mplex;
|
|
|
|
use libp2p_noise as noise;
|
|
|
|
use libp2p_ping as ping;
|
2022-10-06 03:50:11 +11:00
|
|
|
use libp2p_swarm::keep_alive;
|
2022-12-13 07:58:01 +11:00
|
|
|
use libp2p_swarm::{NetworkBehaviour, Swarm, SwarmEvent};
|
|
|
|
use libp2p_tcp as tcp;
|
|
|
|
use libp2p_yamux as yamux;
|
2020-08-10 12:54:55 +02:00
|
|
|
use quickcheck::*;
|
2020-10-16 16:53:02 +02:00
|
|
|
use std::{num::NonZeroU8, time::Duration};
|
2019-04-16 15:57:29 +02:00
|
|
|
|
|
|
|
#[test]
|
2020-08-10 12:54:55 +02:00
|
|
|
fn ping_pong() {
|
2020-09-28 10:57:02 +02:00
|
|
|
fn prop(count: NonZeroU8, muxer: MuxerChoice) {
|
2022-10-06 03:50:11 +11:00
|
|
|
let cfg = ping::Config::new().with_interval(Duration::from_millis(10));
|
2019-04-25 10:33:57 +02:00
|
|
|
|
2020-09-28 10:57:02 +02:00
|
|
|
let (peer1_id, trans) = mk_transport(muxer);
|
2022-11-15 15:26:03 +01:00
|
|
|
let mut swarm1 =
|
|
|
|
Swarm::with_async_std_executor(trans, Behaviour::new(cfg.clone()), peer1_id);
|
2019-04-16 15:57:29 +02:00
|
|
|
|
2020-09-28 10:57:02 +02:00
|
|
|
let (peer2_id, trans) = mk_transport(muxer);
|
2022-11-15 15:26:03 +01:00
|
|
|
let mut swarm2 = Swarm::with_async_std_executor(trans, Behaviour::new(cfg), peer2_id);
|
2019-04-16 15:57:29 +02:00
|
|
|
|
2020-08-10 12:54:55 +02:00
|
|
|
let (mut tx, mut rx) = mpsc::channel::<Multiaddr>(1);
|
2019-04-16 15:57:29 +02:00
|
|
|
|
2022-10-04 18:24:38 +11:00
|
|
|
let pid1 = peer1_id;
|
2020-08-10 12:54:55 +02:00
|
|
|
let addr = "/ip4/127.0.0.1/tcp/0".parse().unwrap();
|
2021-03-18 14:55:33 +01:00
|
|
|
swarm1.listen_on(addr).unwrap();
|
2019-11-25 17:33:59 +01:00
|
|
|
|
2020-08-10 12:54:55 +02:00
|
|
|
let mut count1 = count.get();
|
|
|
|
let mut count2 = count.get();
|
2019-11-25 17:33:59 +01:00
|
|
|
|
2020-08-10 12:54:55 +02:00
|
|
|
let peer1 = async move {
|
|
|
|
loop {
|
2021-06-14 20:41:44 +02:00
|
|
|
match swarm1.select_next_some().await {
|
2021-07-08 11:41:33 +02:00
|
|
|
SwarmEvent::NewListenAddr { address, .. } => tx.send(address).await.unwrap(),
|
2022-10-06 03:50:11 +11:00
|
|
|
SwarmEvent::Behaviour(BehaviourEvent::Ping(ping::Event {
|
2021-08-11 13:12:12 +02:00
|
|
|
peer,
|
2021-09-07 00:10:48 +10:00
|
|
|
result: Ok(ping::Success::Ping { rtt }),
|
2022-10-06 03:50:11 +11:00
|
|
|
})) => {
|
2020-08-10 12:54:55 +02:00
|
|
|
count1 -= 1;
|
|
|
|
if count1 == 0 {
|
2022-10-04 18:24:38 +11:00
|
|
|
return (pid1, peer, rtt);
|
2020-08-10 12:54:55 +02:00
|
|
|
}
|
2021-08-11 13:12:12 +02:00
|
|
|
}
|
2022-10-06 03:50:11 +11:00
|
|
|
SwarmEvent::Behaviour(BehaviourEvent::Ping(ping::Event {
|
|
|
|
result: Err(e),
|
|
|
|
..
|
|
|
|
})) => {
|
2022-12-14 16:45:04 +01:00
|
|
|
panic!("Ping failure: {e:?}")
|
2021-08-11 13:12:12 +02:00
|
|
|
}
|
2020-08-10 12:54:55 +02:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-10-04 18:24:38 +11:00
|
|
|
let pid2 = peer2_id;
|
2020-08-10 12:54:55 +02:00
|
|
|
let peer2 = async move {
|
2021-11-15 14:17:23 +01:00
|
|
|
swarm2.dial(rx.next().await.unwrap()).unwrap();
|
2020-08-10 12:54:55 +02:00
|
|
|
|
|
|
|
loop {
|
2021-06-14 20:41:44 +02:00
|
|
|
match swarm2.select_next_some().await {
|
2022-10-06 03:50:11 +11:00
|
|
|
SwarmEvent::Behaviour(BehaviourEvent::Ping(ping::Event {
|
2021-07-31 06:21:21 +10:00
|
|
|
peer,
|
2021-09-07 00:10:48 +10:00
|
|
|
result: Ok(ping::Success::Ping { rtt }),
|
2022-10-06 03:50:11 +11:00
|
|
|
})) => {
|
2020-08-10 12:54:55 +02:00
|
|
|
count2 -= 1;
|
|
|
|
if count2 == 0 {
|
2022-10-04 18:24:38 +11:00
|
|
|
return (pid2, peer, rtt);
|
2020-08-10 12:54:55 +02:00
|
|
|
}
|
2021-08-11 13:12:12 +02:00
|
|
|
}
|
2022-10-06 03:50:11 +11:00
|
|
|
SwarmEvent::Behaviour(BehaviourEvent::Ping(ping::Event {
|
|
|
|
result: Err(e),
|
|
|
|
..
|
|
|
|
})) => {
|
2022-12-14 16:45:04 +01:00
|
|
|
panic!("Ping failure: {e:?}")
|
2021-08-11 13:12:12 +02:00
|
|
|
}
|
2020-08-10 12:54:55 +02:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let result = future::select(Box::pin(peer1), Box::pin(peer2));
|
|
|
|
let ((p1, p2, rtt), _) = async_std::task::block_on(result).factor_first();
|
|
|
|
assert!(p1 == peer1_id && p2 == peer2_id || p1 == peer2_id && p2 == peer1_id);
|
|
|
|
assert!(rtt < Duration::from_millis(50));
|
|
|
|
}
|
|
|
|
|
2021-08-11 13:12:12 +02:00
|
|
|
QuickCheck::new().tests(10).quickcheck(prop as fn(_, _))
|
2020-08-10 12:54:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Tests that the connection is closed upon a configurable
|
|
|
|
/// number of consecutive ping failures.
|
|
|
|
#[test]
|
|
|
|
fn max_failures() {
|
2020-09-28 10:57:02 +02:00
|
|
|
fn prop(max_failures: NonZeroU8, muxer: MuxerChoice) {
|
2021-09-07 00:10:48 +10:00
|
|
|
let cfg = ping::Config::new()
|
2020-08-10 12:54:55 +02:00
|
|
|
.with_interval(Duration::from_millis(10))
|
|
|
|
.with_timeout(Duration::from_millis(0))
|
|
|
|
.with_max_failures(max_failures.into());
|
|
|
|
|
2020-09-28 10:57:02 +02:00
|
|
|
let (peer1_id, trans) = mk_transport(muxer);
|
2022-11-15 15:26:03 +01:00
|
|
|
let mut swarm1 =
|
|
|
|
Swarm::with_async_std_executor(trans, Behaviour::new(cfg.clone()), peer1_id);
|
2020-08-10 12:54:55 +02:00
|
|
|
|
2020-09-28 10:57:02 +02:00
|
|
|
let (peer2_id, trans) = mk_transport(muxer);
|
2022-11-15 15:26:03 +01:00
|
|
|
let mut swarm2 = Swarm::with_async_std_executor(trans, Behaviour::new(cfg), peer2_id);
|
2020-08-10 12:54:55 +02:00
|
|
|
|
|
|
|
let (mut tx, mut rx) = mpsc::channel::<Multiaddr>(1);
|
|
|
|
|
|
|
|
let addr = "/ip4/127.0.0.1/tcp/0".parse().unwrap();
|
2021-03-18 14:55:33 +01:00
|
|
|
swarm1.listen_on(addr).unwrap();
|
2020-08-10 12:54:55 +02:00
|
|
|
|
|
|
|
let peer1 = async move {
|
|
|
|
let mut count1: u8 = 0;
|
|
|
|
|
|
|
|
loop {
|
2021-06-14 20:41:44 +02:00
|
|
|
match swarm1.select_next_some().await {
|
2021-07-08 11:41:33 +02:00
|
|
|
SwarmEvent::NewListenAddr { address, .. } => tx.send(address).await.unwrap(),
|
2022-10-06 03:50:11 +11:00
|
|
|
SwarmEvent::Behaviour(BehaviourEvent::Ping(ping::Event {
|
2021-09-07 00:10:48 +10:00
|
|
|
result: Ok(ping::Success::Ping { .. }),
|
2021-08-11 13:12:12 +02:00
|
|
|
..
|
2022-10-06 03:50:11 +11:00
|
|
|
})) => {
|
2020-08-10 12:54:55 +02:00
|
|
|
count1 = 0; // there may be an occasional success
|
|
|
|
}
|
2022-10-06 03:50:11 +11:00
|
|
|
SwarmEvent::Behaviour(BehaviourEvent::Ping(ping::Event {
|
|
|
|
result: Err(_),
|
|
|
|
..
|
|
|
|
})) => {
|
2020-08-10 12:54:55 +02:00
|
|
|
count1 += 1;
|
|
|
|
}
|
2021-08-11 13:12:12 +02:00
|
|
|
SwarmEvent::ConnectionClosed { .. } => return count1,
|
2020-08-10 12:54:55 +02:00
|
|
|
_ => {}
|
|
|
|
}
|
2019-04-16 15:57:29 +02:00
|
|
|
}
|
2020-08-10 12:54:55 +02:00
|
|
|
};
|
2019-04-16 15:57:29 +02:00
|
|
|
|
2020-08-10 12:54:55 +02:00
|
|
|
let peer2 = async move {
|
2021-11-15 14:17:23 +01:00
|
|
|
swarm2.dial(rx.next().await.unwrap()).unwrap();
|
2020-08-10 12:54:55 +02:00
|
|
|
|
|
|
|
let mut count2: u8 = 0;
|
|
|
|
|
|
|
|
loop {
|
2021-06-14 20:41:44 +02:00
|
|
|
match swarm2.select_next_some().await {
|
2022-10-06 03:50:11 +11:00
|
|
|
SwarmEvent::Behaviour(BehaviourEvent::Ping(ping::Event {
|
2021-09-07 00:10:48 +10:00
|
|
|
result: Ok(ping::Success::Ping { .. }),
|
2021-08-11 13:12:12 +02:00
|
|
|
..
|
2022-10-06 03:50:11 +11:00
|
|
|
})) => {
|
2020-08-10 12:54:55 +02:00
|
|
|
count2 = 0; // there may be an occasional success
|
|
|
|
}
|
2022-10-06 03:50:11 +11:00
|
|
|
SwarmEvent::Behaviour(BehaviourEvent::Ping(ping::Event {
|
|
|
|
result: Err(_),
|
|
|
|
..
|
|
|
|
})) => {
|
2020-08-10 12:54:55 +02:00
|
|
|
count2 += 1;
|
|
|
|
}
|
2021-08-11 13:12:12 +02:00
|
|
|
SwarmEvent::ConnectionClosed { .. } => return count2,
|
2020-08-10 12:54:55 +02:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let future = future::join(peer1, peer2);
|
|
|
|
let (count1, count2) = async_std::task::block_on(future);
|
|
|
|
assert_eq!(u8::max(count1, count2), max_failures.get() - 1);
|
|
|
|
}
|
|
|
|
|
2021-08-11 13:12:12 +02:00
|
|
|
QuickCheck::new().tests(10).quickcheck(prop as fn(_, _))
|
2019-04-16 15:57:29 +02:00
|
|
|
}
|
|
|
|
|
2021-07-31 06:21:21 +10:00
|
|
|
#[test]
|
|
|
|
fn unsupported_doesnt_fail() {
|
|
|
|
let (peer1_id, trans) = mk_transport(MuxerChoice::Mplex);
|
2022-11-15 15:26:03 +01:00
|
|
|
let mut swarm1 = Swarm::with_async_std_executor(trans, keep_alive::Behaviour, peer1_id);
|
2021-07-31 06:21:21 +10:00
|
|
|
|
|
|
|
let (peer2_id, trans) = mk_transport(MuxerChoice::Mplex);
|
2022-11-15 15:26:03 +01:00
|
|
|
let mut swarm2 = Swarm::with_async_std_executor(trans, Behaviour::default(), peer2_id);
|
2021-07-31 06:21:21 +10:00
|
|
|
|
|
|
|
let (mut tx, mut rx) = mpsc::channel::<Multiaddr>(1);
|
|
|
|
|
|
|
|
let addr = "/ip4/127.0.0.1/tcp/0".parse().unwrap();
|
|
|
|
swarm1.listen_on(addr).unwrap();
|
|
|
|
|
|
|
|
async_std::task::spawn(async move {
|
|
|
|
loop {
|
2022-10-04 18:24:38 +11:00
|
|
|
if let SwarmEvent::NewListenAddr { address, .. } = swarm1.select_next_some().await {
|
|
|
|
tx.send(address).await.unwrap()
|
2021-07-31 06:21:21 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let result = async_std::task::block_on(async move {
|
2021-11-15 14:17:23 +01:00
|
|
|
swarm2.dial(rx.next().await.unwrap()).unwrap();
|
2021-07-31 06:21:21 +10:00
|
|
|
|
|
|
|
loop {
|
|
|
|
match swarm2.select_next_some().await {
|
2022-10-06 03:50:11 +11:00
|
|
|
SwarmEvent::Behaviour(BehaviourEvent::Ping(ping::Event {
|
2021-09-07 00:10:48 +10:00
|
|
|
result: Err(ping::Failure::Unsupported),
|
2021-08-11 13:12:12 +02:00
|
|
|
..
|
2022-10-06 03:50:11 +11:00
|
|
|
})) => {
|
2021-07-31 06:21:21 +10:00
|
|
|
swarm2.disconnect_peer_id(peer1_id).unwrap();
|
|
|
|
}
|
|
|
|
SwarmEvent::ConnectionClosed { cause: Some(e), .. } => {
|
|
|
|
break Err(e);
|
|
|
|
}
|
|
|
|
SwarmEvent::ConnectionClosed { cause: None, .. } => {
|
|
|
|
break Ok(());
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
result.expect("node with ping should not fail connection due to unsupported protocol");
|
|
|
|
}
|
|
|
|
|
2021-08-11 13:12:12 +02:00
|
|
|
fn mk_transport(muxer: MuxerChoice) -> (PeerId, transport::Boxed<(PeerId, StreamMuxerBox)>) {
|
2019-04-16 15:57:29 +02:00
|
|
|
let id_keys = identity::Keypair::generate_ed25519();
|
2021-07-22 22:34:13 +02:00
|
|
|
let peer_id = id_keys.public().to_peer_id();
|
2021-08-11 13:12:12 +02:00
|
|
|
(
|
|
|
|
peer_id,
|
2022-10-24 15:41:08 +11:00
|
|
|
tcp::async_io::Transport::new(tcp::Config::default().nodelay(true))
|
2021-08-11 13:12:12 +02:00
|
|
|
.upgrade(upgrade::Version::V1)
|
2022-09-16 11:41:35 +10:00
|
|
|
.authenticate(noise::NoiseAuthenticated::xx(&id_keys).unwrap())
|
2021-08-11 13:12:12 +02:00
|
|
|
.multiplex(match muxer {
|
2023-01-18 13:35:07 +11:00
|
|
|
MuxerChoice::Yamux => Either::Left(yamux::YamuxConfig::default()),
|
|
|
|
MuxerChoice::Mplex => Either::Right(mplex::MplexConfig::default()),
|
2021-08-11 13:12:12 +02:00
|
|
|
})
|
|
|
|
.boxed(),
|
|
|
|
)
|
2019-04-16 15:57:29 +02:00
|
|
|
}
|
2020-09-28 10:57:02 +02:00
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
enum MuxerChoice {
|
|
|
|
Mplex,
|
|
|
|
Yamux,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Arbitrary for MuxerChoice {
|
2022-09-22 12:48:32 +04:00
|
|
|
fn arbitrary(g: &mut Gen) -> MuxerChoice {
|
|
|
|
*g.choose(&[MuxerChoice::Mplex, MuxerChoice::Yamux]).unwrap()
|
2020-09-28 10:57:02 +02:00
|
|
|
}
|
|
|
|
}
|
2022-10-06 03:50:11 +11:00
|
|
|
|
|
|
|
#[derive(NetworkBehaviour, Default)]
|
2022-12-13 07:58:01 +11:00
|
|
|
#[behaviour(prelude = "libp2p_swarm::derive_prelude")]
|
2022-10-06 03:50:11 +11:00
|
|
|
struct Behaviour {
|
|
|
|
keep_alive: keep_alive::Behaviour,
|
|
|
|
ping: ping::Behaviour,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Behaviour {
|
|
|
|
fn new(config: ping::Config) -> Self {
|
|
|
|
Self {
|
|
|
|
keep_alive: keep_alive::Behaviour,
|
|
|
|
ping: ping::Behaviour::new(config),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|