punycode.rs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright 2013 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 crate::test::TestFn;
  9. use idna::punycode::{decode, decode_to_string, encode_str};
  10. use serde_json::map::Map;
  11. use serde_json::Value;
  12. use std::panic::catch_unwind;
  13. use std::str::FromStr;
  14. fn one_test(decoded: &str, encoded: &str) {
  15. match decode(encoded) {
  16. None => panic!("Decoding {} failed.", encoded),
  17. Some(result) => {
  18. let result = result.into_iter().collect::<String>();
  19. assert!(
  20. result == decoded,
  21. "Incorrect decoding of \"{}\":\n \"{}\"\n!= \"{}\"\n",
  22. encoded,
  23. result,
  24. decoded
  25. )
  26. }
  27. }
  28. match decode_to_string(encoded) {
  29. None => panic!("Decoding {} failed.", encoded),
  30. Some(result) => assert!(
  31. result == decoded,
  32. "Incorrect decoding of \"{}\":\n \"{}\"\n!= \"{}\"\n",
  33. encoded,
  34. result,
  35. decoded
  36. ),
  37. }
  38. match encode_str(decoded) {
  39. None => panic!("Encoding {} failed.", decoded),
  40. Some(result) => assert!(
  41. result == encoded,
  42. "Incorrect encoding of \"{}\":\n \"{}\"\n!= \"{}\"\n",
  43. decoded,
  44. result,
  45. encoded
  46. ),
  47. }
  48. }
  49. fn one_bad_test(encode: &str) {
  50. let result = catch_unwind(|| encode_str(encode));
  51. assert!(
  52. matches!(&result, Ok(None)),
  53. "Should neither panic nor return Some result, but got {:?}",
  54. result
  55. )
  56. }
  57. fn get_string<'a>(map: &'a Map<String, Value>, key: &str) -> &'a str {
  58. match map.get(&key.to_string()) {
  59. Some(Value::String(s)) => s,
  60. None => "",
  61. _ => panic!(),
  62. }
  63. }
  64. pub fn collect_tests<F: FnMut(String, TestFn)>(add_test: &mut F) {
  65. match Value::from_str(include_str!("punycode_tests.json")) {
  66. Ok(Value::Array(tests)) => {
  67. for (i, test) in tests.into_iter().enumerate() {
  68. match test {
  69. Value::Object(o) => {
  70. let test_name = {
  71. let desc = get_string(&o, "description");
  72. if desc.is_empty() {
  73. format!("Punycode {}", i + 1)
  74. } else {
  75. format!("Punycode {}: {}", i + 1, desc)
  76. }
  77. };
  78. add_test(
  79. test_name,
  80. TestFn::DynTestFn(Box::new(move || {
  81. one_test(get_string(&o, "decoded"), get_string(&o, "encoded"))
  82. })),
  83. )
  84. }
  85. _ => panic!(),
  86. }
  87. }
  88. }
  89. other => panic!("{:?}", other),
  90. }
  91. match Value::from_str(include_str!("bad_punycode_tests.json")) {
  92. Ok(Value::Array(tests)) => {
  93. for (i, test) in tests.into_iter().enumerate() {
  94. match test {
  95. Value::Object(o) => {
  96. let test_name = {
  97. let desc = get_string(&o, "description");
  98. if desc.is_empty() {
  99. format!("Bad Punycode {}", i + 1)
  100. } else {
  101. format!("Bad Punycode {}: {}", i + 1, desc)
  102. }
  103. };
  104. add_test(
  105. test_name,
  106. TestFn::DynTestFn(Box::new(move || {
  107. one_bad_test(get_string(&o, "decoded"))
  108. })),
  109. )
  110. }
  111. _ => panic!(),
  112. }
  113. }
  114. }
  115. other => panic!("{:?}", other),
  116. }
  117. }