From 6c60efa39118b6fc66a318e06a8ac0102a80153f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Brandon=20Pi=C3=B1a?= Date: Tue, 20 Aug 2024 14:32:03 -0500 Subject: [PATCH 1/6] add unknown variant and test --- src/jwk.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/jwk.rs b/src/jwk.rs index 49c58003..c7511cde 100644 --- a/src/jwk.rs +++ b/src/jwk.rs @@ -188,6 +188,10 @@ pub enum KeyAlgorithm { /// RSAES-OAEP-256 using SHA-2 #[serde(rename = "RSA-OAEP-256")] RSA_OAEP_256, + + /// Catch-All for when the key algorithm can not be determined + #[serde(other)] + UNKNOWN_ALGORITHM } impl FromStr for KeyAlgorithm { @@ -435,7 +439,7 @@ impl JwkSet { #[cfg(test)] mod tests { - use crate::jwk::{AlgorithmParameters, JwkSet, OctetKeyType}; + use crate::jwk::{AlgorithmParameters, JwkSet, KeyAlgorithm, OctetKeyType}; use crate::serialization::b64_encode; use crate::Algorithm; use serde_json::json; @@ -471,4 +475,11 @@ mod tests { _ => panic!("Unexpected key algorithm"), } } + + #[test] + fn deserialize_unknown_key_algorithm(){ + let key_alg_json = json!(""); + let key_alg_result: KeyAlgorithm = serde_json::from_value(key_alg_json).expect("Could not deserialize json"); + assert_eq!(key_alg_result,KeyAlgorithm::UNKNOWN_ALGORITHM); + } } From 3c174f36bda733c8ff9b6a4808d187f6c9c173ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Brandon=20Pi=C3=B1a?= Date: Wed, 21 Aug 2024 08:18:17 -0500 Subject: [PATCH 2/6] add test for unsupported key algorithm --- src/jwk.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/jwk.rs b/src/jwk.rs index c7511cde..d596ba37 100644 --- a/src/jwk.rs +++ b/src/jwk.rs @@ -482,4 +482,12 @@ mod tests { let key_alg_result: KeyAlgorithm = serde_json::from_value(key_alg_json).expect("Could not deserialize json"); assert_eq!(key_alg_result,KeyAlgorithm::UNKNOWN_ALGORITHM); } + + #[test] + fn deserialize_unsupported_key_algorithm(){ + // as of time of writing this algorithm is not supported but this could change in the future + let key_alg_json = json!("ES512"); + let key_alg_result: KeyAlgorithm = serde_json::from_value(key_alg_json).expect("Could not deserialize json"); + assert_eq!(key_alg_result,KeyAlgorithm::UNKNOWN_ALGORITHM); + } } From 803d60e154f4d6c3a096a7e1a7dab6810d92d8ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Brandon=20Pi=C3=B1a?= <32885853+ThatNerdUKnow@users.noreply.github.com> Date: Wed, 21 Aug 2024 08:19:03 -0500 Subject: [PATCH 3/6] Apply suggestions from code review Co-authored-by: Vincent Prouillet --- src/jwk.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/jwk.rs b/src/jwk.rs index d596ba37..c2fede79 100644 --- a/src/jwk.rs +++ b/src/jwk.rs @@ -189,7 +189,7 @@ pub enum KeyAlgorithm { #[serde(rename = "RSA-OAEP-256")] RSA_OAEP_256, - /// Catch-All for when the key algorithm can not be determined + /// Catch-All for when the key algorithm can not be determined or is not supported #[serde(other)] UNKNOWN_ALGORITHM } From a6cef300540a99c0cd0eb47ad8b5f8e915496339 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Brandon=20Pi=C3=B1a?= Date: Thu, 22 Aug 2024 16:05:46 -0500 Subject: [PATCH 4/6] use proptest to test deserialization of keyalgorithm variants --- Cargo.toml | 1 + proptest-regressions/jwk.txt | 7 +++++++ src/jwk.rs | 25 +++++++++++++++---------- 3 files changed, 23 insertions(+), 10 deletions(-) create mode 100644 proptest-regressions/jwk.txt diff --git a/Cargo.toml b/Cargo.toml index 92178dd9..f013a5ff 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,6 +36,7 @@ ring = { version = "0.17.4", features = ["std", "wasm32_unknown_unknown_js"] } [dev-dependencies] wasm-bindgen-test = "0.3.1" +proptest = "1.5.0" [target.'cfg(not(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi")))))'.dev-dependencies] # For the custom time example diff --git a/proptest-regressions/jwk.txt b/proptest-regressions/jwk.txt new file mode 100644 index 00000000..1688e5f2 --- /dev/null +++ b/proptest-regressions/jwk.txt @@ -0,0 +1,7 @@ +# Seeds for failure cases proptest has generated in the past. It is +# automatically read and these particular cases re-run before any +# novel cases are generated. +# +# It is recommended to check this file in to source control so that +# everyone who runs the test benefits from these saved cases. +cc 9588ed7692395e4050cf8adc38499186bbdf9b288462e7fa6c19099a3c557464 # shrinks to s = "" diff --git a/src/jwk.rs b/src/jwk.rs index c2fede79..81d8b311 100644 --- a/src/jwk.rs +++ b/src/jwk.rs @@ -191,7 +191,7 @@ pub enum KeyAlgorithm { /// Catch-All for when the key algorithm can not be determined or is not supported #[serde(other)] - UNKNOWN_ALGORITHM + UNKNOWN_ALGORITHM, } impl FromStr for KeyAlgorithm { @@ -442,6 +442,7 @@ mod tests { use crate::jwk::{AlgorithmParameters, JwkSet, KeyAlgorithm, OctetKeyType}; use crate::serialization::b64_encode; use crate::Algorithm; + use proptest::proptest; use serde_json::json; use wasm_bindgen_test::wasm_bindgen_test; @@ -477,17 +478,21 @@ mod tests { } #[test] - fn deserialize_unknown_key_algorithm(){ + fn deserialize_unknown_key_algorithm() { let key_alg_json = json!(""); - let key_alg_result: KeyAlgorithm = serde_json::from_value(key_alg_json).expect("Could not deserialize json"); - assert_eq!(key_alg_result,KeyAlgorithm::UNKNOWN_ALGORITHM); + let key_alg_result: KeyAlgorithm = + serde_json::from_value(key_alg_json).expect("Could not deserialize json"); + assert_eq!(key_alg_result, KeyAlgorithm::UNKNOWN_ALGORITHM); } - #[test] - fn deserialize_unsupported_key_algorithm(){ - // as of time of writing this algorithm is not supported but this could change in the future - let key_alg_json = json!("ES512"); - let key_alg_result: KeyAlgorithm = serde_json::from_value(key_alg_json).expect("Could not deserialize json"); - assert_eq!(key_alg_result,KeyAlgorithm::UNKNOWN_ALGORITHM); + // Rather than testing against a particular case, test against a sampling of random strings + proptest! { + #[test] + fn deserialize_arbitrary_key_algorithm(s in ".*"){ + // as of time of writing this algorithm is not supported but this could change in the future + let key_alg_json = json!(s); + let _key_alg_result: KeyAlgorithm = serde_json::from_value(key_alg_json).expect("Could not deserialize json"); + // We don't need to assert a specific variant - we only care that the above line does not panic + } } } From 63ecb66a4da033ce0335bc15db74a1b8c24a2e14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Brandon=20Pi=C3=B1a?= Date: Thu, 22 Aug 2024 16:06:45 -0500 Subject: [PATCH 5/6] remove comment --- src/jwk.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/jwk.rs b/src/jwk.rs index 81d8b311..b1caa206 100644 --- a/src/jwk.rs +++ b/src/jwk.rs @@ -489,7 +489,6 @@ mod tests { proptest! { #[test] fn deserialize_arbitrary_key_algorithm(s in ".*"){ - // as of time of writing this algorithm is not supported but this could change in the future let key_alg_json = json!(s); let _key_alg_result: KeyAlgorithm = serde_json::from_value(key_alg_json).expect("Could not deserialize json"); // We don't need to assert a specific variant - we only care that the above line does not panic From 347d20f89fe64adaadefa5d5140e5f8882f04fe1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Brandon=20Pi=C3=B1a?= Date: Mon, 26 Aug 2024 08:04:16 -0500 Subject: [PATCH 6/6] only enable standard featureset for proptest to fix wasm builds --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index f013a5ff..212502e1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,7 +36,7 @@ ring = { version = "0.17.4", features = ["std", "wasm32_unknown_unknown_js"] } [dev-dependencies] wasm-bindgen-test = "0.3.1" -proptest = "1.5.0" +proptest = {version = "1.5.0", default-features = false, features = ["std"]} [target.'cfg(not(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi")))))'.dev-dependencies] # For the custom time example