[domistream] Tests: fuzz targets assert invariants
Every target now checks properties instead of only calling the parser: parse -> encode -> parse stability (RTSP request, ENet, input), caps and TooLarge past them, chunking invariance of the framer, XML vs status parser agreement, seal/open round trips, no forged GCM tags. New targets for the parsers and state machines that had none: server_cert_der, enet_client, media_decrypt, video_depacketizer, audio_depacketizer, fec_reconstruct, pairing_session, rtsp_client (25 targets).
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
//! Fuzzes Reed-Solomon reconstruction with fuzzer-chosen shapes and erasures.
|
||||
//! Asserts invariants, not only "no panic": up to m erasures always rebuild the encoded data byte-exact; more are refused; hostile shard sets never panic.
|
||||
//! How to run: see the comment at the top of `fuzz/Cargo.toml`.
|
||||
|
||||
#![no_main]
|
||||
|
||||
use libfuzzer_sys::fuzz_target;
|
||||
|
||||
use domistream::fec::ReedSolomon;
|
||||
|
||||
fuzz_target!(|data: &[u8]| {
|
||||
if data.len() < 4 {
|
||||
return;
|
||||
}
|
||||
let k = 1 + usize::from(data[0]) % 32;
|
||||
let m = 1 + usize::from(data[1]) % 16;
|
||||
let len = 1 + usize::from(data[2]) % 32;
|
||||
let Ok(code) = ReedSolomon::new(k, m) else { return };
|
||||
let body = &data[3..];
|
||||
let mut shards: Vec<Vec<u8>> =
|
||||
(0..k).map(|i| (0..len).map(|x| body[(i * len + x) % body.len()]).collect()).collect();
|
||||
shards.extend((0..m).map(|_| vec![0; len]));
|
||||
code.encode(&mut shards).expect("encode");
|
||||
let mask = u32::from(body[body.len() - 1]);
|
||||
let lost: Vec<usize> = (0..k + m).filter(|&i| (mask.rotate_left(i as u32) ^ (i as u32 * 7)) % 3 == 0).collect();
|
||||
let mut s: Vec<Option<Vec<u8>>> =
|
||||
shards.iter().enumerate().map(|(i, v)| (!lost.contains(&i)).then(|| v.clone())).collect();
|
||||
let r = code.reconstruct(&mut s);
|
||||
if lost.len() <= m {
|
||||
r.expect("<= m erasures rebuild");
|
||||
for (a, b) in s.iter().zip(&shards) {
|
||||
assert_eq!(a.as_deref(), Some(b.as_slice()));
|
||||
}
|
||||
} else {
|
||||
assert!(matches!(r, Err(domistream::Error::Truncated { .. })));
|
||||
}
|
||||
// The raw bytes as a hostile shard set.
|
||||
let mut hostile: Vec<Option<Vec<u8>>> = body.chunks(len).map(|c| (c[0] & 1 == 0).then(|| c.to_vec())).collect();
|
||||
let _ = code.reconstruct(&mut hostile);
|
||||
});
|
||||
Reference in New Issue
Block a user