Skip to content

Commit f214428

Browse files
joyeecheungruyadorno
authored andcommitted
test: split test-crypto-dh to avoid timeout on slow machines in the CI
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: #49492 Refs: #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>
1 parent b4724e2 commit f214428

File tree

3 files changed

+256
-235
lines changed

3 files changed

+256
-235
lines changed
+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
'use strict';
2+
const common = require('../common');
3+
if (!common.hasCrypto)
4+
common.skip('missing crypto');
5+
6+
const assert = require('assert');
7+
const crypto = require('crypto');
8+
9+
// https://github.com/nodejs/node/issues/32738
10+
// XXX(bnoordhuis) validateInt32() throwing ERR_OUT_OF_RANGE and RangeError
11+
// instead of ERR_INVALID_ARG_TYPE and TypeError is questionable, IMO.
12+
assert.throws(() => crypto.createDiffieHellman(13.37), {
13+
code: 'ERR_OUT_OF_RANGE',
14+
name: 'RangeError',
15+
message: 'The value of "sizeOrKey" is out of range. ' +
16+
'It must be an integer. Received 13.37',
17+
});
18+
19+
assert.throws(() => crypto.createDiffieHellman('abcdef', 13.37), {
20+
code: 'ERR_OUT_OF_RANGE',
21+
name: 'RangeError',
22+
message: 'The value of "generator" is out of range. ' +
23+
'It must be an integer. Received 13.37',
24+
});
25+
26+
for (const bits of [-1, 0, 1]) {
27+
if (common.hasOpenSSL3) {
28+
assert.throws(() => crypto.createDiffieHellman(bits), {
29+
code: 'ERR_OSSL_DH_MODULUS_TOO_SMALL',
30+
name: 'Error',
31+
message: /modulus too small/,
32+
});
33+
} else {
34+
assert.throws(() => crypto.createDiffieHellman(bits), {
35+
code: 'ERR_OSSL_BN_BITS_TOO_SMALL',
36+
name: 'Error',
37+
message: /bits too small/,
38+
});
39+
}
40+
}
41+
42+
for (const g of [-1, 1]) {
43+
const ex = {
44+
code: 'ERR_OSSL_DH_BAD_GENERATOR',
45+
name: 'Error',
46+
message: /bad generator/,
47+
};
48+
assert.throws(() => crypto.createDiffieHellman('abcdef', g), ex);
49+
assert.throws(() => crypto.createDiffieHellman('abcdef', 'hex', g), ex);
50+
}
51+
52+
for (const g of [Buffer.from([]),
53+
Buffer.from([0]),
54+
Buffer.from([1])]) {
55+
const ex = {
56+
code: 'ERR_OSSL_DH_BAD_GENERATOR',
57+
name: 'Error',
58+
message: /bad generator/,
59+
};
60+
assert.throws(() => crypto.createDiffieHellman('abcdef', g), ex);
61+
assert.throws(() => crypto.createDiffieHellman('abcdef', 'hex', g), ex);
62+
}
63+
64+
[
65+
[0x1, 0x2],
66+
() => { },
67+
/abc/,
68+
{},
69+
].forEach((input) => {
70+
assert.throws(
71+
() => crypto.createDiffieHellman(input),
72+
{
73+
code: 'ERR_INVALID_ARG_TYPE',
74+
name: 'TypeError',
75+
}
76+
);
77+
});
78+
79+
// Invalid test: curve argument is undefined
80+
assert.throws(
81+
() => crypto.createECDH(),
82+
{
83+
code: 'ERR_INVALID_ARG_TYPE',
84+
name: 'TypeError',
85+
message: 'The "curve" argument must be of type string. ' +
86+
'Received undefined'
87+
});
88+
89+
assert.throws(
90+
function() {
91+
crypto.getDiffieHellman('unknown-group');
92+
},
93+
{
94+
name: 'Error',
95+
code: 'ERR_CRYPTO_UNKNOWN_DH_GROUP',
96+
message: 'Unknown DH group'
97+
},
98+
'crypto.getDiffieHellman(\'unknown-group\') ' +
99+
'failed to throw the expected error.'
100+
);
101+
102+
assert.throws(
103+
() => crypto.createDiffieHellman('', true),
104+
{
105+
code: 'ERR_INVALID_ARG_TYPE'
106+
}
107+
);
108+
[true, Symbol(), {}, () => {}, []].forEach((generator) => assert.throws(
109+
() => crypto.createDiffieHellman('', 'base64', generator),
110+
{ code: 'ERR_INVALID_ARG_TYPE' }
111+
));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
if (!common.hasCrypto)
5+
common.skip('missing crypto');
6+
7+
const assert = require('assert');
8+
const crypto = require('crypto');
9+
10+
{
11+
const size = common.hasFipsCrypto || common.hasOpenSSL3 ? 1024 : 256;
12+
13+
function unlessInvalidState(f) {
14+
try {
15+
return f();
16+
} catch (err) {
17+
if (err.code !== 'ERR_CRYPTO_INVALID_STATE') {
18+
throw err;
19+
}
20+
}
21+
}
22+
23+
function testGenerateKeysChangesKeys(setup, expected) {
24+
const dh = crypto.createDiffieHellman(size);
25+
setup(dh);
26+
const firstPublicKey = unlessInvalidState(() => dh.getPublicKey());
27+
const firstPrivateKey = unlessInvalidState(() => dh.getPrivateKey());
28+
dh.generateKeys();
29+
const secondPublicKey = dh.getPublicKey();
30+
const secondPrivateKey = dh.getPrivateKey();
31+
function changed(shouldChange, first, second) {
32+
if (shouldChange) {
33+
assert.notDeepStrictEqual(first, second);
34+
} else {
35+
assert.deepStrictEqual(first, second);
36+
}
37+
}
38+
changed(expected.includes('public'), firstPublicKey, secondPublicKey);
39+
changed(expected.includes('private'), firstPrivateKey, secondPrivateKey);
40+
}
41+
42+
// Both the private and the public key are missing: generateKeys() generates both.
43+
testGenerateKeysChangesKeys(() => {
44+
// No setup.
45+
}, ['public', 'private']);
46+
47+
// Neither key is missing: generateKeys() does nothing.
48+
testGenerateKeysChangesKeys((dh) => {
49+
dh.generateKeys();
50+
}, []);
51+
52+
// Only the public key is missing: generateKeys() generates only the public key.
53+
testGenerateKeysChangesKeys((dh) => {
54+
dh.setPrivateKey(Buffer.from('01020304', 'hex'));
55+
}, ['public']);
56+
57+
// The public key is outdated: generateKeys() generates only the public key.
58+
testGenerateKeysChangesKeys((dh) => {
59+
const oldPublicKey = dh.generateKeys();
60+
dh.setPrivateKey(Buffer.from('01020304', 'hex'));
61+
assert.deepStrictEqual(dh.getPublicKey(), oldPublicKey);
62+
}, ['public']);
63+
}

0 commit comments

Comments
 (0)