11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ci// This test ensures that CryptoKey instances can be correctly 41cb0ef41Sopenharmony_ci// sent to a Worker via postMessage. 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_ciconst common = require('../common'); 71cb0ef41Sopenharmony_ciif (!common.hasCrypto) 81cb0ef41Sopenharmony_ci common.skip('missing crypto'); 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_ciconst assert = require('assert'); 111cb0ef41Sopenharmony_ciconst { subtle } = require('crypto').webcrypto; 121cb0ef41Sopenharmony_ciconst { once } = require('events'); 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_ciconst { 151cb0ef41Sopenharmony_ci Worker, 161cb0ef41Sopenharmony_ci parentPort, 171cb0ef41Sopenharmony_ci} = require('worker_threads'); 181cb0ef41Sopenharmony_ci 191cb0ef41Sopenharmony_ciconst keyData = 201cb0ef41Sopenharmony_ci Buffer.from( 211cb0ef41Sopenharmony_ci '000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f', 'hex'); 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_ciconst sig = '13691a79fb55a0417e4d6699a32f91ad29283fa2c1439865cc0632931f4f48dc'; 241cb0ef41Sopenharmony_ci 251cb0ef41Sopenharmony_ciasync function doSig(key) { 261cb0ef41Sopenharmony_ci const signature = await subtle.sign({ 271cb0ef41Sopenharmony_ci name: 'HMAC' 281cb0ef41Sopenharmony_ci }, key, Buffer.from('some data')); 291cb0ef41Sopenharmony_ci assert.strictEqual(Buffer.from(signature).toString('hex'), sig); 301cb0ef41Sopenharmony_ci} 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_ciif (process.env.HAS_STARTED_WORKER) { 331cb0ef41Sopenharmony_ci return parentPort.once('message', (key) => { 341cb0ef41Sopenharmony_ci assert.strictEqual(key.algorithm.name, 'HMAC'); 351cb0ef41Sopenharmony_ci doSig(key).then(common.mustCall()); 361cb0ef41Sopenharmony_ci }); 371cb0ef41Sopenharmony_ci} 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_ci// Don't use isMainThread to allow running this test inside a worker. 401cb0ef41Sopenharmony_ciprocess.env.HAS_STARTED_WORKER = 1; 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_ci(async function() { 431cb0ef41Sopenharmony_ci const worker = new Worker(__filename); 441cb0ef41Sopenharmony_ci 451cb0ef41Sopenharmony_ci await once(worker, 'online'); 461cb0ef41Sopenharmony_ci 471cb0ef41Sopenharmony_ci const key = await subtle.importKey( 481cb0ef41Sopenharmony_ci 'raw', 491cb0ef41Sopenharmony_ci keyData, 501cb0ef41Sopenharmony_ci { name: 'HMAC', hash: 'SHA-256' }, 511cb0ef41Sopenharmony_ci true, ['sign', 'verify']); 521cb0ef41Sopenharmony_ci 531cb0ef41Sopenharmony_ci worker.postMessage(key); 541cb0ef41Sopenharmony_ci 551cb0ef41Sopenharmony_ci await doSig(key); 561cb0ef41Sopenharmony_ci})().then(common.mustCall()); 57