From ce12fddbe946cb7a608065fc7adae8c937629402 Mon Sep 17 00:00:00 2001 From: mice_on_drugs Date: Sun, 8 May 2022 19:18:04 +0200 Subject: [PATCH] (tests) count tests added (#24) Co-authored-by: Tobias Ottenweller Reviewed-on: https://git.comff.net/rustics/dns/pulls/24 Co-authored-by: mice_on_drugs Co-committed-by: mice_on_drugs --- src/proto.rs | 136 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) diff --git a/src/proto.rs b/src/proto.rs index ca6510a..f0c8f9b 100644 --- a/src/proto.rs +++ b/src/proto.rs @@ -376,5 +376,141 @@ mod tests { Ok(()) } } + + mod query_count { + use super::super::super::*; + + #[test] + fn zero() -> 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?.query_count, 0); + Ok(()) + } + + #[test] + fn one() -> Result<(), DNSParseError> { + let dns_query = [ + 0xff, 0x00, 0x80, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + ]; + let parse_result = DNSHeader::from_udp_datagram(&dns_query); + assert_eq!(parse_result?.query_count, 1); + Ok(()) + } + + #[test] + fn max_value() -> Result<(), DNSParseError> { + let dns_query = [ + 0xff, 0x00, 0x80, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + ]; + let parse_result = DNSHeader::from_udp_datagram(&dns_query); + assert_eq!(parse_result?.query_count, 65535); + Ok(()) + } + } + + mod answer_count { + use super::super::super::*; + + #[test] + fn zero() -> 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?.answer_count, 0); + Ok(()) + } + + #[test] + fn one() -> Result<(), DNSParseError> { + let dns_query = [ + 0xff, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, + ]; + let parse_result = DNSHeader::from_udp_datagram(&dns_query); + assert_eq!(parse_result?.answer_count, 1); + Ok(()) + } + + #[test] + fn max_value() -> Result<(), DNSParseError> { + let dns_query = [ + 0xff, 0x00, 0x80, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, + ]; + let parse_result = DNSHeader::from_udp_datagram(&dns_query); + assert_eq!(parse_result?.answer_count, 65535); + Ok(()) + } + } + + mod name_server_count { + use super::super::super::*; + + #[test] + fn zero() -> 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?.name_server_count, 0); + Ok(()) + } + + #[test] + fn one() -> Result<(), DNSParseError> { + let dns_query = [ + 0xff, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, + ]; + let parse_result = DNSHeader::from_udp_datagram(&dns_query); + assert_eq!(parse_result?.name_server_count, 1); + Ok(()) + } + + #[test] + fn max_value() -> Result<(), DNSParseError> { + let dns_query = [ + 0xff, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, + ]; + let parse_result = DNSHeader::from_udp_datagram(&dns_query); + assert_eq!(parse_result?.name_server_count, 65535); + Ok(()) + } + } + + mod additional_count { + use super::super::super::*; + + #[test] + fn zero() -> 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?.additional_count, 0); + Ok(()) + } + + #[test] + fn one() -> Result<(), DNSParseError> { + let dns_query = [ + 0xff, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, + ]; + let parse_result = DNSHeader::from_udp_datagram(&dns_query); + assert_eq!(parse_result?.additional_count, 1); + Ok(()) + } + + #[test] + fn max_value() -> Result<(), DNSParseError> { + let dns_query = [ + 0xff, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, + ]; + let parse_result = DNSHeader::from_udp_datagram(&dns_query); + assert_eq!(parse_result?.additional_count, 65535); + Ok(()) + } + } } }