Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions library/std/src/net/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,13 @@ use crate::env;
use crate::net::{Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6, ToSocketAddrs};
use crate::sync::atomic::{AtomicUsize, Ordering};

static PORT: AtomicUsize = AtomicUsize::new(0);
const BASE_PORT: u16 = 19600;

/// A localhost address whose port will be picked automatically by the OS.
pub const LOCALHOST_IP4: SocketAddr =
SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 0));
/// A localhost address whose port will be picked automatically by the OS.
pub const LOCALHOST_IP6: SocketAddr =
SocketAddr::V6(SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), 0, 0, 0));

pub fn next_test_ip4() -> SocketAddr {
let port = PORT.fetch_add(1, Ordering::Relaxed) as u16 + BASE_PORT;
SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), port))
}

pub fn next_test_ip6() -> SocketAddr {
let port = PORT.fetch_add(1, Ordering::Relaxed) as u16 + BASE_PORT;
SocketAddr::V6(SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), port, 0, 0))
}

pub fn sa4(a: Ipv4Addr, p: u16) -> SocketAddr {
SocketAddr::V4(SocketAddrV4::new(a, p))
}
Expand Down
103 changes: 46 additions & 57 deletions library/std/src/net/udp/tests.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
use crate::io::ErrorKind;
use crate::net::test::{compare_ignore_zoneid, next_test_ip4, next_test_ip6};
use crate::net::test::{LOCALHOST_IP4, LOCALHOST_IP6, compare_ignore_zoneid};
use crate::net::*;
use crate::sync::mpsc::channel;
use crate::thread;
use crate::time::{Duration, Instant};

fn each_ip(f: &mut dyn FnMut(SocketAddr, SocketAddr)) {
f(next_test_ip4(), next_test_ip4());
f(next_test_ip6(), next_test_ip6());
}

macro_rules! t {
($e:expr) => {
match $e {
Expand All @@ -19,6 +14,11 @@ macro_rules! t {
};
}

fn each_ip(f: &mut dyn FnMut(UdpSocket, UdpSocket)) {
f(t!(UdpSocket::bind(LOCALHOST_IP4)), t!(UdpSocket::bind(LOCALHOST_IP4)));
f(t!(UdpSocket::bind(LOCALHOST_IP6)), t!(UdpSocket::bind(LOCALHOST_IP6)));
}

#[test]
fn bind_error() {
match UdpSocket::bind("1.1.1.1:9999") {
Expand All @@ -30,18 +30,19 @@ fn bind_error() {
#[test]
#[cfg_attr(target_os = "wasi", ignore)] // no threads
fn socket_smoke_test_ip4() {
each_ip(&mut |server_ip, client_ip| {
each_ip(&mut |server, client| {
let server_ip = t!(server.local_addr());
let client_ip = t!(client.local_addr());

let (tx1, rx1) = channel();
let (tx2, rx2) = channel();

let _t = thread::spawn(move || {
let client = t!(UdpSocket::bind(&client_ip));
rx1.recv().unwrap();
t!(client.send_to(&[99], &server_ip));
tx2.send(()).unwrap();
});

let server = t!(UdpSocket::bind(&server_ip));
tx1.send(()).unwrap();
let mut buf = [0];
let (nread, src) = t!(server.recv_from(&mut buf));
Expand All @@ -53,29 +54,28 @@ fn socket_smoke_test_ip4() {
}

#[test]
fn socket_name() {
each_ip(&mut |addr, _| {
let server = t!(UdpSocket::bind(&addr));
assert_eq!(addr, t!(server.local_addr()));
})
}
fn socket_and_peer_name() {
each_ip(&mut |sock1, sock2| {
let addr1 = t!(sock1.local_addr());
let addr2 = t!(sock2.local_addr());

#[test]
fn socket_peer() {
each_ip(&mut |addr1, addr2| {
let server = t!(UdpSocket::bind(&addr1));
assert_eq!(server.peer_addr().unwrap_err().kind(), ErrorKind::NotConnected);
t!(server.connect(&addr2));
assert_eq!(addr2, t!(server.peer_addr()));
assert_eq!(sock1.peer_addr().unwrap_err().kind(), ErrorKind::NotConnected);
assert!(addr1.ip() == LOCALHOST_IP4.ip() || addr1.ip() == LOCALHOST_IP6.ip());

t!(sock1.connect(addr2));
t!(sock2.connect(addr1));

assert_eq!(addr2, t!(sock1.peer_addr()));
assert_eq!(addr1, t!(sock2.peer_addr()));
})
}

#[test]
#[cfg_attr(target_os = "wasi", ignore)] // no threads
fn udp_clone_smoke() {
each_ip(&mut |addr1, addr2| {
let sock1 = t!(UdpSocket::bind(&addr1));
let sock2 = t!(UdpSocket::bind(&addr2));
each_ip(&mut |sock1, sock2| {
let addr1 = t!(sock1.local_addr());
let addr2 = t!(sock2.local_addr());

let _t = thread::spawn(move || {
let mut buf = [0, 0];
Expand Down Expand Up @@ -107,9 +107,9 @@ fn udp_clone_smoke() {
#[test]
#[cfg_attr(target_os = "wasi", ignore)] // no threads
fn udp_clone_two_read() {
each_ip(&mut |addr1, addr2| {
let sock1 = t!(UdpSocket::bind(&addr1));
let sock2 = t!(UdpSocket::bind(&addr2));
each_ip(&mut |sock1, sock2| {
let addr1 = t!(sock1.local_addr());

let (tx1, rx) = channel();
let tx2 = tx1.clone();

Expand Down Expand Up @@ -140,9 +140,8 @@ fn udp_clone_two_read() {
#[test]
#[cfg_attr(target_os = "wasi", ignore)] // no threads
fn udp_clone_two_write() {
each_ip(&mut |addr1, addr2| {
let sock1 = t!(UdpSocket::bind(&addr1));
let sock2 = t!(UdpSocket::bind(&addr2));
each_ip(&mut |sock1, sock2| {
let addr2 = t!(sock2.local_addr());

let (tx, rx) = channel();
let (serv_tx, serv_rx) = channel();
Expand Down Expand Up @@ -177,9 +176,9 @@ fn udp_clone_two_write() {
#[test]
fn debug() {
let name = if cfg!(windows) { "socket" } else { "fd" };
let socket_addr = next_test_ip4();

let udpsock = t!(UdpSocket::bind(&socket_addr));
let udpsock = t!(UdpSocket::bind(LOCALHOST_IP4));
let socket_addr = t!(udpsock.local_addr());
let udpsock_inner = udpsock.0.socket().as_raw();
let compare = format!("UdpSocket {{ addr: {socket_addr:?}, {name}: {udpsock_inner:?} }}");
assert_eq!(format!("{udpsock:?}"), compare);
Expand All @@ -195,9 +194,7 @@ fn debug() {
#[cfg_attr(target_os = "wasi", ignore)] // timeout not supported
#[test]
fn timeouts() {
let addr = next_test_ip4();

let stream = t!(UdpSocket::bind(&addr));
let stream = t!(UdpSocket::bind(LOCALHOST_IP4));
let dur = Duration::new(15410, 0);

assert_eq!(None, t!(stream.read_timeout()));
Expand All @@ -220,9 +217,7 @@ fn timeouts() {
#[test]
#[cfg_attr(target_os = "wasi", ignore)] // timeout not supported
fn test_read_timeout() {
let addr = next_test_ip4();

let stream = t!(UdpSocket::bind(&addr));
let stream = t!(UdpSocket::bind(LOCALHOST_IP4));
t!(stream.set_read_timeout(Some(Duration::from_millis(1000))));

let mut buf = [0; 10];
Expand All @@ -245,9 +240,8 @@ fn test_read_timeout() {
#[test]
#[cfg_attr(target_os = "wasi", ignore)] // timeout not supported
fn test_read_with_timeout() {
let addr = next_test_ip4();

let stream = t!(UdpSocket::bind(&addr));
let stream = t!(UdpSocket::bind(LOCALHOST_IP4));
let addr = t!(stream.local_addr());
t!(stream.set_read_timeout(Some(Duration::from_millis(1000))));

t!(stream.send_to(b"hello world", &addr));
Expand Down Expand Up @@ -275,9 +269,7 @@ fn test_read_with_timeout() {
// when passed zero Durations
#[test]
fn test_timeout_zero_duration() {
let addr = next_test_ip4();

let socket = t!(UdpSocket::bind(&addr));
let socket = t!(UdpSocket::bind(LOCALHOST_IP4));

let result = socket.set_write_timeout(Some(Duration::new(0, 0)));
let err = result.unwrap_err();
Expand All @@ -290,9 +282,8 @@ fn test_timeout_zero_duration() {

#[test]
fn connect_send_recv() {
let addr = next_test_ip4();

let socket = t!(UdpSocket::bind(&addr));
let socket = t!(UdpSocket::bind(LOCALHOST_IP4));
let addr = t!(socket.local_addr());
t!(socket.connect(addr));

t!(socket.send(b"hello world"));
Expand All @@ -305,8 +296,8 @@ fn connect_send_recv() {
#[test]
#[cfg_attr(target_os = "wasi", ignore)] // peek not supported
fn connect_send_peek_recv() {
each_ip(&mut |addr, _| {
let socket = t!(UdpSocket::bind(&addr));
each_ip(&mut |socket, _| {
let addr = t!(socket.local_addr());
t!(socket.connect(addr));

t!(socket.send(b"hello world"));
Expand All @@ -328,8 +319,8 @@ fn connect_send_peek_recv() {
#[test]
#[cfg_attr(target_os = "wasi", ignore)] // peek_from not supported
fn peek_from() {
each_ip(&mut |addr, _| {
let socket = t!(UdpSocket::bind(&addr));
each_ip(&mut |socket, _| {
let addr = t!(socket.local_addr());
t!(socket.send_to(b"hello world", &addr));

for _ in 1..3 {
Expand All @@ -350,18 +341,16 @@ fn peek_from() {
fn ttl() {
let ttl = 100;

let addr = next_test_ip4();

let stream = t!(UdpSocket::bind(&addr));
let stream = t!(UdpSocket::bind(LOCALHOST_IP4));

t!(stream.set_ttl(ttl));
assert_eq!(ttl, t!(stream.ttl()));
}

#[test]
fn set_nonblocking() {
each_ip(&mut |addr, _| {
let socket = t!(UdpSocket::bind(&addr));
each_ip(&mut |socket, _| {
let addr = t!(socket.local_addr());

t!(socket.set_nonblocking(true));
t!(socket.set_nonblocking(false));
Expand Down
13 changes: 7 additions & 6 deletions library/std/src/os/net/linux_ext/tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[test]
fn quickack() {
use crate::net::test::next_test_ip4;
use crate::net::test::LOCALHOST_IP4;
use crate::net::{TcpListener, TcpStream};
use crate::os::net::linux_ext::tcp::TcpStreamExt;

Expand All @@ -13,8 +13,8 @@ fn quickack() {
};
}

let addr = next_test_ip4();
let _listener = t!(TcpListener::bind(&addr));
let listener = t!(TcpListener::bind(LOCALHOST_IP4));
let addr = t!(listener.local_addr());

let stream = t!(TcpStream::connect(&("localhost", addr.port())));

Expand All @@ -29,7 +29,7 @@ fn quickack() {
#[test]
#[cfg(target_os = "linux")]
fn deferaccept() {
use crate::net::test::next_test_ip4;
use crate::net::test::LOCALHOST_IP4;
use crate::net::{TcpListener, TcpStream};
use crate::os::net::linux_ext::tcp::TcpStreamExt;
use crate::time::Duration;
Expand All @@ -43,10 +43,11 @@ fn deferaccept() {
};
}

let addr = next_test_ip4();
let one = Duration::from_secs(1u64);
let zero = Duration::from_secs(0u64);
let _listener = t!(TcpListener::bind(&addr));

let listener = t!(TcpListener::bind(LOCALHOST_IP4));
let addr = t!(listener.local_addr());
let stream = t!(TcpStream::connect(&("localhost", addr.port())));
stream.set_deferaccept(one).expect("set_deferaccept failed");
assert_eq!(stream.deferaccept().unwrap(), one);
Expand Down
Loading