[pbs-devel] [PATCH v5 proxmox-apt 05/23] add common_digest helper

Fabian Ebner f.ebner at proxmox.com
Fri May 28 16:29:44 CEST 2021


which allows library users to detect whether they're still dealing with the same
repository configuration or not.

Signed-off-by: Fabian Ebner <f.ebner at proxmox.com>
---

New in v5.

 src/repositories/mod.rs | 24 ++++++++++++++++++++++
 tests/repositories.rs   | 44 +++++++++++++++++++++++++++++++++++++++--
 2 files changed, 66 insertions(+), 2 deletions(-)

diff --git a/src/repositories/mod.rs b/src/repositories/mod.rs
index c2bbc06..2c01011 100644
--- a/src/repositories/mod.rs
+++ b/src/repositories/mod.rs
@@ -1,3 +1,4 @@
+use std::collections::BTreeMap;
 use std::path::PathBuf;
 
 use anyhow::{bail, format_err, Error};
@@ -165,6 +166,29 @@ pub fn check_repositories(files: &[APTRepositoryFile]) -> Vec<APTRepositoryInfo>
     infos
 }
 
+/// Calculates a common digest for successfully parsed repository files.
+///
+/// The digest is invariant with respect to file order.
+///
+/// Files without a digest are ignored.
+pub fn common_digest(files: &[APTRepositoryFile]) -> [u8; 32] {
+    let mut digests = BTreeMap::new();
+
+    for file in files.iter() {
+        digests.insert(file.path.clone(), &file.digest);
+    }
+
+    let mut common_raw = Vec::<u8>::with_capacity(digests.len() * 32);
+    for digest in digests.values() {
+        match digest {
+            Some(digest) => common_raw.extend_from_slice(&digest[..]),
+            None => (),
+        }
+    }
+
+    openssl::sha::sha256(&common_raw[..])
+}
+
 /// Checks if the enterprise repository for the specified Proxmox product is
 /// configured and enabled.
 pub fn enterprise_repository_enabled(files: &[APTRepositoryFile], product: &str) -> bool {
diff --git a/tests/repositories.rs b/tests/repositories.rs
index 9b0cd56..3919077 100644
--- a/tests/repositories.rs
+++ b/tests/repositories.rs
@@ -3,8 +3,8 @@ use std::path::PathBuf;
 use anyhow::{bail, format_err, Error};
 
 use proxmox_apt::repositories::{
-    check_repositories, enterprise_repository_enabled, no_subscription_repository_enabled,
-    write_repositories,
+    check_repositories, common_digest, enterprise_repository_enabled,
+    no_subscription_repository_enabled, write_repositories,
 };
 use proxmox_apt::types::{APTRepositoryFile, APTRepositoryInfo};
 
@@ -252,3 +252,43 @@ fn test_check_repositories() -> Result<(), Error> {
 
     Ok(())
 }
+
+#[test]
+fn test_common_digest() -> Result<(), Error> {
+    let test_dir = std::env::current_dir()?.join("tests");
+    let read_dir = test_dir.join("sources.list.d");
+
+    let pve_list = read_dir.join("pve.list");
+    let mut pve_file = APTRepositoryFile::new(&pve_list)?.unwrap();
+    pve_file.parse()?;
+
+    let standard_sources = read_dir.join("standard.sources");
+    let mut standard_file = APTRepositoryFile::new(&standard_sources)?.unwrap();
+    standard_file.parse()?;
+
+    let expected_digest = [
+        187, 82, 67, 137, 156, 156, 49, 84, 1, 221, 240, 24, 193, 201, 247, 252, 6, 128, 137, 241,
+        169, 176, 78, 193, 4, 190, 117, 136, 96, 28, 46, 78,
+    ];
+
+    let mut files = vec![pve_file, standard_file];
+
+    let digest = common_digest(&files);
+    assert_eq!(digest, expected_digest);
+
+    // order should be irrelevant
+    files.reverse();
+    let digest = common_digest(&files);
+    assert_eq!(digest, expected_digest);
+
+    let digest = common_digest(&files[0..=0]);
+    assert_ne!(digest, expected_digest);
+
+    let mut file = files.first_mut().unwrap();
+    file.digest = Some(expected_digest);
+
+    let digest = common_digest(&files);
+    assert_ne!(digest, expected_digest);
+
+    Ok(())
+}
-- 
2.20.1






More information about the pbs-devel mailing list