Skip to content

Commit

Permalink
test remove resolver cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Litr0 committed Jun 4, 2024
1 parent 6fafcfe commit 9019472
Showing 1 changed file with 213 additions and 0 deletions.
213 changes: 213 additions & 0 deletions src/resolver_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -707,4 +707,217 @@ mod resolver_cache_test{
assert_eq!(message.get_query_id(), 1);
assert_eq!(message.get_question().get_qname(), qname);
}

#[test]
fn remove_answer(){
let mut resolver_cache = ResolverCache::new(None);

let domain_name = DomainName::new_from_string("www.example.com".to_string());

let ip_address_1 = IpAddr::from([127, 0, 0, 0]);
let ip_address_2 = IpAddr::from([127, 0, 0, 1]);
let ip_address_3 = IpAddr::from([127, 0, 0, 2]);

let mut a_rdata_1 = ARdata::new();
let mut a_rdata_2 = ARdata::new();
let mut a_rdata_3 = ARdata::new();

a_rdata_1.set_address(ip_address_1);
a_rdata_2.set_address(ip_address_2);
a_rdata_3.set_address(ip_address_3);

let rdata_1 = Rdata::A(a_rdata_1);
let rdata_2 = Rdata::A(a_rdata_2);
let rdata_3 = Rdata::A(a_rdata_3);

let mut resource_record_1 = ResourceRecord::new(rdata_1);

resource_record_1.set_name(domain_name.clone());
resource_record_1.set_type_code(Rtype::A);
resource_record_1.set_ttl(1000);

let mut resource_record_2 = ResourceRecord::new(rdata_2);

resource_record_2.set_name(domain_name.clone());
resource_record_2.set_type_code(Rtype::A);
resource_record_2.set_ttl(1000);

let mut resource_record_3 = ResourceRecord::new(rdata_3);

resource_record_3.set_name(domain_name.clone());
resource_record_3.set_type_code(Rtype::A);
resource_record_3.set_ttl(1000);

resolver_cache.add_answer(domain_name.clone(), resource_record_1.clone(), Qtype::A, Qclass::IN, None);
resolver_cache.add_answer(domain_name.clone(), resource_record_2.clone(), Qtype::A, Qclass::IN, None);
resolver_cache.add_answer(domain_name.clone(), resource_record_3.clone(), Qtype::A, Qclass::IN, None);

resolver_cache.remove_answer(domain_name.clone(), Qtype::A, Qclass::IN);

let rr = resolver_cache.get_answer(domain_name.clone(), Qtype::A, Qclass::IN);

assert_eq!(rr, None);
}

#[test]
fn remove_authority(){
let mut resolver_cache = ResolverCache::new(None);

let domain_name = DomainName::new_from_string("www.example.com".to_string());

let ip_address_1 = IpAddr::from([127, 0, 0, 0]);
let ip_address_2 = IpAddr::from([127, 0, 0, 1]);
let ip_address_3 = IpAddr::from([127, 0, 0, 2]);

let mut a_rdata_1 = ARdata::new();
let mut a_rdata_2 = ARdata::new();
let mut a_rdata_3 = ARdata::new();

a_rdata_1.set_address(ip_address_1);
a_rdata_2.set_address(ip_address_2);
a_rdata_3.set_address(ip_address_3);

let rdata_1 = Rdata::A(a_rdata_1);
let rdata_2 = Rdata::A(a_rdata_2);
let rdata_3 = Rdata::A(a_rdata_3);

let mut resource_record_1 = ResourceRecord::new(rdata_1);

resource_record_1.set_name(domain_name.clone());
resource_record_1.set_type_code(Rtype::A);
resource_record_1.set_ttl(1000);

let mut resource_record_2 = ResourceRecord::new(rdata_2);

resource_record_2.set_name(domain_name.clone());
resource_record_2.set_type_code(Rtype::A);
resource_record_2.set_ttl(1000);

let mut resource_record_3 = ResourceRecord::new(rdata_3);

resource_record_3.set_name(domain_name.clone());
resource_record_3.set_type_code(Rtype::A);
resource_record_3.set_ttl(1000);

resolver_cache.add_authority(domain_name.clone(), resource_record_1.clone(), Qtype::A, Qclass::IN, None);
resolver_cache.add_authority(domain_name.clone(), resource_record_2.clone(), Qtype::A, Qclass::IN, None);
resolver_cache.add_authority(domain_name.clone(), resource_record_3.clone(), Qtype::A, Qclass::IN, None);

resolver_cache.remove_authority(domain_name.clone(), Qtype::A, Qclass::IN);

let rr = resolver_cache.get_authority(domain_name.clone(), Qtype::A, Qclass::IN);

assert_eq!(rr, None);
}

#[test]
fn remove_additional(){
let mut resolver_cache = ResolverCache::new(None);

let domain_name = DomainName::new_from_string("www.example.com".to_string());

let ip_address_1 = IpAddr::from([127, 0, 0, 0]);
let ip_address_2 = IpAddr::from([127, 0, 0, 1]);
let ip_address_3 = IpAddr::from([127, 0, 0, 2]);

let mut a_rdata_1 = ARdata::new();
let mut a_rdata_2 = ARdata::new();
let mut a_rdata_3 = ARdata::new();

a_rdata_1.set_address(ip_address_1);
a_rdata_2.set_address(ip_address_2);
a_rdata_3.set_address(ip_address_3);

let rdata_1 = Rdata::A(a_rdata_1);
let rdata_2 = Rdata::A(a_rdata_2);
let rdata_3 = Rdata::A(a_rdata_3);

let mut resource_record_1 = ResourceRecord::new(rdata_1);

resource_record_1.set_name(domain_name.clone());
resource_record_1.set_type_code(Rtype::A);
resource_record_1.set_ttl(1000);

let mut resource_record_2 = ResourceRecord::new(rdata_2);

resource_record_2.set_name(domain_name.clone());
resource_record_2.set_type_code(Rtype::A);
resource_record_2.set_ttl(1000);

let mut resource_record_3 = ResourceRecord::new(rdata_3);

resource_record_3.set_name(domain_name.clone());
resource_record_3.set_type_code(Rtype::A);
resource_record_3.set_ttl(1000);

resolver_cache.add_additional(domain_name.clone(), resource_record_1.clone(), Qtype::A, Qclass::IN, None);
resolver_cache.add_additional(domain_name.clone(), resource_record_2.clone(), Qtype::A, Qclass::IN, None);
resolver_cache.add_additional(domain_name.clone(), resource_record_3.clone(), Qtype::A, Qclass::IN, None);

resolver_cache.remove_additional(domain_name.clone(), Qtype::A, Qclass::IN);

let rr = resolver_cache.get_additional(domain_name.clone(), Qtype::A, Qclass::IN);

assert_eq!(rr, None);
}

#[test]
fn remove(){
let mut resolver_cache = ResolverCache::new(None);

let domain_name = DomainName::new_from_string("www.example.com".to_string());

let ip_address_1 = IpAddr::from([127, 0, 0, 0]);
let ip_address_2 = IpAddr::from([127, 0, 0, 1]);
let ip_address_3 = IpAddr::from([127, 0, 0, 2]);

let mut a_rdata_1 = ARdata::new();
let mut a_rdata_2 = ARdata::new();
let mut a_rdata_3 = ARdata::new();

a_rdata_1.set_address(ip_address_1);
a_rdata_2.set_address(ip_address_2);
a_rdata_3.set_address(ip_address_3);

let rdata_1 = Rdata::A(a_rdata_1);
let rdata_2 = Rdata::A(a_rdata_2);
let rdata_3 = Rdata::A(a_rdata_3);

let mut resource_record_1 = ResourceRecord::new(rdata_1);

resource_record_1.set_name(domain_name.clone());
resource_record_1.set_type_code(Rtype::A);
resource_record_1.set_ttl(1000);

let mut resource_record_2 = ResourceRecord::new(rdata_2);

resource_record_2.set_name(domain_name.clone());
resource_record_2.set_type_code(Rtype::A);
resource_record_2.set_ttl(1000);

let mut resource_record_3 = ResourceRecord::new(rdata_3);

resource_record_3.set_name(domain_name.clone());
resource_record_3.set_type_code(Rtype::A);
resource_record_3.set_ttl(1000);

resolver_cache.add_answer(domain_name.clone(), resource_record_1.clone(), Qtype::A, Qclass::IN, None);
resolver_cache.add_authority(domain_name.clone(), resource_record_2.clone(), Qtype::A, Qclass::IN, None);
resolver_cache.add_additional(domain_name.clone(), resource_record_3.clone(), Qtype::A, Qclass::IN, None);

let qname = DomainName::new_from_string("www.example.com".to_string());
let qtype = Qtype::A;
let qclass = Qclass::IN;
let op_code = 0;
let rd = true;
let id = 1;

let query = DnsMessage::new_query_message(qname.clone(), qtype.clone(), qclass.clone(), op_code.clone(), rd.clone(), id.clone());

resolver_cache.remove(domain_name.clone(), Qtype::A, Qclass::IN);

let message = resolver_cache.get(query);

assert_eq!(message, None);
}
}

0 comments on commit 9019472

Please sign in to comment.