Skip to content

Commit 8b733dc

Browse files
targosMylesBorins
authored andcommitted
test: refactor test-crypto-binary-default
* var -> const. * Group and sort imports. * Correctly align function arguments. * Fix incorrect use of string instead of RegExp in `throws` assertions. * assert.equal -> assert.strictEqual. * Verify that callbacks are called with common.mustCall. PR-URL: #9810 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent 45af785 commit 8b733dc

File tree

1 file changed

+77
-65
lines changed

1 file changed

+77
-65
lines changed

test/parallel/test-crypto-binary-default.js

Lines changed: 77 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,30 @@
33
// to use buffers by default.
44

55

6-
var common = require('../common');
7-
var assert = require('assert');
8-
var constants = require('constants');
6+
const common = require('../common');
97

108
if (!common.hasCrypto) {
119
common.skip('missing crypto');
1210
return;
1311
}
14-
var crypto = require('crypto');
15-
var tls = require('tls');
1612

17-
crypto.DEFAULT_ENCODING = 'binary';
13+
const assert = require('assert');
14+
const constants = require('constants');
15+
const crypto = require('crypto');
16+
const fs = require('fs');
17+
const path = require('path');
18+
const tls = require('tls');
1819

19-
var fs = require('fs');
20-
var path = require('path');
20+
crypto.DEFAULT_ENCODING = 'binary';
2121

2222
// Test Certificates
23-
var certPem = fs.readFileSync(common.fixturesDir + '/test_cert.pem', 'ascii');
24-
var certPfx = fs.readFileSync(common.fixturesDir + '/test_cert.pfx');
25-
var keyPem = fs.readFileSync(common.fixturesDir + '/test_key.pem', 'ascii');
26-
var rsaPubPem = fs.readFileSync(common.fixturesDir + '/test_rsa_pubkey.pem',
27-
'ascii');
28-
var rsaKeyPem = fs.readFileSync(common.fixturesDir + '/test_rsa_privkey.pem',
29-
'ascii');
23+
const certPem = fs.readFileSync(common.fixturesDir + '/test_cert.pem', 'ascii');
24+
const certPfx = fs.readFileSync(common.fixturesDir + '/test_cert.pfx');
25+
const keyPem = fs.readFileSync(common.fixturesDir + '/test_key.pem', 'ascii');
26+
const rsaPubPem = fs.readFileSync(common.fixturesDir + '/test_rsa_pubkey.pem',
27+
'ascii');
28+
const rsaKeyPem = fs.readFileSync(common.fixturesDir + '/test_rsa_privkey.pem',
29+
'ascii');
3030

3131
// PFX tests
3232
assert.doesNotThrow(function() {
@@ -35,22 +35,22 @@ assert.doesNotThrow(function() {
3535

3636
assert.throws(function() {
3737
tls.createSecureContext({pfx: certPfx});
38-
}, 'mac verify failure');
38+
}, /^Error: mac verify failure$/);
3939

4040
assert.throws(function() {
4141
tls.createSecureContext({pfx: certPfx, passphrase: 'test'});
42-
}, 'mac verify failure');
42+
}, /^Error: mac verify failure$/);
4343

4444
assert.throws(function() {
4545
tls.createSecureContext({pfx: 'sample', passphrase: 'test'});
46-
}, 'not enough data');
46+
}, /^Error: not enough data$/);
4747

4848
// Test HMAC
4949
const hmacHash = crypto.createHmac('sha1', 'Node')
5050
.update('some data')
5151
.update('to hmac')
5252
.digest('hex');
53-
assert.equal(hmacHash, '19fd6e1ba73d9ed2224dd5094a71babe85d9a892', 'test HMAC');
53+
assert.strictEqual(hmacHash, '19fd6e1ba73d9ed2224dd5094a71babe85d9a892');
5454

5555
// Test HMAC-SHA-* (rfc 4231 Test Cases)
5656
var rfc4231 = [
@@ -207,9 +207,11 @@ for (let i = 0, l = rfc4231.length; i < l; i++) {
207207
if (rfc4231[i]['truncate']) {
208208
result = result.substr(0, 32); // first 128 bits == 32 hex chars
209209
}
210-
assert.equal(rfc4231[i]['hmac'][hash],
211-
result,
212-
'Test HMAC-' + hash + ': Test case ' + (i + 1) + ' rfc 4231');
210+
assert.strictEqual(
211+
rfc4231[i]['hmac'][hash],
212+
result,
213+
`Test HMAC-${hash}: Test case ${i + 1} rfc 4231`
214+
);
213215
}
214216
}
215217

@@ -328,15 +330,15 @@ for (let i = 0, l = rfc2202_md5.length; i < l; i++) {
328330
crypto.createHmac('md5', rfc2202_md5[i]['key'])
329331
.update(rfc2202_md5[i]['data'])
330332
.digest('hex'),
331-
'Test HMAC-MD5 : Test case ' + (i + 1) + ' rfc 2202');
333+
`Test HMAC-MD5 : Test case ${i + 1} rfc 2202`);
332334
}
333335
}
334336
for (let i = 0, l = rfc2202_sha1.length; i < l; i++) {
335337
assert.equal(rfc2202_sha1[i]['hmac'],
336338
crypto.createHmac('sha1', rfc2202_sha1[i]['key'])
337339
.update(rfc2202_sha1[i]['data'])
338340
.digest('hex'),
339-
'Test HMAC-SHA1 : Test case ' + (i + 1) + ' rfc 2202');
341+
`Test HMAC-SHA1 : Test case ${i + 1} rfc 2202`);
340342
}
341343

342344
// Test hashing
@@ -347,24 +349,27 @@ var a4 = crypto.createHash('sha1').update('Test123').digest('buffer');
347349

348350
if (!common.hasFipsCrypto) {
349351
var a0 = crypto.createHash('md5').update('Test123').digest('binary');
350-
assert.equal(
352+
assert.strictEqual(
351353
a0,
352354
'h\u00ea\u00cb\u0097\u00d8o\fF!\u00fa+\u000e\u0017\u00ca\u00bd\u008c',
353355
'Test MD5 as binary'
354356
);
355357
}
356358

357-
assert.equal(a1, '8308651804facb7b9af8ffc53a33a22d6a1c8ac2', 'Test SHA1');
359+
assert.strictEqual(a1, '8308651804facb7b9af8ffc53a33a22d6a1c8ac2', 'Test SHA1');
358360

359-
assert.equal(a2, '2bX1jws4GYKTlxhloUB09Z66PoJZW+y+hq5R8dnx9l4=',
360-
'Test SHA256 as base64');
361+
assert.strictEqual(a2, '2bX1jws4GYKTlxhloUB09Z66PoJZW+y+hq5R8dnx9l4=',
362+
'Test SHA256 as base64');
361363

362-
assert.equal(a3, '\u00c1(4\u00f1\u0003\u001fd\u0097!O\'\u00d4C/&Qz\u00d4' +
363-
'\u0094\u0015l\u00b8\u008dQ+\u00db\u001d\u00c4\u00b5}\u00b2' +
364-
'\u00d6\u0092\u00a3\u00df\u00a2i\u00a1\u009b\n\n*\u000f' +
365-
'\u00d7\u00d6\u00a2\u00a8\u0085\u00e3<\u0083\u009c\u0093' +
366-
'\u00c2\u0006\u00da0\u00a1\u00879(G\u00ed\'',
367-
'Test SHA512 as assumed binary');
364+
assert.strictEqual(
365+
a3,
366+
'\u00c1(4\u00f1\u0003\u001fd\u0097!O\'\u00d4C/&Qz\u00d4' +
367+
'\u0094\u0015l\u00b8\u008dQ+\u00db\u001d\u00c4\u00b5}\u00b2' +
368+
'\u00d6\u0092\u00a3\u00df\u00a2i\u00a1\u009b\n\n*\u000f' +
369+
'\u00d7\u00d6\u00a2\u00a8\u0085\u00e3<\u0083\u009c\u0093' +
370+
'\u00c2\u0006\u00da0\u00a1\u00879(G\u00ed\'',
371+
'Test SHA512 as assumed binary'
372+
);
368373

369374
assert.deepEqual(a4,
370375
new Buffer('8308651804facb7b9af8ffc53a33a22d6a1c8ac2', 'hex'),
@@ -373,7 +378,7 @@ assert.deepEqual(a4,
373378
// Test multiple updates to same hash
374379
var h1 = crypto.createHash('sha1').update('Test123').digest('hex');
375380
var h2 = crypto.createHash('sha1').update('Test').update('123').digest('hex');
376-
assert.equal(h1, h2, 'multipled updates');
381+
assert.strictEqual(h1, h2, 'multipled updates');
377382

378383
// Test hashing for binary files
379384
var fn = path.join(common.fixturesDir, 'sample.png');
@@ -382,16 +387,18 @@ var fileStream = fs.createReadStream(fn);
382387
fileStream.on('data', function(data) {
383388
sha1Hash.update(data);
384389
});
385-
fileStream.on('close', function() {
386-
assert.equal(sha1Hash.digest('hex'),
387-
'22723e553129a336ad96e10f6aecdf0f45e4149e',
388-
'Test SHA1 of sample.png');
389-
});
390+
fileStream.on('close', common.mustCall(function() {
391+
assert.strictEqual(
392+
sha1Hash.digest('hex'),
393+
'22723e553129a336ad96e10f6aecdf0f45e4149e',
394+
'Test SHA1 of sample.png'
395+
);
396+
}));
390397

391398
// Issue #2227: unknown digest method should throw an error.
392399
assert.throws(function() {
393400
crypto.createHash('xyzzy');
394-
});
401+
}, /^Error: Digest method not supported$/);
395402

396403
// Test signing and verifying
397404
var s1 = crypto.createSign('RSA-SHA1')
@@ -437,7 +444,7 @@ function testCipher1(key) {
437444
var txt = decipher.update(ciph, 'hex', 'utf8');
438445
txt += decipher.final('utf8');
439446

440-
assert.equal(txt, plaintext, 'encryption and decryption');
447+
assert.strictEqual(txt, plaintext, 'encryption and decryption');
441448
}
442449

443450

@@ -459,7 +466,7 @@ function testCipher2(key) {
459466
var txt = decipher.update(ciph, 'base64', 'utf8');
460467
txt += decipher.final('utf8');
461468

462-
assert.equal(txt, plaintext, 'encryption and decryption with Base64');
469+
assert.strictEqual(txt, plaintext, 'encryption and decryption with Base64');
463470
}
464471

465472

@@ -477,7 +484,8 @@ function testCipher3(key, iv) {
477484
var txt = decipher.update(ciph, 'hex', 'utf8');
478485
txt += decipher.final('utf8');
479486

480-
assert.equal(txt, plaintext, 'encryption and decryption with key and iv');
487+
assert.strictEqual(txt, plaintext,
488+
'encryption and decryption with key and iv');
481489
}
482490

483491

@@ -495,7 +503,8 @@ function testCipher4(key, iv) {
495503
var txt = decipher.update(ciph, 'buffer', 'utf8');
496504
txt += decipher.final('utf8');
497505

498-
assert.equal(txt, plaintext, 'encryption and decryption with key and iv');
506+
assert.strictEqual(txt, plaintext,
507+
'encryption and decryption with key and iv');
499508
}
500509

501510
if (!common.hasFipsCrypto) {
@@ -517,7 +526,7 @@ testCipher4(new Buffer('0123456789abcd0123456789'), new Buffer('12345678'));
517526
// update() should only take buffers / strings
518527
assert.throws(function() {
519528
crypto.createHash('sha1').update({foo: 'bar'});
520-
}, /buffer/);
529+
}, /^TypeError: Not a string or buffer$/);
521530

522531

523532
// Test Diffie-Hellman with two parties sharing a secret,
@@ -530,7 +539,7 @@ var key2 = dh2.generateKeys('hex');
530539
var secret1 = dh1.computeSecret(key2, 'hex', 'base64');
531540
var secret2 = dh2.computeSecret(key1, 'binary', 'buffer');
532541

533-
assert.equal(secret1, secret2.toString('base64'));
542+
assert.strictEqual(secret1, secret2.toString('base64'));
534543

535544
// Create "another dh1" using generated keys from dh1,
536545
// and compute secret again
@@ -539,37 +548,39 @@ var privkey1 = dh1.getPrivateKey();
539548
dh3.setPublicKey(key1);
540549
dh3.setPrivateKey(privkey1);
541550

542-
assert.equal(dh1.getPrime(), dh3.getPrime());
543-
assert.equal(dh1.getGenerator(), dh3.getGenerator());
544-
assert.equal(dh1.getPublicKey(), dh3.getPublicKey());
545-
assert.equal(dh1.getPrivateKey(), dh3.getPrivateKey());
551+
assert.strictEqual(dh1.getPrime(), dh3.getPrime());
552+
assert.strictEqual(dh1.getGenerator(), dh3.getGenerator());
553+
assert.strictEqual(dh1.getPublicKey(), dh3.getPublicKey());
554+
assert.strictEqual(dh1.getPrivateKey(), dh3.getPrivateKey());
546555

547556
var secret3 = dh3.computeSecret(key2, 'hex', 'base64');
548557

549-
assert.equal(secret1, secret3);
558+
assert.strictEqual(secret1, secret3);
550559

551560
// https://github.com/joyent/node/issues/2338
552561
var p = 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74' +
553562
'020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F1437' +
554563
'4FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' +
555564
'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381FFFFFFFFFFFFFFFF';
556565
var d = crypto.createDiffieHellman(p, 'hex');
557-
assert.equal(d.verifyError, constants.DH_NOT_SUITABLE_GENERATOR);
566+
assert.strictEqual(d.verifyError, constants.DH_NOT_SUITABLE_GENERATOR);
558567

559568
// Test RSA key signing/verification
560569
var rsaSign = crypto.createSign('RSA-SHA1');
561570
var rsaVerify = crypto.createVerify('RSA-SHA1');
562-
assert.ok(rsaSign);
563-
assert.ok(rsaVerify);
571+
assert.ok(rsaSign instanceof crypto.Sign);
572+
assert.ok(rsaVerify instanceof crypto.Verify);
564573

565574
rsaSign.update(rsaPubPem);
566575
var rsaSignature = rsaSign.sign(rsaKeyPem, 'hex');
567-
assert.equal(rsaSignature,
568-
'5c50e3145c4e2497aadb0eabc83b342d0b0021ece0d4c4a064b7c' +
569-
'8f020d7e2688b122bfb54c724ac9ee169f83f66d2fe90abeb95e8' +
570-
'e1290e7e177152a4de3d944cf7d4883114a20ed0f78e70e25ef0f' +
571-
'60f06b858e6af42a2f276ede95bbc6bc9a9bbdda15bd663186a6f' +
572-
'40819a7af19e577bb2efa5e579a1f5ce8a0d4ca8b8f6');
576+
assert.strictEqual(
577+
rsaSignature,
578+
'5c50e3145c4e2497aadb0eabc83b342d0b0021ece0d4c4a064b7c' +
579+
'8f020d7e2688b122bfb54c724ac9ee169f83f66d2fe90abeb95e8' +
580+
'e1290e7e177152a4de3d944cf7d4883114a20ed0f78e70e25ef0f' +
581+
'60f06b858e6af42a2f276ede95bbc6bc9a9bbdda15bd663186a6f' +
582+
'40819a7af19e577bb2efa5e579a1f5ce8a0d4ca8b8f6'
583+
);
573584

574585
rsaVerify.update(rsaPubPem);
575586
assert.strictEqual(rsaVerify.verify(rsaPubPem, rsaSignature, 'hex'), true);
@@ -636,12 +647,13 @@ assert.strictEqual(rsaVerify.verify(rsaPubPem, rsaSignature, 'hex'), true);
636647
// Test PBKDF2 with RFC 6070 test vectors (except #4)
637648
//
638649
function testPBKDF2(password, salt, iterations, keylen, expected) {
639-
var actual = crypto.pbkdf2Sync(password, salt, iterations, keylen);
640-
assert.equal(actual, expected);
650+
const actual = crypto.pbkdf2Sync(password, salt, iterations, keylen);
651+
assert.strictEqual(actual, expected);
641652

642-
crypto.pbkdf2(password, salt, iterations, keylen, function(err, actual) {
643-
assert.equal(actual, expected);
653+
const cb = common.mustCall((err, actual) => {
654+
assert.strictEqual(actual, expected);
644655
});
656+
crypto.pbkdf2(password, salt, iterations, keylen, cb);
645657
}
646658

647659

0 commit comments

Comments
 (0)