rtci-sctp 0.5.0

Sans-I/O SCTP (RFC 9260) with partial reliability and stream reconfiguration, for WebRTC data channels
Documentation
//! The handshake timers to their end (RFC 9260 4, 5.1, 6.3.3): an INIT or COOKIE ECHO that is
//! never answered is retransmitted "up to 'Max.Init.Retransmits' times", each time after the
//! backed-off RTO bounded by RTO.Max, and then "the endpoint MUST abort the initialization
//! process and report the error to the SCTP user". The ETSI TS 102 369 test purposes
//! as-i-1-2-1, as-i-1-2-2, as-i-1-3-1 and as-i-1-3-2 (nplab ETSI-SCTP-Conformance-Testsuite,
//! BSD-2: the purposes, not the scripts) with their RTO.Initial 100 ms and RTO.Max 800 ms,
//! and with the defaults.

use super::robustness_test::*;
use super::*;
use crate::chunk::chunk_type::*;
use crate::endpoint::Endpoint;
use crate::{ClientConfig, EndpointConfig, RtoConfig};

/// Max.Init.Retransmits (RFC 9260 16).
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)
}

/// Runs `timer` until it gives up: the interval before each expiry (ms), and how many `typ`
/// chunks each expiry sent. Bounded: more expiries than the limit allows fail the test.
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),
    })
}

/// The intervals of `MAX_INIT_RETRANSMITS + 1` expiries: RTO.Initial doubled on each, at
/// most RTO.Max (6.3.3 E2).
fn backed_off(initial: u64, max: u64) -> Vec<u64> {
    (0..=MAX_INIT_RETRANSMITS as u32)
        .map(|n| (initial << n).min(max))
        .collect()
}

/// as-i-1-2-1, as-i-1-3-1: T1-init. Eight retransmissions, the ninth expiry ends the
/// association with HandshakeFailed and sends nothing.
#[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");
    }
}

/// as-i-1-2-2, as-i-1-3-2: T1-cookie, after an INIT ACK that came at once (so the RTO is
/// still RTO.Initial). Eight COOKIE ECHO retransmissions, then the setup fails.
#[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");
    }
}