Debian Codegraph @

debian/missing-sources/idna/tests/deprecated.rs

raw
1 // Copyright 2013-2014 The rust-url developers.
2 //
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.
8
9 #![allow(clippy::assigning_clones)]
10 #![allow(deprecated)]
11
12 use crate::test::TestFn;
13 use std::char;
14 use std::fmt::Write;
15
16 use idna::Errors;
17
18 pub fn collect_tests<F: FnMut(String, TestFn)>(add_test: &mut F) {
19 // https://www.unicode.org/Public/idna/13.0.0/IdnaTestV2.txt
20 for (i, line) in include_str!("IdnaTestV2.txt").lines().enumerate() {
21 if line.is_empty() || line.starts_with('#') {
22 continue;
23 }
24
25 // Remove comments
26 let line = match line.find('#') {
27 Some(index) => &line[0..index],
28 None => line,
29 };
30
31 let mut pieces = line.split(';').map(|x| x.trim()).collect::<Vec<&str>>();
32 let source = unescape(pieces.remove(0));
33
34 // ToUnicode
35 let mut to_unicode = unescape(pieces.remove(0));
36 if to_unicode.is_empty() {
37 to_unicode = source.clone();
38 }
39 let to_unicode_status = status(pieces.remove(0));
40
41 // ToAsciiN
42 let to_ascii_n = pieces.remove(0);
43 let to_ascii_n = if to_ascii_n.is_empty() {
44 to_unicode.clone()
45 } else {
46 to_ascii_n.to_owned()
47 };
48 let to_ascii_n_status = pieces.remove(0);
49 let to_ascii_n_status = if to_ascii_n_status.is_empty() {
50 to_unicode_status.clone()
51 } else {
52 status(to_ascii_n_status)
53 };
54
55 // ToAsciiT
56 let to_ascii_t = pieces.remove(0);
57 let to_ascii_t = if to_ascii_t.is_empty() {
58 to_ascii_n.clone()
59 } else {
60 to_ascii_t.to_owned()
61 };
62 let to_ascii_t_status = pieces.remove(0);
63 let to_ascii_t_status = if to_ascii_t_status.is_empty() {
64 to_ascii_n_status.clone()
65 } else {
66 status(to_ascii_t_status)
67 };
68
69 let test_name = format!("UTS #46 (deprecated API) line {}", i + 1);
70 add_test(
71 test_name,
72 TestFn::DynTestFn(Box::new(move || {
73 let config = idna::Config::default()
74 .use_std3_ascii_rules(true)
75 .verify_dns_length(true)
76 .check_hyphens(true);
77
78 // http://unicode.org/reports/tr46/#Deviations
79 // applications that perform IDNA2008 lookup are not required to check
80 // for these contexts, so we skip all tests annotated with C*
81
82 // Everybody ignores V2
83 // https://github.com/servo/rust-url/pull/240
84 // https://github.com/whatwg/url/issues/53#issuecomment-181528158
85 // http://www.unicode.org/review/pri317/
86
87 // "The special error codes X3 and X4_2 are now returned where a toASCII error code
88 // was formerly being generated in toUnicode due to an empty label."
89 // This is not implemented yet, so we skip toUnicode X4_2 tests for now, too.
90
91 let (to_unicode_value, to_unicode_result) =
92 config.transitional_processing(false).to_unicode(&source);
93 let to_unicode_result = to_unicode_result.map(|()| to_unicode_value);
94 check(
95 &source,
96 (&to_unicode, &to_unicode_status),
97 to_unicode_result,
98 |e| e == "X4_2" || e == "V2",
99 );
100
101 let to_ascii_n_result = config.transitional_processing(false).to_ascii(&source);
102 check(
103 &source,
104 (&to_ascii_n, &to_ascii_n_status),
105 to_ascii_n_result,
106 |e| e == "V2" || e == "A4_2",
107 );
108
109 let to_ascii_t_result = config.transitional_processing(true).to_ascii(&source);
110 check(
111 &source,
112 (&to_ascii_t, &to_ascii_t_status),
113 to_ascii_t_result,
114 |e| e == "V2" || e == "A4_2",
115 );
116 })),
117 )
118 }
119 }
120
121 #[allow(clippy::redundant_clone)]
122 fn check<F>(source: &str, expected: (&str, &[&str]), actual: Result<String, Errors>, ignore: F)
123 where
124 F: Fn(&str) -> bool,
125 {
126 if !expected.1.is_empty() {
127 if !expected.1.iter().copied().any(ignore) {
128 let res = actual.ok();
129 assert_eq!(
130 res.clone(),
131 None,
132 "Expected error {:?}. result: {} | source: {}",
133 expected.1,
134 res.unwrap(),
135 source,
136 );
137 }
138 } else {
139 assert!(
140 actual.is_ok(),
141 "Couldn't parse {} | error: {:?}",
142 source,
143 actual.err().unwrap(),
144 );
145 assert_eq!(actual.unwrap(), expected.0, "source: {}", source);
146 }
147 }
148
149 fn unescape(input: &str) -> String {
150 let mut output = String::new();
151 let mut chars = input.chars();
152 loop {
153 match chars.next() {
154 None => return output,
155 Some(c) => {
156 if c == '\\' {
157 match chars.next().unwrap() {
158 '\\' => output.push('\\'),
159 'u' => {
160 let c1 = chars.next().unwrap().to_digit(16).unwrap();
161 let c2 = chars.next().unwrap().to_digit(16).unwrap();
162 let c3 = chars.next().unwrap().to_digit(16).unwrap();
163 let c4 = chars.next().unwrap().to_digit(16).unwrap();
164 match char::from_u32(((c1 * 16 + c2) * 16 + c3) * 16 + c4) {
165 Some(c) => output.push(c),
166 None => {
167 write!(&mut output, "\\u{:X}{:X}{:X}{:X}", c1, c2, c3, c4)
168 .expect("Could not write to output");
169 }
170 };
171 }
172 _ => panic!("Invalid test data input"),
173 }
174 } else {
175 output.push(c);
176 }
177 }
178 }
179 }
180 }
181
182 fn status(status: &str) -> Vec<&str> {
183 if status.is_empty() || status == "[]" {
184 return Vec::new();
185 }
186
187 let mut result = status.split(", ").collect::<Vec<_>>();
188 assert!(result[0].starts_with('['));
189 result[0] = &result[0][1..];
190
191 let idx = result.len() - 1;
192 let last = &mut result[idx];
193 assert!(last.ends_with(']'));
194 *last = &last[..last.len() - 1];
195
196 result
197 }