Debian Codegraph @

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

raw
1 use idna::uts46::AsciiDenyList;
2 use idna::uts46::DnsLength;
3 use idna::uts46::Hyphens;
4
5 /// https://github.com/servo/rust-url/issues/373
6 #[test]
7 fn test_punycode_prefix_with_length_check() {
8 let config = idna::uts46::Uts46::new();
9
10 assert!(config
11 .to_ascii(
12 b"xn--",
13 AsciiDenyList::STD3,
14 Hyphens::Check,
15 DnsLength::Verify
16 )
17 .is_err());
18 assert!(config
19 .to_ascii(
20 b"xn---",
21 AsciiDenyList::STD3,
22 Hyphens::Check,
23 DnsLength::Verify
24 )
25 .is_err());
26 assert!(config
27 .to_ascii(
28 b"xn-----",
29 AsciiDenyList::STD3,
30 Hyphens::Check,
31 DnsLength::Verify,
32 )
33 .is_err());
34 assert!(config
35 .to_ascii(
36 b"xn--.",
37 AsciiDenyList::STD3,
38 Hyphens::Check,
39 DnsLength::Verify
40 )
41 .is_err());
42 assert!(config
43 .to_ascii(
44 b"xn--...",
45 AsciiDenyList::STD3,
46 Hyphens::Check,
47 DnsLength::Verify,
48 )
49 .is_err());
50 assert!(config
51 .to_ascii(
52 b".xn--",
53 AsciiDenyList::STD3,
54 Hyphens::Check,
55 DnsLength::Verify,
56 )
57 .is_err());
58 assert!(config
59 .to_ascii(
60 b"...xn--",
61 AsciiDenyList::STD3,
62 Hyphens::Check,
63 DnsLength::Verify,
64 )
65 .is_err());
66 assert!(config
67 .to_ascii(
68 b"xn--.xn--",
69 AsciiDenyList::STD3,
70 Hyphens::Check,
71 DnsLength::Verify,
72 )
73 .is_err());
74 assert!(config
75 .to_ascii(
76 b"xn--.example.org",
77 AsciiDenyList::STD3,
78 Hyphens::Check,
79 DnsLength::Verify,
80 )
81 .is_err());
82 }
83
84 /// https://github.com/servo/rust-url/issues/373
85 #[test]
86 fn test_punycode_prefix_without_length_check() {
87 let config = idna::uts46::Uts46::new();
88
89 assert!(config
90 .to_ascii(
91 b"xn--",
92 AsciiDenyList::URL,
93 Hyphens::Allow,
94 DnsLength::Ignore
95 )
96 .is_err());
97 assert!(config
98 .to_ascii(
99 b"xn---",
100 AsciiDenyList::URL,
101 Hyphens::Allow,
102 DnsLength::Ignore
103 )
104 .is_err());
105 assert!(config
106 .to_ascii(
107 b"xn-----",
108 AsciiDenyList::URL,
109 Hyphens::Allow,
110 DnsLength::Ignore
111 )
112 .is_err());
113 assert!(config
114 .to_ascii(
115 b"xn--.",
116 AsciiDenyList::URL,
117 Hyphens::Allow,
118 DnsLength::Ignore
119 )
120 .is_err());
121 assert!(config
122 .to_ascii(
123 b"xn--...",
124 AsciiDenyList::URL,
125 Hyphens::Allow,
126 DnsLength::Ignore
127 )
128 .is_err());
129 assert!(config
130 .to_ascii(
131 b".xn--",
132 AsciiDenyList::URL,
133 Hyphens::Allow,
134 DnsLength::Ignore
135 )
136 .is_err());
137 assert!(config
138 .to_ascii(
139 b"...xn--",
140 AsciiDenyList::URL,
141 Hyphens::Allow,
142 DnsLength::Ignore
143 )
144 .is_err());
145 assert!(config
146 .to_ascii(
147 b"xn--.xn--",
148 AsciiDenyList::URL,
149 Hyphens::Allow,
150 DnsLength::Ignore
151 )
152 .is_err());
153 assert!(config
154 .to_ascii(
155 b"xn--.example.org",
156 AsciiDenyList::URL,
157 Hyphens::Allow,
158 DnsLength::Ignore
159 )
160 .is_err());
161 }
162 /*
163 // http://www.unicode.org/reports/tr46/#Table_Example_Processing
164 #[test]
165 fn test_examples() {
166 let codec = idna::uts46bis::Uts46::new();
167 let mut out = String::new();
168
169 assert_matches!(codec.to_unicode("Bloß.de", &mut out), Ok(()));
170 assert_eq!(out, "bloß.de");
171
172 out.clear();
173 assert_matches!(codec.to_unicode("xn--blo-7ka.de", &mut out), Ok(()));
174 assert_eq!(out, "bloß.de");
175
176 out.clear();
177 assert_matches!(codec.to_unicode("u\u{308}.com", &mut out), Ok(()));
178 assert_eq!(out, "ü.com");
179
180 out.clear();
181 assert_matches!(codec.to_unicode("xn--tda.com", &mut out), Ok(()));
182 assert_eq!(out, "ü.com");
183
184 out.clear();
185 assert_matches!(codec.to_unicode("xn--u-ccb.com", &mut out), Err(_));
186
187 out.clear();
188 assert_matches!(codec.to_unicode("a⒈com", &mut out), Err(_));
189
190 out.clear();
191 assert_matches!(codec.to_unicode("xn--a-ecp.ru", &mut out), Err(_));
192
193 out.clear();
194 assert_matches!(codec.to_unicode("xn--0.pt", &mut out), Err(_));
195
196 out.clear();
197 assert_matches!(codec.to_unicode("日本語。JP", &mut out), Ok(()));
198 assert_eq!(out, "日本語.jp");
199
200 out.clear();
201 assert_matches!(codec.to_unicode("☕.us", &mut out), Ok(()));
202 assert_eq!(out, "☕.us");
203 }
204 */
205
206 #[test]
207 fn test_v5() {
208 let config = idna::uts46::Uts46::new();
209
210 // IdnaTest:784 蔏。𑰺
211 assert!(config
212 .to_ascii(
213 "\u{11C3A}".as_bytes(),
214 AsciiDenyList::STD3,
215 Hyphens::Check,
216 DnsLength::Verify,
217 )
218 .is_err());
219 assert!(config
220 .to_ascii(
221 "\u{850f}.\u{11C3A}".as_bytes(),
222 AsciiDenyList::STD3,
223 Hyphens::Check,
224 DnsLength::Verify,
225 )
226 .is_err());
227 assert!(config
228 .to_ascii(
229 "\u{850f}\u{ff61}\u{11C3A}".as_bytes(),
230 AsciiDenyList::STD3,
231 Hyphens::Check,
232 DnsLength::Verify,
233 )
234 .is_err());
235 }
236
237 #[test]
238 fn test_v8_bidi_rules() {
239 let config = idna::uts46::Uts46::new();
240
241 assert_eq!(
242 config
243 .to_ascii(
244 b"abc",
245 AsciiDenyList::STD3,
246 Hyphens::Check,
247 DnsLength::Verify,
248 )
249 .unwrap(),
250 "abc"
251 );
252 assert_eq!(
253 config
254 .to_ascii(
255 b"123",
256 AsciiDenyList::STD3,
257 Hyphens::Check,
258 DnsLength::Verify,
259 )
260 .unwrap(),
261 "123"
262 );
263 assert_eq!(
264 config
265 .to_ascii(
266 "אבּג".as_bytes(),
267 AsciiDenyList::STD3,
268 Hyphens::Check,
269 DnsLength::Verify,
270 )
271 .unwrap(),
272 "xn--kdb3bdf"
273 );
274 assert_eq!(
275 config
276 .to_ascii(
277 "ابج".as_bytes(),
278 AsciiDenyList::STD3,
279 Hyphens::Check,
280 DnsLength::Verify,
281 )
282 .unwrap(),
283 "xn--mgbcm"
284 );
285 assert_eq!(
286 config
287 .to_ascii(
288 "abc.ابج".as_bytes(),
289 AsciiDenyList::STD3,
290 Hyphens::Check,
291 DnsLength::Verify,
292 )
293 .unwrap(),
294 "abc.xn--mgbcm"
295 );
296 assert_eq!(
297 config
298 .to_ascii(
299 "אבּג.ابج".as_bytes(),
300 AsciiDenyList::STD3,
301 Hyphens::Check,
302 DnsLength::Verify,
303 )
304 .unwrap(),
305 "xn--kdb3bdf.xn--mgbcm"
306 );
307
308 // Bidi domain names cannot start with digits
309 assert!(config
310 .to_ascii(
311 "0a.\u{05D0}".as_bytes(),
312 AsciiDenyList::STD3,
313 Hyphens::Check,
314 DnsLength::Verify,
315 )
316 .is_err());
317 assert!(config
318 .to_ascii(
319 "0à.\u{05D0}".as_bytes(),
320 AsciiDenyList::STD3,
321 Hyphens::Check,
322 DnsLength::Verify,
323 )
324 .is_err());
325
326 // Bidi chars may be punycode-encoded
327 assert!(config
328 .to_ascii(
329 b"xn--0ca24w",
330 AsciiDenyList::STD3,
331 Hyphens::Check,
332 DnsLength::Verify,
333 )
334 .is_err());
335 }
336
337 #[test]
338 fn emoji_domains() {
339 // HOT BEVERAGE is allowed here...
340 let config = idna::uts46::Uts46::new();
341 assert_eq!(
342 config
343 .to_ascii(
344 "☕.com".as_bytes(),
345 AsciiDenyList::STD3,
346 Hyphens::Check,
347 DnsLength::Verify,
348 )
349 .unwrap(),
350 "xn--53h.com"
351 );
352 }
353
354 #[test]
355 fn unicode_before_delimiter() {
356 let config = idna::uts46::Uts46::new();
357 assert!(config
358 .to_ascii(
359 "xn--f\u{34a}-PTP".as_bytes(),
360 AsciiDenyList::STD3,
361 Hyphens::Check,
362 DnsLength::Verify,
363 )
364 .is_err());
365 }
366
367 #[test]
368 fn upper_case_ascii_in_punycode() {
369 let config = idna::uts46::Uts46::new();
370 let (unicode, result) =
371 config.to_unicode("xn--A-1ga".as_bytes(), AsciiDenyList::STD3, Hyphens::Check);
372 assert!(result.is_ok());
373 assert_eq!(&unicode, "aö");
374 }