From d13e9e0d077897cd76bfc7a6791429d8d2560186 Mon Sep 17 00:00:00 2001 From: Damian Poddebniak Date: Mon, 24 May 2021 17:27:35 +0200 Subject: [PATCH] Add `cargo fuzz` target for `command` parser (and serializer). --- fuzz/.gitignore | 4 ++++ fuzz/Cargo.toml | 26 ++++++++++++++++++++++++++ fuzz/fuzz_targets/command.rs | 26 ++++++++++++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 fuzz/.gitignore create mode 100644 fuzz/Cargo.toml create mode 100644 fuzz/fuzz_targets/command.rs diff --git a/fuzz/.gitignore b/fuzz/.gitignore new file mode 100644 index 0000000..572e03b --- /dev/null +++ b/fuzz/.gitignore @@ -0,0 +1,4 @@ + +target +corpus +artifacts diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml new file mode 100644 index 0000000..ade9b9a --- /dev/null +++ b/fuzz/Cargo.toml @@ -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 diff --git a/fuzz/fuzz_targets/command.rs b/fuzz/fuzz_targets/command.rs new file mode 100644 index 0000000..2b96a20 --- /dev/null +++ b/fuzz/fuzz_targets/command.rs @@ -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); + } +});