230 lines
7.2 KiB
Rust
230 lines
7.2 KiB
Rust
use std::fs;
|
|
use std::io::{BufRead, BufReader, Read, Write};
|
|
use std::net::{TcpListener, TcpStream};
|
|
use std::process::{Command, Stdio};
|
|
use std::time::{Duration, Instant};
|
|
|
|
use tempfile::TempDir;
|
|
|
|
struct ChildGuard(std::process::Child);
|
|
|
|
impl Drop for ChildGuard {
|
|
fn drop(&mut self) {
|
|
let _ = self.0.kill();
|
|
let _ = self.0.wait();
|
|
}
|
|
}
|
|
|
|
fn http_get(addr: &str, path: &str) -> (u16, String) {
|
|
let mut stream = TcpStream::connect(addr).expect("connect to server");
|
|
stream
|
|
.set_read_timeout(Some(Duration::from_secs(3)))
|
|
.expect("set read timeout");
|
|
let request = format!("GET {path} HTTP/1.1\r\nHost: {addr}\r\nConnection: close\r\n\r\n");
|
|
stream.write_all(request.as_bytes()).expect("write request");
|
|
let mut response = String::new();
|
|
stream.read_to_string(&mut response).expect("read response");
|
|
let status_line = response.lines().next().unwrap_or("");
|
|
let code: u16 = status_line
|
|
.split_whitespace()
|
|
.nth(1)
|
|
.unwrap_or("0")
|
|
.parse()
|
|
.unwrap_or(0);
|
|
let body = response.split("\r\n\r\n").nth(1).unwrap_or("").to_string();
|
|
(code, body)
|
|
}
|
|
|
|
#[test]
|
|
fn serve_ingests_sweeps_and_responds_to_api_and_dashboard() {
|
|
// 1. Start a target listener on loopback.
|
|
let target_listener = TcpListener::bind("127.0.0.1:0").expect("bind target listener");
|
|
let target_port = target_listener.local_addr().expect("target address").port();
|
|
std::thread::spawn(move || {
|
|
for stream in target_listener.incoming() {
|
|
if stream.is_err() {
|
|
break;
|
|
}
|
|
}
|
|
});
|
|
|
|
// 2. Setup workspace with config, sweeps dir, and data dir.
|
|
let workspace = TempDir::new().expect("temporary directory");
|
|
let config_path = workspace.path().join("xexeu.toml");
|
|
let sweeps_dir = workspace.path().join("sweeps");
|
|
let data_dir = workspace.path().join("data");
|
|
|
|
fs::write(
|
|
&config_path,
|
|
format!(
|
|
r#"
|
|
[escopo]
|
|
autorizado = ["127.0.0.1/32"]
|
|
responsavel = "teste"
|
|
|
|
[padrao]
|
|
cron = "@daily"
|
|
hosts_simultaneos = 2
|
|
|
|
[perfis.padrao]
|
|
tcp = "{target_port}"
|
|
|
|
[[grupo]]
|
|
nome = "local"
|
|
alvos = ["127.0.0.1/32"]
|
|
"#
|
|
),
|
|
)
|
|
.expect("write configuration");
|
|
|
|
// 3. Spawn `xexeu serve --bind 127.0.0.1:0 ...`.
|
|
let mut child = Command::new(env!("CARGO_BIN_EXE_xexeu"))
|
|
.args([
|
|
"serve",
|
|
"--bind",
|
|
"127.0.0.1:0",
|
|
"--config",
|
|
config_path.to_str().expect("config path"),
|
|
"--sweeps",
|
|
sweeps_dir.to_str().expect("sweeps path"),
|
|
"--data-dir",
|
|
data_dir.to_str().expect("data dir"),
|
|
])
|
|
.stdout(Stdio::piped())
|
|
.stderr(Stdio::inherit())
|
|
.spawn()
|
|
.expect("spawn xexeu serve");
|
|
|
|
let stdout = child.stdout.take().expect("take stdout");
|
|
let mut reader = BufReader::new(stdout);
|
|
let mut server_addr = None;
|
|
let deadline = Instant::now() + Duration::from_secs(5);
|
|
|
|
let mut line = String::new();
|
|
while Instant::now() < deadline {
|
|
line.clear();
|
|
match reader.read_line(&mut line) {
|
|
Ok(n) if n > 0 => {
|
|
if line.contains("listening on http://") {
|
|
let addr = line
|
|
.split("http://")
|
|
.nth(1)
|
|
.expect("server address part")
|
|
.trim();
|
|
server_addr = Some(addr.to_string());
|
|
break;
|
|
}
|
|
}
|
|
Ok(_) => std::thread::sleep(Duration::from_millis(50)),
|
|
Err(_) => break,
|
|
}
|
|
}
|
|
|
|
let _guard = ChildGuard(child);
|
|
let server_addr = server_addr.expect("server must print listening address");
|
|
|
|
// 4. Test /api/health
|
|
let (status, body) = http_get(&server_addr, "/api/health");
|
|
assert_eq!(status, 200);
|
|
assert!(body.contains(r#""status":"ok""#), "health body: {body}");
|
|
|
|
// 5. Test /api/status before sweep ingestion
|
|
let (status, body) = http_get(&server_addr, "/api/status");
|
|
assert_eq!(status, 200);
|
|
assert!(body.contains(r#""status":"ok""#), "status body: {body}");
|
|
|
|
// 6. Run scan --once to produce a sweep batch into sweeps_dir
|
|
let scan_output = Command::new(env!("CARGO_BIN_EXE_xexeu"))
|
|
.args([
|
|
"scan",
|
|
"--group",
|
|
"local",
|
|
"--once",
|
|
"--config",
|
|
config_path.to_str().expect("config path"),
|
|
"--out",
|
|
sweeps_dir.to_str().expect("sweeps path"),
|
|
])
|
|
.output()
|
|
.expect("run xexeu scan");
|
|
assert!(
|
|
scan_output.status.success(),
|
|
"scan failed: {}",
|
|
String::from_utf8_lossy(&scan_output.stderr)
|
|
);
|
|
|
|
// 7. Poll /api/hosts until the background poller ingests the sweep and exposes the host
|
|
let poll_deadline = Instant::now() + Duration::from_secs(6);
|
|
let mut host_found = false;
|
|
while Instant::now() < poll_deadline {
|
|
let (status, body) = http_get(&server_addr, "/api/hosts");
|
|
if status == 200 && body.contains("127.0.0.1") && body.contains(&target_port.to_string()) {
|
|
assert!(
|
|
body.contains(r#""status":"ativo""#),
|
|
"asset status in: {body}"
|
|
);
|
|
host_found = true;
|
|
break;
|
|
}
|
|
std::thread::sleep(Duration::from_millis(300));
|
|
}
|
|
assert!(
|
|
host_found,
|
|
"expected host and port to be ingested and visible in /api/hosts"
|
|
);
|
|
|
|
// 8. Verify /api/status now reflects the ingested sweep
|
|
let (status, body) = http_get(&server_addr, "/api/status");
|
|
assert_eq!(status, 200);
|
|
assert!(
|
|
body.contains(r#""sweeps_ingested":1"#),
|
|
"status sweeps_ingested: {body}"
|
|
);
|
|
|
|
// 9. Verify /api/changes?since=24h returns diff events
|
|
let (status, body) = http_get(&server_addr, "/api/changes?since=24h");
|
|
assert_eq!(status, 200);
|
|
assert!(
|
|
body.contains("host_novo") || body.contains("porta_nova"),
|
|
"changes events: {body}"
|
|
);
|
|
|
|
// 10. Verify / and /dashboard return HTML, and static assets are served
|
|
let (status, body) = http_get(&server_addr, "/");
|
|
assert_eq!(status, 200);
|
|
assert!(body.contains("Xexéu"), "dashboard html: {body}");
|
|
assert!(body.contains("app.css"), "dashboard html links css: {body}");
|
|
|
|
let (status, body) = http_get(&server_addr, "/dashboard");
|
|
assert_eq!(status, 200);
|
|
assert!(body.contains("Xexéu"), "dashboard html: {body}");
|
|
|
|
let (status, body) = http_get(&server_addr, "/app.css");
|
|
assert_eq!(status, 200);
|
|
assert!(
|
|
body.contains("--sev-critical"),
|
|
"app.css contains severity tokens"
|
|
);
|
|
|
|
let (status, body) = http_get(&server_addr, "/app.js");
|
|
assert_eq!(status, 200);
|
|
assert!(
|
|
body.contains("renderSeverityChart"),
|
|
"app.js contains chart logic"
|
|
);
|
|
|
|
let (status, body) = http_get(&server_addr, "/i18n.js");
|
|
assert_eq!(status, 200);
|
|
assert!(
|
|
body.contains("export function tr"),
|
|
"i18n.js contains tr function"
|
|
);
|
|
|
|
let (status, body) = http_get(&server_addr, "/i18n-en.js");
|
|
assert_eq!(status, 200);
|
|
assert!(
|
|
body.contains("export const EN"),
|
|
"i18n-en.js contains dictionary"
|
|
);
|
|
}
|