Skip to content

[idna] Add unit tests for punycode prefix edge cases #374

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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