use crate::csaf::types::purl::csaf_purl::CsafPurl;
use crate::csaf_traits::{CsafTrait, ProductIdentificationHelperTrait, ProductTrait, ProductTreeTrait};
use crate::validation::{IntoValidationError, ValidationError};
pub fn test_6_1_13_purl(doc: &impl CsafTrait) -> Result<(), Vec<ValidationError>> {
let mut errors: Option<Vec<ValidationError>> = None;
if let Some(product_tree) = doc.get_product_tree() {
product_tree.visit_all_products(&mut |product, path| {
if let Some(helper) = product.get_product_identification_helper()
&& let Some(purls) = helper.get_purls()
{
for (i_p, purl) in purls.into_iter().enumerate() {
if let CsafPurl::Invalid(e) = purl {
errors
.get_or_insert_default()
.push(e.into_validation_error(&helper.get_purls_json_path(path, i_p)))
}
}
}
});
}
errors.map_or(Ok(()), Err)
}
crate::test_validation::impl_validator!(ValidatorForTest6_1_13, test_6_1_13_purl);
#[cfg(test)]
mod tests {
use super::*;
use crate::csaf::types::purl::{PurlParseError, PurlParseErrorKind};
use crate::csaf2_0::testcases::TESTS_2_0;
use crate::csaf2_1::testcases::TESTS_2_1;
#[test]
fn test_test_6_1_13() {
let case_01_missing_name = |field: &str, idx: &str| -> Result<(), Vec<ValidationError>> {
Err(vec![
PurlParseError::new_for_test("pkg:maven/@1.3.4", PurlParseErrorKind::MissingName)
.into_validation_error(&format!(
"/product_tree/full_product_names/0/product_identification_helper/{field}{idx}"
)),
])
};
let case_02_or_s06_type_prohibits_namespace = |field: &str, idx: &str| -> Result<(), Vec<ValidationError>> {
Err(vec![
PurlParseError::new_for_test(
"pkg:oci/com.example/product-A@sha256%3Add134261219b2",
PurlParseErrorKind::TypeProhibitsNamespace("oci".to_string()),
)
.into_validation_error(&format!(
"/product_tree/full_product_names/0/product_identification_helper/{field}{idx}"
)),
])
};
TESTS_2_0.test_6_1_13.expect(
case_01_missing_name("purl", ""),
case_02_or_s06_type_prohibits_namespace("purl", ""),
Ok(()),
Ok(()),
);
TESTS_2_1.test_6_1_13.expect(
case_01_missing_name("purls", "/0"),
case_02_or_s06_type_prohibits_namespace("purls", "/0"),
Ok(()),
Ok(()),
);
}
}