2020-10-07 11:10:54 +02:00
|
|
|
// Copyright 2020 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.
|
|
|
|
|
2021-08-11 13:12:12 +02:00
|
|
|
use futures::{channel::oneshot, prelude::*, ready};
|
2022-12-13 07:58:01 +11:00
|
|
|
use libp2p_core::{
|
2020-10-07 11:10:54 +02:00
|
|
|
identity,
|
|
|
|
multiaddr::Protocol,
|
2021-08-11 13:12:12 +02:00
|
|
|
muxing::StreamMuxerBox,
|
|
|
|
transport::{self, MemoryTransport},
|
2022-02-13 21:57:38 +01:00
|
|
|
upgrade, Multiaddr, PeerId, Transport,
|
2020-10-07 11:10:54 +02:00
|
|
|
};
|
2022-12-13 07:58:01 +11:00
|
|
|
use libp2p_mplex::MplexConfig;
|
|
|
|
use libp2p_plaintext::PlainText2Config;
|
|
|
|
use libp2p_swarm::{dummy, Swarm, SwarmEvent};
|
2020-10-07 11:10:54 +02:00
|
|
|
use rand::random;
|
2022-02-13 21:57:38 +01:00
|
|
|
use std::task::Poll;
|
2020-10-07 11:10:54 +02:00
|
|
|
|
2020-10-16 16:53:02 +02:00
|
|
|
type TestTransport = transport::Boxed<(PeerId, StreamMuxerBox)>;
|
2020-10-07 11:10:54 +02:00
|
|
|
|
|
|
|
fn mk_transport(up: upgrade::Version) -> (PeerId, TestTransport) {
|
|
|
|
let keys = identity::Keypair::generate_ed25519();
|
2021-07-22 22:34:13 +02:00
|
|
|
let id = keys.public().to_peer_id();
|
2021-08-11 13:12:12 +02:00
|
|
|
(
|
|
|
|
id,
|
|
|
|
MemoryTransport::default()
|
|
|
|
.upgrade(up)
|
|
|
|
.authenticate(PlainText2Config {
|
|
|
|
local_public_key: keys.public(),
|
|
|
|
})
|
|
|
|
.multiplex(MplexConfig::default())
|
|
|
|
.boxed(),
|
|
|
|
)
|
2020-10-07 11:10:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Tests the transport upgrade process with all supported
|
|
|
|
/// upgrade protocol versions.
|
|
|
|
#[test]
|
|
|
|
fn transport_upgrade() {
|
|
|
|
let _ = env_logger::try_init();
|
|
|
|
|
|
|
|
fn run(up: upgrade::Version) {
|
|
|
|
let (dialer_id, dialer_transport) = mk_transport(up);
|
|
|
|
let (listener_id, listener_transport) = mk_transport(up);
|
|
|
|
|
|
|
|
let listen_addr = Multiaddr::from(Protocol::Memory(random::<u64>()));
|
|
|
|
|
2022-11-15 15:26:03 +01:00
|
|
|
let mut dialer =
|
|
|
|
Swarm::with_async_std_executor(dialer_transport, dummy::Behaviour, dialer_id);
|
|
|
|
let mut listener =
|
|
|
|
Swarm::with_async_std_executor(listener_transport, dummy::Behaviour, listener_id);
|
2020-10-07 11:10:54 +02:00
|
|
|
|
|
|
|
listener.listen_on(listen_addr).unwrap();
|
|
|
|
let (addr_sender, addr_receiver) = oneshot::channel();
|
|
|
|
|
|
|
|
let client = async move {
|
|
|
|
let addr = addr_receiver.await.unwrap();
|
2022-02-13 21:57:38 +01:00
|
|
|
dialer.dial(addr).unwrap();
|
2021-08-11 13:12:12 +02:00
|
|
|
futures::future::poll_fn(move |cx| loop {
|
2022-10-04 18:24:38 +11:00
|
|
|
if let SwarmEvent::ConnectionEstablished { .. } =
|
|
|
|
ready!(dialer.poll_next_unpin(cx)).unwrap()
|
|
|
|
{
|
|
|
|
return Poll::Ready(());
|
2020-10-07 11:10:54 +02:00
|
|
|
}
|
2021-08-11 13:12:12 +02:00
|
|
|
})
|
|
|
|
.await
|
2020-10-07 11:10:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
let mut addr_sender = Some(addr_sender);
|
2021-08-11 13:12:12 +02:00
|
|
|
let server = futures::future::poll_fn(move |cx| loop {
|
2022-02-13 21:57:38 +01:00
|
|
|
match ready!(listener.poll_next_unpin(cx)).unwrap() {
|
|
|
|
SwarmEvent::NewListenAddr { address, .. } => {
|
|
|
|
addr_sender.take().unwrap().send(address).unwrap();
|
2020-10-07 11:10:54 +02:00
|
|
|
}
|
2022-02-13 21:57:38 +01:00
|
|
|
SwarmEvent::IncomingConnection { .. } => {}
|
|
|
|
SwarmEvent::ConnectionEstablished { .. } => return Poll::Ready(()),
|
2021-08-11 13:12:12 +02:00
|
|
|
_ => {}
|
2020-10-07 11:10:54 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
async_std::task::block_on(future::select(Box::pin(server), Box::pin(client)));
|
|
|
|
}
|
|
|
|
|
|
|
|
run(upgrade::Version::V1);
|
|
|
|
run(upgrade::Version::V1Lazy);
|
|
|
|
}
|