Skip to content

Commit

Permalink
bzip2recover.rs: calculate a checksum of the output files
Browse files Browse the repository at this point in the history
  • Loading branch information
folkertdev committed Nov 7, 2024
1 parent 256c388 commit a80dc9c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ libbzip2-rs-sys = { path = "libbzip2-rs-sys/", default-features = false }

[dev-dependencies]
tempfile = "3.13.0"
crc32fast = "=1.4.2"
28 changes: 26 additions & 2 deletions tests/recover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ fn run_bzip2recover(path: Option<&Path>) -> std::process::Output {
}
}

fn checksum(path: &Path) -> u32 {
crc32fast::hash(&std::fs::read(path).unwrap())
}

#[test]
fn basic_valid_file() {
let tmp = tempfile::tempdir().unwrap();
Expand Down Expand Up @@ -65,6 +69,15 @@ fn basic_valid_file() {
"bzip2recover: finished\n"
)
);

assert_eq!(
checksum(&tmp.path().join("rec00001sample1.bz2")),
2309536424
);
assert_eq!(
checksum(&tmp.path().join("rec00002sample1.bz2")),
1823861694
);
}

#[test]
Expand All @@ -79,7 +92,7 @@ fn basic_invalid_file() {
.unwrap();

// write some garbage data at the start of the second block
file.write_all_at(&[0xAA, 0xBB, 0xCC], 544936).unwrap();
file.write_all_at(&[0xAA, 0xBB, 0xCC], 544936 + 64).unwrap();

drop(file);

Expand All @@ -97,13 +110,24 @@ fn basic_invalid_file() {
"bzip2recover: searching for block boundaries ...\n",
" block 1 runs from 80 to 544887\n",
" block 2 runs from 544936 to 589771\n",
" block 3 runs from 589820 to 4359512 (incomplete)\n",
" block 3 runs from 589820 to 4360024 (incomplete)\n",
"bzip2recover: splitting into blocks\n",
" writing block 1 to `$TEMPDIR/rec00001sample1.bz2' ...\n",
" writing block 2 to `$TEMPDIR/rec00002sample1.bz2' ...\n",
"bzip2recover: finished\n"
)
);

assert_eq!(
checksum(&tmp.path().join("rec00001sample1.bz2")),
2309536424
);
assert_eq!(
checksum(&tmp.path().join("rec00002sample1.bz2")),
1823861694
);

assert!(!tmp.path().join("rec00003sample1.bz2").exists());
}

#[test]
Expand Down

0 comments on commit a80dc9c

Please sign in to comment.