|
| 1 | +/* |
| 2 | + * Test rustscan against different targets with a time limit. |
| 3 | + * The tests assumes target/debug/rustscan has already been built. |
| 4 | + * |
| 5 | + * The tests are #[ignore] to avoid running them during normal development. |
| 6 | + * |
| 7 | + * Their tests in the timelimits module are run by travis during CI. |
| 8 | + */ |
| 9 | + |
| 10 | +use std::process::Command; |
| 11 | +use std::time::Duration; |
| 12 | +use wait_timeout::ChildExt; |
| 13 | + |
| 14 | +const TIMEOUT_MARGIN: u32 = 3; |
| 15 | + |
| 16 | +fn run_rustscan_with_timeout(args: &[&str], timeout: Duration) { |
| 17 | + println!("Running: target/debug/rustscan: {}", args.join(" ")); |
| 18 | + |
| 19 | + use std::time::Instant; |
| 20 | + |
| 21 | + let start = Instant::now(); |
| 22 | + |
| 23 | + let mut child = Command::new("target/debug/rustscan") |
| 24 | + .args(args) |
| 25 | + .spawn() |
| 26 | + .unwrap(); |
| 27 | + |
| 28 | + let mut tries = TIMEOUT_MARGIN; |
| 29 | + loop { |
| 30 | + match child.wait_timeout(timeout).unwrap() { |
| 31 | + Some(_status) => break, |
| 32 | + None => { |
| 33 | + tries -= 1; |
| 34 | + if tries == 0 { |
| 35 | + // child hasn't exited yet |
| 36 | + child.kill().unwrap(); |
| 37 | + child.wait().unwrap().code(); |
| 38 | + panic!("Timeout while running command"); |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + let end = Instant::now(); |
| 44 | + let duration = end.saturating_duration_since(start).as_secs_f32(); |
| 45 | + |
| 46 | + println!("time: {:1.1}s", duration); |
| 47 | +} |
| 48 | + |
| 49 | +mod timelimits { |
| 50 | + |
| 51 | + #[test] |
| 52 | + #[ignore] |
| 53 | + fn scan_localhost() { |
| 54 | + let timeout = super::Duration::from_secs(25); |
| 55 | + super::run_rustscan_with_timeout(&["--greppable", "--no-nmap", "127.0.0.1"], timeout); |
| 56 | + } |
| 57 | + |
| 58 | + #[test] |
| 59 | + #[ignore] |
| 60 | + fn scan_google_com() { |
| 61 | + super::run_rustscan_with_timeout( |
| 62 | + &[ |
| 63 | + "--greppable", |
| 64 | + "--no-nmap", |
| 65 | + "-u", |
| 66 | + "5000", |
| 67 | + "-b", |
| 68 | + "2500", |
| 69 | + "google.com", |
| 70 | + ], |
| 71 | + super::Duration::from_secs(28), |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + #[test] |
| 76 | + #[ignore] |
| 77 | + fn scan_example_com() { |
| 78 | + super::run_rustscan_with_timeout( |
| 79 | + &[ |
| 80 | + "--greppable", |
| 81 | + "--no-nmap", |
| 82 | + "-u", |
| 83 | + "5000", |
| 84 | + "-b", |
| 85 | + "2500", |
| 86 | + "example.com", |
| 87 | + ], |
| 88 | + super::Duration::from_secs(28), |
| 89 | + ); |
| 90 | + } |
| 91 | + |
| 92 | + #[test] |
| 93 | + #[ignore] |
| 94 | + fn scan_rustscan_cmnatic_co_uk() { |
| 95 | + super::run_rustscan_with_timeout( |
| 96 | + &[ |
| 97 | + "--greppable", |
| 98 | + "--no-nmap", |
| 99 | + "-u", |
| 100 | + "5000", |
| 101 | + "-b", |
| 102 | + "2500", |
| 103 | + "rustscan.cmnatic.co.uk", |
| 104 | + ], |
| 105 | + super::Duration::from_secs(26), |
| 106 | + ); |
| 107 | + } |
| 108 | +} |
0 commit comments