From c36d4c8475942ae70106643cb4879405461d6d20 Mon Sep 17 00:00:00 2001 From: Folkert de Vries Date: Thu, 7 Nov 2024 21:26:45 +0100 Subject: [PATCH] `bzip2recover.rs`: calculate a checksum of the output files --- Cargo.lock | 10 ++++++++++ Cargo.toml | 1 + tests/recover.rs | 28 ++++++++++++++++++++++++++-- 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e740758b2..0589ba9fe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -23,6 +23,7 @@ dependencies = [ name = "c2rust_out" version = "0.0.0" dependencies = [ + "crc32fast", "libbzip2-rs-sys", "libc", "tempfile", @@ -43,6 +44,15 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "crc32fast" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a97769d94ddab943e4510d138150169a2758b5ef3eb191a9ee688de3e23ef7b3" +dependencies = [ + "cfg-if", +] + [[package]] name = "errno" version = "0.3.9" diff --git a/Cargo.toml b/Cargo.toml index 2c26de158..f13b0b1ae 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,3 +33,4 @@ libbzip2-rs-sys = { path = "libbzip2-rs-sys/", default-features = false } [dev-dependencies] tempfile = "3.13.0" +crc32fast = "=1.4.2" diff --git a/tests/recover.rs b/tests/recover.rs index f13d9ac89..afc3fc76d 100644 --- a/tests/recover.rs +++ b/tests/recover.rs @@ -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(); @@ -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] @@ -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); @@ -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]