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 <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Chemi Atlow <[email protected]> Reviewed-By: Filip Skokan <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent b4724e2 commit f214428

File tree

3 files changed

+256
-235
lines changed

3 files changed

+256
-235
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
'use strict';
2+
constcommon=require('../common');
3+
if(!common.hasCrypto)
4+
common.skip('missing crypto');
5+
6+
constassert=require('assert');
7+
constcrypto=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(constbitsof[-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: /modulustoosmall/,
32+
});
33+
}else{
34+
assert.throws(()=>crypto.createDiffieHellman(bits),{
35+
code: 'ERR_OSSL_BN_BITS_TOO_SMALL',
36+
name: 'Error',
37+
message: /bitstoosmall/,
38+
});
39+
}
40+
}
41+
42+
for(constgof[-1,1]){
43+
constex={
44+
code: 'ERR_OSSL_DH_BAD_GENERATOR',
45+
name: 'Error',
46+
message: /badgenerator/,
47+
};
48+
assert.throws(()=>crypto.createDiffieHellman('abcdef',g),ex);
49+
assert.throws(()=>crypto.createDiffieHellman('abcdef','hex',g),ex);
50+
}
51+
52+
for(constgof[Buffer.from([]),
53+
Buffer.from([0]),
54+
Buffer.from([1])]){
55+
constex={
56+
code: 'ERR_OSSL_DH_BAD_GENERATOR',
57+
name: 'Error',
58+
message: /badgenerator/,
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+
));
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
'use strict';
2+
3+
constcommon=require('../common');
4+
if(!common.hasCrypto)
5+
common.skip('missing crypto');
6+
7+
constassert=require('assert');
8+
constcrypto=require('crypto');
9+
10+
{
11+
constsize=common.hasFipsCrypto||common.hasOpenSSL3 ? 1024 : 256;
12+
13+
functionunlessInvalidState(f){
14+
try{
15+
returnf();
16+
}catch(err){
17+
if(err.code!=='ERR_CRYPTO_INVALID_STATE'){
18+
throwerr;
19+
}
20+
}
21+
}
22+
23+
functiontestGenerateKeysChangesKeys(setup,expected){
24+
constdh=crypto.createDiffieHellman(size);
25+
setup(dh);
26+
constfirstPublicKey=unlessInvalidState(()=>dh.getPublicKey());
27+
constfirstPrivateKey=unlessInvalidState(()=>dh.getPrivateKey());
28+
dh.generateKeys();
29+
constsecondPublicKey=dh.getPublicKey();
30+
constsecondPrivateKey=dh.getPrivateKey();
31+
functionchanged(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+
constoldPublicKey=dh.generateKeys();
60+
dh.setPrivateKey(Buffer.from('01020304','hex'));
61+
assert.deepStrictEqual(dh.getPublicKey(),oldPublicKey);
62+
},['public']);
63+
}

0 commit comments

Comments
(0)