test: improve readability of some crypto tests

PR-URL: https://github.com/nodejs/node/pull/17904
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Weijia Wang <starkwang@126.com>
Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
Reviewed-By: Jon Moss <me@jonathanmoss.me>
This commit is contained in:
Tobias Nießen 2017-12-28 19:47:09 +01:00
parent b21e3f0711
commit d67e71e4f7
No known key found for this signature in database
GPG Key ID: 718207F8FD156B70
2 changed files with 19 additions and 24 deletions

View File

@ -343,9 +343,7 @@ common.expectWarning('Warning', (common.hasFipsCrypto ? [] : [
'deprecated. Valid GCM tag lengths are 4, 8, 12, 13, 14, 15, 16.') 'deprecated. Valid GCM tag lengths are 4, 8, 12, 13, 14, 15, 16.')
)); ));
for (const i in TEST_CASES) { for (const test of TEST_CASES) {
const test = TEST_CASES[i];
if (!ciphers.includes(test.algo)) { if (!ciphers.includes(test.algo)) {
common.printSkipMessage(`unsupported ${test.algo} test`); common.printSkipMessage(`unsupported ${test.algo} test`);
continue; continue;

View File

@ -216,18 +216,17 @@ const rfc4231 = [
} }
]; ];
for (let i = 0, l = rfc4231.length; i < l; i++) { for (const testCase of rfc4231) {
for (const hash in rfc4231[i]['hmac']) { for (const hash in testCase.hmac) {
let result = crypto.createHmac(hash, rfc4231[i]['key']) let result = crypto.createHmac(hash, testCase.key)
.update(rfc4231[i]['data']) .update(testCase.data)
.digest('hex'); .digest('hex');
if (rfc4231[i]['truncate']) { if (testCase.truncate) {
result = result.substr(0, 32); // first 128 bits == 32 hex chars result = result.substr(0, 32); // first 128 bits == 32 hex chars
} }
assert.strictEqual( assert.strictEqual(
rfc4231[i]['hmac'][hash], testCase.hmac[hash],
result, result
`Test HMAC-${hash}: Test case ${i + 1} rfc 4231`
); );
} }
} }
@ -341,24 +340,22 @@ const rfc2202_sha1 = [
} }
]; ];
for (let i = 0, l = rfc2202_md5.length; i < l; i++) { if (!common.hasFipsCrypto) {
if (!common.hasFipsCrypto) { for (const testCase of rfc2202_md5) {
assert.strictEqual( assert.strictEqual(
rfc2202_md5[i]['hmac'], testCase.hmac,
crypto.createHmac('md5', rfc2202_md5[i]['key']) crypto.createHmac('md5', testCase.key)
.update(rfc2202_md5[i]['data']) .update(testCase.data)
.digest('hex'), .digest('hex')
`Test HMAC-MD5 : Test case ${i + 1} rfc 2202`
); );
} }
} }
for (let i = 0, l = rfc2202_sha1.length; i < l; i++) { for (const testCase of rfc2202_sha1) {
assert.strictEqual( assert.strictEqual(
rfc2202_sha1[i]['hmac'], testCase.hmac,
crypto.createHmac('sha1', rfc2202_sha1[i]['key']) crypto.createHmac('sha1', testCase.key)
.update(rfc2202_sha1[i]['data']) .update(testCase.data)
.digest('hex'), .digest('hex')
`Test HMAC-SHA1 : Test case ${i + 1} rfc 2202`
); );
} }