mod support;
use scionic_merkletree::{
build, export, fetch, import, read_file, read_range, record_of, refused_frame, verify_with,
Builder, ChunkRef, Chunking, Error, ExtraData, Hash, Intake, MemoryStore, Refusal, Request,
Responder, Result, Root, Store, Stored, VerifyOptions, BUNDLE_MAGIC, FRAME_CHUNK,
};
use std::collections::HashSet;
use std::io::{self, Read};
use std::panic::AssertUnwindSafe;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use std::thread::ThreadId;
use std::time::{Duration, Instant};
use support::{
bytes, contents, sample_folder, small_chunks, small_options, write, TempDir, TestPeer,
};
#[test]
fn one_file_is_the_same_whatever_number_of_threads_keep_its_chunks() {
let data = bytes("threads-one-file", 100_000);
let mut roots = Vec::new();
let mut stores = Vec::new();
for threads in [1, 2, 8, 0] {
let store = MemoryStore::new();
let mut builder = Builder::new(&store, small_chunks()).threads(threads);
let streamed = builder.file(&mut data.as_slice()).unwrap();
let in_place = builder.file_from_bytes(&data).unwrap();
assert_eq!(streamed, in_place, "{threads} threads");
roots.push(builder.finish(streamed, ExtraData::new()).unwrap());
stores.push(contents(&store));
}
assert!(roots.windows(2).all(|pair| pair[0] == pair[1]));
assert!(stores.windows(2).all(|pair| pair[0] == pair[1]));
}
#[test]
fn a_file_chunked_by_content_is_the_same_with_help() {
let data = bytes("threads-by-content", 9 << 20);
let mut roots = Vec::new();
for threads in [1, 4] {
let store = MemoryStore::new();
let mut builder = Builder::new(&store, Chunking::FastCdcV1).threads(threads);
let streamed = builder.file(&mut data.as_slice()).unwrap();
assert_eq!(streamed, builder.file_from_bytes(&data).unwrap());
assert!(store.count(Stored::Chunk) > 2, "several chunks");
let root = builder.finish(streamed, ExtraData::new()).unwrap();
for workers in [1, 4] {
let options = VerifyOptions::new().workers(workers).rechunk(true);
verify_with(&store, &root, options).unwrap();
}
roots.push(root);
}
assert_eq!(roots[0], roots[1]);
}
#[test]
fn a_folder_is_the_same_when_threads_outnumber_its_files() {
let temp = TempDir::new("threads-folder").unwrap();
let folder = sample_folder(temp.path(), "tree").unwrap();
let mut roots = Vec::new();
let mut stores = Vec::new();
for workers in [1, 2, 64] {
let store = MemoryStore::new();
let root = build(&store, &folder, &small_options().workers(workers)).unwrap();
verify_with(&store, &root, VerifyOptions::new().workers(workers)).unwrap();
roots.push(root);
stores.push(contents(&store));
}
assert!(roots.windows(2).all(|pair| pair[0] == pair[1]));
assert!(stores.windows(2).all(|pair| pair[0] == pair[1]));
}
struct Refusing {
inner: MemoryStore,
refused: Hash,
}
impl Store for Refusing {
fn put(&self, kind: Stored, hash: &Hash, bytes: &[u8]) -> Result<()> {
if kind == Stored::Chunk && *hash == self.refused {
return Err(Error::Invariant {
detail: "this store will not keep that chunk",
});
}
self.inner.put(kind, hash, bytes)
}
fn get(&self, kind: Stored, hash: &Hash) -> Result<Option<Arc<[u8]>>> {
self.inner.get(kind, hash)
}
fn remove(&self, kind: Stored, hash: &Hash) -> Result<()> {
self.inner.remove(kind, hash)
}
}
#[test]
fn a_chunk_that_cannot_be_kept_stops_the_file_whoever_was_keeping_it() {
let data = bytes("threads-refused", 100_000);
let refused = ChunkRef::of(&data[64_000..64_016]).unwrap().hash;
for threads in [1, 8] {
let store = Refusing {
inner: MemoryStore::new(),
refused,
};
let mut builder = Builder::new(&store, small_chunks()).threads(threads);
for attempt in [
builder.file(&mut data.as_slice()),
builder.file_from_bytes(&data),
] {
assert!(
matches!(attempt, Err(Error::Invariant { .. })),
"{threads} threads: {attempt:?}"
);
}
assert_eq!(store.inner.count(Stored::FileList), 0);
}
}
#[test]
fn the_failure_that_is_told_is_the_one_that_happened() {
let temp = TempDir::new("threads-failure").unwrap();
let folder = temp.path().join("tree");
for number in 0..64 {
let data = bytes(&format!("failure-{number}"), 64);
write(folder.join(format!("file-{number:02}")), &data).unwrap();
}
let refused = ChunkRef::of(&bytes("failure-40", 64)[..16]).unwrap().hash;
for workers in [1, 2, 8] {
let store = Refusing {
inner: MemoryStore::new(),
refused,
};
match build(&store, &folder, &small_options().workers(workers)) {
Err(Error::Invariant { detail }) => {
assert_eq!(detail, "this store will not keep that chunk");
}
other => panic!("{workers} workers: {other:?}"),
}
}
}
struct Falling(MemoryStore);
impl Store for Falling {
fn put(&self, kind: Stored, hash: &Hash, bytes: &[u8]) -> Result<()> {
assert!(kind != Stored::Chunk, "this store falls over");
self.0.put(kind, hash, bytes)
}
fn get(&self, kind: Stored, hash: &Hash) -> Result<Option<Arc<[u8]>>> {
self.0.get(kind, hash)
}
fn remove(&self, kind: Stored, hash: &Hash) -> Result<()> {
self.0.remove(kind, hash)
}
}
#[test]
fn a_store_that_falls_over_does_not_leave_a_build_waiting() {
let temp = TempDir::new("threads-falling").unwrap();
let folder = temp.path().join("tree");
for number in 0..400 {
let data = bytes(&format!("falling-{number}"), 16);
write(
folder.join(format!("dir-{}/file-{number:03}", number % 40)),
&data,
)
.unwrap();
}
for workers in [1, 2, 8] {
let folder = folder.clone();
let (tell, hear) = std::sync::mpsc::channel();
std::thread::spawn(move || {
let store = Falling(MemoryStore::new());
let options = small_options().workers(workers);
let outcome = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
build(&store, &folder, &options).map(|_| ())
}));
let _ = tell.send(outcome.is_err());
});
match hear.recv_timeout(std::time::Duration::from_secs(20)) {
Ok(fell) => assert!(fell, "{workers} workers"),
Err(_) => panic!("with {workers} workers the build was left waiting"),
}
}
}
fn long_tree(seed: &str) -> (MemoryStore, Root, Vec<Vec<u8>>) {
let store = MemoryStore::new();
let mut builder = Builder::new(&store, Chunking::FastCdcV1);
let top = builder.file_from_bytes(&bytes(seed, 12 << 20)).unwrap();
let root = builder.finish(top, ExtraData::new()).unwrap();
let mut bundle = Vec::new();
export(&store, &root, &mut bundle).unwrap();
(store, root, frames_of(&bundle))
}
fn frames_of(bundle: &[u8]) -> Vec<Vec<u8>> {
let mut frames = Vec::new();
let mut rest = &bundle[BUNDLE_MAGIC.len()..];
while let Some((length, after)) = rest.split_first_chunk::<4>() {
let (frame, after) = after.split_at(u32::from_be_bytes(*length) as usize);
frames.push(frame.to_vec());
rest = after;
}
frames
}
fn bundle_of(frames: &[Vec<u8>]) -> Vec<u8> {
let mut bundle = BUNDLE_MAGIC.to_vec();
for frame in frames {
bundle.extend_from_slice(&(frame.len() as u32).to_be_bytes());
bundle.extend_from_slice(frame);
}
bundle
}
fn chunk_places(frames: &[Vec<u8>]) -> Vec<usize> {
(0..frames.len())
.filter(|&place| frames[place][0] == FRAME_CHUNK)
.collect()
}
#[test]
fn chunks_taken_in_by_several_threads_are_taken_in_as_by_one() {
let (source, root, frames) = long_tree("threads-intake");
let places = chunk_places(&frames);
assert!(places.len() > 3, "several chunks");
let imported = MemoryStore::new();
import(&imported, &root, &mut bundle_of(&frames).as_slice()).unwrap();
assert_eq!(contents(&imported), contents(&source));
let fetched = MemoryStore::new();
fetch(&fetched, &root, &mut TestPeer::new(&source)).unwrap();
assert_eq!(contents(&fetched), contents(&source));
let mut twice = frames.clone();
twice.insert(places[1] + 1, frames[places[1]].clone());
let store = MemoryStore::new();
import(&store, &root, &mut bundle_of(&twice).as_slice()).unwrap();
assert_eq!(contents(&store), contents(&source));
}
#[test]
fn a_chunk_refused_among_others_is_what_is_told_and_nothing_false_is_kept() {
let (source, root, frames) = long_tree("threads-forged");
let places = chunk_places(&frames);
let mut forged = frames.clone();
let last = forged[places[2]].len() - 1;
forged[places[2]][last] ^= 1;
let store = MemoryStore::new();
let result = import(&store, &root, &mut bundle_of(&forged).as_slice());
assert!(matches!(result, Err(Error::NotWanted { .. })), "{result:?}");
for (kind, hash, kept) in contents(&store) {
let held = source.get(kind, &hash).unwrap();
assert_eq!(held.as_deref(), Some(kept.as_slice()));
}
for &place in &places[..2] {
let hash = Hash::of(&frames[place][1..]);
assert!(store.has(Stored::Chunk, &hash).unwrap());
}
import(&store, &root, &mut bundle_of(&frames).as_slice()).unwrap();
assert_eq!(contents(&store), contents(&source));
}
struct RefusingOnce {
inner: MemoryStore,
refused: Hash,
asked: AtomicBool,
}
impl RefusingOnce {
fn new(refused: Hash) -> Self {
Self {
inner: MemoryStore::new(),
refused,
asked: AtomicBool::new(false),
}
}
}
impl Store for RefusingOnce {
fn put(&self, kind: Stored, hash: &Hash, bytes: &[u8]) -> Result<()> {
if kind == Stored::Chunk
&& *hash == self.refused
&& !self.asked.swap(true, Ordering::Relaxed)
{
return Err(Error::Invariant {
detail: "this store will not keep that chunk yet",
});
}
self.inner.put(kind, hash, bytes)
}
fn get(&self, kind: Stored, hash: &Hash) -> Result<Option<Arc<[u8]>>> {
self.inner.get(kind, hash)
}
fn remove(&self, kind: Stored, hash: &Hash) -> Result<()> {
self.inner.remove(kind, hash)
}
}
#[test]
fn a_chunk_that_a_store_cannot_keep_is_wanted_still() {
let (source, root, frames) = long_tree("threads-refused-once");
let places = chunk_places(&frames);
let refused = Hash::of(&frames[places[1]][1..]);
let not_yet = |result: &Result<_>| {
matches!(
result,
Err(Error::Invariant {
detail: "this store will not keep that chunk yet"
})
)
};
let store = RefusingOnce::new(refused);
let mut intake = Intake::new(&store, root).unwrap();
for frame in &frames[..places[1]] {
intake.accept(frame).unwrap();
}
assert!(not_yet(&intake.accept(&frames[places[1]]).map(|_| ())));
assert!(intake.accept(&frames[places[1]]).unwrap().kept);
for frame in &frames[places[1] + 1..] {
intake.accept(frame).unwrap();
}
intake.finish().unwrap();
assert_eq!(contents(&store.inner), contents(&source));
let store = RefusingOnce::new(refused);
let bundle = bundle_of(&frames);
assert!(not_yet(
&import(&store, &root, &mut bundle.as_slice()).map(|_| ())
));
assert!(!store.inner.has(Stored::Chunk, &refused).unwrap());
import(&store, &root, &mut bundle.as_slice()).unwrap();
assert_eq!(contents(&store.inner), contents(&source));
let store = RefusingOnce::new(refused);
let fetched = fetch(&store, &root, &mut TestPeer::new(&source));
assert!(not_yet(&fetched.map(|_| ())));
fetch(&store, &root, &mut TestPeer::new(&source)).unwrap();
assert_eq!(contents(&store.inner), contents(&source));
}
#[test]
fn a_store_that_falls_over_while_it_keeps_a_chunk_leaves_the_intake_done() {
let (_source, root, frames) = long_tree("threads-falling-intake");
let places = chunk_places(&frames);
let store = Falling(MemoryStore::new());
let mut intake = Intake::new(&store, root).unwrap();
for frame in &frames[..places[0]] {
intake.accept(frame).unwrap();
}
let fell = std::panic::catch_unwind(AssertUnwindSafe(|| intake.accept(&frames[places[0]])));
assert!(fell.is_err());
let again = intake.accept(&frames[places[0]]);
assert!(matches!(again, Err(Error::IntakeFailed)), "{again:?}");
assert!(matches!(intake.finish(), Err(Error::IntakeFailed)));
let bundle = bundle_of(&frames);
let fell = std::panic::catch_unwind(AssertUnwindSafe(|| {
import(&Falling(MemoryStore::new()), &root, &mut bundle.as_slice())
}));
assert!(fell.is_err());
}
struct Slow {
inner: MemoryStore,
unsure: Vec<(Hash, &'static str)>,
}
impl Store for Slow {
fn put(&self, kind: Stored, hash: &Hash, bytes: &[u8]) -> Result<()> {
self.inner.put(kind, hash, bytes)
}
fn get(&self, kind: Stored, hash: &Hash) -> Result<Option<Arc<[u8]>>> {
self.inner.get(kind, hash)
}
fn remove(&self, kind: Stored, hash: &Hash) -> Result<()> {
self.inner.remove(kind, hash)
}
fn size(&self, kind: Stored, hash: &Hash) -> Result<Option<u64>> {
let started = Instant::now();
while started.elapsed() < Duration::from_micros(20) {
std::hint::spin_loop();
}
match self.unsure.iter().find(|(unsure, _)| unsure == hash) {
Some(&(_, detail)) => Err(Error::Invariant { detail }),
None => self.inner.size(kind, hash),
}
}
}
fn many_chunks(seed: u64) -> (MemoryStore, Root, Vec<Vec<u8>>) {
let data = long_bytes(seed, 16 * 1000);
let store = MemoryStore::new();
let mut builder = Builder::new(&store, Chunking::fixed(16).unwrap());
let top = builder.file_from_bytes(&data).unwrap();
let root = builder.finish(top, ExtraData::new()).unwrap();
(store, root, data.chunks(16).map(<[u8]>::to_vec).collect())
}
fn lists_of(source: &MemoryStore) -> MemoryStore {
let store = MemoryStore::new();
for (kind, hash, bytes) in contents(source) {
if kind != Stored::Chunk {
store.put(kind, &hash, &bytes).unwrap();
}
}
store
}
#[test]
fn what_a_store_holds_of_a_long_list_is_found_as_one_thread_finds_it() {
let (source, root, chunks) = many_chunks(11);
let store = Slow {
inner: lists_of(&source),
unsure: Vec::new(),
};
let mut lacking = 0;
for (number, chunk) in chunks.iter().enumerate() {
let hash = Hash::of(chunk);
if number % 3 == 0 {
lacking += 1;
} else if number == 500 {
store.inner.put(Stored::Chunk, &hash, &chunk[1..]).unwrap();
} else {
store.inner.put(Stored::Chunk, &hash, chunk).unwrap();
}
}
let intake = Intake::new(&store, root).unwrap();
assert_eq!(intake.outstanding(), lacking + 1);
fetch(&store, &root, &mut TestPeer::new(&source)).unwrap();
assert_eq!(contents(&store.inner), contents(&source));
}
#[test]
fn the_failure_told_of_a_long_list_is_that_of_its_first_chunk_that_fails() {
let (source, root, chunks) = many_chunks(13);
let store = Slow {
inner: lists_of(&source),
unsure: vec![
(Hash::of(&chunks[900]), "the second chunk it cannot tell of"),
(Hash::of(&chunks[100]), "the first chunk it cannot tell of"),
],
};
let result = Intake::new(&store, root).map(|_| ());
assert!(
matches!(
result,
Err(Error::Invariant {
detail: "the first chunk it cannot tell of"
})
),
"{result:?}"
);
}
#[derive(Default)]
struct Watched {
inner: MemoryStore,
threads: Mutex<HashSet<ThreadId>>,
}
impl Watched {
fn note(&self) {
let thread = std::thread::current().id();
self.threads.lock().unwrap().insert(thread);
}
}
impl Store for Watched {
fn put(&self, kind: Stored, hash: &Hash, bytes: &[u8]) -> Result<()> {
self.note();
self.inner.put(kind, hash, bytes)
}
fn get(&self, kind: Stored, hash: &Hash) -> Result<Option<Arc<[u8]>>> {
self.note();
self.inner.get(kind, hash)
}
fn remove(&self, kind: Stored, hash: &Hash) -> Result<()> {
self.note();
self.inner.remove(kind, hash)
}
fn size(&self, kind: Stored, hash: &Hash) -> Result<Option<u64>> {
self.note();
self.inner.size(kind, hash)
}
}
#[test]
fn a_build_and_a_check_asked_for_one_thread_use_one_thread() {
let data = long_bytes(19, 16 * 1000);
let store = Watched::default();
let mut builder = Builder::new(&store, Chunking::fixed(16).unwrap()).threads(1);
let top = builder.file_from_bytes(&data).unwrap();
let root = builder.finish(top, ExtraData::new()).unwrap();
verify_with(&store, &root, VerifyOptions::new().workers(1)).unwrap();
let here = std::thread::current().id();
assert_eq!(*store.threads.lock().unwrap(), HashSet::from([here]));
}
fn damaged_copy(source: &MemoryStore, hash: &Hash) -> MemoryStore {
let store = MemoryStore::new();
for (kind, held, mut bytes) in contents(source) {
if kind == Stored::Chunk && held == *hash {
bytes[0] ^= 1;
}
store.put(kind, &held, &bytes).unwrap();
}
store
}
#[test]
fn a_bundle_ends_at_the_first_chunk_that_is_damaged_or_missing() {
let (source, root, frames) = long_tree("threads-export");
let places = chunk_places(&frames);
for bad in [places[0], places[2], places[places.len() - 1]] {
let hash = Hash::of(&frames[bad][1..]);
let before = bundle_of(&frames[..bad]);
let damaged = damaged_copy(&source, &hash);
let mut out = Vec::new();
let result = export(&damaged, &root, &mut out);
assert!(matches!(result, Err(Error::Corrupt { .. })), "{result:?}");
assert!(out == before, "damaged chunk at frame {bad}");
let missing = damaged_copy(&source, &hash);
missing.remove(Stored::Chunk, &hash).unwrap();
let mut out = Vec::new();
let result = export(&missing, &root, &mut out);
assert!(matches!(result, Err(Error::Missing { .. })), "{result:?}");
assert!(out == before, "missing chunk at frame {bad}");
}
}
#[test]
fn a_long_file_is_read_back_whole_and_stops_at_a_damaged_chunk() {
let (source, root, frames) = long_tree("threads-read");
let places = chunk_places(&frames);
let data: Vec<u8> = places
.iter()
.flat_map(|&place| frames[place][1..].iter().copied())
.collect();
let file = record_of(&source, &root).unwrap().top();
let mut out = Vec::new();
read_file(&source, &file, &mut out).unwrap();
assert!(out == data);
let (offset, length) = (1_000_003, 7 << 20);
let mut out = Vec::new();
read_range(&source, &file, offset, length, &mut out).unwrap();
assert!(out[..] == data[offset as usize..(offset + length) as usize]);
let damaged = damaged_copy(&source, &Hash::of(&frames[places[2]][1..]));
let mut out = Vec::new();
let result = read_file(&damaged, &file, &mut out);
assert!(matches!(result, Err(Error::Corrupt { .. })), "{result:?}");
let before: usize = places[..2]
.iter()
.map(|&place| frames[place].len() - 1)
.sum();
assert!(out[..] == data[..before]);
}
fn long_bytes(seed: u64, length: usize) -> Vec<u8> {
let mut state = seed | 1;
let mut out = Vec::with_capacity(length + 8);
while out.len() < length {
state ^= state << 13;
state ^= state >> 7;
state ^= state << 17;
out.extend_from_slice(&state.to_le_bytes());
}
out.truncate(length);
out
}
struct Stutter<'a> {
data: &'a [u8],
at: usize,
step: usize,
fails_at: usize,
}
impl<'a> Stutter<'a> {
fn new(data: &'a [u8], step: usize) -> Self {
Self {
data,
at: 0,
step,
fails_at: usize::MAX,
}
}
}
impl Read for Stutter<'_> {
fn read(&mut self, buffer: &mut [u8]) -> io::Result<usize> {
if self.at >= self.fails_at {
return Err(io::Error::other("the disk went away"));
}
let count = buffer.len().min(self.step).min(self.data.len() - self.at);
buffer[..count].copy_from_slice(&self.data[self.at..self.at + count]);
self.at += count;
Ok(count)
}
}
#[test]
fn a_long_file_that_is_read_is_the_file_held_in_memory() {
let data = long_bytes(7, 72 << 20);
for chunking in [Chunking::FastCdcV1, Chunking::fixed(3 << 20).unwrap()] {
let mut first: Option<(scionic_merkletree::Target, _)> = None;
for threads in [1, 3] {
let store = MemoryStore::new();
let mut builder = Builder::new(&store, chunking).threads(threads);
let in_memory = builder.file_from_bytes(&data).unwrap();
for step in [(1 << 20) + 17, 5_000_011] {
let read = builder.file(&mut Stutter::new(&data, step)).unwrap();
assert_eq!(
read, in_memory,
"{chunking:?}, {threads} threads, step {step}"
);
}
let kept = contents(&store);
match &first {
None => first = Some((in_memory, kept)),
Some((target, held)) => {
assert_eq!(&in_memory, target, "{chunking:?}, {threads} threads");
assert!(&kept == held, "{chunking:?}, {threads} threads");
}
}
}
}
}
#[test]
fn a_long_file_that_cannot_be_read_or_kept_whole_has_no_list() {
let data = long_bytes(11, 48 << 20);
for threads in [1, 3] {
let store = MemoryStore::new();
let mut builder = Builder::new(&store, Chunking::FastCdcV1).threads(threads);
let mut reader = Stutter::new(&data, 1 << 20);
reader.fails_at = 40 << 20;
let result = builder.file(&mut reader);
assert!(matches!(result, Err(Error::Stream { .. })), "{result:?}");
assert_eq!(store.count(Stored::FileList), 0);
let chunks = Chunking::FastCdcV1.cut(&data);
let refused = ChunkRef::of(chunks[2]).unwrap().hash;
let store = Refusing {
inner: MemoryStore::new(),
refused,
};
let mut builder = Builder::new(&store, Chunking::FastCdcV1).threads(threads);
let result = builder.file(&mut Stutter::new(&data, 1 << 20));
assert!(matches!(result, Err(Error::Invariant { .. })), "{result:?}");
assert_eq!(store.inner.count(Stored::FileList), 0);
}
}
fn many_chunks_tree() -> (MemoryStore, Root, Vec<Vec<u8>>) {
let store = MemoryStore::new();
let mut builder = Builder::new(&store, Chunking::fixed(64).unwrap());
let top = builder.file_from_bytes(&long_bytes(13, 64 * 4100)).unwrap();
let root = builder.finish(top, ExtraData::new()).unwrap();
let mut bundle = Vec::new();
export(&store, &root, &mut bundle).unwrap();
(store, root, frames_of(&bundle))
}
fn damage(frame: &mut [u8]) {
let last = frame.len() - 1;
frame[last] ^= 1;
}
fn bad_peer(store: &MemoryStore, bad: Option<Hash>, overlong_at: Option<u64>) -> TestPeer<'_> {
TestPeer::tampering(store, move |request, mut answer| {
for frame in &mut answer {
if frame[0] == FRAME_CHUNK && Some(Hash::of(&frame[1..])) == bad {
damage(frame);
}
}
if let Ok(Request::Chunks { start, .. }) = Request::decode(request) {
if Some(start) == overlong_at {
answer.insert(1, vec![FRAME_CHUNK; 4096]);
}
}
answer
})
}
#[test]
fn chunks_taken_in_while_more_come_are_taken_in_as_by_one() {
let (source, root, frames) = many_chunks_tree();
assert!(chunk_places(&frames).len() > 4 * 1024, "several batches");
let imported = MemoryStore::new();
import(&imported, &root, &mut bundle_of(&frames).as_slice()).unwrap();
assert!(contents(&imported) == contents(&source));
let fetched = MemoryStore::new();
fetch(&fetched, &root, &mut TestPeer::new(&source)).unwrap();
assert!(contents(&fetched) == contents(&source));
}
#[test]
fn a_bad_chunk_taken_in_while_more_come_is_what_is_told() {
let (source, root, frames) = many_chunks_tree();
let places = chunk_places(&frames);
for bad in [100, 2_000, places.len() - 1] {
let hash = Hash::of(&frames[places[bad]][1..]);
let mut forged = frames.clone();
damage(&mut forged[places[bad]]);
let imported = MemoryStore::new();
let result = import(&imported, &root, &mut bundle_of(&forged).as_slice());
assert!(
matches!(result, Err(Error::NotWanted { .. })),
"chunk {bad}: {result:?}"
);
let fetched = MemoryStore::new();
let result = fetch(&fetched, &root, &mut bad_peer(&source, Some(hash), None));
assert!(
matches!(result, Err(Error::NotWanted { .. })),
"chunk {bad}: {result:?}"
);
for store in [&imported, &fetched] {
for (kind, hash, kept) in contents(store) {
assert!(source.get(kind, &hash).unwrap().as_deref() == Some(kept.as_slice()));
}
for &place in &places[..bad] {
let hash = Hash::of(&frames[place][1..]);
assert!(store.has(Stored::Chunk, &hash).unwrap(), "chunk {bad}");
}
assert!(!store.has(Stored::Chunk, &hash).unwrap());
}
}
}
#[test]
fn a_failure_after_a_bad_chunk_that_is_being_taken_in_is_not_what_is_told() {
let (source, root, frames) = many_chunks_tree();
let places = chunk_places(&frames);
let bad = Hash::of(&frames[places[100]][1..]);
let mut forged = frames.clone();
damage(&mut forged[places[100]]);
let fails_at = bundle_of(&frames[..places[1_500]]).len();
for (frames, damaged) in [(&forged, true), (&frames, false)] {
let bundle = bundle_of(frames);
let mut reader = Stutter::new(&bundle, 1_000);
reader.fails_at = fails_at;
let result = import(&MemoryStore::new(), &root, &mut reader);
if damaged {
assert!(matches!(result, Err(Error::NotWanted { .. })), "{result:?}");
} else {
assert!(matches!(result, Err(Error::Stream { .. })), "{result:?}");
}
}
for (bad, damaged) in [(Some(bad), true), (None, false)] {
let mut peer = bad_peer(&source, bad, Some(1_472));
let result = fetch(&MemoryStore::new(), &root, &mut peer);
if damaged {
assert!(matches!(result, Err(Error::NotWanted { .. })), "{result:?}");
} else {
assert!(
matches!(result, Err(Error::LimitExceeded { .. })),
"{result:?}"
);
}
}
}
#[test]
fn a_store_that_falls_over_while_more_chunks_come_does_not_leave_a_fetch_waiting() {
let (source, root, frames) = many_chunks_tree();
let bundle = bundle_of(&frames);
let fell = std::panic::catch_unwind(AssertUnwindSafe(|| {
import(&Falling(MemoryStore::new()), &root, &mut bundle.as_slice())
}));
assert!(fell.is_err());
let fell = std::panic::catch_unwind(AssertUnwindSafe(|| {
fetch(
&Falling(MemoryStore::new()),
&root,
&mut TestPeer::new(&source),
)
}));
assert!(fell.is_err());
}
fn chunks_answer(responder: &Responder, file: &Hash, start: u64) -> Vec<Vec<u8>> {
let request = Request::Chunks {
file: *file,
start,
count: 256,
}
.encode();
let mut frames = Vec::new();
responder
.answer(&request, &mut |frame| {
frames.push(frame.to_vec());
Ok(())
})
.unwrap();
frames
}
#[test]
fn a_responder_that_reads_chunks_ahead_answers_as_one_that_does_not() {
let (source, root, frames) = long_tree("threads-responder");
let chunks: Vec<Vec<u8>> = chunk_places(&frames)
.into_iter()
.map(|place| frames[place].clone())
.collect();
assert!(chunks.len() > 3, "several chunks");
let file = record_of(&source, &root).unwrap().top().hash;
let not_held = refused_frame(Refusal::NotHeld);
let missing = damaged_copy(&source, &Hash::of(&chunks[2][1..]));
missing
.remove(Stored::Chunk, &Hash::of(&chunks[2][1..]))
.unwrap();
let third = &chunks[2][1..];
let shortened = damaged_copy(&source, &Hash::of(third));
shortened.remove(Stored::Chunk, &Hash::of(third)).unwrap();
shortened
.put(Stored::Chunk, &Hash::of(third), &third[..third.len() - 1])
.unwrap();
let mut cut_short = chunks[..2].to_vec();
cut_short.push(not_held.clone());
for threads in [1, 2, 0] {
let responder = Responder::new(&source).threads(threads);
assert!(
chunks_answer(&responder, &file, 0) == chunks,
"{threads} threads"
);
assert!(
chunks_answer(&responder, &file, 1) == chunks[1..],
"{threads} threads"
);
let responder = Responder::new(&missing).threads(threads);
assert!(
chunks_answer(&responder, &file, 0) == cut_short,
"{threads} threads"
);
assert!(
chunks_answer(&responder, &file, 2) == [not_held.clone()],
"{threads} threads"
);
let responder = Responder::new(&shortened).threads(threads);
assert!(
chunks_answer(&responder, &file, 0) == cut_short,
"{threads} threads"
);
}
let responder = Responder::new(&source);
std::thread::scope(|scope| {
for start in 0..4 {
let (responder, chunks) = (&responder, &chunks);
scope.spawn(move || {
for _ in 0..3 {
let answer = chunks_answer(responder, &file, start);
assert!(answer == chunks[start as usize..], "from chunk {start}");
}
});
}
});
}
struct ReadingInto {
inner: MemoryStore,
damaged: Option<Hash>,
lost: Option<Hash>,
reads: std::sync::atomic::AtomicUsize,
}
impl ReadingInto {
fn new(inner: MemoryStore) -> Self {
Self {
inner,
damaged: None,
lost: None,
reads: std::sync::atomic::AtomicUsize::new(0),
}
}
}
impl Store for ReadingInto {
fn put(&self, kind: Stored, hash: &Hash, bytes: &[u8]) -> Result<()> {
self.inner.put(kind, hash, bytes)
}
fn get(&self, kind: Stored, hash: &Hash) -> Result<Option<Arc<[u8]>>> {
self.inner.get(kind, hash)
}
fn read_into(&self, kind: Stored, hash: &Hash, into: &mut [u8]) -> Result<Option<u64>> {
self.reads.fetch_add(1, Ordering::Relaxed);
if Some(*hash) == self.lost {
return Ok(None);
}
let Some(bytes) = self.inner.get(kind, hash)? else {
return Ok(None);
};
if bytes.len() == into.len() {
into.copy_from_slice(&bytes);
if Some(*hash) == self.damaged {
into[0] ^= 1;
}
}
Ok(Some(bytes.len() as u64))
}
fn remove(&self, kind: Stored, hash: &Hash) -> Result<()> {
self.inner.remove(kind, hash)
}
}
#[test]
fn what_a_store_reads_into_memory_of_its_own_is_checked_as_what_it_lends() {
let store = MemoryStore::new();
store
.put(Stored::Chunk, &Hash::of(b"held"), b"held")
.unwrap();
let mut into = *b"....";
let length = store.read_into(Stored::Chunk, &Hash::of(b"held"), &mut into);
assert_eq!(length.unwrap(), Some(4));
assert_eq!(&into, b"held");
let mut longer = *b".....";
let length = store.read_into(Stored::Chunk, &Hash::of(b"held"), &mut longer);
assert_eq!(length.unwrap(), Some(4));
assert_eq!(&longer, b".....");
let length = store.read_into(Stored::Chunk, &Hash::of(b"not held"), &mut into);
assert_eq!(length.unwrap(), None);
let (source, root, frames) = long_tree("threads-read-into");
let places = chunk_places(&frames);
let copy = || {
let store = MemoryStore::new();
for (kind, hash, bytes) in contents(&source) {
store.put(kind, &hash, &bytes).unwrap();
}
ReadingInto::new(store)
};
let file = record_of(&source, &root).unwrap().top();
let whole = bundle_of(&frames);
let reading = copy();
let mut out = Vec::new();
export(&reading, &root, &mut out).unwrap();
assert!(out == whole);
assert!(reading.reads.load(Ordering::Relaxed) >= places.len());
let fetched = MemoryStore::new();
fetch(&fetched, &root, &mut Responder::new(&reading)).unwrap();
assert!(contents(&fetched) == contents(&source));
let bad = Hash::of(&frames[places[2]][1..]);
let mut damaged = copy();
damaged.damaged = Some(bad);
let mut out = Vec::new();
let result = export(&damaged, &root, &mut out);
assert!(matches!(result, Err(Error::Corrupt { .. })), "{result:?}");
assert!(out == bundle_of(&frames[..places[2]]));
let result = read_file(&damaged, &file, &mut Vec::new());
assert!(matches!(result, Err(Error::Corrupt { .. })), "{result:?}");
let result = fetch(&MemoryStore::new(), &root, &mut Responder::new(&damaged));
assert!(matches!(result, Err(Error::NotWanted { .. })), "{result:?}");
let mut lost = copy();
lost.lost = Some(bad);
let result = export(&lost, &root, &mut Vec::new());
assert!(matches!(result, Err(Error::Missing { .. })), "{result:?}");
let result = read_file(&lost, &file, &mut Vec::new());
assert!(matches!(result, Err(Error::Missing { .. })), "{result:?}");
let shortened = copy();
let chunk = &frames[places[2]][1..];
shortened.inner.remove(Stored::Chunk, &bad).unwrap();
shortened
.inner
.put(Stored::Chunk, &bad, &chunk[..chunk.len() - 1])
.unwrap();
for store in [&shortened as &dyn Store, &shortened.inner] {
let mut out = Vec::new();
let result = export(store, &root, &mut out);
assert!(matches!(result, Err(Error::Corrupt { .. })), "{result:?}");
assert!(out == bundle_of(&frames[..places[2]]));
let mut out = Vec::new();
let result = read_file(store, &file, &mut out);
assert!(
matches!(result, Err(Error::SizeMismatch { .. })),
"{result:?}"
);
assert!(out.is_empty());
let result = fetch(&MemoryStore::new(), &root, &mut Responder::new(store));
assert!(matches!(result, Err(Error::Peer { .. })), "{result:?}");
}
}