test,crypto: ensure promises resolve in webcrypto tests

PR-URL: https://github.com/nodejs/node/pull/37653
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Antoine du Hamel committed 2021-03-13 10:37:26 +01:00
1 parent e76d762ef4
commit dc8d54d365
4 files changed
+85 -81

No files matched your search

+8 -8
View File
@@ -66,17 +66,17 @@ 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
@@ -84,7 +84,7 @@ assert.rejects(subtle.digest(''), { message: /Unrecognized name/ });
// 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: '',
@@ -65,7 +65,7 @@ assert.rejects(
['sign']),
{
message: /NODE-ED25519 raw keys must be exactly 32-bytes/
});
}).then(common.mustCall());
assert.rejects(
subtle.importKey(
@@ -79,7 +79,7 @@ assert.rejects(
['sign']),
{
message: /NODE-ED448 raw keys must be exactly 57-bytes/
});
}).then(common.mustCall());
const testVectors = {
'NODE-ED25519': [
@@ -331,7 +331,7 @@ assert.rejects(
['sign', 'verify']),
{
message: /Unsupported named curves for ECDSA/
});
}).then(common.mustCall());
assert.rejects(
subtle.generateKey(
@@ -343,7 +343,7 @@ assert.rejects(
['sign', 'verify']),
{
message: /Unsupported named curves for ECDSA/
});
}).then(common.mustCall());
assert.rejects(
subtle.generateKey(
@@ -355,7 +355,7 @@ assert.rejects(
['sign', 'verify']),
{
message: /Unsupported named curves for ECDSA/
});
}).then(common.mustCall());
assert.rejects(
subtle.generateKey(
@@ -367,7 +367,7 @@ assert.rejects(
['sign', 'verify']),
{
message: /Unsupported named curves for ECDSA/
});
}).then(common.mustCall());
{
for (const asymmetricKeyType of ['ed25519', 'ed448']) {
@@ -398,7 +398,7 @@ assert.rejects(
),
{
message: /Invalid algorithm name/
});
}).then(common.mustCall());
assert.rejects(
subtle.importKey(
@@ -413,7 +413,7 @@ assert.rejects(
),
{
message: /Invalid algorithm name/
});
}).then(common.mustCall());
}
}
}
+65 -61
View File
@@ -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']), {
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'
});
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('not valid', keyData, {}, false, ['wrapKey']), {
code: 'ERR_INVALID_ARG_VALUE'
});
[1, null, undefined, {}, []].forEach((keyData) => {
assert.rejects(
subtle.importKey('raw', keyData, {}, false, ['deriveBits']), {
code: 'ERR_INVALID_ARG_TYPE'
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'
});
});
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: 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
+4 -4
View File
@@ -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 {
@@ -235,7 +235,7 @@ assert.rejects(
['deriveBits']),
{
message: /Unsupported named curves for ECDH/
});
}).then(common.mustCall());
assert.rejects(
subtle.generateKey(
@@ -247,7 +247,7 @@ assert.rejects(
['deriveBits']),
{
message: /Unsupported named curves for ECDH/
});
}).then(common.mustCall());
{
// Private JWK import