Browse Source

test: remove var redeclarations in test-crypto-*

PR-URL: https://github.com/nodejs/node/pull/4981
Reviewed-By: Brian White <mscdex@mscdex.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
process-exit-stdio-flushing
Rich Trott 9 years ago
committed by James M Snell
parent
commit
2c97bd47e4
  1. 46
      test/parallel/test-crypto-binary-default.js
  2. 28
      test/parallel/test-crypto-hmac.js
  3. 18
      test/parallel/test-crypto-rsa-dsa.js
  4. 94
      test/parallel/test-crypto-sign-verify.js
  5. 1
      test/parallel/test-crypto.js

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

@ -46,11 +46,11 @@ assert.throws(function() {
}, 'not enough data'); }, 'not enough data');
// Test HMAC // Test HMAC
var h1 = crypto.createHmac('sha1', 'Node') const hmacHash = crypto.createHmac('sha1', 'Node')
.update('some data') .update('some data')
.update('to hmac') .update('to hmac')
.digest('hex'); .digest('hex');
assert.equal(h1, '19fd6e1ba73d9ed2224dd5094a71babe85d9a892', 'test HMAC'); assert.equal(hmacHash, '19fd6e1ba73d9ed2224dd5094a71babe85d9a892', 'test HMAC');
// Test HMAC-SHA-* (rfc 4231 Test Cases) // Test HMAC-SHA-* (rfc 4231 Test Cases)
var rfc4231 = [ var rfc4231 = [
@ -199,7 +199,7 @@ var rfc4231 = [
} }
]; ];
for (var i = 0, l = rfc4231.length; i < l; i++) { for (let i = 0, l = rfc4231.length; i < l; i++) {
for (var hash in rfc4231[i]['hmac']) { for (var hash in rfc4231[i]['hmac']) {
var result = crypto.createHmac(hash, rfc4231[i]['key']) var result = crypto.createHmac(hash, rfc4231[i]['key'])
.update(rfc4231[i]['data']) .update(rfc4231[i]['data'])
@ -322,7 +322,7 @@ var rfc2202_sha1 = [
} }
]; ];
for (var i = 0, l = rfc2202_md5.length; i < l; i++) { for (let i = 0, l = rfc2202_md5.length; i < l; i++) {
if (!common.hasFipsCrypto) { if (!common.hasFipsCrypto) {
assert.equal(rfc2202_md5[i]['hmac'], assert.equal(rfc2202_md5[i]['hmac'],
crypto.createHmac('md5', rfc2202_md5[i]['key']) crypto.createHmac('md5', rfc2202_md5[i]['key'])
@ -331,7 +331,7 @@ for (var i = 0, l = rfc2202_md5.length; i < l; i++) {
'Test HMAC-MD5 : Test case ' + (i + 1) + ' rfc 2202'); 'Test HMAC-MD5 : Test case ' + (i + 1) + ' rfc 2202');
} }
} }
for (var i = 0, l = rfc2202_sha1.length; i < l; i++) { for (let i = 0, l = rfc2202_sha1.length; i < l; i++) {
assert.equal(rfc2202_sha1[i]['hmac'], assert.equal(rfc2202_sha1[i]['hmac'],
crypto.createHmac('sha1', rfc2202_sha1[i]['key']) crypto.createHmac('sha1', rfc2202_sha1[i]['key'])
.update(rfc2202_sha1[i]['data']) .update(rfc2202_sha1[i]['data'])
@ -394,29 +394,29 @@ assert.throws(function() {
var s1 = crypto.createSign('RSA-SHA1') var s1 = crypto.createSign('RSA-SHA1')
.update('Test123') .update('Test123')
.sign(keyPem, 'base64'); .sign(keyPem, 'base64');
var verified = crypto.createVerify('RSA-SHA1') var s1Verified = crypto.createVerify('RSA-SHA1')
.update('Test') .update('Test')
.update('123') .update('123')
.verify(certPem, s1, 'base64'); .verify(certPem, s1, 'base64');
assert.strictEqual(verified, true, 'sign and verify (base 64)'); assert.strictEqual(s1Verified, true, 'sign and verify (base 64)');
var s2 = crypto.createSign('RSA-SHA256') var s2 = crypto.createSign('RSA-SHA256')
.update('Test123') .update('Test123')
.sign(keyPem); // binary .sign(keyPem); // binary
var verified = crypto.createVerify('RSA-SHA256') var s2Verified = crypto.createVerify('RSA-SHA256')
.update('Test') .update('Test')
.update('123') .update('123')
.verify(certPem, s2); // binary .verify(certPem, s2); // binary
assert.strictEqual(verified, true, 'sign and verify (binary)'); assert.strictEqual(s2Verified, true, 'sign and verify (binary)');
var s3 = crypto.createSign('RSA-SHA1') var s3 = crypto.createSign('RSA-SHA1')
.update('Test123') .update('Test123')
.sign(keyPem, 'buffer'); .sign(keyPem, 'buffer');
var verified = crypto.createVerify('RSA-SHA1') var s3Verified = crypto.createVerify('RSA-SHA1')
.update('Test') .update('Test')
.update('123') .update('123')
.verify(certPem, s3); .verify(certPem, s3);
assert.strictEqual(verified, true, 'sign and verify (buffer)'); assert.strictEqual(s3Verified, true, 'sign and verify (buffer)');
function testCipher1(key) { function testCipher1(key) {

28
test/parallel/test-crypto-hmac.js

@ -59,14 +59,14 @@ var wikipedia = [
}, },
]; ];
for (var i = 0, l = wikipedia.length; i < l; i++) { for (let i = 0, l = wikipedia.length; i < l; i++) {
for (var hash in wikipedia[i]['hmac']) { for (const hash in wikipedia[i]['hmac']) {
// FIPS does not support MD5. // FIPS does not support MD5.
if (common.hasFipsCrypto && hash == 'md5') if (common.hasFipsCrypto && hash == 'md5')
continue; continue;
var result = crypto.createHmac(hash, wikipedia[i]['key']) const result = crypto.createHmac(hash, wikipedia[i]['key'])
.update(wikipedia[i]['data']) .update(wikipedia[i]['data'])
.digest('hex'); .digest('hex');
assert.equal(wikipedia[i]['hmac'][hash], assert.equal(wikipedia[i]['hmac'][hash],
result, result,
'Test HMAC-' + hash + ': Test case ' + (i + 1) + ' wikipedia'); 'Test HMAC-' + hash + ': Test case ' + (i + 1) + ' wikipedia');
@ -221,14 +221,14 @@ var rfc4231 = [
} }
]; ];
for (var i = 0, l = rfc4231.length; i < l; i++) { for (let i = 0, l = rfc4231.length; i < l; i++) {
for (var hash in rfc4231[i]['hmac']) { for (const hash in rfc4231[i]['hmac']) {
var str = crypto.createHmac(hash, rfc4231[i].key); const str = crypto.createHmac(hash, rfc4231[i].key);
str.end(rfc4231[i].data); str.end(rfc4231[i].data);
var strRes = str.read().toString('hex'); let strRes = str.read().toString('hex');
var result = crypto.createHmac(hash, rfc4231[i]['key']) let result = crypto.createHmac(hash, rfc4231[i]['key'])
.update(rfc4231[i]['data']) .update(rfc4231[i]['data'])
.digest('hex'); .digest('hex');
if (rfc4231[i]['truncate']) { if (rfc4231[i]['truncate']) {
result = result.substr(0, 32); // first 128 bits == 32 hex chars result = result.substr(0, 32); // first 128 bits == 32 hex chars
strRes = strRes.substr(0, 32); strRes = strRes.substr(0, 32);
@ -350,7 +350,7 @@ var rfc2202_sha1 = [
]; ];
if (!common.hasFipsCrypto) { if (!common.hasFipsCrypto) {
for (var i = 0, l = rfc2202_md5.length; i < l; i++) { for (let i = 0, l = rfc2202_md5.length; i < l; i++) {
assert.equal(rfc2202_md5[i]['hmac'], assert.equal(rfc2202_md5[i]['hmac'],
crypto.createHmac('md5', rfc2202_md5[i]['key']) crypto.createHmac('md5', rfc2202_md5[i]['key'])
.update(rfc2202_md5[i]['data']) .update(rfc2202_md5[i]['data'])
@ -358,7 +358,7 @@ if (!common.hasFipsCrypto) {
'Test HMAC-MD5 : Test case ' + (i + 1) + ' rfc 2202'); 'Test HMAC-MD5 : Test case ' + (i + 1) + ' rfc 2202');
} }
} }
for (var i = 0, l = rfc2202_sha1.length; i < l; i++) { for (let i = 0, l = rfc2202_sha1.length; i < l; i++) {
assert.equal(rfc2202_sha1[i]['hmac'], assert.equal(rfc2202_sha1[i]['hmac'],
crypto.createHmac('sha1', rfc2202_sha1[i]['key']) crypto.createHmac('sha1', rfc2202_sha1[i]['key'])
.update(rfc2202_sha1[i]['data']) .update(rfc2202_sha1[i]['data'])

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

@ -227,28 +227,30 @@ assert.throws(function() {
// //
// Test DSA signing and verification with encrypted key // Test DSA signing and verification with encrypted key
// //
(function() { const input = 'I AM THE WALRUS';
var input = 'I AM THE WALRUS';
var sign = crypto.createSign('DSS1'); {
const sign = crypto.createSign('DSS1');
sign.update(input); sign.update(input);
assert.throws(function() { assert.throws(function() {
sign.sign({ key: dsaKeyPemEncrypted, passphrase: 'wrong' }, 'hex'); sign.sign({ key: dsaKeyPemEncrypted, passphrase: 'wrong' }, 'hex');
}); });
}
{
// DSA signatures vary across runs so there is no static string to verify // DSA signatures vary across runs so there is no static string to verify
// against // against
var sign = crypto.createSign('DSS1'); const sign = crypto.createSign('DSS1');
sign.update(input); sign.update(input);
var signature; let signature;
assert.doesNotThrow(function() { assert.doesNotThrow(function() {
var signOptions = { key: dsaKeyPemEncrypted, passphrase: 'password' }; const signOptions = { key: dsaKeyPemEncrypted, passphrase: 'password' };
signature = sign.sign(signOptions, 'hex'); signature = sign.sign(signOptions, 'hex');
}); });
var verify = crypto.createVerify('DSS1'); const verify = crypto.createVerify('DSS1');
verify.update(input); verify.update(input);
assert.strictEqual(verify.verify(dsaPubPem, signature, 'hex'), true); assert.strictEqual(verify.verify(dsaPubPem, signature, 'hex'), true);
})(); }

94
test/parallel/test-crypto-sign-verify.js

@ -14,53 +14,59 @@ var certPem = fs.readFileSync(common.fixturesDir + '/test_cert.pem', 'ascii');
var keyPem = fs.readFileSync(common.fixturesDir + '/test_key.pem', 'ascii'); var keyPem = fs.readFileSync(common.fixturesDir + '/test_key.pem', 'ascii');
// Test signing and verifying // Test signing and verifying
var s1 = crypto.createSign('RSA-SHA1') {
.update('Test123') const s1 = crypto.createSign('RSA-SHA1')
.sign(keyPem, 'base64'); .update('Test123')
var s1stream = crypto.createSign('RSA-SHA1'); .sign(keyPem, 'base64');
s1stream.end('Test123'); let s1stream = crypto.createSign('RSA-SHA1');
s1stream = s1stream.sign(keyPem, 'base64'); s1stream.end('Test123');
assert.equal(s1, s1stream, 'Stream produces same output'); s1stream = s1stream.sign(keyPem, 'base64');
assert.equal(s1, s1stream, 'Stream produces same output');
var verified = crypto.createVerify('RSA-SHA1') const verified = crypto.createVerify('RSA-SHA1')
.update('Test') .update('Test')
.update('123') .update('123')
.verify(certPem, s1, 'base64'); .verify(certPem, s1, 'base64');
assert.strictEqual(verified, true, 'sign and verify (base 64)'); assert.strictEqual(verified, true, 'sign and verify (base 64)');
}
var s2 = crypto.createSign('RSA-SHA256') {
.update('Test123') const s2 = crypto.createSign('RSA-SHA256')
.sign(keyPem, 'binary'); .update('Test123')
var s2stream = crypto.createSign('RSA-SHA256'); .sign(keyPem, 'binary');
s2stream.end('Test123'); let s2stream = crypto.createSign('RSA-SHA256');
s2stream = s2stream.sign(keyPem, 'binary'); s2stream.end('Test123');
assert.equal(s2, s2stream, 'Stream produces same output'); s2stream = s2stream.sign(keyPem, 'binary');
assert.equal(s2, s2stream, 'Stream produces same output');
var verified = crypto.createVerify('RSA-SHA256') let verified = crypto.createVerify('RSA-SHA256')
.update('Test') .update('Test')
.update('123') .update('123')
.verify(certPem, s2, 'binary'); .verify(certPem, s2, 'binary');
assert.strictEqual(verified, true, 'sign and verify (binary)'); assert.strictEqual(verified, true, 'sign and verify (binary)');
var verStream = crypto.createVerify('RSA-SHA256'); const verStream = crypto.createVerify('RSA-SHA256');
verStream.write('Tes'); verStream.write('Tes');
verStream.write('t12'); verStream.write('t12');
verStream.end('3'); verStream.end('3');
verified = verStream.verify(certPem, s2, 'binary'); verified = verStream.verify(certPem, s2, 'binary');
assert.strictEqual(verified, true, 'sign and verify (stream)'); assert.strictEqual(verified, true, 'sign and verify (stream)');
}
var s3 = crypto.createSign('RSA-SHA1') {
.update('Test123') const s3 = crypto.createSign('RSA-SHA1')
.sign(keyPem, 'buffer'); .update('Test123')
var verified = crypto.createVerify('RSA-SHA1') .sign(keyPem, 'buffer');
.update('Test') let verified = crypto.createVerify('RSA-SHA1')
.update('123') .update('Test')
.verify(certPem, s3); .update('123')
assert.strictEqual(verified, true, 'sign and verify (buffer)'); .verify(certPem, s3);
assert.strictEqual(verified, true, 'sign and verify (buffer)');
var verStream = crypto.createVerify('RSA-SHA1'); const verStream = crypto.createVerify('RSA-SHA1');
verStream.write('Tes'); verStream.write('Tes');
verStream.write('t12'); verStream.write('t12');
verStream.end('3'); verStream.end('3');
verified = verStream.verify(certPem, s3); verified = verStream.verify(certPem, s3);
assert.strictEqual(verified, true, 'sign and verify (stream)'); assert.strictEqual(verified, true, 'sign and verify (stream)');
}

1
test/parallel/test-crypto.js

@ -67,7 +67,6 @@ assert.equal(-1, crypto.getCiphers().indexOf('AES-128-CBC'));
assertSorted(crypto.getCiphers()); assertSorted(crypto.getCiphers());
// Assume that we have at least AES256-SHA. // Assume that we have at least AES256-SHA.
var tls = require('tls');
assert.notEqual(0, tls.getCiphers().length); assert.notEqual(0, tls.getCiphers().length);
assert.notEqual(-1, tls.getCiphers().indexOf('aes256-sha')); assert.notEqual(-1, tls.getCiphers().indexOf('aes256-sha'));
assert.equal(-1, tls.getCiphers().indexOf('AES256-SHA')); assert.equal(-1, tls.getCiphers().indexOf('AES256-SHA'));

Loading…
Cancel
Save