diff --git a/src/client/tcp_connection.rs b/src/client/tcp_connection.rs index 102dbd0d..ae537398 100644 --- a/src/client/tcp_connection.rs +++ b/src/client/tcp_connection.rs @@ -126,7 +126,10 @@ mod tcp_connection_test{ use super::*; use std::net::{IpAddr,Ipv4Addr}; - + use crate::domain_name::DomainName; + use crate::message::type_qtype::Qtype; + use crate::message::class_qclass::Qclass; + #[test] fn create_tcp() { @@ -188,17 +191,42 @@ mod tcp_connection_test{ } #[test] - fn send() { - let ip_addr = IpAddr::V4(Ipv4Addr::new(192, 168, 0, 1)); + fn send_query_tcp(){ + + let ip_addr = IpAddr::V4(Ipv4Addr::new(8, 8, 8, 8)); let _port: u16 = 8088; - let timeout = Duration::from_secs(100); + let timeout = Duration::from_secs(2); let conn_new = ClientTCPConnection::new(ip_addr,timeout); - let dns_msg = DnsMessage::new(); - let response = conn_new.send(dns_msg); - println!("{:?}", response); + let domain_name: DomainName = DomainName::new_from_string("example.com".to_string()); + let dns_query = + DnsMessage::new_query_message( + domain_name, + Qtype::A, + Qclass::IN, + 0, + false, + 1); + let response = conn_new.send(dns_query); + + assert!(response.is_ok()); + assert!(response.unwrap().get_answer().len() > 0); + } + #[test] + fn send_timeout() { + let ip_addr = IpAddr::V4(Ipv4Addr::new(192, 168, 0, 1)); + let _port: u16 = 8088; + let timeout = Duration::from_secs(2); + + let conn_new = ClientTCPConnection::new(ip_addr,timeout); + let dns_query = DnsMessage::new(); + let response = conn_new.send(dns_query); + assert!(response.is_err()); } + + + } \ No newline at end of file diff --git a/src/client/udp_connection.rs b/src/client/udp_connection.rs index 3636cef4..ff68e890 100644 --- a/src/client/udp_connection.rs +++ b/src/client/udp_connection.rs @@ -182,5 +182,28 @@ mod udp_connection_test{ assert!(result.is_err()); } + #[test] + fn send_query_udp(){ + + let server_addr_non_existent = IpAddr::V4(Ipv4Addr::new(8,8 ,8, 8)); + let timeout = Duration::from_secs(2); + + let conn = ClientUDPConnection::new(server_addr_non_existent, timeout); + + let domain_name: DomainName = DomainName::new_from_string("example.com".to_string()); + let dns_query = + DnsMessage::new_query_message( + domain_name, + Qtype::A, + Qclass::IN, + 0, + false, + 1); + + let result = conn.send(dns_query); + + assert!(result.is_ok()); + assert!(result.unwrap().get_answer().len() > 0); + } } \ No newline at end of file