Skip to content

Commit fc05510

Browse files
edsadritaloacasas
authored andcommitted
test: improve test-crypto-rsa-dsa
* use const and let instead of var * use assert.strictEqual or assert.strictDeepEqual instead of assert.equal * use arrow functions * swap assertions arguments to match the standard * validate the error for assert.throws PR-URL: #10681 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent 2f9fdc4 commit fc05510

File tree

1 file changed

+54
-53
lines changed

1 file changed

+54
-53
lines changed

test/parallel/test-crypto-rsa-dsa.js

+54-53
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,24 @@ const constants = require('crypto').constants;
1111
const crypto = require('crypto');
1212

1313
// Test certificates
14-
var certPem = fs.readFileSync(common.fixturesDir + '/test_cert.pem', 'ascii');
15-
var keyPem = fs.readFileSync(common.fixturesDir + '/test_key.pem', 'ascii');
16-
var rsaPubPem = fs.readFileSync(common.fixturesDir + '/test_rsa_pubkey.pem',
17-
'ascii');
18-
var rsaKeyPem = fs.readFileSync(common.fixturesDir + '/test_rsa_privkey.pem',
19-
'ascii');
20-
var rsaKeyPemEncrypted = fs.readFileSync(
14+
const certPem = fs.readFileSync(common.fixturesDir + '/test_cert.pem', 'ascii');
15+
const keyPem = fs.readFileSync(common.fixturesDir + '/test_key.pem', 'ascii');
16+
const rsaPubPem = fs.readFileSync(common.fixturesDir + '/test_rsa_pubkey.pem',
17+
'ascii');
18+
const rsaKeyPem = fs.readFileSync(common.fixturesDir + '/test_rsa_privkey.pem',
19+
'ascii');
20+
const rsaKeyPemEncrypted = fs.readFileSync(
2121
common.fixturesDir + '/test_rsa_privkey_encrypted.pem', 'ascii');
22-
var dsaPubPem = fs.readFileSync(common.fixturesDir + '/test_dsa_pubkey.pem',
23-
'ascii');
24-
var dsaKeyPem = fs.readFileSync(common.fixturesDir + '/test_dsa_privkey.pem',
25-
'ascii');
26-
var dsaKeyPemEncrypted = fs.readFileSync(
22+
const dsaPubPem = fs.readFileSync(common.fixturesDir + '/test_dsa_pubkey.pem',
23+
'ascii');
24+
const dsaKeyPem = fs.readFileSync(common.fixturesDir + '/test_dsa_privkey.pem',
25+
'ascii');
26+
const dsaKeyPemEncrypted = fs.readFileSync(
2727
common.fixturesDir + '/test_dsa_privkey_encrypted.pem', 'ascii');
2828

29+
const decryptError = new RegExp('^Error: error:06065064:digital envelope ' +
30+
'routines:EVP_DecryptFinal_ex:bad decrypt$');
31+
2932
// Test RSA encryption/decryption
3033
{
3134
const input = 'I AM THE WALRUS';
@@ -34,13 +37,13 @@ var dsaKeyPemEncrypted = fs.readFileSync(
3437
let encryptedBuffer = crypto.publicEncrypt(rsaPubPem, bufferToEncrypt);
3538

3639
let decryptedBuffer = crypto.privateDecrypt(rsaKeyPem, encryptedBuffer);
37-
assert.strictEqual(input, decryptedBuffer.toString());
40+
assert.strictEqual(decryptedBuffer.toString(), input);
3841

3942
let decryptedBufferWithPassword = crypto.privateDecrypt({
4043
key: rsaKeyPemEncrypted,
4144
passphrase: 'password'
4245
}, encryptedBuffer);
43-
assert.strictEqual(input, decryptedBufferWithPassword.toString());
46+
assert.strictEqual(decryptedBufferWithPassword.toString(), input);
4447

4548
encryptedBuffer = crypto.publicEncrypt({
4649
key: rsaKeyPemEncrypted,
@@ -51,7 +54,7 @@ var dsaKeyPemEncrypted = fs.readFileSync(
5154
key: rsaKeyPemEncrypted,
5255
passphrase: 'password'
5356
}, encryptedBuffer);
54-
assert.strictEqual(input, decryptedBufferWithPassword.toString());
57+
assert.strictEqual(decryptedBufferWithPassword.toString(), input);
5558

5659
encryptedBuffer = crypto.privateEncrypt({
5760
key: rsaKeyPemEncrypted,
@@ -62,116 +65,114 @@ var dsaKeyPemEncrypted = fs.readFileSync(
6265
key: rsaKeyPemEncrypted,
6366
passphrase: Buffer.from('password')
6467
}, encryptedBuffer);
65-
assert.strictEqual(input, decryptedBufferWithPassword.toString());
68+
assert.strictEqual(decryptedBufferWithPassword.toString(), input);
6669

6770
encryptedBuffer = crypto.publicEncrypt(certPem, bufferToEncrypt);
6871

6972
decryptedBuffer = crypto.privateDecrypt(keyPem, encryptedBuffer);
70-
assert.strictEqual(input, decryptedBuffer.toString());
73+
assert.strictEqual(decryptedBuffer.toString(), input);
7174

7275
encryptedBuffer = crypto.publicEncrypt(keyPem, bufferToEncrypt);
7376

7477
decryptedBuffer = crypto.privateDecrypt(keyPem, encryptedBuffer);
75-
assert.strictEqual(input, decryptedBuffer.toString());
78+
assert.strictEqual(decryptedBuffer.toString(), input);
7679

7780
encryptedBuffer = crypto.privateEncrypt(keyPem, bufferToEncrypt);
7881

7982
decryptedBuffer = crypto.publicDecrypt(keyPem, encryptedBuffer);
80-
assert.strictEqual(input, decryptedBuffer.toString());
83+
assert.strictEqual(decryptedBuffer.toString(), input);
8184

82-
assert.throws(function() {
85+
assert.throws(() => {
8386
crypto.privateDecrypt({
8487
key: rsaKeyPemEncrypted,
8588
passphrase: 'wrong'
8689
}, bufferToEncrypt);
87-
});
90+
}, decryptError);
8891

89-
assert.throws(function() {
92+
assert.throws(() => {
9093
crypto.publicEncrypt({
9194
key: rsaKeyPemEncrypted,
9295
passphrase: 'wrong'
9396
}, encryptedBuffer);
94-
});
97+
}, decryptError);
9598

9699
encryptedBuffer = crypto.privateEncrypt({
97100
key: rsaKeyPemEncrypted,
98101
passphrase: Buffer.from('password')
99102
}, bufferToEncrypt);
100103

101-
assert.throws(function() {
104+
assert.throws(() => {
102105
crypto.publicDecrypt({
103106
key: rsaKeyPemEncrypted,
104107
passphrase: [].concat.apply([], Buffer.from('password'))
105108
}, encryptedBuffer);
106-
});
109+
}, decryptError);
107110
}
108111

109112
function test_rsa(padding) {
110-
var input = Buffer.allocUnsafe(padding === 'RSA_NO_PADDING' ? 1024 / 8 : 32);
111-
for (var i = 0; i < input.length; i++)
113+
const size = (padding === 'RSA_NO_PADDING') ? 1024 / 8 : 32;
114+
const input = Buffer.allocUnsafe(size);
115+
for (let i = 0; i < input.length; i++)
112116
input[i] = (i * 7 + 11) & 0xff;
113-
var bufferToEncrypt = Buffer.from(input);
117+
const bufferToEncrypt = Buffer.from(input);
114118

115119
padding = constants[padding];
116120

117-
var encryptedBuffer = crypto.publicEncrypt({
121+
const encryptedBuffer = crypto.publicEncrypt({
118122
key: rsaPubPem,
119123
padding: padding
120124
}, bufferToEncrypt);
121125

122-
var decryptedBuffer = crypto.privateDecrypt({
126+
const decryptedBuffer = crypto.privateDecrypt({
123127
key: rsaKeyPem,
124128
padding: padding
125129
}, encryptedBuffer);
126-
assert.equal(input, decryptedBuffer.toString());
130+
assert.deepStrictEqual(decryptedBuffer, input);
127131
}
128132

129133
test_rsa('RSA_NO_PADDING');
130134
test_rsa('RSA_PKCS1_PADDING');
131135
test_rsa('RSA_PKCS1_OAEP_PADDING');
132136

133137
// Test RSA key signing/verification
134-
var rsaSign = crypto.createSign('RSA-SHA1');
135-
var rsaVerify = crypto.createVerify('RSA-SHA1');
138+
let rsaSign = crypto.createSign('RSA-SHA1');
139+
let rsaVerify = crypto.createVerify('RSA-SHA1');
136140
assert.ok(rsaSign);
137141
assert.ok(rsaVerify);
138142

143+
const expectedSignature =
144+
'5c50e3145c4e2497aadb0eabc83b342d0b0021ece0d4c4a064b7c' +
145+
'8f020d7e2688b122bfb54c724ac9ee169f83f66d2fe90abeb95e8' +
146+
'e1290e7e177152a4de3d944cf7d4883114a20ed0f78e70e25ef0f' +
147+
'60f06b858e6af42a2f276ede95bbc6bc9a9bbdda15bd663186a6f' +
148+
'40819a7af19e577bb2efa5e579a1f5ce8a0d4ca8b8f6';
149+
139150
rsaSign.update(rsaPubPem);
140-
var rsaSignature = rsaSign.sign(rsaKeyPem, 'hex');
141-
assert.equal(rsaSignature,
142-
'5c50e3145c4e2497aadb0eabc83b342d0b0021ece0d4c4a064b7c' +
143-
'8f020d7e2688b122bfb54c724ac9ee169f83f66d2fe90abeb95e8' +
144-
'e1290e7e177152a4de3d944cf7d4883114a20ed0f78e70e25ef0f' +
145-
'60f06b858e6af42a2f276ede95bbc6bc9a9bbdda15bd663186a6f' +
146-
'40819a7af19e577bb2efa5e579a1f5ce8a0d4ca8b8f6');
151+
let rsaSignature = rsaSign.sign(rsaKeyPem, 'hex');
152+
assert.strictEqual(rsaSignature, expectedSignature);
147153

148154
rsaVerify.update(rsaPubPem);
149155
assert.strictEqual(rsaVerify.verify(rsaPubPem, rsaSignature, 'hex'), true);
150156

151157
// Test RSA key signing/verification with encrypted key
152158
rsaSign = crypto.createSign('RSA-SHA1');
153159
rsaSign.update(rsaPubPem);
154-
assert.doesNotThrow(function() {
160+
assert.doesNotThrow(() => {
155161
var signOptions = { key: rsaKeyPemEncrypted, passphrase: 'password' };
156162
rsaSignature = rsaSign.sign(signOptions, 'hex');
157163
});
158-
assert.equal(rsaSignature,
159-
'5c50e3145c4e2497aadb0eabc83b342d0b0021ece0d4c4a064b7c' +
160-
'8f020d7e2688b122bfb54c724ac9ee169f83f66d2fe90abeb95e8' +
161-
'e1290e7e177152a4de3d944cf7d4883114a20ed0f78e70e25ef0f' +
162-
'60f06b858e6af42a2f276ede95bbc6bc9a9bbdda15bd663186a6f' +
163-
'40819a7af19e577bb2efa5e579a1f5ce8a0d4ca8b8f6');
164+
assert.strictEqual(rsaSignature, expectedSignature);
164165

165166
rsaVerify = crypto.createVerify('RSA-SHA1');
166167
rsaVerify.update(rsaPubPem);
167168
assert.strictEqual(rsaVerify.verify(rsaPubPem, rsaSignature, 'hex'), true);
168169

169170
rsaSign = crypto.createSign('RSA-SHA1');
170171
rsaSign.update(rsaPubPem);
171-
assert.throws(function() {
172+
assert.throws(() => {
172173
var signOptions = { key: rsaKeyPemEncrypted, passphrase: 'wrong' };
173174
rsaSign.sign(signOptions, 'hex');
174-
});
175+
}, decryptError);
175176

176177
//
177178
// Test RSA signing and verification
@@ -196,7 +197,7 @@ assert.throws(function() {
196197
sign.update(input);
197198

198199
const output = sign.sign(privateKey, 'hex');
199-
assert.strictEqual(output, signature);
200+
assert.strictEqual(signature, output);
200201

201202
const verify = crypto.createVerify('RSA-SHA256');
202203
verify.update(input);
@@ -232,9 +233,9 @@ const input = 'I AM THE WALRUS';
232233
{
233234
const sign = crypto.createSign('DSS1');
234235
sign.update(input);
235-
assert.throws(function() {
236+
assert.throws(() => {
236237
sign.sign({ key: dsaKeyPemEncrypted, passphrase: 'wrong' }, 'hex');
237-
});
238+
}, decryptError);
238239
}
239240

240241
{
@@ -244,7 +245,7 @@ const input = 'I AM THE WALRUS';
244245
sign.update(input);
245246

246247
let signature;
247-
assert.doesNotThrow(function() {
248+
assert.doesNotThrow(() => {
248249
const signOptions = { key: dsaKeyPemEncrypted, passphrase: 'password' };
249250
signature = sign.sign(signOptions, 'hex');
250251
});

0 commit comments

Comments
 (0)