Skip to content

Commit b3a6427

Browse files
authored
Add integration tests with timeout (#254)
* Add integration tests with timeout * cargo fmt * Fix travis command line * Add extra margins for the timeout values * Increase the time to scan Output the time taken after each test
1 parent abb0895 commit b3a6427

File tree

4 files changed

+122
-0
lines changed

4 files changed

+122
-0
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ jobs:
1717
script:
1818
- cargo build
1919
- cargo test
20+
- "ulimit -n 5000; cargo test timelimits:: -- --ignored --test-threads=1 --show-output"
2021
- cargo fmt -- --check
2122
after_failure:
2223
- wget https://raw.githubusercontent.com/DiscordHooks/travis-ci-discord-webhook/master/send.sh

Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ cidr-utils = "0.5.0"
3737
itertools = "0.9.0"
3838
trust-dns-resolver = { version = "0.19.5", features = ["dns-over-rustls"] }
3939

40+
[dev-dependencies]
41+
wait-timeout = "0.2"
42+
4043
[package.metadata.deb]
4144
depends = "$auto, nmap"
4245
section = "rust"

tests/timelimits.rs

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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

Comments
 (0)