Skip to content

Commit 1be3f50

Browse files
sholladaytargos
authored andcommitted
test: improve crypto HMAC test assertions
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>
1 parent 2c44072 commit 1be3f50

File tree

1 file changed

+41
-26
lines changed

1 file changed

+41
-26
lines changed

test/parallel/test-crypto-hmac.js

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,17 @@ const crypto = require('crypto');
1414
assert.throws(() => h.update('hello'), /^TypeError: HmacUpdate fail$/);
1515
}
1616

17-
// Test HMAC
18-
const h1 = crypto.createHmac('sha1', 'Node')
19-
.update('some data')
20-
.update('to hmac')
21-
.digest('hex');
22-
assert.strictEqual(h1, '19fd6e1ba73d9ed2224dd5094a71babe85d9a892', 'test HMAC');
17+
{
18+
// Test HMAC
19+
const actual = crypto.createHmac('sha1', 'Node')
20+
.update('some data')
21+
.update('to hmac')
22+
.digest('hex');
23+
const expected = '19fd6e1ba73d9ed2224dd5094a71babe85d9a892';
24+
assert.strictEqual(actual,
25+
expected,
26+
`Test HMAC: ${actual} must be ${expected}`);
27+
}
2328

2429
// Test HMAC (Wikipedia Test Cases)
2530
const wikipedia = [
@@ -70,12 +75,15 @@ for (let i = 0, l = wikipedia.length; i < l; i++) {
7075
// FIPS does not support MD5.
7176
if (common.hasFipsCrypto && hash === 'md5')
7277
continue;
73-
const result = crypto.createHmac(hash, wikipedia[i]['key'])
78+
const expected = wikipedia[i]['hmac'][hash];
79+
const actual = crypto.createHmac(hash, wikipedia[i]['key'])
7480
.update(wikipedia[i]['data'])
7581
.digest('hex');
76-
assert.strictEqual(wikipedia[i]['hmac'][hash],
77-
result,
78-
`Test HMAC-${hash}: Test case ${i + 1} wikipedia`);
82+
assert.strictEqual(
83+
actual,
84+
expected,
85+
`Test HMAC-${hash} wikipedia case ${i + 1}: ${actual} must be ${expected}`
86+
);
7987
}
8088
}
8189

@@ -232,17 +240,20 @@ for (let i = 0, l = rfc4231.length; i < l; i++) {
232240
const str = crypto.createHmac(hash, rfc4231[i].key);
233241
str.end(rfc4231[i].data);
234242
let strRes = str.read().toString('hex');
235-
let result = crypto.createHmac(hash, rfc4231[i]['key'])
243+
let actual = crypto.createHmac(hash, rfc4231[i]['key'])
236244
.update(rfc4231[i]['data'])
237245
.digest('hex');
238246
if (rfc4231[i]['truncate']) {
239-
result = result.substr(0, 32); // first 128 bits == 32 hex chars
247+
actual = actual.substr(0, 32); // first 128 bits == 32 hex chars
240248
strRes = strRes.substr(0, 32);
241249
}
242-
assert.strictEqual(rfc4231[i]['hmac'][hash],
243-
result,
244-
`Test HMAC-${hash}: Test case ${i + 1} rfc 4231`);
245-
assert.strictEqual(strRes, result, 'Should get same result from stream');
250+
const expected = rfc4231[i]['hmac'][hash];
251+
assert.strictEqual(
252+
actual,
253+
expected,
254+
`Test HMAC-${hash} rfc 4231 case ${i + 1}: ${actual} must be ${expected}`
255+
);
256+
assert.strictEqual(actual, strRes, 'Should get same result from stream');
246257
}
247258
}
248259

@@ -357,22 +368,26 @@ const rfc2202_sha1 = [
357368

358369
if (!common.hasFipsCrypto) {
359370
for (let i = 0, l = rfc2202_md5.length; i < l; i++) {
371+
const actual = crypto.createHmac('md5', rfc2202_md5[i]['key'])
372+
.update(rfc2202_md5[i]['data'])
373+
.digest('hex');
374+
const expected = rfc2202_md5[i]['hmac'];
360375
assert.strictEqual(
361-
rfc2202_md5[i]['hmac'],
362-
crypto.createHmac('md5', rfc2202_md5[i]['key'])
363-
.update(rfc2202_md5[i]['data'])
364-
.digest('hex'),
365-
`Test HMAC-MD5 : Test case ${i + 1} rfc 2202`
376+
actual,
377+
expected,
378+
`Test HMAC-MD5 rfc 2202 case ${i + 1}: ${actual} must be ${expected}`
366379
);
367380
}
368381
}
369382
for (let i = 0, l = rfc2202_sha1.length; i < l; i++) {
383+
const actual = crypto.createHmac('sha1', rfc2202_sha1[i]['key'])
384+
.update(rfc2202_sha1[i]['data'])
385+
.digest('hex');
386+
const expected = rfc2202_sha1[i]['hmac'];
370387
assert.strictEqual(
371-
rfc2202_sha1[i]['hmac'],
372-
crypto.createHmac('sha1', rfc2202_sha1[i]['key'])
373-
.update(rfc2202_sha1[i]['data'])
374-
.digest('hex'),
375-
`Test HMAC-SHA1 : Test case ${i + 1} rfc 2202`
388+
actual,
389+
expected,
390+
`Test HMAC-SHA1 rfc 2202 case ${i + 1}: ${actual} must be ${expected}`
376391
);
377392
}
378393

0 commit comments

Comments
 (0)