rtci-rtcp 0.5.0

RTCP packet parsing and serialization (RFC 3550, RFC 4585, RFC 5104, RFC 3611, RFC 8888) for WebRTC
Documentation
//! The checks the RTCP parser 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 crate::packet::{Packet, marshal, unmarshal, unmarshal_lenient};

/// A datagram parsed by the strict reader: no panic.
pub fn packet_unmarshal(data: &[u8]) {
    let mut buf = data;
    let _ = unmarshal(&mut buf);
}

/// The lenient reader keeps a malformed packet as raw bytes, so it accepts everything the
/// strict reader does, and what it accepts it can write back and read again.
pub fn packet_unmarshal_lenient(data: &[u8]) {
    let mut buf = data;
    let lenient = unmarshal_lenient(&mut buf);
    let mut buf = data;
    if unmarshal(&mut buf).is_ok() {
        assert!(
            lenient.is_ok(),
            "the lenient reader accepts what the strict one does"
        );
    }
    if let Ok(packets) = lenient {
        for packet in &packets {
            let _ = packet.header();
            let _ = packet.destination_ssrc();
        }
        if let Ok(raw) = marshal(&packets) {
            unmarshal_lenient(&mut raw.freeze()).expect("what was written is read again");
        }
    }
}

/// Each packet writes exactly the octets its `marshal_size` declares (the compound writer
/// trusts it to place the next packet).
fn sizes_are_what_is_written(packets: &[Box<dyn Packet>]) {
    for packet in packets {
        let mut buf = vec![0u8; packet.marshal_size()];
        if let Ok(n) = packet.marshal_to(&mut buf) {
            assert_eq!(
                n,
                buf.len(),
                "marshal_to writes marshal_size octets: {packet:?}"
            );
        }
    }
}

/// A datagram that parses is written back and read again: the second reading must be written
/// as the same bytes, so the parser and the writers agree on one wire form for every input the
/// parser accepts, and the second and third readings are equal packet by packet. This reaches
/// every packet type through the dispatcher, with the padding and length handling in front of
/// each. Every packet writes the size it declares.
pub fn packet_roundtrip(data: &[u8]) {
    let mut buf = data;
    if let Ok(packets) = unmarshal(&mut buf) {
        sizes_are_what_is_written(&packets);
        if let Ok(raw) = marshal(&packets) {
            let again = unmarshal(&mut raw.clone().freeze()).expect("what was written is read");
            assert_eq!(packets.len(), again.len());
            sizes_are_what_is_written(&again);
            let rewritten = marshal(&again).expect("and written again");
            assert_eq!(raw, rewritten, "marshal(parse(.)) is a fixed point");
            let third = unmarshal(&mut rewritten.freeze()).expect("and read again");
            for (a, b) in again.iter().zip(third.iter()) {
                assert!(
                    a.equal(&**b),
                    "the same bytes read as the same packet: {a:?} {b:?}"
                );
            }
        }
    }
}