Skip to content

Commit

Permalink
Proof of resumability through manual corruption.
Browse files Browse the repository at this point in the history
  • Loading branch information
Narsil committed Dec 27, 2024
1 parent 7692515 commit d5eb805
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/api/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,7 @@ mod tests {
use rand::{distributions::Alphanumeric, Rng};
use serde_json::{json, Value};
use sha2::{Digest, Sha256};
use std::io::{Seek, SeekFrom, Write};

struct TempDir {
path: PathBuf,
Expand Down Expand Up @@ -801,6 +802,41 @@ mod tests {
val[..],
hex!("b908f2b7227d4d31a2105dfa31095e28d304f9bc938bfaaa57ee2cacf1f62d32")
);

// Here we prove the previous part was correctly resuming by purposefully corrupting the
// file.
let blob = std::fs::canonicalize(&downloaded_path).unwrap();
let mut file = std::fs::OpenOptions::new().write(true).open(&blob).unwrap();
let size = file.metadata().unwrap().len();
// Not random for consistent sha corruption
let truncate: f32 = 0.5;
let new_size = (size as f32 * truncate) as u64;
// Truncating
file.set_len(new_size).unwrap();
// Corrupting by changing a single byte.
file.seek(SeekFrom::Start(new_size - 1)).unwrap();
file.write_all(&[0]).unwrap();

let mut blob_part = blob.clone();
blob_part.set_extension(".part");
std::fs::rename(blob, &blob_part).unwrap();
std::fs::remove_file(&downloaded_path).unwrap();
let content = std::fs::read(&*blob_part).unwrap();
assert_eq!(content.len() as u64, new_size);
let val = Sha256::digest(content);
// We modified the sha.
assert!(
val[..] != hex!("b908f2b7227d4d31a2105dfa31095e28d304f9bc938bfaaa57ee2cacf1f62d32")
);
let new_downloaded_path = api.model(model_id.clone()).download("config.json").unwrap();
let val = Sha256::digest(std::fs::read(&*new_downloaded_path).unwrap());
println!("Sha {val:#x}");
assert_eq!(downloaded_path, new_downloaded_path);
assert_eq!(
val[..],
// Corrupted sha
hex!("32b83c94ee55a8d43d68b03a859975f6789d647342ddeb2326fcd5e0127035b5")
);
}

#[test]
Expand Down
43 changes: 43 additions & 0 deletions src/api/tokio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,49 @@ mod tests {
val[..],
hex!("b908f2b7227d4d31a2105dfa31095e28d304f9bc938bfaaa57ee2cacf1f62d32")
);

// Here we prove the previous part was correctly resuming by purposefully corrupting the
// file.
let blob = std::fs::canonicalize(&downloaded_path).unwrap();
let mut file = std::fs::OpenOptions::new().write(true).open(&blob).unwrap();
let size = file.metadata().unwrap().len();
// Not random for consistent sha corruption
let truncate: f32 = 0.5;
let new_size = (size as f32 * truncate) as u64;
// Truncating
file.set_len(new_size).unwrap();
let total_size = size + size_of::<u64>() as u64;
file.set_len(total_size).unwrap();
file.seek(SeekFrom::Start(size)).unwrap();
file.write_all(&new_size.to_le_bytes()).unwrap();

// Corrupting by changing a single byte.
file.seek(SeekFrom::Start(new_size - 1)).unwrap();
file.write_all(&[0]).unwrap();

let mut blob_part = blob.clone();
blob_part.set_extension(".sync.part");
std::fs::rename(blob, &blob_part).unwrap();
std::fs::remove_file(&downloaded_path).unwrap();
let content = std::fs::read(&*blob_part).unwrap();
assert_eq!(content.len() as u64, total_size);
let val = Sha256::digest(content);
// We modified the sha.
assert!(
val[..] != hex!("b908f2b7227d4d31a2105dfa31095e28d304f9bc938bfaaa57ee2cacf1f62d32")
);
let new_downloaded_path = api
.model(model_id.clone())
.download("config.json")
.await
.unwrap();
let val = Sha256::digest(std::fs::read(&*new_downloaded_path).unwrap());
assert_eq!(downloaded_path, new_downloaded_path);
assert_eq!(
val[..],
// Corrupted sha
hex!("32b83c94ee55a8d43d68b03a859975f6789d647342ddeb2326fcd5e0127035b5")
);
}

#[tokio::test]
Expand Down

0 comments on commit d5eb805

Please sign in to comment.