unit.rs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. use unicode_normalization::char::is_combining_mark;
  2. /// https://github.com/servo/rust-url/issues/373
  3. #[test]
  4. fn test_punycode_prefix_with_length_check() {
  5. let config = idna::Config::default()
  6. .verify_dns_length(true)
  7. .check_hyphens(true)
  8. .use_std3_ascii_rules(true);
  9. assert!(config.to_ascii("xn--").is_err());
  10. assert!(config.to_ascii("xn---").is_err());
  11. assert!(config.to_ascii("xn-----").is_err());
  12. assert!(config.to_ascii("xn--.").is_err());
  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--.xn--").is_err());
  17. assert!(config.to_ascii("xn--.example.org").is_err());
  18. }
  19. /// https://github.com/servo/rust-url/issues/373
  20. #[test]
  21. fn test_punycode_prefix_without_length_check() {
  22. let config = idna::Config::default()
  23. .verify_dns_length(false)
  24. .check_hyphens(true)
  25. .use_std3_ascii_rules(true);
  26. assert_eq!(config.to_ascii("xn--").unwrap(), "");
  27. assert!(config.to_ascii("xn---").is_err());
  28. assert!(config.to_ascii("xn-----").is_err());
  29. assert_eq!(config.to_ascii("xn--.").unwrap(), ".");
  30. assert_eq!(config.to_ascii("xn--...").unwrap(), "...");
  31. assert_eq!(config.to_ascii(".xn--").unwrap(), ".");
  32. assert_eq!(config.to_ascii("...xn--").unwrap(), "...");
  33. assert_eq!(config.to_ascii("xn--.xn--").unwrap(), ".");
  34. assert_eq!(config.to_ascii("xn--.example.org").unwrap(), ".example.org");
  35. }
  36. #[test]
  37. fn test_v5() {
  38. let config = idna::Config::default()
  39. .verify_dns_length(true)
  40. .use_std3_ascii_rules(true);
  41. // IdnaTest:784 蔏。𑰺
  42. assert!(is_combining_mark('\u{11C3A}'));
  43. assert!(config.to_ascii("\u{11C3A}").is_err());
  44. assert!(config.to_ascii("\u{850f}.\u{11C3A}").is_err());
  45. assert!(config.to_ascii("\u{850f}\u{ff61}\u{11C3A}").is_err());
  46. }
  47. #[test]
  48. fn test_v8_bidi_rules() {
  49. let config = idna::Config::default()
  50. .verify_dns_length(true)
  51. .use_std3_ascii_rules(true);
  52. assert_eq!(config.to_ascii("abc").unwrap(), "abc");
  53. assert_eq!(config.to_ascii("123").unwrap(), "123");
  54. assert_eq!(config.to_ascii("אבּג").unwrap(), "xn--kdb3bdf");
  55. assert_eq!(config.to_ascii("ابج").unwrap(), "xn--mgbcm");
  56. assert_eq!(config.to_ascii("abc.ابج").unwrap(), "abc.xn--mgbcm");
  57. assert_eq!(config.to_ascii("אבּג.ابج").unwrap(), "xn--kdb3bdf.xn--mgbcm");
  58. // Bidi domain names cannot start with digits
  59. assert!(config.to_ascii("0a.\u{05D0}").is_err());
  60. assert!(config.to_ascii("0à.\u{05D0}").is_err());
  61. // Bidi chars may be punycode-encoded
  62. assert!(config.to_ascii("xn--0ca24w").is_err());
  63. }