rust-dnsbox/lib/dnsbox-base/src/common_types/name/tests.rs

158 lines
4.7 KiB
Rust
Raw Normal View History

2020-03-07 15:57:47 +00:00
use crate::errors::*;
2019-07-01 15:43:34 +00:00
use crate::ser::packet;
use crate::ser::packet::DnsPacketData;
2020-03-07 15:57:47 +00:00
use bytes::Bytes;
2018-02-10 10:32:25 +00:00
use std::io::Cursor;
2020-03-07 15:57:47 +00:00
use super::{DisplayLabels, DisplayLabelsOptions, DnsCompressedName, DnsLabelRef, DnsName};
2018-02-10 10:32:25 +00:00
/*
fn deserialize(bytes: &'static [u8]) -> Result<DnsName> {
let result = packet::deserialize_with(Bytes::from_static(bytes), DnsName::deserialize)?;
{
let check_result = packet::deserialize_with(result.clone().encode(), DnsName::deserialize).unwrap();
assert_eq!(check_result, result);
}
Ok(result)
}
*/
fn de_uncompressed(bytes: &'static [u8]) -> Result<DnsName> {
let result = packet::deserialize_with(Bytes::from_static(bytes), DnsName::deserialize)?;
assert_eq!(bytes, result.clone().encode());
Ok(result)
}
fn check_uncompressed_display(bytes: &'static [u8], txt: &str, label_count: u8) {
let name = de_uncompressed(bytes).unwrap();
2020-03-07 15:57:47 +00:00
assert_eq!(name.labels().count(), label_count as usize);
assert_eq!(format!("{}", name), txt);
2018-02-10 10:32:25 +00:00
}
fn check_uncompressed_debug(bytes: &'static [u8], txt: &str) {
let name = de_uncompressed(bytes).unwrap();
2020-03-07 15:57:47 +00:00
assert_eq!(format!("{:?}", name), txt);
2018-02-10 10:32:25 +00:00
}
#[test]
fn parse_and_display_name() {
2020-03-07 15:57:47 +00:00
check_uncompressed_display(b"\x07example\x03com\x00", "example.com.", 2);
check_uncompressed_display(b"\x07e!am.l\\\x03com\x00", "e\\033am\\.l\\\\.com.", 2);
check_uncompressed_debug(b"\x07e!am.l\\\x03com\x00", r#""e\\033am\\.l\\\\.com.""#);
2018-02-10 10:32:25 +00:00
}
#[test]
fn parse_and_reverse_name() {
let name = de_uncompressed(b"\x03www\x07example\x03com\x00").unwrap();
assert_eq!(
format!(
"{}",
2020-03-07 15:57:47 +00:00
DisplayLabels {
2018-02-10 10:32:25 +00:00
labels: name.labels().rev(),
2020-03-07 15:57:47 +00:00
options: DisplayLabelsOptions {
2018-02-10 10:32:25 +00:00
separator: " ",
trailing: false,
},
}
),
"com example www"
);
}
#[test]
fn modifications() {
let mut name = de_uncompressed(b"\x07example\x03com\x00").unwrap();
name.push_front(DnsLabelRef::new(b"www").unwrap()).unwrap();
2020-03-07 15:57:47 +00:00
assert_eq!(format!("{}", name), "www.example.com.");
2018-02-10 10:32:25 +00:00
name.push_back(DnsLabelRef::new(b"org").unwrap()).unwrap();
2020-03-07 15:57:47 +00:00
assert_eq!(format!("{}", name), "www.example.com.org.");
2018-02-10 10:32:25 +00:00
name.pop_front();
2020-03-07 15:57:47 +00:00
assert_eq!(format!("{}", name), "example.com.org.");
2018-02-10 10:32:25 +00:00
name.push_front(DnsLabelRef::new(b"mx").unwrap()).unwrap();
2020-03-07 15:57:47 +00:00
assert_eq!(format!("{}", name), "mx.example.com.org.");
2018-02-10 10:32:25 +00:00
// the "mx" label should fit into the place "www" used before,
// make sure the buffer was reused and the name not moved within
assert_eq!(1, name.label_offsets.label_pos(0));
name.push_back(DnsLabelRef::new(b"com").unwrap()).unwrap();
2020-03-07 15:57:47 +00:00
assert_eq!(format!("{}", name), "mx.example.com.org.com.");
2018-02-10 10:32:25 +00:00
}
fn de_compressed(bytes: &'static [u8], offset: usize) -> Result<DnsCompressedName> {
use bytes::Buf;
let mut c = Cursor::new(Bytes::from_static(bytes));
c.set_position(offset as u64);
let result = DnsPacketData::deserialize(&mut c)?;
if c.remaining() != 0 {
2019-07-01 15:43:34 +00:00
failure::bail!("data remaining: {}", c.remaining())
2018-02-10 10:32:25 +00:00
}
Ok(result)
}
fn check_compressed_display(bytes: &'static [u8], offset: usize, txt: &str, label_count: u8) {
let name = de_compressed(bytes, offset).unwrap();
2020-03-07 15:57:47 +00:00
assert_eq!(name.labels().count(), label_count as usize);
assert_eq!(format!("{}", name), txt);
2018-02-10 10:32:25 +00:00
}
fn check_compressed_debug(bytes: &'static [u8], offset: usize, txt: &str) {
let name = de_compressed(bytes, offset).unwrap();
2020-03-07 15:57:47 +00:00
assert_eq!(format!("{:?}", name), txt);
2018-02-10 10:32:25 +00:00
}
#[test]
fn parse_invalid_compressed_name() {
de_compressed(b"\x11com\x00\x07example\xc0\x00", 5).unwrap_err();
de_compressed(b"\x10com\x00\x07example\xc0\x00", 5).unwrap_err();
}
#[test]
fn parse_and_display_compressed_name() {
2020-03-07 15:57:47 +00:00
check_compressed_display(b"\x03com\x00\x07example\xc0\x00", 5, "example.com.", 2);
2018-02-10 10:32:25 +00:00
check_compressed_display(
2020-03-07 15:57:47 +00:00
b"\x03com\x00\x07e!am.l\\\xc0\x00",
5,
2018-02-10 10:32:25 +00:00
"e\\033am\\.l\\\\.com.",
2,
);
check_compressed_debug(
2020-03-07 15:57:47 +00:00
b"\x03com\x00\x07e!am.l\\\xc0\x00",
5,
2018-02-10 10:32:25 +00:00
r#""e\\033am\\.l\\\\.com.""#,
);
check_compressed_display(
2020-03-07 15:57:47 +00:00
b"\x03com\x00\x07example\xc0\x00\x03www\xc0\x05",
15,
2018-02-10 10:32:25 +00:00
"www.example.com.",
3,
);
}
#[test]
fn modifications_compressed() {
let mut name = de_compressed(b"\x03com\x00\x07example\xc0\x00\xc0\x05", 15).unwrap();
name.push_front(DnsLabelRef::new(b"www").unwrap()).unwrap();
2020-03-07 15:57:47 +00:00
assert_eq!(format!("{}", name), "www.example.com.");
2018-02-10 10:32:25 +00:00
name.push_back(DnsLabelRef::new(b"org").unwrap()).unwrap();
2020-03-07 15:57:47 +00:00
assert_eq!(format!("{}", name), "www.example.com.org.");
2018-02-10 10:32:25 +00:00
name.pop_front();
2020-03-07 15:57:47 +00:00
assert_eq!(format!("{}", name), "example.com.org.");
2018-02-10 10:32:25 +00:00
name.push_front(DnsLabelRef::new(b"mx").unwrap()).unwrap();
2020-03-07 15:57:47 +00:00
assert_eq!(format!("{}", name), "mx.example.com.org.");
2018-02-10 10:32:25 +00:00
// the "mx" label should fit into the place "www" used before,
// make sure the buffer was reused and the name not moved within
assert_eq!(1, name.label_offsets.label_pos(0));
name.push_back(DnsLabelRef::new(b"com").unwrap()).unwrap();
2020-03-07 15:57:47 +00:00
assert_eq!(format!("{}", name), "mx.example.com.org.com.");
2018-02-10 10:32:25 +00:00
}