Skip to content

Commit

Permalink
test: Write regression tests for parsing DNS records into NodeInfos
Browse files Browse the repository at this point in the history
  • Loading branch information
matheus23 committed Jan 8, 2025
1 parent c650ea8 commit 2f0beff
Showing 1 changed file with 85 additions and 2 deletions.
87 changes: 85 additions & 2 deletions iroh/src/dns/node_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,9 +405,14 @@ fn node_domain(node_id: &NodeId, origin: &str) -> Result<Name> {

#[cfg(test)]
mod tests {
use std::str::FromStr;
use std::{collections::BTreeSet, str::FromStr};

use iroh_base::SecretKey;
use hickory_resolver::{
proto::rr::{rdata::TXT, RData, Record},
Name,
};
use iroh_base::{NodeId, SecretKey};
use testresult::TestResult;

use super::NodeInfo;

Expand Down Expand Up @@ -438,4 +443,82 @@ mod tests {
let actual = NodeInfo::from_pkarr_signed_packet(&packet).unwrap();
assert_eq!(expected, actual);
}

#[test]
fn regression_record_parse_multiple_txt_entries() -> TestResult {
let name = Name::from_utf8(
"_iroh.dgjpkxyn3zyrk3zfads5duwdgbqpkwbjxfj4yt7rezidr3fijccy.dns.iroh.link.",
)?;
let test_records = [
Record::from_rdata(
name.clone(),
30,
RData::TXT(TXT::new(vec!["addr=192.168.96.145:60165".to_string()])),
),
Record::from_rdata(
name.clone(),
30,
RData::TXT(TXT::new(vec!["addr=213.208.157.87:60165".to_string()])),
),
Record::from_rdata(
name.clone(),
30,
RData::TXT(TXT::new(vec![
"relay=https://euw1-1.relay.iroh.network./".to_string()
])),
),
];

let node_info = NodeInfo::from_hickory_records(&test_records)?;

assert_eq!(
node_info,
NodeInfo {
node_id: NodeId::from_str(
"1992d53c02cdc04566e5c0edb1ce83305cd550297953a047a445ea3264b54b18"
)?,
relay_url: Some("https://euw1-1.relay.iroh.network./".parse()?),
direct_addresses: BTreeSet::from([
"192.168.96.145:60165".parse()?,
"213.208.157.87:60165".parse()?,
])
}
);

Ok(())
}

#[test]
fn regression_record_parse_bigger_txt_record() -> TestResult {
let name = Name::from_utf8(
"_iroh.dgjpkxyn3zyrk3zfads5duwdgbqpkwbjxfj4yt7rezidr3fijccy.dns.iroh.link.",
)?;
let test_records = [Record::from_rdata(
name,
30,
RData::TXT(TXT::new(vec![
"addr=192.168.96.145:60165".to_string(),
"addr=213.208.157.87:60165".to_string(),
"relay=https://euw1-1.relay.iroh.network./".to_string(),
])),
)];

let node_info = NodeInfo::from_hickory_records(&test_records)?;

assert_eq!(
node_info,
NodeInfo {
node_id: NodeId::from_str(
"1992d53c02cdc04566e5c0edb1ce83305cd550297953a047a445ea3264b54b18"
)?,
relay_url: Some("https://euw1-1.relay.iroh.network./".parse()?),
direct_addresses: BTreeSet::from([
"192.168.96.145:60165".parse()?,
"213.208.157.87:60165".parse()?,
])
}
);

Ok(())
}
}

0 comments on commit 2f0beff

Please sign in to comment.