| 1 |
#![allow(deprecated)] |
| 2 |
|
| 3 |
use assert_matches::assert_matches; |
| 4 |
|
| 5 |
/// https://github.com/servo/rust-url/issues/373 |
| 6 |
#[test] |
| 7 |
fn test_punycode_prefix_with_length_check() { |
| 8 |
let config = idna::Config::default() |
| 9 |
.verify_dns_length(true) |
| 10 |
.check_hyphens(true) |
| 11 |
.use_std3_ascii_rules(true); |
| 12 |
|
| 13 |
assert!(config.to_ascii("xn--").is_err()); |
| 14 |
assert!(config.to_ascii("xn---").is_err()); |
| 15 |
assert!(config.to_ascii("xn-----").is_err()); |
| 16 |
assert!(config.to_ascii("xn--.").is_err()); |
| 17 |
assert!(config.to_ascii("xn--...").is_err()); |
| 18 |
assert!(config.to_ascii(".xn--").is_err()); |
| 19 |
assert!(config.to_ascii("...xn--").is_err()); |
| 20 |
assert!(config.to_ascii("xn--.xn--").is_err()); |
| 21 |
assert!(config.to_ascii("xn--.example.org").is_err()); |
| 22 |
} |
| 23 |
|
| 24 |
/// https://github.com/servo/rust-url/issues/373 |
| 25 |
#[test] |
| 26 |
fn test_punycode_prefix_without_length_check() { |
| 27 |
let config = idna::Config::default() |
| 28 |
.verify_dns_length(false) |
| 29 |
.check_hyphens(true) |
| 30 |
.use_std3_ascii_rules(true); |
| 31 |
|
| 32 |
assert!(config.to_ascii("xn--").is_err()); |
| 33 |
assert!(config.to_ascii("xn---").is_err()); |
| 34 |
assert!(config.to_ascii("xn-----").is_err()); |
| 35 |
assert!(config.to_ascii("xn--.").is_err()); |
| 36 |
assert!(config.to_ascii("xn--...").is_err()); |
| 37 |
assert!(config.to_ascii(".xn--").is_err()); |
| 38 |
assert!(config.to_ascii("...xn--").is_err()); |
| 39 |
assert!(config.to_ascii("xn--.xn--").is_err()); |
| 40 |
assert!(config.to_ascii("xn--.example.org").is_err()); |
| 41 |
} |
| 42 |
|
| 43 |
#[test] |
| 44 |
fn test_punycode_invalid_encoding() { |
| 45 |
let config = idna::Config::default(); |
| 46 |
assert!(config.to_ascii("xn--55555577").is_err()); |
| 47 |
} |
| 48 |
|
| 49 |
// http://www.unicode.org/reports/tr46/#Table_Example_Processing |
| 50 |
#[test] |
| 51 |
fn test_examples() { |
| 52 |
let mut codec = idna::Idna::default(); |
| 53 |
let mut out = String::new(); |
| 54 |
|
| 55 |
assert_matches!(codec.to_unicode("Bloß.de", &mut out), Ok(())); |
| 56 |
assert_eq!(out, "bloß.de"); |
| 57 |
|
| 58 |
out.clear(); |
| 59 |
assert_matches!(codec.to_unicode("xn--blo-7ka.de", &mut out), Ok(())); |
| 60 |
assert_eq!(out, "bloß.de"); |
| 61 |
|
| 62 |
out.clear(); |
| 63 |
assert_matches!(codec.to_unicode("u\u{308}.com", &mut out), Ok(())); |
| 64 |
assert_eq!(out, "ü.com"); |
| 65 |
|
| 66 |
out.clear(); |
| 67 |
assert_matches!(codec.to_unicode("xn--tda.com", &mut out), Ok(())); |
| 68 |
assert_eq!(out, "ü.com"); |
| 69 |
|
| 70 |
out.clear(); |
| 71 |
assert_matches!(codec.to_unicode("xn--u-ccb.com", &mut out), Err(_)); |
| 72 |
|
| 73 |
out.clear(); |
| 74 |
assert_matches!(codec.to_unicode("a⒈com", &mut out), Err(_)); |
| 75 |
|
| 76 |
out.clear(); |
| 77 |
assert_matches!(codec.to_unicode("xn--a-ecp.ru", &mut out), Err(_)); |
| 78 |
|
| 79 |
out.clear(); |
| 80 |
assert_matches!(codec.to_unicode("xn--0.pt", &mut out), Err(_)); |
| 81 |
|
| 82 |
out.clear(); |
| 83 |
assert_matches!(codec.to_unicode("日本語。JP", &mut out), Ok(())); |
| 84 |
assert_eq!(out, "日本語.jp"); |
| 85 |
|
| 86 |
out.clear(); |
| 87 |
assert_matches!(codec.to_unicode("☕.us", &mut out), Ok(())); |
| 88 |
assert_eq!(out, "☕.us"); |
| 89 |
} |
| 90 |
|
| 91 |
#[test] |
| 92 |
fn test_v5() { |
| 93 |
let config = idna::Config::default() |
| 94 |
.verify_dns_length(true) |
| 95 |
.use_std3_ascii_rules(true) |
| 96 |
.check_hyphens(true); |
| 97 |
|
| 98 |
// IdnaTest:784 蔏。𑰺 |
| 99 |
assert!(config.to_ascii("\u{11C3A}").is_err()); |
| 100 |
assert!(config.to_ascii("\u{850f}.\u{11C3A}").is_err()); |
| 101 |
assert!(config.to_ascii("\u{850f}\u{ff61}\u{11C3A}").is_err()); |
| 102 |
} |
| 103 |
|
| 104 |
#[test] |
| 105 |
fn test_v8_bidi_rules() { |
| 106 |
let config = idna::Config::default() |
| 107 |
.verify_dns_length(true) |
| 108 |
.use_std3_ascii_rules(true) |
| 109 |
.check_hyphens(true); |
| 110 |
|
| 111 |
assert_eq!(config.to_ascii("abc").unwrap(), "abc"); |
| 112 |
assert_eq!(config.to_ascii("123").unwrap(), "123"); |
| 113 |
assert_eq!(config.to_ascii("אבּג").unwrap(), "xn--kdb3bdf"); |
| 114 |
assert_eq!(config.to_ascii("ابج").unwrap(), "xn--mgbcm"); |
| 115 |
assert_eq!(config.to_ascii("abc.ابج").unwrap(), "abc.xn--mgbcm"); |
| 116 |
assert_eq!(config.to_ascii("אבּג.ابج").unwrap(), "xn--kdb3bdf.xn--mgbcm"); |
| 117 |
|
| 118 |
// Bidi domain names cannot start with digits |
| 119 |
assert!(config.to_ascii("0a.\u{05D0}").is_err()); |
| 120 |
assert!(config.to_ascii("0à.\u{05D0}").is_err()); |
| 121 |
|
| 122 |
// Bidi chars may be punycode-encoded |
| 123 |
assert!(config.to_ascii("xn--0ca24w").is_err()); |
| 124 |
} |
| 125 |
|
| 126 |
#[test] |
| 127 |
fn emoji_domains() { |
| 128 |
// HOT BEVERAGE is allowed here... |
| 129 |
let config = idna::Config::default() |
| 130 |
.verify_dns_length(true) |
| 131 |
.use_std3_ascii_rules(true) |
| 132 |
.check_hyphens(true); |
| 133 |
assert_eq!(config.to_ascii("☕.com").unwrap(), "xn--53h.com"); |
| 134 |
} |
| 135 |
|
| 136 |
#[test] |
| 137 |
fn unicode_before_delimiter() { |
| 138 |
let config = idna::Config::default(); |
| 139 |
assert!(config.to_ascii("xn--f\u{34a}-PTP").is_err()); |
| 140 |
} |