From 109a42699050a4e08599be67396cb6a2e247becf Mon Sep 17 00:00:00 2001 From: hashmap Date: Mon, 5 Nov 2018 21:50:16 +0100 Subject: [PATCH] Add fuzz tests for p2p crate (#1931) Add fuzz tests for p2p crate --- p2p/fuzz/Cargo.toml | 63 +++++++++++++++++++ p2p/fuzz/README.md | 38 +++++++++++ p2p/fuzz/fuzz_targets/read_ban_reason.rs | 13 ++++ p2p/fuzz/fuzz_targets/read_get_peer_addrs.rs | 13 ++++ p2p/fuzz/fuzz_targets/read_hand.rs | 13 ++++ p2p/fuzz/fuzz_targets/read_headers.rs | 13 ++++ p2p/fuzz/fuzz_targets/read_locator.rs | 13 ++++ p2p/fuzz/fuzz_targets/read_msg_header.rs | 13 ++++ p2p/fuzz/fuzz_targets/read_peer_addrs.rs | 13 ++++ p2p/fuzz/fuzz_targets/read_peer_error.rs | 13 ++++ p2p/fuzz/fuzz_targets/read_ping.rs | 13 ++++ p2p/fuzz/fuzz_targets/read_pong.rs | 13 ++++ p2p/fuzz/fuzz_targets/read_shake.rs | 13 ++++ p2p/fuzz/fuzz_targets/read_sock_addr.rs | 13 ++++ .../fuzz_targets/read_tx_hashset_archive.rs | 13 ++++ .../fuzz_targets/read_tx_hashset_request.rs | 13 ++++ 16 files changed, 283 insertions(+) create mode 100644 p2p/fuzz/Cargo.toml create mode 100644 p2p/fuzz/README.md create mode 100644 p2p/fuzz/fuzz_targets/read_ban_reason.rs create mode 100644 p2p/fuzz/fuzz_targets/read_get_peer_addrs.rs create mode 100644 p2p/fuzz/fuzz_targets/read_hand.rs create mode 100644 p2p/fuzz/fuzz_targets/read_headers.rs create mode 100644 p2p/fuzz/fuzz_targets/read_locator.rs create mode 100644 p2p/fuzz/fuzz_targets/read_msg_header.rs create mode 100644 p2p/fuzz/fuzz_targets/read_peer_addrs.rs create mode 100644 p2p/fuzz/fuzz_targets/read_peer_error.rs create mode 100644 p2p/fuzz/fuzz_targets/read_ping.rs create mode 100644 p2p/fuzz/fuzz_targets/read_pong.rs create mode 100644 p2p/fuzz/fuzz_targets/read_shake.rs create mode 100644 p2p/fuzz/fuzz_targets/read_sock_addr.rs create mode 100644 p2p/fuzz/fuzz_targets/read_tx_hashset_archive.rs create mode 100644 p2p/fuzz/fuzz_targets/read_tx_hashset_request.rs diff --git a/p2p/fuzz/Cargo.toml b/p2p/fuzz/Cargo.toml new file mode 100644 index 000000000..5337cfa0d --- /dev/null +++ b/p2p/fuzz/Cargo.toml @@ -0,0 +1,63 @@ + +[package] +name = "grin_p2p-fuzz" +version = "0.0.1" +authors = ["Automatically generated"] +publish = false + +[package.metadata] +cargo-fuzz = true + +[dependencies.grin_p2p] +path = ".." +[dependencies.grin_core] +path = "../../core" +[dependencies.libfuzzer-sys] +git = "https://github.com/rust-fuzz/libfuzzer-sys.git" + +# Prevent this from interfering with workspaces +[workspace] +members = ["."] + +[[bin]] +name = "read_msg_header" +path = "fuzz_targets/read_msg_header.rs" +[[bin]] +name = "read_hand" +path = "fuzz_targets/read_hand.rs" +[[bin]] +name = "read_shake" +path = "fuzz_targets/read_shake.rs" +[[bin]] +name = "read_get_peer_addrs" +path = "fuzz_targets/read_get_peer_addrs.rs" +[[bin]] +name = "read_peer_addrs" +path = "fuzz_targets/read_peer_addrs.rs" +[[bin]] +name = "read_peer_error" +path = "fuzz_targets/read_peer_error.rs" +[[bin]] +name = "read_sock_addr" +path = "fuzz_targets/read_sock_addr.rs" +[[bin]] +name = "read_locator" +path = "fuzz_targets/read_locator.rs" +[[bin]] +name = "read_headers" +path = "fuzz_targets/read_headers.rs" +[[bin]] +name = "read_ping" +path = "fuzz_targets/read_ping.rs" +[[bin]] +name = "read_pong" +path = "fuzz_targets/read_pong.rs" +[[bin]] +name = "read_ban_reason" +path = "fuzz_targets/read_ban_reason.rs" +[[bin]] +name = "read_tx_hashset_request" +path = "fuzz_targets/read_tx_hashset_request.rs" +[[bin]] +name = "read_tx_hashset_archive" +path = "fuzz_targets/read_tx_hashset_archive.rs" diff --git a/p2p/fuzz/README.md b/p2p/fuzz/README.md new file mode 100644 index 000000000..d25cbdea1 --- /dev/null +++ b/p2p/fuzz/README.md @@ -0,0 +1,38 @@ +# Fuzz testing + +## Installation +You have to use Rust nightly at the moment. +Cargo-fuzz (https://github.com/rust-fuzz/cargo-fuzz) has been used. +To install it: + +``` +cargo install cargo-fuzz +``` + +## Pattern generation for corpus +This step is optional, libFuzz will generate random patterns to populate +corpus (in folder `corpus`). However we can genearete more meaningful pattern +e.g. use serialized form of a real block or transaction. To generate them: + +``` +cd fuzz + +cargo run --bin gen-corpus +``` + +## Run tests +Fuzz test is basically infinite test, run it for some period of time then +stop if no failures are found. +To run the tests make sure you are in the folder `p2p` otherwise you may get +some misleading errors, then run one of the following tests: + +``` +cargo fuzz run + +``` + +Run +``` +cargo fuzz list +``` +or check `fuzz/Cargo.toml` for the full list of targets. diff --git a/p2p/fuzz/fuzz_targets/read_ban_reason.rs b/p2p/fuzz/fuzz_targets/read_ban_reason.rs new file mode 100644 index 000000000..5d930dbae --- /dev/null +++ b/p2p/fuzz/fuzz_targets/read_ban_reason.rs @@ -0,0 +1,13 @@ +#![no_main] +#[macro_use] +extern crate libfuzzer_sys; +extern crate grin_core; +extern crate grin_p2p; + +use grin_core::ser; +use grin_p2p::msg::BanReason; + +fuzz_target!(|data: &[u8]| { + let mut d = data.clone(); + let _t: Result = ser::deserialize(&mut d); +}); diff --git a/p2p/fuzz/fuzz_targets/read_get_peer_addrs.rs b/p2p/fuzz/fuzz_targets/read_get_peer_addrs.rs new file mode 100644 index 000000000..4a63068b4 --- /dev/null +++ b/p2p/fuzz/fuzz_targets/read_get_peer_addrs.rs @@ -0,0 +1,13 @@ +#![no_main] +#[macro_use] +extern crate libfuzzer_sys; +extern crate grin_core; +extern crate grin_p2p; + +use grin_core::ser; +use grin_p2p::msg::GetPeerAddrs; + +fuzz_target!(|data: &[u8]| { + let mut d = data.clone(); + let _t: Result = ser::deserialize(&mut d); +}); diff --git a/p2p/fuzz/fuzz_targets/read_hand.rs b/p2p/fuzz/fuzz_targets/read_hand.rs new file mode 100644 index 000000000..4ce20f27d --- /dev/null +++ b/p2p/fuzz/fuzz_targets/read_hand.rs @@ -0,0 +1,13 @@ +#![no_main] +#[macro_use] +extern crate libfuzzer_sys; +extern crate grin_core; +extern crate grin_p2p; + +use grin_core::ser; +use grin_p2p::msg::Hand; + +fuzz_target!(|data: &[u8]| { + let mut d = data.clone(); + let _t: Result = ser::deserialize(&mut d); +}); diff --git a/p2p/fuzz/fuzz_targets/read_headers.rs b/p2p/fuzz/fuzz_targets/read_headers.rs new file mode 100644 index 000000000..9fa95fed1 --- /dev/null +++ b/p2p/fuzz/fuzz_targets/read_headers.rs @@ -0,0 +1,13 @@ +#![no_main] +#[macro_use] +extern crate libfuzzer_sys; +extern crate grin_core; +extern crate grin_p2p; + +use grin_core::ser; +use grin_p2p::msg::Headers; + +fuzz_target!(|data: &[u8]| { + let mut d = data.clone(); + let _t: Result = ser::deserialize(&mut d); +}); diff --git a/p2p/fuzz/fuzz_targets/read_locator.rs b/p2p/fuzz/fuzz_targets/read_locator.rs new file mode 100644 index 000000000..accaa570a --- /dev/null +++ b/p2p/fuzz/fuzz_targets/read_locator.rs @@ -0,0 +1,13 @@ +#![no_main] +#[macro_use] +extern crate libfuzzer_sys; +extern crate grin_core; +extern crate grin_p2p; + +use grin_core::ser; +use grin_p2p::msg::Locator; + +fuzz_target!(|data: &[u8]| { + let mut d = data.clone(); + let _t: Result = ser::deserialize(&mut d); +}); diff --git a/p2p/fuzz/fuzz_targets/read_msg_header.rs b/p2p/fuzz/fuzz_targets/read_msg_header.rs new file mode 100644 index 000000000..7a2f7d811 --- /dev/null +++ b/p2p/fuzz/fuzz_targets/read_msg_header.rs @@ -0,0 +1,13 @@ +#![no_main] +#[macro_use] +extern crate libfuzzer_sys; +extern crate grin_core; +extern crate grin_p2p; + +use grin_core::ser; +use grin_p2p::msg::MsgHeader; + +fuzz_target!(|data: &[u8]| { + let mut d = data.clone(); + let _t: Result = ser::deserialize(&mut d); +}); diff --git a/p2p/fuzz/fuzz_targets/read_peer_addrs.rs b/p2p/fuzz/fuzz_targets/read_peer_addrs.rs new file mode 100644 index 000000000..a2896243b --- /dev/null +++ b/p2p/fuzz/fuzz_targets/read_peer_addrs.rs @@ -0,0 +1,13 @@ +#![no_main] +#[macro_use] +extern crate libfuzzer_sys; +extern crate grin_core; +extern crate grin_p2p; + +use grin_core::ser; +use grin_p2p::msg::PeerAddrs; + +fuzz_target!(|data: &[u8]| { + let mut d = data.clone(); + let _t: Result = ser::deserialize(&mut d); +}); diff --git a/p2p/fuzz/fuzz_targets/read_peer_error.rs b/p2p/fuzz/fuzz_targets/read_peer_error.rs new file mode 100644 index 000000000..80fe2ac61 --- /dev/null +++ b/p2p/fuzz/fuzz_targets/read_peer_error.rs @@ -0,0 +1,13 @@ +#![no_main] +#[macro_use] +extern crate libfuzzer_sys; +extern crate grin_core; +extern crate grin_p2p; + +use grin_core::ser; +use grin_p2p::msg::PeerError; + +fuzz_target!(|data: &[u8]| { + let mut d = data.clone(); + let _t: Result = ser::deserialize(&mut d); +}); diff --git a/p2p/fuzz/fuzz_targets/read_ping.rs b/p2p/fuzz/fuzz_targets/read_ping.rs new file mode 100644 index 000000000..788a99500 --- /dev/null +++ b/p2p/fuzz/fuzz_targets/read_ping.rs @@ -0,0 +1,13 @@ +#![no_main] +#[macro_use] +extern crate libfuzzer_sys; +extern crate grin_core; +extern crate grin_p2p; + +use grin_core::ser; +use grin_p2p::msg::Ping; + +fuzz_target!(|data: &[u8]| { + let mut d = data.clone(); + let _t: Result = ser::deserialize(&mut d); +}); diff --git a/p2p/fuzz/fuzz_targets/read_pong.rs b/p2p/fuzz/fuzz_targets/read_pong.rs new file mode 100644 index 000000000..5e741ea9d --- /dev/null +++ b/p2p/fuzz/fuzz_targets/read_pong.rs @@ -0,0 +1,13 @@ +#![no_main] +#[macro_use] +extern crate libfuzzer_sys; +extern crate grin_core; +extern crate grin_p2p; + +use grin_core::ser; +use grin_p2p::msg::Pong; + +fuzz_target!(|data: &[u8]| { + let mut d = data.clone(); + let _t: Result = ser::deserialize(&mut d); +}); diff --git a/p2p/fuzz/fuzz_targets/read_shake.rs b/p2p/fuzz/fuzz_targets/read_shake.rs new file mode 100644 index 000000000..eb8890dce --- /dev/null +++ b/p2p/fuzz/fuzz_targets/read_shake.rs @@ -0,0 +1,13 @@ +#![no_main] +#[macro_use] +extern crate libfuzzer_sys; +extern crate grin_core; +extern crate grin_p2p; + +use grin_core::ser; +use grin_p2p::msg::Shake; + +fuzz_target!(|data: &[u8]| { + let mut d = data.clone(); + let _t: Result = ser::deserialize(&mut d); +}); diff --git a/p2p/fuzz/fuzz_targets/read_sock_addr.rs b/p2p/fuzz/fuzz_targets/read_sock_addr.rs new file mode 100644 index 000000000..cbe9a26ea --- /dev/null +++ b/p2p/fuzz/fuzz_targets/read_sock_addr.rs @@ -0,0 +1,13 @@ +#![no_main] +#[macro_use] +extern crate libfuzzer_sys; +extern crate grin_core; +extern crate grin_p2p; + +use grin_core::ser; +use grin_p2p::msg::SockAddr; + +fuzz_target!(|data: &[u8]| { + let mut d = data.clone(); + let _t: Result = ser::deserialize(&mut d); +}); diff --git a/p2p/fuzz/fuzz_targets/read_tx_hashset_archive.rs b/p2p/fuzz/fuzz_targets/read_tx_hashset_archive.rs new file mode 100644 index 000000000..483d32b55 --- /dev/null +++ b/p2p/fuzz/fuzz_targets/read_tx_hashset_archive.rs @@ -0,0 +1,13 @@ +#![no_main] +#[macro_use] +extern crate libfuzzer_sys; +extern crate grin_core; +extern crate grin_p2p; + +use grin_core::ser; +use grin_p2p::msg::TxHashSetArchive; + +fuzz_target!(|data: &[u8]| { + let mut d = data.clone(); + let _t: Result = ser::deserialize(&mut d); +}); diff --git a/p2p/fuzz/fuzz_targets/read_tx_hashset_request.rs b/p2p/fuzz/fuzz_targets/read_tx_hashset_request.rs new file mode 100644 index 000000000..07c27823e --- /dev/null +++ b/p2p/fuzz/fuzz_targets/read_tx_hashset_request.rs @@ -0,0 +1,13 @@ +#![no_main] +#[macro_use] +extern crate libfuzzer_sys; +extern crate grin_core; +extern crate grin_p2p; + +use grin_core::ser; +use grin_p2p::msg::TxHashSetRequest; + +fuzz_target!(|data: &[u8]| { + let mut d = data.clone(); + let _t: Result = ser::deserialize(&mut d); +});