Skip to content

Commit

Permalink
Move tests to async-std file
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaseizinger committed Feb 26, 2020
1 parent 3f38c78 commit 04ab75f
Show file tree
Hide file tree
Showing 2 changed files with 190 additions and 196 deletions.
189 changes: 189 additions & 0 deletions transports/tcp/src/async_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,192 @@ impl TcpStream for async_std::net::TcpStream {
async_std::net::TcpStream::local_addr(self)
}
}

#[cfg(test)]
mod tests {
use super::TcpConfig;
use crate::internal::multiaddr_to_socketaddr;
use futures::prelude::*;
use libp2p_core::{
multiaddr::{Multiaddr, Protocol},
transport::ListenerEvent,
Transport,
};
use std::net::{IpAddr, Ipv4Addr, SocketAddr};

#[test]
fn wildcard_expansion() {
let mut listener = TcpConfig::new()
.listen_on("/ip4/0.0.0.0/tcp/0".parse().unwrap())
.expect("listener");

// Get the first address.
let addr = futures::executor::block_on_stream(listener.by_ref())
.next()
.expect("some event")
.expect("no error")
.into_new_address()
.expect("listen address");

// Process all initial `NewAddress` events and make sure they
// do not contain wildcard address or port.
let server = listener
.take_while(|event| match event.as_ref().unwrap() {
ListenerEvent::NewAddress(a) => {
let mut iter = a.iter();
match iter.next().expect("ip address") {
Protocol::Ip4(ip) => assert!(!ip.is_unspecified()),
Protocol::Ip6(ip) => assert!(!ip.is_unspecified()),
other => panic!("Unexpected protocol: {}", other),
}
if let Protocol::Tcp(port) = iter.next().expect("port") {
assert_ne!(0, port)
} else {
panic!("No TCP port in address: {}", a)
}
futures::future::ready(true)
}
_ => futures::future::ready(false),
})
.for_each(|_| futures::future::ready(()));

let client = TcpConfig::new().dial(addr).expect("dialer");
async_std::task::block_on(futures::future::join(server, client))
.1
.unwrap();
}

#[test]
fn multiaddr_to_tcp_conversion() {
use std::net::Ipv6Addr;

assert!(
multiaddr_to_socketaddr(&"/ip4/127.0.0.1/udp/1234".parse::<Multiaddr>().unwrap())
.is_err()
);

assert_eq!(
multiaddr_to_socketaddr(&"/ip4/127.0.0.1/tcp/12345".parse::<Multiaddr>().unwrap()),
Ok(SocketAddr::new(
IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)),
12345,
))
);
assert_eq!(
multiaddr_to_socketaddr(
&"/ip4/255.255.255.255/tcp/8080"
.parse::<Multiaddr>()
.unwrap()
),
Ok(SocketAddr::new(
IpAddr::V4(Ipv4Addr::new(255, 255, 255, 255)),
8080,
))
);
assert_eq!(
multiaddr_to_socketaddr(&"/ip6/::1/tcp/12345".parse::<Multiaddr>().unwrap()),
Ok(SocketAddr::new(
IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)),
12345,
))
);
assert_eq!(
multiaddr_to_socketaddr(
&"/ip6/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/tcp/8080"
.parse::<Multiaddr>()
.unwrap()
),
Ok(SocketAddr::new(
IpAddr::V6(Ipv6Addr::new(
65535, 65535, 65535, 65535, 65535, 65535, 65535, 65535,
)),
8080,
))
);
}

#[test]
fn communicating_between_dialer_and_listener() {
let (ready_tx, ready_rx) = futures::channel::oneshot::channel();
let mut ready_tx = Some(ready_tx);

async_std::task::spawn(async move {
let addr = "/ip4/127.0.0.1/tcp/0".parse::<Multiaddr>().unwrap();
let tcp = TcpConfig::new();
let mut listener = tcp.listen_on(addr).unwrap();

loop {
match listener.next().await.unwrap().unwrap() {
ListenerEvent::NewAddress(listen_addr) => {
ready_tx.take().unwrap().send(listen_addr).unwrap();
}
ListenerEvent::Upgrade { upgrade, .. } => {
let mut upgrade = upgrade.await.unwrap();
let mut buf = [0u8; 3];
upgrade.read_exact(&mut buf).await.unwrap();
assert_eq!(buf, [1, 2, 3]);
upgrade.write_all(&[4, 5, 6]).await.unwrap();
}
_ => unreachable!(),
}
}
});

async_std::task::block_on(async move {
let addr = ready_rx.await.unwrap();
let tcp = TcpConfig::new();

// Obtain a future socket through dialing
let mut socket = tcp.dial(addr.clone()).unwrap().await.unwrap();
socket.write_all(&[0x1, 0x2, 0x3]).await.unwrap();

let mut buf = [0u8; 3];
socket.read_exact(&mut buf).await.unwrap();
assert_eq!(buf, [4, 5, 6]);
});
}

#[test]
fn replace_port_0_in_returned_multiaddr_ipv4() {
let tcp = TcpConfig::new();

let addr = "/ip4/127.0.0.1/tcp/0".parse::<Multiaddr>().unwrap();
assert!(addr.to_string().contains("tcp/0"));

let new_addr = futures::executor::block_on_stream(tcp.listen_on(addr).unwrap())
.next()
.expect("some event")
.expect("no error")
.into_new_address()
.expect("listen address");

assert!(!new_addr.to_string().contains("tcp/0"));
}

#[test]
fn replace_port_0_in_returned_multiaddr_ipv6() {
let tcp = TcpConfig::new();

let addr: Multiaddr = "/ip6/::1/tcp/0".parse().unwrap();
assert!(addr.to_string().contains("tcp/0"));

let new_addr = futures::executor::block_on_stream(tcp.listen_on(addr).unwrap())
.next()
.expect("some event")
.expect("no error")
.into_new_address()
.expect("listen address");

assert!(!new_addr.to_string().contains("tcp/0"));
}

#[test]
fn larger_addr_denied() {
let tcp = TcpConfig::new();

let addr = "/ip4/127.0.0.1/tcp/12345/tcp/12345"
.parse::<Multiaddr>()
.unwrap();
assert!(tcp.listen_on(addr).is_err());
}
}
Loading

0 comments on commit 04ab75f

Please sign in to comment.