rtci-sctp 0.5.0

Sans-I/O SCTP (RFC 9260) with partial reliability and stream reconfiguration, for WebRTC data channels
Documentation
//! The checks the SCTP fuzz targets run, one function per target of the same name.
//!
//! They live in the crate so that two callers share them: the cargo-fuzz targets in `fuzz/`
//! (built with `--cfg fuzzing`) and the replay test (`fuzz_regressions_test`), which runs every
//! committed input in `fuzz/regressions/<target>/` through the same function in the ordinary
//! test job. Each function panics when its oracle is violated. Not part of the supported API.
#![allow(missing_docs)]

use bytes::Bytes;

use crate::chunk::Chunk;
use crate::chunk::chunk_header::CHUNK_HEADER_SIZE;
use crate::packet::{PACKET_HEADER_SIZE, Packet};
use crate::util::get_padding_size;

/// Rewrites the CRC-32C field (bytes 8..12) of a raw SCTP packet so it verifies.
fn with_checksum(data: &[u8]) -> Bytes {
    let mut packet = data.to_vec();
    if packet.len() >= PACKET_HEADER_SIZE {
        packet[8..12].fill(0);
        let checksum = crc32c::crc32c(&packet);
        packet[8..12].copy_from_slice(&checksum.to_le_bytes());
    }
    Bytes::from(packet)
}

/// A raw packet parsed, checksum and all: no panic.
pub fn packet_unmarshal(data: &[u8]) {
    let _ = Packet::unmarshal(&Bytes::copy_from_slice(data));
}

/// A raw packet parsed after its checksum is made valid: no panic.
pub fn packet_unmarshal_fixed_checksum(data: &[u8]) {
    let _ = Packet::unmarshal(&with_checksum(data));
}

/// What each chunk declares about its size is what it writes: `value_length` (which
/// `marshaled_len` and the bundling of chunks into one MTU rely on) plus the 4-octet header,
/// and the Chunk Length field it writes says the same (RFC 9260 3.2: the length counts the
/// header and value, not the trailing padding).
fn chunk_sizes_are_what_is_written(chunks: &[Box<dyn Chunk>]) {
    for chunk in chunks {
        let Ok(written) = chunk.marshal() else {
            continue;
        };
        assert_eq!(
            written.len(),
            CHUNK_HEADER_SIZE + chunk.value_length(),
            "a chunk writes the size it declares: {chunk:?}"
        );
        let field = usize::from(u16::from_be_bytes([written[2], written[3]]));
        assert_eq!(
            field,
            written.len(),
            "the Chunk Length field is what was written: {chunk:?}"
        );
    }
}

/// The packet round trip shared by the two targets below. `raw` has a valid checksum.
fn roundtrip(raw: &Bytes) {
    let Ok(first) = Packet::unmarshal(raw) else {
        return;
    };
    chunk_sizes_are_what_is_written(&first.chunks);
    // A chunk may refuse to be written (a value the writer does not produce); the oracle is
    // about what is written.
    let Ok(written) = first.marshal() else {
        return;
    };
    assert_eq!(
        written.len(),
        first.marshaled_len(),
        "marshaled_len is what is written"
    );
    let ends = chunk_ends(&written);
    assert_eq!(
        ends.len(),
        first.chunks.len(),
        "every written chunk is framed by its length"
    );
    assert_eq!(
        ends.last().copied().unwrap_or(0),
        written.len() - PACKET_HEADER_SIZE,
        "the chunks, each padded to 4 octets, are the whole packet"
    );

    let second = Packet::unmarshal(&written).expect("what was written parses");
    assert_eq!(
        second.chunks.len(),
        first.chunks.len(),
        "the same chunks are read back"
    );
    chunk_sizes_are_what_is_written(&second.chunks);
    let again = second.marshal().expect("and is written again");
    assert_eq!(again, written, "marshal(parse(.)) is a fixed point");
}

/// Where each chunk of a written packet ends, padding included.
fn chunk_ends(packet: &[u8]) -> Vec<usize> {
    let mut ends = vec![];
    let mut offset = PACKET_HEADER_SIZE;
    while offset + CHUNK_HEADER_SIZE <= packet.len() {
        let length = usize::from(u16::from_be_bytes([packet[offset + 2], packet[offset + 3]]));
        if length < CHUNK_HEADER_SIZE {
            break;
        }
        offset += length + get_padding_size(length);
        ends.push(offset - PACKET_HEADER_SIZE);
    }
    ends
}

/// Round trip of an SCTP packet (RFC 9260 3): the input gets a valid checksum, and a packet
/// that parses is written and read back with the same chunks, writing it again gives the same
/// bytes, every chunk writes the length it declares and is padded to 4, and the packet is as
/// long as `marshaled_len` says.
pub fn packet_roundtrip(data: &[u8]) {
    roundtrip(&with_checksum(data));
}

/// As [`packet_roundtrip`], with the common header fixed so that every input octet is a chunk:
/// the fuzzer spends its effort on the chunk and parameter codecs.
pub fn chunks_roundtrip(data: &[u8]) {
    let mut packet = Vec::with_capacity(PACKET_HEADER_SIZE + data.len());
    packet.extend_from_slice(&[0x13, 0x88, 0x13, 0x89, 0, 0, 0, 1, 0, 0, 0, 0]);
    packet.extend_from_slice(data);
    roundtrip(&with_checksum(&packet));
}