use clap::Parser;
use hickory_client::rr::RecordType;
use std::{net::IpAddr, str::FromStr};
#[derive(Parser, Debug, Clone, PartialEq)]
#[command(version, about)]
pub struct Args {
pub domain: String,
#[arg(short = 'c', long)]
pub no_positive_cache: bool,
#[arg(short = 'C', long)]
pub negative_cache: bool,
#[arg(short = 'e', long)]
pub no_edns0: bool,
#[arg(short = 'o', long)]
pub overview: bool,
#[arg(short = 'q', long, default_value = "A", value_parser = RecordType::from_str)]
pub query_type: RecordType,
#[arg(short = 'r', long, default_value = "3")]
pub retries: usize,
#[arg(short, long, default_value = ".")]
pub server: String,
#[arg(short = 't', long, default_value = "5")]
pub timeout: u64,
#[arg(short = 'S', long)]
pub source_address: Option<IpAddr>,
#[arg(short = '6', long)]
pub ipv6: bool,
#[arg(short = '4', long)]
pub ipv4: bool,
}
#[cfg(test)]
mod tests {
use super::*;
use clap::Parser;
use std::net::IpAddr;
#[test]
fn test_default_values() {
let args = Args::try_parse_from(&["test", "example.com"]).unwrap();
assert_eq!(args.domain, "example.com");
assert!(!args.no_positive_cache);
assert!(!args.negative_cache);
assert!(!args.no_edns0);
assert!(!args.overview);
assert_eq!(args.query_type, RecordType::A);
assert_eq!(args.retries, 3);
assert_eq!(args.server, ".");
assert_eq!(args.timeout, 5);
assert!(args.source_address.is_none());
assert!(!args.ipv6);
assert!(!args.ipv4);
}
#[test]
fn test_all_flags() {
let args = Args::try_parse_from(&[
"test",
"-c", "-C", "-e", "-o", "-q",
"NS", "-r",
"5", "-s",
"8.8.8.8", "-t",
"10", "-S",
"192.168.0.1", "-6", "example.com",
])
.unwrap();
assert!(args.no_positive_cache);
assert!(args.negative_cache);
assert!(args.no_edns0);
assert!(args.overview);
assert_eq!(args.query_type, RecordType::NS);
assert_eq!(args.retries, 5);
assert_eq!(args.server, "8.8.8.8");
assert_eq!(args.timeout, 10);
assert_eq!(
args.source_address,
Some(IpAddr::from_str("192.168.0.1").unwrap())
);
assert!(args.ipv6);
assert!(!args.ipv4);
}
#[test]
fn test_ipv4_flag() {
let args = Args::try_parse_from(&["test", "example.com", "-4"]).unwrap();
assert!(args.ipv4);
assert!(!args.ipv6);
}
#[test]
fn test_with_server_override() {
let args = Args::try_parse_from(&["test", "-s", "1.1.1.1", "example.com"]).unwrap();
assert_eq!(args.server, "1.1.1.1");
}
#[test]
fn test_with_query_type() {
let args = Args::try_parse_from(&["test", "example.com", "-q", "AAAA"]).unwrap();
assert_eq!(args.query_type, RecordType::AAAA);
}
#[test]
fn test_invalid_query_type() {
let result = Args::try_parse_from(&["test", "example.com", "-q", "INVALID"]);
assert!(result.is_err()); }
}