|
| 1 | +// Copyright 2020 Parity Technologies (UK) Ltd. |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a |
| 4 | +// copy of this software and associated documentation files (the "Software"), |
| 5 | +// to deal in the Software without restriction, including without limitation |
| 6 | +// the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 7 | +// and/or sell copies of the Software, and to permit persons to whom the |
| 8 | +// Software is furnished to do so, subject to the following conditions: |
| 9 | +// |
| 10 | +// The above copyright notice and this permission notice shall be included in |
| 11 | +// all copies or substantial portions of the Software. |
| 12 | +// |
| 13 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 14 | +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 18 | +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 19 | +// DEALINGS IN THE SOFTWARE. |
| 20 | + |
| 21 | +use libp2p_core::{ |
| 22 | + connection::{ConnectionHandler, ConnectionHandlerEvent, Substream, SubstreamEndpoint}, |
| 23 | + identity, |
| 24 | + muxing::StreamMuxerBox, |
| 25 | + upgrade, |
| 26 | + multiaddr::Protocol, |
| 27 | + Multiaddr, |
| 28 | + Network, |
| 29 | + network::{NetworkEvent, NetworkConfig}, |
| 30 | + PeerId, |
| 31 | + Transport, |
| 32 | + transport::{self, MemoryTransport} |
| 33 | +}; |
| 34 | +use libp2p_mplex::MplexConfig; |
| 35 | +use libp2p_plaintext::PlainText2Config; |
| 36 | +use futures::{channel::oneshot, ready, prelude::*}; |
| 37 | +use rand::random; |
| 38 | +use std::{io, task::{Context, Poll}}; |
| 39 | + |
| 40 | +type TestTransport = transport::Boxed<(PeerId, StreamMuxerBox), io::Error>; |
| 41 | +type TestNetwork = Network<TestTransport, (), (), TestHandler>; |
| 42 | + |
| 43 | +fn mk_transport(up: upgrade::Version) -> (PeerId, TestTransport) { |
| 44 | + let keys = identity::Keypair::generate_ed25519(); |
| 45 | + let id = keys.public().into_peer_id(); |
| 46 | + (id, MemoryTransport::default() |
| 47 | + .upgrade(up) |
| 48 | + .authenticate(PlainText2Config { local_public_key: keys.public() }) |
| 49 | + .multiplex(MplexConfig::default()) |
| 50 | + .boxed()) |
| 51 | +} |
| 52 | + |
| 53 | +/// Tests the transport upgrade process with all supported |
| 54 | +/// upgrade protocol versions. |
| 55 | +#[test] |
| 56 | +fn transport_upgrade() { |
| 57 | + let _ = env_logger::try_init(); |
| 58 | + |
| 59 | + fn run(up: upgrade::Version) { |
| 60 | + let (dialer_id, dialer_transport) = mk_transport(up); |
| 61 | + let (listener_id, listener_transport) = mk_transport(up); |
| 62 | + |
| 63 | + let listen_addr = Multiaddr::from(Protocol::Memory(random::<u64>())); |
| 64 | + |
| 65 | + let mut dialer = TestNetwork::new(dialer_transport, dialer_id, NetworkConfig::default()); |
| 66 | + let mut listener = TestNetwork::new(listener_transport, listener_id, NetworkConfig::default()); |
| 67 | + |
| 68 | + listener.listen_on(listen_addr).unwrap(); |
| 69 | + let (addr_sender, addr_receiver) = oneshot::channel(); |
| 70 | + |
| 71 | + let client = async move { |
| 72 | + let addr = addr_receiver.await.unwrap(); |
| 73 | + dialer.dial(&addr, TestHandler()).unwrap(); |
| 74 | + futures::future::poll_fn(move |cx| { |
| 75 | + loop { |
| 76 | + match ready!(dialer.poll(cx)) { |
| 77 | + NetworkEvent::ConnectionEstablished { .. } => { |
| 78 | + return Poll::Ready(()) |
| 79 | + } |
| 80 | + _ => {} |
| 81 | + } |
| 82 | + } |
| 83 | + }).await |
| 84 | + }; |
| 85 | + |
| 86 | + let mut addr_sender = Some(addr_sender); |
| 87 | + let server = futures::future::poll_fn(move |cx| { |
| 88 | + loop { |
| 89 | + match ready!(listener.poll(cx)) { |
| 90 | + NetworkEvent::NewListenerAddress { listen_addr, .. } => { |
| 91 | + addr_sender.take().unwrap().send(listen_addr).unwrap(); |
| 92 | + } |
| 93 | + NetworkEvent::IncomingConnection { connection, .. } => { |
| 94 | + listener.accept(connection, TestHandler()).unwrap(); |
| 95 | + } |
| 96 | + NetworkEvent::ConnectionEstablished { .. } => { |
| 97 | + return Poll::Ready(()) |
| 98 | + } |
| 99 | + _ => {} |
| 100 | + } |
| 101 | + } |
| 102 | + }); |
| 103 | + |
| 104 | + async_std::task::block_on(future::select(Box::pin(server), Box::pin(client))); |
| 105 | + } |
| 106 | + |
| 107 | + run(upgrade::Version::V1); |
| 108 | + run(upgrade::Version::V1Lazy); |
| 109 | +} |
| 110 | + |
| 111 | +struct TestHandler(); |
| 112 | + |
| 113 | +impl ConnectionHandler for TestHandler { |
| 114 | + type InEvent = (); |
| 115 | + type OutEvent = (); |
| 116 | + type Error = io::Error; |
| 117 | + type Substream = Substream<StreamMuxerBox>; |
| 118 | + type OutboundOpenInfo = (); |
| 119 | + |
| 120 | + fn inject_substream(&mut self, _: Self::Substream, _: SubstreamEndpoint<Self::OutboundOpenInfo>) |
| 121 | + {} |
| 122 | + |
| 123 | + fn inject_event(&mut self, _: Self::InEvent) |
| 124 | + {} |
| 125 | + |
| 126 | + fn inject_address_change(&mut self, _: &Multiaddr) |
| 127 | + {} |
| 128 | + |
| 129 | + fn poll(&mut self, _: &mut Context<'_>) |
| 130 | + -> Poll<Result<ConnectionHandlerEvent<Self::OutboundOpenInfo, Self::OutEvent>, Self::Error>> |
| 131 | + { |
| 132 | + Poll::Pending |
| 133 | + } |
| 134 | +} |
0 commit comments