#[cfg(test)]
mod tests {
use crate::dns_key::DNSKey;
use crate::errors::DnsError;
use std::collections::HashMap;
#[test]
fn test_dns_key_equality() {
let key1 = DNSKey::new("example.com".to_string(), 1, 1, false).unwrap();
let key2 = DNSKey::new("example.com".to_string(), 1, 1, false).unwrap();
let key3 = DNSKey::new("example.org".to_string(), 1, 1, false).unwrap();
let key4 = DNSKey::new("example.com".to_string(), 2, 1, false).unwrap();
let key5 = DNSKey::new("example.com".to_string(), 1, 2, false).unwrap();
let key6 = DNSKey::new("example.com".to_string(), 1, 1, true).unwrap();
assert_eq!(key1, key2);
assert_ne!(key1, key3);
assert_ne!(key1, key4);
assert_ne!(key1, key5);
assert_ne!(key1, key6);
}
#[test]
fn test_dns_key_hash() {
let mut map = HashMap::new();
let key1 = DNSKey::new("example.com".to_string(), 1, 1, false).unwrap();
let key2 = DNSKey::new("example.com".to_string(), 1, 1, false).unwrap();
let key3 = DNSKey::new("example.org".to_string(), 1, 1, false).unwrap();
map.insert(key1, "value1");
assert_eq!(map.get(&key2), Some(&"value1"));
assert_eq!(map.get(&key3), None);
}
#[test]
fn test_normalize_name() {
let key1 = DNSKey::new("EXAMPLE.COM".to_string(), 1, 1, false).unwrap();
let key2 = DNSKey::new("example.com".to_string(), 1, 1, false).unwrap();
let key3 = DNSKey::new("example.com.".to_string(), 1, 1, false).unwrap();
assert_eq!(key1, key2);
assert_eq!(key1, key3);
assert_eq!(key2, key3);
assert_eq!(key1.name, "example.com");
assert_eq!(key2.name, "example.com");
assert_eq!(key3.name, "example.com");
assert_eq!(
DNSKey::normalize_name("example.com.").unwrap(),
"example.com"
);
assert_eq!(DNSKey::normalize_name(".").unwrap(), ".");
assert_eq!(DNSKey::normalize_name("").unwrap(), ".");
}
#[test]
fn test_normalize_name_errors() {
match DNSKey::normalize_name("example..com") {
Err(DnsError::InvalidDomainName(_)) => { }
other => panic!("Expected InvalidDomainName error, got {:?}", other),
}
match DNSKey::normalize_name("example...com") {
Err(DnsError::InvalidDomainName(_)) => { }
other => panic!("Expected InvalidDomainName error, got {:?}", other),
}
match DNSKey::normalize_name("...example...com...") {
Err(DnsError::InvalidDomainName(_)) => { }
other => panic!("Expected InvalidDomainName error, got {:?}", other),
}
match DNSKey::normalize_name(".example.com") {
Err(DnsError::InvalidDomainName(_)) => { }
other => panic!("Expected InvalidDomainName error, got {:?}", other),
}
match DNSKey::normalize_name("..example.com") {
Err(DnsError::InvalidDomainName(_)) => { }
other => panic!("Expected InvalidDomainName error, got {:?}", other),
}
}
#[test]
fn test_from_packet() {
let packet = create_dns_query_packet("example.com", 1, 1);
let key = DNSKey::from_packet(&packet).unwrap();
assert_eq!(key.name, "example.com");
assert_eq!(key.qtype, 1);
assert_eq!(key.qclass, 1);
assert!(!key.dnssec);
}
#[test]
fn test_new_with_invalid_name() {
let domain_with_consecutive_dots = "example..com".to_string();
match DNSKey::new(domain_with_consecutive_dots, 1, 1, false) {
Err(DnsError::InvalidDomainName(_)) => { }
other => panic!("Expected InvalidDomainName error, got {:?}", other),
}
}
fn create_dns_query_packet(domain: &str, qtype: u16, qclass: u16) -> Vec<u8> {
let mut packet = vec![
0x12, 0x34, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
];
let labels: Vec<&str> = domain.split('.').collect();
for label in labels {
packet.push(label.len() as u8);
for byte in label.bytes() {
packet.push(byte);
}
}
packet.push(0x00);
packet.push((qtype >> 8) as u8);
packet.push(qtype as u8);
packet.push((qclass >> 8) as u8);
packet.push(qclass as u8);
packet
}
}