diff options
Diffstat (limited to 'cli/tests/unit/webcrypto_test.ts')
-rw-r--r-- | cli/tests/unit/webcrypto_test.ts | 52 |
1 files changed, 51 insertions, 1 deletions
diff --git a/cli/tests/unit/webcrypto_test.ts b/cli/tests/unit/webcrypto_test.ts index 475efde6c..a5de9fa36 100644 --- a/cli/tests/unit/webcrypto_test.ts +++ b/cli/tests/unit/webcrypto_test.ts @@ -189,7 +189,7 @@ unitTest(async function testGenerateHMACKey() { assert(key.usages.includes("sign")); }); -unitTest(async function testSignECDSA() { +unitTest(async function testECDSASignVerify() { const key = await window.crypto.subtle.generateKey( { name: "ECDSA", @@ -208,6 +208,56 @@ unitTest(async function testSignECDSA() { ); assert(signature); + assert(signature instanceof ArrayBuffer); + + const verified = await window.crypto.subtle.verify( + { hash: { name: "SHA-384" }, name: "ECDSA" }, + key.publicKey, + signature, + encoded, + ); + assert(verified); +}); + +// Tests the "bad paths" as a temporary replacement for sign_verify/ecdsa WPT. +unitTest(async function testECDSASignVerifyFail() { + const key = await window.crypto.subtle.generateKey( + { + name: "ECDSA", + namedCurve: "P-384", + }, + true, + ["sign", "verify"], + ); + + const encoded = new Uint8Array([1]); + // Signing with a public key (InvalidAccessError) + await assertThrowsAsync(async () => { + await window.crypto.subtle.sign( + { name: "ECDSA", hash: "SHA-384" }, + key.publicKey, + new Uint8Array([1]), + ); + throw new TypeError("unreachable"); + }, DOMException); + + // Do a valid sign for later verifying. + const signature = await window.crypto.subtle.sign( + { name: "ECDSA", hash: "SHA-384" }, + key.privateKey, + encoded, + ); + + // Verifying with a private key (InvalidAccessError) + await assertThrowsAsync(async () => { + await window.crypto.subtle.verify( + { hash: { name: "SHA-384" }, name: "ECDSA" }, + key.privateKey, + signature, + encoded, + ); + throw new TypeError("unreachable"); + }, DOMException); }); // https://github.com/denoland/deno/issues/11313 |