84 lines
2.2 KiB
Rust
84 lines
2.2 KiB
Rust
use std::fs;
|
|
use std::net::TcpListener;
|
|
use std::process::Command;
|
|
|
|
use tempfile::TempDir;
|
|
use xexeu_core::batch::{SweepBatch, SweepStatus};
|
|
|
|
#[test]
|
|
fn scan_once_writes_a_complete_batch_with_the_open_port() {
|
|
let listener = TcpListener::bind("127.0.0.1:0").expect("bind loopback listener");
|
|
let port = listener.local_addr().expect("listener address").port();
|
|
std::thread::spawn(move || {
|
|
for stream in listener.incoming() {
|
|
if stream.is_err() {
|
|
break;
|
|
}
|
|
}
|
|
});
|
|
|
|
let workspace = TempDir::new().expect("temporary directory");
|
|
let config = workspace.path().join("xexeu.toml");
|
|
fs::write(
|
|
&config,
|
|
format!(
|
|
r#"
|
|
[escopo]
|
|
autorizado = ["127.0.0.1/32"]
|
|
responsavel = "teste"
|
|
|
|
[padrao]
|
|
cron = "@daily"
|
|
hosts_simultaneos = 2
|
|
|
|
[perfis.padrao]
|
|
tcp = "{port}"
|
|
|
|
[[grupo]]
|
|
nome = "local"
|
|
alvos = ["127.0.0.1/32"]
|
|
"#
|
|
),
|
|
)
|
|
.expect("write configuration");
|
|
|
|
let sweeps = workspace.path().join("sweeps");
|
|
let output = Command::new(env!("CARGO_BIN_EXE_xexeu"))
|
|
.args([
|
|
"scan",
|
|
"--group",
|
|
"local",
|
|
"--once",
|
|
"--config",
|
|
config.to_str().expect("config path"),
|
|
"--out",
|
|
sweeps.to_str().expect("sweeps path"),
|
|
])
|
|
.output()
|
|
.expect("run xexeu scan");
|
|
|
|
assert!(
|
|
output.status.success(),
|
|
"scan failed: {}",
|
|
String::from_utf8_lossy(&output.stderr)
|
|
);
|
|
let stdout = String::from_utf8_lossy(&output.stdout);
|
|
assert!(stdout.contains("vivos: 1"), "unexpected stdout: {stdout}");
|
|
|
|
let group_dir = sweeps.join("local");
|
|
let sweep_dir = fs::read_dir(&group_dir)
|
|
.expect("group directory")
|
|
.next()
|
|
.expect("one sweep")
|
|
.expect("sweep entry")
|
|
.path();
|
|
|
|
let batch = SweepBatch::read(&sweep_dir).expect("read batch");
|
|
assert_eq!(batch.manifest.status, SweepStatus::Complete);
|
|
assert_eq!(batch.manifest.group, "local");
|
|
assert_eq!(batch.alive_hosts().count(), 1);
|
|
|
|
let open: Vec<u16> = batch.open_ports("127.0.0.1".parse().expect("ip")).collect();
|
|
assert_eq!(open, vec![port]);
|
|
}
|