Skip to content

Commit 2d1bcad

Browse files
committed
Add missing tests
Signed-off-by: Sebastian Ziebell <[email protected]>
1 parent 9206a9c commit 2d1bcad

File tree

7 files changed

+198
-391
lines changed

7 files changed

+198
-391
lines changed

cyclonedx-bom/src/models/service.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ pub struct DataClassification {
138138

139139
impl Validate for DataClassification {
140140
fn validate_version(&self, _version: SpecVersion) -> ValidationResult {
141-
// TODO implement
142141
ValidationContext::new()
143142
.add_enum("flow", &self.flow, validate_data_flow_type)
144143
.add_enum(

cyclonedx-bom/src/models/tool.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ mod test {
8080

8181
use crate::{
8282
models::tool::{Tool, Tools},
83-
prelude::{NormalizedString, Validate, ValidationResult},
83+
prelude::{NormalizedString, Validate},
8484
validation,
8585
};
8686

cyclonedx-bom/src/models/vulnerability.rs

Lines changed: 129 additions & 256 deletions
Large diffs are not rendered by default.

cyclonedx-bom/src/models/vulnerability_analysis.rs

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,8 @@ impl ToString for ImpactAnalysisResponse {
244244

245245
#[cfg(test)]
246246
mod test {
247+
use crate::validation;
248+
247249
use super::*;
248250
use pretty_assertions::assert_eq;
249251

@@ -278,38 +280,17 @@ mod test {
278280
}
279281
.validate();
280282

281-
/*
282283
assert_eq!(
283284
validation_result,
284-
ValidationResult::Failed {
285-
reasons: vec![
286-
FailureReason {
287-
message: "Undefined impact analysis state".to_string(),
288-
context: ValidationContext(vec![ValidationPathComponent::Struct {
289-
struct_name: "VulnerabilityAnalysis".to_string(),
290-
field_name: "state".to_string()
291-
},])
292-
},
293-
FailureReason {
294-
message: "Undefined impact analysis justification".to_string(),
295-
context: ValidationContext(vec![ValidationPathComponent::Struct {
296-
struct_name: "VulnerabilityAnalysis".to_string(),
297-
field_name: "justification".to_string()
298-
},])
299-
},
300-
FailureReason {
301-
message: "Undefined response".to_string(),
302-
context: ValidationContext(vec![
303-
ValidationPathComponent::Struct {
304-
struct_name: "VulnerabilityAnalysis".to_string(),
305-
field_name: "responses".to_string()
306-
},
307-
ValidationPathComponent::Array { index: 0 },
308-
])
309-
},
310-
]
311-
}
285+
vec![
286+
validation::r#enum("state", "Undefined impact analysis state"),
287+
validation::r#enum("justification", "Undefined impact analysis justification"),
288+
validation::list(
289+
"responses",
290+
[(0, validation::custom("", ["Undefined response"]))]
291+
)
292+
]
293+
.into()
312294
);
313-
*/
314295
}
315296
}

cyclonedx-bom/src/models/vulnerability_reference.rs

Lines changed: 28 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ pub struct VulnerabilityReferences(pub Vec<VulnerabilityReference>);
6868
impl Validate for VulnerabilityReferences {
6969
fn validate_version(&self, version: SpecVersion) -> ValidationResult {
7070
ValidationContext::new()
71-
.add_list("inner", &self.0, |reference| reference.validate_version(version))
71+
.add_list("inner", &self.0, |reference| {
72+
reference.validate_version(version)
73+
})
7274
.into()
7375
}
7476
}
@@ -78,6 +80,7 @@ mod test {
7880
use crate::{
7981
external_models::{normalized_string::NormalizedString, uri::Uri},
8082
models::vulnerability_source::VulnerabilitySource,
83+
validation,
8184
};
8285

8386
use super::*;
@@ -108,56 +111,33 @@ mod test {
108111
}])
109112
.validate();
110113

111-
/*
112114
assert_eq!(
113115
validation_result,
114-
ValidationResult::Failed {
115-
reasons: vec![
116-
FailureReason {
117-
message:
116+
validation::list(
117+
"inner",
118+
[(
119+
0,
120+
vec![
121+
validation::field(
122+
"id",
118123
"NormalizedString contains invalid characters \\r \\n \\t or \\r\\n"
119-
.to_string(),
120-
context: ValidationContext(vec![
121-
ValidationPathComponent::Array { index: 0 },
122-
ValidationPathComponent::Struct {
123-
struct_name: "VulnerabilityReference".to_string(),
124-
field_name: "id".to_string()
125-
},
126-
])
127-
},
128-
FailureReason {
129-
message:
130-
"NormalizedString contains invalid characters \\r \\n \\t or \\r\\n"
131-
.to_string(),
132-
context: ValidationContext(vec![
133-
ValidationPathComponent::Array { index: 0 },
134-
ValidationPathComponent::Struct {
135-
struct_name: "VulnerabilityReference".to_string(),
136-
field_name: "vulnerability_source".to_string()
137-
},
138-
ValidationPathComponent::Struct {
139-
struct_name: "VulnerabilitySource".to_string(),
140-
field_name: "name".to_string()
141-
},
142-
])
143-
},
144-
FailureReason {
145-
message: "Uri does not conform to RFC 3986".to_string(),
146-
context: ValidationContext(vec![
147-
ValidationPathComponent::Array { index: 0 },
148-
ValidationPathComponent::Struct {
149-
struct_name: "VulnerabilityReference".to_string(),
150-
field_name: "vulnerability_source".to_string()
151-
},
152-
ValidationPathComponent::Struct {
153-
struct_name: "VulnerabilitySource".to_string(),
154-
field_name: "url".to_string()
155-
},
156-
])
157-
},
158-
]
159-
}
124+
),
125+
validation::r#struct(
126+
"vulnerability_source",
127+
vec![
128+
validation::field(
129+
"name",
130+
"NormalizedString contains invalid characters \\r \\n \\t or \\r\\n"
131+
),
132+
validation::field(
133+
"url",
134+
"Uri does not conform to RFC 3986"
135+
)
136+
]
137+
),
138+
]
139+
)]
140+
)
160141
);
161-
*/
162142
}
163143
}

cyclonedx-bom/src/models/vulnerability_source.rs

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ impl Validate for VulnerabilitySource {
6565

6666
#[cfg(test)]
6767
mod test {
68-
use crate::external_models::uri::Uri;
68+
use crate::{external_models::uri::Uri, validation};
6969

7070
use super::*;
7171
use pretty_assertions::assert_eq;
@@ -89,30 +89,16 @@ mod test {
8989
}
9090
.validate();
9191

92-
/*
9392
assert_eq!(
9493
validation_result,
95-
ValidationResult::Failed {
96-
reasons: vec![
97-
FailureReason {
98-
message:
99-
"NormalizedString contains invalid characters \\r \\n \\t or \\r\\n"
100-
.to_string(),
101-
context: ValidationContext(vec![ValidationPathComponent::Struct {
102-
struct_name: "VulnerabilitySource".to_string(),
103-
field_name: "name".to_string()
104-
},])
105-
},
106-
FailureReason {
107-
message: "Uri does not conform to RFC 3986".to_string(),
108-
context: ValidationContext(vec![ValidationPathComponent::Struct {
109-
struct_name: "VulnerabilitySource".to_string(),
110-
field_name: "url".to_string()
111-
},])
112-
},
113-
]
114-
}
94+
vec![
95+
validation::field(
96+
"name",
97+
"NormalizedString contains invalid characters \\r \\n \\t or \\r\\n"
98+
),
99+
validation::field("url", "Uri does not conform to RFC 3986"),
100+
]
101+
.into()
115102
);
116-
*/
117103
}
118104
}

cyclonedx-bom/src/models/vulnerability_target.rs

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -105,17 +105,6 @@ impl Validate for Version {
105105
.add_enum("version_range", &self.version_range, validate_version_range)
106106
.add_enum("status", &self.status, validate_status)
107107
.into()
108-
109-
/*
110-
111-
let status_context = context.with_struct("Version", "status");
112-
113-
results.push(self.status.validate_with_context(status_context));
114-
115-
results
116-
.into_iter()
117-
.fold(ValidationResult::default(), |acc, result| acc.merge(result))
118-
*/
119108
}
120109
}
121110

@@ -208,6 +197,8 @@ impl ToString for Status {
208197

209198
#[cfg(test)]
210199
mod test {
200+
use crate::validation;
201+
211202
use super::*;
212203
use pretty_assertions::assert_eq;
213204

@@ -244,30 +235,27 @@ mod test {
244235
}])
245236
.validate();
246237

247-
/*
248238
assert_eq!(
249239
validation_result,
250-
ValidationResult::Failed {
251-
reasons: vec![
252-
FailureReason::new(
253-
"Undefined version range",
254-
ValidationContext::new()
255-
.with_index(0)
256-
.with_struct("VulnerabilityTarget", "versions")
257-
.with_index(0)
258-
.with_struct("Version", "version_range")
259-
),
260-
FailureReason::new(
261-
"Undefined status",
262-
ValidationContext::new()
263-
.with_index(0)
264-
.with_struct("VulnerabilityTarget", "versions")
265-
.with_index(0)
266-
.with_struct("Version", "status")
240+
validation::list(
241+
"inner",
242+
[(
243+
0,
244+
validation::r#struct(
245+
"versions",
246+
validation::list(
247+
"inner",
248+
[(
249+
0,
250+
vec![
251+
validation::r#enum("version_range", "Undefined version range"),
252+
validation::r#enum("status", "Undefined status"),
253+
]
254+
)]
255+
)
267256
)
268-
]
269-
}
257+
)]
258+
)
270259
);
271-
*/
272260
}
273261
}

0 commit comments

Comments
 (0)