refactor: move test case over to ipfs-unixfs

This commit is contained in:
Joonas Koivunen 2020-06-10 20:26:57 +03:00
parent 819d8ebc53
commit 0aca44906e
2 changed files with 46 additions and 51 deletions

View File

@ -473,55 +473,4 @@ mod tests {
p.walk(&doc_cid, example_doc.clone()).unwrap_err();
}
}
#[test]
fn walk_dagpb_links() {
// this is one of the links used by conformance tests
let payload = hex::decode(
"12330a2212206aad27d7e2fc815cd15bf679535062565dc927a831547281\
fc0af9e5d7e67c74120b6166726963616e2e747874180812340a221220fd\
36ac5279964db0cba8f7fa45f8c4c44ef5e2ff55da85936a378c96c9c632\
04120c616d6572696361732e747874180812360a2212207564c20415869d\
77a8a40ca68a9158e397dd48bdff1325cdb23c5bcd181acd17120e617573\
7472616c69616e2e7478741808",
)
.unwrap();
let cid = Cid::try_from("QmbrFTo4s6H23W6wmoZKQC2vSogGeQ4dYiceSqJddzrKVa").unwrap();
let decoded = libipld::block::decode_ipld(&cid, &payload).unwrap();
let paths = [
(
"QmbrFTo4s6H23W6wmoZKQC2vSogGeQ4dYiceSqJddzrKVa/african.txt",
"QmVX54jfjB8eRxLVxyQSod6b1FyDh7mR4mQie9j97i2Qk3",
),
(
"QmbrFTo4s6H23W6wmoZKQC2vSogGeQ4dYiceSqJddzrKVa/americas.txt",
"QmfP6D9bRV4FEYDL4EHZtZG58kDwDfnzmyjuyK5d1pvzbM",
),
(
"QmbrFTo4s6H23W6wmoZKQC2vSogGeQ4dYiceSqJddzrKVa/australian.txt",
"QmWEuXAjUGyndgr4MKqMBgzMW36XgPgvitt2jsXgtuc7JE",
),
];
for (path, link) in &paths {
let mut path = IpfsPath::try_from(*path).unwrap();
let target = Cid::try_from(*link).unwrap();
let doc_cid = path.take_root().unwrap();
// the dag-pb are walked by taking the names from the links, which makes for a bit
// ackward map reads and type hassle. the current walking code panicks if the structure
// isn't compatible so hopefully this test will allow us to catch issues with upgrading
// libipld early.
//
// there are still questionable parts, for example like matching links without a name.
match path.walk(&doc_cid, decoded.clone()).unwrap() {
WalkSuccess::Link(_, cid) => assert_eq!(cid, target),
x => panic!("unexpected result from walk: {:?}", x),
}
}
}
}

View File

@ -100,6 +100,7 @@ fn try_convert_cid(nth: usize, link: PBLink<'_>) -> Result<Cid, InvalidCidInLink
}
/// Resolving result type for the successful cases.
#[derive(Debug)]
pub enum MaybeResolved<'needle> {
/// Link was found for the given segment.
Found(Cid),
@ -151,6 +152,17 @@ pub struct ShardedLookup<'needle> {
needle: Cow<'needle, str>,
}
impl fmt::Debug for ShardedLookup<'_> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
fmt,
"ShardedLookup {{ links: {}, needle: {:?} }}",
self.links.len(),
self.needle.as_ref(),
)
}
}
impl<'needle> ShardedLookup<'needle> {
/// Returns the next pending link and an iterator over the rest.
pub fn pending_links(&self) -> (&Cid, impl Iterator<Item = &Cid>) {
@ -537,3 +549,37 @@ impl std::error::Error for LookupError {
}
}
}
#[cfg(test)]
mod tests {
use std::convert::TryFrom;
use cid::Cid;
use hex_literal::hex;
use super::{resolve, MaybeResolved};
#[test]
fn resolve_paths() {
let payload = hex!("12330a2212206aad27d7e2fc815cd15bf679535062565dc927a831547281fc0af9e5d7e67c74120b6166726963616e2e747874180812340a221220fd36ac5279964db0cba8f7fa45f8c4c44ef5e2ff55da85936a378c96c9c63204120c616d6572696361732e747874180812360a2212207564c20415869d77a8a40ca68a9158e397dd48bdff1325cdb23c5bcd181acd17120e6175737472616c69616e2e7478741808");
let segments = [
("african.txt", "QmVX54jfjB8eRxLVxyQSod6b1FyDh7mR4mQie9j97i2Qk3"),
("americas.txt","QmfP6D9bRV4FEYDL4EHZtZG58kDwDfnzmyjuyK5d1pvzbM"),
("australian.txt", "QmWEuXAjUGyndgr4MKqMBgzMW36XgPgvitt2jsXgtuc7JE")
];
let mut cache = None;
for (segment, link) in &segments {
let target = Cid::try_from(*link).unwrap();
let res = resolve(&payload[..], segment, &mut cache);
match res {
Ok(MaybeResolved::Found(cid)) => assert_eq!(cid, target),
x => panic!("{:?}", x),
}
}
}
}