Add `cargo fuzz` target for `command` parser (and serializer).

This commit is contained in:
Damian Poddebniak 2021-05-24 17:27:35 +02:00
parent fcd8ee93ff
commit d13e9e0d07
3 changed files with 56 additions and 0 deletions

4
fuzz/.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
target
corpus
artifacts

26
fuzz/Cargo.toml Normal file
View File

@ -0,0 +1,26 @@
[package]
name = "smtp-codec-fuzz"
version = "0.0.0"
authors = ["Automatically generated"]
publish = false
edition = "2018"
[package.metadata]
cargo-fuzz = true
[dependencies]
libfuzzer-sys = "0.4"
[dependencies.smtp-codec]
path = ".."
# Prevent this from interfering with workspaces
[workspace]
members = ["."]
[[bin]]
name = "command"
path = "fuzz_targets/command.rs"
test = false
doc = false

View File

@ -0,0 +1,26 @@
#![no_main]
use libfuzzer_sys::fuzz_target;
use smtp_codec::parse::command::command;
fuzz_target!(|data: &[u8]| {
if let Ok((_, cmd)) = command(data) {
// Fuzzer created a valid SMTP command.
// dbg!(&cmd);
let cmd2 = {
// Let's serialize the command into bytes ...
let mut buf = Vec::new();
cmd.serialize(&mut buf).unwrap();
// ... parse it again ...
let (rem, cmd2) = command(&buf).unwrap();
assert!(rem.is_empty());
// dbg!(&cmd2);
cmd2
};
// ... and verify that we got the same results.
assert_eq!(cmd, cmd2);
}
});