Skip to content

Commit

Permalink
test: improve readability of some crypto tests
Browse files Browse the repository at this point in the history
PR-URL: #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>
  • Loading branch information
tniessen authored and MylesBorins committed Jan 24, 2018
1 parent 8b3c233 commit 2e4fa26
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 24 deletions.
4 changes: 1 addition & 3 deletions test/parallel/test-crypto-authenticated.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,9 +343,7 @@ common.expectWarning('Warning', (common.hasFipsCrypto ? [] : [
'deprecated. Valid GCM tag lengths are 4, 8, 12, 13, 14, 15, 16.')
));

for (const i in TEST_CASES) {
const test = TEST_CASES[i];

for (const test of TEST_CASES) {
if (!ciphers.includes(test.algo)) {
common.printSkipMessage(`unsupported ${test.algo} test`);
continue;
Expand Down
39 changes: 18 additions & 21 deletions test/parallel/test-crypto-binary-default.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,18 +216,17 @@ const rfc4231 = [
}
];

for (let i = 0, l = rfc4231.length; i < l; i++) {
for (const hash in rfc4231[i]['hmac']) {
let result = crypto.createHmac(hash, rfc4231[i]['key'])
.update(rfc4231[i]['data'])
for (const testCase of rfc4231) {
for (const hash in testCase.hmac) {
let result = crypto.createHmac(hash, testCase.key)
.update(testCase.data)
.digest('hex');
if (rfc4231[i]['truncate']) {
if (testCase.truncate) {
result = result.substr(0, 32); // first 128 bits == 32 hex chars
}
assert.strictEqual(
rfc4231[i]['hmac'][hash],
result,
`Test HMAC-${hash}: Test case ${i + 1} rfc 4231`
testCase.hmac[hash],
result
);
}
}
Expand Down Expand Up @@ -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(
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`
testCase.hmac,
crypto.createHmac('md5', testCase.key)
.update(testCase.data)
.digest('hex')
);
}
}
for (let i = 0, l = rfc2202_sha1.length; i < l; i++) {
for (const testCase of rfc2202_sha1) {
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`
testCase.hmac,
crypto.createHmac('sha1', testCase.key)
.update(testCase.data)
.digest('hex')
);
}

Expand Down

0 comments on commit 2e4fa26

Please sign in to comment.