diff --git a/src/resolver/lookup.rs b/src/resolver/lookup.rs index 09dcec6f..1b30cbc4 100644 --- a/src/resolver/lookup.rs +++ b/src/resolver/lookup.rs @@ -110,7 +110,7 @@ pub async fn lookup_stub( //FIXME: podemos ponerle de nombre lookup_strategy y waker: Option, referenced_query:Arc> + Send>>>>, config: ResolverConfig, -) { +) -> Result{ // Create random generator let mut rng = thread_rng(); @@ -143,6 +143,7 @@ pub async fn lookup_stub( //FIXME: podemos ponerle de nombre lookup_strategy y let mut response = new_query.clone().to_owned(); let mut new_header:Header = response.get_header(); new_header.set_rcode(2); + new_header.set_qr(true); response.set_header(new_header); let mut retry_count = 0; @@ -190,12 +191,14 @@ pub async fn lookup_stub( //FIXME: podemos ponerle de nombre lookup_strategy y } let mut future_query = referenced_query.lock().unwrap(); *future_query = future::ready(Ok(response.clone())).boxed(); + + Ok(response) } #[cfg(test)] mod async_resolver_test { // use tokio::runtime::Runtime; - use crate::message::rdata::a_rdata::ARdata; + use crate::message::rdata::{a_rdata::ARdata,ns_rdata::NsRdata}; use crate::message::rdata::Rdata; use crate::{ domain_name::DomainName, dns_cache::DnsCache}; use std::net::{IpAddr, Ipv4Addr, SocketAddr}; @@ -229,46 +232,16 @@ mod async_resolver_test { assert_eq!(lookup_future.config.get_addr(),SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 5333)); assert_eq!(lookup_future.cache.get_max_size(), 20); assert_eq!(lookup_future.cache.get_size(), 1); - - } - - // TODO: test poll (not shure) - - #[ignore] - #[tokio::test] - async fn lookup_stub_max_tries(){ - - let domain_name = DomainName::new_from_string("example.com".to_string()); - let timeout = Duration::from_secs(2); - let record_type = Qtype::A; - - let mut config: ResolverConfig = ResolverConfig::default(); - let non_existent_server:IpAddr = IpAddr::V4(Ipv4Addr::new(44, 44, 1, 81)); - let conn_udp:ClientUDPConnection = ClientUDPConnection::new(non_existent_server, timeout); - let conn_tcp:ClientTCPConnection = ClientTCPConnection::new(non_existent_server, timeout); - config.set_name_servers(vec![(conn_udp,conn_tcp)]); - config.set_retry(1); - let cache = DnsCache::new(); - - let response_future = LookupFutureStub::lookup(domain_name, record_type ,config, cache).await; - println!("response_future {:?}",response_future); - - assert_eq!(response_future.is_ok(), true); - // assert_eq!(response_future.unwrap().get_header().get_ancount(), 0); - assert_eq!(response_future.unwrap().get_header().get_rcode() , 2); - // assert_eq!(response_future.unwrap().get_header().get_rcode() , 2); //FIXME: } #[tokio::test] - async fn lookup_stub_response() { + async fn lookup_stub_a_response() { let domain_name = DomainName::new_from_string("example.com".to_string()); let cache = DnsCache::new(); let waker = None; - - let query = Arc::new(Mutex::new(future::err(ResolverError::Message("Empty")).boxed())); + let query = Arc::new(Mutex::new(future::err(ResolverError::EmptyQuery).boxed())); - // Create vect of name servers let google_server:IpAddr = IpAddr::V4(Ipv4Addr::new(8, 8, 8, 8)); let timeout: Duration = Duration::from_secs(20); @@ -279,8 +252,10 @@ mod async_resolver_test { let record_type = Qtype::A; let name_servers = vec![(conn_udp,conn_tcp)]; - lookup_stub(domain_name,record_type, cache, name_servers, waker,query,config).await; + let response = lookup_stub(domain_name,record_type, cache, name_servers, waker,query,config).await.unwrap(); + assert_eq!(response.get_header().get_qr(),true); + assert_ne!(response.get_answer().len(),0); } #[tokio::test] @@ -289,7 +264,7 @@ mod async_resolver_test { let cache = DnsCache::new(); let waker = None; - let query = Arc::new(Mutex::new(future::err(ResolverError::Message("Empty")).boxed())); + let query = Arc::new(Mutex::new(future::err(ResolverError::EmptyQuery).boxed())); // Create vect of name servers let google_server:IpAddr = IpAddr::V4(Ipv4Addr::new(8, 8, 8, 8)); @@ -299,13 +274,96 @@ mod async_resolver_test { let conn_tcp:ClientTCPConnection = ClientTCPConnection::new(google_server, timeout); let config = ResolverConfig::default(); - let record_type = Qtype::A; + let record_type = Qtype::NS; let name_servers = vec![(conn_udp,conn_tcp)]; - lookup_stub(domain_name, record_type, cache, name_servers, waker,query,config).await; + let response = lookup_stub(domain_name, record_type, cache, name_servers, waker,query,config).await.unwrap(); + assert_eq!(response.get_header().get_qr(),true); + assert_ne!(response.get_answer().len(),0); } - //TODO: lookup_stub para diferentes qtype + #[tokio::test] + async fn lookup_stub_max_tries(){ + + let domain_name = DomainName::new_from_string("example.com".to_string()); + let waker = None; + let query = Arc::new(Mutex::new(future::err(ResolverError::EmptyQuery).boxed())); + let timeout = Duration::from_secs(2); + let record_type = Qtype::A; + + let mut config: ResolverConfig = ResolverConfig::default(); + let non_existent_server:IpAddr = IpAddr::V4(Ipv4Addr::new(44, 44, 1, 81)); + config.set_retry(1); + let cache = DnsCache::new(); + + let conn_udp:ClientUDPConnection = ClientUDPConnection::new(non_existent_server, timeout); + let conn_tcp:ClientTCPConnection = ClientTCPConnection::new(non_existent_server, timeout); + config.set_name_servers(vec![(conn_udp,conn_tcp)]); + + let name_servers = vec![(conn_udp,conn_tcp)]; + let response = lookup_stub(domain_name, record_type, cache, name_servers, waker,query,config).await.unwrap(); + + println!("response_future {:?}",response); + + assert_eq!(response.get_header().get_ancount(), 0); + assert_eq!(response.get_header().get_rcode() , 2); + assert_eq!(response.get_header().get_qr(),true); + } + + #[ignore] + #[tokio::test] + async fn poll_lookup_a(){ + + let domain_name = DomainName::new_from_string("example.com".to_string()); + let timeout = Duration::from_secs(2); + let record_type = Qtype::A; + + let mut config: ResolverConfig = ResolverConfig::default(); + let non_existent_server:IpAddr = IpAddr::V4(Ipv4Addr::new(44, 44, 1, 81)); + + let conn_udp:ClientUDPConnection = ClientUDPConnection::new(non_existent_server, timeout); + let conn_tcp:ClientTCPConnection = ClientTCPConnection::new(non_existent_server, timeout); + config.set_name_servers(vec![(conn_udp,conn_tcp)]); + config.set_retry(3); + let cache = DnsCache::new(); + + let response_future = LookupFutureStub::lookup(domain_name, record_type ,config, cache).await; + println!("response_future {:?}",response_future); + + assert_eq!(response_future.is_ok(), true); + let response = response_future.unwrap(); + // assert_eq!(response_future.unwrap().get_header().get_ancount(), 0); + assert_eq!(response.get_header().get_rcode() , 2); + // assert_eq!(response_future.unwrap().get_header().get_rcode() , 2); //FIXME: + } + + #[ignore] + #[tokio::test] + async fn poll_lookup_max_tries(){ + + let domain_name = DomainName::new_from_string("example.com".to_string()); + let timeout = Duration::from_secs(2); + let record_type = Qtype::A; + + let mut config: ResolverConfig = ResolverConfig::default(); + let non_existent_server:IpAddr = IpAddr::V4(Ipv4Addr::new(44, 44, 1, 81)); + + let conn_udp:ClientUDPConnection = ClientUDPConnection::new(non_existent_server, timeout); + let conn_tcp:ClientTCPConnection = ClientTCPConnection::new(non_existent_server, timeout); + config.set_name_servers(vec![(conn_udp,conn_tcp)]); + config.set_retry(1); + let cache = DnsCache::new(); + + let response_future = LookupFutureStub::lookup(domain_name, record_type ,config, cache).await; + println!("response_future {:?}",response_future); + + assert_eq!(response_future.is_ok(), true); + // assert_eq!(response_future.unwrap().get_header().get_ancount(), 0); + assert_eq!(response_future.unwrap().get_header().get_rcode() , 2); + // assert_eq!(response_future.unwrap().get_header().get_rcode() , 2); //FIXME: + } + + //TODO: add cache test } \ No newline at end of file