From 2b78e63c82a7ab826bcc78229729704658314460 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20R=C3=B3=C5=BCa=C5=84ski?= Date: Tue, 20 Aug 2024 11:46:00 +0200 Subject: [PATCH] tests for unpacking --- Cargo.lock | 319 +------------------------------------------------- Cargo.toml | 5 +- src/main.rs | 10 +- src/unpack.rs | 72 +++++++++++- 4 files changed, 73 insertions(+), 333 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0a0ec08..7c70e58 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,23 +17,6 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" -[[package]] -name = "adler2" -version = "2.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" - -[[package]] -name = "aes" -version = "0.8.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b169f7a6d4742236a0a00c541b845991d0ac43e546831af1249753ab4c3aa3a0" -dependencies = [ - "cfg-if", - "cipher", - "cpufeatures", -] - [[package]] name = "ahash" version = "0.8.11" @@ -156,7 +139,7 @@ dependencies = [ "cc", "cfg-if", "libc", - "miniz_oxide 0.7.4", + "miniz_oxide", "object", "rustc-demangle", ] @@ -173,27 +156,12 @@ version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" -[[package]] -name = "block-buffer" -version = "0.10.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" -dependencies = [ - "generic-array", -] - [[package]] name = "bumpalo" version = "3.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" -[[package]] -name = "byteorder" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" - [[package]] name = "bytes" version = "1.7.1" @@ -252,16 +220,6 @@ dependencies = [ "windows-targets", ] -[[package]] -name = "cipher" -version = "0.4.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "773f3b9af64447d2ce9850330c473515014aa235e6a783b02db81ff39e4a3dad" -dependencies = [ - "crypto-common", - "inout", -] - [[package]] name = "clap" version = "4.5.16" @@ -308,12 +266,6 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d3fd119d74b830634cea2a0f58bbd0d54540518a14397557951e79340abc28c0" -[[package]] -name = "constant_time_eq" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f7144d30dcf0fafbce74250a3963025d8d52177934239851c917d29f1df280c2" - [[package]] name = "core-foundation" version = "0.9.4" @@ -330,30 +282,6 @@ version = "0.8.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" -[[package]] -name = "cpufeatures" -version = "0.2.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "51e852e6dc9a5bed1fae92dd2375037bf2b768725bf3be87811edee3249d09ad" -dependencies = [ - "libc", -] - -[[package]] -name = "crc" -version = "3.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "69e6e4d7b33a94f0991c26729976b10ebde1d34c3ee82408fb536164fa10d636" -dependencies = [ - "crc-catalog", -] - -[[package]] -name = "crc-catalog" -version = "2.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "19d374276b40fb8bbdee95aef7c7fa6b5316ec764510eb64b8dd0e2ed0d7e7f5" - [[package]] name = "crc32fast" version = "1.4.2" @@ -369,31 +297,6 @@ version = "0.8.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80" -[[package]] -name = "crypto-common" -version = "0.1.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" -dependencies = [ - "generic-array", - "typenum", -] - -[[package]] -name = "deflate64" -version = "0.1.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da692b8d1080ea3045efaab14434d40468c3d8657e42abddfffca87b428f4c1b" - -[[package]] -name = "deranged" -version = "0.3.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b42b6fa04a440b495c8b04d0e71b707c585f83cb9cb28cf8cd0d976c315e31b4" -dependencies = [ - "powerfmt", -] - [[package]] name = "derive_arbitrary" version = "1.3.2" @@ -405,17 +308,6 @@ dependencies = [ "syn", ] -[[package]] -name = "digest" -version = "0.10.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" -dependencies = [ - "block-buffer", - "crypto-common", - "subtle", -] - [[package]] name = "displaydoc" version = "0.2.5" @@ -476,16 +368,6 @@ version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9fc0510504f03c51ada170672ac806f1f105a88aa97a5281117e1ddc3368e51a" -[[package]] -name = "flate2" -version = "1.0.32" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c0596c1eac1f9e04ed902702e9878208b336edc9d6fddc8a48387349bab3666" -dependencies = [ - "crc32fast", - "miniz_oxide 0.8.0", -] - [[package]] name = "fnv" version = "1.0.7" @@ -578,16 +460,6 @@ dependencies = [ "slab", ] -[[package]] -name = "generic-array" -version = "0.14.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" -dependencies = [ - "typenum", - "version_check", -] - [[package]] name = "getrandom" version = "0.2.15" @@ -654,15 +526,6 @@ version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" -[[package]] -name = "hmac" -version = "0.12.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e" -dependencies = [ - "digest", -] - [[package]] name = "http" version = "1.1.0" @@ -819,15 +682,6 @@ dependencies = [ "hashbrown", ] -[[package]] -name = "inout" -version = "0.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a0c10553d664a4d0bcff9f4215d0aac67a639cc68ef660840afe309b807bc9f5" -dependencies = [ - "generic-array", -] - [[package]] name = "ipnet" version = "2.9.0" @@ -887,28 +741,12 @@ version = "0.4.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89" -[[package]] -name = "lockfree-object-pool" -version = "0.1.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9374ef4228402d4b7e403e5838cb880d9ee663314b0a900d5a6aabf0c213552e" - [[package]] name = "log" version = "0.4.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" -[[package]] -name = "lzma-rs" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "297e814c836ae64db86b36cf2a557ba54368d03f6afcd7d947c266692f71115e" -dependencies = [ - "byteorder", - "crc", -] - [[package]] name = "md5" version = "0.7.0" @@ -936,15 +774,6 @@ dependencies = [ "adler", ] -[[package]] -name = "miniz_oxide" -version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2d80299ef12ff69b16a84bb182e3b9df68b5a91574d3d4fa6e41b65deec4df1" -dependencies = [ - "adler2", -] - [[package]] name = "mio" version = "1.0.2" @@ -974,12 +803,6 @@ dependencies = [ "tempfile", ] -[[package]] -name = "num-conv" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9" - [[package]] name = "num-traits" version = "0.2.19" @@ -1048,16 +871,6 @@ dependencies = [ "vcpkg", ] -[[package]] -name = "pbkdf2" -version = "0.12.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8ed6a7761f76e3b9f92dfb0a60a6a6477c61024b775147ff0973a02653abaf2" -dependencies = [ - "digest", - "hmac", -] - [[package]] name = "percent-encoding" version = "2.3.1" @@ -1102,21 +915,6 @@ version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec" -[[package]] -name = "powerfmt" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" - -[[package]] -name = "ppv-lite86" -version = "0.2.20" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77957b295656769bb8ad2b6a6b09d897d94f05c41b069aede1fcdaa675eaea04" -dependencies = [ - "zerocopy", -] - [[package]] name = "proc-macro2" version = "1.0.86" @@ -1140,6 +938,7 @@ dependencies = [ "rusqlite", "serde", "serde_json", + "tempfile", "url", "zip", "zstd", @@ -1154,36 +953,6 @@ dependencies = [ "proc-macro2", ] -[[package]] -name = "rand" -version = "0.8.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" -dependencies = [ - "libc", - "rand_chacha", - "rand_core", -] - -[[package]] -name = "rand_chacha" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" -dependencies = [ - "ppv-lite86", - "rand_core", -] - -[[package]] -name = "rand_core" -version = "0.6.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" -dependencies = [ - "getrandom", -] - [[package]] name = "regex" version = "1.10.6" @@ -1429,29 +1198,12 @@ dependencies = [ "serde", ] -[[package]] -name = "sha1" -version = "0.10.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e3bf829a2d51ab4a5ddf1352d8470c140cadc8301b2ae1789db023f01cedd6ba" -dependencies = [ - "cfg-if", - "cpufeatures", - "digest", -] - [[package]] name = "shlex" version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" -[[package]] -name = "simd-adler32" -version = "0.3.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d66dc143e6b11c1eddc06d5c423cfc97062865baf299914ab64caa38182078fe" - [[package]] name = "slab" version = "0.4.9" @@ -1569,25 +1321,6 @@ dependencies = [ "syn", ] -[[package]] -name = "time" -version = "0.3.36" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5dfd88e563464686c916c7e46e623e520ddc6d79fa6641390f2e3fa86e83e885" -dependencies = [ - "deranged", - "num-conv", - "powerfmt", - "serde", - "time-core", -] - -[[package]] -name = "time-core" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3" - [[package]] name = "tinyvec" version = "1.8.0" @@ -1704,12 +1437,6 @@ version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" -[[package]] -name = "typenum" -version = "1.17.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" - [[package]] name = "unicode-bidi" version = "0.3.15" @@ -1998,7 +1725,6 @@ version = "0.7.35" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" dependencies = [ - "byteorder", "zerocopy-derive", ] @@ -2018,20 +1744,6 @@ name = "zeroize" version = "1.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ced3678a2879b30306d323f4542626697a464a97c0a07c9aebf7ebca65cd4dde" -dependencies = [ - "zeroize_derive", -] - -[[package]] -name = "zeroize_derive" -version = "1.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ce36e65b0d2999d2aafac989fb249189a141aee1f53c612c1f37d72631959f69" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] [[package]] name = "zip" @@ -2039,41 +1751,14 @@ version = "2.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dc5e4288ea4057ae23afc69a4472434a87a2495cafce6632fd1c4ec9f5cf3494" dependencies = [ - "aes", "arbitrary", "bzip2", - "constant_time_eq", "crc32fast", "crossbeam-utils", - "deflate64", "displaydoc", - "flate2", - "hmac", "indexmap", - "lzma-rs", "memchr", - "pbkdf2", - "rand", - "sha1", "thiserror", - "time", - "zeroize", - "zopfli", - "zstd", -] - -[[package]] -name = "zopfli" -version = "0.8.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e5019f391bac5cf252e93bbcc53d039ffd62c7bfb7c150414d61369afe57e946" -dependencies = [ - "bumpalo", - "crc32fast", - "lockfree-object-pool", - "log", - "once_cell", - "simd-adler32", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 91803ec..673b706 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,5 +17,8 @@ rusqlite = { version = "0.32.1", features = ["bundled"] } serde = { version = "1.0.197", features = ["derive"] } serde_json = "1.0.114" url = "2.5.0" -zip = "2.2.0" +zip = { version = "2.2.0", default-features = false, features = ["bzip2"] } zstd = "0.13.0" + +[dev-dependencies] +tempfile = "3.12.0" diff --git a/src/main.rs b/src/main.rs index 1f922ff..4f54256 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,7 +22,6 @@ use download::download_with_retries; use go_spacemesh::get_version; use parsers::*; use sql::get_last_layer_from_db; -use unpack::{unpack_zip, unpack_zstd}; use utils::*; #[derive(Parser, Debug)] @@ -245,14 +244,7 @@ fn main() -> anyhow::Result<()> { println!("Download URL is not found: skip archive checksum verification"); } - let unpack = if archive_file_path.ends_with(".zip") { - unpack_zip - } else { - unpack_zstd - }; - - // Unzip - match unpack(&archive_file_path, &unpacked_file_path) { + match unpack::unpack(&archive_file_path, &unpacked_file_path) { Ok(_) => { println!("Archive unpacked successfully"); } diff --git a/src/unpack.rs b/src/unpack.rs index 91d5fb3..8b8291c 100644 --- a/src/unpack.rs +++ b/src/unpack.rs @@ -9,6 +9,8 @@ use zstd::stream::read::Decoder; use crate::reader_with_bytes::ReaderWithBytes; use crate::reader_with_progress::ReaderWithProgress; +const DB_FILENAME: &str = "state.sql"; + fn find_file_in_archive<'a>( archive: &'a mut ZipArchive, file_name: &str, @@ -31,7 +33,7 @@ fn find_file_in_archive<'a>( )) } -pub fn unpack_zstd(archive_path: &Path, output_path: &Path) -> Result<()> { +fn unpack_zstd(archive_path: &Path, outpath: &Path) -> Result<()> { let file = File::open(archive_path).context(format!( "Failed to open archive at path: {:?}", archive_path @@ -40,7 +42,6 @@ pub fn unpack_zstd(archive_path: &Path, output_path: &Path) -> Result<()> { let mut decoder = Decoder::new(reader)?; decoder.window_log_max(31)?; - let outpath = Path::new(output_path); if let Some(p) = outpath.parent() { std::fs::create_dir_all(p).context(format!("Failed to create directory at path: {:?}", p))?; } @@ -58,12 +59,11 @@ pub fn unpack_zstd(archive_path: &Path, output_path: &Path) -> Result<()> { Ok(()) } -pub fn unpack_zip(archive_path: &Path, output_path: &Path) -> Result<()> { +fn unpack_zip(archive_path: &Path, outpath: &Path) -> Result<()> { let file = File::open(archive_path)?; let mut zip = ZipArchive::new(file)?; - let state_sql: ZipFile = find_file_in_archive(&mut zip, "state.sql")?; - let outpath = Path::new(output_path); + let state_sql: ZipFile = find_file_in_archive(&mut zip, DB_FILENAME)?; if let Some(p) = outpath.parent() { std::fs::create_dir_all(p)?; @@ -75,7 +75,67 @@ pub fn unpack_zip(archive_path: &Path, output_path: &Path) -> Result<()> { ReaderWithProgress::new(BufReader::with_capacity(1024 * 1024, state_sql), total_size); std::io::copy(&mut reader, &mut outfile)?; - println!("Unzipping... 100%"); Ok(()) } + +pub(crate) fn unpack(archive_path: &Path, output_path: &Path) -> Result<()> { + match archive_path.extension() { + Some(ext) if ext == "zst" => unpack_zstd(archive_path, output_path), + Some(ext) if ext == "zip" => unpack_zip(archive_path, output_path), + _ => Err(anyhow::anyhow!("Unsupported archive format")), + } +} + +#[cfg(test)] +mod tests { + use std::fs::File; + use std::io::{Read, Write}; + + use zip::write::SimpleFileOptions; + use zip::CompressionMethod; + + use super::{unpack, DB_FILENAME}; + + fn test_unpack(ext: &str) { + let tempdir = tempfile::tempdir().unwrap(); + let archive_path = tempdir.path().join(format!("database.{}", ext)); + let archive = File::create(&archive_path).unwrap(); + + match ext { + "zip" => { + let mut zip = zip::ZipWriter::new(&archive); + let options = SimpleFileOptions::default().compression_method(CompressionMethod::Bzip2); + zip.start_file(DB_FILENAME, options).unwrap(); + zip.write_all(b"Hello, World!\n").unwrap(); + zip.finish().unwrap(); + } + "zst" => { + let mut encoder = zstd::stream::write::Encoder::new(archive, 0).unwrap(); + encoder.write_all(b"Hello, World!\n").unwrap(); + encoder.finish().unwrap(); + } + _ => panic!("Unsupported archive format"), + } + + // unpack the archive + let output_filepath = tempdir.path().join(DB_FILENAME); + unpack(&archive_path, &output_filepath).unwrap(); + + // check the output + let mut output_file = File::open(&output_filepath).unwrap(); + let mut output = String::new(); + output_file.read_to_string(&mut output).unwrap(); + assert_eq!(output, "Hello, World!\n"); + } + + #[test] + fn unpack_zst() { + test_unpack("zst"); + } + + #[test] + fn unpack_zip() { + test_unpack("zip"); + } +}