From e9bee485ae74ed17468abad6a91f2fa8ff1dc61d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20M=C3=A4rtens?= Date: Sat, 29 Jan 2022 18:18:30 +0100 Subject: [PATCH] Add a test for the new gameserver IPv6 IPv4 behavior --- network/tests/helper.rs | 8 ++++---- network/tests/integration.rs | 37 ++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/network/tests/helper.rs b/network/tests/helper.rs index b22df211b4..517e8ea3a9 100644 --- a/network/tests/helper.rs +++ b/network/tests/helper.rs @@ -1,6 +1,6 @@ use lazy_static::*; use std::{ - net::SocketAddr, + net::{Ipv4Addr, SocketAddr}, sync::{ atomic::{AtomicU16, AtomicU64, Ordering}, Arc, @@ -89,8 +89,8 @@ pub fn tcp() -> (ListenAddr, ConnectAddr) { } let port = PORTS.fetch_add(1, Ordering::Relaxed); ( - ListenAddr::Tcp(SocketAddr::from(([127, 0, 0, 1], port))), - ConnectAddr::Tcp(SocketAddr::from(([127, 0, 0, 1], port))), + ListenAddr::Tcp(SocketAddr::from((Ipv4Addr::LOCALHOST, port))), + ConnectAddr::Tcp(SocketAddr::from((Ipv4Addr::LOCALHOST, port))), ) } @@ -117,7 +117,7 @@ pub fn quic() -> (ListenAddr, ConnectAddr) { let server_config = quinn::ServerConfig::with_single_cert(vec![cert], key) .expect("Server Config Cert/Key failed"); let client_config = quinn::ClientConfig::with_root_certificates(root_store); - use std::net::{IpAddr, Ipv4Addr}; + use std::net::IpAddr; ( ListenAddr::Quic( SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), port), diff --git a/network/tests/integration.rs b/network/tests/integration.rs index d49c7a49ef..0ee4e76b7f 100644 --- a/network/tests/integration.rs +++ b/network/tests/integration.rs @@ -262,3 +262,40 @@ fn multiple_try_recv() { assert_eq!(s1_b.try_recv::(), Err(StreamError::StreamClosed)); drop((_n_a, _n_b, _p_a, _p_b)); //clean teardown } + +/// If we listen on a IPv6 UNSPECIFIED address, on linux it will automatically +/// listen on the respective IPv4 address. This must not be as we should behave +/// similar under windows and linux. +#[test] +fn listen_on_ipv6_doesnt_block_ipv4() { + let (_, _) = helper::setup(false, 0); + let tcpv4 = tcp(); + let port = if let ListenAddr::Tcp(x) = tcpv4.0 { + x.port() + } else { + unreachable!() + }; + let tcpv6 = ( + ListenAddr::Tcp(std::net::SocketAddr::from(( + std::net::Ipv6Addr::UNSPECIFIED, + port, + ))), + ConnectAddr::Tcp(std::net::SocketAddr::from(( + std::net::Ipv6Addr::UNSPECIFIED, + port, + ))), + ); + + let (_r, _n_a, _p_a, mut s1_a, _n_b, _p_b, mut s1_b) = network_participant_stream(tcpv6); + std::thread::sleep(SLEEP_EXTERNAL); + let (_r2, _n_a2, _p_a2, mut s1_a2, _n_b2, _p_b2, mut s1_b2) = network_participant_stream(tcpv4); + + s1_a.send(42u32).unwrap(); + s1_a2.send(1337u32).unwrap(); + std::thread::sleep(SLEEP_EXTERNAL); + assert_eq!(s1_b.try_recv::(), Ok(Some(42u32))); + assert_eq!(s1_b2.try_recv::(), Ok(Some(1337u32))); + + drop((s1_a, s1_b, _n_a, _n_b, _p_a, _p_b)); + drop((s1_a2, s1_b2, _n_a2, _n_b2, _p_a2, _p_b2)); //clean teardown +}