diff --git a/Cargo.toml b/Cargo.toml index 92178dd..212502e 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 = {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 diff --git a/proptest-regressions/jwk.txt b/proptest-regressions/jwk.txt new file mode 100644 index 0000000..1688e5f --- /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 49c5800..b1caa20 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 or is not supported + #[serde(other)] + UNKNOWN_ALGORITHM, } impl FromStr for KeyAlgorithm { @@ -435,9 +439,10 @@ 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 proptest::proptest; use serde_json::json; use wasm_bindgen_test::wasm_bindgen_test; @@ -471,4 +476,22 @@ 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); + } + + // Rather than testing against a particular case, test against a sampling of random strings + proptest! { + #[test] + fn deserialize_arbitrary_key_algorithm(s in ".*"){ + 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 + } + } }