idna/ascii_fast_path_wbtest.mbt1///|2/// A label the fast path always hands to the general algorithm.3fn has_ace_label(domain : String) -> Bool {4 domain.split(".").any(label => label.to_lower().has_prefix("xn--"))5}67///|8/// Compare the fast path with the general algorithm under every combination9/// of the flags it depends on. It may only answer when the general algorithm10/// succeeds with the same result, and it must answer for every valid ASCII11/// domain without A-labels.12fn check_fast_path(domain : String) -> Unit raise {13 let plain = domain.all(c => c.to_int() < 0x80) && !has_ace_label(domain)14 for use_std3_ascii_rules in [true, false] {15 for check_hyphens in [true, false] {16 for verify_dns_length in [true, false] {17 let fast = ascii_fast_path(18 domain,19 use_std3_ascii_rules~,20 check_hyphens~,21 verify_dns_length~,22 )23 let flags = "\{repr(domain)} std3=\{use_std3_ascii_rules} hyphens=\{check_hyphens} dns=\{verify_dns_length}"24 try25 to_ascii_general(26 domain,27 use_std3_ascii_rules~,28 check_hyphens~,29 check_bidi=true,30 check_joiners=true,31 check_idna2008=false,32 check_contexto=false,33 verify_dns_length~,34 )35 catch {36 _ =>37 if fast is Some(_) {38 fail("fast path accepted an invalid domain: \{flags}")39 }40 } noraise {41 expected =>42 if fast is Some(actual) {43 assert_eq(actual, expected)44 } else if plain {45 fail("fast path missed a valid domain: \{flags}")46 }47 }48 }49 let fast = ascii_fast_path(50 domain,51 use_std3_ascii_rules~,52 check_hyphens~,53 verify_dns_length=false,54 )55 let flags = "\{repr(domain)} std3=\{use_std3_ascii_rules} hyphens=\{check_hyphens}"56 try57 to_unicode_general(58 domain,59 use_std3_ascii_rules~,60 check_hyphens~,61 check_bidi=true,62 check_joiners=true,63 check_idna2008=false,64 check_contexto=false,65 )66 catch {67 _ =>68 if fast is Some(_) {69 fail("fast path accepted an invalid domain: \{flags}")70 }71 } noraise {72 expected =>73 if fast is Some(actual) {74 assert_eq(actual, expected)75 } else if plain {76 fail("fast path missed a valid domain: \{flags}")77 }78 }79 }80 }81}8283///|84test "ASCII fast path agrees with the general algorithm on short domains" {85 // Every string up to five characters over an alphabet that reaches each86 // branch: letters in both cases, a digit, hyphens (including "xn--"),87 // label separators and a non-LDH character.88 let alphabet = ['a', 'X', 'n', '1', '-', '.', '_']89 let mut domains = [""]90 for _ in 0..<=5 {91 let longer = []92 for domain in domains {93 check_fast_path(domain)94 for c in alphabet {95 longer.push("\{domain}\{c}")96 }97 }98 domains = longer99 }100}101102///|103test "ASCII fast path agrees with the general algorithm on every ASCII character" {104 for code in 0..<0x80 {105 let c = code.unsafe_to_char()106 for domain in ["\{c}", "\{c}a", "a\{c}", "a\{c}b", "ab\{c}\{c}c"] {107 check_fast_path(domain)108 }109 }110}111112///|113test "ASCII fast path agrees with the general algorithm at DNS length limits" {114 let label_63 = "a".repeat(63)115 let label_64 = "a".repeat(64)116 // Three 63-character labels and one of 61 make 253 characters with dots.117 let domain_253 = "\{label_63}.\{label_63}.\{label_63}.\{"b".repeat(61)}"118 for119 domain in [120 label_63,121 label_64,122 "\{label_63}.com",123 "\{label_64}.com",124 "www.\{label_64}",125 domain_253,126 "\{domain_253}c",127 "\{domain_253}.",128 "example.com.",129 "example.com..",130 ".example.com",131 "example..com",132 ] {133 check_fast_path(domain)134 }135}136137///|138test "ASCII fast path leaves other input to the general algorithm" {139 for140 domain in [141 "bücher.example", "example。com", "example.😀", "a\u{00AD}b.com", "xn--bcher-kva.example",142 "XN--BCHER-KVA.example", "www.xn--bcher-kva",143 ] {144 check_fast_path(domain)145 assert_true(146 ascii_fast_path(147 domain,148 use_std3_ascii_rules=false,149 check_hyphens=false,150 verify_dns_length=false,151 )152 is None,153 )154 }155}156157///|158test "ASCII fast path lowercases and keeps a trailing dot" {159 inspect(to_ascii("www.example.com"), content="www.example.com")160 inspect(to_ascii("WWW.Example.COM"), content="www.example.com")161 inspect(to_unicode("WWW.Example.COM."), content="www.example.com.")162 inspect(163 to_ascii("_dmarc.Example.com", use_std3_ascii_rules=false),164 content="_dmarc.example.com",165 )166}167
///|
/// A label the fast path always hands to the general algorithm.
fn has_ace_label(domain : String) -> Bool {
domain.split(".").any(label => label.to_lower().has_prefix("xn--"))
}
///|
/// Compare the fast path with the general algorithm under every combination
/// of the flags it depends on. It may only answer when the general algorithm
/// succeeds with the same result, and it must answer for every valid ASCII
/// domain without A-labels.
fn check_fast_path(domain : String) -> Unit raise {
let plain = domain.all(c => c.to_int() < 0x80) && !has_ace_label(domain)
for use_std3_ascii_rules in [true, false] {
for check_hyphens in [true, false] {
for verify_dns_length in [true, false] {
let fast = ascii_fast_path(
domain,
use_std3_ascii_rules~,
check_hyphens~,
verify_dns_length~,
)
let flags = "\{repr(domain)} std3=\{use_std3_ascii_rules} hyphens=\{check_hyphens} dns=\{verify_dns_length}"
try
to_ascii_general(
domain,
use_std3_ascii_rules~,
check_hyphens~,
check_bidi=true,
check_joiners=true,
check_idna2008=false,
check_contexto=false,
verify_dns_length~,
)
catch {
_ =>
if fast is Some(_) {
fail("fast path accepted an invalid domain: \{flags}")
}
} noraise {
expected =>
if fast is Some(actual) {
assert_eq(actual, expected)
} else if plain {
fail("fast path missed a valid domain: \{flags}")
}
}
}
let fast = ascii_fast_path(
domain,
use_std3_ascii_rules~,
check_hyphens~,
verify_dns_length=false,
)
let flags = "\{repr(domain)} std3=\{use_std3_ascii_rules} hyphens=\{check_hyphens}"
try
to_unicode_general(
domain,
use_std3_ascii_rules~,
check_hyphens~,
check_bidi=true,
check_joiners=true,
check_idna2008=false,
check_contexto=false,
)
catch {
_ =>
if fast is Some(_) {
fail("fast path accepted an invalid domain: \{flags}")
}
} noraise {
expected =>
if fast is Some(actual) {
assert_eq(actual, expected)
} else if plain {
fail("fast path missed a valid domain: \{flags}")
}
}
}
}
}
///|
test "ASCII fast path agrees with the general algorithm on short domains" {
// Every string up to five characters over an alphabet that reaches each
// branch: letters in both cases, a digit, hyphens (including "xn--"),
// label separators and a non-LDH character.
let alphabet = ['a', 'X', 'n', '1', '-', '.', '_']
let mut domains = [""]
for _ in 0..<=5 {
let longer = []
for domain in domains {
check_fast_path(domain)
for c in alphabet {
longer.push("\{domain}\{c}")
}
}
domains = longer
}
}
///|
test "ASCII fast path agrees with the general algorithm on every ASCII character" {
for code in 0..<0x80 {
let c = code.unsafe_to_char()
for domain in ["\{c}", "\{c}a", "a\{c}", "a\{c}b", "ab\{c}\{c}c"] {
check_fast_path(domain)
}
}
}
///|
test "ASCII fast path agrees with the general algorithm at DNS length limits" {
let label_63 = "a".repeat(63)
let label_64 = "a".repeat(64)
// Three 63-character labels and one of 61 make 253 characters with dots.
let domain_253 = "\{label_63}.\{label_63}.\{label_63}.\{"b".repeat(61)}"
for
domain in [
label_63,
label_64,
"\{label_63}.com",
"\{label_64}.com",
"www.\{label_64}",
domain_253,
"\{domain_253}c",
"\{domain_253}.",
"example.com.",
"example.com..",
".example.com",
"example..com",
] {
check_fast_path(domain)
}
}
///|
test "ASCII fast path leaves other input to the general algorithm" {
for
domain in [
"bücher.example", "example。com", "example.😀", "a\u{00AD}b.com", "xn--bcher-kva.example",
"XN--BCHER-KVA.example", "www.xn--bcher-kva",
] {
check_fast_path(domain)
assert_true(
ascii_fast_path(
domain,
use_std3_ascii_rules=false,
check_hyphens=false,
verify_dns_length=false,
)
is None,
)
}
}
///|
test "ASCII fast path lowercases and keeps a trailing dot" {
inspect(to_ascii("www.example.com"), content="www.example.com")
inspect(to_ascii("WWW.Example.COM"), content="www.example.com")
inspect(to_unicode("WWW.Example.COM."), content="www.example.com.")
inspect(
to_ascii("_dmarc.Example.com", use_std3_ascii_rules=false),
content="_dmarc.example.com",
)
}