Skip to content

Commit

Permalink
Add UintBE fuzz tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sgwilym committed Jul 12, 2024
1 parent 1de2102 commit 17f5e2d
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 0 deletions.
4 changes: 4 additions & 0 deletions data-model/src/encoding/unsigned_int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::encoding::error::{DecodeError, EncodingConsumerError};
use crate::encoding::parameters::{Decoder, Encoder};

/// A `u8` wrapper that implements [`Encoding`] and [`Decoding`] by encoding as a big-endian fixed-width integer.
#[derive(PartialEq, Eq, PartialOrd, Ord, Debug)]
pub struct U8BE(u8);

impl Encoder for U8BE {
Expand Down Expand Up @@ -46,6 +47,7 @@ impl From<U8BE> for u64 {
}

/// A `u16` wrapper that implements [`Encoding`] and [`Decoding`] by encoding as a big-endian fixed-width integer.
#[derive(PartialEq, Eq, PartialOrd, Ord, Debug)]
pub struct U16BE(u16);

impl Encoder for U16BE {
Expand Down Expand Up @@ -87,6 +89,7 @@ impl From<U16BE> for u64 {
}

/// A `u32` wrapper that implements [`Encoding`] and [`Decoding`] by encoding as a big-endian fixed-width integer.
#[derive(PartialEq, Eq, PartialOrd, Ord, Debug)]
pub struct U32BE(u32);

impl Encoder for U32BE {
Expand Down Expand Up @@ -128,6 +131,7 @@ impl From<U32BE> for u64 {
}

/// A `u64` wrapper that implements [`Encoding`] and [`Decoding`] by encoding as a big-endian fixed-width integer.
#[derive(PartialEq, Eq, PartialOrd, Ord, Debug)]
pub struct U64BE(u64);

impl Encoder for U64BE {
Expand Down
56 changes: 56 additions & 0 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,59 @@ path = "fuzz_targets/entry_encoding_random.rs"
test = false
doc = false
bench = false

[[bin]]
name = "u8be_encoding"
path = "fuzz_targets/u8be_encoding.rs"
test = false
doc = false
bench = false

[[bin]]
name = "u8be_encoding_random"
path = "fuzz_targets/u8be_encoding_random.rs"
test = false
doc = false
bench = false

[[bin]]
name = "u16be_encoding"
path = "fuzz_targets/u16be_encoding.rs"
test = false
doc = false
bench = false

[[bin]]
name = "u16be_encoding_random"
path = "fuzz_targets/u16be_encoding_random.rs"
test = false
doc = false
bench = false

[[bin]]
name = "u32be_encoding"
path = "fuzz_targets/u32be_encoding.rs"
test = false
doc = false
bench = false

[[bin]]
name = "u32be_encoding_random"
path = "fuzz_targets/u32be_encoding_random.rs"
test = false
doc = false
bench = false

[[bin]]
name = "u64be_encoding"
path = "fuzz_targets/u64be_encoding.rs"
test = false
doc = false
bench = false

[[bin]]
name = "u64be_encoding_random"
path = "fuzz_targets/u64be_encoding_random.rs"
test = false
doc = false
bench = false
14 changes: 14 additions & 0 deletions fuzz/fuzz_targets/u16be_encoding.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#![no_main]

use libfuzzer_sys::fuzz_target;
use ufotofu::local_nb::consumer::TestConsumer;
use willow_data_model::encoding::unsigned_int::U16BE;
use willow_data_model_fuzz::encoding_roundtrip;

fuzz_target!(|data: (u16, TestConsumer<u8, u16, ()>)| {
let (n, mut consumer) = data;

smol::block_on(async {
encoding_roundtrip::<_, TestConsumer<u8, u16, ()>>(U16BE::from(n), &mut consumer).await;
});
});
11 changes: 11 additions & 0 deletions fuzz/fuzz_targets/u16be_encoding_random.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#![no_main]

use libfuzzer_sys::fuzz_target;
use willow_data_model::encoding::unsigned_int::U16BE;
use willow_data_model_fuzz::encoding_random;

fuzz_target!(|data: &[u8]| {
smol::block_on(async {
encoding_random::<U16BE>(data).await;
});
});
14 changes: 14 additions & 0 deletions fuzz/fuzz_targets/u32be_encoding.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#![no_main]

use libfuzzer_sys::fuzz_target;
use ufotofu::local_nb::consumer::TestConsumer;
use willow_data_model::encoding::unsigned_int::U32BE;
use willow_data_model_fuzz::encoding_roundtrip;

fuzz_target!(|data: (u32, TestConsumer<u8, u16, ()>)| {
let (n, mut consumer) = data;

smol::block_on(async {
encoding_roundtrip::<_, TestConsumer<u8, u16, ()>>(U32BE::from(n), &mut consumer).await;
});
});
11 changes: 11 additions & 0 deletions fuzz/fuzz_targets/u32be_encoding_random.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#![no_main]

use libfuzzer_sys::fuzz_target;
use willow_data_model::encoding::unsigned_int::U32BE;
use willow_data_model_fuzz::encoding_random;

fuzz_target!(|data: &[u8]| {
smol::block_on(async {
encoding_random::<U32BE>(data).await;
});
});
14 changes: 14 additions & 0 deletions fuzz/fuzz_targets/u64be_encoding.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#![no_main]

use libfuzzer_sys::fuzz_target;
use ufotofu::local_nb::consumer::TestConsumer;
use willow_data_model::encoding::unsigned_int::U64BE;
use willow_data_model_fuzz::encoding_roundtrip;

fuzz_target!(|data: (u64, TestConsumer<u8, u16, ()>)| {
let (n, mut consumer) = data;

smol::block_on(async {
encoding_roundtrip::<_, TestConsumer<u8, u16, ()>>(U64BE::from(n), &mut consumer).await;
});
});
11 changes: 11 additions & 0 deletions fuzz/fuzz_targets/u64be_encoding_random.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#![no_main]

use libfuzzer_sys::fuzz_target;
use willow_data_model::encoding::unsigned_int::U64BE;
use willow_data_model_fuzz::encoding_random;

fuzz_target!(|data: &[u8]| {
smol::block_on(async {
encoding_random::<U64BE>(data).await;
});
});
14 changes: 14 additions & 0 deletions fuzz/fuzz_targets/u8be_encoding.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#![no_main]

use libfuzzer_sys::fuzz_target;
use ufotofu::local_nb::consumer::TestConsumer;
use willow_data_model::encoding::unsigned_int::U8BE;
use willow_data_model_fuzz::encoding_roundtrip;

fuzz_target!(|data: (u8, TestConsumer<u8, u16, ()>)| {
let (n, mut consumer) = data;

smol::block_on(async {
encoding_roundtrip::<_, TestConsumer<u8, u16, ()>>(U8BE::from(n), &mut consumer).await;
});
});
11 changes: 11 additions & 0 deletions fuzz/fuzz_targets/u8be_encoding_random.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#![no_main]

use libfuzzer_sys::fuzz_target;
use willow_data_model::encoding::unsigned_int::U8BE;
use willow_data_model_fuzz::encoding_random;

fuzz_target!(|data: &[u8]| {
smol::block_on(async {
encoding_random::<U8BE>(data).await;
});
});

0 comments on commit 17f5e2d

Please sign in to comment.