More tests added #24

Merged
ddidderr merged 3 commits from count_tests into master 2022-05-08 19:18:04 +02:00
Showing only changes of commit d5de7a02d4 - Show all commits

View File

@ -229,135 +229,152 @@ pub enum DNSParseError {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; mod from_udp_datagram {
#[test] mod header_length {
fn parse_dns_header_too_short() { use super::super::super::*;
// minimal header with 1 byte missing
let dns_query = [
0x13, 0x37, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
];
let parse_result = DNSHeader::from_udp_datagram(&dns_query);
assert_eq!(parse_result.err().unwrap(), DNSParseError::DatagramTooShort);
}
#[test] #[test]
fn parse_dns_header_opcode_invalid() { fn too_short() {
let mut dns_query = [ // minimal header with 1 byte missing
0xff, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, let dns_query = [
]; 0x13, 0x37, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
let invalid_opcodes = [3, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; ];
let parse_result = DNSHeader::from_udp_datagram(&dns_query);
for opcode in invalid_opcodes { assert_eq!(parse_result.err().unwrap(), DNSParseError::DatagramTooShort);
dns_query[2] = opcode << 3; }
let parse_result = DNSHeader::from_udp_datagram(&dns_query);
assert_eq!(
parse_result.err(),
Some(DNSParseError::DNSOpCodeInvalid),
"query: {:02x?}, opcode: {}",
dns_query,
opcode
);
} }
}
#[test] mod opcode {
fn parse_dns_header_opcode_valid() -> Result<(), DNSParseError> { use super::super::super::*;
let mut dns_query = [
0xff, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
];
let valid_opcodes = [
(0, DNSOpCode::Query),
(1, DNSOpCode::IQuery),
(2, DNSOpCode::Status),
(4, DNSOpCode::Notify),
(5, DNSOpCode::Update),
];
for (opcode, parsed_opcode) in valid_opcodes { #[test]
dns_query[2] = opcode << 3; fn invalid() {
let parse_result = DNSHeader::from_udp_datagram(&dns_query); let mut dns_query = [
assert_eq!( 0xff, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
parse_result?.opcode, parsed_opcode, ];
"query: {:02x?}, opcode: {}", let invalid_opcodes = [3, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
dns_query, opcode
); for opcode in invalid_opcodes {
dns_query[2] = opcode << 3;
let parse_result = DNSHeader::from_udp_datagram(&dns_query);
assert_eq!(
parse_result.err(),
Some(DNSParseError::DNSOpCodeInvalid),
"query: {:02x?}, opcode: {}",
dns_query,
opcode
);
}
}
#[test]
fn valid() -> Result<(), DNSParseError> {
let mut dns_query = [
0xff, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
];
let valid_opcodes = [
(0, DNSOpCode::Query),
(1, DNSOpCode::IQuery),
(2, DNSOpCode::Status),
(4, DNSOpCode::Notify),
(5, DNSOpCode::Update),
];
for (opcode, parsed_opcode) in valid_opcodes {
dns_query[2] = opcode << 3;
let parse_result = DNSHeader::from_udp_datagram(&dns_query);
assert_eq!(
parse_result?.opcode, parsed_opcode,
"query: {:02x?}, opcode: {}",
dns_query, opcode
);
}
Ok(())
}
} }
Ok(())
}
#[test] mod message_type {
fn parse_dns_header_message_type_query() -> Result<(), DNSParseError> { use super::super::super::*;
let dns_query = [
0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
];
let parse_result = DNSHeader::from_udp_datagram(&dns_query);
assert_eq!(parse_result?.message_type, DNSMessageType::Query);
Ok(())
}
#[test] #[test]
fn parse_dns_header_message_type_response() -> Result<(), DNSParseError> { fn query() -> Result<(), DNSParseError> {
let dns_query = [ let dns_query = [
0xff, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
]; ];
let parse_result = DNSHeader::from_udp_datagram(&dns_query); let parse_result = DNSHeader::from_udp_datagram(&dns_query);
assert_eq!(parse_result?.message_type, DNSMessageType::Response); assert_eq!(parse_result?.message_type, DNSMessageType::Query);
Ok(()) Ok(())
} }
#[test] #[test]
fn parse_dns_header_response_code_invalid() { fn response() -> Result<(), DNSParseError> {
let mut dns_query = [ let dns_query = [
0xff, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
]; ];
let invalid_rcodes = [11, 12, 13, 14, 15]; let parse_result = DNSHeader::from_udp_datagram(&dns_query);
assert_eq!(parse_result?.message_type, DNSMessageType::Response);
for rcode in invalid_rcodes { Ok(())
dns_query[3] = (rcode as u8) << 0; }
let parse_result = DNSHeader::from_udp_datagram(&dns_query);
assert_eq!(
parse_result,
Err(DNSParseError::DNSRCodeInvalid),
"query: {:02x?}, rcode: {}",
dns_query,
rcode,
);
} }
}
#[test] mod response_code {
fn parse_dns_header_response_code_valid() -> Result<(), DNSParseError> { use super::super::super::*;
let mut dns_query = [
0xff, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
];
// for now we only support RCodes 0 to 10 (inclusive) #[test]
let valid_rcodes = [ fn invalid() {
(0, DNSRCode::NoError), let mut dns_query = [
(1, DNSRCode::FormErr), 0xff, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
(2, DNSRCode::ServFail), ];
(3, DNSRCode::NXDomain), let invalid_rcodes = [11, 12, 13, 14, 15];
(4, DNSRCode::NotImp),
(5, DNSRCode::Refused),
(6, DNSRCode::YXDomain),
(7, DNSRCode::YXRRSet),
(8, DNSRCode::NXRRSet),
(9, DNSRCode::NotAuth),
(10, DNSRCode::NotZone),
];
for (rcode, parsed_rcode) in valid_rcodes { for rcode in invalid_rcodes {
dns_query[3] = rcode << 0; dns_query[3] = (rcode as u8) << 0;
let parse_result = DNSHeader::from_udp_datagram(&dns_query);
assert_eq!( let parse_result = DNSHeader::from_udp_datagram(&dns_query);
parse_result?.response_code, parsed_rcode,
"query: {:02x?}, rcode: {}, parsed_rcode: {:?}", assert_eq!(
dns_query, rcode, parsed_rcode parse_result,
); Err(DNSParseError::DNSRCodeInvalid),
"query: {:02x?}, rcode: {}",
dns_query,
rcode,
);
}
}
#[test]
fn valid() -> Result<(), DNSParseError> {
let mut dns_query = [
0xff, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
];
// for now we only support RCodes 0 to 10 (inclusive)
let valid_rcodes = [
(0, DNSRCode::NoError),
(1, DNSRCode::FormErr),
(2, DNSRCode::ServFail),
(3, DNSRCode::NXDomain),
(4, DNSRCode::NotImp),
(5, DNSRCode::Refused),
(6, DNSRCode::YXDomain),
(7, DNSRCode::YXRRSet),
(8, DNSRCode::NXRRSet),
(9, DNSRCode::NotAuth),
(10, DNSRCode::NotZone),
];
for (rcode, parsed_rcode) in valid_rcodes {
dns_query[3] = rcode << 0;
let parse_result = DNSHeader::from_udp_datagram(&dns_query);
assert_eq!(
parse_result?.response_code, parsed_rcode,
"query: {:02x?}, rcode: {}, parsed_rcode: {:?}",
dns_query, rcode, parsed_rcode
);
}
Ok(())
}
} }
Ok(())
} }
} }