use std::path::{Path, PathBuf};
use crate::fuzz_oracles as oracles;
fn regressions_dir() -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR"))
.join("fuzz")
.join("regressions")
}
fn replay(target: &str, check: fn(&[u8])) {
let dir = regressions_dir().join(target);
let mut inputs: Vec<PathBuf> = std::fs::read_dir(&dir)
.unwrap_or_else(|e| panic!("{}: {e}", dir.display()))
.map(|entry| entry.expect("a readable directory entry").path())
.filter(|path| path.is_file())
.collect();
inputs.sort();
assert!(!inputs.is_empty(), "{} holds no inputs", dir.display());
let failed: Vec<String> = inputs
.iter()
.filter(|path| {
let data = std::fs::read(path).expect("a readable input");
std::panic::catch_unwind(|| check(&data)).is_err()
})
.map(|path| path.display().to_string())
.collect();
assert!(
failed.is_empty(),
"{target}: {} of {} inputs fail: {failed:#?}",
failed.len(),
inputs.len()
);
}
macro_rules! replay_tests {
($($target:ident),* $(,)?) => {
$(
#[test]
fn $target() {
replay(stringify!($target), oracles::$target);
}
)*
const REPLAYED: &[&str] = &[$(stringify!($target)),*];
};
}
replay_tests!(description_session_unmarshal, description_roundtrip,);
#[test]
fn every_regression_directory_is_replayed() {
let dir = regressions_dir();
let mut directories = 0;
for entry in std::fs::read_dir(&dir).unwrap_or_else(|e| panic!("{}: {e}", dir.display())) {
let path = entry.expect("a readable directory entry").path();
if path.is_dir() {
let name = path.file_name().unwrap().to_string_lossy().into_owned();
assert!(
REPLAYED.contains(&name.as_str()),
"fuzz/regressions/{name} is replayed by no test: add it to replay_tests!"
);
directories += 1;
}
}
assert_eq!(
directories,
REPLAYED.len(),
"every replayed target has a directory"
);
}