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 <ruben@bridgewater.de>
Reviewed-By: Ujjwal Sharma <usharma1998@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
This commit is contained in:
Alexander Avakov 2019-05-26 16:47:09 +03:00 committed by Rich Trott
parent a40aae8d1b
commit f692299764
2 changed files with 23 additions and 1 deletions

View File

@ -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;

View File

@ -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);