1'use strict';
2
3const common = require('../common');
4
5if (!common.hasCrypto)
6  common.skip('missing crypto');
7
8const { Buffer } = require('buffer');
9const assert = require('assert');
10const { webcrypto } = require('crypto');
11
12[
13  undefined, null, '', 1, {}, [],
14  new Float32Array(1),
15  new Float64Array(1),
16  new DataView(new ArrayBuffer(1)),
17].forEach((i) => {
18  assert.throws(
19    () => webcrypto.getRandomValues(i),
20    { name: 'TypeMismatchError', code: 17 },
21  );
22});
23
24{
25  const buf = new Uint8Array(0);
26  webcrypto.getRandomValues(buf);
27}
28
29const intTypedConstructors = [
30  Int8Array,
31  Int16Array,
32  Int32Array,
33  Uint8Array,
34  Uint16Array,
35  Uint32Array,
36  Uint8ClampedArray,
37  BigInt64Array,
38  BigUint64Array,
39];
40
41for (const ctor of intTypedConstructors) {
42  const buf = new ctor(10);
43  const before = Buffer.from(buf.buffer).toString('hex');
44  webcrypto.getRandomValues(buf);
45  const after = Buffer.from(buf.buffer).toString('hex');
46  assert.notStrictEqual(before, after);
47}
48
49{
50  const buf = Buffer.alloc(10);
51  const before = buf.toString('hex');
52  webcrypto.getRandomValues(buf);
53  const after = buf.toString('hex');
54  assert.notStrictEqual(before, after);
55}
56
57{
58  let kData;
59  try {
60    kData = Buffer.alloc(65536 + 1);
61  } catch {
62    // Ignore if error here.
63  }
64
65  if (kData !== undefined) {
66    assert.throws(
67      () => webcrypto.getRandomValues(kData),
68      { name: 'QuotaExceededError', code: 22 },
69    );
70  }
71}
72