tests: use the registry to make sure it finds the correct type

This commit is contained in:
Stefan Bühler 2020-03-08 15:57:48 +01:00
parent abfd1e9b95
commit eeb415b3ae
1 changed files with 22 additions and 15 deletions

View File

@ -1,10 +1,10 @@
mod powerdns_tests; mod powerdns_tests;
use crate::common_types::classes; use crate::common_types::{classes, Type};
use crate::errors::*; use crate::errors::*;
use crate::records::structs; use crate::records::{structs, registry};
use crate::ser::packet::DnsPacketData; use crate::ser::packet::DnsPacketData;
use crate::ser::{packet, text, StaticRRData}; use crate::ser::{packet, text, StaticRRData, RRData};
use bytes::{Buf, Bytes}; use bytes::{Buf, Bytes};
use failure::ResultExt; use failure::ResultExt;
use std::fmt; use std::fmt;
@ -20,15 +20,22 @@ where
Ok(result) Ok(result)
} }
fn rrdata_parse<T>(data: &str) -> Result<T> fn rrdata_parse_type(q: Type, data: &str) -> Result<Box<dyn RRData>>
{
let mut ctx = text::DnsTextContext::new_root_origin();
ctx.set_zone_class(classes::IN);
ctx.set_record_type(q);
ctx.set_last_ttl(3600);
text::parse_with(data, |data| registry::parse_rr_data(&ctx, data))
}
fn rrdata_parse_struct<T>(data: &str) -> Result<T>
where where
T: StaticRRData, T: StaticRRData,
{ {
let mut ctx = text::DnsTextContext::new(); // make sure registry parses the correct type
ctx.set_zone_class(classes::IN); let result = rrdata_parse_type(T::TYPE, data)?.as_box_any();
ctx.set_record_type(T::TYPE); Ok(*result.downcast::<T>().expect("registry decoded different type"))
ctx.set_last_ttl(3600);
text::parse_with(data, |data| T::dns_parse_rr_data(&ctx, data))
} }
fn check<T>(txt: &str, data: &'static [u8]) -> Result<()> fn check<T>(txt: &str, data: &'static [u8]) -> Result<()>
@ -36,7 +43,7 @@ where
T: StaticRRData + fmt::Debug + PartialEq, T: StaticRRData + fmt::Debug + PartialEq,
{ {
let d1: T = rrdata_de(data).context("couldn't parse binary record")?; let d1: T = rrdata_de(data).context("couldn't parse binary record")?;
let d2: T = rrdata_parse(txt).context("couldn't parse text record")?; let d2: T = rrdata_parse_struct(txt).context("couldn't parse text record")?;
failure::ensure!(d1 == d2, "decoded data not equal: {:?} != {:?}", d1, d2); failure::ensure!(d1 == d2, "decoded data not equal: {:?} != {:?}", d1, d2);
Ok(()) Ok(())
} }
@ -46,7 +53,7 @@ where
T: StaticRRData + fmt::Debug + PartialEq, T: StaticRRData + fmt::Debug + PartialEq,
{ {
let d1: T = rrdata_de(data).context("couldn't parse binary record")?; let d1: T = rrdata_de(data).context("couldn't parse binary record")?;
let d2: T = rrdata_parse(txt).context("couldn't parse text record")?; let d2: T = rrdata_parse_struct(txt).context("couldn't parse text record")?;
failure::ensure!(d1 == d2, "decoded data not equal: {:?} != {:?}", d1, d2); failure::ensure!(d1 == d2, "decoded data not equal: {:?} != {:?}", d1, d2);
let d1_text = d1.text().unwrap(); let d1_text = d1.text().unwrap();
@ -100,7 +107,7 @@ where
s.push('a'); s.push('a');
} }
s.push('"'); s.push('"');
rrdata_parse::<T>(&s).unwrap_err(); rrdata_parse_struct::<T>(&s).unwrap_err();
} }
} }
@ -154,8 +161,8 @@ fn test_nsec3() {
.unwrap(); .unwrap();
// invalid base32 texts // invalid base32 texts
rrdata_parse::<structs::NSEC3>("1 2 300 - v").unwrap_err(); rrdata_parse_struct::<structs::NSEC3>("1 2 300 - v").unwrap_err();
rrdata_parse::<structs::NSEC3>("1 2 300 - vv").unwrap_err(); rrdata_parse_struct::<structs::NSEC3>("1 2 300 - vv").unwrap_err();
// invalid (empty) next-hashed values // invalid (empty) next-hashed values
packet::deserialize_with( packet::deserialize_with(
@ -170,7 +177,7 @@ fn test_nsec3param() {
check::<structs::NSEC3PARAM>("1 2 300 -", b"\x01\x02\x01\x2c\x00").unwrap(); check::<structs::NSEC3PARAM>("1 2 300 -", b"\x01\x02\x01\x2c\x00").unwrap();
check::<structs::NSEC3PARAM>("1 2 300 ab", b"\x01\x02\x01\x2c\x01\xab").unwrap(); check::<structs::NSEC3PARAM>("1 2 300 ab", b"\x01\x02\x01\x2c\x01\xab").unwrap();
// `salt` hex string must not contain spaces // `salt` hex string must not contain spaces
rrdata_parse::<structs::NSEC3PARAM>("1 2 300 a b").unwrap_err(); rrdata_parse_struct::<structs::NSEC3PARAM>("1 2 300 a b").unwrap_err();
} }
#[test] #[test]