| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374 |
- use idna::uts46::AsciiDenyList;
- use idna::uts46::DnsLength;
- use idna::uts46::Hyphens;
- /// https://github.com/servo/rust-url/issues/373
- #[test]
- fn test_punycode_prefix_with_length_check() {
- let config = idna::uts46::Uts46::new();
- assert!(config
- .to_ascii(
- b"xn--",
- AsciiDenyList::STD3,
- Hyphens::Check,
- DnsLength::Verify
- )
- .is_err());
- assert!(config
- .to_ascii(
- b"xn---",
- AsciiDenyList::STD3,
- Hyphens::Check,
- DnsLength::Verify
- )
- .is_err());
- assert!(config
- .to_ascii(
- b"xn-----",
- AsciiDenyList::STD3,
- Hyphens::Check,
- DnsLength::Verify,
- )
- .is_err());
- assert!(config
- .to_ascii(
- b"xn--.",
- AsciiDenyList::STD3,
- Hyphens::Check,
- DnsLength::Verify
- )
- .is_err());
- assert!(config
- .to_ascii(
- b"xn--...",
- AsciiDenyList::STD3,
- Hyphens::Check,
- DnsLength::Verify,
- )
- .is_err());
- assert!(config
- .to_ascii(
- b".xn--",
- AsciiDenyList::STD3,
- Hyphens::Check,
- DnsLength::Verify,
- )
- .is_err());
- assert!(config
- .to_ascii(
- b"...xn--",
- AsciiDenyList::STD3,
- Hyphens::Check,
- DnsLength::Verify,
- )
- .is_err());
- assert!(config
- .to_ascii(
- b"xn--.xn--",
- AsciiDenyList::STD3,
- Hyphens::Check,
- DnsLength::Verify,
- )
- .is_err());
- assert!(config
- .to_ascii(
- b"xn--.example.org",
- AsciiDenyList::STD3,
- Hyphens::Check,
- DnsLength::Verify,
- )
- .is_err());
- }
- /// https://github.com/servo/rust-url/issues/373
- #[test]
- fn test_punycode_prefix_without_length_check() {
- let config = idna::uts46::Uts46::new();
- assert!(config
- .to_ascii(
- b"xn--",
- AsciiDenyList::URL,
- Hyphens::Allow,
- DnsLength::Ignore
- )
- .is_err());
- assert!(config
- .to_ascii(
- b"xn---",
- AsciiDenyList::URL,
- Hyphens::Allow,
- DnsLength::Ignore
- )
- .is_err());
- assert!(config
- .to_ascii(
- b"xn-----",
- AsciiDenyList::URL,
- Hyphens::Allow,
- DnsLength::Ignore
- )
- .is_err());
- assert!(config
- .to_ascii(
- b"xn--.",
- AsciiDenyList::URL,
- Hyphens::Allow,
- DnsLength::Ignore
- )
- .is_err());
- assert!(config
- .to_ascii(
- b"xn--...",
- AsciiDenyList::URL,
- Hyphens::Allow,
- DnsLength::Ignore
- )
- .is_err());
- assert!(config
- .to_ascii(
- b".xn--",
- AsciiDenyList::URL,
- Hyphens::Allow,
- DnsLength::Ignore
- )
- .is_err());
- assert!(config
- .to_ascii(
- b"...xn--",
- AsciiDenyList::URL,
- Hyphens::Allow,
- DnsLength::Ignore
- )
- .is_err());
- assert!(config
- .to_ascii(
- b"xn--.xn--",
- AsciiDenyList::URL,
- Hyphens::Allow,
- DnsLength::Ignore
- )
- .is_err());
- assert!(config
- .to_ascii(
- b"xn--.example.org",
- AsciiDenyList::URL,
- Hyphens::Allow,
- DnsLength::Ignore
- )
- .is_err());
- }
- /*
- // http://www.unicode.org/reports/tr46/#Table_Example_Processing
- #[test]
- fn test_examples() {
- let codec = idna::uts46bis::Uts46::new();
- let mut out = String::new();
- assert_matches!(codec.to_unicode("Bloß.de", &mut out), Ok(()));
- assert_eq!(out, "bloß.de");
- out.clear();
- assert_matches!(codec.to_unicode("xn--blo-7ka.de", &mut out), Ok(()));
- assert_eq!(out, "bloß.de");
- out.clear();
- assert_matches!(codec.to_unicode("u\u{308}.com", &mut out), Ok(()));
- assert_eq!(out, "ü.com");
- out.clear();
- assert_matches!(codec.to_unicode("xn--tda.com", &mut out), Ok(()));
- assert_eq!(out, "ü.com");
- out.clear();
- assert_matches!(codec.to_unicode("xn--u-ccb.com", &mut out), Err(_));
- out.clear();
- assert_matches!(codec.to_unicode("a⒈com", &mut out), Err(_));
- out.clear();
- assert_matches!(codec.to_unicode("xn--a-ecp.ru", &mut out), Err(_));
- out.clear();
- assert_matches!(codec.to_unicode("xn--0.pt", &mut out), Err(_));
- out.clear();
- assert_matches!(codec.to_unicode("日本語。JP", &mut out), Ok(()));
- assert_eq!(out, "日本語.jp");
- out.clear();
- assert_matches!(codec.to_unicode("☕.us", &mut out), Ok(()));
- assert_eq!(out, "☕.us");
- }
- */
- #[test]
- fn test_v5() {
- let config = idna::uts46::Uts46::new();
- // IdnaTest:784 蔏。𑰺
- assert!(config
- .to_ascii(
- "\u{11C3A}".as_bytes(),
- AsciiDenyList::STD3,
- Hyphens::Check,
- DnsLength::Verify,
- )
- .is_err());
- assert!(config
- .to_ascii(
- "\u{850f}.\u{11C3A}".as_bytes(),
- AsciiDenyList::STD3,
- Hyphens::Check,
- DnsLength::Verify,
- )
- .is_err());
- assert!(config
- .to_ascii(
- "\u{850f}\u{ff61}\u{11C3A}".as_bytes(),
- AsciiDenyList::STD3,
- Hyphens::Check,
- DnsLength::Verify,
- )
- .is_err());
- }
- #[test]
- fn test_v8_bidi_rules() {
- let config = idna::uts46::Uts46::new();
- assert_eq!(
- config
- .to_ascii(
- b"abc",
- AsciiDenyList::STD3,
- Hyphens::Check,
- DnsLength::Verify,
- )
- .unwrap(),
- "abc"
- );
- assert_eq!(
- config
- .to_ascii(
- b"123",
- AsciiDenyList::STD3,
- Hyphens::Check,
- DnsLength::Verify,
- )
- .unwrap(),
- "123"
- );
- assert_eq!(
- config
- .to_ascii(
- "אבּג".as_bytes(),
- AsciiDenyList::STD3,
- Hyphens::Check,
- DnsLength::Verify,
- )
- .unwrap(),
- "xn--kdb3bdf"
- );
- assert_eq!(
- config
- .to_ascii(
- "ابج".as_bytes(),
- AsciiDenyList::STD3,
- Hyphens::Check,
- DnsLength::Verify,
- )
- .unwrap(),
- "xn--mgbcm"
- );
- assert_eq!(
- config
- .to_ascii(
- "abc.ابج".as_bytes(),
- AsciiDenyList::STD3,
- Hyphens::Check,
- DnsLength::Verify,
- )
- .unwrap(),
- "abc.xn--mgbcm"
- );
- assert_eq!(
- config
- .to_ascii(
- "אבּג.ابج".as_bytes(),
- AsciiDenyList::STD3,
- Hyphens::Check,
- DnsLength::Verify,
- )
- .unwrap(),
- "xn--kdb3bdf.xn--mgbcm"
- );
- // Bidi domain names cannot start with digits
- assert!(config
- .to_ascii(
- "0a.\u{05D0}".as_bytes(),
- AsciiDenyList::STD3,
- Hyphens::Check,
- DnsLength::Verify,
- )
- .is_err());
- assert!(config
- .to_ascii(
- "0à.\u{05D0}".as_bytes(),
- AsciiDenyList::STD3,
- Hyphens::Check,
- DnsLength::Verify,
- )
- .is_err());
- // Bidi chars may be punycode-encoded
- assert!(config
- .to_ascii(
- b"xn--0ca24w",
- AsciiDenyList::STD3,
- Hyphens::Check,
- DnsLength::Verify,
- )
- .is_err());
- }
- #[test]
- fn emoji_domains() {
- // HOT BEVERAGE is allowed here...
- let config = idna::uts46::Uts46::new();
- assert_eq!(
- config
- .to_ascii(
- "☕.com".as_bytes(),
- AsciiDenyList::STD3,
- Hyphens::Check,
- DnsLength::Verify,
- )
- .unwrap(),
- "xn--53h.com"
- );
- }
- #[test]
- fn unicode_before_delimiter() {
- let config = idna::uts46::Uts46::new();
- assert!(config
- .to_ascii(
- "xn--f\u{34a}-PTP".as_bytes(),
- AsciiDenyList::STD3,
- Hyphens::Check,
- DnsLength::Verify,
- )
- .is_err());
- }
- #[test]
- fn upper_case_ascii_in_punycode() {
- let config = idna::uts46::Uts46::new();
- let (unicode, result) =
- config.to_unicode("xn--A-1ga".as_bytes(), AsciiDenyList::STD3, Hyphens::Check);
- assert!(result.is_ok());
- assert_eq!(&unicode, "aö");
- }
|