Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: split test-crypto-dh to avoid timeout on slow machines in the CI #49492

Merged
merged 1 commit into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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