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

220 lines
4.8 KiB
Rust

use bytes::Bytes;
use crate::ser::packet;
use crate::ser::packet::DnsPacketData;
use std::io::Cursor;
use crate::errors::*;
use super::{DnsName, DnsCompressedName, DnsLabelRef, DisplayLabels, DisplayLabelsOptions};
/*
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();
assert_eq!(
name.labels().count(),
label_count as usize
);
assert_eq!(
format!("{}", name),
txt
);
}
fn check_uncompressed_debug(bytes: &'static [u8], txt: &str) {
let name = de_uncompressed(bytes).unwrap();
assert_eq!(
format!("{:?}", name),
txt
);
}
#[test]
fn parse_and_display_name() {
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.""#,
);
}
#[test]
fn parse_and_reverse_name() {
let name = de_uncompressed(b"\x03www\x07example\x03com\x00").unwrap();
assert_eq!(
format!(
"{}",
DisplayLabels{
labels: name.labels().rev(),
options: DisplayLabelsOptions{
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();
assert_eq!(
format!("{}", name),
"www.example.com."
);
name.push_back(DnsLabelRef::new(b"org").unwrap()).unwrap();
assert_eq!(
format!("{}", name),
"www.example.com.org."
);
name.pop_front();
assert_eq!(
format!("{}", name),
"example.com.org."
);
name.push_front(DnsLabelRef::new(b"mx").unwrap()).unwrap();
assert_eq!(
format!("{}", name),
"mx.example.com.org."
);
// 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();
assert_eq!(
format!("{}", name),
"mx.example.com.org.com."
);
}
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 {
failure::bail!("data remaining: {}", c.remaining())
}
Ok(result)
}
fn check_compressed_display(bytes: &'static [u8], offset: usize, txt: &str, label_count: u8) {
let name = de_compressed(bytes, offset).unwrap();
assert_eq!(
name.labels().count(),
label_count as usize
);
assert_eq!(
format!("{}", name),
txt
);
}
fn check_compressed_debug(bytes: &'static [u8], offset: usize, txt: &str) {
let name = de_compressed(bytes, offset).unwrap();
assert_eq!(
format!("{:?}", name),
txt
);
}
#[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() {
check_compressed_display(
b"\x03com\x00\x07example\xc0\x00", 5,
"example.com.",
2,
);
check_compressed_display(
b"\x03com\x00\x07e!am.l\\\xc0\x00", 5,
"e\\033am\\.l\\\\.com.",
2,
);
check_compressed_debug(
b"\x03com\x00\x07e!am.l\\\xc0\x00", 5,
r#""e\\033am\\.l\\\\.com.""#,
);
check_compressed_display(
b"\x03com\x00\x07example\xc0\x00\x03www\xc0\x05", 15,
"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();
assert_eq!(
format!("{}", name),
"www.example.com."
);
name.push_back(DnsLabelRef::new(b"org").unwrap()).unwrap();
assert_eq!(
format!("{}", name),
"www.example.com.org."
);
name.pop_front();
assert_eq!(
format!("{}", name),
"example.com.org."
);
name.push_front(DnsLabelRef::new(b"mx").unwrap()).unwrap();
assert_eq!(
format!("{}", name),
"mx.example.com.org."
);
// 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();
assert_eq!(
format!("{}", name),
"mx.example.com.org.com."
);
}