Skip to content

Commit 9af4f10

Browse files
committed
Extended README.md
1 parent e497792 commit 9af4f10

File tree

3 files changed

+61
-20
lines changed

3 files changed

+61
-20
lines changed

README.md

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,47 @@
22

33
This repository is a proof-of-concept for a CSAF library in Rust that relies on automatically generating CSAF document structs from the JSON schema.
44

5-
This is work-in-progress.
5+
This is work-in-progress.
6+
7+
## Build
8+
9+
If you want to build `csaf-validator` on your own, please install Rust (see https://rustup.rs) and then run
10+
11+
```bash
12+
cargo build --release
13+
```
14+
15+
The final binary will be in `target/release` and can then installed for example in a system-wide folder.
16+
17+
## Usage
18+
19+
After building or downloading `csaf-validator`, the usage is quite simple and additional help can be display using `--help`.
20+
21+
```
22+
A validator for CSAF documents
23+
24+
Usage: csaf-validator [OPTIONS] <PATH>
25+
26+
Arguments:
27+
<PATH>
28+
29+
Options:
30+
-c, --csaf-version <CSAF_VERSION> Version of CSAF to use [default: 2.0]
31+
-p, --profile <PROFILE> The profile to use [default: basic]
32+
-o, --only-test <ONLY_TEST> Run only the selected test
33+
-h, --help Print help
34+
-V, --version Print version
35+
```
36+
37+
Some examples to use are included below. Please note that the validation is not yet fully implemented!
38+
39+
```
40+
# validate a CSAF 2.0 document with profile basic (the default)
41+
csaf-validator --csaf-version 2.0 my-csaf-2-0-document.json
42+
43+
# validate a CSAF 2.0 document with profile full
44+
csaf-validator --csaf-version 2.0 --profile full my-csaf-2-0-document.json
45+
46+
# validate a CSAF 2.1 document with a specific test
47+
csaf-validator --csaf-version 2.1 --only-test 6.1.34 my-csaf-2-1-document.json
48+
```

csaf-lib/src/csaf/validation.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub trait Validatable<VersionedDocument> {
5252
/// Executes all tests of the specified [profile] against the [target]
5353
/// (which is of type [VersionedDocument], e.g. a CSAF 2.0 document).
5454
pub fn validate_by_profile<VersionedDocument>(
55-
target: impl Validatable<VersionedDocument>,
55+
target: &impl Validatable<VersionedDocument>,
5656
profile: ValidationProfile,
5757
) {
5858
println!("Validating document with {:?} profile... \n", profile);
@@ -61,15 +61,7 @@ pub fn validate_by_profile<VersionedDocument>(
6161
if let Some(tests) = target.profiles().get(&profile) {
6262
for test_id in tests {
6363
println!("Executing Test {}... ", test_id);
64-
65-
if let Some(test_fn) = target.tests().get(test_id) {
66-
let _ = match test_fn(target.doc()) {
67-
Ok(()) => println!("> Test Success"),
68-
Err(e) => println!("> Error: {}", e),
69-
};
70-
} else {
71-
println!("Test with ID {} is missing implementation", test_id);
72-
}
64+
validate_by_test(target, test_id);
7365

7466
println!()
7567
}
@@ -79,8 +71,15 @@ pub fn validate_by_profile<VersionedDocument>(
7971
}
8072

8173
pub fn validate_by_test<VersionedDocument>(
82-
target: impl Validatable<VersionedDocument>,
74+
target: &impl Validatable<VersionedDocument>,
8375
test_id: &str,
8476
) {
85-
todo!()
77+
if let Some(test_fn) = target.tests().get(test_id) {
78+
let _ = match test_fn(target.doc()) {
79+
Ok(()) => println!("> Test Success"),
80+
Err(e) => println!("> Error: {}", e),
81+
};
82+
} else {
83+
println!("Test with ID {} is missing implementation", test_id);
84+
}
8685
}

csaf-validator/src/main.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
use anyhow::{anyhow, bail, Result};
1+
use anyhow::{bail, Result};
22
use csaf_lib::csaf::csaf2_0::loader::load_document as load_document_2_0;
33
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, Validatable, ValidationProfile};
5-
use std::env;
4+
use csaf_lib::csaf::validation::{validate_by_profile, validate_by_test, ValidationProfile};
65
use clap::Parser;
76

87
/// A validator for CSAF documents
@@ -34,10 +33,10 @@ fn main() -> Result<()> {
3433
if let Some(test_id) = args.only_test {
3534
let result = match args.csaf_version.as_str() {
3635
"2.0" => {
37-
validate_by_test(load_document_2_0(args.path.as_str())?, test_id.as_str())
36+
validate_by_test(&load_document_2_0(args.path.as_str())?, test_id.as_str())
3837
}
3938
"2.1" => {
40-
validate_by_test(load_document_2_1(args.path.as_str())?, test_id.as_str())
39+
validate_by_test(&load_document_2_1(args.path.as_str())?, test_id.as_str())
4140
}
4241
_ => bail!("invalid version"),
4342
};
@@ -46,10 +45,10 @@ fn main() -> Result<()> {
4645
} else {
4746
let result = match args.csaf_version.as_str() {
4847
"2.0" => {
49-
validate_by_profile(load_document_2_0(args.path.as_str())?, profile)
48+
validate_by_profile(&load_document_2_0(args.path.as_str())?, profile)
5049
}
5150
"2.1" => {
52-
validate_by_profile(load_document_2_1(args.path.as_str())?, profile)
51+
validate_by_profile(&load_document_2_1(args.path.as_str())?, profile)
5352
}
5453
_ => bail!("invalid version"),
5554
};

0 commit comments

Comments
 (0)