Skip to content

Commit

Permalink
test: improve crypto HMAC test assertions
Browse files Browse the repository at this point in the history
Fixes argument order for assertions and makes their failure messages
more descriptive and easier to debug.

PR-URL: #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>
  • Loading branch information
sholladay authored and targos committed Oct 18, 2017
1 parent 2c44072 commit 1be3f50
Showing 1 changed file with 41 additions and 26 deletions.
67 changes: 41 additions & 26 deletions test/parallel/test-crypto-hmac.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@ const crypto = require('crypto');
assert.throws(() => h.update('hello'), /^TypeError: HmacUpdate fail$/);
}

// Test HMAC
const h1 = crypto.createHmac('sha1', 'Node')
.update('some data')
.update('to hmac')
.digest('hex');
assert.strictEqual(h1, '19fd6e1ba73d9ed2224dd5094a71babe85d9a892', 'test HMAC');
{
// Test HMAC
const actual = crypto.createHmac('sha1', 'Node')
.update('some data')
.update('to hmac')
.digest('hex');
const expected = '19fd6e1ba73d9ed2224dd5094a71babe85d9a892';
assert.strictEqual(actual,
expected,
`Test HMAC: ${actual} must be ${expected}`);
}

// Test HMAC (Wikipedia Test Cases)
const wikipedia = [
Expand Down Expand Up @@ -70,12 +75,15 @@ for (let i = 0, l = wikipedia.length; i < l; i++) {
// FIPS does not support MD5.
if (common.hasFipsCrypto && hash === 'md5')
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'])
.digest('hex');
assert.strictEqual(wikipedia[i]['hmac'][hash],
result,
`Test HMAC-${hash}: Test case ${i + 1} wikipedia`);
assert.strictEqual(
actual,
expected,
`Test HMAC-${hash} wikipedia case ${i + 1}: ${actual} must be ${expected}`
);
}
}

Expand Down Expand Up @@ -232,17 +240,20 @@ for (let i = 0, l = rfc4231.length; i < l; i++) {
const str = crypto.createHmac(hash, rfc4231[i].key);
str.end(rfc4231[i].data);
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'])
.digest('hex');
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);
}
assert.strictEqual(rfc4231[i]['hmac'][hash],
result,
`Test HMAC-${hash}: Test case ${i + 1} rfc 4231`);
assert.strictEqual(strRes, result, 'Should get same result from stream');
const expected = rfc4231[i]['hmac'][hash];
assert.strictEqual(
actual,
expected,
`Test HMAC-${hash} rfc 4231 case ${i + 1}: ${actual} must be ${expected}`
);
assert.strictEqual(actual, strRes, 'Should get same result from stream');
}
}

Expand Down Expand Up @@ -357,22 +368,26 @@ const rfc2202_sha1 = [

if (!common.hasFipsCrypto) {
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(
rfc2202_md5[i]['hmac'],
crypto.createHmac('md5', rfc2202_md5[i]['key'])
.update(rfc2202_md5[i]['data'])
.digest('hex'),
`Test HMAC-MD5 : Test case ${i + 1} rfc 2202`
actual,
expected,
`Test HMAC-MD5 rfc 2202 case ${i + 1}: ${actual} must be ${expected}`
);
}
}
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(
rfc2202_sha1[i]['hmac'],
crypto.createHmac('sha1', rfc2202_sha1[i]['key'])
.update(rfc2202_sha1[i]['data'])
.digest('hex'),
`Test HMAC-SHA1 : Test case ${i + 1} rfc 2202`
actual,
expected,
`Test HMAC-SHA1 rfc 2202 case ${i + 1}: ${actual} must be ${expected}`
);
}

Expand Down

0 comments on commit 1be3f50

Please sign in to comment.