Skip to content

Commit

Permalink
test: use common.mustNotCall in test-crypto-random
Browse files Browse the repository at this point in the history
Use `common.mustNotCall()` to confirm that noop callbacks are not run
when functions throw errors.

PR-URL: #13183
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Daniel Bevenius <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
  • Loading branch information
Trott committed May 26, 2017
1 parent 49e91e2 commit 112ef23
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions test/parallel/test-crypto-random.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ const expectedErrorRegexp = /^TypeError: size must be a number >= 0$/;
[crypto.randomBytes, crypto.pseudoRandomBytes].forEach(function(f) {
[-1, undefined, null, false, true, {}, []].forEach(function(value) {
assert.throws(function() { f(value); }, expectedErrorRegexp);
assert.throws(function() { f(value, common.noop); }, expectedErrorRegexp);
assert.throws(function() { f(value, common.mustNotCall()); },
expectedErrorRegexp);
});

[0, 1, 2, 4, 16, 256, 1024].forEach(function(len) {
Expand Down Expand Up @@ -153,11 +154,11 @@ const expectedErrorRegexp = /^TypeError: size must be a number >= 0$/;
}, /offset must be a number/);

assert.throws(() => {
crypto.randomFill(buf, 'test', common.noop);
crypto.randomFill(buf, 'test', common.mustNotCall());
}, /offset must be a number/);

assert.throws(() => {
crypto.randomFill(buf, NaN, common.noop);
crypto.randomFill(buf, NaN, common.mustNotCall());
}, /offset must be a number/);

const max = require('buffer').kMaxLength + 1;
Expand All @@ -171,11 +172,11 @@ const expectedErrorRegexp = /^TypeError: size must be a number >= 0$/;
}, /offset out of range/);

assert.throws(() => {
crypto.randomFill(buf, 11, common.noop);
crypto.randomFill(buf, 11, common.mustNotCall());
}, /offset out of range/);

assert.throws(() => {
crypto.randomFill(buf, max, common.noop);
crypto.randomFill(buf, max, common.mustNotCall());
}, /offset out of range/);

assert.throws(() => {
Expand All @@ -187,11 +188,11 @@ const expectedErrorRegexp = /^TypeError: size must be a number >= 0$/;
}, /size must be a number/);

assert.throws(() => {
crypto.randomFill(buf, 0, 'test', common.noop);
crypto.randomFill(buf, 0, 'test', common.mustNotCall());
}, /size must be a number/);

assert.throws(() => {
crypto.randomFill(buf, 0, NaN, common.noop);
crypto.randomFill(buf, 0, NaN, common.mustNotCall());
}, /size must be a number/);

{
Expand All @@ -206,11 +207,11 @@ const expectedErrorRegexp = /^TypeError: size must be a number >= 0$/;
}, /size must be a uint32/);

assert.throws(() => {
crypto.randomFill(buf, 0, -10, common.noop);
crypto.randomFill(buf, 0, -10, common.mustNotCall());
}, /size must be a uint32/);

assert.throws(() => {
crypto.randomFill(buf, 0, size, common.noop);
crypto.randomFill(buf, 0, size, common.mustNotCall());
}, /size must be a uint32/);
}

Expand All @@ -219,23 +220,23 @@ const expectedErrorRegexp = /^TypeError: size must be a number >= 0$/;
}, /offset must be a uint32/);

assert.throws(() => {
crypto.randomFill(buf, -10, common.noop);
crypto.randomFill(buf, -10, common.mustNotCall());
}, /offset must be a uint32/);

assert.throws(() => {
crypto.randomFillSync(buf, 1, 10);
}, /buffer too small/);

assert.throws(() => {
crypto.randomFill(buf, 1, 10, common.noop);
crypto.randomFill(buf, 1, 10, common.mustNotCall());
}, /buffer too small/);

assert.throws(() => {
crypto.randomFillSync(buf, 0, 12);
}, /buffer too small/);

assert.throws(() => {
crypto.randomFill(buf, 0, 12, common.noop);
crypto.randomFill(buf, 0, 12, common.mustNotCall());
}, /buffer too small/);

{
Expand All @@ -246,7 +247,7 @@ const expectedErrorRegexp = /^TypeError: size must be a number >= 0$/;
}, /offset must be a uint32/);

assert.throws(() => {
crypto.randomFill(buf, offset, 10, common.noop);
crypto.randomFill(buf, offset, 10, common.mustNotCall());
}, /offset must be a uint32/);
}
}
Expand Down

0 comments on commit 112ef23

Please sign in to comment.