diff --git a/idna/src/uts46.rs b/idna/src/uts46.rs index 7115bd8ff..a7ed3833d 100644 --- a/idna/src/uts46.rs +++ b/idna/src/uts46.rs @@ -357,7 +357,7 @@ enum Error { /// /// This is opaque for now, only indicating the presence of at least one error. /// More details may be exposed in the future. -#[derive(Debug)] +#[derive(Debug, Eq, PartialEq)] pub struct Errors(Vec); /// http://www.unicode.org/reports/tr46/#ToASCII diff --git a/idna/tests/unit.rs b/idna/tests/unit.rs index a7d158d5c..5d2cd05e4 100644 --- a/idna/tests/unit.rs +++ b/idna/tests/unit.rs @@ -5,16 +5,69 @@ use idna::uts46; use unicode_normalization::char::is_combining_mark; -fn _to_ascii(domain: &str) -> Result { - uts46::to_ascii(domain, uts46::Flags { - transitional_processing: false, - use_std3_ascii_rules: true, - verify_dns_length: true, - }) +/// https://github.com/servo/rust-url/issues/373 +#[test] +fn test_punycode_prefix_with_length_check() { + fn _to_ascii(domain: &str) -> Result { + uts46::to_ascii( + domain, + uts46::Flags { + transitional_processing: false, + use_std3_ascii_rules: true, + verify_dns_length: true, + }, + ) + } + + assert!(_to_ascii("xn--").is_err()); + assert!(_to_ascii("xn---").is_err()); + assert!(_to_ascii("xn-----").is_err()); + assert!(_to_ascii("xn--.").is_err()); + assert!(_to_ascii("xn--...").is_err()); + assert!(_to_ascii(".xn--").is_err()); + assert!(_to_ascii("...xn--").is_err()); + assert!(_to_ascii("xn--.xn--").is_err()); + assert!(_to_ascii("xn--.example.org").is_err()); +} + +/// https://github.com/servo/rust-url/issues/373 +#[test] +fn test_punycode_prefix_without_length_check() { + fn _to_ascii(domain: &str) -> Result { + uts46::to_ascii( + domain, + uts46::Flags { + transitional_processing: false, + use_std3_ascii_rules: true, + verify_dns_length: false, + }, + ) + } + + assert_eq!(_to_ascii("xn--"), Ok("".to_owned())); + assert!(_to_ascii("xn---").is_err()); + assert!(_to_ascii("xn-----").is_err()); + assert_eq!(_to_ascii("xn--."), Ok(".".to_owned())); + assert_eq!(_to_ascii("xn--..."), Ok("...".to_owned())); + assert_eq!(_to_ascii(".xn--"), Ok(".".to_owned())); + assert_eq!(_to_ascii("...xn--"), Ok("...".to_owned())); + assert_eq!(_to_ascii("xn--.xn--"), Ok(".".to_owned())); + assert_eq!(_to_ascii("xn--.example.org"), Ok(".example.org".to_owned())); } #[test] fn test_v5() { + fn _to_ascii(domain: &str) -> Result { + uts46::to_ascii( + domain, + uts46::Flags { + transitional_processing: false, + use_std3_ascii_rules: true, + verify_dns_length: true, + }, + ) + } + // IdnaTest:784 蔏。𑰺 assert!(is_combining_mark('\u{11C3A}')); assert!(_to_ascii("\u{11C3A}").is_err()); @@ -24,12 +77,26 @@ fn test_v5() { #[test] fn test_v8_bidi_rules() { - assert_eq!(_to_ascii("abc").unwrap(), "abc"); - assert_eq!(_to_ascii("123").unwrap(), "123"); - assert_eq!(_to_ascii("אבּג").unwrap(), "xn--kdb3bdf"); - assert_eq!(_to_ascii("ابج").unwrap(), "xn--mgbcm"); - assert_eq!(_to_ascii("abc.ابج").unwrap(), "abc.xn--mgbcm"); - assert_eq!(_to_ascii("אבּג.ابج").unwrap(), "xn--kdb3bdf.xn--mgbcm"); + fn _to_ascii(domain: &str) -> Result { + uts46::to_ascii( + domain, + uts46::Flags { + transitional_processing: false, + use_std3_ascii_rules: true, + verify_dns_length: true, + }, + ) + } + + assert_eq!(_to_ascii("abc"), Ok("abc".to_owned())); + assert_eq!(_to_ascii("123"), Ok("123".to_owned())); + assert_eq!(_to_ascii("אבּג"), Ok("xn--kdb3bdf".to_owned())); + assert_eq!(_to_ascii("ابج"), Ok("xn--mgbcm".to_owned())); + assert_eq!(_to_ascii("abc.ابج"), Ok("abc.xn--mgbcm".to_owned())); + assert_eq!( + _to_ascii("אבּג.ابج"), + Ok("xn--kdb3bdf.xn--mgbcm".to_owned()) + ); // Bidi domain names cannot start with digits assert!(_to_ascii("0a.\u{05D0}").is_err());