public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Robert Obkircher <r.obkircher@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [PATCH proxmox-backup 3/9] datastore: add tests for fixed and dynamic index reader
Date: Fri, 31 Jul 2026 16:21:16 +0200	[thread overview]
Message-ID: <20260731142430.289893-4-r.obkircher@proxmox.com> (raw)
In-Reply-To: <20260731142430.289893-1-r.obkircher@proxmox.com>

Focus on the error paths to make it more visible if they change during
refactoring.

Signed-off-by: Robert Obkircher <r.obkircher@proxmox.com>
---
 pbs-datastore/src/dynamic_index.rs | 77 ++++++++++++++++++++++++++
 pbs-datastore/src/fixed_index.rs   | 86 ++++++++++++++++++++++++++++++
 2 files changed, 163 insertions(+)

diff --git a/pbs-datastore/src/dynamic_index.rs b/pbs-datastore/src/dynamic_index.rs
index a0d9369a5..7ee8bdcab 100644
--- a/pbs-datastore/src/dynamic_index.rs
+++ b/pbs-datastore/src/dynamic_index.rs
@@ -586,3 +586,80 @@ impl<R: ReadChunk> ReadAt for LocalDynamicReadAt<R> {
         panic!("LocalDynamicReadAt::start_read_at returned Pending");
     }
 }
+
+#[cfg(test)]
+mod tests {
+    use std::fs;
+
+    use openssl::sha::sha256;
+    use tempfile::TempDir;
+
+    use super::*;
+
+    #[track_caller]
+    fn check_error_contains<T>(result: Result<T, Error>, substring: &str) {
+        let error = result.map(|_| ()).unwrap_err().to_string();
+        assert!(
+            error.contains(substring),
+            "'{error}' does not contain '{substring}'"
+        );
+    }
+
+    #[test]
+    fn test_index_reader() {
+        let dir = TempDir::new().unwrap();
+
+        check_error_contains(
+            DynamicIndexReader::open(Path::new("/dev/stdin")),
+            "Illegal seek",
+        );
+        check_error_contains(
+            DynamicIndexReader::open(Path::new("/dev/zero")),
+            "index too small (0)",
+        );
+
+        let path = dir.path().join("file");
+        check_error_contains(DynamicIndexReader::open(&path), "No such file or directory");
+
+        fs::write(&path, []).unwrap();
+        check_error_contains(DynamicIndexReader::open(&path), "index too small (0)");
+
+        let mut data = vec![0; size_of::<DynamicIndexHeader>()];
+
+        fs::write(&path, &data).unwrap();
+        check_error_contains(DynamicIndexReader::open(&path), "got unknown magic number");
+
+        data[..8].copy_from_slice(&file_formats::DYNAMIC_SIZED_CHUNK_INDEX_1_0);
+        fs::write(&path, &data).unwrap();
+        check_error_contains(
+            DynamicIndexReader::open(&path),
+            "mapped length must not be zero",
+        );
+
+        data.extend_from_slice(&[0, 1, 2]);
+        fs::write(&path, &data).unwrap();
+        check_error_contains(DynamicIndexReader::open(&path), "got unexpected file size");
+
+        data.extend(3u8..80);
+        fs::write(&path, &data).unwrap();
+        let reader = DynamicIndexReader::open(&path).unwrap();
+        let size = u64::from_le_bytes(std::array::from_fn(|i| i as u8 + 40));
+        assert_eq!(reader.index_bytes(), size);
+        assert_eq!(reader.index_size(), 4096 + 80);
+        assert_eq!(reader.index_count(), 2);
+        assert_eq!(
+            reader.index_digest(0),
+            Some(&std::array::from_fn(|i| i as u8 + 8))
+        );
+        assert_eq!(
+            reader.index_digest(1),
+            Some(&std::array::from_fn(|i| i as u8 + 48))
+        );
+        assert_eq!(
+            reader.compute_csum(),
+            (sha256(&data[size_of::<DynamicIndexHeader>()..]), size,)
+        );
+
+        dir.close().unwrap();
+    }
+}
diff --git a/pbs-datastore/src/fixed_index.rs b/pbs-datastore/src/fixed_index.rs
index 2324fc3e0..8104cbe72 100644
--- a/pbs-datastore/src/fixed_index.rs
+++ b/pbs-datastore/src/fixed_index.rs
@@ -585,12 +585,98 @@ impl FixedIndexWriter {
 #[cfg(test)]
 mod tests {
     use std::fs;
+
+    use openssl::sha::sha256;
     use tempfile::TempDir;
 
     use super::*;
 
     const CS: u32 = 4096;
 
+    #[track_caller]
+    fn check_error_contains<T>(result: Result<T, Error>, substring: &str) {
+        let error = result.map(|_| ()).unwrap_err().to_string();
+        assert!(
+            error.contains(substring),
+            "'{error}' does not contain '{substring}'"
+        );
+    }
+
+    #[test]
+    fn test_index_reader() {
+        let dir = TempDir::new().unwrap();
+
+        check_error_contains(
+            FixedIndexReader::open(Path::new("/dev/stdin")),
+            "Illegal seek",
+        );
+        check_error_contains(
+            FixedIndexReader::open(Path::new("/dev/zero")),
+            "index too small (0)",
+        );
+
+        let path = dir.path().join("file");
+        check_error_contains(FixedIndexReader::open(&path), "No such file or directory");
+
+        fs::write(&path, []).unwrap();
+        check_error_contains(FixedIndexReader::open(&path), "index too small (0)");
+
+        let mut data = vec![0; size_of::<FixedIndexHeader>()];
+
+        fs::write(&path, &data).unwrap();
+        check_error_contains(FixedIndexReader::open(&path), "got unknown magic number");
+
+        data[..8].copy_from_slice(&file_formats::FIXED_SIZED_CHUNK_INDEX_1_0);
+        fs::write(&path, &data).unwrap();
+        check_error_contains(
+            FixedIndexReader::open(&path),
+            "got non-power-of-two chunk size: 0",
+        );
+
+        let chunk_size = 4096 * 1024u64;
+        data[72..][..8].copy_from_slice(&chunk_size.to_le_bytes());
+        fs::write(&path, &data).unwrap();
+        check_error_contains(FixedIndexReader::open(&path), "invalid index size"); // 0
+
+        let size = chunk_size + 1;
+        data[64..][..8].copy_from_slice(&size.to_le_bytes());
+        data.extend_from_slice(&[0, 1, 2]);
+        fs::write(&path, &data).unwrap();
+        check_error_contains(
+            FixedIndexReader::open(&path),
+            "got unexpected file size (64 != 3)",
+        );
+
+        data.extend(3u8..32);
+        fs::write(&path, &data).unwrap();
+        check_error_contains(
+            FixedIndexReader::open(&path),
+            "got unexpected file size (64 != 32)",
+        );
+
+        data.extend(32u8..64);
+        fs::write(&path, &data).unwrap();
+        let reader = FixedIndexReader::open(&path).unwrap();
+        assert_eq!(reader.index_bytes(), size);
+        assert_eq!(reader.index_size(), size as usize);
+        assert_eq!(reader.chunk_size, chunk_size as usize);
+        assert_eq!(reader.index_count(), 2);
+        assert_eq!(
+            reader.index_digest(0),
+            Some(&std::array::from_fn(|i| i as u8))
+        );
+        assert_eq!(
+            reader.index_digest(1),
+            Some(&std::array::from_fn(|i| i as u8 + 32))
+        );
+        assert_eq!(
+            reader.compute_csum(),
+            (sha256(&data[size_of::<FixedIndexHeader>()..]), size)
+        );
+
+        dir.close().unwrap();
+    }
+
     #[test]
     fn test_empty() {
         let dir = TempDir::new().unwrap();
-- 
2.47.3





  parent reply	other threads:[~2026-07-31 14:25 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-31 14:21 [PATCH proxmox{,-backup} 0/9] support index readers on larger page sizes Robert Obkircher
2026-07-31 14:21 ` [PATCH proxmox 1/9] proxmox-sys: add len and as_non_null method to Mmap Robert Obkircher
2026-07-31 14:21 ` [PATCH proxmox-backup 2/9] datastore: limit scope of mutable reference in FixedIndexWriter::close Robert Obkircher
2026-07-31 14:21 ` Robert Obkircher [this message]
2026-07-31 14:21 ` [PATCH proxmox-backup 4/9] datastore: fix inconsistent implementation of index_size method Robert Obkircher
2026-07-31 14:21 ` [PATCH proxmox-backup 5/9] bin: debug: print referenced backup size for index files Robert Obkircher
2026-07-31 14:21 ` [PATCH proxmox-backup 6/9] datastore: check that index files are regular files Robert Obkircher
2026-07-31 14:21 ` [PATCH proxmox-backup 7/9] datastore: simplify FixedIndexReader::chunk_info Robert Obkircher
2026-07-31 14:21 ` [PATCH proxmox-backup 8/9] datastore: always access dynamic index reader slice through method Robert Obkircher
2026-07-31 14:21 ` [PATCH proxmox-backup 9/9] fix #7244: datastore: always mmap index files from offset 0 Robert Obkircher

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=20260731142430.289893-4-r.obkircher@proxmox.com \
    --to=r.obkircher@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