Skip to content

Commit

Permalink
test: improve coverage for Cipher and Decipher
Browse files Browse the repository at this point in the history
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: #17449
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
Reviewed-By: Jon Moss <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
  • Loading branch information
Leko authored and apapirovski committed Dec 8, 2017
1 parent de3a7db commit 84b7a86
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions test/parallel/test-crypto-cipher-decipher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 84b7a86

Please sign in to comment.