11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciif (!common.hasCrypto)
61cb0ef41Sopenharmony_ci  common.skip('missing crypto');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst { Buffer } = require('buffer');
91cb0ef41Sopenharmony_ciconst assert = require('assert');
101cb0ef41Sopenharmony_ciconst { webcrypto } = require('crypto');
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci[
131cb0ef41Sopenharmony_ci  undefined, null, '', 1, {}, [],
141cb0ef41Sopenharmony_ci  new Float32Array(1),
151cb0ef41Sopenharmony_ci  new Float64Array(1),
161cb0ef41Sopenharmony_ci  new DataView(new ArrayBuffer(1)),
171cb0ef41Sopenharmony_ci].forEach((i) => {
181cb0ef41Sopenharmony_ci  assert.throws(
191cb0ef41Sopenharmony_ci    () => webcrypto.getRandomValues(i),
201cb0ef41Sopenharmony_ci    { name: 'TypeMismatchError', code: 17 },
211cb0ef41Sopenharmony_ci  );
221cb0ef41Sopenharmony_ci});
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci{
251cb0ef41Sopenharmony_ci  const buf = new Uint8Array(0);
261cb0ef41Sopenharmony_ci  webcrypto.getRandomValues(buf);
271cb0ef41Sopenharmony_ci}
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ciconst intTypedConstructors = [
301cb0ef41Sopenharmony_ci  Int8Array,
311cb0ef41Sopenharmony_ci  Int16Array,
321cb0ef41Sopenharmony_ci  Int32Array,
331cb0ef41Sopenharmony_ci  Uint8Array,
341cb0ef41Sopenharmony_ci  Uint16Array,
351cb0ef41Sopenharmony_ci  Uint32Array,
361cb0ef41Sopenharmony_ci  Uint8ClampedArray,
371cb0ef41Sopenharmony_ci  BigInt64Array,
381cb0ef41Sopenharmony_ci  BigUint64Array,
391cb0ef41Sopenharmony_ci];
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_cifor (const ctor of intTypedConstructors) {
421cb0ef41Sopenharmony_ci  const buf = new ctor(10);
431cb0ef41Sopenharmony_ci  const before = Buffer.from(buf.buffer).toString('hex');
441cb0ef41Sopenharmony_ci  webcrypto.getRandomValues(buf);
451cb0ef41Sopenharmony_ci  const after = Buffer.from(buf.buffer).toString('hex');
461cb0ef41Sopenharmony_ci  assert.notStrictEqual(before, after);
471cb0ef41Sopenharmony_ci}
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci{
501cb0ef41Sopenharmony_ci  const buf = Buffer.alloc(10);
511cb0ef41Sopenharmony_ci  const before = buf.toString('hex');
521cb0ef41Sopenharmony_ci  webcrypto.getRandomValues(buf);
531cb0ef41Sopenharmony_ci  const after = buf.toString('hex');
541cb0ef41Sopenharmony_ci  assert.notStrictEqual(before, after);
551cb0ef41Sopenharmony_ci}
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci{
581cb0ef41Sopenharmony_ci  let kData;
591cb0ef41Sopenharmony_ci  try {
601cb0ef41Sopenharmony_ci    kData = Buffer.alloc(65536 + 1);
611cb0ef41Sopenharmony_ci  } catch {
621cb0ef41Sopenharmony_ci    // Ignore if error here.
631cb0ef41Sopenharmony_ci  }
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ci  if (kData !== undefined) {
661cb0ef41Sopenharmony_ci    assert.throws(
671cb0ef41Sopenharmony_ci      () => webcrypto.getRandomValues(kData),
681cb0ef41Sopenharmony_ci      { name: 'QuotaExceededError', code: 22 },
691cb0ef41Sopenharmony_ci    );
701cb0ef41Sopenharmony_ci  }
711cb0ef41Sopenharmony_ci}
72