test: remove assert message and add block scope

The default message will be printed if the assertion fires. Use block
scope for related variables and tests.

PR-URL: https://github.com/nodejs/node/pull/19054
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
wuweiweiwu 2018-02-27 23:38:04 -08:00 committed by Anatoli Papirovski
parent 4489a48dff
commit 1ebd966d2c
No known key found for this signature in database
GPG Key ID: 614E2E1ABEB4B2C0

View File

@ -61,13 +61,16 @@ assert.throws(function() {
}, /^Error: not enough data$/); }, /^Error: not enough data$/);
// Test HMAC // Test HMAC
{
const hmacHash = 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.strictEqual(hmacHash, '19fd6e1ba73d9ed2224dd5094a71babe85d9a892'); assert.strictEqual(hmacHash, '19fd6e1ba73d9ed2224dd5094a71babe85d9a892');
}
// Test HMAC-SHA-* (rfc 4231 Test Cases) // Test HMAC-SHA-* (rfc 4231 Test Cases)
{
const rfc4231 = [ const rfc4231 = [
{ {
key: Buffer.from('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b', 'hex'), key: Buffer.from('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b', 'hex'),
@ -143,7 +146,6 @@ const rfc4231 = [
'e2adebeb10a298dd' 'e2adebeb10a298dd'
} }
}, },
{ {
key: Buffer.from('0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c', 'hex'), key: Buffer.from('0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c', 'hex'),
// 'Test With Truncation' // 'Test With Truncation'
@ -228,8 +230,10 @@ for (const testCase of rfc4231) {
); );
} }
} }
}
// Test HMAC-MD5/SHA1 (rfc 2202 Test Cases) // Test HMAC-MD5/SHA1 (rfc 2202 Test Cases)
{
const rfc2202_md5 = [ const rfc2202_md5 = [
{ {
key: Buffer.from('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b', 'hex'), key: Buffer.from('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b', 'hex'),
@ -356,8 +360,10 @@ for (const testCase of rfc2202_sha1) {
.digest('hex') .digest('hex')
); );
} }
}
// Test hashing // Test hashing
{
const a1 = crypto.createHash('sha1').update('Test123').digest('hex'); const a1 = crypto.createHash('sha1').update('Test123').digest('hex');
const a2 = crypto.createHash('sha256').update('Test123').digest('base64'); const a2 = crypto.createHash('sha256').update('Test123').digest('base64');
const a3 = crypto.createHash('sha512').update('Test123').digest(); // binary const a3 = crypto.createHash('sha512').update('Test123').digest(); // binary
@ -375,27 +381,32 @@ assert.strictEqual(a1, '8308651804facb7b9af8ffc53a33a22d6a1c8ac2');
assert.strictEqual(a2, '2bX1jws4GYKTlxhloUB09Z66PoJZW+y+hq5R8dnx9l4='); assert.strictEqual(a2, '2bX1jws4GYKTlxhloUB09Z66PoJZW+y+hq5R8dnx9l4=');
// Test SHA512 as assumed latin1
assert.strictEqual( assert.strictEqual(
a3, a3,
'\u00c1(4\u00f1\u0003\u001fd\u0097!O\'\u00d4C/&Qz\u00d4' + '\u00c1(4\u00f1\u0003\u001fd\u0097!O\'\u00d4C/&Qz\u00d4' +
'\u0094\u0015l\u00b8\u008dQ+\u00db\u001d\u00c4\u00b5}\u00b2' + '\u0094\u0015l\u00b8\u008dQ+\u00db\u001d\u00c4\u00b5}\u00b2' +
'\u00d6\u0092\u00a3\u00df\u00a2i\u00a1\u009b\n\n*\u000f' + '\u00d6\u0092\u00a3\u00df\u00a2i\u00a1\u009b\n\n*\u000f' +
'\u00d7\u00d6\u00a2\u00a8\u0085\u00e3<\u0083\u009c\u0093' + '\u00d7\u00d6\u00a2\u00a8\u0085\u00e3<\u0083\u009c\u0093' +
'\u00c2\u0006\u00da0\u00a1\u00879(G\u00ed\'', '\u00c2\u0006\u00da0\u00a1\u00879(G\u00ed\''
'Test SHA512 as assumed latin1'
); );
assert.deepStrictEqual( assert.deepStrictEqual(
a4, a4,
Buffer.from('8308651804facb7b9af8ffc53a33a22d6a1c8ac2', 'hex') Buffer.from('8308651804facb7b9af8ffc53a33a22d6a1c8ac2', 'hex')
); );
}
// Test multiple updates to same hash // Test multiple updates to same hash
{
const h1 = crypto.createHash('sha1').update('Test123').digest('hex'); const h1 = crypto.createHash('sha1').update('Test123').digest('hex');
const h2 = crypto.createHash('sha1').update('Test').update('123').digest('hex'); const h2 = crypto.createHash('sha1').update('Test').update('123')
.digest('hex');
assert.strictEqual(h1, h2); assert.strictEqual(h1, h2);
}
// Test hashing for binary files // Test hashing for binary files
{
const fn = fixtures.path('sample.png'); const fn = fixtures.path('sample.png');
const sha1Hash = crypto.createHash('sha1'); const sha1Hash = crypto.createHash('sha1');
const fileStream = fs.createReadStream(fn); const fileStream = fs.createReadStream(fn);
@ -408,6 +419,7 @@ fileStream.on('close', common.mustCall(function() {
'22723e553129a336ad96e10f6aecdf0f45e4149e' '22723e553129a336ad96e10f6aecdf0f45e4149e'
); );
})); }));
}
// Unknown digest method should throw an error: // Unknown digest method should throw an error:
// https://github.com/nodejs/node-v0.x-archive/issues/2227 // https://github.com/nodejs/node-v0.x-archive/issues/2227
@ -416,6 +428,7 @@ assert.throws(function() {
}, /^Error: Digest method not supported$/); }, /^Error: Digest method not supported$/);
// Test signing and verifying // Test signing and verifying
{
const s1 = crypto.createSign('SHA1') const s1 = crypto.createSign('SHA1')
.update('Test123') .update('Test123')
.sign(keyPem, 'base64'); .sign(keyPem, 'base64');
@ -442,6 +455,7 @@ const s3Verified = crypto.createVerify('SHA1')
.update('123') .update('123')
.verify(certPem, s3); .verify(certPem, s3);
assert.strictEqual(s3Verified, true); assert.strictEqual(s3Verified, true);
}
function testCipher1(key) { function testCipher1(key) {
@ -567,6 +581,7 @@ common.expectsError(
// Test Diffie-Hellman with two parties sharing a secret, // Test Diffie-Hellman with two parties sharing a secret,
// using various encodings as we go along // using various encodings as we go along
{
const dh1 = crypto.createDiffieHellman(common.hasFipsCrypto ? 1024 : 256); const dh1 = crypto.createDiffieHellman(common.hasFipsCrypto ? 1024 : 256);
const p1 = dh1.getPrime('buffer'); const p1 = dh1.getPrime('buffer');
const dh2 = crypto.createDiffieHellman(p1, 'base64'); const dh2 = crypto.createDiffieHellman(p1, 'base64');
@ -620,7 +635,7 @@ assert.strictEqual(
rsaVerify.update(rsaPubPem); rsaVerify.update(rsaPubPem);
assert.strictEqual(rsaVerify.verify(rsaPubPem, rsaSignature, 'hex'), true); assert.strictEqual(rsaVerify.verify(rsaPubPem, rsaSignature, 'hex'), true);
}
// //
// Test RSA signing and verification // Test RSA signing and verification