From 64ec072f7b0b71e5cf7d9597ff8911ecb71c8ee4 Mon Sep 17 00:00:00 2001 From: Skgland Date: Mon, 6 Nov 2023 23:33:39 +0100 Subject: [PATCH] add test to idna for bad punycode --- idna/tests/bad_punycode_tests.json | 6 +++++ idna/tests/punycode.rs | 35 ++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 idna/tests/bad_punycode_tests.json diff --git a/idna/tests/bad_punycode_tests.json b/idna/tests/bad_punycode_tests.json new file mode 100644 index 000000000..c0115290c --- /dev/null +++ b/idna/tests/bad_punycode_tests.json @@ -0,0 +1,6 @@ +[ + { + "description": "issue 870", + "decoded": " 񠀷 󠀷 󠀷 󠀷 󠀷 󠀷 󠀷 󠀷 󠀷 󠀷 󠀷 󠀷 󠀷 󠀷 21日 ᄢ 21日 㩴 ᄢ 21日 ᄢ " + } +] diff --git a/idna/tests/punycode.rs b/idna/tests/punycode.rs index 95674137a..7f5c35f8e 100644 --- a/idna/tests/punycode.rs +++ b/idna/tests/punycode.rs @@ -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) { @@ -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, key: &str) -> &'a str { match map.get(&key.to_string()) { Some(Value::String(s)) => s, @@ -74,4 +82,31 @@ pub fn collect_tests(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), + } }