11use super :: product_helper:: * ;
22use super :: schema:: CommonSecurityAdvisoryFramework ;
3- use crate :: csaf:: validation :: { Validate , ValidationProfile } ;
4- use lazy_static :: lazy_static ;
3+ use crate :: csaf:: helpers :: find_duplicates ;
4+ use crate :: csaf :: validation :: { Test , Validatable , Validate , ValidationProfile } ;
55use std:: collections:: { HashMap , HashSet } ;
66
7- type Test = fn ( & CommonSecurityAdvisoryFramework ) -> Result < ( ) , String > ;
8-
9- // TODO: convert this to code using std::sync::OnceLock
10- lazy_static ! {
11- static ref PROFILES : HashMap <ValidationProfile , Vec <& ' static str >> = HashMap :: from( [
12- (
13- ValidationProfile :: Basic ,
14- Vec :: from( [ "6.1.1" , "6.1.2" , "6.1.34" ] )
15- ) ,
16- ( ValidationProfile :: Extended , Vec :: from( [ "6.1.1" , "6.1.2" ] ) ) ,
17- ( ValidationProfile :: Full , Vec :: from( [ "6.1.1" , "6.1.2" ] ) ) ,
18- ] ) ;
19- static ref TESTS : HashMap <& ' static str , Test > = HashMap :: <& str , Test >:: from( [
20- ( "6.1.1" , test_6_01_01_missing_definition_of_product_id) ,
21- ( "6.1.2" , test_6_01_02_multiple_definition_of_product_id) ,
22- ] as [ ( & str , Test ) ; 2 ] ) ;
23- }
24-
25- impl Validate for CommonSecurityAdvisoryFramework {
26- fn validate_profile ( & self , profile : ValidationProfile ) {
27- println ! ( "Validating document... \n " ) ;
28-
29- println ! ( "Executing Test 6.1.1... " ) ;
30-
31- let _ = match test_6_01_01_missing_definition_of_product_id ( self ) {
32- Ok ( ( ) ) => println ! ( "> Test Success" ) ,
33- Err ( e) => println ! ( "> Error: {}" , e) ,
34- } ;
35-
36- println ! ( "Executing Test 6.1.2... " ) ;
7+ impl Validatable < CommonSecurityAdvisoryFramework > for CommonSecurityAdvisoryFramework {
8+ fn profiles ( & self ) -> HashMap < ValidationProfile , Vec < & str > > {
9+ HashMap :: from ( [
10+ (
11+ ValidationProfile :: Basic ,
12+ Vec :: from ( [ "6.1.1" , "6.1.2" , "6.1.34" ] ) ,
13+ ) ,
14+ ( ValidationProfile :: Extended , Vec :: from ( [ "6.1.1" , "6.1.2" ] ) ) ,
15+ ( ValidationProfile :: Full , Vec :: from ( [ "6.1.1" , "6.1.2" ] ) ) ,
16+ ] )
17+ }
3718
38- let _ = match test_6_01_02_multiple_definition_of_product_id ( self ) {
39- Ok ( ( ) ) => println ! ( "> Test Success" ) ,
40- Err ( e) => println ! ( "> Error: {}" , e) ,
41- } ;
19+ fn tests ( & self ) -> HashMap < & str , Test < CommonSecurityAdvisoryFramework > > {
20+ HashMap :: < & str , Test < CommonSecurityAdvisoryFramework > > :: from ( [
21+ ( "6.1.1" , test_6_01_01_missing_definition_of_product_id) ,
22+ ( "6.1.2" , test_6_01_02_multiple_definition_of_product_id) ,
23+ ( "6.1.34" , test_6_01_34_branches_recursion_depth) ,
24+ ]
25+ as [ ( & str , Test < CommonSecurityAdvisoryFramework > ) ; 3 ] )
4226 }
4327
44- fn validate_by_test ( & self , version : & str ) {
45- todo ! ( )
28+ fn doc ( & self ) -> & CommonSecurityAdvisoryFramework {
29+ self
4630 }
4731}
4832
@@ -62,10 +46,6 @@ pub fn test_6_01_01_missing_definition_of_product_id(
6246 }
6347}
6448
65- pub fn testbla ( doc : & CommonSecurityAdvisoryFramework ) -> Result < ( ) , String > {
66- Ok ( ( ) )
67- }
68-
6949pub fn test_6_01_02_multiple_definition_of_product_id (
7050 doc : & CommonSecurityAdvisoryFramework ,
7151) -> Result < ( ) , String > {
@@ -95,34 +75,17 @@ pub fn test_6_01_34_branches_recursion_depth(
9575 }
9676}
9777
98- fn find_duplicates < T : std:: hash:: Hash + Eq + Clone > ( vec : Vec < T > ) -> Vec < T > {
99- let mut occurrences = HashMap :: new ( ) ;
100- let mut duplicates = Vec :: new ( ) ;
101-
102- for item in vec. iter ( ) {
103- let count = occurrences. entry ( item. clone ( ) ) . or_insert ( 0 ) ;
104- * count += 1 ;
105- }
106-
107- for ( item, count) in occurrences {
108- if count > 1 {
109- duplicates. push ( item) ;
110- }
111- }
112-
113- duplicates
114- }
115-
11678#[ cfg( test) ]
11779mod tests {
118- use crate :: csaf:: csaf2_0:: validation:: test_6_01_02_multiple_definition_of_product_id;
119- use crate :: csaf:: csaf2_0:: {
80+ use crate :: csaf:: csaf2_1:: {
12081 loader:: load_document, validation:: test_6_01_01_missing_definition_of_product_id,
82+ validation:: test_6_01_02_multiple_definition_of_product_id,
83+ validation:: test_6_01_34_branches_recursion_depth,
12184 } ;
12285
12386 #[ test]
12487 fn test_test_6_01_01 ( ) {
125- let doc = load_document ( "../csaf/csaf_2.0 /test/validator/data/mandatory/oasis_csaf_tc-csaf_2_0-2021 -6-1-01-01.json" ) . unwrap ( ) ;
88+ let doc = load_document ( "../csaf/csaf_2.1 /test/validator/data/mandatory/oasis_csaf_tc-csaf_2_1-2024 -6-1-01-01.json" ) . unwrap ( ) ;
12689 assert_eq ! (
12790 test_6_01_01_missing_definition_of_product_id( & doc) ,
12891 Err ( String :: from( "Missing definitions: [ProductIdT(\" CSAFPID-9080700\" ), ProductIdT(\" CSAFPID-9080701\" )]" ) )
@@ -131,12 +94,21 @@ mod tests {
13194
13295 #[ test]
13396 fn test_test_6_01_02 ( ) {
134- let doc = load_document ( "../csaf/csaf_2.0 /test/validator/data/mandatory/oasis_csaf_tc-csaf_2_0-2021 -6-1-02-01.json" ) . unwrap ( ) ;
97+ let doc = load_document ( "../csaf/csaf_2.1 /test/validator/data/mandatory/oasis_csaf_tc-csaf_2_1-2024 -6-1-02-01.json" ) . unwrap ( ) ;
13598 assert_eq ! (
13699 test_6_01_02_multiple_definition_of_product_id( & doc) ,
137100 Err ( String :: from(
138101 "Duplicate definitions: [ProductIdT(\" CSAFPID-9080700\" )]"
139102 ) )
140103 )
141104 }
105+
106+ #[ test]
107+ fn test_test_6_01_34 ( ) {
108+ let doc = load_document ( "../csaf/csaf_2.1/test/validator/data/mandatory/oasis_csaf_tc-csaf_2_1-2024-6-1-34-01.json" ) . unwrap ( ) ;
109+ assert_eq ! (
110+ test_6_01_34_branches_recursion_depth( & doc) ,
111+ Err ( String :: from( "Recursion depth too big: 31" ) )
112+ )
113+ }
142114}
0 commit comments