use super::robustness_test::*;
use super::*;
use crate::chunk::chunk_type::*;
use crate::endpoint::Endpoint;
use crate::{ClientConfig, EndpointConfig, RtoConfig};
const MAX_INIT_RETRANSMITS: usize = 8;
fn connecting(config: TransportConfig) -> (Association, Instant) {
let mut endpoint = Endpoint::new(
remote(),
TransportProtocol::UDP,
Arc::new(EndpointConfig::default()),
None,
);
let now = Instant::now();
let (_, mut a) = endpoint
.connect(
now,
ClientConfig::new(config),
"10.0.0.2:5000".parse().unwrap(),
)
.unwrap();
assert_eq!(
1,
chunks_of_type(&sent_datagrams(&mut a, now), CT_INIT.0).len(),
"precondition: the INIT"
);
(a, now)
}
fn expire_until_given_up(
a: &mut Association,
mut now: Instant,
timer: Timer,
typ: u8,
) -> (Vec<u64>, Vec<usize>) {
let (mut intervals, mut sent) = (vec![], vec![]);
for _ in 0..=MAX_INIT_RETRANSMITS + 2 {
let Some(due) = a.timers.get(timer) else {
break;
};
intervals.push((due - now).as_millis() as u64);
a.handle_timeout(due);
sent.push(chunks_of_type(&sent_datagrams(a, due), typ).len());
now = due;
}
(intervals, sent)
}
fn handshake_failed(a: &mut Association) -> bool {
let mut failed = false;
while let Some(e) = a.poll() {
failed |= matches!(
e,
Event::AssociationClosed {
reason: AssociationError::HandshakeFailed(_)
}
);
}
failed
}
fn etsi_rto() -> TransportConfig {
TransportConfig::default().with_rto_config(RtoConfig {
initial: Duration::from_millis(100),
min: Duration::from_millis(100),
max: Duration::from_millis(800),
})
}
fn backed_off(initial: u64, max: u64) -> Vec<u64> {
(0..=MAX_INIT_RETRANSMITS as u32)
.map(|n| (initial << n).min(max))
.collect()
}
#[test]
fn an_unanswered_init_is_sent_max_init_retransmits_times_then_the_setup_fails() {
for (config, initial, max) in [
(etsi_rto(), 100, 800),
(TransportConfig::default(), 1000, 60_000),
] {
let (mut a, now) = connecting(config);
let (intervals, sent) = expire_until_given_up(&mut a, now, Timer::T1Init, CT_INIT.0);
assert_eq!(backed_off(initial, max), intervals, "RTO.Max {max} ms");
let mut expected = vec![1; MAX_INIT_RETRANSMITS];
expected.push(0);
assert_eq!(expected, sent, "an INIT per expiry, none after the last");
assert!(a.is_handshaking(), "it never connected");
assert!(a.poll_timeout().is_none(), "no timer left running");
assert!(handshake_failed(&mut a), "the failure is reported");
}
}
#[test]
fn an_unanswered_cookie_echo_is_sent_max_init_retransmits_times_then_the_setup_fails() {
for (config, initial, max) in [
(etsi_rto(), 100, 800),
(TransportConfig::default(), 1000, 60_000),
] {
let (mut a, now) = connecting(config);
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(), "precondition");
assert_eq!(
1,
chunks_of_type(&sent_datagrams(&mut a, now), CT_COOKIE_ECHO.0).len(),
"precondition: the COOKIE ECHO"
);
assert!(a.timers.get(Timer::T1Init).is_none(), "T1-init stopped");
let (intervals, sent) =
expire_until_given_up(&mut a, now, Timer::T1Cookie, CT_COOKIE_ECHO.0);
assert_eq!(backed_off(initial, max), intervals, "RTO.Max {max} ms");
let mut expected = vec![1; MAX_INIT_RETRANSMITS];
expected.push(0);
assert_eq!(
expected, sent,
"a COOKIE ECHO per expiry, none after the last"
);
assert!(a.is_handshaking(), "it never connected");
assert!(a.poll_timeout().is_none(), "no timer left running");
assert!(handshake_failed(&mut a), "the failure is reported");
}
}