Skip to content

Commit

Permalink
Merge pull request #4 from LinkTed/add-more-tests
Browse files Browse the repository at this point in the history
Add more tests
  • Loading branch information
LinkTed authored Dec 26, 2024
2 parents 6f358a4 + f82ca4f commit 6e15a07
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 168 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dns-stamp-parser"
version = "3.0.0"
version = "3.0.1"
authors = ["LinkTed <[email protected]>", "Evan Cameron <[email protected]>"]
license = "BSD-3-Clause"
edition = "2018"
Expand All @@ -14,7 +14,7 @@ categories = ["parser-implementations", "encoding"]
base64 = "0.22"
bitflags = "2"
thiserror = "1"
trust-dns-resolver = { version = "0.20", optional = true }
trust-dns-resolver = { version = "0.23", optional = true }

[features]
default = []
Expand Down
7 changes: 1 addition & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,12 +291,7 @@ fn bootstrap_hostname(addr: Option<Addr>, host: &str, bootstrap: &[IpAddr]) -> i
let mut config = ResolverConfig::new();
for ip in bootstrap {
let socket_addr = SocketAddr::new(*ip, 53);
config.add_name_server(NameServerConfig {
socket_addr,
protocol: Protocol::Udp,
tls_dns_name: None,
trust_nx_responses: false,
});
config.add_name_server(NameServerConfig::new(socket_addr, Protocol::Udp));
}

let resolver = Resolver::new(config, ResolverOpts::default()).unwrap();
Expand Down
24 changes: 4 additions & 20 deletions tests/ip_addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,9 @@ static DNS_STAMPS: [&str; 2] = [
#[test]
fn ip_addrs() {
for stamp_1 in DNS_STAMPS.iter() {
match DnsStamp::decode(stamp_1) {
Ok(dns_stamp_1) => match dns_stamp_1.encode() {
Ok(stamp_2) => match DnsStamp::decode(&stamp_2) {
Ok(dns_stamp_2) => {
if dns_stamp_1 != dns_stamp_2 {
panic!("Not equal: {} {}", stamp_1, stamp_2);
}
}
Err(e) => {
panic!("Decode 2: {:?}: {} {}", e, stamp_1, stamp_2);
}
},
Err(e) => {
panic!("Encode 1: {:?}: {}", e, stamp_1);
}
},
Err(e) => {
panic!("Decode 1: {:?}: {}", e, stamp_1);
}
}
let dns_stamp_1 = DnsStamp::decode(stamp_1).unwrap();
let stamp_2 = dns_stamp_1.encode().unwrap();
let dns_stamp_2 = DnsStamp::decode(&stamp_2).unwrap();
assert_eq!(dns_stamp_1, dns_stamp_2);
}
}
22 changes: 22 additions & 0 deletions tests/odoh-relays.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use dns_stamp_parser::DnsStamp;

// The list is from https://github.com/DNSCrypt/dnscrypt-resolvers/blob/master/v3/odoh-relays.md
static DNS_STAMPS: [&str; 4] = [
"sdns://hQcAAAAAAAAADDg5LjM4LjEzMS4zOAAYb2RvaC1ubC5hbGVrYmVyZy5uZXQ6NDQzBi9wcm94eQ",
"sdns://hQcAAAAAAAAAAAAab2RvaC1yZWxheS5lZGdlY29tcHV0ZS5hcHABLw",
"sdns://hQcAAAAAAAAADjIxMy4xOTYuMTkxLjk2ABhpYmtzdHVybS5zeW5vbG9neS5tZTo0NDMGL3Byb3h5",
"sdns://hQcAAAAAAAAADTQ1LjE1My4xODcuOTYAGG9kb2gtc2UuYWxla2JlcmcubmV0OjQ0MwYvcHJveHk",
];

/// Test all DNS Stamp from the [list] by decode and encode and decode it again.
///
/// [list]: https://github.com/DNSCrypt/dnscrypt-resolvers/blob/master/v3/odoh-relays.md
#[test]
fn relays() {
for stamp_1 in DNS_STAMPS.iter() {
let dns_stamp_1 = DnsStamp::decode(stamp_1).unwrap();
let stamp_2 = dns_stamp_1.encode().unwrap();
let dns_stamp_2 = DnsStamp::decode(&stamp_2).unwrap();
assert_eq!(dns_stamp_1, dns_stamp_2);
}
}
24 changes: 4 additions & 20 deletions tests/onion_services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,9 @@ static DNS_STAMPS: [&str; 1] = [
#[test]
fn parental_control() {
for stamp_1 in DNS_STAMPS.iter() {
match DnsStamp::decode(stamp_1) {
Ok(dns_stamp_1) => match dns_stamp_1.encode() {
Ok(stamp_2) => match DnsStamp::decode(&stamp_2) {
Ok(dns_stamp_2) => {
if dns_stamp_1 != dns_stamp_2 {
panic!("Not equal: {} {}", stamp_1, stamp_2);
}
}
Err(e) => {
panic!("Decode 2: {:?}: {} {}", e, stamp_1, stamp_2);
}
},
Err(e) => {
panic!("Encode 1: {:?}: {}", e, stamp_1);
}
},
Err(e) => {
panic!("Decode 1: {:?}: {}", e, stamp_1);
}
}
let dns_stamp_1 = DnsStamp::decode(stamp_1).unwrap();
let stamp_2 = dns_stamp_1.encode().unwrap();
let dns_stamp_2 = DnsStamp::decode(&stamp_2).unwrap();
assert_eq!(dns_stamp_1, dns_stamp_2);
}
}
24 changes: 4 additions & 20 deletions tests/opennic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,9 @@ static DNS_STAMPS: [&str; 7] = [
#[test]
fn opennic() {
for stamp_1 in DNS_STAMPS.iter() {
match DnsStamp::decode(stamp_1) {
Ok(dns_stamp_1) => match dns_stamp_1.encode() {
Ok(stamp_2) => match DnsStamp::decode(&stamp_2) {
Ok(dns_stamp_2) => {
if dns_stamp_1 != dns_stamp_2 {
panic!("Not equal: {} {}", stamp_1, stamp_2);
}
}
Err(e) => {
panic!("Decode 2: {:?}: {} {}", e, stamp_1, stamp_2);
}
},
Err(e) => {
panic!("Encode 1: {:?}: {}", e, stamp_1);
}
},
Err(e) => {
panic!("Decode 1: {:?}: {}", e, stamp_1);
}
}
let dns_stamp_1 = DnsStamp::decode(stamp_1).unwrap();
let stamp_2 = dns_stamp_1.encode().unwrap();
let dns_stamp_2 = DnsStamp::decode(&stamp_2).unwrap();
assert_eq!(dns_stamp_1, dns_stamp_2);
}
}
24 changes: 4 additions & 20 deletions tests/parental_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,9 @@ static DNS_STAMPS: [&str; 21] = [
#[test]
fn parental_control() {
for stamp_1 in DNS_STAMPS.iter() {
match DnsStamp::decode(stamp_1) {
Ok(dns_stamp_1) => match dns_stamp_1.encode() {
Ok(stamp_2) => match DnsStamp::decode(&stamp_2) {
Ok(dns_stamp_2) => {
if dns_stamp_1 != dns_stamp_2 {
panic!("Not equal: {} {}", stamp_1, stamp_2);
}
}
Err(e) => {
panic!("Decode 2: {:?}: {} {}", e, stamp_1, stamp_2);
}
},
Err(e) => {
panic!("Encode 1: {:?}: {}", e, stamp_1);
}
},
Err(e) => {
panic!("Decode 1: {:?}: {}", e, stamp_1);
}
}
let dns_stamp_1 = DnsStamp::decode(stamp_1).unwrap();
let stamp_2 = dns_stamp_1.encode().unwrap();
let dns_stamp_2 = DnsStamp::decode(&stamp_2).unwrap();
assert_eq!(dns_stamp_1, dns_stamp_2);
}
}
51 changes: 11 additions & 40 deletions tests/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,50 +39,21 @@ fn decode_uint64() {
assert_eq!(DnsStamp::decode(stamp), Err(DecodeError::NotEnoughBytes))
}

// The list is from https://github.com/DNSCrypt/dnscrypt-resolvers/blob/master/v2/public-resolvers.md
static DNS_STAMPS: [&str; 15] = [
"sdns://AgcAAAAAAAAAACA-GhoPbFPz6XpJLVcIS1uYBwWe4FerFQWHb9g_2j24OBRpYmtzdHVybS5zeW5vbG9neS5tZQovZG5zLXF1ZXJ5",
"sdns://AQcAAAAAAAAAEDg1LjUuOTMuMjMwOjg0NDMgwc9XUACwW8JsYh9ez5qiVgrOvwB-vss6f_SyDeC0Oe4YMi5kbnNjcnlwdC1jZXJ0Lmlia3N0dXJt",
"sdns://AQcAAAAAAAAALlsyYTAyOjEyMDU6NTA1NTpkZTYwOmIyNmU6YmZmZjpmZTFkOmUxOWJdOjg0NDMgwc9XUACwW8JsYh9ez5qiVgrOvwB-vss6f_SyDeC0Oe4YMi5kbnNjcnlwdC1jZXJ0Lmlia3N0dXJt",
"sdns://AQcAAAAAAAAAETE1MS44MC4yMjIuNzk6N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VnZ3MuY28",
"sdns://AQcAAAAAAAAADDQ1Ljc2LjExMy4zMSAIVGh4i6eKXqlF6o9Fg92cgD2WcDvKQJ7v_Wq4XrQsVhsyLmRuc2NyeXB0LWNlcnQuZG5zLnNlYnkuaW8",
"sdns://AgcAAAAAAAAADDQ1Ljc2LjExMy4zMSA-GhoPbFPz6XpJLVcIS1uYBwWe4FerFQWHb9g_2j24OBBkb2guc2VieS5pbzo4NDQzCi9kbnMtcXVlcnk",
"sdns://AQcAAAAAAAAADTEzOS45OS4yMjIuNzIgCwVoTw0L4dgal5LC1FbZUtHtLR_rVuV6rVnxO95e4GUbMi5kbnNjcnlwdC1jZXJ0LmRucy5zZWJ5Lmlv",
"sdns://AgcAAAAAAAAADTEzOS45OS4yMjIuNzIgPhoaD2xT8-l6SS1XCEtbmAcFnuBXqxUFh2_YP9o9uDgRZG9oLTIuc2VieS5pbzo0NDMKL2Rucy1xdWVyeQ",
// Self generated dns-stamp via https://dnscrypt.info/stamps.
static DNS_STAMPS: [&str; 4] = [
"sdns://AwcAAAAAAAAACTEyNy4wLjAuMQAObG9jYWxob3N0OjgwODA",
"sdns://BAcAAAAAAAAACTEyNy4wLjAuMQAObG9jYWxob3N0OjgwODA",
"sdns://BAcAAAAAAAAACTEyNy4wLjAuMQAJbG9jYWxob3N0",
"sdns://BQcAAAAAAAAACWxvY2FsaG9zdAovZG5zLXF1ZXJ5",
];

/// Test all DNS Stamp from the [list] by decode and encode and decode it again.
///
/// [list]: https://github.com/DNSCrypt/dnscrypt-resolvers/blob/master/v2/opennic.md
#[test]
fn opennic() {
fn decode_encode_decode() {
for stamp_1 in DNS_STAMPS.iter() {
match dbg!(stamp_1.parse::<DnsStamp>()) {
Ok(dns_stamp_1) => match dns_stamp_1.encode() {
Ok(stamp_2) => match stamp_2.parse::<DnsStamp>() {
Ok(dns_stamp_2) => {
if dns_stamp_1 != dns_stamp_2 {
panic!("Not equal: {} {}", stamp_1, stamp_2);
}
}
Err(e) => {
panic!("Decode 2: {:?}: {} {}", e, stamp_1, stamp_2);
}
},
Err(e) => {
panic!("Encode 1: {:?}: {}", e, stamp_1);
}
},
Err(e) => {
panic!("Decode 1: {:?}: {}", e, stamp_1);
}
}
let dns_stamp_1 = DnsStamp::decode(stamp_1).unwrap();
let stamp_2 = dns_stamp_1.encode().unwrap();
let dns_stamp_2 = DnsStamp::decode(&stamp_2).unwrap();
assert_eq!(dns_stamp_1, dns_stamp_2);
}
}
24 changes: 4 additions & 20 deletions tests/public_resolvers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,25 +257,9 @@ static DNS_STAMPS: [&str; 247] = [
#[test]
fn public_resolvers() {
for stamp_1 in DNS_STAMPS.iter() {
match DnsStamp::decode(stamp_1) {
Ok(dns_stamp_1) => match dns_stamp_1.encode() {
Ok(stamp_2) => match DnsStamp::decode(&stamp_2) {
Ok(dns_stamp_2) => {
if dns_stamp_1 != dns_stamp_2 {
panic!("Not equal: {} {}", stamp_1, stamp_2);
}
}
Err(e) => {
panic!("Decode 2: {:?}: {} {}", e, stamp_1, stamp_2);
}
},
Err(e) => {
panic!("Encode 1: {:?}: {}", e, stamp_1);
}
},
Err(e) => {
panic!("Decode 1: {:?}: {}", e, stamp_1);
}
}
let dns_stamp_1 = DnsStamp::decode(stamp_1).unwrap();
let stamp_2 = dns_stamp_1.encode().unwrap();
let dns_stamp_2 = DnsStamp::decode(&stamp_2).unwrap();
assert_eq!(dns_stamp_1, dns_stamp_2);
}
}
24 changes: 4 additions & 20 deletions tests/relays.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,9 @@ static DNS_STAMPS: [&str; 40] = [
#[test]
fn relays() {
for stamp_1 in DNS_STAMPS.iter() {
match DnsStamp::decode(stamp_1) {
Ok(dns_stamp_1) => match dns_stamp_1.encode() {
Ok(stamp_2) => match DnsStamp::decode(&stamp_2) {
Ok(dns_stamp_2) => {
if dns_stamp_1 != dns_stamp_2 {
panic!("Not equal: {} {}", stamp_1, stamp_2);
}
}
Err(e) => {
panic!("Decode 2: {:?}: {} {}", e, stamp_1, stamp_2);
}
},
Err(e) => {
panic!("Encode 1: {:?}: {}", e, stamp_1);
}
},
Err(e) => {
panic!("Decode 1: {:?}: {}", e, stamp_1);
}
}
let dns_stamp_1 = DnsStamp::decode(stamp_1).unwrap();
let stamp_2 = dns_stamp_1.encode().unwrap();
let dns_stamp_2 = DnsStamp::decode(&stamp_2).unwrap();
assert_eq!(dns_stamp_1, dns_stamp_2);
}
}

0 comments on commit 6e15a07

Please sign in to comment.