@@ -4,7 +4,12 @@ if (!common.hasCrypto)
44 common . skip ( 'missing crypto' ) ;
55
66const 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 ;
813const { createContext } = require ( 'vm' ) ;
914const {
1015 MessageChannel,
@@ -15,6 +20,9 @@ const {
1520
1621function keyToString ( key ) {
1722 let ret ;
23+ if ( key instanceof CryptoKey ) {
24+ key = KeyObject . from ( key ) ;
25+ }
1826 if ( key . type === 'secret' ) {
1927 ret = key . export ( ) . toString ( 'hex' ) ;
2028 } else {
@@ -33,55 +41,59 @@ if (process.env.HAS_STARTED_WORKER) {
3341// Don't use isMainThread to allow running this test inside a worker.
3442process . env . HAS_STARTED_WORKER = 1 ;
3543
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' ] ) ;
4152
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 ]
4455 . map ( ( key ) => [ key , keyToString ( key ) ] ) ;
4556
46- for ( const [ key , repr ] of keys ) {
47- {
57+ for ( const [ key , repr ] of keys ) {
58+ {
4859 // 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 ( ) ;
5361
54- port2 . once ( 'message' , common . mustCall ( ( { key } ) => {
62+ port1 . postMessage ( { key } ) ;
5563 assert . strictEqual ( keyToString ( key ) , repr ) ;
56- } ) ) ;
57- }
5864
59- {
65+ port2 . once ( 'message' , common . mustCall ( ( { key } ) => {
66+ assert . strictEqual ( keyToString ( key ) , repr ) ;
67+ } ) ) ;
68+ }
69+
70+ {
6071 // 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+ }
6879
69- {
80+ {
7081 // 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 ) ) ;
7586
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+ } ) ;
8293
83- port2moved . start ( ) ;
84- port1 . postMessage ( { key } ) ;
85- port1 . close ( ) ;
94+ port2moved . start ( ) ;
95+ port1 . postMessage ( { key } ) ;
96+ port1 . close ( ) ;
97+ }
8698 }
87- }
99+ } ) ( ) . then ( common . mustCall ( ) ) ;
0 commit comments