|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | +if (!common.hasCrypto) |
| 5 | + common.skip('missing crypto'); |
| 6 | + |
| 7 | +if (common.isWindows) |
| 8 | + common.skip('Not supported on Windows'); |
| 9 | + |
| 10 | +const assert = require('assert'); |
| 11 | +const { fork } = require('child_process'); |
| 12 | +const fixtures = require('../common/fixtures'); |
| 13 | +const { |
| 14 | + secureHeapUsed, |
| 15 | + createDiffieHellman, |
| 16 | +} = require('crypto'); |
| 17 | + |
| 18 | +if (process.argv[2] === 'child') { |
| 19 | + |
| 20 | + const a = secureHeapUsed(); |
| 21 | + |
| 22 | + assert(a); |
| 23 | + assert.strictEqual(typeof a, 'object'); |
| 24 | + assert.strictEqual(a.total, 65536); |
| 25 | + assert.strictEqual(a.min, 4); |
| 26 | + assert.strictEqual(a.used, 0); |
| 27 | + |
| 28 | + { |
| 29 | + const dh1 = createDiffieHellman(common.hasFipsCrypto ? 1024 : 256); |
| 30 | + const p1 = dh1.getPrime('buffer'); |
| 31 | + const dh2 = createDiffieHellman(p1, 'buffer'); |
| 32 | + const key1 = dh1.generateKeys(); |
| 33 | + const key2 = dh2.generateKeys('hex'); |
| 34 | + dh1.computeSecret(key2, 'hex', 'base64'); |
| 35 | + dh2.computeSecret(key1, 'latin1', 'buffer'); |
| 36 | + |
| 37 | + const b = secureHeapUsed(); |
| 38 | + assert(b); |
| 39 | + assert.strictEqual(typeof b, 'object'); |
| 40 | + assert.strictEqual(b.total, 65536); |
| 41 | + assert.strictEqual(b.min, 4); |
| 42 | + // The amount used can vary on a number of factors |
| 43 | + assert(b.used > 0); |
| 44 | + assert(b.utilization > 0.0); |
| 45 | + } |
| 46 | + |
| 47 | + return; |
| 48 | +} |
| 49 | + |
| 50 | +const child = fork( |
| 51 | + process.argv[1], |
| 52 | + ['child'], |
| 53 | + { execArgv: ['--secure-heap=65536', '--secure-heap-min=4'] }); |
| 54 | + |
| 55 | +child.on('exit', common.mustCall((code) => { |
| 56 | + assert.strictEqual(code, 0); |
| 57 | +})); |
| 58 | + |
| 59 | +{ |
| 60 | + const child = fork(fixtures.path('a.js'), { |
| 61 | + execArgv: ['--secure-heap=3', '--secure-heap-min=3'], |
| 62 | + stdio: 'pipe' |
| 63 | + }); |
| 64 | + let res = ''; |
| 65 | + child.on('exit', common.mustCall((code) => { |
| 66 | + assert.notStrictEqual(code, 0); |
| 67 | + assert.match(res, /--secure-heap must be a power of 2/); |
| 68 | + assert.match(res, /--secure-heap-min must be a power of 2/); |
| 69 | + })); |
| 70 | + child.stderr.setEncoding('utf8'); |
| 71 | + child.stderr.on('data', (chunk) => res += chunk); |
| 72 | +} |
0 commit comments