]>
git.proxmox.com Git - rustc.git/blob - vendor/idna/tests/uts46.rs
1 // Copyright 2013-2014 The rust-url developers.
3 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6 // option. This file may not be copied, modified, or distributed
7 // except according to those terms.
9 use crate::test
::TestFn
;
14 pub fn collect_tests
<F
: FnMut(String
, TestFn
)>(add_test
: &mut F
) {
15 // https://www.unicode.org/Public/idna/13.0.0/IdnaTestV2.txt
16 for (i
, line
) in include_str
!("IdnaTestV2.txt").lines().enumerate() {
17 if line
.is_empty() || line
.starts_with('
#') {
22 let line
= match line
.find('
#') {
23 Some(index
) => &line
[0..index
],
27 let mut pieces
= line
.split('
;'
).map(|x
| x
.trim()).collect
::<Vec
<&str>>();
28 let source
= unescape(&pieces
.remove(0));
31 let mut to_unicode
= unescape(&pieces
.remove(0));
32 if to_unicode
.is_empty() {
33 to_unicode
= source
.clone();
35 let to_unicode_status
= status(pieces
.remove(0));
38 let to_ascii_n
= pieces
.remove(0);
39 let to_ascii_n
= if to_ascii_n
.is_empty() {
44 let to_ascii_n_status
= pieces
.remove(0);
45 let to_ascii_n_status
= if to_ascii_n_status
.is_empty() {
46 to_unicode_status
.clone()
48 status(to_ascii_n_status
)
52 let to_ascii_t
= pieces
.remove(0);
53 let to_ascii_t
= if to_ascii_t
.is_empty() {
58 let to_ascii_t_status
= pieces
.remove(0);
59 let to_ascii_t_status
= if to_ascii_t_status
.is_empty() {
60 to_ascii_n_status
.clone()
62 status(to_ascii_t_status
)
65 let test_name
= format
!("UTS #46 line {}", i
+ 1);
68 TestFn
::dyn_test_fn(move || {
69 let config
= idna
::Config
::default()
70 .use_std3_ascii_rules(true)
71 .verify_dns_length(true)
74 // http://unicode.org/reports/tr46/#Deviations
75 // applications that perform IDNA2008 lookup are not required to check
76 // for these contexts, so we skip all tests annotated with C*
78 // Everybody ignores V2
79 // https://github.com/servo/rust-url/pull/240
80 // https://github.com/whatwg/url/issues/53#issuecomment-181528158
81 // http://www.unicode.org/review/pri317/
83 // "The special error codes X3 and X4_2 are now returned where a toASCII error code
84 // was formerly being generated in toUnicode due to an empty label."
85 // This is not implemented yet, so we skip toUnicode X4_2 tests for now, too.
87 let (to_unicode_value
, to_unicode_result
) =
88 config
.transitional_processing(false).to_unicode(&source
);
89 let to_unicode_result
= to_unicode_result
.map(|()| to_unicode_value
);
92 (&to_unicode
, &to_unicode_status
),
94 |e
| e
.starts_with('C'
) || e
== "V2" || e
== "X4_2",
97 let to_ascii_n_result
= config
.transitional_processing(false).to_ascii(&source
);
100 (&to_ascii_n
, &to_ascii_n_status
),
102 |e
| e
.starts_with('C'
) || e
== "V2",
105 let to_ascii_t_result
= config
.transitional_processing(true).to_ascii(&source
);
108 (&to_ascii_t
, &to_ascii_t_status
),
110 |e
| e
.starts_with('C'
) || e
== "V2",
117 #[allow(clippy::redundant_clone)]
118 fn check
<F
>(source
: &str, expected
: (&str, &[&str]), actual
: Result
<String
, Errors
>, ignore
: F
)
122 if !expected
.1.is_empty
() {
123 if !expected
.1.iter
().copied().any(ignore
) {
124 let res
= actual
.ok();
128 "Expected error {:?}. result: {} | source: {}",
137 "Couldn't parse {} | error: {:?}",
139 actual
.err().unwrap(),
141 assert_eq
!(actual
.unwrap(), expected
.0, "source: {}", source
);
145 fn unescape(input
: &str) -> String
{
146 let mut output
= String
::new();
147 let mut chars
= input
.chars();
150 None
=> return output
,
153 match chars
.next().unwrap() {
154 '
\\'
=> output
.push('
\\'
),
156 let c1
= chars
.next().unwrap().to_digit(16).unwrap();
157 let c2
= chars
.next().unwrap().to_digit(16).unwrap();
158 let c3
= chars
.next().unwrap().to_digit(16).unwrap();
159 let c4
= chars
.next().unwrap().to_digit(16).unwrap();
160 match char::from_u32(((c1
* 16 + c2
) * 16 + c3
) * 16 + c4
) {
161 Some(c
) => output
.push(c
),
164 .push_str(&format
!("\\u{:X}{:X}{:X}{:X}", c1
, c2
, c3
, c4
));
168 _
=> panic
!("Invalid test data input"),
178 fn status(status
: &str) -> Vec
<&str> {
179 if status
.is_empty() || status
== "[]" {
183 let mut result
= status
.split(", ").collect
::<Vec
<_
>>();
184 assert
!(result
[0].starts_with('
['
));
185 result
[0] = &result
[0][1..];
187 let idx
= result
.len() - 1;
188 let last
= &mut result
[idx
];
189 assert
!(last
.ends_with('
]'
));
190 *last
= &last
[..last
.len() - 1];