| 1 |
#![cfg(all(feature = "v4", feature = "std"))] |
| 2 |
|
| 3 |
use cvss::v4::Vector; |
| 4 |
use std::{fs, str::FromStr}; |
| 5 |
|
| 6 |
// We run the test set from Red Hat's Security Python implementation: https://github.com/RedHatProductSecurity/cvss |
| 7 |
// It seems to be the best test set available (at least for CVSS v4.0). |
| 8 |
|
| 9 |
fn run_tests_from_file(name: &str, test_serialization: bool) { |
| 10 |
let content = fs::read_to_string(format!("tests/cvss-redhat/tests/{}", name)).unwrap(); |
| 11 |
for l in content.lines() { |
| 12 |
let parts = l.split(" - ").collect::<Vec<&str>>(); |
| 13 |
let cvss = Vector::from_str(parts[0]).unwrap(); |
| 14 |
if test_serialization { |
| 15 |
// Test correct serialization. |
| 16 |
assert_eq!(cvss.to_string(), parts[0]); |
| 17 |
} |
| 18 |
assert!(cvss.score().value() >= 0.0); |
| 19 |
assert!(cvss.score().value() <= 10.0); |
| 20 |
let diff: f64 = cvss.score().value() - parts[1].parse::<f64>().unwrap(); |
| 21 |
assert!(diff.abs() < 0.0001); |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
#[test] |
| 26 |
fn cvss_v4_base() { |
| 27 |
// All vector combinations with only mandatory fields, 104,976 vectors. |
| 28 |
run_tests_from_file("vectors_base4", true); |
| 29 |
} |
| 30 |
|
| 31 |
#[test] |
| 32 |
fn cvss_v4_modified() { |
| 33 |
// All vector combinations of modified environmental fields, 373,248 vectors. |
| 34 |
run_tests_from_file("vectors_modified4", true); |
| 35 |
} |
| 36 |
|
| 37 |
#[test] |
| 38 |
fn cvss_v4_supplemental() { |
| 39 |
// All vector combinations of supplemental fields, 576 vectors. |
| 40 |
run_tests_from_file("vectors_supplemental4", true); |
| 41 |
} |
| 42 |
|
| 43 |
#[test] |
| 44 |
fn cvss_v4_security() { |
| 45 |
// All vector combinations of security fields, 54 vectors. |
| 46 |
run_tests_from_file("vectors_security4", true); |
| 47 |
} |
| 48 |
|
| 49 |
#[test] |
| 50 |
fn cvss_v4_threat() { |
| 51 |
// All vector combinations of threat fields, 6 vectors. |
| 52 |
run_tests_from_file("vectors_threat4", true); |
| 53 |
} |
| 54 |
|
| 55 |
#[test] |
| 56 |
fn cvss_v4_random() { |
| 57 |
// Random vector combinations across all fields, 10,000 vectors. |
| 58 |
run_tests_from_file("vectors_random4", false); |
| 59 |
} |