Skip to content

Commit

Permalink
test: split test-crypto-dh to avoid timeout on slow machines in the CI
Browse files Browse the repository at this point in the history
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: nodejs#49492
Refs: nodejs#49202
Refs: nodejs/reliability#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>
  • Loading branch information
joyeecheung authored Sep 7, 2023
1 parent b3c4f66 commit e0fb3f7
Show file tree
Hide file tree
Showing 3 changed files with 256 additions and 235 deletions.
111 changes: 111 additions & 0 deletions test/parallel/test-crypto-dh-errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
'use strict';
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');

const assert = require('assert');
const crypto = require('crypto');

// https://github.com/nodejs/node/issues/32738
// XXX(bnoordhuis) validateInt32() throwing ERR_OUT_OF_RANGE and RangeError
// instead of ERR_INVALID_ARG_TYPE and TypeError is questionable, IMO.
assert.throws(() => crypto.createDiffieHellman(13.37), {
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
message: 'The value of "sizeOrKey" is out of range. ' +
'It must be an integer. Received 13.37',
});

assert.throws(() => crypto.createDiffieHellman('abcdef', 13.37), {
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
message: 'The value of "generator" is out of range. ' +
'It must be an integer. Received 13.37',
});

for (const bits of [-1, 0, 1]) {
if (common.hasOpenSSL3) {
assert.throws(() => crypto.createDiffieHellman(bits), {
code: 'ERR_OSSL_DH_MODULUS_TOO_SMALL',
name: 'Error',
message: /modulus too small/,
});
} else {
assert.throws(() => crypto.createDiffieHellman(bits), {
code: 'ERR_OSSL_BN_BITS_TOO_SMALL',
name: 'Error',
message: /bits too small/,
});
}
}

for (const g of [-1, 1]) {
const ex = {
code: 'ERR_OSSL_DH_BAD_GENERATOR',
name: 'Error',
message: /bad generator/,
};
assert.throws(() => crypto.createDiffieHellman('abcdef', g), ex);
assert.throws(() => crypto.createDiffieHellman('abcdef', 'hex', g), ex);
}

for (const g of [Buffer.from([]),
Buffer.from([0]),
Buffer.from([1])]) {
const ex = {
code: 'ERR_OSSL_DH_BAD_GENERATOR',
name: 'Error',
message: /bad generator/,
};
assert.throws(() => crypto.createDiffieHellman('abcdef', g), ex);
assert.throws(() => crypto.createDiffieHellman('abcdef', 'hex', g), ex);
}

[
[0x1, 0x2],
() => { },
/abc/,
{},
].forEach((input) => {
assert.throws(
() => crypto.createDiffieHellman(input),
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
}
);
});

// Invalid test: curve argument is undefined
assert.throws(
() => crypto.createECDH(),
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: 'The "curve" argument must be of type string. ' +
'Received undefined'
});

assert.throws(
function() {
crypto.getDiffieHellman('unknown-group');
},
{
name: 'Error',
code: 'ERR_CRYPTO_UNKNOWN_DH_GROUP',
message: 'Unknown DH group'
},
'crypto.getDiffieHellman(\'unknown-group\') ' +
'failed to throw the expected error.'
);

assert.throws(
() => crypto.createDiffieHellman('', true),
{
code: 'ERR_INVALID_ARG_TYPE'
}
);
[true, Symbol(), {}, () => {}, []].forEach((generator) => assert.throws(
() => crypto.createDiffieHellman('', 'base64', generator),
{ code: 'ERR_INVALID_ARG_TYPE' }
));
63 changes: 63 additions & 0 deletions test/parallel/test-crypto-dh-generate-keys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
'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']);
}
Loading

0 comments on commit e0fb3f7

Please sign in to comment.