From b9a6ab5c48757af7bab27fd8c2263377c592f60c Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Fri, 18 Aug 2023 01:50:12 +0200 Subject: [PATCH] test: split JWK async elliptic curve keygen tests PR-URL: https://github.com/nodejs/node/pull/49221 Refs: https://github.com/nodejs/node/issues/49202 Refs: https://github.com/nodejs/node/issues/41206 Reviewed-By: Luigi Pinca --- ...ypto-keygen-async-elliptic-curve-jwk-ec.js | 35 ++++++ ...pto-keygen-async-elliptic-curve-jwk-rsa.js | 38 ++++++ ...-crypto-keygen-async-elliptic-curve-jwk.js | 108 ++++-------------- 3 files changed, 97 insertions(+), 84 deletions(-) create mode 100644 test/parallel/test-crypto-keygen-async-elliptic-curve-jwk-ec.js create mode 100644 test/parallel/test-crypto-keygen-async-elliptic-curve-jwk-rsa.js diff --git a/test/parallel/test-crypto-keygen-async-elliptic-curve-jwk-ec.js b/test/parallel/test-crypto-keygen-async-elliptic-curve-jwk-ec.js new file mode 100644 index 00000000000000..bddb4aa2fbdcd6 --- /dev/null +++ b/test/parallel/test-crypto-keygen-async-elliptic-curve-jwk-ec.js @@ -0,0 +1,35 @@ +'use strict'; + +const common = require('../common'); +if (!common.hasCrypto) + common.skip('missing crypto'); + +const assert = require('assert'); +const { + generateKeyPair, +} = require('crypto'); + +// Test async elliptic curve key generation with 'jwk' encoding and named +// curve. +['P-384', 'P-256', 'P-521', 'secp256k1'].forEach((curve) => { + generateKeyPair('ec', { + namedCurve: curve, + publicKeyEncoding: { + format: 'jwk' + }, + privateKeyEncoding: { + format: 'jwk' + } + }, common.mustSucceed((publicKey, privateKey) => { + assert.strictEqual(typeof publicKey, 'object'); + assert.strictEqual(typeof privateKey, 'object'); + assert.strictEqual(publicKey.x, privateKey.x); + assert.strictEqual(publicKey.y, privateKey.y); + assert(!publicKey.d); + assert(privateKey.d); + assert.strictEqual(publicKey.kty, 'EC'); + assert.strictEqual(publicKey.kty, privateKey.kty); + assert.strictEqual(publicKey.crv, curve); + assert.strictEqual(publicKey.crv, privateKey.crv); + })); +}); diff --git a/test/parallel/test-crypto-keygen-async-elliptic-curve-jwk-rsa.js b/test/parallel/test-crypto-keygen-async-elliptic-curve-jwk-rsa.js new file mode 100644 index 00000000000000..5f62a506334b93 --- /dev/null +++ b/test/parallel/test-crypto-keygen-async-elliptic-curve-jwk-rsa.js @@ -0,0 +1,38 @@ +'use strict'; + +const common = require('../common'); +if (!common.hasCrypto) + common.skip('missing crypto'); + +const assert = require('assert'); +const { + generateKeyPair, +} = require('crypto'); + +// Test async elliptic curve key generation with 'jwk' encoding and RSA. +{ + generateKeyPair('rsa', { + modulusLength: 4096, + publicKeyEncoding: { + format: 'jwk' + }, + privateKeyEncoding: { + format: 'jwk' + } + }, common.mustSucceed((publicKey, privateKey) => { + assert.strictEqual(typeof publicKey, 'object'); + assert.strictEqual(typeof privateKey, 'object'); + assert.strictEqual(publicKey.kty, 'RSA'); + assert.strictEqual(publicKey.kty, privateKey.kty); + assert.strictEqual(typeof publicKey.n, 'string'); + assert.strictEqual(publicKey.n, privateKey.n); + assert.strictEqual(typeof publicKey.e, 'string'); + assert.strictEqual(publicKey.e, privateKey.e); + assert.strictEqual(typeof privateKey.d, 'string'); + assert.strictEqual(typeof privateKey.p, 'string'); + assert.strictEqual(typeof privateKey.q, 'string'); + assert.strictEqual(typeof privateKey.dp, 'string'); + assert.strictEqual(typeof privateKey.dq, 'string'); + assert.strictEqual(typeof privateKey.qi, 'string'); + })); +} diff --git a/test/parallel/test-crypto-keygen-async-elliptic-curve-jwk.js b/test/parallel/test-crypto-keygen-async-elliptic-curve-jwk.js index a716f26dfe0275..5243edd8c825b7 100644 --- a/test/parallel/test-crypto-keygen-async-elliptic-curve-jwk.js +++ b/test/parallel/test-crypto-keygen-async-elliptic-curve-jwk.js @@ -9,92 +9,32 @@ const { generateKeyPair, } = require('crypto'); -// Test async elliptic curve key generation with 'jwk' encoding +// Test async elliptic curve key generation with 'jwk' encoding. { [ - ['ec', ['P-384', 'P-256', 'P-521', 'secp256k1']], - ['rsa'], - ['ed25519'], - ['ed448'], - ['x25519'], - ['x448'], - ].forEach((types) => { - const [type, options] = types; - switch (type) { - case 'ec': { - return options.forEach((curve) => { - generateKeyPair(type, { - namedCurve: curve, - publicKeyEncoding: { - format: 'jwk' - }, - privateKeyEncoding: { - format: 'jwk' - } - }, common.mustSucceed((publicKey, privateKey) => { - assert.strictEqual(typeof publicKey, 'object'); - assert.strictEqual(typeof privateKey, 'object'); - assert.strictEqual(publicKey.x, privateKey.x); - assert.strictEqual(publicKey.y, privateKey.y); - assert(!publicKey.d); - assert(privateKey.d); - assert.strictEqual(publicKey.kty, 'EC'); - assert.strictEqual(publicKey.kty, privateKey.kty); - assert.strictEqual(publicKey.crv, curve); - assert.strictEqual(publicKey.crv, privateKey.crv); - })); - }); + 'ed25519', + 'ed448', + 'x25519', + 'x448', + ].forEach((type) => { + generateKeyPair(type, { + publicKeyEncoding: { + format: 'jwk' + }, + privateKeyEncoding: { + format: 'jwk' } - case 'rsa': { - return generateKeyPair(type, { - modulusLength: 4096, - publicKeyEncoding: { - format: 'jwk' - }, - privateKeyEncoding: { - format: 'jwk' - } - }, common.mustSucceed((publicKey, privateKey) => { - assert.strictEqual(typeof publicKey, 'object'); - assert.strictEqual(typeof privateKey, 'object'); - assert.strictEqual(publicKey.kty, 'RSA'); - assert.strictEqual(publicKey.kty, privateKey.kty); - assert.strictEqual(typeof publicKey.n, 'string'); - assert.strictEqual(publicKey.n, privateKey.n); - assert.strictEqual(typeof publicKey.e, 'string'); - assert.strictEqual(publicKey.e, privateKey.e); - assert.strictEqual(typeof privateKey.d, 'string'); - assert.strictEqual(typeof privateKey.p, 'string'); - assert.strictEqual(typeof privateKey.q, 'string'); - assert.strictEqual(typeof privateKey.dp, 'string'); - assert.strictEqual(typeof privateKey.dq, 'string'); - assert.strictEqual(typeof privateKey.qi, 'string'); - })); - } - case 'ed25519': - case 'ed448': - case 'x25519': - case 'x448': { - generateKeyPair(type, { - publicKeyEncoding: { - format: 'jwk' - }, - privateKeyEncoding: { - format: 'jwk' - } - }, common.mustSucceed((publicKey, privateKey) => { - assert.strictEqual(typeof publicKey, 'object'); - assert.strictEqual(typeof privateKey, 'object'); - assert.strictEqual(publicKey.x, privateKey.x); - assert(!publicKey.d); - assert(privateKey.d); - assert.strictEqual(publicKey.kty, 'OKP'); - assert.strictEqual(publicKey.kty, privateKey.kty); - const expectedCrv = `${type.charAt(0).toUpperCase()}${type.slice(1)}`; - assert.strictEqual(publicKey.crv, expectedCrv); - assert.strictEqual(publicKey.crv, privateKey.crv); - })); - } - } + }, common.mustSucceed((publicKey, privateKey) => { + assert.strictEqual(typeof publicKey, 'object'); + assert.strictEqual(typeof privateKey, 'object'); + assert.strictEqual(publicKey.x, privateKey.x); + assert(!publicKey.d); + assert(privateKey.d); + assert.strictEqual(publicKey.kty, 'OKP'); + assert.strictEqual(publicKey.kty, privateKey.kty); + const expectedCrv = `${type.charAt(0).toUpperCase()}${type.slice(1)}`; + assert.strictEqual(publicKey.crv, expectedCrv); + assert.strictEqual(publicKey.crv, privateKey.crv); + })); }); }