diff options
Diffstat (limited to 'tests/fuzz.rs')
| -rw-r--r-- | tests/fuzz.rs | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/tests/fuzz.rs b/tests/fuzz.rs new file mode 100644 index 0000000..69804e8 --- /dev/null +++ b/tests/fuzz.rs @@ -0,0 +1,50 @@ +use core::slice; +use gentoo_utils::{Parseable, atom::Atom}; +use mon::{Parser, ParserFinishedError, input::InputIter}; +use std::{ + io::Write, + process::{Command, Stdio}, +}; + +#[allow(clippy::missing_safety_doc, clippy::needless_return)] +#[unsafe(no_mangle)] +pub unsafe extern "C" fn LLVMFuzzerTestOneInput(input: *const u8, len: usize) -> i32 { + let slice = unsafe { slice::from_raw_parts(input, len) }; + + let atom = match str::from_utf8(slice) { + Ok(str) => str.trim(), + _ => return -1, + }; + + let mut proc = Command::new("atom.py") + .stdin(Stdio::piped()) + .spawn() + .unwrap(); + + proc.stdin + .as_mut() + .unwrap() + .write_all(atom.as_bytes()) + .unwrap(); + + let status = proc.wait().unwrap(); + + let result = Atom::parser().check_finished(InputIter::new(atom)); + + match (status.success(), result) { + (true, Ok(_)) => { + eprintln!("agreement that {atom} is valid"); + return 0; + } + (true, Err(ParserFinishedError::Err(it) | ParserFinishedError::Unfinished(it))) => { + panic!("gentoo-utils rejected valid atom: {atom}: {}", it.rest()); + } + (false, Err(_)) => { + eprintln!("agreement that {atom} is invalid"); + return -1; + } + (false, Ok(_)) => { + panic!("gentoo-utils accepted invalid atom: {atom}"); + } + } +} |
