Skip to content

Commit 3927c6f

Browse files
tniessenMylesBorins
authored andcommitted
test: improve readability of some crypto tests
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>
1 parent 181b897 commit 3927c6f

File tree

2 files changed

+19
-24
lines changed

2 files changed

+19
-24
lines changed

test/parallel/test-crypto-authenticated.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -343,9 +343,7 @@ common.expectWarning('Warning', (common.hasFipsCrypto ? [] : [
343343
'deprecated. Valid GCM tag lengths are 4, 8, 12, 13, 14, 15, 16.')
344344
));
345345

346-
for (const i in TEST_CASES) {
347-
const test = TEST_CASES[i];
348-
346+
for (const test of TEST_CASES) {
349347
if (!ciphers.includes(test.algo)) {
350348
common.printSkipMessage(`unsupported ${test.algo} test`);
351349
continue;

test/parallel/test-crypto-binary-default.js

+18-21
Original file line numberDiff line numberDiff line change
@@ -216,18 +216,17 @@ const rfc4231 = [
216216
}
217217
];
218218

219-
for (let i = 0, l = rfc4231.length; i < l; i++) {
220-
for (const hash in rfc4231[i]['hmac']) {
221-
let result = crypto.createHmac(hash, rfc4231[i]['key'])
222-
.update(rfc4231[i]['data'])
219+
for (const testCase of rfc4231) {
220+
for (const hash in testCase.hmac) {
221+
let result = crypto.createHmac(hash, testCase.key)
222+
.update(testCase.data)
223223
.digest('hex');
224-
if (rfc4231[i]['truncate']) {
224+
if (testCase.truncate) {
225225
result = result.substr(0, 32); // first 128 bits == 32 hex chars
226226
}
227227
assert.strictEqual(
228-
rfc4231[i]['hmac'][hash],
229-
result,
230-
`Test HMAC-${hash}: Test case ${i + 1} rfc 4231`
228+
testCase.hmac[hash],
229+
result
231230
);
232231
}
233232
}
@@ -341,24 +340,22 @@ const rfc2202_sha1 = [
341340
}
342341
];
343342

344-
for (let i = 0, l = rfc2202_md5.length; i < l; i++) {
345-
if (!common.hasFipsCrypto) {
343+
if (!common.hasFipsCrypto) {
344+
for (const testCase of rfc2202_md5) {
346345
assert.strictEqual(
347-
rfc2202_md5[i]['hmac'],
348-
crypto.createHmac('md5', rfc2202_md5[i]['key'])
349-
.update(rfc2202_md5[i]['data'])
350-
.digest('hex'),
351-
`Test HMAC-MD5 : Test case ${i + 1} rfc 2202`
346+
testCase.hmac,
347+
crypto.createHmac('md5', testCase.key)
348+
.update(testCase.data)
349+
.digest('hex')
352350
);
353351
}
354352
}
355-
for (let i = 0, l = rfc2202_sha1.length; i < l; i++) {
353+
for (const testCase of rfc2202_sha1) {
356354
assert.strictEqual(
357-
rfc2202_sha1[i]['hmac'],
358-
crypto.createHmac('sha1', rfc2202_sha1[i]['key'])
359-
.update(rfc2202_sha1[i]['data'])
360-
.digest('hex'),
361-
`Test HMAC-SHA1 : Test case ${i + 1} rfc 2202`
355+
testCase.hmac,
356+
crypto.createHmac('sha1', testCase.key)
357+
.update(testCase.data)
358+
.digest('hex')
362359
);
363360
}
364361

0 commit comments

Comments
 (0)