From: Matt Corallo Date: Tue, 6 Feb 2024 05:46:47 +0000 (+0000) Subject: Correct TXT record sort order X-Git-Tag: v0.5.4~93 X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=5330d7651e4e1ea59085dacc19291aa94613cb5c;p=dnssec-prover Correct TXT record sort order --- diff --git a/src/query.rs b/src/query.rs index 967c840..548ff15 100644 --- a/src/query.rs +++ b/src/query.rs @@ -201,6 +201,23 @@ mod tests { use std::net::ToSocketAddrs; use std::time::SystemTime; + + #[test] + fn test_cloudflare_txt_query() { + let sockaddr = "8.8.8.8:53".to_socket_addrs().unwrap().next().unwrap(); + let query_name = "cloudflare.com.".try_into().unwrap(); + let proof = build_txt_proof(sockaddr, query_name).unwrap(); + + let mut rrs = parse_rr_stream(&proof).unwrap(); + rrs.shuffle(&mut rand::rngs::OsRng); + let verified_rrs = verify_rr_stream(&rrs).unwrap(); + assert!(verified_rrs.verified_rrs.len() > 1); + + let now = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs(); + assert!(verified_rrs.valid_from < now); + assert!(verified_rrs.expires > now); + } + #[test] fn test_txt_query() { let sockaddr = "8.8.8.8:53".to_socket_addrs().unwrap().next().unwrap(); diff --git a/src/rr.rs b/src/rr.rs index 002ea7e..3d1a6a8 100644 --- a/src/rr.rs +++ b/src/rr.rs @@ -7,6 +7,8 @@ use alloc::vec::Vec; use alloc::string::String; use alloc::borrow::ToOwned; +use core::cmp::Ordering; + use crate::ser::*; /// A valid domain name. @@ -155,7 +157,7 @@ impl Record for RR { } } -#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] // TODO: ord is wrong cause need to consider len first, maybe +#[derive(Debug, Clone, PartialEq, Eq, Ord)] /// A text resource record, containing arbitrary text data pub struct Txt { /// The name this record is at. @@ -166,6 +168,13 @@ pub struct Txt { /// is an arbitrary series of bytes here. pub data: Vec, } +impl PartialOrd for Txt { + fn partial_cmp(&self, o: &Txt) -> Option { + Some(self.name.cmp(&o.name) + .then_with(|| self.data.len().cmp(&o.data.len())) + .then_with(|| self.data.cmp(&o.data))) + } +} impl StaticRecord for Txt { const TYPE: u16 = 16; fn name(&self) -> &Name { &self.name }