Skip to content

Commit

Permalink
fix test lookup and async resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
valesteban committed Nov 8, 2023
1 parent 81845eb commit 7a52bcb
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 101 deletions.
89 changes: 38 additions & 51 deletions src/resolver/async_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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]
Expand All @@ -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";
Expand Down Expand Up @@ -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());
}

}
106 changes: 56 additions & 50 deletions src/resolver/lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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

}

0 comments on commit 7a52bcb

Please sign in to comment.