From f7e09c6a555e115c004feca56f8b279c5f18c9b2 Mon Sep 17 00:00:00 2001 From: Yacine Hmito Date: Sun, 10 Jan 2021 17:27:15 +0100 Subject: [PATCH] Test crypto.getRandomValues() with wpt (#9016) --- cli/tests/wpt.jsonc | 3 +++ op_crates/crypto/01_crypto.js | 41 ++++++++++++++++++++++++++++------- 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/cli/tests/wpt.jsonc b/cli/tests/wpt.jsonc index 0a14f8fa67..2e3882fe23 100644 --- a/cli/tests/wpt.jsonc +++ b/cli/tests/wpt.jsonc @@ -173,5 +173,8 @@ "measure-l3", "structured-serialize-detail", "user_timing_exists" + ], + "WebCryptoApi": [ + "getRandomValues" ] } diff --git a/op_crates/crypto/01_crypto.js b/op_crates/crypto/01_crypto.js index 2f94244920..d0b0d98c7c 100644 --- a/op_crates/crypto/01_crypto.js +++ b/op_crates/crypto/01_crypto.js @@ -2,19 +2,44 @@ ((window) => { const core = window.Deno.core; - function getRandomValues(typedArray) { - if (typedArray == null) throw new Error("Input must not be null"); - if (typedArray.length > 65536) { - throw new Error("Input must not be longer than 65536"); + + function getRandomValues(arrayBufferView) { + if (!ArrayBuffer.isView(arrayBufferView)) { + throw new TypeError( + "Argument 1 does not implement interface ArrayBufferView", + ); + } + if ( + !( + arrayBufferView instanceof Int8Array || + arrayBufferView instanceof Uint8Array || + arrayBufferView instanceof Int16Array || + arrayBufferView instanceof Uint16Array || + arrayBufferView instanceof Int32Array || + arrayBufferView instanceof Uint32Array || + arrayBufferView instanceof Uint8ClampedArray + ) + ) { + throw new DOMException( + "The provided ArrayBufferView is not an integer array type", + "TypeMismatchError", + ); + } + if (arrayBufferView.byteLength > 65536) { + throw new DOMException( + `The ArrayBufferView's byte length (${arrayBufferView.byteLength}) exceeds the number of bytes of entropy available via this API (65536)`, + "QuotaExceededError", + ); } const ui8 = new Uint8Array( - typedArray.buffer, - typedArray.byteOffset, - typedArray.byteLength, + arrayBufferView.buffer, + arrayBufferView.byteOffset, + arrayBufferView.byteLength, ); core.jsonOpSync("op_get_random_values", {}, ui8); - return typedArray; + return arrayBufferView; } + window.crypto = { getRandomValues, };