use crate::csaf_traits::{
CsafTrait, FileHashTrait, HashTrait, ProductIdentificationHelperTrait, ProductTrait, ProductTreeTrait,
};
use crate::validation::ValidationError;
fn create_short_hash_error(
path: &str,
hash_index: usize,
file_hash_index: usize,
hash_length: usize,
) -> ValidationError {
ValidationError {
message: format!("Too short hash found (length: {hash_length}), expected to be >= 64 chars"),
instance_path: format!(
"{path}/product_identification_helper/hashes/{hash_index}/file_hashes/{file_hash_index}/value"
),
}
}
pub fn test_6_3_5_use_of_short_hash(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() {
for (fh_i, file_hash) in hash.get_file_hashes().iter().enumerate() {
let file_hash_len = file_hash.get_hash().len();
if file_hash_len < 64 {
errors.get_or_insert_default().push(create_short_hash_error(
path,
h_i,
fh_i,
file_hash_len,
));
}
}
}
}
});
}
errors.map_or(Ok(()), Err)
}
crate::test_validation::impl_validator!(ValidatorForTest6_3_5, test_6_3_5_use_of_short_hash);
#[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_3_5() {
let case_01 = Err(vec![create_short_hash_error(
"/product_tree/full_product_names/0",
0,
0,
32,
)]);
TESTS_2_0.test_6_3_5.expect(case_01.clone());
TESTS_2_1.test_6_3_5.expect(case_01);
}
}