uts46.rs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright 2013-2014 The rust-url developers.
  2. //
  3. // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
  4. // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
  5. // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
  6. // option. This file may not be copied, modified, or distributed
  7. // except according to those terms.
  8. use std::char;
  9. use idna::uts46;
  10. use test::TestFn;
  11. pub fn collect_tests<F: FnMut(String, TestFn)>(add_test: &mut F) {
  12. // http://www.unicode.org/Public/idna/latest/IdnaTest.txt
  13. for (i, line) in include_str!("IdnaTest.txt").lines().enumerate() {
  14. if line == "" || line.starts_with("#") {
  15. continue
  16. }
  17. // Remove comments
  18. let mut line = match line.find("#") {
  19. Some(index) => &line[0..index],
  20. None => line
  21. };
  22. let mut expected_failure = false;
  23. if line.starts_with("XFAIL") {
  24. expected_failure = true;
  25. line = &line[5..line.len()];
  26. };
  27. let mut pieces = line.split(';').map(|x| x.trim()).collect::<Vec<&str>>();
  28. let test_type = pieces.remove(0);
  29. let original = pieces.remove(0);
  30. let source = unescape(original);
  31. let to_unicode = pieces.remove(0);
  32. let to_ascii = pieces.remove(0);
  33. let nv8 = if pieces.len() > 0 { pieces.remove(0) } else { "" };
  34. if expected_failure {
  35. continue;
  36. }
  37. let test_name = format!("UTS #46 line {}", i + 1);
  38. add_test(test_name, TestFn::dyn_test_fn(move || {
  39. let result = uts46::to_ascii(&source, uts46::Flags {
  40. use_std3_ascii_rules: true,
  41. transitional_processing: test_type == "T",
  42. verify_dns_length: true,
  43. });
  44. if to_ascii.starts_with("[") {
  45. if to_ascii.starts_with("[C") {
  46. // http://unicode.org/reports/tr46/#Deviations
  47. // applications that perform IDNA2008 lookup are not required to check
  48. // for these contexts
  49. return;
  50. }
  51. if to_ascii == "[V2]" {
  52. // Everybody ignores V2
  53. // https://github.com/servo/rust-url/pull/240
  54. // https://github.com/whatwg/url/issues/53#issuecomment-181528158
  55. // http://www.unicode.org/review/pri317/
  56. return;
  57. }
  58. let res = result.ok();
  59. assert!(res == None, "Expected error. result: {} | original: {} | source: {}",
  60. res.unwrap(), original, source);
  61. return;
  62. }
  63. let to_ascii = if to_ascii.len() > 0 {
  64. to_ascii.to_string()
  65. } else {
  66. if to_unicode.len() > 0 {
  67. to_unicode.to_string()
  68. } else {
  69. source.clone()
  70. }
  71. };
  72. if nv8 == "NV8" {
  73. // This result isn't valid under IDNA2008. Skip it
  74. return;
  75. }
  76. assert!(result.is_ok(), "Couldn't parse {} | original: {} | error: {:?}",
  77. source, original, result.err());
  78. let output = result.ok().unwrap();
  79. assert!(output == to_ascii, "result: {} | expected: {} | original: {} | source: {}",
  80. output, to_ascii, original, source);
  81. }))
  82. }
  83. }
  84. fn unescape(input: &str) -> String {
  85. let mut output = String::new();
  86. let mut chars = input.chars();
  87. loop {
  88. match chars.next() {
  89. None => return output,
  90. Some(c) =>
  91. if c == '\\' {
  92. match chars.next().unwrap() {
  93. '\\' => output.push('\\'),
  94. 'u' => {
  95. let c1 = chars.next().unwrap().to_digit(16).unwrap();
  96. let c2 = chars.next().unwrap().to_digit(16).unwrap();
  97. let c3 = chars.next().unwrap().to_digit(16).unwrap();
  98. let c4 = chars.next().unwrap().to_digit(16).unwrap();
  99. match char::from_u32((((c1 * 16 + c2) * 16 + c3) * 16 + c4))
  100. {
  101. Some(c) => output.push(c),
  102. None => { output.push_str(&format!("\\u{:X}{:X}{:X}{:X}",c1,c2,c3,c4)); }
  103. };
  104. }
  105. _ => panic!("Invalid test data input"),
  106. }
  107. } else {
  108. output.push(c);
  109. }
  110. }
  111. }
  112. }