Skip to content

Commit 58c3f67

Browse files
committed
quic/tests: add tcp_and_quic test
1 parent c17df6d commit 58c3f67

File tree

2 files changed

+87
-3
lines changed

2 files changed

+87
-3
lines changed

transports/quic/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ rustc-args = ["--cfg", "docsrs"]
4040

4141
[dev-dependencies]
4242
async-std = { version = "1.12.0", features = ["attributes"] }
43-
libp2p = { path = "../..", features = ["request-response"] }
43+
libp2p = { path = "../..", features = ["request-response", "tcp", "yamux", "noise", "async-std"] }
4444
env_logger = "0.9.0"
4545
rand = "0.8.4"
4646
tokio = { version = "1.21.1", features = ["macros", "rt-multi-thread"] }

transports/quic/tests/smoke.rs

+86-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ use libp2p::request_response::{
1616
};
1717
use libp2p::swarm::dial_opts::{DialOpts, PeerCondition};
1818
use libp2p::swarm::{ConnectionError, DialError, Swarm, SwarmEvent};
19-
use libp2p::Multiaddr;
19+
use libp2p::{noise, tcp, yamux, Multiaddr};
20+
use libp2p_core::either::EitherOutput;
21+
use libp2p_core::transport::OrTransport;
2022
use libp2p_quic as quic;
2123
use quic::Provider;
2224
use rand::RngCore;
@@ -566,7 +568,7 @@ async fn ipv4_dial_ipv6() {
566568

567569
#[cfg(feature = "async-std")]
568570
#[async_std::test]
569-
async fn wrong_peerod() {
571+
async fn wrong_peerid() {
570572
use libp2p::PeerId;
571573

572574
let _ = env_logger::try_init();
@@ -595,3 +597,85 @@ async fn wrong_peerod() {
595597
}
596598
}
597599
}
600+
601+
#[cfg(feature = "async-std")]
602+
fn new_tcp_quic_swarm() -> Swarm<RequestResponse<PingCodec>> {
603+
let keypair = generate_tls_keypair();
604+
let peer_id = keypair.public().to_peer_id();
605+
let mut config = quic::Config::new(&keypair);
606+
config.handshake_timeout = Duration::from_secs(1);
607+
let quic_transport = quic::async_std::Transport::new(config);
608+
let tcp_transport = tcp::async_io::Transport::new(tcp::Config::default())
609+
.upgrade(upgrade::Version::V1Lazy)
610+
.authenticate(
611+
noise::NoiseConfig::xx(
612+
noise::Keypair::<noise::X25519Spec>::new()
613+
.into_authentic(&keypair)
614+
.unwrap(),
615+
)
616+
.into_authenticated(),
617+
)
618+
.multiplex(yamux::YamuxConfig::default());
619+
620+
let transport = OrTransport::new(quic_transport, tcp_transport)
621+
.map(|either_output, _| match either_output {
622+
EitherOutput::First((peer_id, muxer)) => (peer_id, StreamMuxerBox::new(muxer)),
623+
EitherOutput::Second((peer_id, muxer)) => (peer_id, StreamMuxerBox::new(muxer)),
624+
})
625+
.boxed();
626+
627+
let protocols = iter::once((PingProtocol(), ProtocolSupport::Full));
628+
let cfg = RequestResponseConfig::default();
629+
let behaviour = RequestResponse::new(PingCodec(), protocols, cfg);
630+
631+
Swarm::new(transport, behaviour, peer_id)
632+
}
633+
634+
#[cfg(feature = "async-std")]
635+
#[async_std::test]
636+
async fn tcp_and_quic() {
637+
let mut swarm_a = new_tcp_quic_swarm();
638+
let swarm_a_id = *swarm_a.local_peer_id();
639+
println!("{}", swarm_a_id);
640+
641+
let mut swarm_b = new_tcp_quic_swarm();
642+
643+
let quic_addr = start_listening(&mut swarm_a, "/ip4/127.0.0.1/udp/0/quic").await;
644+
let tcp_addr = start_listening(&mut swarm_a, "/ip4/127.0.0.1/tcp/0").await;
645+
646+
swarm_b.dial(quic_addr.clone()).unwrap();
647+
648+
loop {
649+
select! {
650+
ev = swarm_a.select_next_some() => match ev {
651+
SwarmEvent::ConnectionEstablished { .. } => break,
652+
SwarmEvent::IncomingConnection { .. } => { }
653+
e => panic!("{:?}", e),
654+
},
655+
ev = swarm_b.select_next_some() => match ev {
656+
SwarmEvent::ConnectionEstablished { .. } => {},
657+
e => panic!("{:?}", e),
658+
}
659+
}
660+
}
661+
662+
swarm_b.dial(tcp_addr).unwrap();
663+
664+
loop {
665+
select! {
666+
ev = swarm_a.select_next_some() => match ev {
667+
SwarmEvent::ConnectionEstablished { .. } => break,
668+
SwarmEvent::IncomingConnection { .. }
669+
| SwarmEvent::ConnectionClosed { .. } => { }
670+
e => panic!("{:?}", e),
671+
},
672+
ev = swarm_b.select_next_some() => match ev {
673+
SwarmEvent::ConnectionEstablished { .. } => {},
674+
SwarmEvent::ConnectionClosed { endpoint, .. } => {
675+
assert_eq!(endpoint.get_remote_address(), &quic_addr );
676+
}
677+
e => panic!("{:?}", e),
678+
}
679+
}
680+
}
681+
}

0 commit comments

Comments
 (0)