@@ -16,7 +16,9 @@ use libp2p::request_response::{
16
16
} ;
17
17
use libp2p:: swarm:: dial_opts:: { DialOpts , PeerCondition } ;
18
18
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 ;
20
22
use libp2p_quic as quic;
21
23
use quic:: Provider ;
22
24
use rand:: RngCore ;
@@ -566,7 +568,7 @@ async fn ipv4_dial_ipv6() {
566
568
567
569
#[ cfg( feature = "async-std" ) ]
568
570
#[ async_std:: test]
569
- async fn wrong_peerod ( ) {
571
+ async fn wrong_peerid ( ) {
570
572
use libp2p:: PeerId ;
571
573
572
574
let _ = env_logger:: try_init ( ) ;
@@ -595,3 +597,85 @@ async fn wrong_peerod() {
595
597
}
596
598
}
597
599
}
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