11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciif (!common.hasCrypto)
51cb0ef41Sopenharmony_ci  common.skip('missing crypto');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciif (common.isWindows)
81cb0ef41Sopenharmony_ci  common.skip('Not supported on Windows');
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciif (process.config.variables.asan)
111cb0ef41Sopenharmony_ci  common.skip('ASAN does not play well with secure heap allocations');
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciconst assert = require('assert');
141cb0ef41Sopenharmony_ciconst { fork } = require('child_process');
151cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
161cb0ef41Sopenharmony_ciconst {
171cb0ef41Sopenharmony_ci  secureHeapUsed,
181cb0ef41Sopenharmony_ci  createDiffieHellman,
191cb0ef41Sopenharmony_ci} = require('crypto');
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ciif (process.argv[2] === 'child') {
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci  const a = secureHeapUsed();
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci  assert(a);
261cb0ef41Sopenharmony_ci  assert.strictEqual(typeof a, 'object');
271cb0ef41Sopenharmony_ci  assert.strictEqual(a.total, 65536);
281cb0ef41Sopenharmony_ci  assert.strictEqual(a.min, 4);
291cb0ef41Sopenharmony_ci  assert.strictEqual(a.used, 0);
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci  {
321cb0ef41Sopenharmony_ci    const size = common.hasFipsCrypto || common.hasOpenSSL3 ? 1024 : 256;
331cb0ef41Sopenharmony_ci    const dh1 = createDiffieHellman(size);
341cb0ef41Sopenharmony_ci    const p1 = dh1.getPrime('buffer');
351cb0ef41Sopenharmony_ci    const dh2 = createDiffieHellman(p1, 'buffer');
361cb0ef41Sopenharmony_ci    const key1 = dh1.generateKeys();
371cb0ef41Sopenharmony_ci    const key2 = dh2.generateKeys('hex');
381cb0ef41Sopenharmony_ci    dh1.computeSecret(key2, 'hex', 'base64');
391cb0ef41Sopenharmony_ci    dh2.computeSecret(key1, 'latin1', 'buffer');
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci    const b = secureHeapUsed();
421cb0ef41Sopenharmony_ci    assert(b);
431cb0ef41Sopenharmony_ci    assert.strictEqual(typeof b, 'object');
441cb0ef41Sopenharmony_ci    assert.strictEqual(b.total, 65536);
451cb0ef41Sopenharmony_ci    assert.strictEqual(b.min, 4);
461cb0ef41Sopenharmony_ci    // The amount used can vary on a number of factors
471cb0ef41Sopenharmony_ci    assert(b.used > 0);
481cb0ef41Sopenharmony_ci    assert(b.utilization > 0.0);
491cb0ef41Sopenharmony_ci  }
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci  return;
521cb0ef41Sopenharmony_ci}
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ciconst child = fork(
551cb0ef41Sopenharmony_ci  process.argv[1],
561cb0ef41Sopenharmony_ci  ['child'],
571cb0ef41Sopenharmony_ci  { execArgv: ['--secure-heap=65536', '--secure-heap-min=4'] });
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_cichild.on('exit', common.mustCall((code) => {
601cb0ef41Sopenharmony_ci  assert.strictEqual(code, 0);
611cb0ef41Sopenharmony_ci}));
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ci{
641cb0ef41Sopenharmony_ci  const child = fork(fixtures.path('a.js'), {
651cb0ef41Sopenharmony_ci    execArgv: ['--secure-heap=3', '--secure-heap-min=3'],
661cb0ef41Sopenharmony_ci    stdio: 'pipe'
671cb0ef41Sopenharmony_ci  });
681cb0ef41Sopenharmony_ci  let res = '';
691cb0ef41Sopenharmony_ci  child.on('exit', common.mustCall((code) => {
701cb0ef41Sopenharmony_ci    assert.notStrictEqual(code, 0);
711cb0ef41Sopenharmony_ci    assert.match(res, /--secure-heap must be a power of 2/);
721cb0ef41Sopenharmony_ci    assert.match(res, /--secure-heap-min must be a power of 2/);
731cb0ef41Sopenharmony_ci  }));
741cb0ef41Sopenharmony_ci  child.stderr.setEncoding('utf8');
751cb0ef41Sopenharmony_ci  child.stderr.on('data', (chunk) => res += chunk);
761cb0ef41Sopenharmony_ci}
77