Skip to content

Commit

Permalink
chore: Write failing testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
matheus23 committed Jan 1, 2024
1 parent d0d29e3 commit a92678f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 19 deletions.
3 changes: 1 addition & 2 deletions car-mirror/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
8 changes: 5 additions & 3 deletions car-mirror/src/test_utils/blockstore_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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())?;
Expand Down
40 changes: 26 additions & 14 deletions car-mirror/src/test_utils/dag_strategy.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down Expand Up @@ -46,20 +50,28 @@ pub fn links_to_padded_ipld(
padding_bytes: usize,
) -> impl Fn(Vec<Cid>, &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::<u8>());
}
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)
}
}
Expand Down

0 comments on commit a92678f

Please sign in to comment.