[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:
@@ -1,11 +1,41 @@
|
||||
//! Fuzzes the XML reader behind every HTTP answer: never panics, whatever the bytes.
|
||||
//! Phase A: the parser returns `Unimplemented`; QA adds the properties (round trips, caps) with the logic.
|
||||
//! Fuzzes the XML reader behind every HTTP answer, and `parse_status_response` on the same bytes.
|
||||
//! Asserts invariants, not only "no panic": every cap holds on success, size over the cap is TooLarge, and the status parser agrees with the XML parser.
|
||||
//! How to run: see the comment at the top of `fuzz/Cargo.toml`.
|
||||
|
||||
#![no_main]
|
||||
|
||||
use libfuzzer_sys::fuzz_target;
|
||||
|
||||
use domistream::Error;
|
||||
use domistream::http::xml::{self, Element, MAX_XML_ATTRIBUTES, MAX_XML_BYTES, MAX_XML_DEPTH, MAX_XML_ELEMENTS, MAX_XML_TEXT_BYTES};
|
||||
|
||||
fn walk(e: &Element, depth: usize, count: &mut usize) {
|
||||
*count += 1;
|
||||
assert!(depth <= MAX_XML_DEPTH);
|
||||
assert!(e.attributes.len() <= MAX_XML_ATTRIBUTES);
|
||||
assert!(e.text.len() <= MAX_XML_TEXT_BYTES);
|
||||
for c in &e.children {
|
||||
walk(c, depth + 1, count);
|
||||
}
|
||||
}
|
||||
|
||||
fuzz_target!(|data: &[u8]| {
|
||||
let _ = domistream::http::xml::parse(data);
|
||||
let parsed = xml::parse(data);
|
||||
if data.len() > MAX_XML_BYTES {
|
||||
assert!(matches!(parsed, Err(Error::TooLarge { .. })));
|
||||
}
|
||||
if let Ok(d) = &parsed {
|
||||
assert_eq!(d.root.name, "root");
|
||||
let mut count = 0;
|
||||
walk(&d.root, 1, &mut count);
|
||||
assert!(count <= MAX_XML_ELEMENTS);
|
||||
}
|
||||
let status = domistream::http::parse_status_response(data);
|
||||
match (&parsed, &status) {
|
||||
(Ok(d), Ok(())) => assert_eq!(d.status_code, 200),
|
||||
(Ok(d), Err(Error::HostStatus { code })) => assert_eq!(d.status_code, *code),
|
||||
(Ok(_), Err(e)) => panic!("xml parsed but status failed with {e:?}"),
|
||||
(Err(_), Ok(())) => panic!("status accepted what xml refused"),
|
||||
(Err(_), Err(_)) => {}
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user