summaryrefslogtreecommitdiff
path: root/test/parallel/test-crypto-pbkdf2.js
diff options
context:
space:
mode:
authorRuben Bridgewater <ruben@bridgewater.de>2018-04-11 03:10:22 +0200
committerRuben Bridgewater <ruben@bridgewater.de>2018-04-13 19:59:44 +0200
commitdca7fb2225ea5bd8452c51d4167962f2838389f8 (patch)
tree3494051d50e3815e965b56142d57f8ff71916d09 /test/parallel/test-crypto-pbkdf2.js
parentd5495e859c3aceab7f8a823e27950e715b746a35 (diff)
downloadandroid-node-v8-dca7fb2225ea5bd8452c51d4167962f2838389f8.tar.gz
android-node-v8-dca7fb2225ea5bd8452c51d4167962f2838389f8.tar.bz2
android-node-v8-dca7fb2225ea5bd8452c51d4167962f2838389f8.zip
errors: validate input arguments
This makes sure the input arguments get validated so implementation errors will be caught early. It also improves a couple of error messages by providing more detailed information and fixes errors detected by the new functionality. Besides that a error type got simplified and tests got refactored. PR-URL: https://github.com/nodejs/node/pull/19924 Reviewed-By: Michaƫl Zasso <targos@protonmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'test/parallel/test-crypto-pbkdf2.js')
-rw-r--r--test/parallel/test-crypto-pbkdf2.js63
1 files changed, 32 insertions, 31 deletions
diff --git a/test/parallel/test-crypto-pbkdf2.js b/test/parallel/test-crypto-pbkdf2.js
index d3f02cf8bd..b4ecd3c606 100644
--- a/test/parallel/test-crypto-pbkdf2.js
+++ b/test/parallel/test-crypto-pbkdf2.js
@@ -57,45 +57,46 @@ function ondone(err, key) {
}
// Error path should not leak memory (check with valgrind).
-common.expectsError(
+assert.throws(
() => crypto.pbkdf2('password', 'salt', 1, 20, null),
{
code: 'ERR_INVALID_CALLBACK',
- type: TypeError
+ name: 'TypeError [ERR_INVALID_CALLBACK]'
}
);
-common.expectsError(
+assert.throws(
() => crypto.pbkdf2Sync('password', 'salt', -1, 20, null),
{
code: 'ERR_OUT_OF_RANGE',
- type: RangeError,
+ name: 'RangeError [ERR_OUT_OF_RANGE]',
message: 'The value of "iterations" is out of range. ' +
'It must be a non-negative number. Received -1'
}
);
['str', null, undefined, [], {}].forEach((notNumber) => {
- common.expectsError(
+ assert.throws(
() => {
crypto.pbkdf2Sync('password', 'salt', 1, notNumber, 'sha256');
}, {
code: 'ERR_INVALID_ARG_TYPE',
- type: TypeError,
+ name: 'TypeError [ERR_INVALID_ARG_TYPE]',
message: 'The "keylen" argument must be of type number. ' +
`Received type ${typeof notNumber}`
});
});
-[Infinity, -Infinity, NaN, -1, 4073741824, INT_MAX + 1].forEach((i) => {
- common.expectsError(
+[Infinity, -Infinity, NaN, -1, 4073741824, INT_MAX + 1].forEach((input) => {
+ assert.throws(
() => {
- crypto.pbkdf2('password', 'salt', 1, i, 'sha256',
+ crypto.pbkdf2('password', 'salt', 1, input, 'sha256',
common.mustNotCall());
}, {
code: 'ERR_OUT_OF_RANGE',
- type: RangeError,
- message: 'The value of "keylen" is out of range.'
+ name: 'RangeError [ERR_OUT_OF_RANGE]',
+ message: 'The value of "keylen" is out of range. It ' +
+ `must be >= 0 && <= 2147483647. Received ${input}`
});
});
@@ -103,58 +104,58 @@ common.expectsError(
// https://github.com/nodejs/node/issues/8571
crypto.pbkdf2('', '', 1, 32, 'sha256', common.mustCall(assert.ifError));
-common.expectsError(
+assert.throws(
() => crypto.pbkdf2('password', 'salt', 8, 8, common.mustNotCall()),
{
code: 'ERR_INVALID_ARG_TYPE',
- type: TypeError,
+ name: 'TypeError [ERR_INVALID_ARG_TYPE]',
message: 'The "digest" argument must be one of type string or null. ' +
'Received type undefined'
});
-common.expectsError(
+assert.throws(
() => crypto.pbkdf2Sync('password', 'salt', 8, 8),
{
code: 'ERR_INVALID_ARG_TYPE',
- type: TypeError,
+ name: 'TypeError [ERR_INVALID_ARG_TYPE]',
message: 'The "digest" argument must be one of type string or null. ' +
'Received type undefined'
});
[1, {}, [], true, undefined, null].forEach((input) => {
const msgPart2 = `Buffer, or TypedArray. Received type ${typeof input}`;
- common.expectsError(
+ assert.throws(
() => crypto.pbkdf2(input, 'salt', 8, 8, 'sha256', common.mustNotCall()),
{
code: 'ERR_INVALID_ARG_TYPE',
- type: TypeError,
+ name: 'TypeError [ERR_INVALID_ARG_TYPE]',
message: `The "password" argument must be one of type string, ${msgPart2}`
}
);
- common.expectsError(
+ assert.throws(
() => crypto.pbkdf2('pass', input, 8, 8, 'sha256', common.mustNotCall()),
{
code: 'ERR_INVALID_ARG_TYPE',
- type: TypeError,
+ name: 'TypeError [ERR_INVALID_ARG_TYPE]',
message: `The "salt" argument must be one of type string, ${msgPart2}`
}
);
- common.expectsError(
+ assert.throws(
() => crypto.pbkdf2Sync(input, 'salt', 8, 8, 'sha256'),
{
code: 'ERR_INVALID_ARG_TYPE',
- type: TypeError,
+ name: 'TypeError [ERR_INVALID_ARG_TYPE]',
message: `The "password" argument must be one of type string, ${msgPart2}`
}
);
- common.expectsError(
+ assert.throws(
() => crypto.pbkdf2Sync('pass', input, 8, 8, 'sha256'),
{
code: 'ERR_INVALID_ARG_TYPE',
- type: TypeError,
+ name: 'TypeError [ERR_INVALID_ARG_TYPE]',
message: `The "salt" argument must be one of type string, ${msgPart2}`
}
);
@@ -162,20 +163,20 @@ common.expectsError(
['test', {}, [], true, undefined, null].forEach((i) => {
const received = `Received type ${typeof i}`;
- common.expectsError(
+ assert.throws(
() => crypto.pbkdf2('pass', 'salt', i, 8, 'sha256', common.mustNotCall()),
{
code: 'ERR_INVALID_ARG_TYPE',
- type: TypeError,
+ name: 'TypeError [ERR_INVALID_ARG_TYPE]',
message: `The "iterations" argument must be of type number. ${received}`
}
);
- common.expectsError(
+ assert.throws(
() => crypto.pbkdf2Sync('pass', 'salt', i, 8, 'sha256'),
{
code: 'ERR_INVALID_ARG_TYPE',
- type: TypeError,
+ name: 'TypeError [ERR_INVALID_ARG_TYPE]',
message: `The "iterations" argument must be of type number. ${received}`
}
);
@@ -204,20 +205,20 @@ crypto.pbkdf2Sync('pass', new Float32Array(10), 8, 8, 'sha256');
crypto.pbkdf2Sync(new Float64Array(10), 'salt', 8, 8, 'sha256');
crypto.pbkdf2Sync('pass', new Float64Array(10), 8, 8, 'sha256');
-common.expectsError(
+assert.throws(
() => crypto.pbkdf2('pass', 'salt', 8, 8, 'md55', common.mustNotCall()),
{
code: 'ERR_CRYPTO_INVALID_DIGEST',
- type: TypeError,
+ name: 'TypeError [ERR_CRYPTO_INVALID_DIGEST]',
message: 'Invalid digest: md55'
}
);
-common.expectsError(
+assert.throws(
() => crypto.pbkdf2Sync('pass', 'salt', 8, 8, 'md55'),
{
code: 'ERR_CRYPTO_INVALID_DIGEST',
- type: TypeError,
+ name: 'TypeError [ERR_CRYPTO_INVALID_DIGEST]',
message: 'Invalid digest: md55'
}
);