From 156017012729c13283f73a46320c4549595a69cb Mon Sep 17 00:00:00 2001 From: Diane Huxley Date: Wed, 21 Aug 2024 10:35:11 -0700 Subject: [PATCH] Test Secp256k1Generator --- crates/web5/src/crypto/dsa/secp256k1.rs | 50 +++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/crates/web5/src/crypto/dsa/secp256k1.rs b/crates/web5/src/crypto/dsa/secp256k1.rs index df9ff995..06e97c86 100644 --- a/crates/web5/src/crypto/dsa/secp256k1.rs +++ b/crates/web5/src/crypto/dsa/secp256k1.rs @@ -72,3 +72,53 @@ pub fn public_jwk_from_bytes(public_key: &[u8]) -> Result { ..Default::default() }) } + +#[cfg(test)] +mod tests { + use super::*; + + mod generate { + use super::*; + + #[test] + fn test_must_set_alg() { + let jwk = Secp256k1Generator::generate(); + assert_eq!(jwk.alg, Some("ES256K".to_string())); + } + + #[test] + fn test_must_set_kty() { + let jwk = Secp256k1Generator::generate(); + assert_eq!(jwk.kty, "EC".to_string()); + } + + #[test] + fn test_must_set_crv() { + let jwk = Secp256k1Generator::generate(); + assert_eq!(jwk.crv, "secp256k1"); + } + + #[test] + fn test_must_set_public_key_with_correct_length() { + let jwk = Secp256k1Generator::generate(); + let x_bytes = general_purpose::URL_SAFE_NO_PAD + .decode(&jwk.x) + .expect("Failed to decode x coordinate"); + let y_bytes = general_purpose::URL_SAFE_NO_PAD + .decode(jwk.y.as_ref().expect("y coordinate is missing")) + .expect("Failed to decode y coordinate"); + assert_eq!(x_bytes.len(), 32); + assert_eq!(y_bytes.len(), 32); + } + + #[test] + fn test_must_set_private_key_with_correct_length() { + let jwk = Secp256k1Generator::generate(); + let private_key_bytes = jwk.d.expect("Private key is missing"); + let decoded_private_key_bytes = general_purpose::URL_SAFE_NO_PAD + .decode(private_key_bytes) + .expect("Failed to decode private key"); + assert_eq!(decoded_private_key_bytes.len(), 32); + } + } +}