Skip to content

Commit

Permalink
added test
Browse files Browse the repository at this point in the history
  • Loading branch information
epi052 committed Mar 18, 2023
1 parent bd54ad0 commit d561e59
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,20 +238,32 @@ async fn wrapped_main(config: Arc<Configuration>) -> Result<()> {
}

let words = if config.wordlist.starts_with("http") {
// found a url scheme, attempt to download the wordlist
let response = config.client.get(&config.wordlist).send().await?;

if !response.status().is_success() {
// status code isn't a 200, bail
bail!(
"[{}] Unable to download wordlist from url: {}",
response.status().as_str(),
config.wordlist
);
}

// attempt to get the filename from the url's path
let Some(path_segments) = response
.url()
.path_segments() else {
bail!("Unable to parse path segments from url: {}", config.wordlist);
bail!("Unable to parse path from url: {}", response.url());
};

let Some(filename) = path_segments.last() else {
bail!("Unable to parse filename from url: {}", config.wordlist);
bail!("Unable to parse filename from url's path: {}", response.url().path());
};

let filename = filename.to_string();

// read the body and write it to disk, then use existing code to read the wordlist
let body = response.text().await?;

std::fs::write(&filename, body)?;
Expand Down
44 changes: 44 additions & 0 deletions tests/test_main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod utils;
use anyhow::Result;
use assert_cmd::Command;
use httpmock::Method::GET;
use httpmock::{MockServer, Regex};
Expand Down Expand Up @@ -218,3 +219,46 @@ fn main_parallel_creates_output_directory() -> Result<(), Box<dyn std::error::Er

Ok(())
}

#[test]
/// download a wordlist from a url
fn main_download_wordlist_from_url() -> Result<(), Box<dyn std::error::Error>> {
let srv = MockServer::start();

let (tmp_dir, _) = setup_tmp_directory(&["a".to_string()], "wordlist")?;

let mock1 = srv.mock(|when, then| {
when.method(GET).path("/derp");
then.status(200).body("stuff\nthings");
});

// serve endpoints stuff and things
let mock2 = srv.mock(|when, then| {
when.method(GET).path("/stuff");
then.status(200);
});

let mock3 = srv.mock(|when, then| {
when.method(GET).path("/things");
then.status(200);
});

Command::cargo_bin("feroxbuster")
.unwrap()
.current_dir(&tmp_dir)
.arg("--url")
.arg(srv.url("/"))
.arg("--wordlist")
.arg(srv.url("/derp"))
.assert()
.success()
.stderr(predicate::str::contains(srv.url("/derp")));

teardown_tmp_directory(tmp_dir);

assert_eq!(mock1.hits(), 1); // downloaded wordlist
assert_eq!(mock2.hits(), 1); // found stuff from wordlist
assert_eq!(mock3.hits(), 1); // found things from wordlist

Ok(())
}

0 comments on commit d561e59

Please sign in to comment.