1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
|
#[cfg(feature = "idna")]
use rspec::report::ExampleResult;
#[cfg(feature = "idna")]
use serde::Deserialize;
#[cfg(feature = "idna")]
#[derive(Debug, Deserialize)]
struct JsonSchemaTestSuite {
description: String,
schema: Format,
tests: Vec<Test>,
}
#[cfg(feature = "idna")]
#[derive(Debug, Deserialize)]
struct Format {
format: String,
}
#[cfg(feature = "idna")]
#[derive(Debug, Deserialize)]
struct Test {
description: String,
data: String,
valid: bool,
}
#[test]
fn addr_parsing() {
rspec::run(&rspec::given("a domain", (), |ctx| {
ctx.it("should allow non-fully qualified domain names", |_| {
assert!(addr::parse_domain_name("example.com").is_ok())
});
ctx.it("should allow fully qualified domain names", |_| {
assert!(addr::parse_domain_name("example.com.").is_ok())
});
ctx.it("should allow sub-domains", |_| {
let domain = addr::parse_domain_name("www.example.com.").unwrap();
assert_eq!(domain.suffix(), "com.");
assert_eq!(domain.root(), Some("example.com."));
assert_eq!(domain.prefix(), Some("www"));
});
ctx.it("should allow IDNs", |_| {
assert!(addr::parse_domain_name("københavn.eu").is_ok())
});
ctx.it("should not allow more than 1 trailing dot", |_| {
assert!(addr::parse_domain_name("example.com..").is_err());
});
ctx.it("should allow single-label domains", |_| {
let domains = vec![
// real TLDs
"com",
"saarland",
"museum.",
// non-existant TLDs
"localhost",
"madeup",
"with-dot.",
"y̆es",
"y̆",
"❤",
];
for domain in domains {
let name = addr::parse_domain_name(domain).unwrap();
assert_eq!(name.root(), None);
assert_eq!(name.suffix(), domain);
let name = addr::parse_dns_name(domain).unwrap();
assert_eq!(name.root(), None);
assert_eq!(name.suffix(), Some(domain));
}
});
ctx.it(
"should not have the same result with or without the trailing dot",
|_| {
assert_ne!(
addr::parse_domain_name("example.com.").unwrap(),
addr::parse_domain_name("example.com").unwrap()
);
},
);
ctx.it("should not have empty labels", |_| {
assert!(addr::parse_domain_name("exa..mple.com").is_err());
});
ctx.it("should not contain spaces", |_| {
assert!(addr::parse_domain_name("exa mple.com").is_err());
});
ctx.it("should not start with a dash", |_| {
assert!(addr::parse_domain_name("-example.com").is_err());
});
ctx.it("should not end with a dash", |_| {
assert!(addr::parse_domain_name("example-.com").is_err());
});
ctx.it("should not contain /", |_| {
assert!(addr::parse_domain_name("exa/mple.com").is_err());
});
ctx.it("should not have a label > 63 characters", |_| {
let mut too_long_domain = String::from("a");
for _ in 0..64 {
too_long_domain.push_str("a");
}
too_long_domain.push_str(".com");
assert!(addr::parse_domain_name(too_long_domain.as_str()).is_err());
});
ctx.it("should not be an IPv4 address", |_| {
assert!(addr::parse_domain_name("127.38.53.247").is_err());
});
ctx.it("should not be an IPv6 address", |_| {
assert!(addr::parse_domain_name("fd79:cdcb:38cc:9dd:f686:e06d:32f3:c123").is_err());
});
ctx.it(
"should allow numbers only labels that are not the tld",
|_| {
assert!(addr::parse_domain_name("127.com").is_ok());
},
);
ctx.it("should not allow number only tlds", |_| {
assert!(addr::parse_domain_name("example.127").is_err());
});
ctx.it("should not have more than 127 labels", |_| {
let mut too_many_labels_domain = String::from("a");
for _ in 0..126 {
too_many_labels_domain.push_str(".a");
}
too_many_labels_domain.push_str(".com");
assert!(addr::parse_domain_name(too_many_labels_domain.as_str()).is_err());
});
ctx.it("should not have more than 253 characters", |_| {
let mut too_many_chars_domain = String::from("aaaaa");
for _ in 0..50 {
too_many_chars_domain.push_str(".aaaaaa");
}
too_many_chars_domain.push_str(".com");
assert!(addr::parse_domain_name(too_many_chars_domain.as_str()).is_err());
});
ctx.it("should handle lifetimes correctly", |_| {
let input = "sub.example.com";
let root_domain = addr::parse_domain_name(input).unwrap().root().unwrap();
assert_eq!(root_domain, "example.com");
});
}));
#[cfg(all(feature = "idna", feature = "psl"))]
rspec::run(&rspec::given("the JSON schema test suite", (), |ctx| {
// The JSON schema test suite is downloaded from
// https://raw.githubusercontent.com/json-schema-org/JSON-Schema-Test-Suite/master/tests/draft7/optional/format/idn-hostname.json
let suites: Vec<JsonSchemaTestSuite> =
serde_json::from_slice(include_bytes!("idn-hostname.json")).unwrap();
for suite in suites {
// we only care about IDNs
if suite.schema.format == "idn-hostname" {
for Test {
valid,
description,
data,
} in suite.tests
{
let label = format!(
"{} {}",
if valid {
"validates"
} else {
"doesn't validate"
},
description
);
ctx.it(msg(label), move |_| {
if parse_domain_name(&data).is_ok() == valid {
ExampleResult::Success
} else {
let msg = format!(
"failed the test; `{}` {}",
data,
if valid {
"should be valid"
} else {
"shouldn't be valid"
}
);
ExampleResult::Failure(Some(msg))
}
});
}
}
}
}));
rspec::run(&rspec::given("a DNS name", (), |ctx| {
ctx.it("should allow extended characters", |_| {
let names = vec![
"example.com.",
"_tcp.example.com.",
"_telnet._tcp.example.com.",
"*.example.com.",
"!.example.com.",
"Elgato Ring Light A4EE",
"fc:fc:f2:a1:e1:51@fe80::ee2c:e2ff:fea1:e151",
];
for name in names {
assert!(addr::parse_dns_name(name).is_ok());
}
});
ctx.it(
"should allow extracting the correct root and suffix where possible",
|_| {
let names = vec![
("_tcp.example.com.", Some("example.com."), Some("com.")),
(
"_telnet._tcp.example.com.",
Some("example.com."),
Some("com."),
),
("example.com", Some("example.com"), Some("com")),
("example.com.com", Some("com.com"), Some("com")),
];
for (input, root, suffix) in names {
let name = addr::parse_dns_name(input).unwrap();
assert_eq!(name.root(), root);
assert_eq!(name.suffix(), suffix);
}
},
);
ctx.it("should not require a valid root domain", |_| {
let names = vec!["_tcp.com.", "_telnet._tcp.com.", "*.com.", "ex!mple.com."];
for name in names {
assert!(addr::parse_dns_name(name).is_ok());
}
});
ctx.it("should not allow more than 1 trailing dot", |_| {
assert!(addr::parse_dns_name("example.com..").is_err());
});
}));
rspec::run(&rspec::given("a parsed email", (), |ctx| {
ctx.it("should allow valid email addresses", |_| {
let emails = vec![
"prettyandsimple@example.com",
"prettyandsimple@1example.com",
"very.common@example.com",
"disposable.style.email.with+symbol@example.com",
"other.email-with-dash@example.com",
"x@example.com",
"example-indeed@strange-example.com",
"#!$%&'*+-/=?^_`{}|~@example.org",
"example@s.solutions",
#[cfg(any(feature = "net", feature = "std"))]
"user@[fd79:cdcb:38cc:9dd:f686:e06d:32f3:c123]",
#[cfg(any(feature = "net", feature = "std"))]
"user@[127.0.0.1]",
r#""Abc\@def"@example.com"#,
r#""Fred Bloggs"@example.com"#,
r#""Joe\\Blow"@example.com"#,
r#""Abc@def"@example.com"#,
r#"customer/department=shipping@example.com"#,
"$A12345@example.com",
"!def!xyz%abc@example.com",
"_somename@example.com",
];
for email in emails {
assert_eq!(addr::parse_email_address(email).unwrap().as_str(), email);
}
});
ctx.it("should reject invalid email addresses", |_| {
let emails = vec![
"Abc.example.com",
"A@b@c@example.com",
r#"a"b(c)d,e:f;g<h>i[j\k]l@example.com"#,
r#""just"not"right@example.com"#,
r#"this is"not\allowed@example.com"#,
r#"this\ still\"not\\allowed@example.com"#,
"1234567890123456789012345678901234567890123456789012345678901234+x@example.com",
"john..doe@example.com",
"john.doe@example..com",
" prettyandsimple@example.com",
"prettyandsimple@example.com ",
"@example.com",
];
for email in emails {
assert!(addr::parse_email_address(email).is_err(), "{}", email);
}
});
ctx.it("should allow parsing IDN email addresses", |_| {
let emails = vec![
r#"Pelé@example.com"#,
r#"δοκιμή@παράδειγμα.δοκιμή"#,
r#"我買@屋企.香港"#,
r#"甲斐@黒川.日本"#,
r#"二ノ宮@黒川.日本"#,
r#"чебурашка@ящик-с-апельсинами.рф"#,
r#"медведь@с-балалайкой.рф"#,
r#"संपर्क@डाटामेल.भारत"#,
r#"用户@例子.广告"#,
];
for email in emails {
assert!(addr::parse_email_address(email).is_ok(), "{}", email);
}
});
}));
}
// Converts a String to &'static str
//
// This will leak memory but that's OK for our testing purposes
#[cfg(feature = "idna")]
fn msg(s: String) -> &'static str {
unsafe {
let ret = std::mem::transmute(&s as &str);
std::mem::forget(s);
ret
}
}
|