use super::robustness_test::*;
use super::*;
use crate::chunk::chunk_type::*;
use crate::endpoint::link_test::{Link, first_chunk_type};
use std::cell::RefCell;
use std::rc::Rc;
const PPI: PayloadProtocolIdentifier = PayloadProtocolIdentifier::Binary;
fn capture(
link: &mut Link,
mut drop: impl FnMut(bool, &Bytes) -> bool + 'static,
) -> Rc<RefCell<Vec<(bool, Bytes)>>> {
let seen: Rc<RefCell<Vec<(bool, Bytes)>>> = Rc::default();
let record = seen.clone();
link.filter = Some(Box::new(move |from_client, datagram: &Bytes| {
record.borrow_mut().push((from_client, datagram.clone()));
drop(from_client, datagram)
}));
seen
}
fn types(datagram: &Bytes) -> Vec<u8> {
chunks_of(datagram).iter().map(|chunk| chunk[0]).collect()
}
#[test]
fn outstanding_data_follows_the_cookie_ack_that_answers_a_repeated_cookie_echo() {
let mut link = Link::new(TransportConfig::default(), TransportConfig::default(), 7);
let mut cookie_acks = 0;
let seen = capture(&mut link, move |from_client, datagram| {
if !from_client && first_chunk_type(datagram) == Some(CT_COOKIE_ACK.0) {
cookie_acks += 1;
return cookie_acks == 1;
}
false
});
link.begin_connect();
let deadline = link.now + Duration::from_secs(5);
assert!(link.run_until(deadline, |l| l.server.connected()));
assert_eq!(
AssociationState::CookieEchoed,
link.client.conn().state(),
"the first COOKIE ACK is lost"
);
let now = link.now;
link.server
.conn()
.open_stream(1, PPI)
.unwrap()
.write_sctp(now, &Bytes::from_static(b"before the cookie ack"), PPI)
.unwrap();
let deadline = link.now + Duration::from_secs(30);
assert!(link.run_until(deadline, |l| !l.client.received.is_empty()));
let arrived = link.now;
let seen = seen.borrow();
let from_server: Vec<Vec<u8>> = seen
.iter()
.filter(|(from_client, _)| !from_client)
.map(|(_, datagram)| types(datagram))
.collect();
let second_ack = from_server
.iter()
.filter(|t| t.first() == Some(&CT_COOKIE_ACK.0))
.nth(1)
.expect("a second COOKIE ACK");
assert_eq!(
&vec![CT_COOKIE_ACK.0, CT_PAYLOAD_DATA.0],
second_ack,
"the COOKIE ACK first, then the DATA"
);
assert_eq!(
vec![(1, Bytes::from_static(b"before the cookie ack"))],
link.client.received
);
assert_eq!(AssociationState::Established, link.client.conn().state());
let server = link.server.conn();
assert!(
server
.timers
.get(Timer::T3RTX)
.is_none_or(|t3| t3 > arrived),
"the message did not wait for T3-rtx"
);
let deadline = link.now + Duration::from_secs(5);
assert!(link.run_until(deadline, |l| l.server.conn().inflight_queue.is_empty()));
}
#[test]
fn a_t3_retransmission_carries_a_cookie_ack_while_the_peer_may_be_in_cookie_echoed() {
let mut link = Link::new(TransportConfig::default(), TransportConfig::default(), 11);
let (mut cookie_acks, mut echoes) = (0, 0);
let seen = capture(&mut link, move |from_client, datagram| {
let typ = first_chunk_type(datagram);
if !from_client && typ == Some(CT_COOKIE_ACK.0) {
cookie_acks += 1;
return cookie_acks == 1;
}
if from_client && typ == Some(CT_COOKIE_ECHO.0) {
echoes += 1;
return echoes > 1;
}
false
});
link.begin_connect();
let deadline = link.now + Duration::from_secs(5);
assert!(link.run_until(deadline, |l| l.server.connected()));
let now = link.now;
link.server
.conn()
.open_stream(1, PPI)
.unwrap()
.write_sctp(now, &Bytes::from_static(b"open"), PPI)
.unwrap();
let deadline = link.now + Duration::from_secs(5);
assert!(
link.run_until(deadline, |l| !l.client.received.is_empty()),
"the client is in {:?}",
link.client.conn().state()
);
assert_eq!(AssociationState::Established, link.client.conn().state());
let with_cookie_ack = seen
.borrow()
.iter()
.filter(|(from_client, d)| !from_client && types(d) == [CT_COOKIE_ACK.0, CT_PAYLOAD_DATA.0])
.count();
assert_eq!(
1, with_cookie_ack,
"one T3-rtx retransmission behind a COOKIE ACK"
);
link.run_for(Duration::from_millis(100));
assert!(
!link.server.conn().peer_awaits_cookie_ack,
"the client's SACK showed it"
);
}
#[test]
fn data_written_when_the_cookie_ack_arrives_leaves_at_once() {
let mut link = Link::new(TransportConfig::default(), TransportConfig::default(), 8);
let seen = capture(&mut link, |_, _| false);
link.begin_connect();
let deadline = link.now + Duration::from_secs(5);
assert!(link.run_until(deadline, |l| l.client.conn().state()
== AssociationState::CookieEchoed));
let now = link.now;
assert!(
link.client
.conn()
.open_stream(1, PPI)
.unwrap()
.write_sctp(now, &Bytes::from_static(b"too early"), PPI)
.is_err(),
"no DATA in COOKIE-ECHOED"
);
let deadline = link.now + Duration::from_secs(5);
assert!(link.run_until(deadline, |l| l.client.connected()));
let connected_at = link.now;
link.client
.conn()
.stream(1)
.unwrap()
.write_sctp(connected_at, &Bytes::from_static(b"open"), PPI)
.unwrap();
let client_data = |seen: &Rc<RefCell<Vec<(bool, Bytes)>>>| {
seen.borrow()
.iter()
.filter(|(from_client, d)| *from_client && types(d).contains(&CT_PAYLOAD_DATA.0))
.count()
};
assert_eq!(0, client_data(&seen));
link.run_until(connected_at, |_| false);
assert_eq!(connected_at, link.now);
assert_eq!(
1,
client_data(&seen),
"on the link at the instant of the COOKIE ACK"
);
let deadline = link.now + link.latency + Duration::from_millis(1);
assert!(link.run_until(deadline, |l| !l.server.received.is_empty()));
}
#[test]
fn handshake_timers_back_off_on_their_own_and_leave_the_rto_alone() {
let mut endpoint = crate::endpoint::Endpoint::new(
remote(),
TransportProtocol::UDP,
Arc::new(crate::EndpointConfig::default()),
None,
);
let start = Instant::now();
let (_, mut a) = endpoint
.connect(
start,
crate::ClientConfig::new(TransportConfig::default()),
"10.0.0.2:5000".parse().unwrap(),
)
.unwrap();
let second = Duration::from_secs(1);
let expiries = |a: &mut Association, timer: Timer, typ: u8, from: Instant| {
let mut at = from;
for backoff in [1, 2, 4] {
let due = a.timers.get(timer).expect("the timer runs");
assert_eq!(at + second * backoff, due, "{timer:?}, expiry {backoff}");
a.handle_timeout(due);
assert_eq!(1, chunks_of_type(&sent_datagrams(a, due), typ).len());
at = due;
}
at
};
assert_eq!(
1,
chunks_of_type(&sent_datagrams(&mut a, start), CT_INIT.0).len()
);
let now = expiries(&mut a, Timer::T1Init, CT_INIT.0, start);
let init_ack = init_chunk(CT_INIT_ACK.0, 4242, 9000, &[raw_tlv(7, b"cookie")]);
let raw = packet_to(&a, &[init_ack]);
inject(&mut a, now, raw);
assert_eq!(AssociationState::CookieEchoed, a.state());
assert_eq!(
1,
chunks_of_type(&sent_datagrams(&mut a, now), CT_COOKIE_ECHO.0).len()
);
let now = expiries(&mut a, Timer::T1Cookie, CT_COOKIE_ECHO.0, now);
let raw = packet_to(&a, &[raw_chunk(CT_COOKIE_ACK.0, 0, &[])]);
inject(&mut a, now, raw);
assert_eq!(AssociationState::Established, a.state());
assert_eq!(1000, a.rto_mgr.get_rto(), "no backoff carried over");
a.open_stream(1, PPI)
.unwrap()
.write_sctp(now, &Bytes::from_static(b"first"), PPI)
.unwrap();
assert_eq!(1, transmitted_data(&a.gather_outbound(now).0).len());
assert_eq!(Some(now + second), a.timers.get(Timer::T3RTX));
}
#[test]
fn a_heartbeat_is_not_answered_before_the_cookie_ack() {
let mut endpoint = crate::endpoint::Endpoint::new(
remote(),
TransportProtocol::UDP,
Arc::new(crate::EndpointConfig::default()),
None,
);
let now = Instant::now();
let (_, mut a) = endpoint
.connect(
now,
crate::ClientConfig::new(TransportConfig::default()),
"10.0.0.2:5000".parse().unwrap(),
)
.unwrap();
let _ = sent_datagrams(&mut a, now);
let init_ack = init_chunk(CT_INIT_ACK.0, 4242, 9000, &[raw_tlv(7, b"cookie")]);
let raw = packet_to(&a, &[init_ack]);
inject(&mut a, now, raw);
assert_eq!(AssociationState::CookieEchoed, a.state());
let _ = sent_datagrams(&mut a, now);
let heartbeat = raw_chunk(CT_HEARTBEAT.0, 0, &raw_tlv(1, &[7; 16]));
let raw = packet_to(&a, std::slice::from_ref(&heartbeat));
inject(&mut a, now, raw);
let sent = sent_datagrams(&mut a, now);
assert!(
chunks_of_type(&sent, CT_HEARTBEAT_ACK.0).is_empty(),
"no HEARTBEAT ACK in COOKIE-ECHOED"
);
let raw = packet_to(&a, &[raw_chunk(CT_COOKIE_ACK.0, 0, &[])]);
inject(&mut a, now, raw);
assert_eq!(AssociationState::Established, a.state());
let _ = sent_datagrams(&mut a, now);
let raw = packet_to(&a, std::slice::from_ref(&heartbeat));
inject(&mut a, now, raw);
assert_eq!(
1,
chunks_of_type(&sent_datagrams(&mut a, now), CT_HEARTBEAT_ACK.0).len(),
"answered once ESTABLISHED"
);
}