Files
DomiStream/fuzz/fuzz_targets/audio_depacketizer.rs
2026-11-25 12:52:38 +01:00

51 lines
1.9 KiB
Rust

//! Fuzzes the audio depacketizer with a sequence of datagrams (FEC on, a Cauchy test matrix).
//! Asserts invariants, not only "no panic": never two events for one sequence number in a short run; output stays bounded by the input.
//! How to run: see the comment at the top of `fuzz/Cargo.toml`.
#![no_main]
use libfuzzer_sys::fuzz_target;
use core::time::Duration;
use std::collections::HashSet;
use domistream::Instant;
use domistream::audio::{AudioConfig, AudioDepacketizer, AudioEvent, AudioFec};
// 1 / (x ^ y) over GF(2^8)/0x11D with x in {0, 1} and y in {2, 3, 4, 5}: a 2 x 4 Cauchy matrix, the same one
// tests/common builds with gf::cauchy(2, 4).
const MATRIX: [u8; 8] = [0x8e, 0xf4, 0x47, 0xa7, 0xf4, 0x8e, 0xa7, 0x47];
fuzz_target!(|data: &[u8]| {
let cfg = AudioConfig {
cipher: None,
packet_duration_ms: 5,
fec: AudioFec::ParityMatrix(MATRIX),
loss_timeout: Duration::from_millis(20),
};
let Ok(mut d) = AudioDepacketizer::new(cfg) else { return };
let mut rest = data;
let mut us = 0u64;
let mut seen = HashSet::new();
let mut out_bytes = 0usize;
while let Some((&len, tail)) = rest.split_first() {
let len = usize::from(len).min(tail.len());
let (g, tail) = tail.split_at(len);
rest = tail;
us += 1000;
let _ = d.push(Instant::from_micros(us), g);
let _ = d.handle_timeout(Instant::from_micros(us));
while let Some(e) = d.poll_event() {
let seq = match &e {
AudioEvent::Packet { sequence, opus, .. } => {
out_bytes += opus.len();
*sequence
}
AudioEvent::Lost { sequence } => *sequence,
};
assert!(seen.insert(seq), "two events for sequence {seq}");
}
}
assert!(out_bytes <= data.len() * 2, "FEC rebuilds packets, but not out of nothing");
});