From a92678fee900362d9b3bf50095dd09b41455abc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Kr=C3=BCger?= Date: Mon, 1 Jan 2024 12:22:18 +0100 Subject: [PATCH] chore: Write failing testcase --- car-mirror/src/common.rs | 3 +- car-mirror/src/test_utils/blockstore_utils.rs | 8 ++-- car-mirror/src/test_utils/dag_strategy.rs | 40 ++++++++++++------- 3 files changed, 32 insertions(+), 19 deletions(-) diff --git a/car-mirror/src/common.rs b/car-mirror/src/common.rs index 73c60c5..7f7ab3b 100644 --- a/car-mirror/src/common.rs +++ b/car-mirror/src/common.rs @@ -472,10 +472,9 @@ impl Default for Config { #[cfg(test)] mod tests { - use wnfs_common::MemoryBlockStore; - use super::*; use crate::{test_utils::assert_cond_send_sync, traits::NoCache}; + use wnfs_common::MemoryBlockStore; #[allow(clippy::unreachable, unused)] fn test_assert_send() { diff --git a/car-mirror/src/test_utils/blockstore_utils.rs b/car-mirror/src/test_utils/blockstore_utils.rs index ee34a99..3a47b8b 100644 --- a/car-mirror/src/test_utils/blockstore_utils.rs +++ b/car-mirror/src/test_utils/blockstore_utils.rs @@ -20,8 +20,9 @@ pub async fn setup_existing_blockstore( store: &impl BlockStore, ) -> Result<()> { for (cid, ipld) in blocks.into_iter() { - let block: Bytes = encode(&ipld, IpldCodec::DagCbor)?.into(); - let cid_store = store.put_block(block, IpldCodec::DagCbor.into()).await?; + let codec: IpldCodec = cid.codec().try_into()?; + let block: Bytes = encode(&ipld, codec)?.into(); + let cid_store = store.put_block(block, codec.into()).await?; debug_assert_eq!(cid, cid_store); } @@ -36,7 +37,8 @@ pub fn dag_to_dot( writeln!(writer, "digraph {{")?; for (cid, ipld) in blocks { - let bytes = encode(&ipld, IpldCodec::DagCbor)?; + let codec: IpldCodec = cid.codec().try_into()?; + let bytes = encode(&ipld, codec)?; let refs = references(cid, bytes, Vec::new())?; for to_cid in refs { print_truncated_string(writer, cid.to_string())?; diff --git a/car-mirror/src/test_utils/dag_strategy.rs b/car-mirror/src/test_utils/dag_strategy.rs index b98984e..ca88824 100644 --- a/car-mirror/src/test_utils/dag_strategy.rs +++ b/car-mirror/src/test_utils/dag_strategy.rs @@ -1,7 +1,11 @@ use bytes::Bytes; use libipld::{Cid, Ipld, IpldCodec}; use libipld_core::multihash::{Code, MultihashDigest}; -use proptest::{prelude::Rng, strategy::Strategy, test_runner::TestRng}; +use proptest::{ + prelude::{Rng, RngCore}, + strategy::Strategy, + test_runner::TestRng, +}; use roaring_graphs::{arb_dag, DirectedAcyclicGraph, Vertex}; use std::{ collections::{BTreeMap, HashSet}, @@ -46,20 +50,28 @@ pub fn links_to_padded_ipld( padding_bytes: usize, ) -> impl Fn(Vec, &mut TestRng) -> (Cid, Ipld) + Clone { move |cids, rng| { - let mut padding = Vec::with_capacity(padding_bytes); - for _ in 0..padding_bytes { - padding.push(rng.gen::()); - } + let mut padding = vec![0u8; padding_bytes]; + rng.fill_bytes(&mut padding); + + let codec = match rng.gen_bool(0.5) { + true if cids.is_empty() => IpldCodec::Raw, + _ => IpldCodec::DagCbor, + }; + + let ipld = if cids.is_empty() && codec == IpldCodec::Raw { + Ipld::Bytes(padding) + } else { + Ipld::Map(BTreeMap::from([ + ("data".into(), Ipld::Bytes(padding)), + ( + "links".into(), + Ipld::List(cids.into_iter().map(Ipld::Link).collect()), + ), + ])) + }; - let ipld = Ipld::Map(BTreeMap::from([ - ("data".into(), Ipld::Bytes(padding)), - ( - "links".into(), - Ipld::List(cids.into_iter().map(Ipld::Link).collect()), - ), - ])); - let bytes = encode(&ipld, IpldCodec::DagCbor).unwrap(); - let cid = Cid::new_v1(IpldCodec::DagCbor.into(), Code::Blake3_256.digest(&bytes)); + let bytes = encode(&ipld, codec).unwrap(); + let cid = Cid::new_v1(codec.into(), Code::Blake3_256.digest(&bytes)); (cid, ipld) } }