From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id E463C73734; Fri, 28 May 2021 16:30:10 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id DC114AC68; Fri, 28 May 2021 16:30:10 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id 661A6AC19; Fri, 28 May 2021 16:30:08 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 302FD466D3; Fri, 28 May 2021 16:30:08 +0200 (CEST) From: Fabian Ebner To: pve-devel@lists.proxmox.com, pbs-devel@lists.proxmox.com Date: Fri, 28 May 2021 16:29:44 +0200 Message-Id: <20210528143002.16190-6-f.ebner@proxmox.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210528143002.16190-1-f.ebner@proxmox.com> References: <20210528143002.16190-1-f.ebner@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.002 Adjusted score from AWL reputation of From: address 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 URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [mod.rs, repositories.rs] Subject: [pve-devel] [PATCH v5 proxmox-apt 05/23] add common_digest helper X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 28 May 2021 14:30:11 -0000 which allows library users to detect whether they're still dealing with the same repository configuration or not. Signed-off-by: Fabian Ebner --- 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 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::::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