node/test/parallel/test-webcrypto-random.js
LiviaMedeiros 188c3ab918
crypto: adjust types for getRandomValues
prevents Web Crypto API's getRandomValues from accepting DataView

Fixes: https://github.com/nodejs/node/issues/41480
Refs: https://www.w3.org/TR/WebCryptoAPI/#Crypto-method-getRandomValues

PR-URL: https://github.com/nodejs/node/pull/41481
Reviewed-By: Derek Lewis <DerekNonGeneric@inf.is>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Zeyu Yang <himself65@outlook.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
2022-03-14 09:32:01 -04:00

70 lines
1.4 KiB
JavaScript

'use strict';
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
const { Buffer } = require('buffer');
const assert = require('assert');
const { getRandomValues } = require('crypto').webcrypto;
[
undefined, null, '', 1, {}, [],
new Float32Array(1),
new Float64Array(1),
new DataView(new ArrayBuffer(1)),
].forEach((i) => {
assert.throws(
() => getRandomValues(i),
{ name: 'TypeMismatchError', code: 17 },
);
});
{
const buf = new Uint8Array(0);
getRandomValues(buf);
}
const intTypedConstructors = [
Int8Array,
Int16Array,
Int32Array,
Uint8Array,
Uint16Array,
Uint32Array,
Uint8ClampedArray,
BigInt64Array,
BigUint64Array,
];
for (const ctor of intTypedConstructors) {
const buf = new ctor(10);
const before = Buffer.from(buf.buffer).toString('hex');
getRandomValues(buf);
const after = Buffer.from(buf.buffer).toString('hex');
assert.notStrictEqual(before, after);
}
{
const buf = new Uint16Array(10);
const before = Buffer.from(buf).toString('hex');
getRandomValues(buf);
const after = Buffer.from(buf).toString('hex');
assert.notStrictEqual(before, after);
}
{
let kData;
try {
kData = Buffer.alloc(65536 + 1);
} catch {
// Ignore if error here.
}
if (kData !== undefined) {
assert.throws(() => getRandomValues(kData), {
code: 22
});
}
}