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:
parent
4489a48dff
commit
1ebd966d2c
@ -61,14 +61,17 @@ assert.throws(function() {
|
||||
}, /^Error: not enough data$/);
|
||||
|
||||
// Test HMAC
|
||||
const hmacHash = crypto.createHmac('sha1', 'Node')
|
||||
{
|
||||
const hmacHash = crypto.createHmac('sha1', 'Node')
|
||||
.update('some data')
|
||||
.update('to hmac')
|
||||
.digest('hex');
|
||||
assert.strictEqual(hmacHash, '19fd6e1ba73d9ed2224dd5094a71babe85d9a892');
|
||||
assert.strictEqual(hmacHash, '19fd6e1ba73d9ed2224dd5094a71babe85d9a892');
|
||||
}
|
||||
|
||||
// Test HMAC-SHA-* (rfc 4231 Test Cases)
|
||||
const rfc4231 = [
|
||||
{
|
||||
const rfc4231 = [
|
||||
{
|
||||
key: Buffer.from('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b', 'hex'),
|
||||
data: Buffer.from('4869205468657265', 'hex'), // 'Hi There'
|
||||
@ -143,7 +146,6 @@ const rfc4231 = [
|
||||
'e2adebeb10a298dd'
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
key: Buffer.from('0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c', 'hex'),
|
||||
// 'Test With Truncation'
|
||||
@ -212,9 +214,9 @@ const rfc4231 = [
|
||||
'65c97440fa8c6a58'
|
||||
}
|
||||
}
|
||||
];
|
||||
];
|
||||
|
||||
for (const testCase of rfc4231) {
|
||||
for (const testCase of rfc4231) {
|
||||
for (const hash in testCase.hmac) {
|
||||
let result = crypto.createHmac(hash, testCase.key)
|
||||
.update(testCase.data)
|
||||
@ -227,10 +229,12 @@ for (const testCase of rfc4231) {
|
||||
result
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Test HMAC-MD5/SHA1 (rfc 2202 Test Cases)
|
||||
const rfc2202_md5 = [
|
||||
{
|
||||
const rfc2202_md5 = [
|
||||
{
|
||||
key: Buffer.from('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b', 'hex'),
|
||||
data: 'Hi There',
|
||||
@ -282,8 +286,8 @@ const rfc2202_md5 = [
|
||||
'Block-Size Data',
|
||||
hmac: '6f630fad67cda0ee1fb1f562db3aa53e'
|
||||
}
|
||||
];
|
||||
const rfc2202_sha1 = [
|
||||
];
|
||||
const rfc2202_sha1 = [
|
||||
{
|
||||
key: Buffer.from('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b', 'hex'),
|
||||
data: 'Hi There',
|
||||
@ -336,9 +340,9 @@ const rfc2202_sha1 = [
|
||||
'Block-Size Data',
|
||||
hmac: 'e8e99d0f45237d786d6bbaa7965c7808bbff1a91'
|
||||
}
|
||||
];
|
||||
];
|
||||
|
||||
if (!common.hasFipsCrypto) {
|
||||
if (!common.hasFipsCrypto) {
|
||||
for (const testCase of rfc2202_md5) {
|
||||
assert.strictEqual(
|
||||
testCase.hmac,
|
||||
@ -347,67 +351,75 @@ if (!common.hasFipsCrypto) {
|
||||
.digest('hex')
|
||||
);
|
||||
}
|
||||
}
|
||||
for (const testCase of rfc2202_sha1) {
|
||||
}
|
||||
for (const testCase of rfc2202_sha1) {
|
||||
assert.strictEqual(
|
||||
testCase.hmac,
|
||||
crypto.createHmac('sha1', testCase.key)
|
||||
.update(testCase.data)
|
||||
.digest('hex')
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Test hashing
|
||||
const a1 = crypto.createHash('sha1').update('Test123').digest('hex');
|
||||
const a2 = crypto.createHash('sha256').update('Test123').digest('base64');
|
||||
const a3 = crypto.createHash('sha512').update('Test123').digest(); // binary
|
||||
const a4 = crypto.createHash('sha1').update('Test123').digest('buffer');
|
||||
{
|
||||
const a1 = crypto.createHash('sha1').update('Test123').digest('hex');
|
||||
const a2 = crypto.createHash('sha256').update('Test123').digest('base64');
|
||||
const a3 = crypto.createHash('sha512').update('Test123').digest(); // binary
|
||||
const a4 = crypto.createHash('sha1').update('Test123').digest('buffer');
|
||||
|
||||
if (!common.hasFipsCrypto) {
|
||||
if (!common.hasFipsCrypto) {
|
||||
const a0 = crypto.createHash('md5').update('Test123').digest('latin1');
|
||||
assert.strictEqual(
|
||||
a0,
|
||||
'h\u00ea\u00cb\u0097\u00d8o\fF!\u00fa+\u000e\u0017\u00ca\u00bd\u008c'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
assert.strictEqual(a1, '8308651804facb7b9af8ffc53a33a22d6a1c8ac2');
|
||||
assert.strictEqual(a1, '8308651804facb7b9af8ffc53a33a22d6a1c8ac2');
|
||||
|
||||
assert.strictEqual(a2, '2bX1jws4GYKTlxhloUB09Z66PoJZW+y+hq5R8dnx9l4=');
|
||||
assert.strictEqual(a2, '2bX1jws4GYKTlxhloUB09Z66PoJZW+y+hq5R8dnx9l4=');
|
||||
|
||||
assert.strictEqual(
|
||||
// Test SHA512 as assumed latin1
|
||||
assert.strictEqual(
|
||||
a3,
|
||||
'\u00c1(4\u00f1\u0003\u001fd\u0097!O\'\u00d4C/&Qz\u00d4' +
|
||||
'\u0094\u0015l\u00b8\u008dQ+\u00db\u001d\u00c4\u00b5}\u00b2' +
|
||||
'\u00d6\u0092\u00a3\u00df\u00a2i\u00a1\u009b\n\n*\u000f' +
|
||||
'\u00d7\u00d6\u00a2\u00a8\u0085\u00e3<\u0083\u009c\u0093' +
|
||||
'\u00c2\u0006\u00da0\u00a1\u00879(G\u00ed\'',
|
||||
'Test SHA512 as assumed latin1'
|
||||
);
|
||||
'\u00c2\u0006\u00da0\u00a1\u00879(G\u00ed\''
|
||||
);
|
||||
|
||||
assert.deepStrictEqual(
|
||||
assert.deepStrictEqual(
|
||||
a4,
|
||||
Buffer.from('8308651804facb7b9af8ffc53a33a22d6a1c8ac2', 'hex')
|
||||
);
|
||||
);
|
||||
}
|
||||
|
||||
// Test multiple updates to same hash
|
||||
const h1 = crypto.createHash('sha1').update('Test123').digest('hex');
|
||||
const h2 = crypto.createHash('sha1').update('Test').update('123').digest('hex');
|
||||
assert.strictEqual(h1, h2);
|
||||
{
|
||||
const h1 = crypto.createHash('sha1').update('Test123').digest('hex');
|
||||
const h2 = crypto.createHash('sha1').update('Test').update('123')
|
||||
.digest('hex');
|
||||
assert.strictEqual(h1, h2);
|
||||
}
|
||||
|
||||
// Test hashing for binary files
|
||||
const fn = fixtures.path('sample.png');
|
||||
const sha1Hash = crypto.createHash('sha1');
|
||||
const fileStream = fs.createReadStream(fn);
|
||||
fileStream.on('data', function(data) {
|
||||
{
|
||||
const fn = fixtures.path('sample.png');
|
||||
const sha1Hash = crypto.createHash('sha1');
|
||||
const fileStream = fs.createReadStream(fn);
|
||||
fileStream.on('data', function(data) {
|
||||
sha1Hash.update(data);
|
||||
});
|
||||
fileStream.on('close', common.mustCall(function() {
|
||||
});
|
||||
fileStream.on('close', common.mustCall(function() {
|
||||
assert.strictEqual(
|
||||
sha1Hash.digest('hex'),
|
||||
'22723e553129a336ad96e10f6aecdf0f45e4149e'
|
||||
);
|
||||
}));
|
||||
}));
|
||||
}
|
||||
|
||||
// Unknown digest method should throw an error:
|
||||
// https://github.com/nodejs/node-v0.x-archive/issues/2227
|
||||
@ -416,32 +428,34 @@ assert.throws(function() {
|
||||
}, /^Error: Digest method not supported$/);
|
||||
|
||||
// Test signing and verifying
|
||||
const s1 = crypto.createSign('SHA1')
|
||||
{
|
||||
const s1 = crypto.createSign('SHA1')
|
||||
.update('Test123')
|
||||
.sign(keyPem, 'base64');
|
||||
const s1Verified = crypto.createVerify('SHA1')
|
||||
const s1Verified = crypto.createVerify('SHA1')
|
||||
.update('Test')
|
||||
.update('123')
|
||||
.verify(certPem, s1, 'base64');
|
||||
assert.strictEqual(s1Verified, true);
|
||||
assert.strictEqual(s1Verified, true);
|
||||
|
||||
const s2 = crypto.createSign('SHA256')
|
||||
const s2 = crypto.createSign('SHA256')
|
||||
.update('Test123')
|
||||
.sign(keyPem); // binary
|
||||
const s2Verified = crypto.createVerify('SHA256')
|
||||
const s2Verified = crypto.createVerify('SHA256')
|
||||
.update('Test')
|
||||
.update('123')
|
||||
.verify(certPem, s2); // binary
|
||||
assert.strictEqual(s2Verified, true);
|
||||
assert.strictEqual(s2Verified, true);
|
||||
|
||||
const s3 = crypto.createSign('SHA1')
|
||||
const s3 = crypto.createSign('SHA1')
|
||||
.update('Test123')
|
||||
.sign(keyPem, 'buffer');
|
||||
const s3Verified = crypto.createVerify('SHA1')
|
||||
const s3Verified = crypto.createVerify('SHA1')
|
||||
.update('Test')
|
||||
.update('123')
|
||||
.verify(certPem, s3);
|
||||
assert.strictEqual(s3Verified, true);
|
||||
assert.strictEqual(s3Verified, true);
|
||||
}
|
||||
|
||||
|
||||
function testCipher1(key) {
|
||||
@ -567,60 +581,61 @@ common.expectsError(
|
||||
|
||||
// Test Diffie-Hellman with two parties sharing a secret,
|
||||
// using various encodings as we go along
|
||||
const dh1 = crypto.createDiffieHellman(common.hasFipsCrypto ? 1024 : 256);
|
||||
const p1 = dh1.getPrime('buffer');
|
||||
const dh2 = crypto.createDiffieHellman(p1, 'base64');
|
||||
const key1 = dh1.generateKeys();
|
||||
const key2 = dh2.generateKeys('hex');
|
||||
const secret1 = dh1.computeSecret(key2, 'hex', 'base64');
|
||||
const secret2 = dh2.computeSecret(key1, 'latin1', 'buffer');
|
||||
{
|
||||
const dh1 = crypto.createDiffieHellman(common.hasFipsCrypto ? 1024 : 256);
|
||||
const p1 = dh1.getPrime('buffer');
|
||||
const dh2 = crypto.createDiffieHellman(p1, 'base64');
|
||||
const key1 = dh1.generateKeys();
|
||||
const key2 = dh2.generateKeys('hex');
|
||||
const secret1 = dh1.computeSecret(key2, 'hex', 'base64');
|
||||
const secret2 = dh2.computeSecret(key1, 'latin1', 'buffer');
|
||||
|
||||
assert.strictEqual(secret1, secret2.toString('base64'));
|
||||
assert.strictEqual(secret1, secret2.toString('base64'));
|
||||
|
||||
// Create "another dh1" using generated keys from dh1,
|
||||
// and compute secret again
|
||||
const dh3 = crypto.createDiffieHellman(p1, 'buffer');
|
||||
const privkey1 = dh1.getPrivateKey();
|
||||
dh3.setPublicKey(key1);
|
||||
dh3.setPrivateKey(privkey1);
|
||||
// Create "another dh1" using generated keys from dh1,
|
||||
// and compute secret again
|
||||
const dh3 = crypto.createDiffieHellman(p1, 'buffer');
|
||||
const privkey1 = dh1.getPrivateKey();
|
||||
dh3.setPublicKey(key1);
|
||||
dh3.setPrivateKey(privkey1);
|
||||
|
||||
assert.strictEqual(dh1.getPrime(), dh3.getPrime());
|
||||
assert.strictEqual(dh1.getGenerator(), dh3.getGenerator());
|
||||
assert.strictEqual(dh1.getPublicKey(), dh3.getPublicKey());
|
||||
assert.strictEqual(dh1.getPrivateKey(), dh3.getPrivateKey());
|
||||
assert.strictEqual(dh1.getPrime(), dh3.getPrime());
|
||||
assert.strictEqual(dh1.getGenerator(), dh3.getGenerator());
|
||||
assert.strictEqual(dh1.getPublicKey(), dh3.getPublicKey());
|
||||
assert.strictEqual(dh1.getPrivateKey(), dh3.getPrivateKey());
|
||||
|
||||
const secret3 = dh3.computeSecret(key2, 'hex', 'base64');
|
||||
const secret3 = dh3.computeSecret(key2, 'hex', 'base64');
|
||||
|
||||
assert.strictEqual(secret1, secret3);
|
||||
assert.strictEqual(secret1, secret3);
|
||||
|
||||
// https://github.com/joyent/node/issues/2338
|
||||
const p = 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74' +
|
||||
// https://github.com/joyent/node/issues/2338
|
||||
const p = 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74' +
|
||||
'020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F1437' +
|
||||
'4FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' +
|
||||
'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381FFFFFFFFFFFFFFFF';
|
||||
const d = crypto.createDiffieHellman(p, 'hex');
|
||||
assert.strictEqual(d.verifyError, DH_NOT_SUITABLE_GENERATOR);
|
||||
const d = crypto.createDiffieHellman(p, 'hex');
|
||||
assert.strictEqual(d.verifyError, DH_NOT_SUITABLE_GENERATOR);
|
||||
|
||||
// Test RSA key signing/verification
|
||||
const rsaSign = crypto.createSign('SHA1');
|
||||
const rsaVerify = crypto.createVerify('SHA1');
|
||||
assert.ok(rsaSign instanceof crypto.Sign);
|
||||
assert.ok(rsaVerify instanceof crypto.Verify);
|
||||
// Test RSA key signing/verification
|
||||
const rsaSign = crypto.createSign('SHA1');
|
||||
const rsaVerify = crypto.createVerify('SHA1');
|
||||
assert.ok(rsaSign instanceof crypto.Sign);
|
||||
assert.ok(rsaVerify instanceof crypto.Verify);
|
||||
|
||||
rsaSign.update(rsaPubPem);
|
||||
const rsaSignature = rsaSign.sign(rsaKeyPem, 'hex');
|
||||
assert.strictEqual(
|
||||
rsaSign.update(rsaPubPem);
|
||||
const rsaSignature = rsaSign.sign(rsaKeyPem, 'hex');
|
||||
assert.strictEqual(
|
||||
rsaSignature,
|
||||
'5c50e3145c4e2497aadb0eabc83b342d0b0021ece0d4c4a064b7c' +
|
||||
'8f020d7e2688b122bfb54c724ac9ee169f83f66d2fe90abeb95e8' +
|
||||
'e1290e7e177152a4de3d944cf7d4883114a20ed0f78e70e25ef0f' +
|
||||
'60f06b858e6af42a2f276ede95bbc6bc9a9bbdda15bd663186a6f' +
|
||||
'40819a7af19e577bb2efa5e579a1f5ce8a0d4ca8b8f6'
|
||||
);
|
||||
|
||||
rsaVerify.update(rsaPubPem);
|
||||
assert.strictEqual(rsaVerify.verify(rsaPubPem, rsaSignature, 'hex'), true);
|
||||
);
|
||||
|
||||
rsaVerify.update(rsaPubPem);
|
||||
assert.strictEqual(rsaVerify.verify(rsaPubPem, rsaSignature, 'hex'), true);
|
||||
}
|
||||
|
||||
//
|
||||
// Test RSA signing and verification
|
||||
|
Loading…
x
Reference in New Issue
Block a user