public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Filip Schauer <f.schauer@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH proxmox v5 3/5] compression: add tests for the ZipEncoder
Date: Mon, 27 Oct 2025 14:24:45 +0100	[thread overview]
Message-ID: <20251027132450.101103-4-f.schauer@proxmox.com> (raw)
In-Reply-To: <20251027132450.101103-1-f.schauer@proxmox.com>

Signed-off-by: Filip Schauer <f.schauer@proxmox.com>
---
 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


  parent reply	other threads:[~2025-10-27 13:25 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-27 13:24 [pbs-devel] [PATCH proxmox{, -backup} v5 0/5] fix #4995: include symlinks in zip file restore Filip Schauer
2025-10-27 13:24 ` [pbs-devel] [PATCH proxmox v5 1/5] compression: zip: add a FileType enum Filip Schauer
2025-10-27 13:24 ` [pbs-devel] [PATCH proxmox v5 2/5] compression: zip: add support for symlinks Filip Schauer
2025-10-27 13:24 ` Filip Schauer [this message]
2025-10-27 13:24 ` [pbs-devel] [PATCH proxmox-backup v5 4/5] pxar: Adopt FileType enum when adding a zip entry Filip Schauer
2025-10-27 13:24 ` [pbs-devel] [PATCH proxmox-backup v5 5/5] fix #4995: pxar: Include symlinks in zip file creation Filip Schauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20251027132450.101103-4-f.schauer@proxmox.com \
    --to=f.schauer@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal