From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9]) by lore.proxmox.com (Postfix) with ESMTPS id C64461FF15E for ; Mon, 27 Oct 2025 14:25:47 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 34BE15E71; Mon, 27 Oct 2025 14:26:19 +0100 (CET) From: Filip Schauer To: pbs-devel@lists.proxmox.com Date: Mon, 27 Oct 2025 14:24:45 +0100 Message-ID: <20251027132450.101103-4-f.schauer@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20251027132450.101103-1-f.schauer@proxmox.com> References: <20251027132450.101103-1-f.schauer@proxmox.com> MIME-Version: 1.0 X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1761571564698 X-SPAM-LEVEL: Spam detection results: 0 AWL -0.006 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pbs-devel] [PATCH proxmox v5 3/5] compression: add tests for the ZipEncoder X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Proxmox Backup Server development discussion Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: pbs-devel-bounces@lists.proxmox.com Sender: "pbs-devel" Signed-off-by: Filip Schauer --- proxmox-compression/tests/zip.rs | 118 +++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 proxmox-compression/tests/zip.rs diff --git a/proxmox-compression/tests/zip.rs b/proxmox-compression/tests/zip.rs new file mode 100644 index 00000000..db7aa171 --- /dev/null +++ b/proxmox-compression/tests/zip.rs @@ -0,0 +1,118 @@ +use std::io::Cursor; + +use anyhow::{ensure, Result}; +use flate2::{Decompress, FlushDecompress}; +use tokio::test; + +use proxmox_compression::zip::{FileType, ZipEncoder, ZipEntry}; + +fn check_zip_with_one_file( + zip_file: &[u8], + expected_file_name: &str, + expected_file_attributes: u16, + expected_content: Option<&[u8]>, +) -> Result<()> { + ensure!(zip_file.starts_with(b"PK\x03\x04")); + + let general_purpose_flags = &zip_file[6..8]; + let size_compressed = &zip_file[18..22]; + let size_uncompressed = &zip_file[22..26]; + let file_name_len = (zip_file[26] as usize) | ((zip_file[27] as usize) << 8); + let extra_len = zip_file[28] as usize | ((zip_file[29] as usize) << 8); + let file_name = &zip_file[30..30 + file_name_len]; + let mut offset = 30 + file_name_len; + + ensure!(file_name == expected_file_name.as_bytes()); + + offset += extra_len; + + if let Some(expected_content) = expected_content { + let mut decompress = Decompress::new(false); + let mut decompressed = Vec::with_capacity(expected_content.len()); + decompress.decompress_vec( + &zip_file[offset..], + &mut decompressed, + FlushDecompress::Finish, + )?; + + ensure!(decompressed == expected_content); + + offset += decompress.total_in() as usize; + } + + // Optional data descriptor + if &zip_file[offset..offset + 4] == b"PK\x07\x08" { + offset += 4; + + if (general_purpose_flags[0] & 8) != 0 { + offset += 12; + + if size_compressed == b"\xff\xff\xff\xff" && size_uncompressed == b"\xff\xff\xff\xff" { + offset += 8; + } + } + } + + ensure!( + &zip_file[offset..offset + 4] == b"PK\x01\x02", + "Expecting a central directory file header" + ); + + let external_file_attributes = &zip_file[offset + 38..offset + 42]; + let file_attributes = u16::from_le_bytes(external_file_attributes[2..4].try_into()?); + + ensure!(file_attributes == expected_file_attributes); + + Ok(()) +} + +#[test] +async fn test_zip_file() -> Result<()> { + let mut zip_file = Vec::new(); + let mut zip_encoder = ZipEncoder::new(&mut zip_file); + zip_encoder + .add_entry( + ZipEntry::new("foo", 0, 0o755, FileType::Regular), + Some(Cursor::new(b"bar")), + ) + .await?; + zip_encoder.finish().await?; + + check_zip_with_one_file(&zip_file, "foo", 0o100755, Some(b"bar"))?; + + Ok(()) +} + +#[test] +async fn test_zip_symlink() -> Result<()> { + let mut zip_file = Vec::new(); + let mut zip_encoder = ZipEncoder::new(&mut zip_file); + zip_encoder + .add_entry( + ZipEntry::new("link", 0, 0o755, FileType::Symlink), + Some(Cursor::new(b"/dev/null")), + ) + .await?; + zip_encoder.finish().await?; + + check_zip_with_one_file(&zip_file, "link", 0o120755, Some(b"/dev/null"))?; + + Ok(()) +} + +#[test] +async fn test_zip_directory() -> Result<()> { + let mut zip_file = Vec::new(); + let mut zip_encoder = ZipEncoder::new(&mut zip_file); + zip_encoder + .add_entry::<&[u8]>( + ZipEntry::new("directory", 0, 0o755, FileType::Directory), + None, + ) + .await?; + zip_encoder.finish().await?; + + check_zip_with_one_file(&zip_file, "directory/", 0o40755, None)?; + + Ok(()) +} -- 2.47.3 _______________________________________________ pbs-devel mailing list pbs-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel