From a906b9731ee8aad3dd44b7b4b8ba9fc97cf9fb8f Mon Sep 17 00:00:00 2001 From: epi Date: Wed, 7 Oct 2020 05:30:58 -0500 Subject: [PATCH] added client test; setup_tmp_directory accepts a filename now --- src/client.rs | 9 ++++++--- tests/test_banner.rs | 2 +- tests/test_config.rs | 27 +++++++++++++++++++++++++++ tests/test_heuristics.rs | 10 +++++----- tests/test_main.rs | 4 ++-- tests/test_scanner.rs | 4 ++-- tests/utils/mod.rs | 6 ++++-- 7 files changed, 47 insertions(+), 15 deletions(-) create mode 100644 tests/test_config.rs diff --git a/src/client.rs b/src/client.rs index 74ef04e..4b4e13c 100644 --- a/src/client.rs +++ b/src/client.rs @@ -3,6 +3,7 @@ use reqwest::header::HeaderMap; use reqwest::{redirect::Policy, Client, Proxy}; use std::collections::HashMap; use std::convert::TryInto; +#[cfg(not(test))] use std::process::exit; use std::time::Duration; @@ -87,15 +88,17 @@ mod tests { #[test] #[should_panic] + /// create client with a bad proxy, expect panic fn client_with_bad_proxy() { let headers = HashMap::new(); - let client = initialize(0, "stuff", true, false, &headers, Some("not a valid proxy")); + initialize(0, "stuff", true, false, &headers, Some("not a valid proxy")); } #[test] + /// create client with a proxy, expect no error fn client_with_good_proxy() { let headers = HashMap::new(); let proxy = "http://127.0.0.1:8080"; - let client = initialize(0, "stuff", true, true, &headers, Some(proxy.clone())); + initialize(0, "stuff", true, true, &headers, Some(proxy)); } -} \ No newline at end of file +} diff --git a/tests/test_banner.rs b/tests/test_banner.rs index 6cf56ad..4d25fcb 100644 --- a/tests/test_banner.rs +++ b/tests/test_banner.rs @@ -11,7 +11,7 @@ fn banner_prints_proxy() -> Result<(), Box> { String::from("http://localhost"), String::from("http://schmocalhost"), ]; - let (tmp_dir, file) = setup_tmp_directory(&urls)?; + let (tmp_dir, file) = setup_tmp_directory(&urls, "wordlist")?; Command::cargo_bin("feroxbuster") .unwrap() diff --git a/tests/test_config.rs b/tests/test_config.rs new file mode 100644 index 0000000..c9a68eb --- /dev/null +++ b/tests/test_config.rs @@ -0,0 +1,27 @@ +mod utils; +use assert_cmd::prelude::*; +use predicates::prelude::*; +use std::process::Command; +use utils::{setup_tmp_directory, teardown_tmp_directory}; + +#[test] +/// send a single valid request, expect a 200 response +fn read_in_config_file_for_settings() -> Result<(), Box> { + let (tmp_dir, file) = setup_tmp_directory(&["threads = 37".to_string()], "ferox-config.toml")?; + + Command::cargo_bin("feroxbuster") + .unwrap() + .current_dir(&tmp_dir) + .arg("--url") + .arg("http://localhost") + .arg("--wordlist") + .arg(file.as_os_str()) + .arg("-vvvv") + .assert() + .success() + .stderr(predicate::str::contains("│ 37")); + + teardown_tmp_directory(tmp_dir); + + Ok(()) +} diff --git a/tests/test_heuristics.rs b/tests/test_heuristics.rs index 8e48c91..6f8b0cc 100644 --- a/tests/test_heuristics.rs +++ b/tests/test_heuristics.rs @@ -10,7 +10,7 @@ use utils::{setup_tmp_directory, teardown_tmp_directory}; /// test passes one bad target via -u to the scanner, expected result is that the /// scanner dies fn test_single_target_cannot_connect() -> Result<(), Box> { - let (tmp_dir, file) = setup_tmp_directory(&["LICENSE".to_string()])?; + let (tmp_dir, file) = setup_tmp_directory(&["LICENSE".to_string()], "wordlist")?; Command::cargo_bin("feroxbuster") .unwrap() @@ -37,7 +37,7 @@ fn test_two_targets_cannot_connect() -> Result<(), Box> { let not_real = String::from("http://fjdksafjkdsajfkdsajkfdsajkfsdjkdsfdsafdsafdsajkr3l2ajfdskafdsjk"); let urls = vec![not_real.clone(), not_real]; - let (tmp_dir, file) = setup_tmp_directory(&urls)?; + let (tmp_dir, file) = setup_tmp_directory(&urls, "wordlist")?; Command::cargo_bin("feroxbuster") .unwrap() @@ -67,7 +67,7 @@ fn test_one_good_and_one_bad_target_scan_succeeds() -> Result<(), Box Result<(), Box Result<(), Box> { let srv = MockServer::start(); - let (tmp_dir, file) = setup_tmp_directory(&["LICENSE".to_string()])?; + let (tmp_dir, file) = setup_tmp_directory(&["LICENSE".to_string()], "wordlist")?; let mock = Mock::new() .expect_method(GET) @@ -135,7 +135,7 @@ fn test_static_wildcard_request_found() -> Result<(), Box /// test finds a dynamic wildcard and reports as much to stdout fn test_dynamic_wildcard_request_found() -> Result<(), Box> { let srv = MockServer::start(); - let (tmp_dir, file) = setup_tmp_directory(&["LICENSE".to_string()])?; + let (tmp_dir, file) = setup_tmp_directory(&["LICENSE".to_string()], "wordlist")?; let mock = Mock::new() .expect_method(GET) diff --git a/tests/test_main.rs b/tests/test_main.rs index eb92ca1..ca7167a 100644 --- a/tests/test_main.rs +++ b/tests/test_main.rs @@ -39,7 +39,7 @@ fn main_use_root_owned_file_as_wordlist() -> Result<(), Box Result<(), Box> { let srv = MockServer::start(); - let (tmp_dir, file) = setup_tmp_directory(&[])?; + let (tmp_dir, file) = setup_tmp_directory(&[], "wordlist")?; let mock = Mock::new() .expect_method(GET) @@ -70,7 +70,7 @@ fn main_use_empty_wordlist() -> Result<(), Box> { #[test] /// send nothing over stdin, expect heuristics to be upset during connectivity test fn main_use_empty_stdin_targets() -> Result<(), Box> { - let (tmp_dir, file) = setup_tmp_directory(&[])?; + let (tmp_dir, file) = setup_tmp_directory(&[], "wordlist")?; // get_targets is called before scan, so the empty wordlist shouldn't trigger // the 'Did not find any words' error diff --git a/tests/test_scanner.rs b/tests/test_scanner.rs index 4e94140..b8a4a75 100644 --- a/tests/test_scanner.rs +++ b/tests/test_scanner.rs @@ -10,7 +10,7 @@ use utils::{setup_tmp_directory, teardown_tmp_directory}; /// send a single valid request, expect a 200 response fn test_single_request_scan() -> Result<(), Box> { let srv = MockServer::start(); - let (tmp_dir, file) = setup_tmp_directory(&["LICENSE".to_string()])?; + let (tmp_dir, file) = setup_tmp_directory(&["LICENSE".to_string()], "wordlist")?; let mock = Mock::new() .expect_method(GET) @@ -49,7 +49,7 @@ fn scanner_recursive_request_scan() -> Result<(), Box> { "dev".to_string(), "file.js".to_string(), ]; - let (tmp_dir, file) = setup_tmp_directory(&urls)?; + let (tmp_dir, file) = setup_tmp_directory(&urls, "wordlist")?; let js_mock = Mock::new() .expect_method(GET) diff --git a/tests/utils/mod.rs b/tests/utils/mod.rs index a148705..e80db5c 100644 --- a/tests/utils/mod.rs +++ b/tests/utils/mod.rs @@ -3,12 +3,14 @@ use std::path::PathBuf; use tempfile::TempDir; /// integration test helper: creates a temp directory, and writes `words` to -/// a file named `wordlist` in the temp directory +/// a file named `filename` in the temp directory pub fn setup_tmp_directory( words: &[String], + filename: &str, ) -> Result<(TempDir, PathBuf), Box> { let tmp_dir = TempDir::new()?; - let file = tmp_dir.path().join("wordlist"); + let file = tmp_dir.path().join(&filename); + println!("file: {:?}", file); write(&file, words.join("\n"))?; Ok((tmp_dir, file)) }