Skip to content

Commit

Permalink
[idna] Add unit tests for punycode prefix edge cases
Browse files Browse the repository at this point in the history
Helps with answering question asked in
servo#373
  • Loading branch information
behnam committed Jul 3, 2017
1 parent 034b1cd commit a87f126
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 13 deletions.
2 changes: 1 addition & 1 deletion idna/src/uts46.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Error>);

/// http://www.unicode.org/reports/tr46/#ToASCII
Expand Down
91 changes: 79 additions & 12 deletions idna/tests/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,69 @@ use idna::uts46;
use unicode_normalization::char::is_combining_mark;


fn _to_ascii(domain: &str) -> Result<String, uts46::Errors> {
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<String, uts46::Errors> {
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<String, uts46::Errors> {
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<String, uts46::Errors> {
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());
Expand All @@ -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<String, uts46::Errors> {
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());
Expand Down

0 comments on commit a87f126

Please sign in to comment.