use crate::{
transport::{
connection::{KEEPALIVE_REQUEST, KEEPALIVE_RESPONSE},
udp::UdpConnection,
TransportEvent,
},
Result,
};
use std::time::Duration;
use tokio::{select, sync::mpsc::unbounded_channel, time::sleep};
#[tokio::test]
async fn test_udp_bind_is_exclusive_and_reusable_after_close() -> Result<()> {
let first = UdpConnection::create_connection("127.0.0.1:0".parse()?, None, None).await?;
let addr = first.get_addr().get_socketaddr()?;
match UdpConnection::create_connection(addr, None, None).await {
Err(crate::Error::IoError(err)) => {
assert_eq!(err.kind(), std::io::ErrorKind::AddrInUse);
}
Err(err) => panic!("expected AddrInUse, got {err}"),
Ok(_) => panic!("a second UDP socket bound an address still in use"),
}
drop(first);
let rebound = UdpConnection::create_connection(addr, None, None).await?;
assert_eq!(rebound.get_addr().get_socketaddr()?, addr);
Ok(())
}
#[tokio::test]
async fn test_udp_keepalive() -> Result<()> {
let peer_bob = UdpConnection::create_connection("127.0.0.1:0".parse()?, None, None).await?;
let peer_alice = UdpConnection::create_connection("127.0.0.1:0".parse()?, None, None).await?;
let (alice_tx, _) = unbounded_channel::<TransportEvent>();
let bob_loop = async {
sleep(Duration::from_millis(20)).await; peer_bob
.send_raw(KEEPALIVE_REQUEST, peer_alice.get_addr())
.await
.expect("send_raw");
let buf = &mut [0u8; 2048];
let (n, _) = peer_bob.recv_raw(buf).await.expect("recv_raw");
assert_eq!(&buf[..n], KEEPALIVE_RESPONSE);
};
select! {
_ = peer_alice.serve_loop(alice_tx) => {
assert!(false, "serve_loop exited");
}
_ = bob_loop => {}
_= sleep(Duration::from_millis(200)) => {
assert!(false, "timeout waiting for keep alive response");
}
};
Ok(())
}
#[tokio::test]
async fn test_udp_recv_sip_message() -> Result<()> {
let peer_bob = UdpConnection::create_connection("127.0.0.1:0".parse()?, None, None).await?;
let peer_alice = UdpConnection::create_connection("127.0.0.1:0".parse()?, None, None).await?;
let (alice_tx, _) = unbounded_channel();
let (bob_tx, mut bob_rx) = unbounded_channel();
let send_loop = async {
sleep(Duration::from_millis(20)).await; let msg_1 = "REGISTER sip:bob@restsend.com SIP/2.0\r\nVia: SIP/2.0/UDP 127.0.0.1:5061;branch=z9hG4bKnashd92\r\nCSeq: 1 REGISTER\r\n\r\n";
peer_alice
.send_raw(msg_1.as_bytes(), peer_bob.get_addr())
.await
.expect("send_raw");
sleep(Duration::from_secs(3)).await;
};
select! {
_ = peer_alice.serve_loop(alice_tx) => {
assert!(false, "alice serve_loop exited");
}
_ = peer_bob.serve_loop(bob_tx) => {
assert!(false, "bob serve_loop exited");
}
_ = send_loop => {
assert!(false, "send_loop exited");
}
event = bob_rx.recv() => {
match event {
Some(TransportEvent::Incoming(msg, connection, from)) => {
assert!(msg.is_request());
assert_eq!(from, peer_alice.get_addr().to_owned());
assert_eq!(connection.get_addr(), peer_bob.get_addr());
assert_eq!(connection.get_remote_addr(), Some(peer_alice.get_addr()));
}
_ => {
assert!(false, "unexpected event");
}
}
}
_= sleep(Duration::from_millis(500)) => {
assert!(false, "timeout waiting");
}
};
Ok(())
}
#[tokio::test]
async fn test_udp_recv_binary_body_sip_message() -> Result<()> {
let peer_bob = UdpConnection::create_connection("127.0.0.1:0".parse()?, None, None).await?;
let peer_alice = UdpConnection::create_connection("127.0.0.1:0".parse()?, None, None).await?;
let (bob_tx, mut bob_rx) = unbounded_channel();
let send_loop = async {
sleep(Duration::from_millis(20)).await;
let mut datagram = b"MESSAGE sip:bob@example.com SIP/2.0\r\n\
Via: SIP/2.0/UDP 192.0.2.1:5060;branch=z9hG4bK1\r\n\
CSeq: 1 MESSAGE\r\n\
Content-Type: application/isup\r\n\
Content-Length: 3\r\n\r\n"
.to_vec();
datagram.extend_from_slice(&[0x00, 0x91, 0x01]);
assert!(std::str::from_utf8(&datagram).is_err());
peer_alice
.send_raw(&datagram, peer_bob.get_addr())
.await
.expect("send_raw");
sleep(Duration::from_secs(3)).await;
};
select! {
_ = peer_bob.serve_loop(bob_tx) => {
assert!(false, "bob serve_loop exited");
}
_ = send_loop => {}
event = bob_rx.recv() => {
match event {
Some(TransportEvent::Incoming(msg, _, from)) => {
assert!(msg.is_request());
assert_eq!(from, peer_alice.get_addr().to_owned());
match msg {
crate::sip::SipMessage::Request(req) => {
assert_eq!(req.body, vec![0x00, 0x91, 0x01]);
}
crate::sip::SipMessage::Response(_) => {
assert!(false, "expected request");
}
}
}
_ => {
assert!(false, "unexpected event");
}
}
}
_= sleep(Duration::from_millis(500)) => {
assert!(false, "timeout waiting for binary body message");
}
};
Ok(())
}
#[tokio::test]
async fn test_udp_dual_stack_reports_ipv4_peer_as_ipv4() -> Result<()> {
let dual = UdpConnection::create_connection("[::]:0".parse()?, None, None).await?;
let port = dual.get_addr().get_socketaddr()?.port();
let v4 = tokio::net::UdpSocket::bind("127.0.0.1:0").await?;
v4.send_to(b"hello", ("127.0.0.1", port)).await?;
let buf = &mut [0u8; 64];
let (n, from) = dual.recv_raw(buf).await?;
assert_eq!(&buf[..n], b"hello");
assert_eq!(from.get_socketaddr()?, v4.local_addr()?);
let v6 = tokio::net::UdpSocket::bind("[::1]:0").await?;
v6.send_to(b"hello", ("::1", port)).await?;
let (_, from) = dual.recv_raw(buf).await?;
assert_eq!(from.get_socketaddr()?, v6.local_addr()?);
Ok(())
}
#[test]
fn test_ipv6_sockets_set_v6only_explicitly() -> Result<()> {
use crate::platform::net::socket_for;
use socket2::{Protocol, Type};
for (ty, proto) in [(Type::DGRAM, Protocol::UDP), (Type::STREAM, Protocol::TCP)] {
let any = socket_for("[::]:0".parse()?, ty, proto)?;
assert!(!any.only_v6()?, "`::` is dual-stack");
let one = socket_for("[::1]:0".parse()?, ty, proto)?;
assert!(one.only_v6()?, "a given IPv6 address is IPv6 only");
let v4 = socket_for("127.0.0.1:0".parse()?, ty, proto)?;
let ipv6: std::net::SocketAddr = "[::1]:0".parse()?;
assert!(
v4.bind(&ipv6.into()).is_err(),
"an IPv4 socket must not accept an IPv6 address"
);
}
Ok(())
}