Skip to content

Commit

Permalink
Update encoding fuzz tests to test both encoding function and relation
Browse files Browse the repository at this point in the history
  • Loading branch information
sgwilym committed Sep 11, 2024
1 parent 3e12e41 commit 95b1331
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 42 deletions.
6 changes: 3 additions & 3 deletions fuzz/fuzz_targets/area_rel_area_encoding_random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
use earthstar::identity_id::IdentityIdentifier as IdentityId;
use libfuzzer_sys::fuzz_target;
use willow_data_model::grouping::Area;
use willow_fuzz::encode::relative_encoding_relation_random;
use willow_fuzz::encode::relative_encoding_random;

fuzz_target!(|data: (&[u8], Area<16, 16, 16, IdentityId>)| {
// fuzzed code goes here
let (random_bytes, area) = data;

relative_encoding_relation_random::<Area<16, 16, 16, IdentityId>, Area<16, 16, 16, IdentityId>>(
area,
relative_encoding_random::<Area<16, 16, 16, IdentityId>, Area<16, 16, 16, IdentityId>>(
&area,
random_bytes,
)
});
6 changes: 3 additions & 3 deletions fuzz/fuzz_targets/entry_rel_entry_encoding_random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use earthstar::identity_id::IdentityIdentifier as IdentityId;
use earthstar::namespace_id::NamespaceIdentifier as EsNamespaceId;
use libfuzzer_sys::fuzz_target;
use willow_data_model::Entry;
use willow_fuzz::encode::relative_encoding_relation_random;
use willow_fuzz::encode::relative_encoding_random;
use willow_fuzz::placeholder_params::FakePayloadDigest;

fuzz_target!(|data: (
Expand All @@ -13,8 +13,8 @@ fuzz_target!(|data: (
)| {
let (random_bytes, ref_entry) = data;

relative_encoding_relation_random::<
relative_encoding_random::<
Entry<16, 16, 16, EsNamespaceId, IdentityId, FakePayloadDigest>,
Entry<16, 16, 16, EsNamespaceId, IdentityId, FakePayloadDigest>,
>(ref_entry, random_bytes)
>(&ref_entry, random_bytes)
});
6 changes: 3 additions & 3 deletions fuzz/fuzz_targets/entry_rel_namespace_area_encoding_random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ use earthstar::namespace_id::NamespaceIdentifier as EsNamespaceId;
use libfuzzer_sys::fuzz_target;
use willow_data_model::grouping::Area;
use willow_data_model::Entry;
use willow_fuzz::encode::relative_encoding_relation_random;
use willow_fuzz::encode::relative_encoding_random;
use willow_fuzz::placeholder_params::FakePayloadDigest;

fuzz_target!(
|data: (&[u8], (EsNamespaceId, Area<16, 16, 16, IdentityId>))| {
// fuzzed code goes here
let (random_bytes, namespaced_area) = data;

relative_encoding_relation_random::<
relative_encoding_random::<
(EsNamespaceId, Area<16, 16, 16, IdentityId>),
Entry<16, 16, 16, EsNamespaceId, IdentityId, FakePayloadDigest>,
>(namespaced_area, random_bytes)
>(&namespaced_area, random_bytes)
}
);
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ use earthstar::namespace_id::NamespaceIdentifier as EsNamespaceId;
use libfuzzer_sys::fuzz_target;
use willow_data_model::grouping::Range3d;
use willow_data_model::Entry;
use willow_fuzz::encode::relative_encoding_relation_random;
use willow_fuzz::encode::relative_encoding_random;
use willow_fuzz::placeholder_params::FakePayloadDigest;

fuzz_target!(
|data: (&[u8], (EsNamespaceId, Range3d<16, 16, 16, IdentityId>))| {
// fuzzed code goes here
let (random_bytes, namespaced_range_3d) = data;

relative_encoding_relation_random::<
relative_encoding_random::<
(EsNamespaceId, Range3d<16, 16, 16, IdentityId>),
Entry<16, 16, 16, EsNamespaceId, IdentityId, FakePayloadDigest>,
>(namespaced_range_3d, random_bytes);
>(&namespaced_range_3d, random_bytes);
}
);
6 changes: 3 additions & 3 deletions fuzz/fuzz_targets/mc_capability_rel_area_encoding_random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
use libfuzzer_sys::fuzz_target;
use meadowcap::McCapability;
use willow_data_model::grouping::Area;
use willow_fuzz::encode::relative_encoding_canonical_random;
use willow_fuzz::encode::relative_encoding_random;
use willow_fuzz::silly_sigs::{SillyPublicKey, SillySig};

fuzz_target!(|data: (&[u8], Area<3, 3, 3, SillyPublicKey>,)| {
let (random_bytes, out) = data;

relative_encoding_canonical_random::<
relative_encoding_random::<
Area<3, 3, 3, SillyPublicKey>,
McCapability<3, 3, 3, SillyPublicKey, SillySig, SillyPublicKey, SillySig>,
>(out, random_bytes)
>(&out, random_bytes)
});
7 changes: 2 additions & 5 deletions fuzz/fuzz_targets/path_rel_path_encoding_random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use libfuzzer_sys::fuzz_target;
use willow_data_model::Path;
use willow_fuzz::encode::relative_encoding_canonical_random;
use willow_fuzz::encode::relative_encoding_random;

const MCL: usize = 300;
const MCC: usize = 300;
Expand All @@ -11,8 +11,5 @@ const MPL: usize = 300;
fuzz_target!(|data: (&[u8], Path<MCL, MCC, MPL>)| {
let (random_bytes, ref_path) = data;

relative_encoding_canonical_random::<Path<MCL, MCC, MPL>, Path<MCL, MCC, MPL>>(
ref_path,
random_bytes,
)
relative_encoding_random::<Path<MCL, MCC, MPL>, Path<MCL, MCC, MPL>>(&ref_path, random_bytes)
});
7 changes: 2 additions & 5 deletions fuzz/fuzz_targets/path_rel_path_encoding_random2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use libfuzzer_sys::fuzz_target;
use willow_data_model::Path;
use willow_fuzz::encode::relative_encoding_canonical_random;
use willow_fuzz::encode::relative_encoding_random;

const MCL: usize = 2;
const MCC: usize = 3;
Expand All @@ -11,8 +11,5 @@ const MPL: usize = 3;
fuzz_target!(|data: (&[u8], Path<MCL, MCC, MPL>)| {
let (random_bytes, ref_path) = data;

relative_encoding_canonical_random::<Path<MCL, MCC, MPL>, Path<MCL, MCC, MPL>>(
ref_path,
random_bytes,
)
relative_encoding_random::<Path<MCL, MCC, MPL>, Path<MCL, MCC, MPL>>(&ref_path, random_bytes)
});
7 changes: 2 additions & 5 deletions fuzz/fuzz_targets/path_rel_path_encoding_random3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use libfuzzer_sys::fuzz_target;
use willow_data_model::Path;
use willow_fuzz::encode::relative_encoding_canonical_random;
use willow_fuzz::encode::relative_encoding_random;

const MCL: usize = 4;
const MCC: usize = 4;
Expand All @@ -11,8 +11,5 @@ const MPL: usize = 16;
fuzz_target!(|data: (&[u8], Path<MCL, MCC, MPL>)| {
let (random_bytes, ref_path) = data;

relative_encoding_canonical_random::<Path<MCL, MCC, MPL>, Path<MCL, MCC, MPL>>(
ref_path,
random_bytes,
)
relative_encoding_random::<Path<MCL, MCC, MPL>, Path<MCL, MCC, MPL>>(&ref_path, random_bytes)
});
10 changes: 5 additions & 5 deletions fuzz/fuzz_targets/range3d_rel_rang3d_encoding_random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
use earthstar::identity_id::IdentityIdentifier as IdentityId;
use libfuzzer_sys::fuzz_target;
use willow_data_model::grouping::Range3d;
use willow_fuzz::encode::relative_encoding_relation_random;
use willow_fuzz::encode::relative_encoding_random;

fuzz_target!(|data: (&[u8], Range3d<16, 16, 16, IdentityId>)| {
// fuzzed code goes here
let (random_bytes, area) = data;

relative_encoding_relation_random::<
Range3d<16, 16, 16, IdentityId>,
Range3d<16, 16, 16, IdentityId>,
>(area, random_bytes);
relative_encoding_random::<Range3d<16, 16, 16, IdentityId>, Range3d<16, 16, 16, IdentityId>>(
&area,
random_bytes,
);
});
31 changes: 24 additions & 7 deletions fuzz/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ where
assert_eq!(decoded_item, item);
}

pub fn encoding_random<T>(data: &[u8])
where
T: Encodable + Decodable + std::fmt::Debug + PartialEq + Eq,
{
encoding_canonical_random::<T>(data);
encoding_relation_random::<T>(data);
}

pub fn encoding_canonical_random<T>(data: &[u8])
where
T: Encodable + Decodable + std::fmt::Debug,
Expand Down Expand Up @@ -145,14 +153,23 @@ pub fn relative_encoding_canonical_roundtrip<T, R, C>(
assert_eq!(decoded_item, subject);
}

pub fn relative_encoding_canonical_random<R, T>(reference: R, data: &[u8])
pub fn relative_encoding_random<R, T>(reference: &R, data: &[u8])
where
T: RelativeEncodable<R> + RelativeDecodable<R> + std::fmt::Debug + Eq,
R: std::fmt::Debug,
{
relative_encoding_canonical_random::<R, T>(reference, data);
relative_encoding_relation_random::<R, T>(reference, data);
}

pub fn relative_encoding_canonical_random<R, T>(reference: &R, data: &[u8])
where
T: RelativeEncodable<R> + RelativeDecodable<R> + std::fmt::Debug,
R: std::fmt::Debug,
{
let mut producer = FromSlice::new(data);

match T::relative_decode_canonical(&reference, &mut producer) {
match T::relative_decode_canonical(reference, &mut producer) {
Ok(item) => {
// It decoded to a valid item! Gasp!
// Can we turn it back into the same encoding?
Expand All @@ -161,7 +178,7 @@ where
// println!("item {:?}", item);
// println!("ref {:?}", reference);

item.relative_encode(&reference, &mut consumer).unwrap();
item.relative_encode(reference, &mut consumer).unwrap();

let encoded = consumer.as_ref();

Expand All @@ -180,14 +197,14 @@ where
};
}

pub fn relative_encoding_relation_random<R, T>(reference: R, data: &[u8])
pub fn relative_encoding_relation_random<R, T>(reference: &R, data: &[u8])
where
T: RelativeEncodable<R> + RelativeDecodable<R> + std::fmt::Debug + Eq,
R: std::fmt::Debug,
{
let mut producer = FromSlice::new(data);

match T::relative_decode_relation(&reference, &mut producer) {
match T::relative_decode_relation(reference, &mut producer) {
Ok(item) => {
// It decoded to a valid item! Gasp!
// Can we turn it back into the same encoding?
Expand All @@ -196,11 +213,11 @@ where
// println!("item {:?}", item);
// println!("ref {:?}", reference);

item.relative_encode(&reference, &mut consumer).unwrap();
item.relative_encode(reference, &mut consumer).unwrap();

let mut producer_2 = FromSlice::new(consumer.as_ref());

match T::relative_decode_relation(&reference, &mut producer_2) {
match T::relative_decode_relation(reference, &mut producer_2) {
Ok(decoded_again) => {
assert_eq!(item, decoded_again);
}
Expand Down

0 comments on commit 95b1331

Please sign in to comment.