@@ -9,70 +9,74 @@ const assert = require('assert');
9
9
const { subtle, getRandomValues } = require ( 'crypto' ) . webcrypto ;
10
10
11
11
{
12
- const keyData = getRandomValues ( new Uint8Array ( 32 ) ) ;
13
- [ 1 , null , undefined , { } , [ ] ] . forEach ( ( format ) => {
14
- assert . rejects (
15
- subtle . importKey ( format , keyData , { } , false , [ 'wrapKey' ] ) , {
16
- code : 'ERR_INVALID_ARG_TYPE'
12
+ async function test ( ) {
13
+ const keyData = getRandomValues ( new Uint8Array ( 32 ) ) ;
14
+ await Promise . all ( [ 1 , null , undefined , { } , [ ] ] . map ( ( format ) =>
15
+ assert . rejects (
16
+ subtle . importKey ( format , keyData , { } , false , [ 'wrapKey' ] ) , {
17
+ code : 'ERR_INVALID_ARG_TYPE'
18
+ } )
19
+ ) ) ;
20
+ await assert . rejects (
21
+ subtle . importKey ( 'not valid' , keyData , { } , false , [ 'wrapKey' ] ) , {
22
+ code : 'ERR_INVALID_ARG_VALUE'
17
23
} ) ;
18
- } ) ;
19
- assert . rejects (
20
- subtle . importKey ( 'not valid' , keyData , { } , false , [ 'wrapKey' ] ) , {
21
- code : 'ERR_INVALID_ARG_VALUE'
22
- } ) ;
23
- [ 1 , null , undefined , { } , [ ] ] . forEach ( ( keyData ) => {
24
- assert . rejects (
25
- subtle . importKey ( 'raw' , keyData , { } , false , [ 'deriveBits' ] ) , {
24
+ await Promise . all ( [ 1 , null , undefined , { } , [ ] ] . map ( ( keyData ) =>
25
+ assert . rejects (
26
+ subtle . importKey ( 'raw' , keyData , { } , false , [ 'deriveBits' ] ) , {
27
+ code : 'ERR_INVALID_ARG_TYPE'
28
+ } )
29
+ ) ) ;
30
+ await assert . rejects (
31
+ subtle . importKey ( 'raw' , keyData , {
32
+ name : 'HMAC'
33
+ } , false , [ 'sign' , 'verify' ] ) , {
34
+ code : 'ERR_MISSING_OPTION'
35
+ } ) ;
36
+ await assert . rejects (
37
+ subtle . importKey ( 'raw' , keyData , {
38
+ name : 'HMAC' ,
39
+ hash : 'SHA-256'
40
+ } , false , [ 'deriveBits' ] ) , {
41
+ name : 'SyntaxError' ,
42
+ message : 'Unsupported key usage for an HMAC key'
43
+ } ) ;
44
+ await assert . rejects (
45
+ subtle . importKey ( 'node.keyObject' , '' , {
46
+ name : 'HMAC' ,
47
+ hash : 'SHA-256'
48
+ } , false , [ 'sign' , 'verify' ] ) , {
26
49
code : 'ERR_INVALID_ARG_TYPE'
27
50
} ) ;
28
- } ) ;
29
- assert . rejects (
30
- subtle . importKey ( 'raw' , keyData , {
31
- name : 'HMAC'
32
- } , false , [ 'sign' , 'verify' ] ) , {
33
- code : 'ERR_MISSING_OPTION'
34
- } ) . then ( common . mustCall ( ) ) ;
35
- assert . rejects (
36
- subtle . importKey ( 'raw' , keyData , {
37
- name : 'HMAC' ,
38
- hash : 'SHA-256'
39
- } , false , [ 'deriveBits' ] ) , {
40
- name : 'SyntaxError' ,
41
- message : 'Unsupported key usage for an HMAC key'
42
- } ) . then ( common . mustCall ( ) ) ;
43
- assert . rejects (
44
- subtle . importKey ( 'node.keyObject' , '' , {
45
- name : 'HMAC' ,
46
- hash : 'SHA-256'
47
- } , false , [ 'sign' , 'verify' ] ) , {
48
- code : 'ERR_INVALID_ARG_TYPE'
49
- } ) . then ( common . mustCall ( ) ) ;
50
- assert . rejects (
51
- subtle . importKey ( 'raw' , keyData , {
52
- name : 'HMAC' ,
53
- hash : 'SHA-256' ,
54
- length : 0
55
- } , false , [ 'sign' , 'verify' ] ) , {
56
- name : 'DataError' ,
57
- message : 'Zero-length key is not supported'
58
- } ) . then ( common . mustCall ( ) ) ;
59
- assert . rejects (
60
- subtle . importKey ( 'raw' , keyData , {
61
- name : 'HMAC' ,
62
- hash : 'SHA-256' ,
63
- length : 1
64
- } , false , [ 'sign' , 'verify' ] ) , {
65
- name : 'DataError' ,
66
- message : 'Invalid key length'
67
- } ) . then ( common . mustCall ( ) ) ;
68
- assert . rejects (
69
- subtle . importKey ( 'jwk' , null , {
70
- name : 'HMAC' ,
71
- hash : 'SHA-256' ,
72
- } , false , [ 'sign' , 'verify' ] ) , {
73
- name : 'DataError' ,
74
- message : 'Invalid JWK keyData'
75
- } ) . then ( common . mustCall ( ) ) ;
51
+ await assert . rejects (
52
+ subtle . importKey ( 'raw' , keyData , {
53
+ name : 'HMAC' ,
54
+ hash : 'SHA-256' ,
55
+ length : 0
56
+ } , false , [ 'sign' , 'verify' ] ) , {
57
+ name : 'DataError' ,
58
+ message : 'Zero-length key is not supported'
59
+ } ) ;
60
+ await assert . rejects (
61
+ subtle . importKey ( 'raw' , keyData , {
62
+ name : 'HMAC' ,
63
+ hash : 'SHA-256' ,
64
+ length : 1
65
+ } , false , [ 'sign' , 'verify' ] ) , {
66
+ name : 'DataError' ,
67
+ message : 'Invalid key length'
68
+ } ) ;
69
+ await assert . rejects (
70
+ subtle . importKey ( 'jwk' , null , {
71
+ name : 'HMAC' ,
72
+ hash : 'SHA-256' ,
73
+ } , false , [ 'sign' , 'verify' ] ) , {
74
+ name : 'DataError' ,
75
+ message : 'Invalid JWK keyData'
76
+ } ) ;
77
+ }
78
+
79
+ test ( ) . then ( common . mustCall ( ) ) ;
76
80
}
77
81
78
82
// Import/Export HMAC Secret Key
0 commit comments