From 84b7a8678690a937b70a8edcfe47208d6d402215 Mon Sep 17 00:00:00 2001 From: Leko Date: Mon, 4 Dec 2017 17:23:59 +0900 Subject: [PATCH] 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 Reviewed-By: Michael Dawson Reviewed-By: Jon Moss Reviewed-By: James M Snell Reviewed-By: Ruben Bridgewater --- test/parallel/test-crypto-cipher-decipher.js | 75 ++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/test/parallel/test-crypto-cipher-decipher.js b/test/parallel/test-crypto-cipher-decipher.js index 78172d00035..2a4d188dc4b 100644 --- a/test/parallel/test-crypto-cipher-decipher.js +++ b/test/parallel/test-crypto-cipher-decipher.js @@ -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');