Skip to content

Commit 45af785

Browse files
committedDec 27, 2016
test: refactor and fix test-crypto
* var -> const. * Group and sort imports. * Replace use of the deprecated crypto.createCredentials. * Fix incorrect use of string instead of RegExp in `throws` assertions. * Clone array with `.slice()` and remove dependency on util. * assert.notEqual -> assert.notStrictEqual. * indexOf -> includes. PR-URL: #9807 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
1 parent e0c8aaf commit 45af785

File tree

1 file changed

+43
-44
lines changed

1 file changed

+43
-44
lines changed
 

‎test/parallel/test-crypto.js

+43-44
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,33 @@
11
'use strict';
2-
var common = require('../common');
3-
var assert = require('assert');
4-
var util = require('util');
2+
const common = require('../common');
53

64
if (!common.hasCrypto) {
75
common.skip('missing crypto');
86
return;
97
}
10-
var crypto = require('crypto');
118

12-
crypto.DEFAULT_ENCODING = 'buffer';
9+
const assert = require('assert');
10+
const crypto = require('crypto');
11+
const fs = require('fs');
12+
const tls = require('tls');
1313

14-
var fs = require('fs');
14+
crypto.DEFAULT_ENCODING = 'buffer';
1515

1616
// Test Certificates
17-
var caPem = fs.readFileSync(common.fixturesDir + '/test_ca.pem', 'ascii');
18-
var certPem = fs.readFileSync(common.fixturesDir + '/test_cert.pem', 'ascii');
19-
var certPfx = fs.readFileSync(common.fixturesDir + '/test_cert.pfx');
20-
var keyPem = fs.readFileSync(common.fixturesDir + '/test_key.pem', 'ascii');
21-
var tls = require('tls');
17+
const caPem = fs.readFileSync(common.fixturesDir + '/test_ca.pem', 'ascii');
18+
const certPem = fs.readFileSync(common.fixturesDir + '/test_cert.pem', 'ascii');
19+
const certPfx = fs.readFileSync(common.fixturesDir + '/test_cert.pfx');
20+
const keyPem = fs.readFileSync(common.fixturesDir + '/test_key.pem', 'ascii');
2221

2322
// 'this' safety
2423
// https://github.com/joyent/node/issues/6690
2524
assert.throws(function() {
26-
var options = {key: keyPem, cert: certPem, ca: caPem};
27-
var credentials = crypto.createCredentials(options);
28-
var context = credentials.context;
29-
var notcontext = { setOptions: context.setOptions, setKey: context.setKey };
30-
crypto.createCredentials({ secureOptions: 1 }, notcontext);
31-
}, TypeError);
25+
const options = {key: keyPem, cert: certPem, ca: caPem};
26+
const credentials = tls.createSecureContext(options);
27+
const context = credentials.context;
28+
const notcontext = { setOptions: context.setOptions, setKey: context.setKey };
29+
tls.createSecureContext({ secureOptions: 1 }, notcontext);
30+
}, /^TypeError: Illegal invocation$/);
3231

3332
// PFX tests
3433
assert.doesNotThrow(function() {
@@ -37,55 +36,55 @@ assert.doesNotThrow(function() {
3736

3837
assert.throws(function() {
3938
tls.createSecureContext({pfx: certPfx});
40-
}, 'mac verify failure');
39+
}, /^Error: mac verify failure$/);
4140

4241
assert.throws(function() {
4342
tls.createSecureContext({pfx: certPfx, passphrase: 'test'});
44-
}, 'mac verify failure');
43+
}, /^Error: mac verify failure$/);
4544

4645
assert.throws(function() {
4746
tls.createSecureContext({pfx: 'sample', passphrase: 'test'});
48-
}, 'not enough data');
47+
}, /^Error: not enough data$/);
4948

5049

5150
// update() should only take buffers / strings
5251
assert.throws(function() {
5352
crypto.createHash('sha1').update({foo: 'bar'});
54-
}, /buffer/);
53+
}, /^TypeError: Not a string or buffer$/);
5554

5655

5756
function assertSorted(list) {
5857
// Array#sort() modifies the list in place so make a copy.
59-
var sorted = util._extend([], list).sort();
58+
const sorted = list.slice().sort();
6059
assert.deepEqual(list, sorted);
6160
}
6261

6362
// Assume that we have at least AES-128-CBC.
64-
assert.notEqual(0, crypto.getCiphers().length);
65-
assert.notEqual(-1, crypto.getCiphers().indexOf('aes-128-cbc'));
66-
assert.equal(-1, crypto.getCiphers().indexOf('AES-128-CBC'));
63+
assert.notStrictEqual(0, crypto.getCiphers().length);
64+
assert.notStrictEqual(-1, crypto.getCiphers().indexOf('aes-128-cbc'));
65+
assert.strictEqual(-1, crypto.getCiphers().indexOf('AES-128-CBC'));
6766
assertSorted(crypto.getCiphers());
6867

6968
// Assume that we have at least AES256-SHA.
70-
assert.notEqual(0, tls.getCiphers().length);
71-
assert.notEqual(-1, tls.getCiphers().indexOf('aes256-sha'));
72-
assert.equal(-1, tls.getCiphers().indexOf('AES256-SHA'));
69+
assert.notStrictEqual(0, tls.getCiphers().length);
70+
assert.notStrictEqual(-1, tls.getCiphers().indexOf('aes256-sha'));
71+
assert.strictEqual(-1, tls.getCiphers().indexOf('AES256-SHA'));
7372
assertSorted(tls.getCiphers());
7473

7574
// Assert that we have sha and sha1 but not SHA and SHA1.
76-
assert.notEqual(0, crypto.getHashes().length);
77-
assert.notEqual(-1, crypto.getHashes().indexOf('sha1'));
78-
assert.notEqual(-1, crypto.getHashes().indexOf('sha'));
79-
assert.equal(-1, crypto.getHashes().indexOf('SHA1'));
80-
assert.equal(-1, crypto.getHashes().indexOf('SHA'));
81-
assert.notEqual(-1, crypto.getHashes().indexOf('RSA-SHA1'));
82-
assert.equal(-1, crypto.getHashes().indexOf('rsa-sha1'));
75+
assert.notStrictEqual(0, crypto.getHashes().length);
76+
assert.notStrictEqual(-1, crypto.getHashes().indexOf('sha1'));
77+
assert.notStrictEqual(-1, crypto.getHashes().indexOf('sha'));
78+
assert.strictEqual(-1, crypto.getHashes().indexOf('SHA1'));
79+
assert.strictEqual(-1, crypto.getHashes().indexOf('SHA'));
80+
assert.notStrictEqual(-1, crypto.getHashes().indexOf('RSA-SHA1'));
81+
assert.strictEqual(-1, crypto.getHashes().indexOf('rsa-sha1'));
8382
assertSorted(crypto.getHashes());
8483

8584
// Assume that we have at least secp384r1.
86-
assert.notEqual(0, crypto.getCurves().length);
87-
assert.notEqual(-1, crypto.getCurves().indexOf('secp384r1'));
88-
assert.equal(-1, crypto.getCurves().indexOf('SECP384R1'));
85+
assert.notStrictEqual(0, crypto.getCurves().length);
86+
assert.notStrictEqual(-1, crypto.getCurves().indexOf('secp384r1'));
87+
assert.strictEqual(-1, crypto.getCurves().indexOf('SECP384R1'));
8988
assertSorted(crypto.getCurves());
9089

9190
// Regression tests for #5725: hex input that's not a power of two should
@@ -100,18 +99,18 @@ assert.throws(function() {
10099

101100
assert.throws(function() {
102101
crypto.createHash('sha1').update('0', 'hex');
103-
}, /Bad input string/);
102+
}, /^TypeError: Bad input string$/);
104103

105104
assert.throws(function() {
106105
crypto.createSign('RSA-SHA1').update('0', 'hex');
107-
}, /Bad input string/);
106+
}, /^TypeError: Bad input string$/);
108107

109108
assert.throws(function() {
110109
crypto.createVerify('RSA-SHA1').update('0', 'hex');
111-
}, /Bad input string/);
110+
}, /^TypeError: Bad input string$/);
112111

113112
assert.throws(function() {
114-
var priv = [
113+
const priv = [
115114
'-----BEGIN RSA PRIVATE KEY-----',
116115
'MIGrAgEAAiEA+3z+1QNF2/unumadiwEr+C5vfhezsb3hp4jAnCNRpPcCAwEAAQIgQNriSQK4',
117116
'EFwczDhMZp2dvbcz7OUUyt36z3S4usFPHSECEQD/41K7SujrstBfoCPzwC1xAhEA+5kt4BJy',
@@ -121,7 +120,7 @@ assert.throws(function() {
121120
''
122121
].join('\n');
123122
crypto.createSign('RSA-SHA256').update('test').sign(priv);
124-
}, /digest too big for rsa key/);
123+
}, /digest too big for rsa key$/);
125124

126125
assert.throws(function() {
127126
// The correct header inside `test_bad_rsa_privkey.pem` should have been
@@ -133,7 +132,7 @@ assert.throws(function() {
133132
// $ openssl pkcs8 -topk8 -inform PEM -outform PEM -in mykey.pem \
134133
// -out private_key.pem -nocrypt;
135134
// Then open private_key.pem and change its header and footer.
136-
var sha1_privateKey = fs.readFileSync(common.fixturesDir +
135+
const sha1_privateKey = fs.readFileSync(common.fixturesDir +
137136
'/test_bad_rsa_privkey.pem', 'ascii');
138137
// this would inject errors onto OpenSSL's error stack
139138
crypto.createSign('sha1').sign(sha1_privateKey);

0 commit comments

Comments
 (0)
Please sign in to comment.