From 9220a68a76d9457b633a250c5caf7e689f3024dc Mon Sep 17 00:00:00 2001 From: Alexander Avakov Date: Sun, 26 May 2019 16:47:09 +0300 Subject: [PATCH] crypto: fix KeyObject handle type error message Fix KeyObject handle type error message. Add test to cover KeyObject ERR_INVALID_ARG_TYPE exception. PR-URL: https://github.com/nodejs/node/pull/27904 Reviewed-By: Ruben Bridgewater Reviewed-By: Ujjwal Sharma Reviewed-By: Rich Trott Reviewed-By: Trivikram Kamat --- lib/internal/crypto/keys.js | 2 +- test/parallel/test-crypto-key-objects.js | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/internal/crypto/keys.js b/lib/internal/crypto/keys.js index 321506eaf4a531..fb17ba36ced0e3 100644 --- a/lib/internal/crypto/keys.js +++ b/lib/internal/crypto/keys.js @@ -44,7 +44,7 @@ class KeyObject { if (type !== 'secret' && type !== 'public' && type !== 'private') throw new ERR_INVALID_ARG_VALUE('type', type); if (typeof handle !== 'object') - throw new ERR_INVALID_ARG_TYPE('handle', 'string', handle); + throw new ERR_INVALID_ARG_TYPE('handle', 'object', handle); this[kKeyType] = type; diff --git a/test/parallel/test-crypto-key-objects.js b/test/parallel/test-crypto-key-objects.js index ab9005f8598a73..2a3a3ec2f0bff1 100644 --- a/test/parallel/test-crypto-key-objects.js +++ b/test/parallel/test-crypto-key-objects.js @@ -13,6 +13,7 @@ const { createSecretKey, createPublicKey, createPrivateKey, + KeyObject, randomBytes, publicEncrypt, privateDecrypt @@ -39,6 +40,27 @@ const privateDsa = fixtures.readKey('dsa_private_encrypted_1025.pem', }); } +{ + // Attempting to create a key of a wrong type should throw + const TYPE = 'wrong_type'; + + common.expectsError(() => new KeyObject(TYPE), { + type: TypeError, + code: 'ERR_INVALID_ARG_VALUE', + message: `The argument 'type' is invalid. Received '${TYPE}'` + }); +} + +{ + // Attempting to create a key with non-object handle should throw + common.expectsError(() => new KeyObject('secret', ''), { + type: TypeError, + code: 'ERR_INVALID_ARG_TYPE', + message: + 'The "handle" argument must be of type object. Received type string' + }); +} + { const keybuf = randomBytes(32); const key = createSecretKey(keybuf);