Skip to content

Commit

Permalink
Add: function get_refs()
Browse files Browse the repository at this point in the history
Also add tests for it
  • Loading branch information
jjnicola committed Nov 23, 2022
1 parent ef20279 commit 2b822a0
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 4 deletions.
51 changes: 48 additions & 3 deletions rust/nvtcache/src/nvt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down Expand Up @@ -515,4 +515,49 @@ impl Nvt {
pub fn get_family(&mut self) -> Result<String> {
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()));
}
}
70 changes: 69 additions & 1 deletion rust/nvtcache/tests/nvt_tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use nvtcache::nvt::Nvt;
use nvtcache::dberror::Result;
use nvtcache::nvt::{Nvt, NvtRef};

#[cfg(test)]
mod test {
Expand Down Expand Up @@ -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(())
}
}

0 comments on commit 2b822a0

Please sign in to comment.