Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: improve readability of some crypto tests #17904

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions test/parallel/test-crypto-authenticated.js
Original file line number Diff line number Diff line change
@@ -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;
39 changes: 18 additions & 21 deletions test/parallel/test-crypto-binary-default.js
Original file line number Diff line number Diff line change
@@ -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
);
}
}
@@ -341,24 +340,22 @@ const rfc2202_sha1 = [
}
];

for (let i = 0, l = rfc2202_md5.length; i < l; i++) {
if (!common.hasFipsCrypto) {
if (!common.hasFipsCrypto) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI this condition was moved out of the loop.

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')
);
}