File: redhat-v3.rs

package info (click to toggle)
rust-cvss 2.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 560 kB
  • sloc: makefile: 4
file content (40 lines) | stat: -rw-r--r-- 1,340 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#![cfg(all(feature = "v3", feature = "std"))]

use cvss::v3::Base;
use std::{fs, str::FromStr};

// Run the test set from Red Hat's Security Python implementation: https://github.com/RedHatProductSecurity/cvss

fn run_tests_from_file(name: &str) {
    let content = fs::read_to_string(format!("tests/cvss-redhat/tests/{}", name)).unwrap();
    for l in content.lines() {
        let parts = l.split(" - ").collect::<Vec<&str>>();
        let vector = parts[0];
        if vector.len() > 44 {
            // more than base, skip
            continue;
        }
        // "(base, _, _)"
        let score = parts[1].split(',').next().unwrap().trim_start_matches('(');

        let cvss = Base::from_str(vector).unwrap();
        // Test correct serialization.
        assert_eq!(cvss.to_string(), parts[0]);
        assert!(cvss.score().value() >= 0.0);
        assert!(cvss.score().value() <= 10.0);
        let diff: f64 = cvss.score().value() - score.parse::<f64>().unwrap();
        assert!(diff.abs() < 0.0001);
    }
}

// #[test] disabled due to missing testdata
fn cvss_v3_simple() {
    run_tests_from_file("vectors_simple3");
    run_tests_from_file("vectors_simple31");
}

// #[test] disabled due to missing testdata
fn cvss_v3_random() {
    run_tests_from_file("vectors_random3");
    run_tests_from_file("vectors_random31");
}