From 1c48f754a28aa29443502e68a20bdb9a625f87fa Mon Sep 17 00:00:00 2001 From: epi Date: Sun, 19 Dec 2021 11:12:51 -0600 Subject: [PATCH] added some tests --- src/scan_manager/menu.rs | 56 ++++++++++++++---------------- src/scan_manager/scan_container.rs | 6 +++- src/scan_manager/tests.rs | 51 +++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 31 deletions(-) diff --git a/src/scan_manager/menu.rs b/src/scan_manager/menu.rs index d00b635..3af36e7 100644 --- a/src/scan_manager/menu.rs +++ b/src/scan_manager/menu.rs @@ -33,7 +33,7 @@ pub(super) struct Menu { footer: String, /// target for output - term: Term, + pub(super) term: Term, } /// Implementation of Menu @@ -175,39 +175,35 @@ impl Menu { } /// get input from the user and translate it to a `MenuCmd` - pub(super) fn get_command_input_from_user(&self) -> Option { - if let Ok(line) = self.term.read_line() { - let line = line.trim(); // normalize input if there are leading spaces + pub(super) fn get_command_input_from_user(&self, line: &str) -> Option { + let line = line.trim(); // normalize input if there are leading spaces - match line.chars().next().unwrap_or('_').to_ascii_lowercase() { - 'c' => { - // cancel command; start by determining if -f was used - let force = line.contains("-f"); + match line.chars().next().unwrap_or('_').to_ascii_lowercase() { + 'c' => { + // cancel command; start by determining if -f was used + let force = line.contains("-f"); - // then remove c[ancel] from the command so it can be passed to the number - // splitter - let re = Regex::new(r"^[cC][ancelANCEL]*").unwrap(); - let line = line.replace("-f", ""); - let line = re.replace(&line, "").to_string(); + // then remove c[ancel] from the command so it can be passed to the number + // splitter + let re = Regex::new(r"^[cC][ancelANCEL]*").unwrap(); + let line = line.replace("-f", ""); + let line = re.replace(&line, "").to_string(); - Some(MenuCmd::Cancel(self.split_to_nums(&line), force)) - } - 'a' => { - // add command - // similar to cancel, we need to remove the a[dd] substring, the rest should be - // a url - let re = Regex::new(r"^[aA][dD]*").unwrap(); - let line = re.replace(line, "").to_string().trim().to_string(); - - Some(MenuCmd::Add(line)) - } - _ => { - // invalid input - None - } + Some(MenuCmd::Cancel(self.split_to_nums(&line), force)) + } + 'a' => { + // add command + // similar to cancel, we need to remove the a[dd] substring, the rest should be + // a url + let re = Regex::new(r"^[aA][dD]*").unwrap(); + let line = re.replace(line, "").to_string().trim().to_string(); + + Some(MenuCmd::Add(line)) + } + _ => { + // invalid input + None } - } else { - None } } diff --git a/src/scan_manager/scan_container.rs b/src/scan_manager/scan_container.rs index 0034f41..ab1124e 100644 --- a/src/scan_manager/scan_container.rs +++ b/src/scan_manager/scan_container.rs @@ -308,7 +308,11 @@ impl FeroxScans { self.display_scans().await; self.menu.print_footer(); - let menu_cmd = self.menu.get_command_input_from_user(); + let menu_cmd = if let Ok(line) = self.menu.term.read_line() { + self.menu.get_command_input_from_user(&line) + } else { + None + }; let result = match menu_cmd { Some(MenuCmd::Cancel(indices, should_force)) => { diff --git a/src/scan_manager/tests.rs b/src/scan_manager/tests.rs index 9a540de..625cf9c 100644 --- a/src/scan_manager/tests.rs +++ b/src/scan_manager/tests.rs @@ -572,6 +572,9 @@ async fn ferox_scan_abort() { /// and their correctness can be verified easily manually; just calling for now fn menu_print_header_and_footer() { let menu = Menu::new(); + let menu_cmd_res_1 = MenuCmdResult::Url(String::from("http://localhost")); + let menu_cmd_res_2 = MenuCmdResult::NumCancelled(2); + println!("{:?}{:?}", menu_cmd_res_1, menu_cmd_res_2); menu.clear_screen(); menu.print_header(); menu.print_footer(); @@ -579,6 +582,54 @@ fn menu_print_header_and_footer() { menu.show_progress_bars(); } +#[test] +/// ensure command parsing from user input results int he correct MenuCmd returned +fn menu_get_command_input_from_user_returns_cancel() { + let menu = Menu::new(); + + for (idx, cmd) in ["cancel", "Cancel", "c", "C"].iter().enumerate() { + let force = idx % 2 == 0; + + let full_cmd = if force { + format!("{} -f {}\n", cmd, idx) + } else { + format!("{} {}\n", cmd, idx) + }; + + let result = menu.get_command_input_from_user(&full_cmd).unwrap(); + + assert!(matches!(result, MenuCmd::Cancel(_, _))); + + if let MenuCmd::Cancel(canx_list, ret_force) = result { + if idx == 0 { + assert!(canx_list.is_empty()); + } else { + assert_eq!(canx_list, vec![idx]); + } + assert_eq!(force, ret_force); + } + } +} + +#[test] +/// ensure command parsing from user input results int he correct MenuCmd returned +fn menu_get_command_input_from_user_returns_add() { + let menu = Menu::new(); + + for cmd in &["add", "Addd", "a", "A"] { + let test_url = "http://happyfuntimes.commmm"; + let full_cmd = format!("{} {}\n", cmd, test_url); + + let result = menu.get_command_input_from_user(&full_cmd).unwrap(); + + assert!(matches!(result, MenuCmd::Add(_))); + + if let MenuCmd::Add(url) = result { + assert_eq!(url, test_url); + } + } +} + #[test] /// ensure spaces are trimmed and numbers are returned from split_to_nums fn split_to_nums_is_correct() {