2017-11-07 18:25:10 +01:00
|
|
|
// Copyright 2017 Parity Technologies (UK) Ltd.
|
2018-03-07 16:20:55 +01:00
|
|
|
//
|
|
|
|
// 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
|
2017-11-08 10:11:58 +01:00
|
|
|
// Software is furnished to do so, subject to the following conditions:
|
|
|
|
//
|
2018-03-07 16:20:55 +01:00
|
|
|
// The above copyright notice and this permission notice shall be included in
|
2017-11-08 10:11:58 +01:00
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
2018-03-07 16:20:55 +01:00
|
|
|
// 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
|
2017-11-08 10:11:58 +01:00
|
|
|
// DEALINGS IN THE SOFTWARE.
|
2017-11-07 18:25:10 +01:00
|
|
|
|
|
|
|
//! Contains the unit tests of the library.
|
|
|
|
|
2017-11-07 10:26:45 +01:00
|
|
|
#![cfg(test)]
|
|
|
|
|
2018-12-11 10:45:28 +01:00
|
|
|
use tokio::runtime::current_thread::Runtime;
|
|
|
|
use tokio_tcp::{TcpListener, TcpStream};
|
2017-11-07 10:26:45 +01:00
|
|
|
use bytes::Bytes;
|
2018-12-11 10:45:28 +01:00
|
|
|
use crate::dialer_select::{dialer_select_proto_parallel, dialer_select_proto_serial};
|
2017-11-07 10:26:45 +01:00
|
|
|
use futures::Future;
|
2018-05-14 15:55:16 +02:00
|
|
|
use futures::{Sink, Stream};
|
2018-12-11 10:45:28 +01:00
|
|
|
use crate::protocol::{Dialer, DialerToListenerMessage, Listener, ListenerToDialerMessage};
|
|
|
|
use crate::ProtocolChoiceError;
|
|
|
|
use crate::{dialer_select_proto, listener_select_proto};
|
2017-11-07 10:26:45 +01:00
|
|
|
|
2018-11-22 18:15:35 +01:00
|
|
|
/// Holds a `Vec` and satifies the iterator requirements of `listener_select_proto`.
|
|
|
|
struct VecRefIntoIter<T>(Vec<T>);
|
|
|
|
|
|
|
|
impl<'a, T> IntoIterator for &'a VecRefIntoIter<T>
|
|
|
|
where T: Clone
|
|
|
|
{
|
|
|
|
type Item = T;
|
|
|
|
type IntoIter = std::vec::IntoIter<T>;
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
|
|
self.0.clone().into_iter()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-07 10:26:45 +01:00
|
|
|
#[test]
|
2017-11-07 18:25:10 +01:00
|
|
|
fn negotiate_with_self_succeeds() {
|
2018-07-16 12:15:27 +02:00
|
|
|
let listener = TcpListener::bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
|
2018-03-07 16:20:55 +01:00
|
|
|
let listener_addr = listener.local_addr().unwrap();
|
|
|
|
|
|
|
|
let server = listener
|
|
|
|
.incoming()
|
|
|
|
.into_future()
|
|
|
|
.map_err(|(e, _)| e.into())
|
2018-07-16 12:15:27 +02:00
|
|
|
.and_then(move |(connec, _)| Listener::new(connec.unwrap()))
|
2018-03-07 16:20:55 +01:00
|
|
|
.and_then(|l| l.into_future().map_err(|(e, _)| e))
|
|
|
|
.and_then(|(msg, rest)| {
|
|
|
|
let proto = match msg {
|
|
|
|
Some(DialerToListenerMessage::ProtocolRequest { name }) => name,
|
|
|
|
_ => panic!(),
|
|
|
|
};
|
|
|
|
rest.send(ListenerToDialerMessage::ProtocolAck { name: proto })
|
|
|
|
});
|
|
|
|
|
2018-07-16 12:15:27 +02:00
|
|
|
let client = TcpStream::connect(&listener_addr)
|
2018-03-07 16:20:55 +01:00
|
|
|
.from_err()
|
|
|
|
.and_then(move |stream| Dialer::new(stream))
|
|
|
|
.and_then(move |dialer| {
|
|
|
|
let p = Bytes::from("/hello/1.0.0");
|
|
|
|
dialer.send(DialerToListenerMessage::ProtocolRequest { name: p })
|
|
|
|
})
|
|
|
|
.and_then(move |dialer| dialer.into_future().map_err(|(e, _)| e))
|
|
|
|
.and_then(move |(msg, _)| {
|
|
|
|
let proto = match msg {
|
|
|
|
Some(ListenerToDialerMessage::ProtocolAck { name }) => name,
|
|
|
|
_ => panic!(),
|
|
|
|
};
|
|
|
|
assert_eq!(proto, "/hello/1.0.0");
|
|
|
|
Ok(())
|
|
|
|
});
|
2018-10-25 05:26:37 -04:00
|
|
|
let mut rt = Runtime::new().unwrap();
|
|
|
|
let _ = rt.block_on(server.join(client)).unwrap();
|
2017-11-07 10:26:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn select_proto_basic() {
|
2018-07-16 12:15:27 +02:00
|
|
|
let listener = TcpListener::bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
|
2018-03-07 16:20:55 +01:00
|
|
|
let listener_addr = listener.local_addr().unwrap();
|
|
|
|
|
|
|
|
let server = listener
|
|
|
|
.incoming()
|
|
|
|
.into_future()
|
2018-07-16 12:15:27 +02:00
|
|
|
.map(|s| s.0.unwrap())
|
2018-03-07 16:20:55 +01:00
|
|
|
.map_err(|(e, _)| e.into())
|
|
|
|
.and_then(move |connec| {
|
2018-12-11 15:13:10 +01:00
|
|
|
let protos = vec![b"/proto1", b"/proto2"];
|
2018-11-22 18:15:35 +01:00
|
|
|
listener_select_proto(connec, VecRefIntoIter(protos)).map(|r| r.0)
|
2018-03-07 16:20:55 +01:00
|
|
|
});
|
|
|
|
|
2018-07-16 12:15:27 +02:00
|
|
|
let client = TcpStream::connect(&listener_addr)
|
2018-03-07 16:20:55 +01:00
|
|
|
.from_err()
|
|
|
|
.and_then(move |connec| {
|
2018-12-11 15:13:10 +01:00
|
|
|
let protos = vec![b"/proto3", b"/proto2"];
|
2018-03-07 16:20:55 +01:00
|
|
|
dialer_select_proto(connec, protos).map(|r| r.0)
|
|
|
|
});
|
2018-10-25 05:26:37 -04:00
|
|
|
let mut rt = Runtime::new().unwrap();
|
2018-07-17 11:55:18 +02:00
|
|
|
let (dialer_chosen, listener_chosen) =
|
2018-10-25 05:26:37 -04:00
|
|
|
rt.block_on(client.join(server)).unwrap();
|
2018-12-11 15:13:10 +01:00
|
|
|
assert_eq!(dialer_chosen, b"/proto2");
|
|
|
|
assert_eq!(listener_chosen, b"/proto2");
|
2017-11-07 10:26:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_protocol_found() {
|
2018-07-16 12:15:27 +02:00
|
|
|
let listener = TcpListener::bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
|
2018-03-07 16:20:55 +01:00
|
|
|
let listener_addr = listener.local_addr().unwrap();
|
|
|
|
|
|
|
|
let server = listener
|
|
|
|
.incoming()
|
|
|
|
.into_future()
|
2018-07-16 12:15:27 +02:00
|
|
|
.map(|s| s.0.unwrap())
|
2018-03-07 16:20:55 +01:00
|
|
|
.map_err(|(e, _)| e.into())
|
|
|
|
.and_then(move |connec| {
|
2018-12-11 15:13:10 +01:00
|
|
|
let protos = vec![b"/proto1", b"/proto2"];
|
2018-11-22 18:15:35 +01:00
|
|
|
listener_select_proto(connec, VecRefIntoIter(protos)).map(|r| r.0)
|
2018-03-07 16:20:55 +01:00
|
|
|
});
|
|
|
|
|
2018-07-16 12:15:27 +02:00
|
|
|
let client = TcpStream::connect(&listener_addr)
|
2018-03-07 16:20:55 +01:00
|
|
|
.from_err()
|
|
|
|
.and_then(move |connec| {
|
2018-12-11 15:13:10 +01:00
|
|
|
let protos = vec![b"/proto3", b"/proto4"];
|
2018-03-07 16:20:55 +01:00
|
|
|
dialer_select_proto(connec, protos).map(|r| r.0)
|
|
|
|
});
|
2018-10-25 05:26:37 -04:00
|
|
|
let mut rt = Runtime::new().unwrap();
|
|
|
|
match rt.block_on(client.join(server)) {
|
2018-03-07 16:20:55 +01:00
|
|
|
Err(ProtocolChoiceError::NoProtocolFound) => (),
|
|
|
|
_ => panic!(),
|
|
|
|
}
|
2017-11-07 10:26:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn select_proto_parallel() {
|
2018-07-16 12:15:27 +02:00
|
|
|
let listener = TcpListener::bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
|
2018-03-07 16:20:55 +01:00
|
|
|
let listener_addr = listener.local_addr().unwrap();
|
|
|
|
|
|
|
|
let server = listener
|
|
|
|
.incoming()
|
|
|
|
.into_future()
|
2018-07-16 12:15:27 +02:00
|
|
|
.map(|s| s.0.unwrap())
|
2018-03-07 16:20:55 +01:00
|
|
|
.map_err(|(e, _)| e.into())
|
|
|
|
.and_then(move |connec| {
|
2018-12-11 15:13:10 +01:00
|
|
|
let protos = vec![b"/proto1", b"/proto2"];
|
2018-11-22 18:15:35 +01:00
|
|
|
listener_select_proto(connec, VecRefIntoIter(protos)).map(|r| r.0)
|
2018-03-07 16:20:55 +01:00
|
|
|
});
|
|
|
|
|
2018-07-16 12:15:27 +02:00
|
|
|
let client = TcpStream::connect(&listener_addr)
|
2018-03-07 16:20:55 +01:00
|
|
|
.from_err()
|
|
|
|
.and_then(move |connec| {
|
2018-12-11 15:13:10 +01:00
|
|
|
let protos = vec![b"/proto3", b"/proto2"];
|
|
|
|
dialer_select_proto_parallel(connec, protos.into_iter()).map(|r| r.0)
|
2018-03-07 16:20:55 +01:00
|
|
|
});
|
|
|
|
|
2018-10-25 05:26:37 -04:00
|
|
|
let mut rt = Runtime::new().unwrap();
|
2018-07-17 11:55:18 +02:00
|
|
|
let (dialer_chosen, listener_chosen) =
|
2018-10-25 05:26:37 -04:00
|
|
|
rt.block_on(client.join(server)).unwrap();
|
2018-12-11 15:13:10 +01:00
|
|
|
assert_eq!(dialer_chosen, b"/proto2");
|
|
|
|
assert_eq!(listener_chosen, b"/proto2");
|
2017-11-07 10:26:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn select_proto_serial() {
|
2018-07-16 12:15:27 +02:00
|
|
|
let listener = TcpListener::bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
|
2018-03-07 16:20:55 +01:00
|
|
|
let listener_addr = listener.local_addr().unwrap();
|
|
|
|
|
|
|
|
let server = listener
|
|
|
|
.incoming()
|
|
|
|
.into_future()
|
2018-07-16 12:15:27 +02:00
|
|
|
.map(|s| s.0.unwrap())
|
2018-03-07 16:20:55 +01:00
|
|
|
.map_err(|(e, _)| e.into())
|
|
|
|
.and_then(move |connec| {
|
2018-12-11 15:13:10 +01:00
|
|
|
let protos = vec![b"/proto1", b"/proto2"];
|
2018-11-22 18:15:35 +01:00
|
|
|
listener_select_proto(connec, VecRefIntoIter(protos)).map(|r| r.0)
|
2018-03-07 16:20:55 +01:00
|
|
|
});
|
|
|
|
|
2018-07-16 12:15:27 +02:00
|
|
|
let client = TcpStream::connect(&listener_addr)
|
2018-03-07 16:20:55 +01:00
|
|
|
.from_err()
|
|
|
|
.and_then(move |connec| {
|
2018-12-11 15:13:10 +01:00
|
|
|
let protos = vec![b"/proto3", b"/proto2"];
|
|
|
|
dialer_select_proto_serial(connec, protos.into_iter()).map(|r| r.0)
|
2018-03-07 16:20:55 +01:00
|
|
|
});
|
|
|
|
|
2018-10-25 05:26:37 -04:00
|
|
|
let mut rt = Runtime::new().unwrap();
|
2018-07-17 11:55:18 +02:00
|
|
|
let (dialer_chosen, listener_chosen) =
|
2018-10-25 05:26:37 -04:00
|
|
|
rt.block_on(client.join(server)).unwrap();
|
2018-12-11 15:13:10 +01:00
|
|
|
assert_eq!(dialer_chosen, b"/proto2");
|
|
|
|
assert_eq!(listener_chosen, b"/proto2");
|
2017-11-07 14:31:18 +01:00
|
|
|
}
|