Add a test for the new gameserver IPv6 IPv4 behavior

This commit is contained in:
Marcel Märtens 2022-01-29 18:18:30 +01:00
parent 7ba45fa7e5
commit e9bee485ae
2 changed files with 41 additions and 4 deletions

View File

@ -1,6 +1,6 @@
use lazy_static::*; use lazy_static::*;
use std::{ use std::{
net::SocketAddr, net::{Ipv4Addr, SocketAddr},
sync::{ sync::{
atomic::{AtomicU16, AtomicU64, Ordering}, atomic::{AtomicU16, AtomicU64, Ordering},
Arc, Arc,
@ -89,8 +89,8 @@ pub fn tcp() -> (ListenAddr, ConnectAddr) {
} }
let port = PORTS.fetch_add(1, Ordering::Relaxed); let port = PORTS.fetch_add(1, Ordering::Relaxed);
( (
ListenAddr::Tcp(SocketAddr::from(([127, 0, 0, 1], port))), ListenAddr::Tcp(SocketAddr::from((Ipv4Addr::LOCALHOST, port))),
ConnectAddr::Tcp(SocketAddr::from(([127, 0, 0, 1], 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) let server_config = quinn::ServerConfig::with_single_cert(vec![cert], key)
.expect("Server Config Cert/Key failed"); .expect("Server Config Cert/Key failed");
let client_config = quinn::ClientConfig::with_root_certificates(root_store); let client_config = quinn::ClientConfig::with_root_certificates(root_store);
use std::net::{IpAddr, Ipv4Addr}; use std::net::IpAddr;
( (
ListenAddr::Quic( ListenAddr::Quic(
SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), port), SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), port),

View File

@ -262,3 +262,40 @@ fn multiple_try_recv() {
assert_eq!(s1_b.try_recv::<String>(), Err(StreamError::StreamClosed)); assert_eq!(s1_b.try_recv::<String>(), Err(StreamError::StreamClosed));
drop((_n_a, _n_b, _p_a, _p_b)); //clean teardown 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::<u32>(), Ok(Some(42u32)));
assert_eq!(s1_b2.try_recv::<u32>(), 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
}