From 57319770bbda6fbbb28e9d75aec39072f40714df Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Mon, 8 Mar 2021 12:31:59 +0100 Subject: [PATCH] test,crypto: ensure promises resolve in webcrypto tests PR-URL: https://github.com/nodejs/node/pull/37653 Reviewed-By: Rich Trott Reviewed-By: James M Snell --- test/parallel/test-webcrypto-digest.js | 16 +-- test/parallel/test-webcrypto-ed25519-ed448.js | 16 +-- test/parallel/test-webcrypto-export-import.js | 126 +++++++++--------- test/parallel/test-webcrypto-x25519-x448.js | 8 +- 4 files changed, 85 insertions(+), 81 deletions(-) diff --git a/test/parallel/test-webcrypto-digest.js b/test/parallel/test-webcrypto-digest.js index 7ca85908d63e9c..327a7ba32a9990 100644 --- a/test/parallel/test-webcrypto-digest.js +++ b/test/parallel/test-webcrypto-digest.js @@ -66,17 +66,17 @@ const kData = (new TextEncoder()).encode('hello'); })); })().then(common.mustCall()); -[1, [], {}, null, undefined].forEach((i) => { - assert.rejects(subtle.digest(i), { message: /Unrecognized name/ }); -}); +Promise.all([1, [], {}, null, undefined].map((i) => + assert.rejects(subtle.digest(i), { message: /Unrecognized name/ }) +)).then(common.mustCall()); -assert.rejects(subtle.digest(''), { message: /Unrecognized name/ }); +assert.rejects(subtle.digest(''), { message: /Unrecognized name/ }).then(common.mustCall()); -[1, [], {}, null, undefined].forEach((i) => { +Promise.all([1, [], {}, null, undefined].map((i) => assert.rejects(subtle.digest('SHA-256', i), { code: 'ERR_INVALID_ARG_TYPE' - }); -}); + }) +)).then(common.mustCall()); // If there is a mismatch between length and the expected digest size for // the selected algorithm, we fail. The length is a Node.js specific @@ -84,7 +84,7 @@ assert.rejects(subtle.digest(''), { message: /Unrecognized name/ }); // hash algorithms that support variable digest output lengths. assert.rejects(subtle.digest({ name: 'SHA-512', length: 510 }, kData), { message: /Digest method not supported/ -}); +}).then(common.mustCall()); const kSourceData = { empty: '', diff --git a/test/parallel/test-webcrypto-ed25519-ed448.js b/test/parallel/test-webcrypto-ed25519-ed448.js index c13bf05d5cee9c..80b9b9ebe90cf7 100644 --- a/test/parallel/test-webcrypto-ed25519-ed448.js +++ b/test/parallel/test-webcrypto-ed25519-ed448.js @@ -65,7 +65,7 @@ assert.rejects( ['sign']), { message: /NODE-ED25519 raw keys must be exactly 32-bytes/ - }); + }).then(common.mustCall()); assert.rejects( subtle.importKey( @@ -79,7 +79,7 @@ assert.rejects( ['sign']), { message: /NODE-ED448 raw keys must be exactly 57-bytes/ - }); + }).then(common.mustCall()); const testVectors = { 'NODE-ED25519': [ @@ -331,7 +331,7 @@ assert.rejects( ['sign', 'verify']), { message: /Unsupported named curves for ECDSA/ - }); + }).then(common.mustCall()); assert.rejects( subtle.generateKey( @@ -343,7 +343,7 @@ assert.rejects( ['sign', 'verify']), { message: /Unsupported named curves for ECDSA/ - }); + }).then(common.mustCall()); assert.rejects( subtle.generateKey( @@ -355,7 +355,7 @@ assert.rejects( ['sign', 'verify']), { message: /Unsupported named curves for ECDSA/ - }); + }).then(common.mustCall()); assert.rejects( subtle.generateKey( @@ -367,7 +367,7 @@ assert.rejects( ['sign', 'verify']), { message: /Unsupported named curves for ECDSA/ - }); + }).then(common.mustCall()); { for (const asymmetricKeyType of ['ed25519', 'ed448']) { @@ -398,7 +398,7 @@ assert.rejects( ), { message: /Invalid algorithm name/ - }); + }).then(common.mustCall()); assert.rejects( subtle.importKey( @@ -413,7 +413,7 @@ assert.rejects( ), { message: /Invalid algorithm name/ - }); + }).then(common.mustCall()); } } } diff --git a/test/parallel/test-webcrypto-export-import.js b/test/parallel/test-webcrypto-export-import.js index 7ef1b355bbcb18..93d8967f293551 100644 --- a/test/parallel/test-webcrypto-export-import.js +++ b/test/parallel/test-webcrypto-export-import.js @@ -9,70 +9,74 @@ const assert = require('assert'); const { subtle, getRandomValues } = require('crypto').webcrypto; { - const keyData = getRandomValues(new Uint8Array(32)); - [1, null, undefined, {}, []].forEach((format) => { - assert.rejects( - subtle.importKey(format, keyData, {}, false, ['wrapKey']), { - code: 'ERR_INVALID_ARG_TYPE' + async function test() { + const keyData = getRandomValues(new Uint8Array(32)); + await Promise.all([1, null, undefined, {}, []].map((format) => + assert.rejects( + subtle.importKey(format, keyData, {}, false, ['wrapKey']), { + code: 'ERR_INVALID_ARG_TYPE' + }) + )); + await assert.rejects( + subtle.importKey('not valid', keyData, {}, false, ['wrapKey']), { + code: 'ERR_INVALID_ARG_VALUE' }); - }); - assert.rejects( - subtle.importKey('not valid', keyData, {}, false, ['wrapKey']), { - code: 'ERR_INVALID_ARG_VALUE' - }); - [1, null, undefined, {}, []].forEach((keyData) => { - assert.rejects( - subtle.importKey('raw', keyData, {}, false, ['deriveBits']), { + await Promise.all([1, null, undefined, {}, []].map((keyData) => + assert.rejects( + subtle.importKey('raw', keyData, {}, false, ['deriveBits']), { + code: 'ERR_INVALID_ARG_TYPE' + }) + )); + await assert.rejects( + subtle.importKey('raw', keyData, { + name: 'HMAC' + }, false, ['sign', 'verify']), { + code: 'ERR_MISSING_OPTION' + }); + await assert.rejects( + subtle.importKey('raw', keyData, { + name: 'HMAC', + hash: 'SHA-256' + }, false, ['deriveBits']), { + name: 'SyntaxError', + message: 'Unsupported key usage for an HMAC key' + }); + await assert.rejects( + subtle.importKey('node.keyObject', '', { + name: 'HMAC', + hash: 'SHA-256' + }, false, ['sign', 'verify']), { code: 'ERR_INVALID_ARG_TYPE' }); - }); - assert.rejects( - subtle.importKey('raw', keyData, { - name: 'HMAC' - }, false, ['sign', 'verify']), { - code: 'ERR_MISSING_OPTION' - }).then(common.mustCall()); - assert.rejects( - subtle.importKey('raw', keyData, { - name: 'HMAC', - hash: 'SHA-256' - }, false, ['deriveBits']), { - name: 'SyntaxError', - message: 'Unsupported key usage for an HMAC key' - }).then(common.mustCall()); - assert.rejects( - subtle.importKey('node.keyObject', '', { - name: 'HMAC', - hash: 'SHA-256' - }, false, ['sign', 'verify']), { - code: 'ERR_INVALID_ARG_TYPE' - }).then(common.mustCall()); - assert.rejects( - subtle.importKey('raw', keyData, { - name: 'HMAC', - hash: 'SHA-256', - length: 0 - }, false, ['sign', 'verify']), { - name: 'DataError', - message: 'Zero-length key is not supported' - }).then(common.mustCall()); - assert.rejects( - subtle.importKey('raw', keyData, { - name: 'HMAC', - hash: 'SHA-256', - length: 1 - }, false, ['sign', 'verify']), { - name: 'DataError', - message: 'Invalid key length' - }).then(common.mustCall()); - assert.rejects( - subtle.importKey('jwk', null, { - name: 'HMAC', - hash: 'SHA-256', - }, false, ['sign', 'verify']), { - name: 'DataError', - message: 'Invalid JWK keyData' - }).then(common.mustCall()); + await assert.rejects( + subtle.importKey('raw', keyData, { + name: 'HMAC', + hash: 'SHA-256', + length: 0 + }, false, ['sign', 'verify']), { + name: 'DataError', + message: 'Zero-length key is not supported' + }); + await assert.rejects( + subtle.importKey('raw', keyData, { + name: 'HMAC', + hash: 'SHA-256', + length: 1 + }, false, ['sign', 'verify']), { + name: 'DataError', + message: 'Invalid key length' + }); + await assert.rejects( + subtle.importKey('jwk', null, { + name: 'HMAC', + hash: 'SHA-256', + }, false, ['sign', 'verify']), { + name: 'DataError', + message: 'Invalid JWK keyData' + }); + } + + test().then(common.mustCall()); } // Import/Export HMAC Secret Key diff --git a/test/parallel/test-webcrypto-x25519-x448.js b/test/parallel/test-webcrypto-x25519-x448.js index 84f8b88f254261..8668964524eb45 100644 --- a/test/parallel/test-webcrypto-x25519-x448.js +++ b/test/parallel/test-webcrypto-x25519-x448.js @@ -59,10 +59,10 @@ async function importKey(namedCurve, keyData, isPublic = false) { assert.rejects(importKey('NODE-X25519', Buffer.alloc(10), true), { message: /NODE-X25519 raw keys must be exactly 32-bytes/ -}); +}).then(common.mustCall()); assert.rejects(importKey('NODE-X448', Buffer.alloc(10), true), { message: /NODE-X448 raw keys must be exactly 56-bytes/ -}); +}).then(common.mustCall()); async function test1(namedCurve) { const { @@ -235,7 +235,7 @@ assert.rejects( ['deriveBits']), { message: /Unsupported named curves for ECDH/ - }); + }).then(common.mustCall()); assert.rejects( subtle.generateKey( @@ -247,7 +247,7 @@ assert.rejects( ['deriveBits']), { message: /Unsupported named curves for ECDH/ - }); + }).then(common.mustCall()); { // Private JWK import