Browse Source

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

preview
Stefan Bühler 2 years ago
parent
commit
eeb415b3ae
  1. 37
      lib/dnsbox-base/src/records/tests/mod.rs

37
lib/dnsbox-base/src/records/tests/mod.rs

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

Loading…
Cancel
Save