From 2b822a08a891a260f80157f0a785c992a9f67b78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ing=2E=20Juan=20Jos=C3=A9=20Nicola?= Date: Tue, 15 Nov 2022 08:57:30 +0100 Subject: [PATCH] Add: function get_refs() Also add tests for it --- rust/nvtcache/src/nvt.rs | 51 +++++++++++++++++++++-- rust/nvtcache/tests/nvt_tests.rs | 70 +++++++++++++++++++++++++++++++- 2 files changed, 117 insertions(+), 4 deletions(-) diff --git a/rust/nvtcache/src/nvt.rs b/rust/nvtcache/src/nvt.rs index 582c50090..2a82fd3f6 100644 --- a/rust/nvtcache/src/nvt.rs +++ b/rust/nvtcache/src/nvt.rs @@ -82,15 +82,15 @@ impl NvtRef { } /// Return the type of the NvtRef pub fn get_type(&mut self) -> String { - return self.ref_type; + return self.ref_type.clone(); } /// Return the id of the NvtRef pub fn get_id(&mut self) -> String { - return self.ref_id; + return self.ref_id.clone(); } /// Return the text of the NvtRef pub fn get_text(&mut self) -> String { - return self.ref_text; + return self.ref_text.clone(); } } @@ -515,4 +515,49 @@ impl Nvt { pub fn get_family(&mut self) -> Result { Ok(self.family.clone()) } + + /// Get References. It returns a tuple of three strings + /// Each string is a references type, and each string + /// can contain a list of references of the same type. + /// The string contains in the following types: + /// (cve_types, bid_types, other_types) + /// cve and bid strings are CSC strings containing only + /// "id, id, ...", while other custom types includes the type + /// and the string is in the format "type:id, type:id, ..." + pub fn get_refs(&mut self) -> Result<(String, String, String)> { + let mut bid = String::new(); + let mut cve = String::new(); + let mut xrefs = String::new(); + + for r in self.refs.iter_mut() { + let single_ref = r; + let reftype = single_ref.get_type(); + + let id = single_ref.get_id(); + match reftype.as_str() { + "bid" => { + if !bid.is_empty() { + bid = [bid.as_str(), ", ", id.as_str()].join(""); + } else { + bid = [id.as_str()].join(""); + } + } + "cve" => { + if !cve.is_empty() { + cve = [cve.as_str(), ", ", id.as_str()].join(""); + } else { + cve = [id.as_str()].join(""); + } + } + _ => { + if !xrefs.is_empty() { + xrefs = [xrefs.as_str(), ", ", reftype.as_str(), ":", id.as_str()].join(""); + } else { + xrefs = [reftype.as_str(), ":", id.as_str()].join(""); + } + } + } + } + return Ok((cve.to_string(), bid.to_string(), xrefs.to_string())); + } } diff --git a/rust/nvtcache/tests/nvt_tests.rs b/rust/nvtcache/tests/nvt_tests.rs index 27cb1bf66..e35e7b3ce 100644 --- a/rust/nvtcache/tests/nvt_tests.rs +++ b/rust/nvtcache/tests/nvt_tests.rs @@ -1,4 +1,5 @@ -use nvtcache::nvt::Nvt; +use nvtcache::dberror::Result; +use nvtcache::nvt::{Nvt, NvtRef}; #[cfg(test)] mod test { @@ -37,4 +38,71 @@ mod test { Err(e) => println!("Error:{}", e), } } + + #[test] + fn test_bid_refs() -> Result<()> { + let mut nvt = Nvt::new()?; + let bid_refs1 = NvtRef::new( + "bid".to_owned(), + "BID_ID1".to_owned(), + "BID-text".to_owned(), + )?; + let bid_refs2 = NvtRef::new( + "bid".to_owned(), + "BID_ID2".to_owned(), + "BID-text".to_owned(), + )?; + + nvt.add_ref(bid_refs1)?; + nvt.add_ref(bid_refs2)?; + let bid; + (_, bid, _) = nvt.get_refs()?; + + assert_eq!(bid, "BID_ID1, BID_ID2"); + + Ok(()) + } + #[test] + fn test_cve_refs() -> Result<()> { + let mut nvt = Nvt::new()?; + let cve_refs1 = NvtRef::new( + "cve".to_owned(), + "cve_ID1".to_owned(), + "CVE-text".to_owned(), + )?; + let cve_refs2 = NvtRef::new( + "cve".to_owned(), + "cve_ID1".to_owned(), + "CVE-text".to_owned(), + )?; + nvt.add_ref(cve_refs1)?; + nvt.add_ref(cve_refs2)?; + let cve; + (cve, _, _) = nvt.get_refs()?; + assert_eq!(cve, "cve_ID1, cve_ID1"); + + Ok(()) + } + #[test] + fn test_xrefs() -> Result<()> { + let mut nvt = Nvt::new()?; + let xrefs1 = NvtRef::new( + "URL".to_owned(), + "http://greenbone.net".to_owned(), + "some text".to_owned(), + )?; + let xrefs2 = NvtRef::new( + "URL".to_owned(), + "http://openvas.net".to_owned(), + "some text".to_owned(), + )?; + + nvt.add_ref(xrefs1)?; + nvt.add_ref(xrefs2)?; + let xrefs; + (_, _, xrefs) = nvt.get_refs()?; + assert_eq!(xrefs, "URL:http://greenbone.net, URL:http://openvas.net"); + + Ok(()) + } }