use crate::csaf::types::csaf_hash_algo::CsafHashAlgorithm;
use crate::csaf_traits::{CsafTrait, HashTrait, ProductIdentificationHelperTrait, ProductTrait, ProductTreeTrait};
use crate::validation::ValidationError;
pub fn test_6_2_08_use_of_md5_as_only_hash_algo(doc: &impl CsafTrait) -> Result<(), Vec<ValidationError>> {
let mut errors: Option<Vec<ValidationError>> = None;
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::Md5) {
errors
.get_or_insert_default()
.push(create_md5_only_hash_error(path, h_i));
}
}
}
});
}
errors.map_or(Ok(()), Err)
}
fn create_md5_only_hash_error(path: &str, hash_index: usize) -> ValidationError {
ValidationError {
message: "Product identification helper uses hashes with `md5` 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_8, test_6_2_08_use_of_md5_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_08() {
let case_01_and_02 = Err(vec![create_md5_only_hash_error(
"/product_tree/full_product_names/0",
0,
)]);
TESTS_2_0.test_6_2_8.expect(
case_01_and_02.clone(),
case_01_and_02.clone(),
case_01_and_02.clone(),
Ok(()),
);
TESTS_2_1
.test_6_2_8
.expect(case_01_and_02.clone(), case_01_and_02, Ok(()));
}
}