test: improve coverage for Cipher and Decipher

Cipher
- Call constructor withour new keyword
- Call constructor with cipher is not string
- Call constructor with cipher is string and password is not string
- Call Cipher#update with data is not string
- Call Cipher#setAuthTag with tagbuf is not string
- Call Cipher#setAAD with aadbuf is not string

Decipher
- Call constructor withour new keyword
- Call constructor with cipher is not string
- Call constructor with cipher is string and password is not string

PR-URL: https://github.com/nodejs/node/pull/17449
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: Jon Moss <me@jonathanmoss.me>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
Leko 2017-12-04 17:23:59 +09:00 committed by Anatoli Papirovski
parent de3a7dbb2d
commit 84b7a86786
No known key found for this signature in database
GPG Key ID: 614E2E1ABEB4B2C0

View File

@ -70,6 +70,81 @@ testCipher1(Buffer.from('MySecretKey123'));
testCipher2('0123456789abcdef');
testCipher2(Buffer.from('0123456789abcdef'));
{
const Cipher = crypto.Cipher;
const instance = crypto.Cipher('aes-256-cbc', 'secret');
assert(instance instanceof Cipher, 'Cipher is expected to return a new ' +
'instance when called without `new`');
common.expectsError(
() => crypto.createCipher(null),
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "cipher" argument must be of type string'
});
common.expectsError(
() => crypto.createCipher('aes-256-cbc', null),
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "password" argument must be one of type string, Buffer, ' +
'TypedArray, or DataView'
});
common.expectsError(
() => crypto.createCipher('aes-256-cbc', 'secret').update(null),
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "data" argument must be one of type string, Buffer, ' +
'TypedArray, or DataView'
});
common.expectsError(
() => crypto.createCipher('aes-256-cbc', 'secret').setAuthTag(null),
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "buffer" argument must be one of type Buffer, ' +
'TypedArray, or DataView'
});
common.expectsError(
() => crypto.createCipher('aes-256-cbc', 'secret').setAAD(null),
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "buffer" argument must be one of type Buffer, ' +
'TypedArray, or DataView'
});
}
{
const Decipher = crypto.Decipher;
const instance = crypto.Decipher('aes-256-cbc', 'secret');
assert(instance instanceof Decipher, 'Decipher is expected to return a new ' +
'instance when called without `new`');
common.expectsError(
() => crypto.createDecipher(null),
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "cipher" argument must be of type string'
});
common.expectsError(
() => crypto.createDecipher('aes-256-cbc', null),
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "password" argument must be one of type string, Buffer, ' +
'TypedArray, or DataView'
});
}
// Base64 padding regression test, see #4837.
{
const c = crypto.createCipher('aes-256-cbc', 'secret');