Skip to content

Commit

Permalink
fuzz: clean up all fuzz targets, remove honggfuzz feature
Browse files Browse the repository at this point in the history
  • Loading branch information
apoelstra committed Jun 15, 2023
1 parent abdd5bb commit 78191ea
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 222 deletions.
7 changes: 1 addition & 6 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,8 @@ publish = false
[package.metadata]
cargo-fuzz = true

[features]
afl_fuzz = ["afl"]
honggfuzz_fuzz = ["honggfuzz"]

[dependencies]
honggfuzz = { version = "0.5", default-features = false, optional = true }
afl = { version = "0.8", optional = true }
honggfuzz = { version = "0.5.55", default-features = false }
regex = { version = "1.4"}
elements-miniscript = { path = "..", features = ["compiler"] }

Expand Down
38 changes: 16 additions & 22 deletions fuzz/fuzz_targets/compile_descriptor.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
extern crate elements_miniscript as miniscript;

use miniscript::Segwitv0;
use miniscript::{policy, Miniscript};
use policy::Liftable;

use std::str::FromStr;

use miniscript::{policy, Miniscript, Segwitv0};
use policy::Liftable;

type Script = Miniscript<String, Segwitv0>;
type Policy = policy::Concrete<String>;

Expand All @@ -15,10 +14,7 @@ fn do_test(data: &[u8]) {
// Compile
if let Ok(desc) = pol.compile::<Segwitv0>() {
// Lift
assert_eq!(
desc.clone().lift().unwrap().sorted(),
pol.clone().lift().unwrap().sorted()
);
assert_eq!(desc.lift().unwrap().sorted(), pol.lift().unwrap().sorted());
// Try to roundtrip the output of the compiler
let output = desc.to_string();
if let Ok(desc) = Script::from_str(&output) {
Expand All @@ -31,23 +27,21 @@ fn do_test(data: &[u8]) {
}
}

#[cfg(feature = "afl")]
extern crate afl;
#[cfg(feature = "afl")]
fn main() {
afl::read_stdio_bytes(|data| {
do_test(&data);
});
}

#[cfg(feature = "honggfuzz")]
#[macro_use]
extern crate honggfuzz;
#[cfg(feature = "honggfuzz")]
fn main() {
loop {
fuzz!(|data| {
honggfuzz::fuzz!(|data| {
do_test(data);
});
}
}

#[cfg(test)]
mod tests {
use miniscript::elements::hex::FromHex;

#[test]
fn duplicate_crash() {
let hex = Vec::<u8>::from_hex("00").unwrap();
super::do_test(&hex);
}
}
42 changes: 7 additions & 35 deletions fuzz/fuzz_targets/parse_descriptor.rs
Original file line number Diff line number Diff line change
@@ -1,60 +1,32 @@
extern crate elements_miniscript as miniscript;

use miniscript::DescriptorPublicKey;
use std::str::FromStr;

use miniscript::DescriptorPublicKey;

fn do_test(data: &[u8]) {
let data_str = String::from_utf8_lossy(data);
if let Ok(dpk) = DescriptorPublicKey::from_str(&data_str) {
let output = dpk.to_string();
let _output = dpk.to_string();
// assert_eq!(data_str.to_lowercase(), output.to_lowercase());
}
}

#[cfg(feature = "afl")]
extern crate afl;
#[cfg(feature = "afl")]
fn main() {
afl::read_stdio_bytes(|data| {
do_test(&data);
});
}

#[cfg(feature = "honggfuzz")]
#[macro_use]
extern crate honggfuzz;
#[cfg(feature = "honggfuzz")]
fn main() {
loop {
fuzz!(|data| {
honggfuzz::fuzz!(|data| {
do_test(data);
});
}
}

#[cfg(test)]
mod tests {
fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) {
let mut b = 0;
for (idx, c) in hex.as_bytes().iter().enumerate() {
b <<= 4;
match *c {
b'A'...b'F' => b |= c - b'A' + 10,
b'a'...b'f' => b |= c - b'a' + 10,
b'0'...b'9' => b |= c - b'0',
_ => panic!("Bad hex"),
}
if (idx & 1) == 1 {
out.push(b);
b = 0;
}
}
}
use miniscript::elements::hex::FromHex;

#[test]
fn duplicate_crash() {
let mut a = Vec::new();
extend_vec_from_hex("6d75736967", &mut a);
super::do_test(&a);
let hex = Vec::<u8>::from_hex("00").unwrap();
super::do_test(&hex);
}
}
29 changes: 14 additions & 15 deletions fuzz/fuzz_targets/parse_descriptor_secret.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
extern crate elements_miniscript as miniscript;

use miniscript::descriptor::DescriptorSecretKey;
use std::str::FromStr;

use miniscript::descriptor::DescriptorSecretKey;

fn do_test(data: &[u8]) {
let data_str = String::from_utf8_lossy(data);
if let Ok(dsk) = DescriptorSecretKey::from_str(&data_str) {
Expand All @@ -11,23 +12,21 @@ fn do_test(data: &[u8]) {
}
}

#[cfg(feature = "afl")]
extern crate afl;
#[cfg(feature = "afl")]
fn main() {
afl::read_stdio_bytes(|data| {
do_test(&data);
});
}

#[cfg(feature = "honggfuzz")]
#[macro_use]
extern crate honggfuzz;
#[cfg(feature = "honggfuzz")]
fn main() {
loop {
fuzz!(|data| {
honggfuzz::fuzz!(|data| {
do_test(data);
});
}
}

#[cfg(test)]
mod tests {
use miniscript::elements::hex::FromHex;

#[test]
fn duplicate_crash() {
let hex = Vec::<u8>::from_hex("00").unwrap();
super::do_test(&hex);
}
}
40 changes: 6 additions & 34 deletions fuzz/fuzz_targets/roundtrip_concrete.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
extern crate elements_miniscript as miniscript;
extern crate regex;
use std::str::FromStr;

use miniscript::policy;
use regex::Regex;
use std::str::FromStr;

type Policy = policy::Concrete<String>;

Expand All @@ -18,50 +19,21 @@ fn do_test(data: &[u8]) {
}
}

#[cfg(feature = "afl")]
extern crate afl;
#[cfg(feature = "afl")]
fn main() {
afl::read_stdio_bytes(|data| {
do_test(&data);
});
}

#[cfg(feature = "honggfuzz")]
#[macro_use]
extern crate honggfuzz;
#[cfg(feature = "honggfuzz")]
fn main() {
loop {
fuzz!(|data| {
honggfuzz::fuzz!(|data| {
do_test(data);
});
}
}

#[cfg(test)]
mod tests {
fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) {
let mut b = 0;
for (idx, c) in hex.as_bytes().iter().enumerate() {
b <<= 4;
match *c {
b'A'...b'F' => b |= c - b'A' + 10,
b'a'...b'f' => b |= c - b'a' + 10,
b'0'...b'9' => b |= c - b'0',
_ => panic!("Bad hex"),
}
if (idx & 1) == 1 {
out.push(b);
b = 0;
}
}
}
use miniscript::elements::hex::FromHex;

#[test]
fn duplicate_crash() {
let mut a = Vec::new();
extend_vec_from_hex("048531e80700ae6400670000af5168", &mut a);
super::do_test(&a);
let hex = Vec::<u8>::from_hex("00").unwrap();
super::do_test(&hex);
}
}
27 changes: 8 additions & 19 deletions fuzz/fuzz_targets/roundtrip_descriptor.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
extern crate elements_miniscript as miniscript;
extern crate regex;

use miniscript::Descriptor;
use regex::Regex;
use std::str::FromStr;

use miniscript::Descriptor;

fn do_test(data: &[u8]) {
// This is how we test in rust-miniscript. It is difficult to enforce wrapping logic in fuzzer
// for alias like t: and_v(1), likely and unlikely.
Expand All @@ -18,32 +18,21 @@ fn do_test(data: &[u8]) {
}
}

#[cfg(feature = "afl")]
extern crate afl;
#[cfg(feature = "afl")]
fn main() {
afl::read_stdio_bytes(|data| {
do_test(&data);
});
}

#[cfg(feature = "honggfuzz")]
#[macro_use]
extern crate honggfuzz;
#[cfg(feature = "honggfuzz")]
fn main() {
loop {
fuzz!(|data| {
honggfuzz::fuzz!(|data| {
do_test(data);
});
}
}

#[cfg(test)]
mod tests {
use super::*;
use miniscript::elements::hex::FromHex;

#[test]
fn test() {
do_test(b"elc:pk_h()");
fn duplicate_crash() {
let hex = Vec::<u8>::from_hex("00").unwrap();
super::do_test(&hex);
}
}
44 changes: 6 additions & 38 deletions fuzz/fuzz_targets/roundtrip_miniscript_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ extern crate elements_miniscript as miniscript;

use miniscript::bitcoin::PublicKey;
use miniscript::elements::script;
use miniscript::CovenantExt;
use miniscript::Miniscript;
use miniscript::NoExt;
use miniscript::Segwitv0;
use miniscript::{Miniscript, NoExt, Segwitv0};

fn do_test(data: &[u8]) {
// Try round-tripping as a script
Expand All @@ -18,50 +15,21 @@ fn do_test(data: &[u8]) {
}
}

#[cfg(feature = "afl")]
extern crate afl;
#[cfg(feature = "afl")]
fn main() {
afl::read_stdio_bytes(|data| {
do_test(&data);
});
}

#[cfg(feature = "honggfuzz")]
#[macro_use]
extern crate honggfuzz;
#[cfg(feature = "honggfuzz")]
fn main() {
loop {
fuzz!(|data| {
honggfuzz::fuzz!(|data| {
do_test(data);
});
}
}

#[cfg(test)]
mod tests {
fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) {
let mut b = 0;
for (idx, c) in hex.as_bytes().iter().enumerate() {
b <<= 4;
match *c {
b'A'...b'F' => b |= c - b'A' + 10,
b'a'...b'f' => b |= c - b'a' + 10,
b'0'...b'9' => b |= c - b'0',
_ => panic!("Bad hex"),
}
if (idx & 1) == 1 {
out.push(b);
b = 0;
}
}
}
use miniscript::elements::hex::FromHex;

#[test]
fn duplicate_crash3() {
let mut a = Vec::new();
extend_vec_from_hex("1479002d00000020323731363342740000004000000000000000000000000000000000000063630004636363639c00000000000000000000", &mut a);
super::do_test(&a);
fn duplicate_crash() {
let hex = Vec::<u8>::from_hex("00").unwrap();
super::do_test(&hex);
}
}
Loading

0 comments on commit 78191ea

Please sign in to comment.