use crate::{base::Stix, error::StixError as Error};
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;
use stixflayer_derive::StixProperties;
#[skip_serializing_none]
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, StixProperties)]
pub struct Vulnerability {
pub name: String,
pub description: Option<String>,
}
impl Stix for Vulnerability {
fn stix_check(&self) -> 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());
}
}