uts46.rs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. let res = result.ok();
  52. assert!(res == None, "Expected error. result: {} | original: {} | source: {}",
  53. res.unwrap(), original, source);
  54. return;
  55. }
  56. let to_ascii = if to_ascii.len() > 0 {
  57. to_ascii.to_string()
  58. } else {
  59. if to_unicode.len() > 0 {
  60. to_unicode.to_string()
  61. } else {
  62. source.clone()
  63. }
  64. };
  65. if nv8 == "NV8" {
  66. // This result isn't valid under IDNA2008. Skip it
  67. return;
  68. }
  69. assert!(result.is_ok(), "Couldn't parse {} | original: {} | error: {:?}",
  70. source, original, result.err());
  71. let output = result.ok().unwrap();
  72. assert!(output == to_ascii, "result: {} | expected: {} | original: {} | source: {}",
  73. output, to_ascii, original, source);
  74. }))
  75. }
  76. }
  77. fn unescape(input: &str) -> String {
  78. let mut output = String::new();
  79. let mut chars = input.chars();
  80. loop {
  81. match chars.next() {
  82. None => return output,
  83. Some(c) =>
  84. if c == '\\' {
  85. match chars.next().unwrap() {
  86. '\\' => output.push('\\'),
  87. 'u' => {
  88. let c1 = chars.next().unwrap().to_digit(16).unwrap();
  89. let c2 = chars.next().unwrap().to_digit(16).unwrap();
  90. let c3 = chars.next().unwrap().to_digit(16).unwrap();
  91. let c4 = chars.next().unwrap().to_digit(16).unwrap();
  92. match char::from_u32((((c1 * 16 + c2) * 16 + c3) * 16 + c4))
  93. {
  94. Some(c) => output.push(c),
  95. None => { output.push_str(&format!("\\u{:X}{:X}{:X}{:X}",c1,c2,c3,c4)); }
  96. };
  97. }
  98. _ => panic!("Invalid test data input"),
  99. }
  100. } else {
  101. output.push(c);
  102. }
  103. }
  104. }
  105. }