Files
2026-11-25 12:52:38 +01:00

44 lines
1.6 KiB
Rust

//! 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 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(_)) => {}
}
});