Skip to content

Commit

Permalink
Added: Basic TSIG integration test . Tested with a localhost UDP server
Browse files Browse the repository at this point in the history
  • Loading branch information
EstudianteGenerico047 committed Jun 6, 2024
1 parent d0bcda6 commit 5f751a5
Showing 1 changed file with 54 additions and 48 deletions.
102 changes: 54 additions & 48 deletions tests/integration_test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::{net::IpAddr, str::FromStr};
use dns_rust::{async_resolver::{config::ResolverConfig, AsyncResolver}, client::client_error::ClientError, domain_name::DomainName, message::{rdata::Rdata, resource_record::ResourceRecord},tsig};
use std::{net::IpAddr, str::FromStr, thread, net::UdpSocket, time::Duration};
use dns_rust::{async_resolver::{config::ResolverConfig, AsyncResolver}, client::client_error::ClientError, domain_name::DomainName, message::{rdata::Rdata,class_qclass::Qclass, type_qtype, resource_record::ResourceRecord, header::Header, DnsMessage},tsig::{self, TsigAlgorithm}};



Expand All @@ -13,6 +13,7 @@ async fn query_response(domain_name: &str, protocol: &str, qtype: &str) -> Resul
domain_name,
protocol,
qtype,

"IN").await;

response.map(|lookup_response| lookup_response.to_vec_of_rr())
Expand Down Expand Up @@ -108,61 +109,66 @@ async fn no_resource_available() {
println!("{:?}", response);
assert!(response.is_err());
}
/*


///RFC 8945 TSIG tests
#[tokio::test]
async fn tsig_signature() {
let sock = UdpSocket::bind("127.0.0.1:8001").expect("Puerto ocupado");
// global test variables
let key = b"1234567890";
let alg_name = TsigAlgorithm::HmacSha1;
let fudge = 0;
let time_signed = 0;
let id = 6502;
let mut dns_query_message =
DnsMessage::new_query_message(
DomainName::new_from_string("uchile.cl".to_string()),
Qtype::A,
DomainName::new_from_string("nictest.cl".to_string()),
type_qtype::Qtype::A,
Qclass::IN,
0,
false,
1);
let signature = dns_rust::tsig::sign_tsig();
let digest = keyed_hash(b"alalalalalalalalalalalalalalalal", &dns_query_message.to_bytes()[..]);
let digstr = digest.to_string();
let x = format!("hmac-md5.sig-alg.reg.int.\n51921\n1234\n32\n{}\n1234\n0\n0", digstr);
println!("El dig stirng es: {:#?}" , digstr);
let resource_record = TSigRdata::rr_from_master_file(
x.split_whitespace(),
56,
"IN",
String::from("uchile.cl"),
String::from("uchile.cl"));
let mut vec = vec![];
vec.push(resource_record);
dns_query_message.add_additionals(vec);
println!("{:#?}", dns_query_message);
id);
let q_for_mac = dns_query_message.clone();
//Lanzamiento de threads
//Se lanza el servidor. Recibe un mensaje sin firmar, lo firma y lo reenvía
fn host(){
println!("I am a host");
let udp_socket = UdpSocket::bind("127.0.0.1:8002").expect("Failed to bind to address");
let mut buf = [0; 512];

match udp_socket.recv_from(&mut buf) {

Ok((size, source)) => {
println!("Received {} bytes from {}", size, source);
let mut data = DnsMessage::from_bytes(&buf[0..size]).unwrap();
println!("The data is {:?}", data);
tsig::sign_tsig(&mut data, b"1234567890",TsigAlgorithm::HmacSha1,0,0);
let response = &DnsMessage::to_bytes(&data);
udp_socket
.send_to(&response, source)
.expect("Failed to send response");

}
Err(e) => {
eprintln!("Error receiving data: {}", e);

}
}

}
println!("Starting server");
let server_handle = thread::spawn(|| {
host();

});
thread::sleep(Duration::from_secs(2));
// se instancia un socket cliente que enviará y mensajes
let client_sock = UdpSocket::bind("127.0.0.1:8001").expect("Nothing");
let buf = dns_query_message.to_bytes();
client_sock.send_to(&buf,"127.0.0.1:8002").unwrap();
println!("Mensaje enviado");
server_handle.join().unwrap();

let s = sock.send_to(&buf, "127.0.0.1:8001").unwrap();
let response = query_response("example.com", "UDP", "MX").await;
if let Ok(rrs) = response {
assert_eq!(rrs.len(), 1);
if let Rdata::MX(mxdata) = rrs[0].get_rdata() {
assert_eq!(
mxdata.get_exchange(),
DomainName::new_from_str(""));

assert_eq!(
mxdata.get_preference(),
0
)
} else {
panic!("Record is not MX type");
}
}
}
*/

0 comments on commit 5f751a5

Please sign in to comment.