Skip to content

Commit

Permalink
test,crypto: ensure promises resolve in webcrypto tests
Browse files Browse the repository at this point in the history
PR-URL: #37653
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
aduh95 authored and danielleadams committed Mar 16, 2021
1 parent 341ee31 commit 5731977
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 81 deletions.
16 changes: 8 additions & 8 deletions test/parallel/test-webcrypto-digest.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,25 +66,25 @@ const kData = (new TextEncoder()).encode('hello');
}));
})().then(common.mustCall());

[1, [], {}, null, undefined].forEach((i) => {
assert.rejects(subtle.digest(i), { message: /Unrecognized name/ });
});
Promise.all([1, [], {}, null, undefined].map((i) =>
assert.rejects(subtle.digest(i), { message: /Unrecognized name/ })
)).then(common.mustCall());

assert.rejects(subtle.digest(''), { message: /Unrecognized name/ });
assert.rejects(subtle.digest(''), { message: /Unrecognized name/ }).then(common.mustCall());

[1, [], {}, null, undefined].forEach((i) => {
Promise.all([1, [], {}, null, undefined].map((i) =>
assert.rejects(subtle.digest('SHA-256', i), {
code: 'ERR_INVALID_ARG_TYPE'
});
});
})
)).then(common.mustCall());

// If there is a mismatch between length and the expected digest size for
// the selected algorithm, we fail. The length is a Node.js specific
// addition to the API, and is added as a support for future additional
// hash algorithms that support variable digest output lengths.
assert.rejects(subtle.digest({ name: 'SHA-512', length: 510 }, kData), {
message: /Digest method not supported/
});
}).then(common.mustCall());

const kSourceData = {
empty: '',
Expand Down
16 changes: 8 additions & 8 deletions test/parallel/test-webcrypto-ed25519-ed448.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ assert.rejects(
['sign']),
{
message: /NODE-ED25519 raw keys must be exactly 32-bytes/
});
}).then(common.mustCall());

assert.rejects(
subtle.importKey(
Expand All @@ -79,7 +79,7 @@ assert.rejects(
['sign']),
{
message: /NODE-ED448 raw keys must be exactly 57-bytes/
});
}).then(common.mustCall());

const testVectors = {
'NODE-ED25519': [
Expand Down Expand Up @@ -331,7 +331,7 @@ assert.rejects(
['sign', 'verify']),
{
message: /Unsupported named curves for ECDSA/
});
}).then(common.mustCall());

assert.rejects(
subtle.generateKey(
Expand All @@ -343,7 +343,7 @@ assert.rejects(
['sign', 'verify']),
{
message: /Unsupported named curves for ECDSA/
});
}).then(common.mustCall());

assert.rejects(
subtle.generateKey(
Expand All @@ -355,7 +355,7 @@ assert.rejects(
['sign', 'verify']),
{
message: /Unsupported named curves for ECDSA/
});
}).then(common.mustCall());

assert.rejects(
subtle.generateKey(
Expand All @@ -367,7 +367,7 @@ assert.rejects(
['sign', 'verify']),
{
message: /Unsupported named curves for ECDSA/
});
}).then(common.mustCall());

{
for (const asymmetricKeyType of ['ed25519', 'ed448']) {
Expand Down Expand Up @@ -398,7 +398,7 @@ assert.rejects(
),
{
message: /Invalid algorithm name/
});
}).then(common.mustCall());

assert.rejects(
subtle.importKey(
Expand All @@ -413,7 +413,7 @@ assert.rejects(
),
{
message: /Invalid algorithm name/
});
}).then(common.mustCall());
}
}
}
126 changes: 65 additions & 61 deletions test/parallel/test-webcrypto-export-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,70 +9,74 @@ const assert = require('assert');
const { subtle, getRandomValues } = require('crypto').webcrypto;

{
const keyData = getRandomValues(new Uint8Array(32));
[1, null, undefined, {}, []].forEach((format) => {
assert.rejects(
subtle.importKey(format, keyData, {}, false, ['wrapKey']), {
code: 'ERR_INVALID_ARG_TYPE'
async function test() {
const keyData = getRandomValues(new Uint8Array(32));
await Promise.all([1, null, undefined, {}, []].map((format) =>
assert.rejects(
subtle.importKey(format, keyData, {}, false, ['wrapKey']), {
code: 'ERR_INVALID_ARG_TYPE'
})
));
await assert.rejects(
subtle.importKey('not valid', keyData, {}, false, ['wrapKey']), {
code: 'ERR_INVALID_ARG_VALUE'
});
});
assert.rejects(
subtle.importKey('not valid', keyData, {}, false, ['wrapKey']), {
code: 'ERR_INVALID_ARG_VALUE'
});
[1, null, undefined, {}, []].forEach((keyData) => {
assert.rejects(
subtle.importKey('raw', keyData, {}, false, ['deriveBits']), {
await Promise.all([1, null, undefined, {}, []].map((keyData) =>
assert.rejects(
subtle.importKey('raw', keyData, {}, false, ['deriveBits']), {
code: 'ERR_INVALID_ARG_TYPE'
})
));
await assert.rejects(
subtle.importKey('raw', keyData, {
name: 'HMAC'
}, false, ['sign', 'verify']), {
code: 'ERR_MISSING_OPTION'
});
await assert.rejects(
subtle.importKey('raw', keyData, {
name: 'HMAC',
hash: 'SHA-256'
}, false, ['deriveBits']), {
name: 'SyntaxError',
message: 'Unsupported key usage for an HMAC key'
});
await assert.rejects(
subtle.importKey('node.keyObject', '', {
name: 'HMAC',
hash: 'SHA-256'
}, false, ['sign', 'verify']), {
code: 'ERR_INVALID_ARG_TYPE'
});
});
assert.rejects(
subtle.importKey('raw', keyData, {
name: 'HMAC'
}, false, ['sign', 'verify']), {
code: 'ERR_MISSING_OPTION'
}).then(common.mustCall());
assert.rejects(
subtle.importKey('raw', keyData, {
name: 'HMAC',
hash: 'SHA-256'
}, false, ['deriveBits']), {
name: 'SyntaxError',
message: 'Unsupported key usage for an HMAC key'
}).then(common.mustCall());
assert.rejects(
subtle.importKey('node.keyObject', '', {
name: 'HMAC',
hash: 'SHA-256'
}, false, ['sign', 'verify']), {
code: 'ERR_INVALID_ARG_TYPE'
}).then(common.mustCall());
assert.rejects(
subtle.importKey('raw', keyData, {
name: 'HMAC',
hash: 'SHA-256',
length: 0
}, false, ['sign', 'verify']), {
name: 'DataError',
message: 'Zero-length key is not supported'
}).then(common.mustCall());
assert.rejects(
subtle.importKey('raw', keyData, {
name: 'HMAC',
hash: 'SHA-256',
length: 1
}, false, ['sign', 'verify']), {
name: 'DataError',
message: 'Invalid key length'
}).then(common.mustCall());
assert.rejects(
subtle.importKey('jwk', null, {
name: 'HMAC',
hash: 'SHA-256',
}, false, ['sign', 'verify']), {
name: 'DataError',
message: 'Invalid JWK keyData'
}).then(common.mustCall());
await assert.rejects(
subtle.importKey('raw', keyData, {
name: 'HMAC',
hash: 'SHA-256',
length: 0
}, false, ['sign', 'verify']), {
name: 'DataError',
message: 'Zero-length key is not supported'
});
await assert.rejects(
subtle.importKey('raw', keyData, {
name: 'HMAC',
hash: 'SHA-256',
length: 1
}, false, ['sign', 'verify']), {
name: 'DataError',
message: 'Invalid key length'
});
await assert.rejects(
subtle.importKey('jwk', null, {
name: 'HMAC',
hash: 'SHA-256',
}, false, ['sign', 'verify']), {
name: 'DataError',
message: 'Invalid JWK keyData'
});
}

test().then(common.mustCall());
}

// Import/Export HMAC Secret Key
Expand Down
8 changes: 4 additions & 4 deletions test/parallel/test-webcrypto-x25519-x448.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ async function importKey(namedCurve, keyData, isPublic = false) {

assert.rejects(importKey('NODE-X25519', Buffer.alloc(10), true), {
message: /NODE-X25519 raw keys must be exactly 32-bytes/
});
}).then(common.mustCall());
assert.rejects(importKey('NODE-X448', Buffer.alloc(10), true), {
message: /NODE-X448 raw keys must be exactly 56-bytes/
});
}).then(common.mustCall());

async function test1(namedCurve) {
const {
Expand Down Expand Up @@ -235,7 +235,7 @@ assert.rejects(
['deriveBits']),
{
message: /Unsupported named curves for ECDH/
});
}).then(common.mustCall());

assert.rejects(
subtle.generateKey(
Expand All @@ -247,7 +247,7 @@ assert.rejects(
['deriveBits']),
{
message: /Unsupported named curves for ECDH/
});
}).then(common.mustCall());

{
// Private JWK import
Expand Down

0 comments on commit 5731977

Please sign in to comment.