Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions proptest-regressions/jwk.txt
Original file line number Diff line number Diff line change
@@ -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 = ""
25 changes: 24 additions & 1 deletion src/jwk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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
}
}
}