2019-06-24 17:42:33 +09: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.
|
|
|
|
|
2019-11-21 16:14:27 +01:00
|
|
|
use libp2p_core::{muxing, upgrade, Transport};
|
2019-06-24 17:42:33 +09:00
|
|
|
use libp2p_tcp::TcpConfig;
|
2019-11-21 16:14:27 +01:00
|
|
|
use futures::{prelude::*, channel::oneshot};
|
|
|
|
use std::sync::Arc;
|
2019-06-24 17:42:33 +09:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn async_write() {
|
2019-11-21 16:14:27 +01:00
|
|
|
// Tests that `AsyncWrite::close` implies flush.
|
2019-06-24 17:42:33 +09:00
|
|
|
|
2019-11-21 16:14:27 +01:00
|
|
|
let (tx, rx) = oneshot::channel();
|
2019-06-24 17:42:33 +09:00
|
|
|
|
2019-11-21 16:14:27 +01:00
|
|
|
let bg_thread = async_std::task::spawn(async move {
|
Rework the transport upgrade API. (#1240)
* Rework the transport upgrade API.
ALthough transport upgrades must follow a specific pattern
in order fot the resulting transport to be usable with a
`Network` or `Swarm`, that pattern is currently not well
reflected in the transport upgrade API. Rather, transport
upgrades are rather laborious and involve non-trivial code
duplication.
This commit introduces a `transport::upgrade::Builder` that is
obtained from `Transport::upgrade`. The `Builder` encodes the
previously implicit rules for transport upgrades:
1. Authentication upgrades must happen first.
2. Any number of upgrades may follow.
3. A multiplexer upgrade must happen last.
Since multiplexing is the last (regular) transport upgrade (because
that upgrade yields a `StreamMuxer` which is no longer a `AsyncRead`
/ `AsyncWrite` resource, which the upgrade process is based on),
the upgrade starts with `Transport::upgrade` and ends with
`Builder::multiplex`, which drops back down to the `Transport`,
providing a fluent API.
Authentication and multiplexer upgrades must furthermore adhere
to a minimal contract w.r.t their outputs:
1. An authentication upgrade is given an (async) I/O resource `C`
and must produce a pair `(I, D)` where `I: ConnectionInfo` and
`D` is a new (async) I/O resource `D`.
2. A multiplexer upgrade is given an (async) I/O resource `C`
and must produce a `M: StreamMuxer`.
To that end, two changes to the `secio` and `noise` protocols have been
made:
1. The `secio` upgrade now outputs a pair of `(PeerId, SecioOutput)`.
The former implements `ConnectionInfo` and the latter `AsyncRead` /
`AsyncWrite`, fulfilling the `Builder` contract.
2. A new `NoiseAuthenticated` upgrade has been added that wraps around
any noise upgrade (i.e. `NoiseConfig`) and has an output of
`(PeerId, NoiseOutput)`, i.e. it checks if the `RemoteIdentity` from
the handshake output is an `IdentityKey`, failing if that is not the
case. This is the standard upgrade procedure one wants for integrating
noise with libp2p-core/swarm.
* Cleanup
* Add a new integration test.
* Add missing license.
2019-09-10 15:42:45 +02:00
|
|
|
let mplex = libp2p_mplex::MplexConfig::new();
|
|
|
|
|
2019-10-10 11:31:44 +02:00
|
|
|
let transport = TcpConfig::new().and_then(move |c, e|
|
|
|
|
upgrade::apply(c, mplex, e, upgrade::Version::V1));
|
2019-06-24 17:42:33 +09:00
|
|
|
|
|
|
|
let mut listener = transport
|
|
|
|
.listen_on("/ip4/127.0.0.1/tcp/0".parse().unwrap())
|
|
|
|
.unwrap();
|
|
|
|
|
2019-11-21 16:14:27 +01:00
|
|
|
let addr = listener.next().await
|
2019-06-24 17:42:33 +09:00
|
|
|
.expect("some event")
|
|
|
|
.expect("no error")
|
|
|
|
.into_new_address()
|
|
|
|
.expect("listen address");
|
|
|
|
|
|
|
|
tx.send(addr).unwrap();
|
|
|
|
|
2019-11-21 16:14:27 +01:00
|
|
|
let client = listener
|
|
|
|
.next().await
|
|
|
|
.unwrap()
|
|
|
|
.unwrap()
|
|
|
|
.into_upgrade().unwrap().0.await.unwrap();
|
2020-06-30 17:10:53 +02:00
|
|
|
|
2019-11-21 16:14:27 +01:00
|
|
|
let mut outbound = muxing::outbound_from_ref_and_wrap(Arc::new(client)).await.unwrap();
|
2019-06-24 17:42:33 +09:00
|
|
|
|
2019-11-21 16:14:27 +01:00
|
|
|
let mut buf = Vec::new();
|
|
|
|
outbound.read_to_end(&mut buf).await.unwrap();
|
|
|
|
assert_eq!(buf, b"hello world");
|
2019-06-24 17:42:33 +09:00
|
|
|
});
|
|
|
|
|
2019-11-21 16:14:27 +01:00
|
|
|
async_std::task::block_on(async {
|
|
|
|
let mplex = libp2p_mplex::MplexConfig::new();
|
|
|
|
let transport = TcpConfig::new().and_then(move |c, e|
|
|
|
|
upgrade::apply(c, mplex, e, upgrade::Version::V1));
|
2020-06-30 17:10:53 +02:00
|
|
|
|
|
|
|
let client = Arc::new(transport.dial(rx.await.unwrap()).unwrap().await.unwrap());
|
|
|
|
let mut inbound = loop {
|
|
|
|
if let Some(s) = muxing::event_from_ref_and_wrap(client.clone()).await.unwrap()
|
|
|
|
.into_inbound_substream() {
|
|
|
|
break s;
|
|
|
|
}
|
|
|
|
};
|
2019-11-21 16:14:27 +01:00
|
|
|
inbound.write_all(b"hello world").await.unwrap();
|
2019-06-24 17:42:33 +09:00
|
|
|
|
2019-11-21 16:14:27 +01:00
|
|
|
// The test consists in making sure that this flushes the substream.
|
|
|
|
inbound.close().await.unwrap();
|
2019-06-24 17:42:33 +09:00
|
|
|
|
2019-11-21 16:14:27 +01:00
|
|
|
bg_thread.await;
|
|
|
|
});
|
2019-06-24 17:42:33 +09:00
|
|
|
}
|