Files

75 lines
2.0 KiB
Rust
Raw Permalink Normal View History

2021-02-24 23:05:00 -07:00
#![no_main]
2024-01-22 18:41:33 +01:00
#[macro_use]
extern crate libfuzzer_sys;
2024-03-31 23:39:43 +02:00
use diffutilslib::params::Params;
2024-03-19 04:50:33 +08:00
use diffutilslib::unified_diff;
2021-02-24 23:05:00 -07:00
use std::fs::{self, File};
use std::io::Write;
use std::process::Command;
fuzz_target!(|x: (Vec<u8>, Vec<u8>, u8)| {
let (from, to, context) = x;
2021-02-25 00:50:38 -07:00
/*if let Ok(s) = String::from_utf8(from.clone()) {
2021-02-24 23:05:00 -07:00
if !s.is_ascii() { return }
if s.find(|x| x < ' ' && x != '\n').is_some() { return }
} else {
return
}
if let Ok(s) = String::from_utf8(to.clone()) {
if !s.is_ascii() { return }
if s.find(|x| x < ' ' && x != '\n').is_some() { return }
} else {
return
2021-02-25 00:50:38 -07:00
}*/
fs::create_dir_all("target").unwrap();
let patched = "target/fuzz.file";
2024-01-22 18:41:33 +01:00
let diff = unified_diff::diff(
&from,
&to,
2024-03-31 23:39:43 +02:00
&Params {
from: patched.into(),
to: patched.into(),
2024-03-31 23:39:43 +02:00
context_count: context as usize,
..Default::default()
},
2024-01-22 18:41:33 +01:00
);
2021-02-24 23:05:00 -07:00
File::create("target/fuzz.file.original")
.unwrap()
.write_all(&from)
.unwrap();
File::create("target/fuzz.file.expected")
.unwrap()
.write_all(&to)
.unwrap();
File::create("target/fuzz.file")
.unwrap()
.write_all(&from)
.unwrap();
File::create("target/fuzz.diff")
.unwrap()
.write_all(&diff)
.unwrap();
let output = Command::new("patch")
.arg("-p0")
.arg("--binary")
.arg("--fuzz=0")
.stdin(File::open("target/fuzz.diff").unwrap())
.output()
.unwrap();
if !output.status.success() {
2024-01-22 18:41:33 +01:00
panic!(
"STDOUT:\n{}\nSTDERR:\n{}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
);
2021-02-24 23:05:00 -07:00
}
let result = fs::read("target/fuzz.file").unwrap();
if result != to {
2024-01-22 18:41:33 +01:00
panic!(
"STDOUT:\n{}\nSTDERR:\n{}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
);
2021-02-24 23:05:00 -07:00
}
});