From 19abe8521fdbc51e9183de7abb27a85b1fb46957 Mon Sep 17 00:00:00 2001 From: Eric Walker Date: Thu, 4 Jul 2024 10:43:53 -0400 Subject: [PATCH] Improve testing --- crates/javy/src/apis/crypto/mod.rs | 36 +++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/crates/javy/src/apis/crypto/mod.rs b/crates/javy/src/apis/crypto/mod.rs index b2721c0b..aa8662c5 100644 --- a/crates/javy/src/apis/crypto/mod.rs +++ b/crates/javy/src/apis/crypto/mod.rs @@ -19,7 +19,6 @@ impl Intrinsic for Crypto { fn register(this: Ctx<'_>) -> Result<()> { let globals = this.globals(); - // let crypto_obj = Object::nw(cx)?; let crypto_obj = Object::new(this.clone())?; crypto_obj.set( @@ -120,7 +119,7 @@ impl<'js> HmacClass<'js> { .map_err(|e| rquickjs::Exception::throw_type(ctx, &format!("Failed to convert message to string: {}", e))).unwrap(); let v = val_to_string(ctx, js_v.clone().into()) - .map_err(|e| rquickjs::Exception::throw_type(ctx, &format!("Failed to convert message to string: {}", e))).unwrap(); + .map_err(|e| rquickjs::Exception::throw_type(ctx, &format!("Failed to convert update input to string: {}", e))).unwrap(); string_message.push_str(&v); self.message = JSString::from_str(ctx.clone(), &string_message).unwrap(); @@ -129,7 +128,7 @@ impl<'js> HmacClass<'js> { #[cfg(test)] mod tests { - use crate::{quickjs::Value, Config, Runtime}; + use crate::{from_js_error, quickjs::Value, Config, Runtime}; use anyhow::{Error, Result}; #[test] @@ -153,6 +152,31 @@ mod tests { Ok(()) } + #[test] + fn test_crypto_digest_with_lossy_input() -> Result<()> { + let mut config = Config::default(); + config.crypto(true); + let runtime = Runtime::new(config)?; + + runtime.context().with(|this| { + let result: Value<'_> = this.eval( + r#" + // matched tested behavior in node v18 + let expectedHex = "c06ae855290abd8f397af6975e9c2f72fe27a90a3e0f0bb73b4f991567501980"; + let hmac = crypto.createHmac("sha256", "\uD800\uD800\uD800\uD800\uD800"); + hmac.update("\uD800\uD800\uD800\uD800\uD800"); + let result = hmac.digest("hex"); + console.log(result); + console.log("Match?", result === expectedHex); + result === expectedHex; + "#, + )?; + assert!(result.as_bool().unwrap()); + Ok::<_, Error>(()) + })?; + Ok(()) + } + #[test] fn test_not_sha256_algo_errors() -> Result<()> { let mut config = Config::default(); @@ -166,7 +190,8 @@ mod tests { "#, ); assert!(result.is_err()); - assert_eq!("Exception generated by QuickJS", result.err().unwrap().to_string()); + let e = result.map_err(|e| from_js_error(this.clone(), e)).unwrap_err(); + assert_eq!("Error:2:28 Argument 1: only sha256 supported.\n at (eval_script:2:28)\n", e.to_string()); Ok::<_, Error>(()) })?; Ok(()) @@ -187,7 +212,8 @@ mod tests { "#, ); assert!(result.is_err()); - assert_eq!("Exception generated by QuickJS", result.err().unwrap().to_string()); + let e = result.map_err(|e| from_js_error(this.clone(), e)).unwrap_err(); + assert_eq!("Error:4:26 digest type must be 'hex'\n at (eval_script:4:26)\n", e.to_string()); Ok::<_, Error>(()) })?; Ok(())