aboutsummaryrefslogtreecommitdiff
path: root/test/parallel/test-webcrypto-random.js
blob: fd933915c5e48eaab6ffea2fd2e11beaba38a32f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
'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, {}, []].forEach((i) => {
  assert.throws(() => getRandomValues(i), { code: 17 });
});

{
  const buf = new Uint8Array(0);
  getRandomValues(buf);
}

{
  const buf = new Uint8Array(new Array(10).fill(0));
  const before = Buffer.from(buf).toString('hex');
  getRandomValues(buf);
  const after = Buffer.from(buf).toString('hex');
  assert.notStrictEqual(before, after);
}

{
  const buf = new Uint16Array(new Array(10).fill(0));
  const before = Buffer.from(buf).toString('hex');
  getRandomValues(buf);
  const after = Buffer.from(buf).toString('hex');
  assert.notStrictEqual(before, after);
}

{
  const buf = new Uint32Array(new Array(10).fill(0));
  const before = Buffer.from(buf).toString('hex');
  getRandomValues(buf);
  const after = Buffer.from(buf).toString('hex');
  assert.notStrictEqual(before, after);
}

{
  const buf = new Uint16Array(new Array(10).fill(0));
  const before = Buffer.from(buf).toString('hex');
  getRandomValues(new DataView(buf.buffer));
  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
    });
  }
}