Skip to content

Commit

Permalink
Improve testing
Browse files Browse the repository at this point in the history
  • Loading branch information
ewalk153 committed Jul 4, 2024
1 parent 52ad035 commit 19abe85
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions crates/javy/src/apis/crypto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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();
Expand All @@ -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]
Expand All @@ -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();
Expand All @@ -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> (eval_script:2:28)\n", e.to_string());
Ok::<_, Error>(())
})?;
Ok(())
Expand All @@ -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> (eval_script:4:26)\n", e.to_string());
Ok::<_, Error>(())
})?;
Ok(())
Expand Down

0 comments on commit 19abe85

Please sign in to comment.