Skip to content

Commit

Permalink
add test to idna for bad punycode
Browse files Browse the repository at this point in the history
  • Loading branch information
Skgland committed Nov 7, 2023
1 parent ae47571 commit 64ec072
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
6 changes: 6 additions & 0 deletions idna/tests/bad_punycode_tests.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"description": "issue 870",
"decoded": " 񠀷 󠀷 󠀷 󠀷 󠀷 󠀷 󠀷 󠀷 󠀷 󠀷 󠀷 󠀷 󠀷 󠀷 21日 ᄢ 21日 㩴 ᄢ 21日 ᄢ "
}
]
35 changes: 35 additions & 0 deletions idna/tests/punycode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::test::TestFn;
use idna::punycode::{decode, encode_str};
use serde_json::map::Map;
use serde_json::Value;
use std::panic::catch_unwind;
use std::str::FromStr;

fn one_test(decoded: &str, encoded: &str) {
Expand Down Expand Up @@ -39,6 +40,13 @@ fn one_test(decoded: &str, encoded: &str) {
}
}

fn one_bad_test(encode: &str) {
let result = catch_unwind(|| {
encode_str(encode)
});
assert!(matches!(&result, Ok(None)), "Should neither panic nor return Some result, but got {:?}", result)
}

fn get_string<'a>(map: &'a Map<String, Value>, key: &str) -> &'a str {
match map.get(&key.to_string()) {
Some(Value::String(s)) => s,
Expand Down Expand Up @@ -74,4 +82,31 @@ pub fn collect_tests<F: FnMut(String, TestFn)>(add_test: &mut F) {
}
other => panic!("{:?}", other),
}

match Value::from_str(include_str!("bad_punycode_tests.json")) {
Ok(Value::Array(tests)) => {
for (i, test) in tests.into_iter().enumerate() {
match test {
Value::Object(o) => {
let test_name = {
let desc = get_string(&o, "description");
if desc.is_empty() {
format!("Bad Punycode {}", i + 1)
} else {
format!("Bad Punycode {}: {}", i + 1, desc)
}
};
add_test(
test_name,
TestFn::DynTestFn(Box::new(move || {
one_bad_test(get_string(&o, "decoded"))
})),
)
}
_ => panic!(),
}
}
}
other => panic!("{:?}", other),
}
}

0 comments on commit 64ec072

Please sign in to comment.