//! Fuzzes the video depacketizer with a sequence of datagrams. //! Asserts invariants, not only "no panic": frames stay within MAX_FRAME_BYTES and come out in increasing frame order; lost ranges are forward ranges. //! 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 domistream::Instant; use domistream::video::{MAX_FRAME_BYTES, VideoConfig, VideoDepacketizer, VideoEvent}; fuzz_target!(|data: &[u8]| { let cfg = VideoConfig { packet_size: 64, cipher: None, frame_timeout: Duration::from_millis(20) }; let Ok(mut d) = VideoDepacketizer::new(cfg) else { return }; let mut rest = data; let mut us = 0u64; let mut prev: Option = None; // Datagrams separated by a 1-byte length; time advances 1 ms per datagram. 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() { match e { VideoEvent::Frame(f) => { assert!(f.bitstream.len() <= MAX_FRAME_BYTES); if let Some(p) = prev { assert!(f.frame_index.wrapping_sub(p).wrapping_sub(1) < 1 << 31, "frame order"); } prev = Some(f.frame_index); } VideoEvent::FrameLost { first, last } => assert!(last.wrapping_sub(first) < 1 << 31), } } } });