stixflayer 0.1.1

Fast, spec-correct STIX 2.1 parsing, validation, and serialization in Rust.
Documentation
//! Vulnerability SDO
//!
//! A Vulnerability is a weakness or defect in the requirements, designs, or implementations of the computational logic found in software and some hardware components.
//!
//! For more information, see <https://docs.oasis-open.org/cti/stix/v2.1/os/stix-v2.1-os.html#_q5ytzmajn6re>

use crate::error::CheckContext;
use crate::{base::Stix, error::StixError as Error};
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;
use stixflayer_derive::StixProperties;

/// Represents a weakness or defect in the computational logic of software or hardware components.
#[skip_serializing_none]
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, StixProperties)]
pub struct Vulnerability {
    /// Required: The name of the Vulnerability.
    pub name: String,
    /// An optional description providing more details about the Vulnerability.
    pub description: Option<String>,
}

impl Stix for Vulnerability {
    fn check(&self, _ctx: &mut CheckContext) -> Result<(), Error> {
        Ok(())
    }
}

#[cfg(test)]
mod tests {

    use crate::{
        domain_objects::sdo::{DomainObject, DomainObjectBuilder},
        types::ExternalReference,
    };
    use serde_json::Value;

    fn expected_vulnerability() -> DomainObject {
        DomainObjectBuilder::new("vulnerability")
            .unwrap()
            .name("vulnerability".to_string())
            .unwrap()
            .description("vulnerability desc".to_string())
            .unwrap()
            .external_references(vec![ExternalReference::new(
                "capec",
                None,
                None,
                Some("CAPEC-163".to_string()),
            )
            .unwrap()])
            .build()
            .unwrap()
            .test_id()
            .created("2016-05-12T08:17:27.000Z")
            .modified("2016-05-12T08:17:27.000Z")
    }

    #[test]
    fn serialize_vulnerability() {
        let vulnerabililty = expected_vulnerability();
        let result = serde_json::to_value(&vulnerabililty).unwrap();

        let expected = r#"{
            "type": "vulnerability",
            "name": "vulnerability",
            "description": "vulnerability desc",
            "spec_version": "2.1",
            "id": "vulnerability--cc7fa653-c35f-43db-afdd-dce4c3a241d5",
            "created": "2016-05-12T08:17:27Z",
            "modified": "2016-05-12T08:17:27Z",
            "external_references": [
            {
            "source_name": "capec",
            "external_id": "CAPEC-163"
            }
        ]
        }"#;

        let expected_value: Value = serde_json::from_str(expected).unwrap();
        assert_eq!(&result, &expected_value)
    }

    #[test]
    fn deserialize_vulnerability() {
        let json = r#"{
            "type": "vulnerability",
            "name": "vulnerability",
            "description": "vulnerability desc",
            "spec_version": "2.1",
            "id": "vulnerability--cc7fa653-c35f-43db-afdd-dce4c3a241d5",
            "created": "2016-05-12T08:17:27Z",
            "modified": "2016-05-12T08:17:27Z",
            "external_references": [
            {
            "source_name": "capec",
            "external_id": "CAPEC-163"
            }
        ]
        }"#;

        let result = DomainObject::from_json(json, false).unwrap();
        assert_eq!(result, expected_vulnerability());
    }
}