csaf-rs 0.5.1

A parser for the CSAF standard written in Rust
use crate::csaf::types::csaf_hash_algo::CsafHashAlgorithm;
use crate::csaf_traits::{CsafTrait, HashTrait, ProductIdentificationHelperTrait, ProductTrait, ProductTreeTrait};
use crate::validation::ValidationError;

/// 6.2.9 Use of SHA1 as the only Hash Algorithm
///
/// When hashes are provided as product identification helpers for a product, another hash
/// besides a SHA1 hash must be provided.
pub fn test_6_2_09_use_of_sha1_as_only_hash_algo(doc: &impl CsafTrait) -> Result<(), Vec<ValidationError>> {
    let mut errors: Option<Vec<ValidationError>> = None;

    // For each product and its product identification helpers, check if any hash uses SHA-1 as the only hash algorithm.
    if let Some(tree) = doc.get_product_tree() {
        tree.visit_all_products(&mut |fpn, path| {
            if let Some(helper) = fpn.get_product_identification_helper() {
                for (h_i, hash) in helper.get_hashes().iter().enumerate() {
                    if hash.contains_only_hash_algorithm(CsafHashAlgorithm::Sha1) {
                        errors
                            .get_or_insert_default()
                            .push(create_sha1_only_hash_error(path, h_i));
                    }
                }
            }
        });
    }

    errors.map_or(Ok(()), Err)
}

fn create_sha1_only_hash_error(path: &str, hash_index: usize) -> ValidationError {
    ValidationError {
        message: "Product identification helper uses hashes with `sha1` as the only hash algorithm".to_string(),
        instance_path: format!("{path}/product_identification_helper/hashes/{hash_index}/file_hashes",),
    }
}

crate::test_validation::impl_validator!(ValidatorForTest6_2_9, test_6_2_09_use_of_sha1_as_only_hash_algo);

#[cfg(test)]
mod tests {
    use super::*;
    use crate::csaf2_0::testcases::TESTS_2_0;
    use crate::csaf2_1::testcases::TESTS_2_1;

    #[test]
    fn test_test_6_2_09() {
        let case_01_and_02 = Err(vec![create_sha1_only_hash_error(
            "/product_tree/full_product_names/0",
            0,
        )]);

        // Case 01: one sha1 hash
        // Case 02: two sha1 hashes
        // Case S01: (CSAF 2.0 only) two sha1 hashes with non-default casing
        // Case S11: two file hashes, one with sha1

        TESTS_2_0.test_6_2_9.expect(
            case_01_and_02.clone(),
            case_01_and_02.clone(),
            case_01_and_02.clone(),
            Ok(()),
        );
        TESTS_2_1
            .test_6_2_9
            .expect(case_01_and_02.clone(), case_01_and_02, Ok(()));
    }
}