Skip to content

Commit 1f62520

Browse files
committed
Expand Bom test
Signed-off-by: Sebastian Ziebell <[email protected]>
1 parent 2658f3a commit 1f62520

File tree

2 files changed

+85
-202
lines changed

2 files changed

+85
-202
lines changed

cyclonedx-bom/src/models/bom.rs

Lines changed: 84 additions & 201 deletions
Original file line numberDiff line numberDiff line change
@@ -234,11 +234,14 @@ impl Validate for Bom {
234234
validate_urn_uuid,
235235
)
236236
.add_struct_option("metadata", self.metadata.as_ref(), version)
237+
.add_struct_option("components", self.components.as_ref(), version)
238+
.add_struct_option("services", self.services.as_ref(), version)
237239
.add_struct_option(
238240
"external_references",
239241
self.external_references.as_ref(),
240242
version,
241243
)
244+
.add_struct_option("compositions", self.compositions.as_ref(), version)
242245
.add_struct_option("properties", self.properties.as_ref(), version)
243246
.add_struct_option("vulnerabilities", self.vulnerabilities.as_ref(), version);
244247

@@ -687,109 +690,78 @@ mod test {
687690

688691
let actual = bom.validate();
689692

690-
/*
691693
assert_eq!(
692-
actual,
693-
ValidationResult::Failed {
694-
reasons: vec![
695-
FailureReason {
696-
message: "UrnUuid does not match regular expression".to_string(),
697-
context: ValidationContext(vec![ValidationPathComponent::Struct {
698-
struct_name: "Bom".to_string(),
699-
field_name: "serial_number".to_string()
700-
}])
701-
},
702-
FailureReason {
703-
message: "DateTime does not conform to ISO 8601".to_string(),
704-
context: ValidationContext(vec![
705-
ValidationPathComponent::Struct {
706-
struct_name: "Bom".to_string(),
707-
field_name: "metadata".to_string()
708-
},
709-
ValidationPathComponent::Struct {
710-
struct_name: "Metadata".to_string(),
711-
field_name: "timestamp".to_string()
712-
}
713-
])
714-
},
715-
FailureReason {
716-
message: "Unknown classification".to_string(),
717-
context: ValidationContext(vec![
718-
ValidationPathComponent::Struct {
719-
struct_name: "Bom".to_string(),
720-
field_name: "components".to_string()
721-
},
722-
ValidationPathComponent::Array { index: 0 },
723-
ValidationPathComponent::Struct {
724-
struct_name: "Component".to_string(),
725-
field_name: "component_type".to_string()
726-
}
727-
])
728-
},
729-
FailureReason {
730-
message:
731-
"NormalizedString contains invalid characters \\r \\n \\t or \\r\\n"
732-
.to_string(),
733-
context: ValidationContext(vec![
734-
ValidationPathComponent::Struct {
735-
struct_name: "Bom".to_string(),
736-
field_name: "services".to_string()
737-
},
738-
ValidationPathComponent::Array { index: 0 },
739-
ValidationPathComponent::Struct {
740-
struct_name: "Service".to_string(),
741-
field_name: "name".to_string()
742-
}
743-
])
744-
},
745-
FailureReason {
746-
message: "Unknown external reference type".to_string(),
747-
context: ValidationContext(vec![
748-
ValidationPathComponent::Struct {
749-
struct_name: "Bom".to_string(),
750-
field_name: "external_references".to_string()
751-
},
752-
ValidationPathComponent::Array { index: 0 },
753-
ValidationPathComponent::Struct {
754-
struct_name: "ExternalReference".to_string(),
755-
field_name: "external_reference_type".to_string()
756-
}
757-
])
758-
},
759-
FailureReason {
760-
message: "Unknown aggregate type".to_string(),
761-
context: ValidationContext(vec![
762-
ValidationPathComponent::Struct {
763-
struct_name: "Bom".to_string(),
764-
field_name: "compositions".to_string()
765-
},
766-
ValidationPathComponent::Array { index: 0 },
767-
ValidationPathComponent::Struct {
768-
struct_name: "Composition".to_string(),
769-
field_name: "aggregate".to_string()
770-
}
771-
])
772-
},
773-
FailureReason {
774-
message:
775-
"NormalizedString contains invalid characters \\r \\n \\t or \\r\\n"
776-
.to_string(),
777-
context: ValidationContext(vec![
778-
ValidationPathComponent::Struct {
779-
struct_name: "Bom".to_string(),
780-
field_name: "properties".to_string()
781-
},
782-
ValidationPathComponent::Array { index: 0 },
783-
ValidationPathComponent::Struct {
784-
struct_name: "Property".to_string(),
785-
field_name: "value".to_string()
786-
}
787-
])
788-
},
694+
actual.errors(),
695+
Some(
696+
vec![
697+
validation::field("serial_number", "UrnUuid does not match regular expression"),
698+
validation::r#struct(
699+
"metadata",
700+
validation::field(
701+
"timestamp",
702+
"DateTime does not conform to ISO 8601"
703+
)
704+
),
705+
validation::r#struct(
706+
"components",
707+
validation::list(
708+
"inner",
709+
[(
710+
0,
711+
validation::field("component_type", "Unknown classification")
712+
)]
713+
)
714+
),
715+
validation::r#struct(
716+
"services",
717+
validation::list(
718+
"inner",
719+
[(
720+
0,
721+
validation::field(
722+
"name",
723+
"NormalizedString contains invalid characters \\r \\n \\t or \\r\\n"
724+
)
725+
)]
726+
)
727+
),
728+
validation::r#struct(
729+
"external_references",
730+
validation::list(
731+
"inner",
732+
[(
733+
0,
734+
validation::field("external_reference_type", "Unknown external reference type")
735+
)]
736+
)
737+
),
738+
validation::r#struct(
739+
"compositions",
740+
validation::list(
741+
"composition",
742+
[(
743+
0,
744+
validation::field("aggregate", "Unknown aggregate type")
745+
)]
746+
)
747+
),
748+
validation::r#struct(
749+
"properties",
750+
validation::list(
751+
"inner",
752+
[(
753+
0,
754+
validation::field(
755+
"value",
756+
"NormalizedString contains invalid characters \\r \\n \\t or \\r\\n"
757+
)
758+
)]
759+
)
760+
)
789761
]
790-
}
791-
)
792-
*/
762+
.into()
763+
)
764+
);
793765
}
794766

795767
#[test]
@@ -847,109 +819,20 @@ mod test {
847819
}
848820
.validate();
849821

850-
/*
851822
assert_eq!(
852-
validation_result,
853-
ValidationResult::Failed {
854-
reasons: vec![
855-
FailureReason {
856-
message: r#"Bom ref "metadata-component" is not unique"#.to_string(),
857-
context: ValidationContext(vec![
858-
ValidationPathComponent::Struct {
859-
struct_name: "Bom".to_string(),
860-
field_name: "components".to_string()
861-
},
862-
ValidationPathComponent::Array { index: 0 },
863-
ValidationPathComponent::Struct {
864-
struct_name: "Component".to_string(),
865-
field_name: "bom_ref".to_string()
866-
},
867-
])
868-
},
869-
FailureReason {
870-
message: r#"Bom ref "component-component" is not unique"#.to_string(),
871-
context: ValidationContext(vec![
872-
ValidationPathComponent::Struct {
873-
struct_name: "Bom".to_string(),
874-
field_name: "components".to_string()
875-
},
876-
ValidationPathComponent::Array { index: 2 },
877-
ValidationPathComponent::Struct {
878-
struct_name: "Component".to_string(),
879-
field_name: "bom_ref".to_string()
880-
},
881-
])
882-
},
883-
FailureReason {
884-
message: r#"Bom ref "subcomponent-component" is not unique"#.to_string(),
885-
context: ValidationContext(vec![
886-
ValidationPathComponent::Struct {
887-
struct_name: "Bom".to_string(),
888-
field_name: "components".to_string()
889-
},
890-
ValidationPathComponent::Array { index: 3 },
891-
ValidationPathComponent::Struct {
892-
struct_name: "Component".to_string(),
893-
field_name: "components".to_string()
894-
},
895-
ValidationPathComponent::Array { index: 0 },
896-
ValidationPathComponent::Struct {
897-
struct_name: "Component".to_string(),
898-
field_name: "bom_ref".to_string()
899-
},
900-
])
901-
},
902-
FailureReason {
903-
message: r#"Bom ref "service-service" is not unique"#.to_string(),
904-
context: ValidationContext(vec![
905-
ValidationPathComponent::Struct {
906-
struct_name: "Bom".to_string(),
907-
field_name: "services".to_string()
908-
},
909-
ValidationPathComponent::Array { index: 1 },
910-
ValidationPathComponent::Struct {
911-
struct_name: "Service".to_string(),
912-
field_name: "bom_ref".to_string()
913-
},
914-
])
915-
},
916-
FailureReason {
917-
message: r#"Bom ref "subservice-service" is not unique"#.to_string(),
918-
context: ValidationContext(vec![
919-
ValidationPathComponent::Struct {
920-
struct_name: "Bom".to_string(),
921-
field_name: "services".to_string()
922-
},
923-
ValidationPathComponent::Array { index: 2 },
924-
ValidationPathComponent::Struct {
925-
struct_name: "Service".to_string(),
926-
field_name: "services".to_string()
927-
},
928-
ValidationPathComponent::Array { index: 0 },
929-
ValidationPathComponent::Struct {
930-
struct_name: "Service".to_string(),
931-
field_name: "bom_ref".to_string()
932-
},
933-
])
934-
},
935-
FailureReason {
936-
message: r#"Bom ref "component-service" is not unique"#.to_string(),
937-
context: ValidationContext(vec![
938-
ValidationPathComponent::Struct {
939-
struct_name: "Bom".to_string(),
940-
field_name: "services".to_string()
941-
},
942-
ValidationPathComponent::Array { index: 3 },
943-
ValidationPathComponent::Struct {
944-
struct_name: "Service".to_string(),
945-
field_name: "bom_ref".to_string()
946-
},
947-
])
948-
},
823+
validation_result.errors(),
824+
Some(validation::custom(
825+
"bom_ref",
826+
[
827+
r#"Bom ref "metadata-component" is not unique"#,
828+
r#"Bom ref "component-component" is not unique"#,
829+
r#"Bom ref "subcomponent-component" is not unique"#,
830+
r#"Bom ref "service-service" is not unique"#,
831+
r#"Bom ref "subservice-service" is not unique"#,
832+
r#"Bom ref "component-service" is not unique"#,
949833
]
950-
},
834+
)),
951835
);
952-
*/
953836
}
954837

955838
#[test]

cyclonedx-bom/src/models/composition.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub struct Composition {
2929
}
3030

3131
impl Validate for Composition {
32-
fn validate_version(&self, version: SpecVersion) -> ValidationResult {
32+
fn validate_version(&self, _version: SpecVersion) -> ValidationResult {
3333
ValidationContext::new()
3434
.add_field("aggregate", &self.aggregate, validate_aggregate_type)
3535
.into()

0 commit comments

Comments
 (0)