Skip to content

Commit 4a7bb5b

Browse files
edsadritaloacasas
authored andcommitted
test: improve the code in test-crypto-dh
* validate the errors for all assert.throws * use arrow functions PR-URL: #10734 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
1 parent 78a495e commit 4a7bb5b

File tree

1 file changed

+41
-29
lines changed

1 file changed

+41
-29
lines changed

test/parallel/test-crypto-dh.js

+41-29
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,28 @@ let key2 = dh2.generateKeys('hex');
1919
let secret1 = dh1.computeSecret(key2, 'hex', 'base64');
2020
let secret2 = dh2.computeSecret(key1, 'latin1', 'buffer');
2121

22-
assert.strictEqual(secret1, secret2.toString('base64'));
22+
assert.strictEqual(secret2.toString('base64'), secret1);
2323
assert.strictEqual(dh1.verifyError, 0);
2424
assert.strictEqual(dh2.verifyError, 0);
2525

26-
assert.throws(function() {
26+
const argumentsError =
27+
/^TypeError: First argument should be number, string or Buffer$/;
28+
29+
assert.throws(() => {
2730
crypto.createDiffieHellman([0x1, 0x2]);
28-
});
31+
}, argumentsError);
2932

30-
assert.throws(function() {
31-
crypto.createDiffieHellman(function() { });
32-
});
33+
assert.throws(() => {
34+
crypto.createDiffieHellman(() => { });
35+
}, argumentsError);
3336

34-
assert.throws(function() {
37+
assert.throws(() => {
3538
crypto.createDiffieHellman(/abc/);
36-
});
39+
}, argumentsError);
3740

38-
assert.throws(function() {
41+
assert.throws(() => {
3942
crypto.createDiffieHellman({});
40-
});
43+
}, argumentsError);
4144

4245
// Create "another dh1" using generated keys from dh1,
4346
// and compute secret again
@@ -56,21 +59,29 @@ const secret3 = dh3.computeSecret(key2, 'hex', 'base64');
5659

5760
assert.strictEqual(secret1, secret3);
5861

62+
const wrongBlockLength =
63+
new RegExp('^Error: error:0606506D:digital envelope' +
64+
' routines:EVP_DecryptFinal_ex:wrong final block length$');
65+
5966
// Run this one twice to make sure that the dh3 clears its error properly
6067
{
6168
const c = crypto.createDecipheriv('aes-128-ecb', crypto.randomBytes(16), '');
62-
assert.throws(function() { c.final('utf8'); }, /wrong final block length/);
69+
assert.throws(() => {
70+
c.final('utf8');
71+
}, wrongBlockLength);
6372
}
6473

65-
assert.throws(function() {
66-
dh3.computeSecret('');
67-
}, /key is too small/i);
68-
6974
{
7075
const c = crypto.createDecipheriv('aes-128-ecb', crypto.randomBytes(16), '');
71-
assert.throws(function() { c.final('utf8'); }, /wrong final block length/);
76+
assert.throws(() => {
77+
c.final('utf8');
78+
}, wrongBlockLength);
7279
}
7380

81+
assert.throws(() => {
82+
dh3.computeSecret('');
83+
}, /^Error: Supplied key is too small$/);
84+
7485
// Create a shared using a DH group.
7586
const alice = crypto.createDiffieHellmanGroup('modp5');
7687
const bob = crypto.createDiffieHellmanGroup('modp5');
@@ -176,30 +187,31 @@ assert(firstByte === 6 || firstByte === 7);
176187
const ecdh3 = crypto.createECDH('secp256k1');
177188
const key3 = ecdh3.generateKeys();
178189

179-
assert.throws(function() {
190+
assert.throws(() => {
180191
ecdh2.computeSecret(key3, 'latin1', 'buffer');
181-
});
192+
}, /^Error: Failed to translate Buffer to a EC_POINT$/);
182193

183194
// ECDH should allow .setPrivateKey()/.setPublicKey()
184195
const ecdh4 = crypto.createECDH('prime256v1');
185196

186197
ecdh4.setPrivateKey(ecdh1.getPrivateKey());
187198
ecdh4.setPublicKey(ecdh1.getPublicKey());
188199

189-
assert.throws(function() {
200+
assert.throws(() => {
190201
ecdh4.setPublicKey(ecdh3.getPublicKey());
191-
}, /Failed to convert Buffer to EC_POINT/);
202+
}, /^Error: Failed to convert Buffer to EC_POINT$/);
192203

193204
// Verify that we can use ECDH without having to use newly generated keys.
194205
const ecdh5 = crypto.createECDH('secp256k1');
195206

196207
// Verify errors are thrown when retrieving keys from an uninitialized object.
197-
assert.throws(function() {
208+
assert.throws(() => {
198209
ecdh5.getPublicKey();
199-
}, /Failed to get ECDH public key/);
200-
assert.throws(function() {
210+
}, /^Error: Failed to get ECDH public key$/);
211+
212+
assert.throws(() => {
201213
ecdh5.getPrivateKey();
202-
}, /Failed to get ECDH private key/);
214+
}, /^Error: Failed to get ECDH private key$/);
203215

204216
// A valid private key for the secp256k1 curve.
205217
const cafebabeKey = 'cafebabe'.repeat(8);
@@ -245,10 +257,10 @@ assert.strictEqual(ecdh5.getPublicKey('hex', 'compressed'), cafebabePubPtComp);
245257
// Show why allowing the public key to be set on this type does not make sense.
246258
ecdh5.setPublicKey(peerPubPtComp, 'hex');
247259
assert.strictEqual(ecdh5.getPublicKey('hex'), peerPubPtUnComp);
248-
assert.throws(function() {
260+
assert.throws(() => {
249261
// Error because the public key does not match the private key anymore.
250262
ecdh5.computeSecret(peerPubPtComp, 'hex', 'hex');
251-
}, /Invalid key pair/);
263+
}, /^Error: Invalid key pair$/);
252264

253265
// Set to a valid key to show that later attempts to set an invalid key are
254266
// rejected.
@@ -258,10 +270,10 @@ ecdh5.setPrivateKey(cafebabeKey, 'hex');
258270
'0000000000000000000000000000000000000000000000000000000000000000',
259271
'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141',
260272
'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF',
261-
].forEach(function(element, index, object) {
262-
assert.throws(function() {
273+
].forEach((element) => {
274+
assert.throws(() => {
263275
ecdh5.setPrivateKey(element, 'hex');
264-
}, /Private key is not valid for specified curve/);
276+
}, /^Error: Private key is not valid for specified curve.$/);
265277
// Verify object state did not change.
266278
assert.strictEqual(ecdh5.getPrivateKey('hex'), cafebabeKey);
267279
});

0 commit comments

Comments
 (0)