From 5f751a5c3976071bcd1b3d9981ab996a938d6c71 Mon Sep 17 00:00:00 2001 From: Carlo Date: Thu, 6 Jun 2024 18:08:44 -0400 Subject: [PATCH] Added: Basic TSIG integration test . Tested with a localhost UDP server --- tests/integration_test.rs | 102 ++++++++++++++++++++------------------ 1 file changed, 54 insertions(+), 48 deletions(-) diff --git a/tests/integration_test.rs b/tests/integration_test.rs index 6746125d..97803302 100644 --- a/tests/integration_test.rs +++ b/tests/integration_test.rs @@ -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}}; @@ -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()) @@ -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"); - } - } } -*/ + \ No newline at end of file