uts46.rs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 idna::uts46;
  9. use std::char;
  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 {
  34. pieces.remove(0)
  35. } else {
  36. ""
  37. };
  38. if expected_failure {
  39. continue;
  40. }
  41. let test_name = format!("UTS #46 line {}", i + 1);
  42. add_test(
  43. test_name,
  44. TestFn::dyn_test_fn(move || {
  45. let result = uts46::to_ascii(
  46. &source,
  47. uts46::Flags {
  48. use_std3_ascii_rules: true,
  49. transitional_processing: test_type == "T",
  50. verify_dns_length: true,
  51. },
  52. );
  53. if to_ascii.starts_with("[") {
  54. if to_ascii.starts_with("[C") {
  55. // http://unicode.org/reports/tr46/#Deviations
  56. // applications that perform IDNA2008 lookup are not required to check
  57. // for these contexts
  58. return;
  59. }
  60. if to_ascii == "[V2]" {
  61. // Everybody ignores V2
  62. // https://github.com/servo/rust-url/pull/240
  63. // https://github.com/whatwg/url/issues/53#issuecomment-181528158
  64. // http://www.unicode.org/review/pri317/
  65. return;
  66. }
  67. let res = result.ok();
  68. assert!(
  69. res == None,
  70. "Expected error. result: {} | original: {} | source: {}",
  71. res.unwrap(),
  72. original,
  73. source
  74. );
  75. return;
  76. }
  77. let to_ascii = if to_ascii.len() > 0 {
  78. to_ascii.to_string()
  79. } else {
  80. if to_unicode.len() > 0 {
  81. to_unicode.to_string()
  82. } else {
  83. source.clone()
  84. }
  85. };
  86. if nv8 == "NV8" {
  87. // This result isn't valid under IDNA2008. Skip it
  88. return;
  89. }
  90. assert!(
  91. result.is_ok(),
  92. "Couldn't parse {} | original: {} | error: {:?}",
  93. source,
  94. original,
  95. result.err()
  96. );
  97. let output = result.ok().unwrap();
  98. assert!(
  99. output == to_ascii,
  100. "result: {} | expected: {} | original: {} | source: {}",
  101. output,
  102. to_ascii,
  103. original,
  104. source
  105. );
  106. }),
  107. )
  108. }
  109. }
  110. fn unescape(input: &str) -> String {
  111. let mut output = String::new();
  112. let mut chars = input.chars();
  113. loop {
  114. match chars.next() {
  115. None => return output,
  116. Some(c) => {
  117. if c == '\\' {
  118. match chars.next().unwrap() {
  119. '\\' => output.push('\\'),
  120. 'u' => {
  121. let c1 = chars.next().unwrap().to_digit(16).unwrap();
  122. let c2 = chars.next().unwrap().to_digit(16).unwrap();
  123. let c3 = chars.next().unwrap().to_digit(16).unwrap();
  124. let c4 = chars.next().unwrap().to_digit(16).unwrap();
  125. match char::from_u32(((c1 * 16 + c2) * 16 + c3) * 16 + c4) {
  126. Some(c) => output.push(c),
  127. None => {
  128. output
  129. .push_str(&format!("\\u{:X}{:X}{:X}{:X}", c1, c2, c3, c4));
  130. }
  131. };
  132. }
  133. _ => panic!("Invalid test data input"),
  134. }
  135. } else {
  136. output.push(c);
  137. }
  138. }
  139. }
  140. }
  141. }