group tests #23
249
src/proto.rs
249
src/proto.rs
@ -229,135 +229,152 @@ pub enum DNSParseError {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
use super::*;
|
||||
mod from_udp_datagram {
|
||||
|
||||
#[test]
|
||||
fn parse_dns_header_too_short() {
|
||||
// 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);
|
||||
}
|
||||
mod header_length {
|
||||
use super::super::super::*;
|
||||
|
||||
#[test]
|
||||
fn parse_dns_header_opcode_invalid() {
|
||||
let mut dns_query = [
|
||||
0xff, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
];
|
||||
let invalid_opcodes = [3, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
|
||||
|
||||
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 too_short() {
|
||||
// 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]
|
||||
fn parse_dns_header_opcode_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),
|
||||
];
|
||||
mod opcode {
|
||||
use super::super::super::*;
|
||||
|
||||
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
|
||||
);
|
||||
#[test]
|
||||
fn invalid() {
|
||||
let mut dns_query = [
|
||||
0xff, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
];
|
||||
let invalid_opcodes = [3, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
|
||||
|
||||
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]
|
||||
fn parse_dns_header_message_type_query() -> Result<(), DNSParseError> {
|
||||
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(())
|
||||
}
|
||||
mod message_type {
|
||||
use super::super::super::*;
|
||||
|
||||
#[test]
|
||||
fn parse_dns_header_message_type_response() -> Result<(), DNSParseError> {
|
||||
let dns_query = [
|
||||
0xff, 0x00, 0x80, 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::Response);
|
||||
Ok(())
|
||||
}
|
||||
#[test]
|
||||
fn query() -> Result<(), DNSParseError> {
|
||||
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]
|
||||
fn parse_dns_header_response_code_invalid() {
|
||||
let mut dns_query = [
|
||||
0xff, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
];
|
||||
let invalid_rcodes = [11, 12, 13, 14, 15];
|
||||
|
||||
for rcode in invalid_rcodes {
|
||||
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]
|
||||
fn response() -> Result<(), DNSParseError> {
|
||||
let dns_query = [
|
||||
0xff, 0x00, 0x80, 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::Response);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_dns_header_response_code_valid() -> Result<(), DNSParseError> {
|
||||
let mut dns_query = [
|
||||
0xff, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
];
|
||||
mod response_code {
|
||||
use super::super::super::*;
|
||||
|
||||
// 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),
|
||||
];
|
||||
#[test]
|
||||
fn invalid() {
|
||||
let mut dns_query = [
|
||||
0xff, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
];
|
||||
let invalid_rcodes = [11, 12, 13, 14, 15];
|
||||
|
||||
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
|
||||
);
|
||||
for rcode in invalid_rcodes {
|
||||
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]
|
||||
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(())
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user