Skip to content
This repository has been archived by the owner on Oct 23, 2020. It is now read-only.

Commit

Permalink
Improve coverage of lib/random.js
Browse files Browse the repository at this point in the history
  • Loading branch information
tniessen committed Dec 20, 2019
1 parent b1ef533 commit 08f4feb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/random.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ function isIntegerTypedArray(o) {

// Spec: https://www.w3.org/TR/WebCryptoAPI/#dfn-Crypto-method-getRandomValues
module.exports.getRandomValues = (array) => {
if (!ArrayBuffer.isView(array))
throw new TypeError();

if (!isIntegerTypedArray(array))
throw new TypeMismatchError();

Expand Down
19 changes: 19 additions & 0 deletions test/unit/random.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,23 @@ describe('crypto.getRandomBytes', () => {
const buf2 = getRandomValues(Buffer.alloc(1024));
assert(!buf1.equals(buf2));
});

it('should throw if the input is invalid', () => {
for (const notAnArrayBufferView of [undefined, null, 5, 'foo']) {
assert.throws(() => {
getRandomValues(notAnArrayBufferView);
}, /TypeError/);
}

const buf = new ArrayBuffer(65544);
for (const View of [Float32Array, Float64Array]) {
assert.throws(() => {
getRandomValues(new View(buf));
}, /TypeMismatchError/);
}

assert.throws(() => {
getRandomValues(new Uint32Array(buf));
}, /QuotaExceededError/);
});
});

0 comments on commit 08f4feb

Please sign in to comment.