Skip to content

Commit b159259

Browse files
authored
Rename validation profile to validation preset throughout (#7)
1 parent 474ea33 commit b159259

File tree

6 files changed

+67
-64
lines changed

6 files changed

+67
-64
lines changed

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ This is work-in-progress.
99
If you want to build `csaf-validator` on your own, please install Rust (see https://rustup.rs) and then run
1010

1111
```bash
12-
# make sure, submodules are up-to-date
12+
# make sure submodules are up-to-date
13+
git submodule init
1314
git submodule update --remote
1415

1516
# run the tests
@@ -35,8 +36,8 @@ Arguments:
3536
3637
Options:
3738
-c, --csaf-version <CSAF_VERSION> Version of CSAF to use [default: 2.0]
38-
-p, --profile <PROFILE> The profile to use [default: basic]
39-
-o, --only-test <ONLY_TEST> Run only the selected test
39+
-p, --preset <PRESET> The validation preset to use [default: basic]
40+
-t, --test-id <TEST_ID> Run only the selected tests, may be specified multiple times
4041
-h, --help Print help
4142
-V, --version Print version
4243
```
@@ -48,8 +49,8 @@ Some examples to use are included below. Please note that the validation is not
4849
csaf-validator --csaf-version 2.0 my-csaf-2-0-document.json
4950

5051
# validate a CSAF 2.0 document with profile full
51-
csaf-validator --csaf-version 2.0 --profile full my-csaf-2-0-document.json
52+
csaf-validator --csaf-version 2.0 --preset full my-csaf-2-0-document.json
5253

53-
# validate a CSAF 2.1 document with a specific test
54-
csaf-validator --csaf-version 2.1 --only-test 6.1.34 my-csaf-2-1-document.json
54+
# validate a CSAF 2.1 document with one specific test
55+
csaf-validator --csaf-version 2.1 --test-id 6.1.34 my-csaf-2-1-document.json
5556
```

csaf

Submodule csaf updated 128 files

csaf-lib/src/csaf/csaf2_0/validation.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
use super::product_helper::*;
22
use super::schema::CommonSecurityAdvisoryFramework;
3-
use crate::csaf::validation::{Test, Validatable, ValidationProfile};
3+
use crate::csaf::validation::{Test, Validatable, ValidationPreset};
44
use std::collections::{HashMap, HashSet};
55
use crate::csaf::helpers::find_duplicates;
66

77
impl Validatable<CommonSecurityAdvisoryFramework> for CommonSecurityAdvisoryFramework {
8-
fn profiles(&self) -> HashMap<ValidationProfile, Vec<&str>> {
8+
fn presets(&self) -> HashMap<ValidationPreset, Vec<&str>> {
99
HashMap::from([
10-
(ValidationProfile::Basic, Vec::from(["6.1.1", "6.1.2"])),
11-
(ValidationProfile::Extended, Vec::from(["6.1.1", "6.1.2"])),
12-
(ValidationProfile::Full, Vec::from(["6.1.1", "6.1.2"])),
10+
(ValidationPreset::Basic, Vec::from(["6.1.1", "6.1.2"])),
11+
(ValidationPreset::Extended, Vec::from(["6.1.1", "6.1.2"])),
12+
(ValidationPreset::Full, Vec::from(["6.1.1", "6.1.2"])),
1313
])
1414
}
1515

csaf-lib/src/csaf/csaf2_1/validation.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
use super::product_helper::*;
22
use super::schema::CommonSecurityAdvisoryFramework;
33
use crate::csaf::helpers::find_duplicates;
4-
use crate::csaf::validation::{Test, Validatable, ValidationProfile};
4+
use crate::csaf::validation::{Test, Validatable, ValidationPreset};
55
use std::collections::{HashMap, HashSet};
66

77
impl Validatable<CommonSecurityAdvisoryFramework> for CommonSecurityAdvisoryFramework {
8-
fn profiles(&self) -> HashMap<ValidationProfile, Vec<&str>> {
8+
fn presets(&self) -> HashMap<ValidationPreset, Vec<&str>> {
99
HashMap::from([
1010
(
11-
ValidationProfile::Basic,
11+
ValidationPreset::Basic,
1212
Vec::from(["6.1.1", "6.1.2", "6.1.34"]),
1313
),
14-
(ValidationProfile::Extended, Vec::from(["6.1.1", "6.1.2"])),
15-
(ValidationProfile::Full, Vec::from(["6.1.1", "6.1.2"])),
14+
(ValidationPreset::Extended, Vec::from(["6.1.1", "6.1.2"])),
15+
(ValidationPreset::Full, Vec::from(["6.1.1", "6.1.2"])),
1616
])
1717
}
1818

csaf-lib/src/csaf/validation.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,28 @@ use std::str::FromStr;
44
pub enum ValidationError {}
55

66
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
7-
pub enum ValidationProfile {
7+
pub enum ValidationPreset {
88
Basic,
99
Extended,
1010
Full,
1111
}
1212

13-
impl FromStr for ValidationProfile {
13+
impl FromStr for ValidationPreset {
1414
type Err = ();
1515

16-
fn from_str(input: &str) -> Result<ValidationProfile, Self::Err> {
16+
fn from_str(input: &str) -> Result<ValidationPreset, Self::Err> {
1717
match input {
18-
"basic" => Ok(ValidationProfile::Basic),
19-
"extended" => Ok(ValidationProfile::Extended),
20-
"full" => Ok(ValidationProfile::Full),
18+
"basic" => Ok(ValidationPreset::Basic),
19+
"extended" => Ok(ValidationPreset::Extended),
20+
"full" => Ok(ValidationPreset::Full),
2121
_ => Err(()),
2222
}
2323
}
2424
}
2525

2626
pub trait Validate {
27-
/// Validates this object according to a validation profile
28-
fn validate_profile(&'static self, profile: ValidationProfile);
27+
/// Validates this object according to a validation preset
28+
fn validate_preset(&'static self, preset: ValidationPreset);
2929

3030
/// Validates this object according to a specific test ID.
3131
fn validate_by_test(&self, version: &str);
@@ -38,35 +38,35 @@ pub type Test<VersionedDocument> =
3838
/// This trait MUST be implemented by the struct that represents a CSAF document
3939
/// in the respective version.
4040
///
41-
/// It can then be used to validate documents with either [validate_by_profile] or [validate_by_test].
41+
/// It can then be used to validate documents with either [validate_by_preset] or [validate_by_test].
4242
pub trait Validatable<VersionedDocument> {
43-
/// Returns a hashmap containing the test ID per profile
44-
fn profiles(&self) -> HashMap<ValidationProfile, Vec<&str>>;
43+
/// Returns a hashmap containing the test ID per preset
44+
fn presets(&self) -> HashMap<ValidationPreset, Vec<&str>>;
4545

4646
/// Returns a hashmap containing the test function per test ID
4747
fn tests(&self) -> HashMap<&str, Test<VersionedDocument>>;
4848

4949
fn doc(&self) -> &VersionedDocument;
5050
}
5151

52-
/// Executes all tests of the specified [profile] against the [target]
52+
/// Executes all tests of the specified [preset] against the [target]
5353
/// (which is of type [VersionedDocument], e.g. a CSAF 2.0 document).
54-
pub fn validate_by_profile<VersionedDocument>(
54+
pub fn validate_by_preset<VersionedDocument>(
5555
target: &impl Validatable<VersionedDocument>,
56-
profile: ValidationProfile,
56+
preset: ValidationPreset,
5757
) {
58-
println!("Validating document with {:?} profile... \n", profile);
58+
println!("Validating document with {:?} preset... \n", preset);
5959

6060
// Loop through tests
61-
if let Some(tests) = target.profiles().get(&profile) {
61+
if let Some(tests) = target.presets().get(&preset) {
6262
for test_id in tests {
6363
println!("Executing Test {}... ", test_id);
6464
validate_by_test(target, test_id);
6565

6666
println!()
6767
}
6868
} else {
69-
println!("No tests found for profile")
69+
println!("No tests found for preset")
7070
}
7171
}
7272

csaf-validator/src/main.rs

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
use std::str::FromStr;
12
use anyhow::{bail, Result};
23
use csaf_lib::csaf::csaf2_0::loader::load_document as load_document_2_0;
34
use csaf_lib::csaf::csaf2_1::loader::load_document as load_document_2_1;
4-
use csaf_lib::csaf::validation::{validate_by_profile, validate_by_test, ValidationProfile};
5+
use csaf_lib::csaf::validation::{validate_by_preset, validate_by_test, Validatable, ValidationPreset};
56
use clap::Parser;
67

78
/// A validator for CSAF documents
@@ -15,44 +16,45 @@ struct Args {
1516
#[arg(short, long, default_value = "2.0")]
1617
csaf_version: String,
1718

18-
/// The profile to use
19+
/// The validation preset to use
1920
#[arg(short, long, default_value = "basic")]
20-
profile: String,
21+
preset: String,
2122

22-
/// Run only the selected test
23-
#[arg(short, long)]
24-
only_test: Option<String>,
23+
/// Run only the selected tests, may be specified multiple times
24+
#[arg(short, long, action = clap::ArgAction::Append)]
25+
test_id: Vec<String>,
2526
}
2627

2728
fn main() -> Result<()> {
2829
let args = Args::parse();
29-
let profile = ValidationProfile::Basic;
3030

31-
// TODO: it would be nice to return the validatable from this match, but this is beyond my
32-
// rust generics knowledge, so a little bit of duplicate code here
33-
if let Some(test_id) = args.only_test {
34-
let result = match args.csaf_version.as_str() {
35-
"2.0" => {
36-
validate_by_test(&load_document_2_0(args.path.as_str())?, test_id.as_str())
37-
}
38-
"2.1" => {
39-
validate_by_test(&load_document_2_1(args.path.as_str())?, test_id.as_str())
40-
}
41-
_ => bail!("invalid version"),
42-
};
31+
match args.csaf_version.as_str() {
32+
"2.0" => {
33+
process_document(load_document_2_0(args.path.as_str())?, &args)
34+
}
35+
"2.1" => {
36+
process_document(load_document_2_1(args.path.as_str())?, &args)
37+
}
38+
_ => bail!(format!("Invalid CSAF version: {}", args.csaf_version)),
39+
}
40+
}
4341

44-
Ok(result)
42+
fn process_document<T>(document: T, args: &Args) -> Result<()>
43+
where
44+
T: Validatable<T>,
45+
{
46+
if !args.test_id.is_empty() {
47+
for test_id in &args.test_id {
48+
println!("\nExecuting Test {}... ", test_id);
49+
validate_by_test(&document, test_id.as_str());
50+
}
51+
Ok(())
4552
} else {
46-
let result = match args.csaf_version.as_str() {
47-
"2.0" => {
48-
validate_by_profile(&load_document_2_0(args.path.as_str())?, profile)
49-
}
50-
"2.1" => {
51-
validate_by_profile(&load_document_2_1(args.path.as_str())?, profile)
52-
}
53-
_ => bail!("invalid version"),
53+
let preset = match ValidationPreset::from_str(args.preset.as_str()) {
54+
Ok(preset) => preset,
55+
Err(_) => bail!(format!("Invalid validation preset: {}", args.preset)),
5456
};
55-
56-
Ok(result)
57+
validate_by_preset(&document, preset);
58+
Ok(())
5759
}
5860
}

0 commit comments

Comments
 (0)