Skip to content

Commit 272a971

Browse files
targosMylesBorins
authored andcommitted
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 8bb66cd commit 272a971

1 file changed

Lines changed: 43 additions & 44 deletions

File tree

‎test/parallel/test-crypto.js‎

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,33 @@
11
'use strict';
2-
varcommon=require('../common');
3-
varassert=require('assert');
4-
varutil=require('util');
2+
constcommon=require('../common');
53

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

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

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

1616
// Test Certificates
17-
varcaPem=fs.readFileSync(common.fixturesDir+'/test_ca.pem','ascii');
18-
varcertPem=fs.readFileSync(common.fixturesDir+'/test_cert.pem','ascii');
19-
varcertPfx=fs.readFileSync(common.fixturesDir+'/test_cert.pfx');
20-
varkeyPem=fs.readFileSync(common.fixturesDir+'/test_key.pem','ascii');
21-
vartls=require('tls');
17+
constcaPem=fs.readFileSync(common.fixturesDir+'/test_ca.pem','ascii');
18+
constcertPem=fs.readFileSync(common.fixturesDir+'/test_cert.pem','ascii');
19+
constcertPfx=fs.readFileSync(common.fixturesDir+'/test_cert.pfx');
20+
constkeyPem=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-
varoptions={key: keyPem,cert: certPem,ca: caPem};
27-
varcredentials=crypto.createCredentials(options);
28-
varcontext=credentials.context;
29-
varnotcontext={setOptions: context.setOptions,setKey: context.setKey};
30-
crypto.createCredentials({secureOptions: 1},notcontext);
31-
},TypeError);
25+
constoptions={key: keyPem,cert: certPem,ca: caPem};
26+
constcredentials=tls.createSecureContext(options);
27+
constcontext=credentials.context;
28+
constnotcontext={setOptions: context.setOptions,setKey: context.setKey};
29+
tls.createSecureContext({secureOptions: 1},notcontext);
30+
},/^TypeError:Illegalinvocation$/);
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:macverifyfailure$/);
4140

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

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

5049

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

5655

5756
functionassertSorted(list){
5857
// Array#sort() modifies the list in place so make a copy.
59-
varsorted=util._extend([],list).sort();
58+
constsorted=list.slice().sort();
6059
assert.deepStrictEqual(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(crypto.getCiphers().includes('aes-128-cbc'));
65+
assert(!crypto.getCiphers().includes('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(tls.getCiphers().includes('aes256-sha'));
71+
assert(!tls.getCiphers().includes('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(crypto.getHashes().includes('sha1'));
77+
assert(crypto.getHashes().includes('sha'));
78+
assert(!crypto.getHashes().includes('SHA1'));
79+
assert(!crypto.getHashes().includes('SHA'));
80+
assert(crypto.getHashes().includes('RSA-SHA1'));
81+
assert(!crypto.getHashes().includes('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(crypto.getCurves().includes('secp384r1'));
87+
assert(!crypto.getCurves().includes('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-
},/Badinputstring/);
102+
},/^TypeError:Badinputstring$/);
104103

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

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

113112
assert.throws(function(){
114-
varpriv=[
113+
constpriv=[
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-
},/digesttoobigforrsakey/);
123+
},/digesttoobigforrsakey$/);
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-
varsha1_privateKey=fs.readFileSync(common.fixturesDir+
135+
constsha1_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)