Skip to content

Commit bab8a36

Browse files
edsadrMylesBorins
authored andcommitted
test: improve the code in test-crypto-dh
* validate the errors for all assert.throws * use arrow functions PR-URL: #10734 Backport-PR-URL: #13785 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 752bc24 commit bab8a36

File tree

1 file changed

+41
-29
lines changed

1 file changed

+41
-29
lines changed

test/parallel/test-crypto-dh.js

Lines changed: 41 additions & 29 deletions
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');
@@ -180,30 +191,31 @@ assert.throws(() => {
180191
const ecdh3 = crypto.createECDH('secp256k1');
181192
const key3 = ecdh3.generateKeys();
182193

183-
assert.throws(function() {
194+
assert.throws(() => {
184195
ecdh2.computeSecret(key3, 'latin1', 'buffer');
185-
});
196+
}, /^Error: Failed to translate Buffer to a EC_POINT$/);
186197

187198
// ECDH should allow .setPrivateKey()/.setPublicKey()
188199
const ecdh4 = crypto.createECDH('prime256v1');
189200

190201
ecdh4.setPrivateKey(ecdh1.getPrivateKey());
191202
ecdh4.setPublicKey(ecdh1.getPublicKey());
192203

193-
assert.throws(function() {
204+
assert.throws(() => {
194205
ecdh4.setPublicKey(ecdh3.getPublicKey());
195-
}, /Failed to convert Buffer to EC_POINT/);
206+
}, /^Error: Failed to convert Buffer to EC_POINT$/);
196207

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

200211
// Verify errors are thrown when retrieving keys from an uninitialized object.
201-
assert.throws(function() {
212+
assert.throws(() => {
202213
ecdh5.getPublicKey();
203-
}, /Failed to get ECDH public key/);
204-
assert.throws(function() {
214+
}, /^Error: Failed to get ECDH public key$/);
215+
216+
assert.throws(() => {
205217
ecdh5.getPrivateKey();
206-
}, /Failed to get ECDH private key/);
218+
}, /^Error: Failed to get ECDH private key$/);
207219

208220
// A valid private key for the secp256k1 curve.
209221
const cafebabeKey = 'cafebabe'.repeat(8);
@@ -249,10 +261,10 @@ assert.strictEqual(ecdh5.getPublicKey('hex', 'compressed'), cafebabePubPtComp);
249261
// Show why allowing the public key to be set on this type does not make sense.
250262
ecdh5.setPublicKey(peerPubPtComp, 'hex');
251263
assert.strictEqual(ecdh5.getPublicKey('hex'), peerPubPtUnComp);
252-
assert.throws(function() {
264+
assert.throws(() => {
253265
// Error because the public key does not match the private key anymore.
254266
ecdh5.computeSecret(peerPubPtComp, 'hex', 'hex');
255-
}, /Invalid key pair/);
267+
}, /^Error: Invalid key pair$/);
256268

257269
// Set to a valid key to show that later attempts to set an invalid key are
258270
// rejected.
@@ -262,10 +274,10 @@ ecdh5.setPrivateKey(cafebabeKey, 'hex');
262274
'0000000000000000000000000000000000000000000000000000000000000000',
263275
'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141',
264276
'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF',
265-
].forEach(function(element, index, object) {
266-
assert.throws(function() {
277+
].forEach((element) => {
278+
assert.throws(() => {
267279
ecdh5.setPrivateKey(element, 'hex');
268-
}, /Private key is not valid for specified curve/);
280+
}, /^Error: Private key is not valid for specified curve.$/);
269281
// Verify object state did not change.
270282
assert.strictEqual(ecdh5.getPrivateKey('hex'), cafebabeKey);
271283
});

0 commit comments

Comments
 (0)