11cb0ef41Sopenharmony_ci// Copyright Joyent, Inc. and other Node contributors.
21cb0ef41Sopenharmony_ci//
31cb0ef41Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a
41cb0ef41Sopenharmony_ci// copy of this software and associated documentation files (the
51cb0ef41Sopenharmony_ci// "Software"), to deal in the Software without restriction, including
61cb0ef41Sopenharmony_ci// without limitation the rights to use, copy, modify, merge, publish,
71cb0ef41Sopenharmony_ci// distribute, sublicense, and/or sell copies of the Software, and to permit
81cb0ef41Sopenharmony_ci// persons to whom the Software is furnished to do so, subject to the
91cb0ef41Sopenharmony_ci// following conditions:
101cb0ef41Sopenharmony_ci//
111cb0ef41Sopenharmony_ci// The above copyright notice and this permission notice shall be included
121cb0ef41Sopenharmony_ci// in all copies or substantial portions of the Software.
131cb0ef41Sopenharmony_ci//
141cb0ef41Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
151cb0ef41Sopenharmony_ci// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
161cb0ef41Sopenharmony_ci// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
171cb0ef41Sopenharmony_ci// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
181cb0ef41Sopenharmony_ci// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
191cb0ef41Sopenharmony_ci// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
201cb0ef41Sopenharmony_ci// USE OR OTHER DEALINGS IN THE SOFTWARE.
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci// Flags: --pending-deprecation
231cb0ef41Sopenharmony_ci'use strict';
241cb0ef41Sopenharmony_ciconst common = require('../common');
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ciif (!common.hasCrypto)
271cb0ef41Sopenharmony_ci  common.skip('missing crypto');
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ciconst assert = require('assert');
301cb0ef41Sopenharmony_ciconst crypto = require('crypto');
311cb0ef41Sopenharmony_ciconst cryptop = require('crypto').webcrypto;
321cb0ef41Sopenharmony_ciconst { kMaxLength } = require('buffer');
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ciconst kMaxInt32 = 2 ** 31 - 1;
351cb0ef41Sopenharmony_ciconst kMaxPossibleLength = Math.min(kMaxLength, kMaxInt32);
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_cicommon.expectWarning('DeprecationWarning',
381cb0ef41Sopenharmony_ci                     'crypto.pseudoRandomBytes is deprecated.', 'DEP0115');
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci{
411cb0ef41Sopenharmony_ci  [crypto.randomBytes, crypto.pseudoRandomBytes].forEach((f) => {
421cb0ef41Sopenharmony_ci    [undefined, null, false, true, {}, []].forEach((value) => {
431cb0ef41Sopenharmony_ci      const errObj = {
441cb0ef41Sopenharmony_ci        code: 'ERR_INVALID_ARG_TYPE',
451cb0ef41Sopenharmony_ci        name: 'TypeError',
461cb0ef41Sopenharmony_ci        message: 'The "size" argument must be of type number.' +
471cb0ef41Sopenharmony_ci                 common.invalidArgTypeHelper(value)
481cb0ef41Sopenharmony_ci      };
491cb0ef41Sopenharmony_ci      assert.throws(() => f(value), errObj);
501cb0ef41Sopenharmony_ci      assert.throws(() => f(value, common.mustNotCall()), errObj);
511cb0ef41Sopenharmony_ci    });
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci    [-1, NaN, 2 ** 32, 2 ** 31].forEach((value) => {
541cb0ef41Sopenharmony_ci      const errObj = {
551cb0ef41Sopenharmony_ci        code: 'ERR_OUT_OF_RANGE',
561cb0ef41Sopenharmony_ci        name: 'RangeError',
571cb0ef41Sopenharmony_ci        message: 'The value of "size" is out of range. It must be >= 0 && <= ' +
581cb0ef41Sopenharmony_ci                 `${kMaxPossibleLength}. Received ${value}`
591cb0ef41Sopenharmony_ci      };
601cb0ef41Sopenharmony_ci      assert.throws(() => f(value), errObj);
611cb0ef41Sopenharmony_ci      assert.throws(() => f(value, common.mustNotCall()), errObj);
621cb0ef41Sopenharmony_ci    });
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci    [0, 1, 2, 4, 16, 256, 1024, 101.2].forEach((len) => {
651cb0ef41Sopenharmony_ci      f(len, common.mustCall((ex, buf) => {
661cb0ef41Sopenharmony_ci        assert.strictEqual(ex, null);
671cb0ef41Sopenharmony_ci        assert.strictEqual(buf.length, Math.floor(len));
681cb0ef41Sopenharmony_ci        assert.ok(Buffer.isBuffer(buf));
691cb0ef41Sopenharmony_ci      }));
701cb0ef41Sopenharmony_ci    });
711cb0ef41Sopenharmony_ci  });
721cb0ef41Sopenharmony_ci}
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ci{
751cb0ef41Sopenharmony_ci  const buf = Buffer.alloc(10);
761cb0ef41Sopenharmony_ci  const before = buf.toString('hex');
771cb0ef41Sopenharmony_ci  const after = crypto.randomFillSync(buf).toString('hex');
781cb0ef41Sopenharmony_ci  assert.notStrictEqual(before, after);
791cb0ef41Sopenharmony_ci}
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ci{
821cb0ef41Sopenharmony_ci  const buf = new Uint8Array(new Array(10).fill(0));
831cb0ef41Sopenharmony_ci  const before = Buffer.from(buf).toString('hex');
841cb0ef41Sopenharmony_ci  crypto.randomFillSync(buf);
851cb0ef41Sopenharmony_ci  const after = Buffer.from(buf).toString('hex');
861cb0ef41Sopenharmony_ci  assert.notStrictEqual(before, after);
871cb0ef41Sopenharmony_ci}
881cb0ef41Sopenharmony_ci
891cb0ef41Sopenharmony_ci{
901cb0ef41Sopenharmony_ci  [
911cb0ef41Sopenharmony_ci    new Uint16Array(10),
921cb0ef41Sopenharmony_ci    new Uint32Array(10),
931cb0ef41Sopenharmony_ci    new Float32Array(10),
941cb0ef41Sopenharmony_ci    new Float64Array(10),
951cb0ef41Sopenharmony_ci    new DataView(new ArrayBuffer(10)),
961cb0ef41Sopenharmony_ci  ].forEach((buf) => {
971cb0ef41Sopenharmony_ci    const before = Buffer.from(buf.buffer).toString('hex');
981cb0ef41Sopenharmony_ci    crypto.randomFillSync(buf);
991cb0ef41Sopenharmony_ci    const after = Buffer.from(buf.buffer).toString('hex');
1001cb0ef41Sopenharmony_ci    assert.notStrictEqual(before, after);
1011cb0ef41Sopenharmony_ci  });
1021cb0ef41Sopenharmony_ci}
1031cb0ef41Sopenharmony_ci
1041cb0ef41Sopenharmony_ci{
1051cb0ef41Sopenharmony_ci  [
1061cb0ef41Sopenharmony_ci    new Uint16Array(10),
1071cb0ef41Sopenharmony_ci    new Uint32Array(10),
1081cb0ef41Sopenharmony_ci  ].forEach((buf) => {
1091cb0ef41Sopenharmony_ci    const before = Buffer.from(buf.buffer).toString('hex');
1101cb0ef41Sopenharmony_ci    cryptop.getRandomValues(buf);
1111cb0ef41Sopenharmony_ci    const after = Buffer.from(buf.buffer).toString('hex');
1121cb0ef41Sopenharmony_ci    assert.notStrictEqual(before, after);
1131cb0ef41Sopenharmony_ci  });
1141cb0ef41Sopenharmony_ci}
1151cb0ef41Sopenharmony_ci
1161cb0ef41Sopenharmony_ci{
1171cb0ef41Sopenharmony_ci  [
1181cb0ef41Sopenharmony_ci    new ArrayBuffer(10),
1191cb0ef41Sopenharmony_ci    new SharedArrayBuffer(10),
1201cb0ef41Sopenharmony_ci  ].forEach((buf) => {
1211cb0ef41Sopenharmony_ci    const before = Buffer.from(buf).toString('hex');
1221cb0ef41Sopenharmony_ci    crypto.randomFillSync(buf);
1231cb0ef41Sopenharmony_ci    const after = Buffer.from(buf).toString('hex');
1241cb0ef41Sopenharmony_ci    assert.notStrictEqual(before, after);
1251cb0ef41Sopenharmony_ci  });
1261cb0ef41Sopenharmony_ci}
1271cb0ef41Sopenharmony_ci
1281cb0ef41Sopenharmony_ci{
1291cb0ef41Sopenharmony_ci  const buf = Buffer.alloc(10);
1301cb0ef41Sopenharmony_ci  const before = buf.toString('hex');
1311cb0ef41Sopenharmony_ci  crypto.randomFill(buf, common.mustSucceed((buf) => {
1321cb0ef41Sopenharmony_ci    const after = buf.toString('hex');
1331cb0ef41Sopenharmony_ci    assert.notStrictEqual(before, after);
1341cb0ef41Sopenharmony_ci  }));
1351cb0ef41Sopenharmony_ci}
1361cb0ef41Sopenharmony_ci
1371cb0ef41Sopenharmony_ci{
1381cb0ef41Sopenharmony_ci  const buf = new Uint8Array(new Array(10).fill(0));
1391cb0ef41Sopenharmony_ci  const before = Buffer.from(buf).toString('hex');
1401cb0ef41Sopenharmony_ci  crypto.randomFill(buf, common.mustSucceed((buf) => {
1411cb0ef41Sopenharmony_ci    const after = Buffer.from(buf).toString('hex');
1421cb0ef41Sopenharmony_ci    assert.notStrictEqual(before, after);
1431cb0ef41Sopenharmony_ci  }));
1441cb0ef41Sopenharmony_ci}
1451cb0ef41Sopenharmony_ci
1461cb0ef41Sopenharmony_ci{
1471cb0ef41Sopenharmony_ci  [
1481cb0ef41Sopenharmony_ci    new Uint16Array(10),
1491cb0ef41Sopenharmony_ci    new Uint32Array(10),
1501cb0ef41Sopenharmony_ci    new Float32Array(10),
1511cb0ef41Sopenharmony_ci    new Float64Array(10),
1521cb0ef41Sopenharmony_ci    new DataView(new ArrayBuffer(10)),
1531cb0ef41Sopenharmony_ci  ].forEach((buf) => {
1541cb0ef41Sopenharmony_ci    const before = Buffer.from(buf.buffer).toString('hex');
1551cb0ef41Sopenharmony_ci    crypto.randomFill(buf, common.mustSucceed((buf) => {
1561cb0ef41Sopenharmony_ci      const after = Buffer.from(buf.buffer).toString('hex');
1571cb0ef41Sopenharmony_ci      assert.notStrictEqual(before, after);
1581cb0ef41Sopenharmony_ci    }));
1591cb0ef41Sopenharmony_ci  });
1601cb0ef41Sopenharmony_ci}
1611cb0ef41Sopenharmony_ci
1621cb0ef41Sopenharmony_ci{
1631cb0ef41Sopenharmony_ci  [
1641cb0ef41Sopenharmony_ci    new ArrayBuffer(10),
1651cb0ef41Sopenharmony_ci    new SharedArrayBuffer(10),
1661cb0ef41Sopenharmony_ci  ].forEach((buf) => {
1671cb0ef41Sopenharmony_ci    const before = Buffer.from(buf).toString('hex');
1681cb0ef41Sopenharmony_ci    crypto.randomFill(buf, common.mustSucceed((buf) => {
1691cb0ef41Sopenharmony_ci      const after = Buffer.from(buf).toString('hex');
1701cb0ef41Sopenharmony_ci      assert.notStrictEqual(before, after);
1711cb0ef41Sopenharmony_ci    }));
1721cb0ef41Sopenharmony_ci  });
1731cb0ef41Sopenharmony_ci}
1741cb0ef41Sopenharmony_ci
1751cb0ef41Sopenharmony_ci{
1761cb0ef41Sopenharmony_ci  const buf = Buffer.alloc(10);
1771cb0ef41Sopenharmony_ci  const before = buf.toString('hex');
1781cb0ef41Sopenharmony_ci  crypto.randomFillSync(buf, 5, 5);
1791cb0ef41Sopenharmony_ci  const after = buf.toString('hex');
1801cb0ef41Sopenharmony_ci  assert.notStrictEqual(before, after);
1811cb0ef41Sopenharmony_ci  assert.deepStrictEqual(before.slice(0, 5), after.slice(0, 5));
1821cb0ef41Sopenharmony_ci}
1831cb0ef41Sopenharmony_ci
1841cb0ef41Sopenharmony_ci{
1851cb0ef41Sopenharmony_ci  const buf = new Uint8Array(new Array(10).fill(0));
1861cb0ef41Sopenharmony_ci  const before = Buffer.from(buf).toString('hex');
1871cb0ef41Sopenharmony_ci  crypto.randomFillSync(buf, 5, 5);
1881cb0ef41Sopenharmony_ci  const after = Buffer.from(buf).toString('hex');
1891cb0ef41Sopenharmony_ci  assert.notStrictEqual(before, after);
1901cb0ef41Sopenharmony_ci  assert.deepStrictEqual(before.slice(0, 5), after.slice(0, 5));
1911cb0ef41Sopenharmony_ci}
1921cb0ef41Sopenharmony_ci
1931cb0ef41Sopenharmony_ci{
1941cb0ef41Sopenharmony_ci  const buf = Buffer.alloc(10);
1951cb0ef41Sopenharmony_ci  const before = buf.toString('hex');
1961cb0ef41Sopenharmony_ci  crypto.randomFillSync(buf, 5);
1971cb0ef41Sopenharmony_ci  const after = buf.toString('hex');
1981cb0ef41Sopenharmony_ci  assert.notStrictEqual(before, after);
1991cb0ef41Sopenharmony_ci  assert.deepStrictEqual(before.slice(0, 5), after.slice(0, 5));
2001cb0ef41Sopenharmony_ci}
2011cb0ef41Sopenharmony_ci
2021cb0ef41Sopenharmony_ci{
2031cb0ef41Sopenharmony_ci  const buf = Buffer.alloc(10);
2041cb0ef41Sopenharmony_ci  const before = buf.toString('hex');
2051cb0ef41Sopenharmony_ci  crypto.randomFill(buf, 5, 5, common.mustSucceed((buf) => {
2061cb0ef41Sopenharmony_ci    const after = buf.toString('hex');
2071cb0ef41Sopenharmony_ci    assert.notStrictEqual(before, after);
2081cb0ef41Sopenharmony_ci    assert.deepStrictEqual(before.slice(0, 5), after.slice(0, 5));
2091cb0ef41Sopenharmony_ci  }));
2101cb0ef41Sopenharmony_ci}
2111cb0ef41Sopenharmony_ci
2121cb0ef41Sopenharmony_ci{
2131cb0ef41Sopenharmony_ci  const buf = new Uint8Array(new Array(10).fill(0));
2141cb0ef41Sopenharmony_ci  const before = Buffer.from(buf).toString('hex');
2151cb0ef41Sopenharmony_ci  crypto.randomFill(buf, 5, 5, common.mustSucceed((buf) => {
2161cb0ef41Sopenharmony_ci    const after = Buffer.from(buf).toString('hex');
2171cb0ef41Sopenharmony_ci    assert.notStrictEqual(before, after);
2181cb0ef41Sopenharmony_ci    assert.deepStrictEqual(before.slice(0, 5), after.slice(0, 5));
2191cb0ef41Sopenharmony_ci  }));
2201cb0ef41Sopenharmony_ci}
2211cb0ef41Sopenharmony_ci
2221cb0ef41Sopenharmony_ci{
2231cb0ef41Sopenharmony_ci  [
2241cb0ef41Sopenharmony_ci    Buffer.alloc(10),
2251cb0ef41Sopenharmony_ci    new Uint8Array(new Array(10).fill(0)),
2261cb0ef41Sopenharmony_ci  ].forEach((buf) => {
2271cb0ef41Sopenharmony_ci    const len = Buffer.byteLength(buf);
2281cb0ef41Sopenharmony_ci    assert.strictEqual(len, 10, `Expected byteLength of 10, got ${len}`);
2291cb0ef41Sopenharmony_ci
2301cb0ef41Sopenharmony_ci    const typeErrObj = {
2311cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_ARG_TYPE',
2321cb0ef41Sopenharmony_ci      name: 'TypeError',
2331cb0ef41Sopenharmony_ci      message: 'The "offset" argument must be of type number. ' +
2341cb0ef41Sopenharmony_ci               "Received type string ('test')"
2351cb0ef41Sopenharmony_ci    };
2361cb0ef41Sopenharmony_ci
2371cb0ef41Sopenharmony_ci    assert.throws(() => crypto.randomFillSync(buf, 'test'), typeErrObj);
2381cb0ef41Sopenharmony_ci
2391cb0ef41Sopenharmony_ci    assert.throws(
2401cb0ef41Sopenharmony_ci      () => crypto.randomFill(buf, 'test', common.mustNotCall()),
2411cb0ef41Sopenharmony_ci      typeErrObj);
2421cb0ef41Sopenharmony_ci
2431cb0ef41Sopenharmony_ci    typeErrObj.message = typeErrObj.message.replace('offset', 'size');
2441cb0ef41Sopenharmony_ci    assert.throws(() => crypto.randomFillSync(buf, 0, 'test'), typeErrObj);
2451cb0ef41Sopenharmony_ci
2461cb0ef41Sopenharmony_ci    assert.throws(
2471cb0ef41Sopenharmony_ci      () => crypto.randomFill(buf, 0, 'test', common.mustNotCall()),
2481cb0ef41Sopenharmony_ci      typeErrObj
2491cb0ef41Sopenharmony_ci    );
2501cb0ef41Sopenharmony_ci
2511cb0ef41Sopenharmony_ci    [NaN, kMaxPossibleLength + 1, -10, (-1 >>> 0) + 1].forEach((offsetSize) => {
2521cb0ef41Sopenharmony_ci      const errObj = {
2531cb0ef41Sopenharmony_ci        code: 'ERR_OUT_OF_RANGE',
2541cb0ef41Sopenharmony_ci        name: 'RangeError',
2551cb0ef41Sopenharmony_ci        message: 'The value of "offset" is out of range. ' +
2561cb0ef41Sopenharmony_ci                 `It must be >= 0 && <= 10. Received ${offsetSize}`
2571cb0ef41Sopenharmony_ci      };
2581cb0ef41Sopenharmony_ci
2591cb0ef41Sopenharmony_ci      assert.throws(() => crypto.randomFillSync(buf, offsetSize), errObj);
2601cb0ef41Sopenharmony_ci
2611cb0ef41Sopenharmony_ci      assert.throws(
2621cb0ef41Sopenharmony_ci        () => crypto.randomFill(buf, offsetSize, common.mustNotCall()),
2631cb0ef41Sopenharmony_ci        errObj);
2641cb0ef41Sopenharmony_ci
2651cb0ef41Sopenharmony_ci      errObj.message = 'The value of "size" is out of range. It must be >= ' +
2661cb0ef41Sopenharmony_ci                       `0 && <= ${kMaxPossibleLength}. Received ${offsetSize}`;
2671cb0ef41Sopenharmony_ci      assert.throws(() => crypto.randomFillSync(buf, 1, offsetSize), errObj);
2681cb0ef41Sopenharmony_ci
2691cb0ef41Sopenharmony_ci      assert.throws(
2701cb0ef41Sopenharmony_ci        () => crypto.randomFill(buf, 1, offsetSize, common.mustNotCall()),
2711cb0ef41Sopenharmony_ci        errObj
2721cb0ef41Sopenharmony_ci      );
2731cb0ef41Sopenharmony_ci    });
2741cb0ef41Sopenharmony_ci
2751cb0ef41Sopenharmony_ci    const rangeErrObj = {
2761cb0ef41Sopenharmony_ci      code: 'ERR_OUT_OF_RANGE',
2771cb0ef41Sopenharmony_ci      name: 'RangeError',
2781cb0ef41Sopenharmony_ci      message: 'The value of "size + offset" is out of range. ' +
2791cb0ef41Sopenharmony_ci               'It must be <= 10. Received 11'
2801cb0ef41Sopenharmony_ci    };
2811cb0ef41Sopenharmony_ci    assert.throws(() => crypto.randomFillSync(buf, 1, 10), rangeErrObj);
2821cb0ef41Sopenharmony_ci
2831cb0ef41Sopenharmony_ci    assert.throws(
2841cb0ef41Sopenharmony_ci      () => crypto.randomFill(buf, 1, 10, common.mustNotCall()),
2851cb0ef41Sopenharmony_ci      rangeErrObj
2861cb0ef41Sopenharmony_ci    );
2871cb0ef41Sopenharmony_ci  });
2881cb0ef41Sopenharmony_ci}
2891cb0ef41Sopenharmony_ci
2901cb0ef41Sopenharmony_ci// https://github.com/nodejs/node-v0.x-archive/issues/5126,
2911cb0ef41Sopenharmony_ci// "FATAL ERROR: v8::Object::SetIndexedPropertiesToExternalArrayData() length
2921cb0ef41Sopenharmony_ci// exceeds max acceptable value"
2931cb0ef41Sopenharmony_ciassert.throws(
2941cb0ef41Sopenharmony_ci  () => crypto.randomBytes((-1 >>> 0) + 1),
2951cb0ef41Sopenharmony_ci  {
2961cb0ef41Sopenharmony_ci    code: 'ERR_OUT_OF_RANGE',
2971cb0ef41Sopenharmony_ci    name: 'RangeError',
2981cb0ef41Sopenharmony_ci    message: 'The value of "size" is out of range. ' +
2991cb0ef41Sopenharmony_ci             `It must be >= 0 && <= ${kMaxPossibleLength}. Received 4294967296`
3001cb0ef41Sopenharmony_ci  }
3011cb0ef41Sopenharmony_ci);
3021cb0ef41Sopenharmony_ci
3031cb0ef41Sopenharmony_ci[1, true, NaN, null, undefined, {}, []].forEach((i) => {
3041cb0ef41Sopenharmony_ci  const buf = Buffer.alloc(10);
3051cb0ef41Sopenharmony_ci  assert.throws(
3061cb0ef41Sopenharmony_ci    () => crypto.randomFillSync(i),
3071cb0ef41Sopenharmony_ci    {
3081cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_ARG_TYPE',
3091cb0ef41Sopenharmony_ci      name: 'TypeError'
3101cb0ef41Sopenharmony_ci    }
3111cb0ef41Sopenharmony_ci  );
3121cb0ef41Sopenharmony_ci  assert.throws(
3131cb0ef41Sopenharmony_ci    () => crypto.randomFill(i, common.mustNotCall()),
3141cb0ef41Sopenharmony_ci    {
3151cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_ARG_TYPE',
3161cb0ef41Sopenharmony_ci      name: 'TypeError'
3171cb0ef41Sopenharmony_ci    }
3181cb0ef41Sopenharmony_ci  );
3191cb0ef41Sopenharmony_ci  assert.throws(
3201cb0ef41Sopenharmony_ci    () => crypto.randomFill(buf, 0, 10, i),
3211cb0ef41Sopenharmony_ci    {
3221cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_ARG_TYPE',
3231cb0ef41Sopenharmony_ci      name: 'TypeError',
3241cb0ef41Sopenharmony_ci    });
3251cb0ef41Sopenharmony_ci});
3261cb0ef41Sopenharmony_ci
3271cb0ef41Sopenharmony_ci[1, true, NaN, null, {}, []].forEach((i) => {
3281cb0ef41Sopenharmony_ci  assert.throws(
3291cb0ef41Sopenharmony_ci    () => crypto.randomBytes(1, i),
3301cb0ef41Sopenharmony_ci    {
3311cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_ARG_TYPE',
3321cb0ef41Sopenharmony_ci      name: 'TypeError',
3331cb0ef41Sopenharmony_ci    }
3341cb0ef41Sopenharmony_ci  );
3351cb0ef41Sopenharmony_ci});
3361cb0ef41Sopenharmony_ci
3371cb0ef41Sopenharmony_ci['pseudoRandomBytes', 'prng', 'rng'].forEach((f) => {
3381cb0ef41Sopenharmony_ci  const desc = Object.getOwnPropertyDescriptor(crypto, f);
3391cb0ef41Sopenharmony_ci  assert.ok(desc);
3401cb0ef41Sopenharmony_ci  assert.strictEqual(desc.configurable, true);
3411cb0ef41Sopenharmony_ci  assert.strictEqual(desc.enumerable, false);
3421cb0ef41Sopenharmony_ci});
3431cb0ef41Sopenharmony_ci
3441cb0ef41Sopenharmony_ci
3451cb0ef41Sopenharmony_ci{
3461cb0ef41Sopenharmony_ci  // Asynchronous API
3471cb0ef41Sopenharmony_ci  const randomInts = [];
3481cb0ef41Sopenharmony_ci  for (let i = 0; i < 100; i++) {
3491cb0ef41Sopenharmony_ci    crypto.randomInt(3, common.mustSucceed((n) => {
3501cb0ef41Sopenharmony_ci      assert.ok(n >= 0);
3511cb0ef41Sopenharmony_ci      assert.ok(n < 3);
3521cb0ef41Sopenharmony_ci      randomInts.push(n);
3531cb0ef41Sopenharmony_ci      if (randomInts.length === 100) {
3541cb0ef41Sopenharmony_ci        assert.ok(!randomInts.includes(-1));
3551cb0ef41Sopenharmony_ci        assert.ok(randomInts.includes(0));
3561cb0ef41Sopenharmony_ci        assert.ok(randomInts.includes(1));
3571cb0ef41Sopenharmony_ci        assert.ok(randomInts.includes(2));
3581cb0ef41Sopenharmony_ci        assert.ok(!randomInts.includes(3));
3591cb0ef41Sopenharmony_ci      }
3601cb0ef41Sopenharmony_ci    }));
3611cb0ef41Sopenharmony_ci  }
3621cb0ef41Sopenharmony_ci}
3631cb0ef41Sopenharmony_ci{
3641cb0ef41Sopenharmony_ci  // Synchronous API
3651cb0ef41Sopenharmony_ci  const randomInts = [];
3661cb0ef41Sopenharmony_ci  for (let i = 0; i < 100; i++) {
3671cb0ef41Sopenharmony_ci    const n = crypto.randomInt(3);
3681cb0ef41Sopenharmony_ci    assert.ok(n >= 0);
3691cb0ef41Sopenharmony_ci    assert.ok(n < 3);
3701cb0ef41Sopenharmony_ci    randomInts.push(n);
3711cb0ef41Sopenharmony_ci  }
3721cb0ef41Sopenharmony_ci
3731cb0ef41Sopenharmony_ci  assert.ok(!randomInts.includes(-1));
3741cb0ef41Sopenharmony_ci  assert.ok(randomInts.includes(0));
3751cb0ef41Sopenharmony_ci  assert.ok(randomInts.includes(1));
3761cb0ef41Sopenharmony_ci  assert.ok(randomInts.includes(2));
3771cb0ef41Sopenharmony_ci  assert.ok(!randomInts.includes(3));
3781cb0ef41Sopenharmony_ci}
3791cb0ef41Sopenharmony_ci{
3801cb0ef41Sopenharmony_ci  // Positive range
3811cb0ef41Sopenharmony_ci  const randomInts = [];
3821cb0ef41Sopenharmony_ci  for (let i = 0; i < 100; i++) {
3831cb0ef41Sopenharmony_ci    crypto.randomInt(1, 3, common.mustSucceed((n) => {
3841cb0ef41Sopenharmony_ci      assert.ok(n >= 1);
3851cb0ef41Sopenharmony_ci      assert.ok(n < 3);
3861cb0ef41Sopenharmony_ci      randomInts.push(n);
3871cb0ef41Sopenharmony_ci      if (randomInts.length === 100) {
3881cb0ef41Sopenharmony_ci        assert.ok(!randomInts.includes(0));
3891cb0ef41Sopenharmony_ci        assert.ok(randomInts.includes(1));
3901cb0ef41Sopenharmony_ci        assert.ok(randomInts.includes(2));
3911cb0ef41Sopenharmony_ci        assert.ok(!randomInts.includes(3));
3921cb0ef41Sopenharmony_ci      }
3931cb0ef41Sopenharmony_ci    }));
3941cb0ef41Sopenharmony_ci  }
3951cb0ef41Sopenharmony_ci}
3961cb0ef41Sopenharmony_ci{
3971cb0ef41Sopenharmony_ci  // Negative range
3981cb0ef41Sopenharmony_ci  const randomInts = [];
3991cb0ef41Sopenharmony_ci  for (let i = 0; i < 100; i++) {
4001cb0ef41Sopenharmony_ci    crypto.randomInt(-10, -8, common.mustSucceed((n) => {
4011cb0ef41Sopenharmony_ci      assert.ok(n >= -10);
4021cb0ef41Sopenharmony_ci      assert.ok(n < -8);
4031cb0ef41Sopenharmony_ci      randomInts.push(n);
4041cb0ef41Sopenharmony_ci      if (randomInts.length === 100) {
4051cb0ef41Sopenharmony_ci        assert.ok(!randomInts.includes(-11));
4061cb0ef41Sopenharmony_ci        assert.ok(randomInts.includes(-10));
4071cb0ef41Sopenharmony_ci        assert.ok(randomInts.includes(-9));
4081cb0ef41Sopenharmony_ci        assert.ok(!randomInts.includes(-8));
4091cb0ef41Sopenharmony_ci      }
4101cb0ef41Sopenharmony_ci    }));
4111cb0ef41Sopenharmony_ci  }
4121cb0ef41Sopenharmony_ci}
4131cb0ef41Sopenharmony_ci{
4141cb0ef41Sopenharmony_ci
4151cb0ef41Sopenharmony_ci  ['10', true, NaN, null, {}, []].forEach((i) => {
4161cb0ef41Sopenharmony_ci    const invalidMinError = {
4171cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_ARG_TYPE',
4181cb0ef41Sopenharmony_ci      name: 'TypeError',
4191cb0ef41Sopenharmony_ci      message: 'The "min" argument must be a safe integer.' +
4201cb0ef41Sopenharmony_ci               `${common.invalidArgTypeHelper(i)}`,
4211cb0ef41Sopenharmony_ci    };
4221cb0ef41Sopenharmony_ci    const invalidMaxError = {
4231cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_ARG_TYPE',
4241cb0ef41Sopenharmony_ci      name: 'TypeError',
4251cb0ef41Sopenharmony_ci      message: 'The "max" argument must be a safe integer.' +
4261cb0ef41Sopenharmony_ci               `${common.invalidArgTypeHelper(i)}`,
4271cb0ef41Sopenharmony_ci    };
4281cb0ef41Sopenharmony_ci
4291cb0ef41Sopenharmony_ci    assert.throws(
4301cb0ef41Sopenharmony_ci      () => crypto.randomInt(i, 100),
4311cb0ef41Sopenharmony_ci      invalidMinError
4321cb0ef41Sopenharmony_ci    );
4331cb0ef41Sopenharmony_ci    assert.throws(
4341cb0ef41Sopenharmony_ci      () => crypto.randomInt(i, 100, common.mustNotCall()),
4351cb0ef41Sopenharmony_ci      invalidMinError
4361cb0ef41Sopenharmony_ci    );
4371cb0ef41Sopenharmony_ci    assert.throws(
4381cb0ef41Sopenharmony_ci      () => crypto.randomInt(i),
4391cb0ef41Sopenharmony_ci      invalidMaxError
4401cb0ef41Sopenharmony_ci    );
4411cb0ef41Sopenharmony_ci    assert.throws(
4421cb0ef41Sopenharmony_ci      () => crypto.randomInt(i, common.mustNotCall()),
4431cb0ef41Sopenharmony_ci      invalidMaxError
4441cb0ef41Sopenharmony_ci    );
4451cb0ef41Sopenharmony_ci    assert.throws(
4461cb0ef41Sopenharmony_ci      () => crypto.randomInt(0, i, common.mustNotCall()),
4471cb0ef41Sopenharmony_ci      invalidMaxError
4481cb0ef41Sopenharmony_ci    );
4491cb0ef41Sopenharmony_ci    assert.throws(
4501cb0ef41Sopenharmony_ci      () => crypto.randomInt(0, i),
4511cb0ef41Sopenharmony_ci      invalidMaxError
4521cb0ef41Sopenharmony_ci    );
4531cb0ef41Sopenharmony_ci  });
4541cb0ef41Sopenharmony_ci
4551cb0ef41Sopenharmony_ci  const maxInt = Number.MAX_SAFE_INTEGER;
4561cb0ef41Sopenharmony_ci  const minInt = Number.MIN_SAFE_INTEGER;
4571cb0ef41Sopenharmony_ci
4581cb0ef41Sopenharmony_ci  crypto.randomInt(minInt, minInt + 5, common.mustSucceed());
4591cb0ef41Sopenharmony_ci  crypto.randomInt(maxInt - 5, maxInt, common.mustSucceed());
4601cb0ef41Sopenharmony_ci
4611cb0ef41Sopenharmony_ci  assert.throws(
4621cb0ef41Sopenharmony_ci    () => crypto.randomInt(minInt - 1, minInt + 5, common.mustNotCall()),
4631cb0ef41Sopenharmony_ci    {
4641cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_ARG_TYPE',
4651cb0ef41Sopenharmony_ci      name: 'TypeError',
4661cb0ef41Sopenharmony_ci      message: 'The "min" argument must be a safe integer.' +
4671cb0ef41Sopenharmony_ci      `${common.invalidArgTypeHelper(minInt - 1)}`,
4681cb0ef41Sopenharmony_ci    }
4691cb0ef41Sopenharmony_ci  );
4701cb0ef41Sopenharmony_ci
4711cb0ef41Sopenharmony_ci  assert.throws(
4721cb0ef41Sopenharmony_ci    () => crypto.randomInt(maxInt + 1, common.mustNotCall()),
4731cb0ef41Sopenharmony_ci    {
4741cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_ARG_TYPE',
4751cb0ef41Sopenharmony_ci      name: 'TypeError',
4761cb0ef41Sopenharmony_ci      message: 'The "max" argument must be a safe integer.' +
4771cb0ef41Sopenharmony_ci      `${common.invalidArgTypeHelper(maxInt + 1)}`,
4781cb0ef41Sopenharmony_ci    }
4791cb0ef41Sopenharmony_ci  );
4801cb0ef41Sopenharmony_ci
4811cb0ef41Sopenharmony_ci  crypto.randomInt(1, common.mustSucceed());
4821cb0ef41Sopenharmony_ci  crypto.randomInt(0, 1, common.mustSucceed());
4831cb0ef41Sopenharmony_ci  for (const arg of [[0], [1, 1], [3, 2], [-5, -5], [11, -10]]) {
4841cb0ef41Sopenharmony_ci    assert.throws(() => crypto.randomInt(...arg, common.mustNotCall()), {
4851cb0ef41Sopenharmony_ci      code: 'ERR_OUT_OF_RANGE',
4861cb0ef41Sopenharmony_ci      name: 'RangeError',
4871cb0ef41Sopenharmony_ci      message: 'The value of "max" is out of range. It must be greater than ' +
4881cb0ef41Sopenharmony_ci      `the value of "min" (${arg[arg.length - 2] || 0}). ` +
4891cb0ef41Sopenharmony_ci      `Received ${arg[arg.length - 1]}`
4901cb0ef41Sopenharmony_ci    });
4911cb0ef41Sopenharmony_ci  }
4921cb0ef41Sopenharmony_ci
4931cb0ef41Sopenharmony_ci  const MAX_RANGE = 0xFFFF_FFFF_FFFF;
4941cb0ef41Sopenharmony_ci  crypto.randomInt(MAX_RANGE, common.mustSucceed());
4951cb0ef41Sopenharmony_ci  crypto.randomInt(1, MAX_RANGE + 1, common.mustSucceed());
4961cb0ef41Sopenharmony_ci  assert.throws(
4971cb0ef41Sopenharmony_ci    () => crypto.randomInt(1, MAX_RANGE + 2, common.mustNotCall()),
4981cb0ef41Sopenharmony_ci    {
4991cb0ef41Sopenharmony_ci      code: 'ERR_OUT_OF_RANGE',
5001cb0ef41Sopenharmony_ci      name: 'RangeError',
5011cb0ef41Sopenharmony_ci      message: 'The value of "max - min" is out of range. ' +
5021cb0ef41Sopenharmony_ci               `It must be <= ${MAX_RANGE}. ` +
5031cb0ef41Sopenharmony_ci               'Received 281_474_976_710_656'
5041cb0ef41Sopenharmony_ci    }
5051cb0ef41Sopenharmony_ci  );
5061cb0ef41Sopenharmony_ci
5071cb0ef41Sopenharmony_ci  assert.throws(() => crypto.randomInt(MAX_RANGE + 1, common.mustNotCall()), {
5081cb0ef41Sopenharmony_ci    code: 'ERR_OUT_OF_RANGE',
5091cb0ef41Sopenharmony_ci    name: 'RangeError',
5101cb0ef41Sopenharmony_ci    message: 'The value of "max" is out of range. ' +
5111cb0ef41Sopenharmony_ci             `It must be <= ${MAX_RANGE}. ` +
5121cb0ef41Sopenharmony_ci             'Received 281_474_976_710_656'
5131cb0ef41Sopenharmony_ci  });
5141cb0ef41Sopenharmony_ci
5151cb0ef41Sopenharmony_ci  [true, NaN, null, {}, [], 10].forEach((i) => {
5161cb0ef41Sopenharmony_ci    const cbError = {
5171cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_ARG_TYPE',
5181cb0ef41Sopenharmony_ci      name: 'TypeError',
5191cb0ef41Sopenharmony_ci    };
5201cb0ef41Sopenharmony_ci    assert.throws(() => crypto.randomInt(0, 1, i), cbError);
5211cb0ef41Sopenharmony_ci  });
5221cb0ef41Sopenharmony_ci}
5231cb0ef41Sopenharmony_ci
5241cb0ef41Sopenharmony_ci{
5251cb0ef41Sopenharmony_ci  // Verify that it doesn't throw or abort
5261cb0ef41Sopenharmony_ci  crypto.randomFill(new Uint16Array(10), 0, common.mustSucceed());
5271cb0ef41Sopenharmony_ci  crypto.randomFill(new Uint32Array(10), 0, common.mustSucceed());
5281cb0ef41Sopenharmony_ci  crypto.randomFill(new Uint32Array(10), 0, 1, common.mustSucceed());
5291cb0ef41Sopenharmony_ci}
530