mirror of
https://github.com/nodejs/node.git
synced 2024-11-21 21:19:50 +01:00
e0fb3f7cb8
Locally this speeds up running test-crypto-dh* from 7s to 2s. This was previously timing out in CI (took more than 2 minutes) so should see a bigger gap in the CI. PR-URL: https://github.com/nodejs/node/pull/49492 Refs: https://github.com/nodejs/node/issues/49202 Refs: https://github.com/nodejs/reliability/issues/655 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Filip Skokan <panva.ip@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
64 lines
1.9 KiB
JavaScript
64 lines
1.9 KiB
JavaScript
'use strict';
|
|
|
|
const common = require('../common');
|
|
if (!common.hasCrypto)
|
|
common.skip('missing crypto');
|
|
|
|
const assert = require('assert');
|
|
const crypto = require('crypto');
|
|
|
|
{
|
|
const size = common.hasFipsCrypto || common.hasOpenSSL3 ? 1024 : 256;
|
|
|
|
function unlessInvalidState(f) {
|
|
try {
|
|
return f();
|
|
} catch (err) {
|
|
if (err.code !== 'ERR_CRYPTO_INVALID_STATE') {
|
|
throw err;
|
|
}
|
|
}
|
|
}
|
|
|
|
function testGenerateKeysChangesKeys(setup, expected) {
|
|
const dh = crypto.createDiffieHellman(size);
|
|
setup(dh);
|
|
const firstPublicKey = unlessInvalidState(() => dh.getPublicKey());
|
|
const firstPrivateKey = unlessInvalidState(() => dh.getPrivateKey());
|
|
dh.generateKeys();
|
|
const secondPublicKey = dh.getPublicKey();
|
|
const secondPrivateKey = dh.getPrivateKey();
|
|
function changed(shouldChange, first, second) {
|
|
if (shouldChange) {
|
|
assert.notDeepStrictEqual(first, second);
|
|
} else {
|
|
assert.deepStrictEqual(first, second);
|
|
}
|
|
}
|
|
changed(expected.includes('public'), firstPublicKey, secondPublicKey);
|
|
changed(expected.includes('private'), firstPrivateKey, secondPrivateKey);
|
|
}
|
|
|
|
// Both the private and the public key are missing: generateKeys() generates both.
|
|
testGenerateKeysChangesKeys(() => {
|
|
// No setup.
|
|
}, ['public', 'private']);
|
|
|
|
// Neither key is missing: generateKeys() does nothing.
|
|
testGenerateKeysChangesKeys((dh) => {
|
|
dh.generateKeys();
|
|
}, []);
|
|
|
|
// Only the public key is missing: generateKeys() generates only the public key.
|
|
testGenerateKeysChangesKeys((dh) => {
|
|
dh.setPrivateKey(Buffer.from('01020304', 'hex'));
|
|
}, ['public']);
|
|
|
|
// The public key is outdated: generateKeys() generates only the public key.
|
|
testGenerateKeysChangesKeys((dh) => {
|
|
const oldPublicKey = dh.generateKeys();
|
|
dh.setPrivateKey(Buffer.from('01020304', 'hex'));
|
|
assert.deepStrictEqual(dh.getPublicKey(), oldPublicKey);
|
|
}, ['public']);
|
|
}
|