@@ -4,7 +4,12 @@ if (!common.hasCrypto)
4
4
common . skip ( 'missing crypto' ) ;
5
5
6
6
const assert = require ( 'assert' ) ;
7
- const { createSecretKey, generateKeyPairSync, randomBytes } = require ( 'crypto' ) ;
7
+ const {
8
+ generateKeySync,
9
+ generateKeyPairSync,
10
+ KeyObject,
11
+ } = require ( 'crypto' ) ;
12
+ const { subtle } = globalThis . crypto ;
8
13
const { createContext } = require ( 'vm' ) ;
9
14
const {
10
15
MessageChannel,
@@ -15,6 +20,9 @@ const {
15
20
16
21
function keyToString ( key ) {
17
22
let ret ;
23
+ if ( key instanceof CryptoKey ) {
24
+ key = KeyObject . from ( key ) ;
25
+ }
18
26
if ( key . type === 'secret' ) {
19
27
ret = key . export ( ) . toString ( 'hex' ) ;
20
28
} else {
@@ -33,55 +41,59 @@ if (process.env.HAS_STARTED_WORKER) {
33
41
// Don't use isMainThread to allow running this test inside a worker.
34
42
process . env . HAS_STARTED_WORKER = 1 ;
35
43
36
- // The main thread generates keys and passes them to worker threads.
37
- const secretKey = createSecretKey ( randomBytes ( 32 ) ) ;
38
- const { publicKey, privateKey } = generateKeyPairSync ( 'rsa' , {
39
- modulusLength : 1024
40
- } ) ;
44
+ ( async ( ) => {
45
+ // The main thread generates keys and passes them to worker threads.
46
+ const secretKey = generateKeySync ( 'aes' , { length : 128 } ) ;
47
+ const { publicKey, privateKey } = generateKeyPairSync ( 'rsa' , {
48
+ modulusLength : 1024
49
+ } ) ;
50
+ const cryptoKey = await subtle . generateKey (
51
+ { name : 'AES-CBC' , length : 128 } , false , [ 'encrypt' ] ) ;
41
52
42
- // Get immutable representations of all keys.
43
- const keys = [ secretKey , publicKey , privateKey ]
53
+ // Get immutable representations of all keys.
54
+ const keys = [ secretKey , publicKey , privateKey , cryptoKey ]
44
55
. map ( ( key ) => [ key , keyToString ( key ) ] ) ;
45
56
46
- for ( const [ key , repr ] of keys ) {
47
- {
57
+ for ( const [ key , repr ] of keys ) {
58
+ {
48
59
// Test 1: No context change.
49
- const { port1, port2 } = new MessageChannel ( ) ;
50
-
51
- port1 . postMessage ( { key } ) ;
52
- assert . strictEqual ( keyToString ( key ) , repr ) ;
60
+ const { port1, port2 } = new MessageChannel ( ) ;
53
61
54
- port2 . once ( 'message' , common . mustCall ( ( { key } ) => {
62
+ port1 . postMessage ( { key } ) ;
55
63
assert . strictEqual ( keyToString ( key ) , repr ) ;
56
- } ) ) ;
57
- }
58
64
59
- {
65
+ port2 . once ( 'message' , common . mustCall ( ( { key } ) => {
66
+ assert . strictEqual ( keyToString ( key ) , repr ) ;
67
+ } ) ) ;
68
+ }
69
+
70
+ {
60
71
// Test 2: Across threads.
61
- const worker = new Worker ( __filename ) ;
62
- worker . once ( 'message' , common . mustCall ( ( receivedRepresentation ) => {
63
- assert . strictEqual ( receivedRepresentation , repr ) ;
64
- } ) ) ;
65
- worker . on ( 'disconnect' , ( ) => console . log ( 'disconnect' ) ) ;
66
- worker . postMessage ( { key } ) ;
67
- }
72
+ const worker = new Worker ( __filename ) ;
73
+ worker . once ( 'message' , common . mustCall ( ( receivedRepresentation ) => {
74
+ assert . strictEqual ( receivedRepresentation , repr ) ;
75
+ } ) ) ;
76
+ worker . on ( 'disconnect' , ( ) => console . log ( 'disconnect' ) ) ;
77
+ worker . postMessage ( { key } ) ;
78
+ }
68
79
69
- {
80
+ {
70
81
// Test 3: Across contexts (should not work).
71
- const { port1, port2 } = new MessageChannel ( ) ;
72
- const context = createContext ( ) ;
73
- const port2moved = moveMessagePortToContext ( port2 , context ) ;
74
- assert ( ! ( port2moved instanceof Object ) ) ;
82
+ const { port1, port2 } = new MessageChannel ( ) ;
83
+ const context = createContext ( ) ;
84
+ const port2moved = moveMessagePortToContext ( port2 , context ) ;
85
+ assert ( ! ( port2moved instanceof Object ) ) ;
75
86
76
- // TODO(addaleax): Switch this to a 'messageerror' event once MessagePort
77
- // implements EventTarget fully and in a cross-context manner.
78
- port2moved . onmessageerror = common . mustCall ( ( event ) => {
79
- assert . strictEqual ( event . data . code ,
80
- 'ERR_MESSAGE_TARGET_CONTEXT_UNAVAILABLE' ) ;
81
- } ) ;
87
+ // TODO(addaleax): Switch this to a 'messageerror' event once MessagePort
88
+ // implements EventTarget fully and in a cross-context manner.
89
+ port2moved . onmessageerror = common . mustCall ( ( event ) => {
90
+ assert . strictEqual ( event . data . code ,
91
+ 'ERR_MESSAGE_TARGET_CONTEXT_UNAVAILABLE' ) ;
92
+ } ) ;
82
93
83
- port2moved . start ( ) ;
84
- port1 . postMessage ( { key } ) ;
85
- port1 . close ( ) ;
94
+ port2moved . start ( ) ;
95
+ port1 . postMessage ( { key } ) ;
96
+ port1 . close ( ) ;
97
+ }
86
98
}
87
- }
99
+ } ) ( ) . then ( common . mustCall ( ) ) ;
0 commit comments