Files
ianitor/tests/fuzz/tests/regressions.rs
T

101 lines
3.5 KiB
Rust

// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//! Every saved fuzz input, replayed in the ordinary `cargo test`: the seeds (`seeds/<target>/`,
//! the starting corpus) and every input that once crashed a target (`regressions/<target>/`,
//! copied there by `tests/fuzz/run.sh` as `<name>.hex`, hex text, since a crash input may hold
//! NUL bytes the pre-commit hook refuses). A crash found once stays a failing test until fixed.
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use std::path::Path;
fn from_hex(raw: &[u8]) -> Option<Vec<u8>> {
let digits: Vec<u8> = raw
.iter()
.copied()
.filter(|c| !c.is_ascii_whitespace())
.collect();
if digits.len() % 2 != 0 {
return None;
}
digits
.chunks(2)
.map(|p| u8::from_str_radix(std::str::from_utf8(p).ok()?, 16).ok())
.collect()
}
fn replay(dir: &str) -> usize {
let root = Path::new(env!("CARGO_MANIFEST_DIR")).join(dir);
let mut n = 0;
for (name, target) in ianitor_fuzz::TARGETS {
let d = root.join(name);
let Ok(entries) = std::fs::read_dir(&d) else {
continue;
};
let mut files: Vec<_> = entries.filter_map(Result::ok).map(|e| e.path()).collect();
files.sort();
for f in files.iter().filter(|f| f.is_file()) {
let raw = std::fs::read(f).unwrap();
let data = if f.extension().is_some_and(|e| e == "hex") {
from_hex(&raw).unwrap_or_else(|| panic!("{}: not hex", f.display()))
} else {
raw
};
let r = std::panic::catch_unwind(|| target(&data));
assert!(r.is_ok(), "{name}: {} panics", f.display());
n += 1;
}
}
n
}
#[test]
fn every_seed_runs_clean() {
let n = replay("seeds");
assert!(n >= 10, "the seed corpus has inputs for every target ({n})");
}
#[test]
fn every_saved_crash_stays_fixed() {
replay("regressions");
}
#[test]
fn every_target_has_seeds() {
let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("seeds");
for (name, _) in ianitor_fuzz::TARGETS {
let n = std::fs::read_dir(root.join(name))
.map(|d| d.count())
.unwrap_or(0);
assert!(n > 0, "no seeds for {name}");
}
}
/// The route seeds meant to be valid are: otherwise the fuzzer would start from refusals only
/// and seldom reach the compilers and edge's runtime.
#[test]
fn the_valid_route_seeds_load() {
let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("seeds/routes");
for name in ["plain", "forward-auth", "everything"] {
let data = std::fs::read(root.join(name)).unwrap();
let i = data.iter().position(|b| *b == ianitor_fuzz::SEP).unwrap();
let site = String::from_utf8(data[..i].to_vec()).unwrap();
let host = String::from_utf8(data[i + 1..].to_vec()).unwrap();
let hostname = host.lines().next().unwrap().split('"').nth(1).unwrap();
let file = format!("hosts/{hostname}.toml");
let r = ianitor_core::load(
&ianitor_core::SourceFile {
name: "ianitor.toml",
contents: &site,
},
&[ianitor_core::SourceFile {
name: &file,
contents: &host,
}],
);
assert!(r.is_ok(), "{name}: {:?}", r.err());
}
}