Correct TXT record sort order
authorMatt Corallo <git@bluematt.me>
Tue, 6 Feb 2024 05:46:47 +0000 (05:46 +0000)
committerMatt Corallo <git@bluematt.me>
Tue, 6 Feb 2024 05:46:47 +0000 (05:46 +0000)
src/query.rs
src/rr.rs

index 967c840b83fae6985f97c4a65f1456a0743847d8..548ff153d0ce2e551e112131d8a02777be554e64 100644 (file)
@@ -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();
index 002ea7e7a60b0317f5ebb84ca3c590ad5a161804..3d1a6a8548ee01d9038001a02f2a1e3d34a46aa9 100644 (file)
--- 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<u8>,
 }
+impl PartialOrd for Txt {
+       fn partial_cmp(&self, o: &Txt) -> Option<Ordering> {
+               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 }