Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crypto key object constructor coverage and message fix #27904

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/internal/crypto/keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
22 changes: 22 additions & 0 deletions test/parallel/test-crypto-key-objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const {
createSecretKey,
createPublicKey,
createPrivateKey,
KeyObject,
randomBytes,
publicEncrypt,
privateDecrypt
Expand All @@ -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);
Expand Down