From cc684f2f35630484b3556ed87172faa5fcb1fd73 Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Mon, 3 Feb 2025 11:38:27 -0300 Subject: [PATCH] fix typo and apply suggestions --- rust/doc/openapi.yml | 4 +-- rust/src/models/host_info.rs | 48 ++++++++++++++++++++-------- rust/src/openvas/result_collector.rs | 24 +++++++------- 3 files changed, 47 insertions(+), 29 deletions(-) diff --git a/rust/doc/openapi.yml b/rust/doc/openapi.yml index 5337faa21..a1bc1a1f9 100644 --- a/rust/doc/openapi.yml +++ b/rust/doc/openapi.yml @@ -910,12 +910,12 @@ components: description: "Additional information about the scanned host", properties: { finished_tests: { - description: "The number of vulnerability test alredy run for the host", + description: "The number of vulnerability tests already run for the host", type: "integer", format: "int32" }, total_tests: { - description: "The total amount of vulnerability test to be run for the host", + description: "The total amount of vulnerability tests to be run for the host", type: "integer", format: "int32" } diff --git a/rust/src/models/host_info.rs b/rust/src/models/host_info.rs index 50baa6113..22a28eb3e 100644 --- a/rust/src/models/host_info.rs +++ b/rust/src/models/host_info.rs @@ -38,24 +38,49 @@ impl HostInfoBuilder { derive(serde::Serialize, serde::Deserialize) )] pub struct SingleHostScanInfo { - finished_tests: i32, - total_tests: i32, + finished_tests: AmountOfTests, + total_tests: AmountOfTests, +} + +#[derive(Debug, Clone, Eq, PartialEq)] +#[cfg_attr( + feature = "serde_support", + derive(serde::Serialize, serde::Deserialize) +)] +enum AmountOfTests { + AmountOfTests(i32), + DeadHost, +} + +impl Default for AmountOfTests { + fn default() -> Self { + AmountOfTests::AmountOfTests(0) + } } impl SingleHostScanInfo { pub fn new(finished_tests: i32, total_tests: i32) -> Self { Self { - finished_tests, - total_tests, + finished_tests: AmountOfTests::AmountOfTests(finished_tests), + total_tests: if total_tests == -1 { + AmountOfTests::DeadHost + } else { + AmountOfTests::AmountOfTests(total_tests) + }, } } - pub fn finished_tests(&self) -> i32 { - self.finished_tests + pub fn is_finished(&self) -> bool { + if let AmountOfTests::AmountOfTests(f) = self.finished_tests { + if let AmountOfTests::AmountOfTests(t) = self.total_tests { + return f == t; + } + } + false } - pub fn total_tests(&self) -> i32 { - self.total_tests + pub fn is_dead(&self) -> bool { + matches!(self.total_tests, AmountOfTests::DeadHost) } } @@ -118,9 +143,6 @@ impl HostInfo { } pub fn update_with(mut self, other: &HostInfo) -> Self { - enum ScanProgress { - DeadHost = -1, - } // total hosts value is sent once and only once must be updated if other.all != 0 { self.all = other.all; @@ -141,9 +163,7 @@ impl HostInfo { // and never completely replaced. let mut hs = other.scanning.clone().unwrap_or_default(); for (host, progress) in self.scanning.clone().unwrap_or_default().iter() { - if progress.finished_tests() == progress.total_tests() - || progress.total_tests == ScanProgress::DeadHost as i32 - { + if progress.is_finished() || progress.is_dead() { hs.remove(host); } else { hs.insert(host.to_string(), progress.clone()); diff --git a/rust/src/openvas/result_collector.rs b/rust/src/openvas/result_collector.rs index 324b13c12..a4b044e38 100644 --- a/rust/src/openvas/result_collector.rs +++ b/rust/src/openvas/result_collector.rs @@ -138,11 +138,12 @@ where } else if result_type == "ALARM" { push_result(OspResultType::Alarm); } else if result_type == "DEADHOST" { - new_dead += i64::from_str(&value).expect("Valid amount of dead hosts"); + new_dead += i64::from_str(&value).expect("Expected a valid amount of dead hosts"); } else if host_count { - count_total = i64::from_str(&value).expect("Valid amount of dead hosts"); + count_total = i64::from_str(&value).expect("Expected a valid amount of dead hosts"); } else if excluded_hosts { - count_excluded = i64::from_str(&value).expect("Valid amount of excluded hosts"); + count_excluded = + i64::from_str(&value).expect("Expected a valid amount of excluded hosts"); } } if let Ok(mut results) = Arc::as_ref(&self.results).lock() { @@ -163,17 +164,14 @@ where } fn process_status(&self, redis_status: Vec) -> RedisStorageResult<()> { - enum ScanProgress { - DeadHost = -1, - } let mut new_dead = 0; let mut new_alive = 0; let mut all_hosts: HashMap = HashMap::new(); for res in redis_status { let mut fields = res.splitn(3, '/'); - let current_host = fields.next().expect("Valid status value"); - let launched = fields.next().expect("Valid status value"); - let total = fields.next().expect("Valid status value"); + let current_host = fields.next().expect("Expected a valid status value"); + let launched = fields.next().expect("Expected a valid status value"); + let total = fields.next().expect("Expected a valid status value"); let total = match i32::from_str(total) { // No plugins @@ -181,21 +179,21 @@ where continue; } // Host Dead - Ok(-1) => ScanProgress::DeadHost as i32, + Ok(-1) => -1, Ok(n) => n, _ => { continue; } }; - let launched = i32::from_str(launched).expect("Integer"); - + let launched = i32::from_str(launched).expect("Expected a valid integer value"); let host_progress = ((launched as f32 / total as f32) * 100.0) as i32; - if host_progress == -1 { + if total == -1 { new_dead += 1; } else if host_progress == 100 { new_alive += 1; } + all_hosts.insert( current_host.to_string(), SingleHostScanInfo::new(launched, total),