diff --git a/Cargo.lock b/Cargo.lock index 0589ba9fe..1e88ea52e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -65,9 +65,9 @@ dependencies = [ [[package]] name = "fastrand" -version = "2.1.1" +version = "2.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e8c02a5121d4ea3eb16a80748c74f5549a5665e4c21333c6098f283870fbdea6" +checksum = "486f806e73c5707928240ddc295403b1b93c96a02038563881c4a2fd84b81ac4" [[package]] name = "libbzip2-rs-sys" @@ -121,9 +121,9 @@ checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" [[package]] name = "tempfile" -version = "3.13.0" +version = "3.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0f2c9fc62d0beef6951ccffd757e241266a2c833136efbe35af6cd2567dca5b" +checksum = "28cce251fcbc87fac86a866eeb0d6c2d536fc16d06f184bb61aeae11aa4cee0c" dependencies = [ "cfg-if", "fastrand", diff --git a/tests/quick.rs b/tests/quick.rs index 2c34bd4a5..241beecee 100644 --- a/tests/quick.rs +++ b/tests/quick.rs @@ -1,17 +1,29 @@ use std::env; +use std::path::Path; use std::process::{Command, Stdio}; -fn run_test(compressed: &str, expected: &[u8]) { - let mut cmd; +/// Useful to test with the C binary +fn bzip2_binary() -> &'static str { + env!("CARGO_BIN_EXE_bzip2") +} + +fn command() -> Command { match env::var("RUNNER") { Ok(runner) if !runner.is_empty() => { let mut runner_args = runner.split(' '); - cmd = Command::new(runner_args.next().unwrap()); + let mut cmd = Command::new(runner_args.next().unwrap()); cmd.args(runner_args); - cmd.arg(env!("CARGO_BIN_EXE_bzip2")); + cmd.arg(bzip2_binary()); + + cmd } - _ => cmd = Command::new(env!("CARGO_BIN_EXE_bzip2")), + _ => Command::new(bzip2_binary()), } +} + +fn run_test(compressed: &str, expected: &[u8]) { + let mut cmd = command(); + let output = match cmd .arg("-d") .arg(compressed) @@ -54,3 +66,143 @@ fn sample3() { include_bytes!("input/quick/sample3.ref"), ); } + +#[test] +fn test_comp_decomp_sample_ref1() { + let sample = Path::new("tests/input/quick/sample1.ref"); + + for block_size in ["-1", "-2", "-3"] { + let mut cmd = command(); + cmd.arg("--compress") + .arg(block_size) + .arg("--keep") + .arg("--stdout") + .arg(sample) + .stdout(Stdio::piped()); + + let output = cmd.output().unwrap(); + + assert!(output.status.success()); + + let tmpdir = tempfile::tempdir().unwrap(); + + let tempfile_path = tmpdir + .path() + .with_file_name(sample.file_name().unwrap()) + .with_extension("bz2"); + + std::fs::write(&tempfile_path, output.stdout).unwrap(); + + let mut cmd = command(); + cmd.arg("--decompress") + .arg("--stdout") + .arg(tempfile_path) + .stdout(Stdio::piped()); + + let output = cmd.output().unwrap(); + + assert!(output.status.success()); + + let out_hash = crc32fast::hash(&output.stdout); + let ref_file = std::fs::read(sample).unwrap(); + let ref_hash = crc32fast::hash(&ref_file); + + assert_eq!(out_hash, ref_hash); + } +} + +#[test] +fn test_comp_decomp_sample_ref2() { + let sample = Path::new("tests/input/quick/sample2.ref"); + + for block_size in ["-1", "-2", "-3"] { + let mut cmd = command(); + cmd.arg("--compress") + .arg(block_size) + .arg("--keep") + .arg("--stdout") + .arg(sample) + .stdout(Stdio::piped()); + + let output = cmd.output().unwrap(); + + assert!( + output.status.success(), + "{}", + String::from_utf8_lossy(&output.stderr) + ); + + // let tmpdir = tempfile::tempdir().unwrap(); + let tmpdir_path = Path::new("/tmp/foo"); + + let tempfile_path = tmpdir_path + .with_file_name(sample.file_name().unwrap()) + .with_extension("bz2"); + + std::fs::write(&tempfile_path, output.stdout).unwrap(); + + let mut cmd = command(); + cmd.arg("--decompress") + .arg("--stdout") + .arg(tempfile_path) + .stdout(Stdio::piped()); + + let output = cmd.output().unwrap(); + + assert!( + output.status.success(), + "{}", + String::from_utf8_lossy(&output.stderr) + ); + + let out_hash = crc32fast::hash(&output.stdout); + let ref_file = std::fs::read(sample).unwrap(); + let ref_hash = crc32fast::hash(&ref_file); + + assert_eq!(out_hash, ref_hash); + } +} + +#[test] +fn test_comp_decomp_sample_ref3() { + let sample = Path::new("tests/input/quick/sample3.ref"); + + for block_size in ["-1", "-2", "-3"] { + let mut cmd = command(); + cmd.arg("--compress") + .arg(block_size) + .arg("--keep") + .arg("--stdout") + .arg(sample) + .stdout(Stdio::piped()); + + let output = cmd.output().unwrap(); + + assert!(output.status.success()); + + let tmpdir = tempfile::tempdir().unwrap(); + + let tempfile_path = tmpdir + .path() + .with_file_name(sample.file_name().unwrap()) + .with_extension("bz2"); + + std::fs::write(&tempfile_path, output.stdout).unwrap(); + + let mut cmd = command(); + cmd.arg("--decompress") + .arg("--stdout") + .arg(tempfile_path) + .stdout(Stdio::piped()); + + let output = cmd.output().unwrap(); + + assert!(output.status.success()); + + let out_hash = crc32fast::hash(&output.stdout); + let ref_file = std::fs::read(sample).unwrap(); + let ref_hash = crc32fast::hash(&ref_file); + + assert_eq!(out_hash, ref_hash); + } +}