use base64::DecodeError as Base64Error; use dns_stamp_parser::{DecodeError, DnsStamp}; #[test] fn parse_fail_one() { let stamp = "sdns://"; assert_eq!(DnsStamp::decode(stamp), Err(DecodeError::NotEnoughBytes)) } #[test] fn parse_fail_two() { let stamp = "sdns://>"; assert_eq!( stamp.parse::(), Err(DecodeError::Base64Error(Base64Error::InvalidByte(0, 62))) ) } #[test] fn parse_fail_three() { let stamp = "sdns://A"; assert_eq!( stamp.parse::(), Err(DecodeError::Base64Error(Base64Error::InvalidLength)) ) } #[test] fn decode_type() { let stamp = "sdns://DzA"; assert_eq!(DnsStamp::decode(stamp), Err(DecodeError::UnknownType(15))) } #[test] fn decode_uint64() { let stamp = "sdns://AA"; 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://AQcAAAAAAAAAETE1MS44MC4yMjIuNzk6NDQzIKnWMjpPJYAJJhl1FQLOIx4fdtned2yHxruyig7_2w5OIDIuZG5zY3J5cHQtY2VydC5vcGVubmljLmkycGQueHl6", "sdns://AQcAAAAAAAAAG1syMDAxOjQ3MDoxZjE1OmI4MDo6NTNdOjQ0M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ADDQ1Ljc2LjExMy4zMSA-GhoPbFPz6XpJLVcIS1uYBwWe4FerFQWHb9g_2j24OBBkb2guc2VieS5pbzo4NDQzCi9kbnMtcXVlcnk", "sdns://AQcAAAAAAAAADTEzOS45OS4yMjIuNzIgCwVoTw0L4dgal5LC1FbZUtHtLR_rVuV6rVnxO95e4GUbMi5kbnNjcnlwdC1jZXJ0LmRucy5zZWJ5Lmlv", "sdns://AgcAAAAAAAAADTEzOS45OS4yMjIuNzIgPhoaD2xT8-l6SS1XCEtbmAcFnuBXqxUFh2_YP9o9uDgRZG9oLTIuc2VieS5pbzo0NDMKL2Rucy1xdWVyeQ", ]; /// 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() { for stamp_1 in DNS_STAMPS.iter() { match dbg!(stamp_1.parse::()) { Ok(dns_stamp_1) => match dns_stamp_1.encode() { Ok(stamp_2) => match stamp_2.parse::() { 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); } } } }