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