use std::process::Command;
use std::net::UdpSocket;
use std::str;
#[test]
fn test_dns_server_startup() {
let output = Command::new("cargo")
.arg("run")
.arg("--")
.arg("--help")
.output()
.expect("Failed to execute command");
assert!(output.status.success());
assert!(str::from_utf8(&output.stdout).unwrap().contains("DNS Server"));
}
#[test]
fn test_dns_query() {
let mut server = Command::new("cargo")
.arg("run")
.spawn()
.expect("Failed to start DNS server");
std::thread::sleep(std::time::Duration::from_secs(2));
let socket = UdpSocket::bind("0.0.0.0:0").expect("Failed to bind UDP socket");
socket.connect("127.0.0.1:5300").expect("Failed to connect to DNS server");
let query = [
0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x03, 0x63, 0x6F, 0x6D, 0x00, 0x00, 0x01, 0x00, 0x01, ];
socket.send(&query).expect("Failed to send DNS query");
let mut buf = [0; 512];
let (amt, _) = socket.recv_from(&mut buf).expect("Failed to receive DNS response");
assert!(amt > 0);
let response = str::from_utf8(&buf[..amt]).unwrap();
assert!(response.contains("93.184.216.34"));
server.kill().expect("Failed to stop DNS server");
}
#[test]
fn test_add_record() {
let output = Command::new("cargo")
.arg("run")
.arg("--")
.arg("add")
.arg("test.com")
.arg("--type")
.arg("A")
.arg("--value")
.arg("1.2.3.4")
.output()
.expect("Failed to execute command");
assert!(output.status.success());
assert!(str::from_utf8(&output.stdout).unwrap().contains("Added record: test.com A 1.2.3.4"));
}