rtci-dtls 0.5.0

Sans-I/O DTLS 1.2 and 1.3 (RFC 6347, RFC 9147) with DTLS-SRTP key export, for WebRTC
Documentation
//! Replays every committed fuzz input through the oracle its fuzz target runs.
//!
//! `fuzz/regressions/<target>/` holds inputs that once crashed a target or that are worth
//! keeping (upstream crash cases, edge cases); each target below is one test that runs every
//! file in its directory through `fuzz_oracles::<target>`, the function the cargo-fuzz target
//! calls. A crash found by the fuzzer becomes a named regression by dropping its input into
//! that directory: no fuzzer is needed to run it. `scripts/check-fuzz-registry.py` checks that
//! every directory there is replayed here.

use std::path::{Path, PathBuf};

use crate::fuzz_oracles as oracles;

fn regressions_dir() -> PathBuf {
    Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("fuzz")
        .join("regressions")
}

/// Runs `check` on every file of `fuzz/regressions/<target>/`, and fails naming each input
/// that panics. The directory must hold at least one input, so the test cannot pass hollow.
fn replay(target: &str, check: fn(&[u8])) {
    let dir = regressions_dir().join(target);
    let mut inputs: Vec<PathBuf> = std::fs::read_dir(&dir)
        .unwrap_or_else(|e| panic!("{}: {e}", dir.display()))
        .map(|entry| entry.expect("a readable directory entry").path())
        .filter(|path| path.is_file())
        .collect();
    inputs.sort();
    assert!(!inputs.is_empty(), "{} holds no inputs", dir.display());
    let failed: Vec<String> = inputs
        .iter()
        .filter(|path| {
            let data = std::fs::read(path).expect("a readable input");
            std::panic::catch_unwind(|| check(&data)).is_err()
        })
        .map(|path| path.display().to_string())
        .collect();
    assert!(
        failed.is_empty(),
        "{target}: {} of {} inputs fail: {failed:#?}",
        failed.len(),
        inputs.len()
    );
}

macro_rules! replay_tests {
    ($($target:ident),* $(,)?) => {
        $(
            #[test]
            fn $target() {
                replay(stringify!($target), oracles::$target);
            }
        )*
        const REPLAYED: &[&str] = &[$(stringify!($target)),*];
    };
}

replay_tests!(
    record_layer_mod_unmarshal,
    handshake_mod_unmarshal,
    handshake_handshake_message_certificate_unmarshal,
    handshake_roundtrip,
    record_roundtrip,
);

#[test]
fn every_regression_directory_is_replayed() {
    let dir = regressions_dir();
    let mut directories = 0;
    for entry in std::fs::read_dir(&dir).unwrap_or_else(|e| panic!("{}: {e}", dir.display())) {
        let path = entry.expect("a readable directory entry").path();
        if path.is_dir() {
            let name = path.file_name().unwrap().to_string_lossy().into_owned();
            assert!(
                REPLAYED.contains(&name.as_str()),
                "fuzz/regressions/{name} is replayed by no test: add it to replay_tests!"
            );
            directories += 1;
        }
    }
    assert_eq!(
        directories,
        REPLAYED.len(),
        "every replayed target has a directory"
    );
}