From 7a52bcb729d8186f5dec9dbd311a727fa9576c8a Mon Sep 17 00:00:00 2001 From: valesteban Date: Wed, 8 Nov 2023 15:10:17 -0300 Subject: [PATCH] fix test lookup and async resolver --- src/resolver/async_resolver.rs | 89 ++++++++++++--------------- src/resolver/lookup.rs | 106 +++++++++++++++++---------------- 2 files changed, 94 insertions(+), 101 deletions(-) diff --git a/src/resolver/async_resolver.rs b/src/resolver/async_resolver.rs index c1d8607a..483bb523 100644 --- a/src/resolver/async_resolver.rs +++ b/src/resolver/async_resolver.rs @@ -153,9 +153,11 @@ impl AsyncResolver { #[cfg(test)] mod async_resolver_test { use crate::client::config::TIMEOUT; + use crate:: message::type_qtype::Qtype; use crate::resolver::config::ResolverConfig; use super::AsyncResolver; use std::time::Duration; + use crate::domain_name::DomainName; #[test] fn create_async_resolver() { @@ -165,45 +167,29 @@ mod async_resolver_test { assert_eq!(resolver.config.get_timeout(), Duration::from_secs(TIMEOUT)); } - //TODO: test inner_lookup - // #[tokio::test] - // async fn inner_lookup() { - // // Create a new resolver with default values - // let resolver = AsyncResolver::new(ResolverConfig::default()); - // let domain_name = DomainName::new_from_string("example.com".to_string()); - // let response = resolver.inner_lookup(domain_name).await; - // assert!(response.is_ok()); - // } - - #[ignore] - #[tokio::test] //TODO - async fn lookup_ip() { - - let mut resolver = AsyncResolver::new(ResolverConfig::default()); - - //let runtime = Runtime::new().unwrap(); - let response = resolver.lookup_ip("example.com", "UDP"); - - println!("[TEST FINISH=> {}]",response.await.unwrap()); - // TODO: add assert test Ip example.com - - //let response = runtime.block_on(resolver.lookup_ip("niclabs.cl","TCP")); - - // TODO: add assert test ip niclabs.cl - - } - - #[ignore] - #[tokio::test] //TODO - async fn lookupip_example() { - println!("[TEST INIT]"); - - let mut resolver = AsyncResolver::new(ResolverConfig::default()); - - let response = resolver.lookup_ip("example.com", "UDP").await.unwrap(); + #[tokio::test] + async fn inner_lookup() { + // Create a new resolver with default values + let resolver = AsyncResolver::new(ResolverConfig::default()); + let domain_name = DomainName::new_from_string("example.com".to_string()); + let qtype = Qtype::A; + let response = resolver.inner_lookup(domain_name,qtype).await; + + //FIXME: add assert + assert!(response.is_ok()); + } - println!("[TEST FINISH=> {}]",response); - + #[tokio::test] + async fn inner_lookup_ns() { + // Create a new resolver with default values + let resolver = AsyncResolver::new(ResolverConfig::default()); + let domain_name = DomainName::new_from_string("example.com".to_string()); + let qtype = Qtype::NS; + let response = resolver.inner_lookup(domain_name,qtype).await; + assert!(response.is_ok()); + + //FIXME: add assert + println!("Response: {:?}",response); } #[ignore] @@ -219,6 +205,20 @@ mod async_resolver_test { assert!(!ip_address.is_unspecified()); } + #[ignore] + #[tokio::test] + async fn lookup_ns() { + let mut resolver = AsyncResolver::new(ResolverConfig::default()); + let domain_name = "example.com"; + let transport_protocol = "UDP"; + let qtype = "NS"; + let response = resolver.lookup(domain_name, transport_protocol,qtype).await.unwrap(); + + println!("RESPONSE : {}",response); + } + + + // async fn reverse_query() { // let resolver = AsyncResolver::new(ResolverConfig::default()); // let ip_address = "192.168.0.1"; @@ -281,18 +281,5 @@ mod async_resolver_test { //TODO: prbar diferentes qtype - #[ignore] - #[tokio::test] - async fn lookup_ns() { - let mut resolver = AsyncResolver::new(ResolverConfig::default()); - let domain_name = "example.com"; - let transport_protocol = "UDP"; - let qtype = "NS"; - let ip_address = resolver.lookup(domain_name, transport_protocol,qtype).await.unwrap(); - - // assert!(ip_address.is_ipv4()); - - // assert!(!ip_address.is_unspecified()); - } } \ No newline at end of file diff --git a/src/resolver/lookup.rs b/src/resolver/lookup.rs index 6b47ae8b..bd1f9e0c 100644 --- a/src/resolver/lookup.rs +++ b/src/resolver/lookup.rs @@ -198,6 +198,7 @@ mod async_resolver_test { use crate::message::rdata::Rdata; use crate::{ domain_name::DomainName, dns_cache::DnsCache}; use std::net::{IpAddr, Ipv4Addr, SocketAddr}; + use std::time::Duration; use super::*; #[test] @@ -232,73 +233,78 @@ mod async_resolver_test { // TODO: test poll (not shure) - // #[ignore] - // #[tokio::test] - // async fn lookup_stub_max_tries(){ + #[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 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 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, 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); //FIXME: - // } + 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); //FIXME: + } - // #[tokio::test] - // async fn lookup_stub_response() { - // let domain_name = DomainName::new_from_string("example.com".to_string()); - // let cache = DnsCache::new(); - // let waker = None; + #[tokio::test] + async fn lookup_stub_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::Message("Empty")).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); + // Create vect of name servers + let google_server:IpAddr = IpAddr::V4(Ipv4Addr::new(8, 8, 8, 8)); + let timeout: Duration = Duration::from_secs(20); - // let conn_udp:ClientUDPConnection = ClientUDPConnection::new(google_server, timeout); - // let conn_tcp:ClientTCPConnection = ClientTCPConnection::new(google_server, timeout); + let conn_udp:ClientUDPConnection = ClientUDPConnection::new(google_server, timeout); + let conn_tcp:ClientTCPConnection = ClientTCPConnection::new(google_server, timeout); - // let config = ResolverConfig::default(); + let config = ResolverConfig::default(); + let record_type = Qtype::A; - // let name_servers = vec![(conn_udp,conn_tcp)]; - // lookup_stub(domain_name, cache, name_servers, waker,query,config).await; + let name_servers = vec![(conn_udp,conn_tcp)]; + lookup_stub(domain_name,record_type, cache, name_servers, waker,query,config).await; - // } + } - // #[tokio::test] - // async fn lookup_stub_ns_response() { - // let domain_name = DomainName::new_from_string("example.com".to_string()); - // let cache = DnsCache::new(); - // let waker = None; + #[tokio::test] + async fn lookup_stub_ns_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::Message("Empty")).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); + // Create vect of name servers + let google_server:IpAddr = IpAddr::V4(Ipv4Addr::new(8, 8, 8, 8)); + let timeout: Duration = Duration::from_secs(20); - // let conn_udp:ClientUDPConnection = ClientUDPConnection::new(google_server, timeout); - // let conn_tcp:ClientTCPConnection = ClientTCPConnection::new(google_server, timeout); + let conn_udp:ClientUDPConnection = ClientUDPConnection::new(google_server, timeout); + let conn_tcp:ClientTCPConnection = ClientTCPConnection::new(google_server, timeout); - // let config = ResolverConfig::default(); + let config = ResolverConfig::default(); + let record_type = Qtype::A; - // let name_servers = vec![(conn_udp,conn_tcp)]; - // lookup_stub(domain_name, cache, name_servers, waker,query,config).await; + let name_servers = vec![(conn_udp,conn_tcp)]; + lookup_stub(domain_name, record_type, cache, name_servers, waker,query,config).await; + + } - // } + //TODO: lookup_stub para diferentes qtype } \ No newline at end of file