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