punycode.rs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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, 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 encode_str(decoded) {
  29. None => panic!("Encoding {} failed.", decoded),
  30. Some(result) => assert!(
  31. result == encoded,
  32. "Incorrect encoding of \"{}\":\n \"{}\"\n!= \"{}\"\n",
  33. decoded,
  34. result,
  35. encoded
  36. ),
  37. }
  38. }
  39. fn one_bad_test(encode: &str) {
  40. let result = catch_unwind(|| encode_str(encode));
  41. assert!(
  42. matches!(&result, Ok(None)),
  43. "Should neither panic nor return Some result, but got {:?}",
  44. result
  45. )
  46. }
  47. fn get_string<'a>(map: &'a Map<String, Value>, key: &str) -> &'a str {
  48. match map.get(&key.to_string()) {
  49. Some(Value::String(s)) => s,
  50. None => "",
  51. _ => panic!(),
  52. }
  53. }
  54. pub fn collect_tests<F: FnMut(String, TestFn)>(add_test: &mut F) {
  55. match Value::from_str(include_str!("punycode_tests.json")) {
  56. Ok(Value::Array(tests)) => {
  57. for (i, test) in tests.into_iter().enumerate() {
  58. match test {
  59. Value::Object(o) => {
  60. let test_name = {
  61. let desc = get_string(&o, "description");
  62. if desc.is_empty() {
  63. format!("Punycode {}", i + 1)
  64. } else {
  65. format!("Punycode {}: {}", i + 1, desc)
  66. }
  67. };
  68. add_test(
  69. test_name,
  70. TestFn::DynTestFn(Box::new(move || {
  71. one_test(get_string(&o, "decoded"), get_string(&o, "encoded"))
  72. })),
  73. )
  74. }
  75. _ => panic!(),
  76. }
  77. }
  78. }
  79. other => panic!("{:?}", other),
  80. }
  81. match Value::from_str(include_str!("bad_punycode_tests.json")) {
  82. Ok(Value::Array(tests)) => {
  83. for (i, test) in tests.into_iter().enumerate() {
  84. match test {
  85. Value::Object(o) => {
  86. let test_name = {
  87. let desc = get_string(&o, "description");
  88. if desc.is_empty() {
  89. format!("Bad Punycode {}", i + 1)
  90. } else {
  91. format!("Bad Punycode {}: {}", i + 1, desc)
  92. }
  93. };
  94. add_test(
  95. test_name,
  96. TestFn::DynTestFn(Box::new(move || {
  97. one_bad_test(get_string(&o, "decoded"))
  98. })),
  99. )
  100. }
  101. _ => panic!(),
  102. }
  103. }
  104. }
  105. other => panic!("{:?}", other),
  106. }
  107. }