diff --git a/src/resolver_cache.rs b/src/resolver_cache.rs index 7ea5f3eb..95f97bc4 100644 --- a/src/resolver_cache.rs +++ b/src/resolver_cache.rs @@ -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); + } } \ No newline at end of file