test: improve crypto HMAC test assertions
Fixes argument order for assertions and makes their failure messages more descriptive and easier to debug. PR-URL: https://github.com/nodejs/node/pull/16026 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Lance Ball <lball@redhat.com>
This commit is contained in:
parent
f8dd1ac918
commit
fe38ace643
@ -6,12 +6,17 @@ if (!common.hasCrypto)
|
|||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
const crypto = require('crypto');
|
const crypto = require('crypto');
|
||||||
|
|
||||||
// Test HMAC
|
{
|
||||||
const h1 = crypto.createHmac('sha1', 'Node')
|
// Test HMAC
|
||||||
.update('some data')
|
const actual = crypto.createHmac('sha1', 'Node')
|
||||||
.update('to hmac')
|
.update('some data')
|
||||||
.digest('hex');
|
.update('to hmac')
|
||||||
assert.strictEqual(h1, '19fd6e1ba73d9ed2224dd5094a71babe85d9a892', 'test HMAC');
|
.digest('hex');
|
||||||
|
const expected = '19fd6e1ba73d9ed2224dd5094a71babe85d9a892';
|
||||||
|
assert.strictEqual(actual,
|
||||||
|
expected,
|
||||||
|
`Test HMAC: ${actual} must be ${expected}`);
|
||||||
|
}
|
||||||
|
|
||||||
// Test HMAC (Wikipedia Test Cases)
|
// Test HMAC (Wikipedia Test Cases)
|
||||||
const wikipedia = [
|
const wikipedia = [
|
||||||
@ -62,12 +67,15 @@ for (let i = 0, l = wikipedia.length; i < l; i++) {
|
|||||||
// FIPS does not support MD5.
|
// FIPS does not support MD5.
|
||||||
if (common.hasFipsCrypto && hash === 'md5')
|
if (common.hasFipsCrypto && hash === 'md5')
|
||||||
continue;
|
continue;
|
||||||
const result = crypto.createHmac(hash, wikipedia[i]['key'])
|
const expected = wikipedia[i]['hmac'][hash];
|
||||||
|
const actual = crypto.createHmac(hash, wikipedia[i]['key'])
|
||||||
.update(wikipedia[i]['data'])
|
.update(wikipedia[i]['data'])
|
||||||
.digest('hex');
|
.digest('hex');
|
||||||
assert.strictEqual(wikipedia[i]['hmac'][hash],
|
assert.strictEqual(
|
||||||
result,
|
actual,
|
||||||
`Test HMAC-${hash}: Test case ${i + 1} wikipedia`);
|
expected,
|
||||||
|
`Test HMAC-${hash} wikipedia case ${i + 1}: ${actual} must be ${expected}`
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -224,17 +232,20 @@ for (let i = 0, l = rfc4231.length; i < l; i++) {
|
|||||||
const 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);
|
||||||
let strRes = str.read().toString('hex');
|
let strRes = str.read().toString('hex');
|
||||||
let result = crypto.createHmac(hash, rfc4231[i]['key'])
|
let actual = 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
|
actual = actual.substr(0, 32); // first 128 bits == 32 hex chars
|
||||||
strRes = strRes.substr(0, 32);
|
strRes = strRes.substr(0, 32);
|
||||||
}
|
}
|
||||||
assert.strictEqual(rfc4231[i]['hmac'][hash],
|
const expected = rfc4231[i]['hmac'][hash];
|
||||||
result,
|
assert.strictEqual(
|
||||||
`Test HMAC-${hash}: Test case ${i + 1} rfc 4231`);
|
actual,
|
||||||
assert.strictEqual(strRes, result, 'Should get same result from stream');
|
expected,
|
||||||
|
`Test HMAC-${hash} rfc 4231 case ${i + 1}: ${actual} must be ${expected}`
|
||||||
|
);
|
||||||
|
assert.strictEqual(actual, strRes, 'Should get same result from stream');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -349,22 +360,26 @@ const rfc2202_sha1 = [
|
|||||||
|
|
||||||
if (!common.hasFipsCrypto) {
|
if (!common.hasFipsCrypto) {
|
||||||
for (let i = 0, l = rfc2202_md5.length; i < l; i++) {
|
for (let i = 0, l = rfc2202_md5.length; i < l; i++) {
|
||||||
|
const actual = crypto.createHmac('md5', rfc2202_md5[i]['key'])
|
||||||
|
.update(rfc2202_md5[i]['data'])
|
||||||
|
.digest('hex');
|
||||||
|
const expected = rfc2202_md5[i]['hmac'];
|
||||||
assert.strictEqual(
|
assert.strictEqual(
|
||||||
rfc2202_md5[i]['hmac'],
|
actual,
|
||||||
crypto.createHmac('md5', rfc2202_md5[i]['key'])
|
expected,
|
||||||
.update(rfc2202_md5[i]['data'])
|
`Test HMAC-MD5 rfc 2202 case ${i + 1}: ${actual} must be ${expected}`
|
||||||
.digest('hex'),
|
|
||||||
`Test HMAC-MD5 : Test case ${i + 1} rfc 2202`
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (let i = 0, l = rfc2202_sha1.length; i < l; i++) {
|
for (let i = 0, l = rfc2202_sha1.length; i < l; i++) {
|
||||||
|
const actual = crypto.createHmac('sha1', rfc2202_sha1[i]['key'])
|
||||||
|
.update(rfc2202_sha1[i]['data'])
|
||||||
|
.digest('hex');
|
||||||
|
const expected = rfc2202_sha1[i]['hmac'];
|
||||||
assert.strictEqual(
|
assert.strictEqual(
|
||||||
rfc2202_sha1[i]['hmac'],
|
actual,
|
||||||
crypto.createHmac('sha1', rfc2202_sha1[i]['key'])
|
expected,
|
||||||
.update(rfc2202_sha1[i]['data'])
|
`Test HMAC-SHA1 rfc 2202 case ${i + 1}: ${actual} must be ${expected}`
|
||||||
.digest('hex'),
|
|
||||||
`Test HMAC-SHA1 : Test case ${i + 1} rfc 2202`
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user